|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package translator |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | + "k8s.io/utils/ptr" |
| 31 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 32 | + |
| 33 | + "github.com/apache/apisix-ingress-controller/internal/provider" |
| 34 | +) |
| 35 | + |
| 36 | +const testCACert = `-----BEGIN CERTIFICATE----- |
| 37 | +MIIBQzCB6qADAgECAgEBMAoGCCqGSM49BAMCMBIxEDAOBgNVBAMTB3Rlc3QtY2Ew |
| 38 | +HhcNNzAwMTAxMDAwMDAwWhcNMzgwMTE5MDMxNDA4WjASMRAwDgYDVQQDEwd0ZXN0 |
| 39 | +LWNhMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJo4AsM30ZHN+mYeHjqwceGBz |
| 40 | +V2bMz1+OyNXuaPYVrSF7HShZhanOYNHb6QLNhjGxMsBDQHVLolPjyTQJp9R5GqMx |
| 41 | +MC8wDgYDVR0PAQH/BAQDAgIEMB0GA1UdDgQWBBRzjh0YVmnpN/cFJziO0aYySuti |
| 42 | +4DAKBggqhkjOPQQDAgNIADBFAiEA7fEGiQA7wX0LrrkRH4KplAPOgVV5Kvm/1dv1 |
| 43 | +3TLq9ssCIHKkv2dhydRvv36KC1WsRDcrl7W+7YmEnCS9PZfb8agM |
| 44 | +-----END CERTIFICATE-----` |
| 45 | + |
| 46 | +func newTLSGateway(frontendValidation *gatewayv1.FrontendTLSValidation) *gatewayv1.Gateway { |
| 47 | + return &gatewayv1.Gateway{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Namespace: "default", |
| 50 | + Name: "gw", |
| 51 | + }, |
| 52 | + Spec: gatewayv1.GatewaySpec{ |
| 53 | + Listeners: []gatewayv1.Listener{ |
| 54 | + { |
| 55 | + Name: "https", |
| 56 | + Hostname: ptr.To(gatewayv1.Hostname("example.com")), |
| 57 | + TLS: &gatewayv1.GatewayTLSConfig{ |
| 58 | + Mode: ptr.To(gatewayv1.TLSModeTerminate), |
| 59 | + CertificateRefs: []gatewayv1.SecretObjectReference{ |
| 60 | + { |
| 61 | + Kind: ptr.To(gatewayv1.Kind("Secret")), |
| 62 | + Name: gatewayv1.ObjectName("server-cert"), |
| 63 | + }, |
| 64 | + }, |
| 65 | + FrontendValidation: frontendValidation, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func newTranslateContextWithTLS() *provider.TranslateContext { |
| 74 | + tctx := provider.NewDefaultTranslateContext(context.Background()) |
| 75 | + tctx.Secrets[types.NamespacedName{Namespace: "default", Name: "server-cert"}] = &corev1.Secret{ |
| 76 | + Data: map[string][]byte{ |
| 77 | + "cert": []byte("server-cert-data"), |
| 78 | + "key": []byte("server-key-data"), |
| 79 | + }, |
| 80 | + } |
| 81 | + tctx.ConfigMaps[types.NamespacedName{Namespace: "default", Name: "ca-cm"}] = &corev1.ConfigMap{ |
| 82 | + Data: map[string]string{ |
| 83 | + corev1.ServiceAccountRootCAKey: testCACert, |
| 84 | + }, |
| 85 | + } |
| 86 | + tctx.Secrets[types.NamespacedName{Namespace: "default", Name: "ca-secret"}] = &corev1.Secret{ |
| 87 | + Data: map[string][]byte{ |
| 88 | + corev1.ServiceAccountRootCAKey: []byte(testCACert), |
| 89 | + }, |
| 90 | + } |
| 91 | + return tctx |
| 92 | +} |
| 93 | + |
| 94 | +func TestTranslateSecret_FrontendValidation(t *testing.T) { |
| 95 | + t.Run("with frontendValidation sets downstream mTLS client CA", func(t *testing.T) { |
| 96 | + tr := &Translator{Log: logr.Discard()} |
| 97 | + gateway := newTLSGateway(&gatewayv1.FrontendTLSValidation{ |
| 98 | + CACertificateRefs: []gatewayv1.ObjectReference{ |
| 99 | + { |
| 100 | + Group: "", |
| 101 | + Kind: "ConfigMap", |
| 102 | + Name: "ca-cm", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }) |
| 106 | + tctx := newTranslateContextWithTLS() |
| 107 | + |
| 108 | + sslObjs, err := tr.translateSecret(tctx, gateway.Spec.Listeners[0], gateway) |
| 109 | + require.NoError(t, err) |
| 110 | + require.Len(t, sslObjs, 1) |
| 111 | + require.NotNil(t, sslObjs[0].Client, "client mTLS config should be set") |
| 112 | + assert.Equal(t, testCACert, sslObjs[0].Client.CA) |
| 113 | + assert.Equal(t, []string{"example.com"}, sslObjs[0].Snis) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("with Secret CA ref sets downstream mTLS client CA", func(t *testing.T) { |
| 117 | + tr := &Translator{Log: logr.Discard()} |
| 118 | + gateway := newTLSGateway(&gatewayv1.FrontendTLSValidation{ |
| 119 | + CACertificateRefs: []gatewayv1.ObjectReference{ |
| 120 | + {Group: "", Kind: "Secret", Name: "ca-secret"}, |
| 121 | + }, |
| 122 | + }) |
| 123 | + tctx := newTranslateContextWithTLS() |
| 124 | + |
| 125 | + sslObjs, err := tr.translateSecret(tctx, gateway.Spec.Listeners[0], gateway) |
| 126 | + require.NoError(t, err) |
| 127 | + require.Len(t, sslObjs, 1) |
| 128 | + require.NotNil(t, sslObjs[0].Client, "client mTLS config should be set") |
| 129 | + assert.Equal(t, testCACert, sslObjs[0].Client.CA) |
| 130 | + }) |
| 131 | + |
| 132 | + t.Run("missing CA Secret returns error", func(t *testing.T) { |
| 133 | + tr := &Translator{Log: logr.Discard()} |
| 134 | + gateway := newTLSGateway(&gatewayv1.FrontendTLSValidation{ |
| 135 | + CACertificateRefs: []gatewayv1.ObjectReference{ |
| 136 | + {Kind: "Secret", Name: "missing"}, |
| 137 | + }, |
| 138 | + }) |
| 139 | + tctx := newTranslateContextWithTLS() |
| 140 | + |
| 141 | + _, err := tr.translateSecret(tctx, gateway.Spec.Listeners[0], gateway) |
| 142 | + require.Error(t, err) |
| 143 | + }) |
| 144 | + |
| 145 | + t.Run("without frontendValidation leaves client nil", func(t *testing.T) { |
| 146 | + tr := &Translator{Log: logr.Discard()} |
| 147 | + gateway := newTLSGateway(nil) |
| 148 | + tctx := newTranslateContextWithTLS() |
| 149 | + |
| 150 | + sslObjs, err := tr.translateSecret(tctx, gateway.Spec.Listeners[0], gateway) |
| 151 | + require.NoError(t, err) |
| 152 | + require.Len(t, sslObjs, 1) |
| 153 | + assert.Nil(t, sslObjs[0].Client) |
| 154 | + }) |
| 155 | + |
| 156 | + t.Run("missing CA ConfigMap returns error", func(t *testing.T) { |
| 157 | + tr := &Translator{Log: logr.Discard()} |
| 158 | + gateway := newTLSGateway(&gatewayv1.FrontendTLSValidation{ |
| 159 | + CACertificateRefs: []gatewayv1.ObjectReference{ |
| 160 | + {Kind: "ConfigMap", Name: "missing"}, |
| 161 | + }, |
| 162 | + }) |
| 163 | + tctx := newTranslateContextWithTLS() |
| 164 | + |
| 165 | + _, err := tr.translateSecret(tctx, gateway.Spec.Listeners[0], gateway) |
| 166 | + require.Error(t, err) |
| 167 | + }) |
| 168 | + |
| 169 | + t.Run("unsupported CA ref kind returns error", func(t *testing.T) { |
| 170 | + tr := &Translator{Log: logr.Discard()} |
| 171 | + gateway := newTLSGateway(&gatewayv1.FrontendTLSValidation{ |
| 172 | + CACertificateRefs: []gatewayv1.ObjectReference{ |
| 173 | + {Kind: "Pod", Name: "ca-cm"}, |
| 174 | + }, |
| 175 | + }) |
| 176 | + tctx := newTranslateContextWithTLS() |
| 177 | + |
| 178 | + _, err := tr.translateSecret(tctx, gateway.Spec.Listeners[0], gateway) |
| 179 | + require.Error(t, err) |
| 180 | + }) |
| 181 | + |
| 182 | + t.Run("unsupported CA ref group returns error", func(t *testing.T) { |
| 183 | + tr := &Translator{Log: logr.Discard()} |
| 184 | + gateway := newTLSGateway(&gatewayv1.FrontendTLSValidation{ |
| 185 | + CACertificateRefs: []gatewayv1.ObjectReference{ |
| 186 | + {Group: "example.com", Kind: "ConfigMap", Name: "ca-cm"}, |
| 187 | + }, |
| 188 | + }) |
| 189 | + tctx := newTranslateContextWithTLS() |
| 190 | + |
| 191 | + _, err := tr.translateSecret(tctx, gateway.Spec.Listeners[0], gateway) |
| 192 | + require.Error(t, err) |
| 193 | + }) |
| 194 | + |
| 195 | + t.Run("malformed CA data returns error", func(t *testing.T) { |
| 196 | + tr := &Translator{Log: logr.Discard()} |
| 197 | + gateway := newTLSGateway(&gatewayv1.FrontendTLSValidation{ |
| 198 | + CACertificateRefs: []gatewayv1.ObjectReference{ |
| 199 | + {Kind: "ConfigMap", Name: "ca-cm"}, |
| 200 | + }, |
| 201 | + }) |
| 202 | + tctx := newTranslateContextWithTLS() |
| 203 | + tctx.ConfigMaps[types.NamespacedName{Namespace: "default", Name: "ca-cm"}] = &corev1.ConfigMap{ |
| 204 | + Data: map[string]string{corev1.ServiceAccountRootCAKey: " not a pem cert "}, |
| 205 | + } |
| 206 | + |
| 207 | + _, err := tr.translateSecret(tctx, gateway.Spec.Listeners[0], gateway) |
| 208 | + require.Error(t, err) |
| 209 | + }) |
| 210 | +} |
0 commit comments