123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- /*
- Copyright 2018 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package podsecuritypolicy
- import (
- "fmt"
- "reflect"
- "testing"
- "github.com/stretchr/testify/assert"
- "k8s.io/apimachinery/pkg/util/diff"
- utilfeature "k8s.io/apiserver/pkg/util/feature"
- featuregatetesting "k8s.io/component-base/featuregate/testing"
- api "k8s.io/kubernetes/pkg/apis/core"
- "k8s.io/kubernetes/pkg/apis/policy"
- "k8s.io/kubernetes/pkg/features"
- )
- func TestDropAllowedProcMountTypes(t *testing.T) {
- allowedProcMountTypes := []api.ProcMountType{api.UnmaskedProcMount}
- scWithoutAllowedProcMountTypes := func() *policy.PodSecurityPolicySpec {
- return &policy.PodSecurityPolicySpec{}
- }
- scWithAllowedProcMountTypes := func() *policy.PodSecurityPolicySpec {
- return &policy.PodSecurityPolicySpec{
- AllowedProcMountTypes: allowedProcMountTypes,
- }
- }
- scInfo := []struct {
- description string
- hasAllowedProcMountTypes bool
- sc func() *policy.PodSecurityPolicySpec
- }{
- {
- description: "PodSecurityPolicySpec Without AllowedProcMountTypes",
- hasAllowedProcMountTypes: false,
- sc: scWithoutAllowedProcMountTypes,
- },
- {
- description: "PodSecurityPolicySpec With AllowedProcMountTypes",
- hasAllowedProcMountTypes: true,
- sc: scWithAllowedProcMountTypes,
- },
- {
- description: "is nil",
- hasAllowedProcMountTypes: false,
- sc: func() *policy.PodSecurityPolicySpec { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPSPSpecInfo := range scInfo {
- for _, newPSPSpecInfo := range scInfo {
- oldPSPSpecHasAllowedProcMountTypes, oldPSPSpec := oldPSPSpecInfo.hasAllowedProcMountTypes, oldPSPSpecInfo.sc()
- newPSPSpecHasAllowedProcMountTypes, newPSPSpec := newPSPSpecInfo.hasAllowedProcMountTypes, newPSPSpecInfo.sc()
- if newPSPSpec == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old PodSecurityPolicySpec %v, new PodSecurityPolicySpec %v", enabled, oldPSPSpecInfo.description, newPSPSpecInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ProcMountType, enabled)()
- DropDisabledFields(newPSPSpec, oldPSPSpec)
- // old PodSecurityPolicySpec should never be changed
- if !reflect.DeepEqual(oldPSPSpec, oldPSPSpecInfo.sc()) {
- t.Errorf("old PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(oldPSPSpec, oldPSPSpecInfo.sc()))
- }
- switch {
- case enabled || oldPSPSpecHasAllowedProcMountTypes:
- // new PodSecurityPolicySpec should not be changed if the feature is enabled, or if the old PodSecurityPolicySpec had AllowedProcMountTypes
- if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
- t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
- }
- case newPSPSpecHasAllowedProcMountTypes:
- // new PodSecurityPolicySpec should be changed
- if reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
- t.Errorf("new PodSecurityPolicySpec was not changed")
- }
- // new PodSecurityPolicySpec should not have AllowedProcMountTypes
- if !reflect.DeepEqual(newPSPSpec, scWithoutAllowedProcMountTypes()) {
- t.Errorf("new PodSecurityPolicySpec had PodSecurityPolicySpecAllowedProcMountTypes: %v", diff.ObjectReflectDiff(newPSPSpec, scWithoutAllowedProcMountTypes()))
- }
- default:
- // new PodSecurityPolicySpec should not need to be changed
- if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
- t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropRunAsGroup(t *testing.T) {
- group := func() *policy.RunAsGroupStrategyOptions {
- return &policy.RunAsGroupStrategyOptions{}
- }
- scWithoutRunAsGroup := func() *policy.PodSecurityPolicySpec {
- return &policy.PodSecurityPolicySpec{}
- }
- scWithRunAsGroup := func() *policy.PodSecurityPolicySpec {
- return &policy.PodSecurityPolicySpec{
- RunAsGroup: group(),
- }
- }
- scInfo := []struct {
- description string
- hasRunAsGroup bool
- sc func() *policy.PodSecurityPolicySpec
- }{
- {
- description: "PodSecurityPolicySpec Without RunAsGroup",
- hasRunAsGroup: false,
- sc: scWithoutRunAsGroup,
- },
- {
- description: "PodSecurityPolicySpec With RunAsGroup",
- hasRunAsGroup: true,
- sc: scWithRunAsGroup,
- },
- {
- description: "is nil",
- hasRunAsGroup: false,
- sc: func() *policy.PodSecurityPolicySpec { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPSPSpecInfo := range scInfo {
- for _, newPSPSpecInfo := range scInfo {
- oldPSPSpecHasRunAsGroup, oldPSPSpec := oldPSPSpecInfo.hasRunAsGroup, oldPSPSpecInfo.sc()
- newPSPSpecHasRunAsGroup, newPSPSpec := newPSPSpecInfo.hasRunAsGroup, newPSPSpecInfo.sc()
- if newPSPSpec == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old PodSecurityPolicySpec %v, new PodSecurityPolicySpec %v", enabled, oldPSPSpecInfo.description, newPSPSpecInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.RunAsGroup, enabled)()
- DropDisabledFields(newPSPSpec, oldPSPSpec)
- // old PodSecurityPolicySpec should never be changed
- if !reflect.DeepEqual(oldPSPSpec, oldPSPSpecInfo.sc()) {
- t.Errorf("old PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(oldPSPSpec, oldPSPSpecInfo.sc()))
- }
- switch {
- case enabled || oldPSPSpecHasRunAsGroup:
- // new PodSecurityPolicySpec should not be changed if the feature is enabled, or if the old PodSecurityPolicySpec had RunAsGroup
- if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
- t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
- }
- case newPSPSpecHasRunAsGroup:
- // new PodSecurityPolicySpec should be changed
- if reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
- t.Errorf("new PodSecurityPolicySpec was not changed")
- }
- // new PodSecurityPolicySpec should not have RunAsGroup
- if !reflect.DeepEqual(newPSPSpec, scWithoutRunAsGroup()) {
- t.Errorf("new PodSecurityPolicySpec had RunAsGroup: %v", diff.ObjectReflectDiff(newPSPSpec, scWithoutRunAsGroup()))
- }
- default:
- // new PodSecurityPolicySpec should not need to be changed
- if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
- t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropSysctls(t *testing.T) {
- scWithSysctls := func() *policy.PodSecurityPolicySpec {
- return &policy.PodSecurityPolicySpec{
- AllowedUnsafeSysctls: []string{"foo/*"},
- ForbiddenSysctls: []string{"bar.*"},
- }
- }
- scWithOneSysctls := func() *policy.PodSecurityPolicySpec {
- return &policy.PodSecurityPolicySpec{
- AllowedUnsafeSysctls: []string{"foo/*"},
- }
- }
- scWithoutSysctls := func() *policy.PodSecurityPolicySpec {
- return &policy.PodSecurityPolicySpec{}
- }
- scInfo := []struct {
- description string
- hasSysctls bool
- sc func() *policy.PodSecurityPolicySpec
- }{
- {
- description: "has Sysctls",
- hasSysctls: true,
- sc: scWithSysctls,
- },
- {
- description: "has one Sysctl",
- hasSysctls: true,
- sc: scWithOneSysctls,
- },
- {
- description: "does not have Sysctls",
- hasSysctls: false,
- sc: scWithoutSysctls,
- },
- {
- description: "is nil",
- hasSysctls: false,
- sc: func() *policy.PodSecurityPolicySpec { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPSPSpecInfo := range scInfo {
- for _, newPSPSpecInfo := range scInfo {
- oldPSPSpecHasSysctls, oldPSPSpec := oldPSPSpecInfo.hasSysctls, oldPSPSpecInfo.sc()
- newPSPSpecHasSysctls, newPSPSpec := newPSPSpecInfo.hasSysctls, newPSPSpecInfo.sc()
- if newPSPSpec == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old PodSecurityPolicySpec %v, new PodSecurityPolicySpec %v", enabled, oldPSPSpecInfo.description, newPSPSpecInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.Sysctls, enabled)()
- DropDisabledFields(newPSPSpec, oldPSPSpec)
- // old PodSecurityPolicySpec should never be changed
- if !reflect.DeepEqual(oldPSPSpec, oldPSPSpecInfo.sc()) {
- t.Errorf("old PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(oldPSPSpec, oldPSPSpecInfo.sc()))
- }
- switch {
- case enabled || oldPSPSpecHasSysctls:
- // new PodSecurityPolicySpec should not be changed if the feature is enabled, or if the old PodSecurityPolicySpec had Sysctls
- if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
- t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
- }
- case newPSPSpecHasSysctls:
- // new PodSecurityPolicySpec should be changed
- if reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
- t.Errorf("new PodSecurityPolicySpec was not changed")
- }
- // new PodSecurityPolicySpec should not have Sysctls
- if !reflect.DeepEqual(newPSPSpec, scWithoutSysctls()) {
- t.Errorf("new PodSecurityPolicySpec had Sysctls: %v", diff.ObjectReflectDiff(newPSPSpec, scWithoutSysctls()))
- }
- default:
- // new PodSecurityPolicySpec should not need to be changed
- if !reflect.DeepEqual(newPSPSpec, newPSPSpecInfo.sc()) {
- t.Errorf("new PodSecurityPolicySpec changed: %v", diff.ObjectReflectDiff(newPSPSpec, newPSPSpecInfo.sc()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropRuntimeClass(t *testing.T) {
- type testcase struct {
- name string
- featureEnabled bool
- pspSpec, oldPSPSpec *policy.PodSecurityPolicySpec
- expectRuntimeClass bool
- }
- tests := []testcase{}
- pspGenerator := func(withRuntimeClass bool) *policy.PodSecurityPolicySpec {
- psp := &policy.PodSecurityPolicySpec{}
- if withRuntimeClass {
- psp.RuntimeClass = &policy.RuntimeClassStrategyOptions{
- AllowedRuntimeClassNames: []string{policy.AllowAllRuntimeClassNames},
- }
- }
- return psp
- }
- for _, enabled := range []bool{true, false} {
- for _, hasRuntimeClass := range []bool{true, false} {
- tests = append(tests, testcase{
- name: fmt.Sprintf("create feature:%t hasRC:%t", enabled, hasRuntimeClass),
- featureEnabled: enabled,
- pspSpec: pspGenerator(hasRuntimeClass),
- expectRuntimeClass: enabled && hasRuntimeClass,
- })
- for _, hadRuntimeClass := range []bool{true, false} {
- tests = append(tests, testcase{
- name: fmt.Sprintf("update feature:%t hasRC:%t hadRC:%t", enabled, hasRuntimeClass, hadRuntimeClass),
- featureEnabled: enabled,
- pspSpec: pspGenerator(hasRuntimeClass),
- oldPSPSpec: pspGenerator(hadRuntimeClass),
- expectRuntimeClass: hasRuntimeClass && (enabled || hadRuntimeClass),
- })
- }
- }
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.RuntimeClass, test.featureEnabled)()
- DropDisabledFields(test.pspSpec, test.oldPSPSpec)
- if test.expectRuntimeClass {
- assert.NotNil(t, test.pspSpec.RuntimeClass)
- } else {
- assert.Nil(t, test.pspSpec.RuntimeClass)
- }
- })
- }
- }
|