|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package settings |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/tls" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/onsi/ginkgo/v2" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | + appsv1 "k8s.io/api/apps/v1" |
| 29 | + corev1 "k8s.io/api/core/v1" |
| 30 | + |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + |
| 33 | + "k8s.io/ingress-nginx/test/e2e/framework" |
| 34 | +) |
| 35 | + |
| 36 | +var _ = framework.IngressNginxDescribe("[Flag] enable-ssl-passthrough", func() { |
| 37 | + f := framework.NewDefaultFramework("ssl-passthrough") |
| 38 | + |
| 39 | + ginkgo.BeforeEach(func() { |
| 40 | + err := f.UpdateIngressControllerDeployment(func(deployment *appsv1.Deployment) error { |
| 41 | + args := deployment.Spec.Template.Spec.Containers[0].Args |
| 42 | + args = append(args, "--enable-ssl-passthrough") |
| 43 | + deployment.Spec.Template.Spec.Containers[0].Args = args |
| 44 | + _, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{}) |
| 45 | + return err |
| 46 | + }) |
| 47 | + assert.Nil(ginkgo.GinkgoT(), err, "updating ingress controller deployment flags") |
| 48 | + |
| 49 | + f.WaitForNginxServer("_", |
| 50 | + func(server string) bool { |
| 51 | + return strings.Contains(server, "listen 442") |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + ginkgo.Describe("With enable-ssl-passthrough enabled", func() { |
| 56 | + ginkgo.It("should enable ssl-passthrough-proxy-port on a different port", func() { |
| 57 | + |
| 58 | + err := f.UpdateIngressControllerDeployment(func(deployment *appsv1.Deployment) error { |
| 59 | + args := deployment.Spec.Template.Spec.Containers[0].Args |
| 60 | + args = append(args, "--ssl-passthrough-proxy-port=1442") |
| 61 | + deployment.Spec.Template.Spec.Containers[0].Args = args |
| 62 | + _, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{}) |
| 63 | + return err |
| 64 | + }) |
| 65 | + assert.Nil(ginkgo.GinkgoT(), err, "updating ingress controller deployment flags") |
| 66 | + |
| 67 | + f.WaitForNginxServer("_", |
| 68 | + func(server string) bool { |
| 69 | + return strings.Contains(server, "listen 1442") |
| 70 | + }) |
| 71 | + |
| 72 | + f.HTTPTestClient(). |
| 73 | + GET("/"). |
| 74 | + WithHeader("Host", "something"). |
| 75 | + Expect(). |
| 76 | + Status(http.StatusNotFound) |
| 77 | + }) |
| 78 | + |
| 79 | + ginkgo.It("should pass unknown traffic to default backend and handle known traffic", func() { |
| 80 | + |
| 81 | + host := "testpassthrough.com" |
| 82 | + echoName := "echopass" |
| 83 | + |
| 84 | + /* Even with enable-ssl-passthrough enabled, only annotated ingresses may receive the trafic */ |
| 85 | + annotations := map[string]string{ |
| 86 | + "nginx.ingress.kubernetes.io/ssl-passthrough": "true", |
| 87 | + } |
| 88 | + |
| 89 | + ingressDef := framework.NewSingleIngressWithTLS(host, "/", host, []string{host}, f.Namespace, echoName, 80, annotations) |
| 90 | + tlsConfig, err := framework.CreateIngressTLSSecret(f.KubeClientSet, |
| 91 | + ingressDef.Spec.TLS[0].Hosts, |
| 92 | + ingressDef.Spec.TLS[0].SecretName, |
| 93 | + ingressDef.Namespace) |
| 94 | + |
| 95 | + volumeMount := []corev1.VolumeMount{ |
| 96 | + { |
| 97 | + Name: "certs", |
| 98 | + ReadOnly: true, |
| 99 | + MountPath: "/certs", |
| 100 | + }, |
| 101 | + } |
| 102 | + volume := []corev1.Volume{ |
| 103 | + { |
| 104 | + Name: "certs", |
| 105 | + VolumeSource: corev1.VolumeSource{ |
| 106 | + Secret: &corev1.SecretVolumeSource{ |
| 107 | + SecretName: ingressDef.Spec.TLS[0].SecretName, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | + envs := []corev1.EnvVar{ |
| 113 | + { |
| 114 | + Name: "HTTPBUN_SSL_CERT", |
| 115 | + Value: "/certs/tls.crt", |
| 116 | + }, |
| 117 | + { |
| 118 | + Name: "HTTPBUN_SSL_KEY", |
| 119 | + Value: "/certs/tls.key", |
| 120 | + }, |
| 121 | + } |
| 122 | + f.NewDeploymentWithOpts("echopass", "ghcr.io/sharat87/httpbun:latest", 80, 1, nil, nil, envs, volumeMount, volume, false) |
| 123 | + |
| 124 | + f.EnsureIngress(ingressDef) |
| 125 | + |
| 126 | + assert.Nil(ginkgo.GinkgoT(), err) |
| 127 | + framework.WaitForTLS(f.GetURL(framework.HTTPS), tlsConfig) |
| 128 | + |
| 129 | + f.WaitForNginxServer(host, |
| 130 | + func(server string) bool { |
| 131 | + return strings.Contains(server, "listen 442") |
| 132 | + }) |
| 133 | + |
| 134 | + /* This one should not receive traffic as it does not contain passthrough annotation */ |
| 135 | + hostBad := "noannotationnopassthrough.com" |
| 136 | + ingBad := f.EnsureIngress(framework.NewSingleIngressWithTLS(hostBad, "/", hostBad, []string{hostBad}, f.Namespace, echoName, 80, nil)) |
| 137 | + tlsConfigBad, err := framework.CreateIngressTLSSecret(f.KubeClientSet, |
| 138 | + ingBad.Spec.TLS[0].Hosts, |
| 139 | + ingBad.Spec.TLS[0].SecretName, |
| 140 | + ingBad.Namespace) |
| 141 | + assert.Nil(ginkgo.GinkgoT(), err) |
| 142 | + framework.WaitForTLS(f.GetURL(framework.HTTPS), tlsConfigBad) |
| 143 | + |
| 144 | + f.WaitForNginxServer(hostBad, |
| 145 | + func(server string) bool { |
| 146 | + return strings.Contains(server, "listen 442") |
| 147 | + }) |
| 148 | + |
| 149 | + f.HTTPTestClientWithTLSConfig(&tls.Config{ServerName: host, InsecureSkipVerify: true}). |
| 150 | + GET("/"). |
| 151 | + WithURL(fmt.Sprintf("https://%s:443", host)). |
| 152 | + ForceResolve(f.GetNginxIP(), 443). |
| 153 | + Expect(). |
| 154 | + Status(http.StatusOK) |
| 155 | + |
| 156 | + f.HTTPTestClientWithTLSConfig(&tls.Config{ServerName: hostBad, InsecureSkipVerify: true}). |
| 157 | + GET("/"). |
| 158 | + WithURL(fmt.Sprintf("https://%s:443", hostBad)). |
| 159 | + ForceResolve(f.GetNginxIP(), 443). |
| 160 | + Expect(). |
| 161 | + Status(http.StatusNotFound) |
| 162 | + |
| 163 | + }) |
| 164 | + }) |
| 165 | +}) |
0 commit comments