This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to Seccomp profile in security Context ⚠️ (#475)
* Initial commit * Fix tests * update docs * cover localhost * Update usages of seccomp * remove test * Update docs * Add warning when annotations are present * upd * Add warning for manifest mode when annotations are present and no seccomp profile * Update auditors/seccomp/seccomp.go Co-authored-by: Genevieve Luyt <11131143+genevieveluyt@users.noreply.github.com> * yml formatting * fix name Co-authored-by: Genevieve Luyt <11131143+genevieveluyt@users.noreply.github.com>
- Loading branch information
1 parent
c875a37
commit 2061bc7
Showing
23 changed files
with
438 additions
and
429 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package seccomp | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Shopify/kubeaudit/pkg/k8s" | ||
apiv1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type BySettingSeccompProfile struct { | ||
seccompProfileType apiv1.SeccompProfileType | ||
} | ||
|
||
func (pending *BySettingSeccompProfile) Plan() string { | ||
return fmt.Sprintf("Set SeccompProfile type to '%s' in pod SecurityContext", pending.seccompProfileType) | ||
} | ||
|
||
func (pending *BySettingSeccompProfile) Apply(resource k8s.Resource) []k8s.Resource { | ||
podSpec := k8s.GetPodSpec(resource) | ||
if podSpec.SecurityContext == nil { | ||
podSpec.SecurityContext = &apiv1.PodSecurityContext{} | ||
} | ||
podSpec.SecurityContext.SeccompProfile = &apiv1.SeccompProfile{Type: pending.seccompProfileType} | ||
|
||
return nil | ||
} | ||
|
||
type BySettingSeccompProfileInContainer struct { | ||
container *k8s.ContainerV1 | ||
seccompProfileType apiv1.SeccompProfileType | ||
} | ||
|
||
func (pending *BySettingSeccompProfileInContainer) Plan() string { | ||
return fmt.Sprintf("Set SeccompProfile type to '%s' in SecurityContext for container `%s`", pending.seccompProfileType, pending.container.Name) | ||
} | ||
|
||
func (pending *BySettingSeccompProfileInContainer) Apply(resource k8s.Resource) []k8s.Resource { | ||
if pending.container.SecurityContext == nil { | ||
pending.container.SecurityContext = &apiv1.SecurityContext{} | ||
} | ||
pending.container.SecurityContext.SeccompProfile = &apiv1.SeccompProfile{Type: pending.seccompProfileType} | ||
return nil | ||
} | ||
|
||
type ByRemovingSeccompProfileInContainer struct { | ||
container *k8s.ContainerV1 | ||
} | ||
|
||
func (pending *ByRemovingSeccompProfileInContainer) Plan() string { | ||
return fmt.Sprintf("Remove SeccompProfile in SecurityContext for container `%s`", pending.container.Name) | ||
} | ||
|
||
func (pending *ByRemovingSeccompProfileInContainer) Apply(resource k8s.Resource) []k8s.Resource { | ||
if pending.container.SecurityContext == nil { | ||
return nil | ||
} | ||
pending.container.SecurityContext.SeccompProfile = nil | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package seccomp | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Shopify/kubeaudit/internal/test" | ||
"github.com/Shopify/kubeaudit/pkg/k8s" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
apiv1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const fixtureDir = "fixtures" | ||
const emptyProfile = apiv1.SeccompProfileType("EMPTY") | ||
const defaultProfile = apiv1.SeccompProfileTypeRuntimeDefault | ||
const localhostProfile = apiv1.SeccompProfileTypeLocalhost | ||
|
||
func TestFixSeccomp(t *testing.T) { | ||
cases := []struct { | ||
file string | ||
expectedPodSeccompProfile apiv1.SeccompProfileType | ||
expectedContainerSeccompProfiles []apiv1.SeccompProfileType | ||
}{ | ||
{"seccomp-profile-missing.yml", defaultProfile, []apiv1.SeccompProfileType{emptyProfile}}, | ||
{"seccomp-profile-missing-disabled-container.yml", defaultProfile, []apiv1.SeccompProfileType{emptyProfile}}, | ||
{"seccomp-profile-missing-annotations.yml", defaultProfile, []apiv1.SeccompProfileType{emptyProfile}}, | ||
{"seccomp-disabled-pod.yml", defaultProfile, []apiv1.SeccompProfileType{defaultProfile}}, | ||
{"seccomp-disabled.yml", defaultProfile, []apiv1.SeccompProfileType{emptyProfile, emptyProfile}}, | ||
{"seccomp-disabled-localhost.yml", localhostProfile, []apiv1.SeccompProfileType{defaultProfile, emptyProfile}}, | ||
} | ||
|
||
for _, tc := range cases { | ||
// This line is needed because of how scopes work with parallel tests (see https://gist.github.com/posener/92a55c4cd441fc5e5e85f27bca008721) | ||
tc := tc | ||
t.Run(tc.file, func(t *testing.T) { | ||
resources, _ := test.FixSetup(t, fixtureDir, tc.file, New()) | ||
require.Len(t, resources, 1) | ||
resource := resources[0] | ||
|
||
updatedPodSpec := k8s.GetPodSpec(resource) | ||
checkPodSeccompProfile(t, updatedPodSpec, tc.expectedPodSeccompProfile) | ||
checkContainerSeccompProfiles(t, updatedPodSpec, tc.expectedContainerSeccompProfiles) | ||
checkNoSeccompAnnotations(t, resource) | ||
}) | ||
} | ||
} | ||
|
||
func checkPodSeccompProfile(t *testing.T, podSpec *apiv1.PodSpec, expectedPodSeccompProfile apiv1.SeccompProfileType) { | ||
securityContext := podSpec.SecurityContext | ||
if expectedPodSeccompProfile == emptyProfile { | ||
require.Nil(t, securityContext) | ||
} else { | ||
assert.Equal(t, expectedPodSeccompProfile, securityContext.SeccompProfile.Type) | ||
} | ||
} | ||
|
||
func checkContainerSeccompProfiles(t *testing.T, podSpec *apiv1.PodSpec, expectedContainerSeccompProfiles []apiv1.SeccompProfileType) { | ||
for i, container := range podSpec.Containers { | ||
securityContext := container.SecurityContext | ||
expectedProfile := expectedContainerSeccompProfiles[i] | ||
if expectedProfile == emptyProfile { | ||
require.True(t, securityContext == nil || securityContext.SeccompProfile == nil) | ||
} else { | ||
assert.Equal(t, expectedProfile, securityContext.SeccompProfile.Type) | ||
} | ||
} | ||
} | ||
|
||
func checkNoSeccompAnnotations(t *testing.T, resource k8s.Resource) { | ||
annotations := k8s.GetAnnotations(resource) | ||
if annotations == nil { | ||
return | ||
} | ||
|
||
seccompAnnotations := []string{} | ||
for annotation := range annotations { | ||
if annotation == PodAnnotationKey || strings.HasPrefix(annotation, ContainerAnnotationKeyPrefix) { | ||
seccompAnnotations = append(seccompAnnotations, annotation) | ||
} | ||
} | ||
assert.Empty(t, seccompAnnotations) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: pod | ||
namespace: seccomp-disabled-localhost | ||
spec: | ||
securityContext: | ||
seccompProfile: | ||
type: Localhost | ||
localhostProfile: my-seccomp-profile.json | ||
containers: | ||
- name: container1 | ||
image: scratch | ||
securityContext: | ||
seccompProfile: | ||
type: Unconfined | ||
- name: container2 | ||
image: scratch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
auditors/seccomp/fixtures/seccomp-profile-missing-disabled-container.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: pod | ||
namespace: seccomp-profile-missing-disabled-container | ||
spec: | ||
containers: | ||
- name: container | ||
image: scratch | ||
securityContext: | ||
seccompProfile: | ||
type: Unconfined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.