Skip to content

Commit de4e04b

Browse files
jpbetzlalitc375
authored andcommitted
Fix NPE in CEl accessors of additionalProperties=true objects
1 parent e95f580 commit de4e04b

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎staging/src/k8s.io/apiserver/pkg/admission/plugin/policy/validating/typechecking_test.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,26 @@ func TestTypeCheck(t *testing.T) {
297297
},
298298
}},
299299
}}
300+
301+
reproducerPolicy := &v1.ValidatingAdmissionPolicy{Spec: v1.ValidatingAdmissionPolicySpec{
302+
Validations: []v1.Validation{
303+
{
304+
Expression: "has(object.spec.text)",
305+
},
306+
},
307+
MatchConstraints: &v1.MatchResources{ResourceRules: []v1.NamedRuleWithOperations{
308+
{
309+
RuleWithOperations: v1.RuleWithOperations{
310+
Rule: v1.Rule{
311+
APIGroups: []string{"example.com"},
312+
APIVersions: []string{"v1alpha1"},
313+
Resources: []string{"reproducers"},
314+
},
315+
},
316+
},
317+
}},
318+
}}
319+
300320
for _, tc := range []struct {
301321
name string
302322
schemaToReturn *spec.Schema
@@ -437,6 +457,39 @@ func TestTypeCheck(t *testing.T) {
437457
toContain("found no matching overload for 'allowed' applied to 'kubernetes.authorization.Authorizer"),
438458
},
439459
},
460+
{
461+
name: "additionalProperties: true",
462+
policy: reproducerPolicy,
463+
schemaToReturn: &spec.Schema{
464+
SchemaProps: spec.SchemaProps{
465+
Type: []string{"object"},
466+
Properties: map[string]spec.Schema{
467+
"spec": {
468+
SchemaProps: spec.SchemaProps{
469+
Type: []string{"object"},
470+
Properties: map[string]spec.Schema{
471+
"text": *spec.StringProperty(),
472+
},
473+
},
474+
},
475+
"status": {
476+
SchemaProps: spec.SchemaProps{
477+
Type: []string{"object"},
478+
Properties: map[string]spec.Schema{
479+
"problematicProperty": {
480+
SchemaProps: spec.SchemaProps{
481+
Type: []string{"object"},
482+
AdditionalProperties: &spec.SchemaOrBool{Allows: true},
483+
},
484+
},
485+
},
486+
},
487+
},
488+
},
489+
},
490+
},
491+
assertions: []assertionFunc{toBeEmpty},
492+
},
440493
{
441494
name: "variables valid",
442495
policy: &v1.ValidatingAdmissionPolicy{Spec: v1.ValidatingAdmissionPolicySpec{

‎staging/src/k8s.io/apiserver/pkg/cel/openapi/adaptor.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type SchemaOrBool struct {
3636
}
3737

3838
func (sb *SchemaOrBool) Schema() common.Schema {
39+
if sb.SchemaOrBool.Schema == nil {
40+
return nil
41+
}
3942
return &Schema{Schema: sb.SchemaOrBool.Schema}
4043
}
4144

‎staging/src/k8s.io/apiserver/pkg/cel/openapi/schemas_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,33 @@ import (
2929
)
3030

3131
func TestSchemaDeclType(t *testing.T) {
32+
t.Run("a schema with additionalProperties: true should be an valid object type", func(t *testing.T) {
33+
schema := &spec.Schema{
34+
SchemaProps: spec.SchemaProps{
35+
Type: []string{"object"},
36+
Properties: map[string]spec.Schema{
37+
"foo": {
38+
SchemaProps: spec.SchemaProps{
39+
Type: []string{"object"},
40+
AdditionalProperties: &spec.SchemaOrBool{Allows: true},
41+
},
42+
},
43+
},
44+
},
45+
}
46+
declType := SchemaDeclType(schema, false)
47+
if declType == nil {
48+
t.Fatal("SchemaDeclType returned nil")
49+
}
50+
fooField, found := declType.FindField("foo")
51+
if !found {
52+
t.Fatal("field 'foo' not found")
53+
}
54+
fooType := fooField.Type
55+
if fooType.TypeName() != "object" {
56+
t.Fatalf("expected foo to be a object, but got %s", fooType.TypeName())
57+
}
58+
})
3259
ts := testSchema()
3360
cust := SchemaDeclType(ts, false)
3461
if cust.TypeName() != "object" {

0 commit comments

Comments
 (0)