Skip to content

Commit d88a0d2

Browse files
authored
chore: add skip_mtls_uril_regex test for ApisixTLS (#260)
1 parent 3a932f0 commit d88a0d2

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

test/e2e/crds/v2/tls.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,99 @@ spec:
216216
assert.Equal(GinkgoT(), normalizePEM(caCert), normalizePEM(tls[0].Client.CA), "client CA should be test-ca-secret")
217217
assert.Equal(GinkgoT(), int64(1), *tls[0].Client.Depth, "client depth should be 1")
218218
})
219+
It("ApisixTls with skip_mtls_uri_regex test", func() {
220+
// TODO: Add support for skip_mtls_uri_regex in API7EE control plane
221+
if s.Deployer.Name() == framework.ProviderTypeAPI7EE {
222+
Skip("skipping test in API7EE mode")
223+
}
224+
const host = "api6.com"
225+
const skipMtlsUriRegex = "/ip.*"
226+
227+
By("generate mTLS certificates")
228+
caCertBytes, serverCertBytes, serverKeyBytes, _, _ := s.GenerateMACert(GinkgoT(), []string{host})
229+
caCert := caCertBytes.String()
230+
serverCert := serverCertBytes.String()
231+
serverKey := serverKeyBytes.String()
232+
233+
By("create server TLS secret")
234+
err := s.NewKubeTlsSecret("test-mtls-server-secret", serverCert, serverKey)
235+
Expect(err).NotTo(HaveOccurred(), "creating server TLS secret")
236+
237+
By("create client CA secret")
238+
err = s.NewClientCASecret("test-client-ca-secret", caCert, "")
239+
Expect(err).NotTo(HaveOccurred(), "creating client CA secret")
240+
241+
const apisixTlsSpec = `
242+
apiVersion: apisix.apache.org/v2
243+
kind: ApisixTls
244+
metadata:
245+
name: test-mtls-skip-regex
246+
spec:
247+
ingressClassName: %s
248+
hosts:
249+
- %s
250+
secret:
251+
name: test-mtls-server-secret
252+
namespace: %s
253+
client:
254+
caSecret:
255+
name: test-client-ca-secret
256+
namespace: %s
257+
depth: 10
258+
skip_mtls_uri_regex:
259+
- %s
260+
`
219261

262+
By("apply ApisixTls with mTLS and skip_mtls_uri_regex")
263+
var apisixTls apiv2.ApisixTls
264+
tlsSpec := fmt.Sprintf(apisixTlsSpec, s.Namespace(), host, s.Namespace(), s.Namespace(), skipMtlsUriRegex)
265+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "test-mtls-skip-regex"}, &apisixTls, tlsSpec)
266+
267+
By("verify mTLS configuration with skip_mtls_uri_regex")
268+
Eventually(func() bool {
269+
tls, err := s.DefaultDataplaneResource().SSL().List(context.Background())
270+
if err != nil {
271+
return false
272+
}
273+
if len(tls) != 1 {
274+
return false
275+
}
276+
return tls[0].Client != nil &&
277+
tls[0].Client.CA != "" &&
278+
len(tls[0].Client.SkipMtlsURIRegex) > 0 &&
279+
tls[0].Client.SkipMtlsURIRegex[0] == skipMtlsUriRegex
280+
}).WithTimeout(30 * time.Second).ProbeEvery(1 * time.Second).Should(BeTrue())
281+
282+
By("test HTTPS request to path matching skip_mtls_uri_regex without client cert")
283+
Eventually(func() int {
284+
return s.NewAPISIXHttpsClient(host).
285+
GET("/ip").
286+
WithHost(host).
287+
Expect().
288+
Raw().StatusCode
289+
}).WithTimeout(30 * time.Second).ProbeEvery(1 * time.Second).Should(Equal(http.StatusOK))
290+
291+
By("test HTTPS request to non-matching path without client cert should fail")
292+
Eventually(func() bool {
293+
resp := s.NewAPISIXHttpsClient(host).
294+
GET("/get").
295+
WithHost(host).
296+
Expect().
297+
Raw()
298+
return resp.StatusCode == http.StatusBadRequest ||
299+
resp.StatusCode == http.StatusForbidden ||
300+
resp.StatusCode >= 500
301+
}).WithTimeout(30 * time.Second).ProbeEvery(1 * time.Second).Should(BeTrue())
302+
303+
// Verify the configuration details
304+
tls, err := s.DefaultDataplaneResource().SSL().List(context.Background())
305+
assert.Nil(GinkgoT(), err, "list tls error")
306+
assert.Len(GinkgoT(), tls, 1, "tls number not expect")
307+
assert.NotNil(GinkgoT(), tls[0].Client, "client configuration should not be nil")
308+
assert.NotEmpty(GinkgoT(), tls[0].Client.CA, "client CA should not be empty")
309+
assert.Equal(GinkgoT(), caCert, tls[0].Client.CA, "client CA should match")
310+
assert.Equal(GinkgoT(), int64(10), *tls[0].Client.Depth, "client depth should be 10")
311+
assert.Contains(GinkgoT(), tls[0].Client.SkipMtlsURIRegex, skipMtlsUriRegex, "skip_mtls_uri_regex should be set")
312+
})
220313
})
221314
})

0 commit comments

Comments
 (0)