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 189
Migrate to Seccomp profile in security Context ⚠️ #475
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
532c2b5
Initial commit
648748b
Fix tests
9e5a377
update docs
1fca2ed
cover localhost
8cd3553
Update usages of seccomp
c5fffe4
remove test
42a64a3
Update docs
81af075
Add warning when annotations are present
7500d89
upd
5770159
Add warning for manifest mode when annotations are present and no sec…
fdcb318
Update auditors/seccomp/seccomp.go
Ser87ch badbe67
yml formatting
8f5ff04
fix name
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package seccomp | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Shopify/kubeaudit/pkg/k8s" | ||
apiv1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type BySettingSeccompProfileAndRemovingAnnotations struct { | ||
seccompProfileType apiv1.SeccompProfileType | ||
annotationsToRemove []string | ||
} | ||
|
||
func (pending *BySettingSeccompProfileAndRemovingAnnotations) Plan() string { | ||
annotationsMessage := "" | ||
if len(pending.annotationsToRemove) > 0 { | ||
annotationsMessage = fmt.Sprintf(" and remove the following annotations %s", pending.annotationsToRemove) | ||
} | ||
return fmt.Sprintf("Set SeccompProfile type to '%s' in pod SecurityContext%s", pending.seccompProfileType, annotationsMessage) | ||
} | ||
|
||
func (pending *BySettingSeccompProfileAndRemovingAnnotations) 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} | ||
|
||
objectMeta := k8s.GetPodObjectMeta(resource) | ||
|
||
if objectMeta.GetAnnotations() == nil { | ||
return nil | ||
} | ||
|
||
for _, annotationToDelete := range pending.annotationsToRemove { | ||
delete(objectMeta.GetAnnotations(), annotationToDelete) | ||
} | ||
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the indentation looks a bit off here, maybe tabs vs spaces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed