mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-17 11:28:58 +00:00
Fix NPE in CEl accessors of additionalProperties=true objects
This commit is contained in:
@@ -297,6 +297,26 @@ func TestTypeCheck(t *testing.T) {
|
||||
},
|
||||
}},
|
||||
}}
|
||||
|
||||
reproducerPolicy := &v1.ValidatingAdmissionPolicy{Spec: v1.ValidatingAdmissionPolicySpec{
|
||||
Validations: []v1.Validation{
|
||||
{
|
||||
Expression: "has(object.spec.text)",
|
||||
},
|
||||
},
|
||||
MatchConstraints: &v1.MatchResources{ResourceRules: []v1.NamedRuleWithOperations{
|
||||
{
|
||||
RuleWithOperations: v1.RuleWithOperations{
|
||||
Rule: v1.Rule{
|
||||
APIGroups: []string{"example.com"},
|
||||
APIVersions: []string{"v1alpha1"},
|
||||
Resources: []string{"reproducers"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
}}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
schemaToReturn *spec.Schema
|
||||
@@ -437,6 +457,39 @@ func TestTypeCheck(t *testing.T) {
|
||||
toContain("found no matching overload for 'allowed' applied to 'kubernetes.authorization.Authorizer"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "additionalProperties: true",
|
||||
policy: reproducerPolicy,
|
||||
schemaToReturn: &spec.Schema{
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Type: []string{"object"},
|
||||
Properties: map[string]spec.Schema{
|
||||
"spec": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Type: []string{"object"},
|
||||
Properties: map[string]spec.Schema{
|
||||
"text": *spec.StringProperty(),
|
||||
},
|
||||
},
|
||||
},
|
||||
"status": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Type: []string{"object"},
|
||||
Properties: map[string]spec.Schema{
|
||||
"problematicProperty": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Type: []string{"object"},
|
||||
AdditionalProperties: &spec.SchemaOrBool{Allows: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
assertions: []assertionFunc{toBeEmpty},
|
||||
},
|
||||
{
|
||||
name: "variables valid",
|
||||
policy: &v1.ValidatingAdmissionPolicy{Spec: v1.ValidatingAdmissionPolicySpec{
|
||||
|
||||
@@ -36,6 +36,9 @@ type SchemaOrBool struct {
|
||||
}
|
||||
|
||||
func (sb *SchemaOrBool) Schema() common.Schema {
|
||||
if sb.SchemaOrBool.Schema == nil {
|
||||
return nil
|
||||
}
|
||||
return &Schema{Schema: sb.SchemaOrBool.Schema}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,33 @@ import (
|
||||
)
|
||||
|
||||
func TestSchemaDeclType(t *testing.T) {
|
||||
t.Run("a schema with additionalProperties: true should be an valid object type", func(t *testing.T) {
|
||||
schema := &spec.Schema{
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Type: []string{"object"},
|
||||
Properties: map[string]spec.Schema{
|
||||
"foo": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Type: []string{"object"},
|
||||
AdditionalProperties: &spec.SchemaOrBool{Allows: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
declType := SchemaDeclType(schema, false)
|
||||
if declType == nil {
|
||||
t.Fatal("SchemaDeclType returned nil")
|
||||
}
|
||||
fooField, found := declType.FindField("foo")
|
||||
if !found {
|
||||
t.Fatal("field 'foo' not found")
|
||||
}
|
||||
fooType := fooField.Type
|
||||
if fooType.TypeName() != "object" {
|
||||
t.Fatalf("expected foo to be a object, but got %s", fooType.TypeName())
|
||||
}
|
||||
})
|
||||
ts := testSchema()
|
||||
cust := SchemaDeclType(ts, false)
|
||||
if cust.TypeName() != "object" {
|
||||
|
||||
Reference in New Issue
Block a user