Skip to content
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

fix: shutdown-manager not respecting security context of container spec #4938

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -8,7 +8,12 @@ package proxy
import (
"testing"

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

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

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)
})
}
}
Loading