Skip to content

Commit

Permalink
fix: shutdown-manager not respecting security context of container sp…
Browse files Browse the repository at this point in the history
…ec (#4938)

* Fix shutdown-manager not respecting security context of container spec

Signed-off-by: Dean Coakley <dean.s.coakley@gmail.com>

* Update securityContext testdata

Signed-off-by: Dean Coakley <dean.s.coakley@gmail.com>

* Lint with gci

Signed-off-by: Dean Coakley <dean.s.coakley@gmail.com>

---------

Signed-off-by: Dean Coakley <dean.s.coakley@gmail.com>
  • Loading branch information
Dean-Coakley authored Jan 6, 2025
1 parent 10a31f1 commit 43621b4
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 92 deletions.
8 changes: 6 additions & 2 deletions internal/infrastructure/kubernetes/proxy/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func expectedProxyContainers(infra *ir.ProxyInfra,
},
},
},
SecurityContext: expectedShutdownManagerSecurityContext(),
SecurityContext: expectedShutdownManagerSecurityContext(containerSpec),
},
}

Expand Down Expand Up @@ -384,7 +384,11 @@ func expectedEnvoySecurityContext(containerSpec *egv1a1.KubernetesContainerSpec)
return sc
}

func expectedShutdownManagerSecurityContext() *corev1.SecurityContext {
func expectedShutdownManagerSecurityContext(containerSpec *egv1a1.KubernetesContainerSpec) *corev1.SecurityContext {
if containerSpec != nil && containerSpec.SecurityContext != nil {
return containerSpec.SecurityContext
}

sc := resource.DefaultSecurityContext()

// run as non-root user
Expand Down
51 changes: 51 additions & 0 deletions internal/infrastructure/kubernetes/proxy/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/infrastructure/kubernetes/resource"
)

func TestEnvoyPodSelector(t *testing.T) {
Expand Down Expand Up @@ -36,3 +41,49 @@ func TestEnvoyPodSelector(t *testing.T) {
})
}
}

func TestExpectedShutdownManagerSecurityContext(t *testing.T) {
defaultSecurityContext := func() *corev1.SecurityContext {
sc := resource.DefaultSecurityContext()

// run as non-root user
sc.RunAsGroup = ptr.To(int64(65532))
sc.RunAsUser = ptr.To(int64(65532))

// ShutdownManger creates a file to indicate the connection drain process is completed,
// so it needs file write permission.
sc.ReadOnlyRootFilesystem = nil
return sc
}

customSc := &corev1.SecurityContext{
Privileged: ptr.To(true),
RunAsUser: ptr.To(int64(21)),
RunAsGroup: ptr.To(int64(2100)),
}

tests := []struct {
name string
in *egv1a1.KubernetesContainerSpec
expected *corev1.SecurityContext
}{
{
name: "default",
in: nil,
expected: defaultSecurityContext(),
},
{
name: "default",
in: &egv1a1.KubernetesContainerSpec{
SecurityContext: customSc,
},
expected: customSc,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := expectedShutdownManagerSecurityContext(tc.in)
require.Equal(t, tc.expected, got)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,7 @@ spec:
cpu: 10m
memory: 32Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
privileged: true
startupProbe:
failureThreshold: 30
httpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,7 @@ spec:
cpu: 10m
memory: 32Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
privileged: true
startupProbe:
failureThreshold: 30
httpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,7 @@ spec:
cpu: 10m
memory: 32Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
privileged: true
startupProbe:
failureThreshold: 30
httpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,7 @@ spec:
cpu: 10m
memory: 32Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
privileged: true
startupProbe:
failureThreshold: 30
httpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,7 @@ spec:
cpu: 10m
memory: 32Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
privileged: true
startupProbe:
failureThreshold: 30
httpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,7 @@ spec:
cpu: 10m
memory: 32Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
privileged: true
startupProbe:
failureThreshold: 30
httpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,7 @@ spec:
cpu: 10m
memory: 32Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
privileged: true
startupProbe:
failureThreshold: 30
httpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,7 @@ spec:
cpu: 10m
memory: 32Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
privileged: true
startupProbe:
failureThreshold: 30
httpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,7 @@ spec:
cpu: 10m
memory: 32Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
privileged: false
runAsGroup: 65532
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
privileged: true
startupProbe:
failureThreshold: 30
httpGet:
Expand Down

0 comments on commit 43621b4

Please sign in to comment.