|
| 1 | +// Copyright Envoy Gateway Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// The full text of the Apache license is available in the LICENSE file at |
| 4 | +// the root of the repo. |
| 5 | + |
| 6 | +//go:build e2e |
| 7 | + |
| 8 | +package tests |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + appsv1 "k8s.io/api/apps/v1" |
| 16 | + corev1 "k8s.io/api/core/v1" |
| 17 | + "k8s.io/apimachinery/pkg/types" |
| 18 | + "k8s.io/utils/ptr" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | + "sigs.k8s.io/gateway-api/conformance/utils/http" |
| 21 | + "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" |
| 22 | + "sigs.k8s.io/gateway-api/conformance/utils/suite" |
| 23 | +) |
| 24 | + |
| 25 | +func init() { |
| 26 | + ConformanceTests = append(ConformanceTests, EnvoyGatewayCustomSecurityContextUseridTest) |
| 27 | +} |
| 28 | + |
| 29 | +var EnvoyGatewayCustomSecurityContextUseridTest = suite.ConformanceTest{ |
| 30 | + ShortName: "EnvoyGatewayCustomSecurityContextUserid", |
| 31 | + Description: "Envoy Gateway container with custom security context user id", |
| 32 | + Manifests: []string{ |
| 33 | + "testdata/custom-container-security-contex-userid.yaml", |
| 34 | + }, |
| 35 | + Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { |
| 36 | + t.Run("route with custom security context user id", func(t *testing.T) { |
| 37 | + // set envoy-gateway deployment security context user id to 65534 to test custom user has the necessary permissions |
| 38 | + // to run the envoy-gateway container |
| 39 | + setEGSecurityContextUserID(t, suite, 65534) |
| 40 | + |
| 41 | + ns := "gateway-conformance-infra" |
| 42 | + routeNN := types.NamespacedName{Name: "custom-eg-security-context-userid", Namespace: ns} |
| 43 | + gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns} |
| 44 | + gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN) |
| 45 | + |
| 46 | + expectedResponse := http.ExpectedResponse{ |
| 47 | + Request: http.Request{ |
| 48 | + Path: "/", |
| 49 | + }, |
| 50 | + Response: http.Response{ |
| 51 | + StatusCode: 200, |
| 52 | + }, |
| 53 | + Namespace: ns, |
| 54 | + } |
| 55 | + |
| 56 | + http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expectedResponse) |
| 57 | + |
| 58 | + // reset envoy-gateway deployment security context user id to the default value 65532 |
| 59 | + setEGSecurityContextUserID(t, suite, 65532) |
| 60 | + // We have to manually delete the envoy proxy deployment to ensure that the test suite can clean up properly. |
| 61 | + // This is because the rollout restart of the envoy-gateway deployment may cause Envoy Gateway fail to delete |
| 62 | + // the envoy proxy deployments after the Gateway resources are deleted in ControllerNamspace mod, which can |
| 63 | + // lead to failure of the upgrade test. |
| 64 | + if suite.Cleanup { |
| 65 | + proxies := appsv1.DeploymentList{} |
| 66 | + err := suite.Client.List( |
| 67 | + context.Background(), |
| 68 | + &proxies, |
| 69 | + client.InNamespace("envoy-gateway-system"), |
| 70 | + client.MatchingLabels{"app.kubernetes.io/component": "proxy", "app.kubernetes.io/managed-by": "envoy-gateway"}) |
| 71 | + require.NoError(t, err, "failed to list envoy proxy deployments") |
| 72 | + for _, proxy := range proxies.Items { |
| 73 | + err = suite.Client.Delete(context.Background(), &proxy) |
| 74 | + require.NoError(t, err, "failed to delete envoy proxy deployment %s", proxy.Name) |
| 75 | + } |
| 76 | + } |
| 77 | + }) |
| 78 | + }, |
| 79 | +} |
| 80 | + |
| 81 | +func setEGSecurityContextUserID(t *testing.T, suite *suite.ConformanceTestSuite, uid int64) { |
| 82 | + // update envoy-gateway deployment with custom security context user id |
| 83 | + egDeployment := &appsv1.Deployment{} |
| 84 | + err := suite.Client.Get( |
| 85 | + context.Background(), |
| 86 | + types.NamespacedName{Name: "envoy-gateway", Namespace: "envoy-gateway-system"}, |
| 87 | + egDeployment) |
| 88 | + require.NoError(t, err) |
| 89 | + egDeployment.Spec.Template.Spec.Containers[0].SecurityContext.RunAsUser = ptr.To(uid) |
| 90 | + egDeployment.Spec.Template.Spec.Containers[0].SecurityContext.RunAsGroup = ptr.To(uid) |
| 91 | + err = suite.Client.Update(context.Background(), egDeployment) |
| 92 | + require.NoError(t, err) |
| 93 | + // test that envoy-gateway pod is running with custom security context user id |
| 94 | + WaitForPods(t, suite.Client, "envoy-gateway-system", map[string]string{"control-plane": "envoy-gateway"}, corev1.PodRunning, PodReady) |
| 95 | + |
| 96 | + // test that envoy-gateway deployment is updated with custom security context user id |
| 97 | + egDeployment = &appsv1.Deployment{} |
| 98 | + err = suite.Client.Get( |
| 99 | + context.Background(), |
| 100 | + types.NamespacedName{Name: "envoy-gateway", Namespace: "envoy-gateway-system"}, |
| 101 | + egDeployment) |
| 102 | + require.NoError(t, err) |
| 103 | + require.Equal(t, uid, *egDeployment.Spec.Template.Spec.Containers[0].SecurityContext.RunAsUser, "envoy-gateway deployment is not updated with custom security context user id") |
| 104 | + require.Equal(t, uid, *egDeployment.Spec.Template.Spec.Containers[0].SecurityContext.RunAsGroup, "envoy-gateway deployment is not updated with custom security context group id") |
| 105 | +} |
0 commit comments