-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add default SecurityContext to every new ksvc #1821
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ import ( | |
"strconv" | ||
"strings" | ||
|
||
"k8s.io/utils/pointer" | ||
|
||
"k8s.io/apimachinery/pkg/util/intstr" | ||
|
||
"k8s.io/apimachinery/pkg/api/resource" | ||
|
@@ -378,6 +380,42 @@ func UpdateImagePullPolicy(spec *corev1.PodSpec, imagePullPolicy string) error { | |
return nil | ||
} | ||
|
||
// UpdateSecurityContext update the Security Context | ||
func UpdateSecurityContext(spec *corev1.PodSpec, securityContext string) error { | ||
container := containerOfPodSpec(spec) | ||
switch strings.ToLower(securityContext) { | ||
case "none": | ||
// Blank any Security Context defined | ||
container.SecurityContext = &corev1.SecurityContext{} | ||
case "strict": | ||
// Add or update Security Context to default strict | ||
container.SecurityContext = DefaultStrictSecCon() | ||
case "": | ||
// Add default strict SC flag is not used, hence empty value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be the case if a default value is set to "strict" ? |
||
if container.SecurityContext == nil { | ||
container.SecurityContext = DefaultStrictSecCon() | ||
} | ||
//TODO(dsimansk): add parsing of SC options from the flag value | ||
default: | ||
return fmt.Errorf("invalid --security-context %s. Valid arguments: strict | none", securityContext) | ||
} | ||
return nil | ||
} | ||
|
||
// DefaultStrictSecCon helper function to get default strict Security Context | ||
func DefaultStrictSecCon() *corev1.SecurityContext { | ||
return &corev1.SecurityContext{ | ||
AllowPrivilegeEscalation: pointer.Bool(false), | ||
RunAsNonRoot: pointer.Bool(true), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My question would be if this does not break the existing setup and whether this is not too restrictive. I mean, there are still tons of container images that can only run as root and/or need certain capabilities. Having such a strict default (that currently can't be changed), is quite drastic. I think we should only add this if there is at least an option to get back the previous behaviour, i.e. having no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I was actually thinking about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's at least ready with on/off switch. 0fcf800 |
||
Capabilities: &corev1.Capabilities{ | ||
Drop: []corev1.Capability{"ALL"}, | ||
}, | ||
SeccompProfile: &corev1.SeccompProfile{ | ||
Type: corev1.SeccompProfileTypeRuntimeDefault, | ||
}, | ||
} | ||
} | ||
|
||
func getPolicy(policy string) v1.PullPolicy { | ||
var ret v1.PullPolicy | ||
switch strings.ToLower(policy) { | ||
|
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.