mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-13 11:25:19 +00:00
PodSecurityPolicy should respect and validate user-supplied RunAsNonRoot fields.
This commit is contained in:
@@ -112,76 +112,86 @@ func TestCreatePodSecurityContextNonmutating(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateContainerSecurityContextNonmutating(t *testing.T) {
|
||||
// Create a pod with a security context that needs filling in
|
||||
createPod := func() *api.Pod {
|
||||
return &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{{
|
||||
SecurityContext: &api.SecurityContext{},
|
||||
}},
|
||||
},
|
||||
untrue := false
|
||||
tests := []struct {
|
||||
security *api.SecurityContext
|
||||
}{
|
||||
{nil},
|
||||
{&api.SecurityContext{RunAsNonRoot: &untrue}},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
// Create a pod with a security context that needs filling in
|
||||
createPod := func() *api.Pod {
|
||||
return &api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{{
|
||||
SecurityContext: tc.security,
|
||||
}},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create a PSP with strategies that will populate a blank security context
|
||||
createPSP := func() *extensions.PodSecurityPolicy {
|
||||
uid := types.UnixUserID(1)
|
||||
return &extensions.PodSecurityPolicy{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "psp-sa",
|
||||
Annotations: map[string]string{
|
||||
seccomp.AllowedProfilesAnnotationKey: "*",
|
||||
seccomp.DefaultProfileAnnotationKey: "foo",
|
||||
// Create a PSP with strategies that will populate a blank security context
|
||||
createPSP := func() *extensions.PodSecurityPolicy {
|
||||
uid := types.UnixUserID(1)
|
||||
return &extensions.PodSecurityPolicy{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "psp-sa",
|
||||
Annotations: map[string]string{
|
||||
seccomp.AllowedProfilesAnnotationKey: "*",
|
||||
seccomp.DefaultProfileAnnotationKey: "foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
Spec: extensions.PodSecurityPolicySpec{
|
||||
DefaultAddCapabilities: []api.Capability{"foo"},
|
||||
RequiredDropCapabilities: []api.Capability{"bar"},
|
||||
RunAsUser: extensions.RunAsUserStrategyOptions{
|
||||
Rule: extensions.RunAsUserStrategyMustRunAs,
|
||||
Ranges: []extensions.UserIDRange{{Min: uid, Max: uid}},
|
||||
Spec: extensions.PodSecurityPolicySpec{
|
||||
DefaultAddCapabilities: []api.Capability{"foo"},
|
||||
RequiredDropCapabilities: []api.Capability{"bar"},
|
||||
RunAsUser: extensions.RunAsUserStrategyOptions{
|
||||
Rule: extensions.RunAsUserStrategyMustRunAs,
|
||||
Ranges: []extensions.UserIDRange{{Min: uid, Max: uid}},
|
||||
},
|
||||
SELinux: extensions.SELinuxStrategyOptions{
|
||||
Rule: extensions.SELinuxStrategyMustRunAs,
|
||||
SELinuxOptions: &api.SELinuxOptions{User: "you"},
|
||||
},
|
||||
// these are pod mutating strategies that are tested above
|
||||
FSGroup: extensions.FSGroupStrategyOptions{
|
||||
Rule: extensions.FSGroupStrategyRunAsAny,
|
||||
},
|
||||
SupplementalGroups: extensions.SupplementalGroupsStrategyOptions{
|
||||
Rule: extensions.SupplementalGroupsStrategyRunAsAny,
|
||||
},
|
||||
// mutates the container SC by defaulting to true if container sets nil
|
||||
ReadOnlyRootFilesystem: true,
|
||||
},
|
||||
SELinux: extensions.SELinuxStrategyOptions{
|
||||
Rule: extensions.SELinuxStrategyMustRunAs,
|
||||
SELinuxOptions: &api.SELinuxOptions{User: "you"},
|
||||
},
|
||||
// these are pod mutating strategies that are tested above
|
||||
FSGroup: extensions.FSGroupStrategyOptions{
|
||||
Rule: extensions.FSGroupStrategyRunAsAny,
|
||||
},
|
||||
SupplementalGroups: extensions.SupplementalGroupsStrategyOptions{
|
||||
Rule: extensions.SupplementalGroupsStrategyRunAsAny,
|
||||
},
|
||||
// mutates the container SC by defaulting to true if container sets nil
|
||||
ReadOnlyRootFilesystem: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pod := createPod()
|
||||
psp := createPSP()
|
||||
pod := createPod()
|
||||
psp := createPSP()
|
||||
|
||||
provider, err := NewSimpleProvider(psp, "namespace", NewSimpleStrategyFactory())
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create provider %v", err)
|
||||
}
|
||||
sc, _, err := provider.CreateContainerSecurityContext(pod, &pod.Spec.Containers[0])
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create container security context %v", err)
|
||||
}
|
||||
provider, err := NewSimpleProvider(psp, "namespace", NewSimpleStrategyFactory())
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create provider %v", err)
|
||||
}
|
||||
sc, _, err := provider.CreateContainerSecurityContext(pod, &pod.Spec.Containers[0])
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create container security context %v", err)
|
||||
}
|
||||
|
||||
// The generated security context should have filled in missing options, so they should differ
|
||||
if reflect.DeepEqual(sc, &pod.Spec.Containers[0].SecurityContext) {
|
||||
t.Error("expected created security context to be different than container's, but they were identical")
|
||||
}
|
||||
// The generated security context should have filled in missing options, so they should differ
|
||||
if reflect.DeepEqual(sc, &pod.Spec.Containers[0].SecurityContext) {
|
||||
t.Error("expected created security context to be different than container's, but they were identical")
|
||||
}
|
||||
|
||||
// Creating the provider or the security context should not have mutated the psp or pod
|
||||
if !reflect.DeepEqual(createPod(), pod) {
|
||||
diffs := diff.ObjectDiff(createPod(), pod)
|
||||
t.Errorf("pod was mutated by CreateContainerSecurityContext. diff:\n%s", diffs)
|
||||
}
|
||||
if !reflect.DeepEqual(createPSP(), psp) {
|
||||
t.Error("psp was mutated by CreateContainerSecurityContext")
|
||||
// Creating the provider or the security context should not have mutated the psp or pod
|
||||
if !reflect.DeepEqual(createPod(), pod) {
|
||||
diffs := diff.ObjectDiff(createPod(), pod)
|
||||
t.Errorf("pod was mutated by CreateContainerSecurityContext. diff:\n%s", diffs)
|
||||
}
|
||||
if !reflect.DeepEqual(createPSP(), psp) {
|
||||
t.Error("psp was mutated by CreateContainerSecurityContext")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user