|
| 1 | +package seccomp |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/Shopify/kubeaudit/internal/test" |
| 8 | + "github.com/Shopify/kubeaudit/pkg/k8s" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + apiv1 "k8s.io/api/core/v1" |
| 12 | +) |
| 13 | + |
| 14 | +const fixtureDir = "fixtures" |
| 15 | +const emptyProfile = apiv1.SeccompProfileType("EMPTY") |
| 16 | +const defaultProfile = apiv1.SeccompProfileTypeRuntimeDefault |
| 17 | +const localhostProfile = apiv1.SeccompProfileTypeLocalhost |
| 18 | + |
| 19 | +func TestFixSeccomp(t *testing.T) { |
| 20 | + cases := []struct { |
| 21 | + file string |
| 22 | + expectedPodSeccompProfile apiv1.SeccompProfileType |
| 23 | + expectedContainerSeccompProfiles []apiv1.SeccompProfileType |
| 24 | + }{ |
| 25 | + {"seccomp-profile-missing.yml", defaultProfile, []apiv1.SeccompProfileType{emptyProfile}}, |
| 26 | + {"seccomp-profile-missing-disabled-container.yml", defaultProfile, []apiv1.SeccompProfileType{emptyProfile}}, |
| 27 | + {"seccomp-profile-missing-annotations.yml", defaultProfile, []apiv1.SeccompProfileType{emptyProfile}}, |
| 28 | + {"seccomp-disabled-pod.yml", defaultProfile, []apiv1.SeccompProfileType{defaultProfile}}, |
| 29 | + {"seccomp-disabled.yml", defaultProfile, []apiv1.SeccompProfileType{emptyProfile, emptyProfile}}, |
| 30 | + {"seccomp-disabled-localhost.yml", localhostProfile, []apiv1.SeccompProfileType{defaultProfile, emptyProfile}}, |
| 31 | + } |
| 32 | + |
| 33 | + for _, tc := range cases { |
| 34 | + // This line is needed because of how scopes work with parallel tests (see https://gist.github.com/posener/92a55c4cd441fc5e5e85f27bca008721) |
| 35 | + tc := tc |
| 36 | + t.Run(tc.file, func(t *testing.T) { |
| 37 | + resources, _ := test.FixSetup(t, fixtureDir, tc.file, New()) |
| 38 | + require.Len(t, resources, 1) |
| 39 | + resource := resources[0] |
| 40 | + |
| 41 | + updatedPodSpec := k8s.GetPodSpec(resource) |
| 42 | + checkPodSeccompProfile(t, updatedPodSpec, tc.expectedPodSeccompProfile) |
| 43 | + checkContainerSeccompProfiles(t, updatedPodSpec, tc.expectedContainerSeccompProfiles) |
| 44 | + checkNoSeccompAnnotations(t, resource) |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func checkPodSeccompProfile(t *testing.T, podSpec *apiv1.PodSpec, expectedPodSeccompProfile apiv1.SeccompProfileType) { |
| 50 | + securityContext := podSpec.SecurityContext |
| 51 | + if expectedPodSeccompProfile == emptyProfile { |
| 52 | + require.Nil(t, securityContext) |
| 53 | + } else { |
| 54 | + assert.Equal(t, expectedPodSeccompProfile, securityContext.SeccompProfile.Type) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func checkContainerSeccompProfiles(t *testing.T, podSpec *apiv1.PodSpec, expectedContainerSeccompProfiles []apiv1.SeccompProfileType) { |
| 59 | + for i, container := range podSpec.Containers { |
| 60 | + securityContext := container.SecurityContext |
| 61 | + expectedProfile := expectedContainerSeccompProfiles[i] |
| 62 | + if expectedProfile == emptyProfile { |
| 63 | + require.True(t, securityContext == nil || securityContext.SeccompProfile == nil) |
| 64 | + } else { |
| 65 | + assert.Equal(t, expectedProfile, securityContext.SeccompProfile.Type) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func checkNoSeccompAnnotations(t *testing.T, resource k8s.Resource) { |
| 71 | + annotations := k8s.GetAnnotations(resource) |
| 72 | + if annotations == nil { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + seccompAnnotations := []string{} |
| 77 | + for annotation := range annotations { |
| 78 | + if annotation == PodAnnotationKey || strings.HasPrefix(annotation, ContainerAnnotationKeyPrefix) { |
| 79 | + seccompAnnotations = append(seccompAnnotations, annotation) |
| 80 | + } |
| 81 | + } |
| 82 | + assert.Empty(t, seccompAnnotations) |
| 83 | +} |
0 commit comments