Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCC can't be patched via JSONPatch because users is nil #17185

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api/swagger-spec/api-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -22774,7 +22774,9 @@
"allowHostPorts",
"allowHostPID",
"allowHostIPC",
"readOnlyRootFilesystem"
"readOnlyRootFilesystem",
"users",
"groups"
],
"properties": {
"kind": {
Expand Down
7 changes: 7 additions & 0 deletions pkg/security/apis/security/v1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ func SetDefaults_SCC(scc *SecurityContextConstraints) {
scc.SupplementalGroups.Type = SupplementalGroupsStrategyRunAsAny
}

if scc.Users == nil {
scc.Users = []string{}
}
if scc.Groups == nil {
scc.Groups = []string{}
}

var defaultAllowedVolumes sets.String
switch {
case scc.Volumes == nil:
Expand Down
2 changes: 2 additions & 0 deletions pkg/security/apis/security/v1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pkg/security/apis/security/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ type SecurityContextConstraints struct {
ReadOnlyRootFilesystem bool `json:"readOnlyRootFilesystem" protobuf:"varint,17,opt,name=readOnlyRootFilesystem"`

// The users who have permissions to use this security context constraints
Users []string `json:"users,omitempty" protobuf:"bytes,18,rep,name=users"`
// +optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surprised this didn't regenerate the proto files

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, it did. surprised that didn't regenerate the fileDescriptorGenerated blob

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated strips comments (last I checked)

Users []string `json:"users" protobuf:"bytes,18,rep,name=users"`
// The groups that have permission to use this security context constraints
Groups []string `json:"groups,omitempty" protobuf:"bytes,19,rep,name=groups"`
// +optional
Groups []string `json:"groups" protobuf:"bytes,19,rep,name=groups"`

// SeccompProfiles lists the allowed profiles that may be set for the pod or
// container's seccomp annotations. An unset (nil) or empty value means that no profiles may
Expand Down
12 changes: 10 additions & 2 deletions pkg/security/apis/security/v1/zz_generated.conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,16 @@ func autoConvert_security_SecurityContextConstraints_To_v1_SecurityContextConstr
}
out.ReadOnlyRootFilesystem = in.ReadOnlyRootFilesystem
out.SeccompProfiles = *(*[]string)(unsafe.Pointer(&in.SeccompProfiles))
out.Users = *(*[]string)(unsafe.Pointer(&in.Users))
out.Groups = *(*[]string)(unsafe.Pointer(&in.Groups))
if in.Users == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated files in their own commit?

out.Users = make([]string, 0)
} else {
out.Users = *(*[]string)(unsafe.Pointer(&in.Users))
}
if in.Groups == nil {
out.Groups = make([]string, 0)
} else {
out.Groups = *(*[]string)(unsafe.Pointer(&in.Groups))
}
return nil
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/security/registry/securitycontextconstraints/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,27 @@ func (strategy) PrepareForCreate(_ genericapirequest.Context, obj runtime.Object
func (strategy) PrepareForUpdate(_ genericapirequest.Context, obj, old runtime.Object) {
}

// Canonicalize removes duplicate user and group values, preserving order.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to preserve order, or do you just need consistent order? If you only need the latter, you could just use StringKeySet.List which would sort and dedupe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order is better in case you have a patch that removes a specific index

func (strategy) Canonicalize(obj runtime.Object) {
scc := obj.(*securityapi.SecurityContextConstraints)
scc.Users = uniqueStrings(scc.Users)
scc.Groups = uniqueStrings(scc.Groups)
}

func uniqueStrings(values []string) []string {
if len(values) < 2 {
return values
}
updated := make([]string, 0, len(values))
existing := make(map[string]struct{})
for _, value := range values {
if _, ok := existing[value]; ok {
continue
}
existing[value] = struct{}{}
updated = append(updated, value)
}
return updated
}

func (strategy) Validate(ctx genericapirequest.Context, obj runtime.Object) field.ErrorList {
Expand Down
54 changes: 54 additions & 0 deletions pkg/security/registry/securitycontextconstraints/strategy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package securitycontextconstraints

import (
"reflect"
"testing"

securityapi "github.com/openshift/origin/pkg/security/apis/security"
)

func TestCanonicalize(t *testing.T) {
testCases := []struct {
obj *securityapi.SecurityContextConstraints
expect *securityapi.SecurityContextConstraints
}{
{
obj: &securityapi.SecurityContextConstraints{},
expect: &securityapi.SecurityContextConstraints{},
},
{
obj: &securityapi.SecurityContextConstraints{
Users: []string{"a"},
},
expect: &securityapi.SecurityContextConstraints{
Users: []string{"a"},
},
},
{
obj: &securityapi.SecurityContextConstraints{
Users: []string{"a", "a"},
Groups: []string{"b", "b"},
},
expect: &securityapi.SecurityContextConstraints{
Users: []string{"a"},
Groups: []string{"b"},
},
},
{
obj: &securityapi.SecurityContextConstraints{
Users: []string{"a", "b", "a"},
Groups: []string{"c", "d", "c"},
},
expect: &securityapi.SecurityContextConstraints{
Users: []string{"a", "b"},
Groups: []string{"c", "d"},
},
},
}
for i, testCase := range testCases {
Strategy.Canonicalize(testCase.obj)
if !reflect.DeepEqual(testCase.expect, testCase.obj) {
t.Errorf("%d: unexpected object: %#v", i, testCase.obj)
}
}
}