Skip to content

Commit

Permalink
[release-1.11] fix securityContext for Knative Service Pod (user-cont…
Browse files Browse the repository at this point in the history
…ainer and queue-proxy) (#14378)

* add seccompProfile to queue container security context

* run as non root by default

* update tests to expect new default run as nonroot

---------

Co-authored-by: Clay Kauzlaric <ckauzlaric@vmware.com>
  • Loading branch information
knative-prow-robot and KauzClay authored Sep 14, 2023
1 parent f1617ef commit f60eb32
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/serving/v1/revision_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ func (rs *RevisionSpec) defaultSecurityContext(psc *corev1.PodSecurityContext, c
}
}

if psc.RunAsNonRoot == nil {
updatedSC.RunAsNonRoot = ptr.Bool(true)
}

if *updatedSC != (corev1.SecurityContext{}) {
container.SecurityContext = updatedSC
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/serving/v1/revision_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ func TestRevisionDefaulting(t *testing.T) {
ReadinessProbe: defaultProbe,
Resources: defaultResources,
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: ptr.Bool(true),
AllowPrivilegeEscalation: ptr.Bool(false),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
Expand All @@ -913,6 +914,7 @@ func TestRevisionDefaulting(t *testing.T) {
Name: "sidecar",
Resources: defaultResources,
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: ptr.Bool(true),
AllowPrivilegeEscalation: ptr.Bool(false),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
Expand All @@ -925,6 +927,7 @@ func TestRevisionDefaulting(t *testing.T) {
Name: "special-sidecar",
Resources: defaultResources,
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: ptr.Bool(true),
AllowPrivilegeEscalation: ptr.Bool(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
Expand All @@ -938,6 +941,7 @@ func TestRevisionDefaulting(t *testing.T) {
InitContainers: []corev1.Container{{
Name: "special-init",
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: ptr.Bool(true),
AllowPrivilegeEscalation: ptr.Bool(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeLocalhost,
Expand Down Expand Up @@ -1000,6 +1004,7 @@ func TestRevisionDefaulting(t *testing.T) {
ReadinessProbe: defaultProbe,
Resources: defaultResources,
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: ptr.Bool(true),
AllowPrivilegeEscalation: ptr.Bool(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
Expand All @@ -1009,6 +1014,7 @@ func TestRevisionDefaulting(t *testing.T) {
InitContainers: []corev1.Container{{
Name: "init",
SecurityContext: &corev1.SecurityContext{
RunAsNonRoot: ptr.Bool(true),
AllowPrivilegeEscalation: ptr.Bool(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
Expand Down
3 changes: 3 additions & 0 deletions pkg/reconciler/revision/resources/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ var (
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
)

Expand Down

0 comments on commit f60eb32

Please sign in to comment.