Skip to content

Commit 476783a

Browse files
authored
chore: add skip_mtls_uril_regex test for ApisixTLS (#2555)
1 parent 40ae032 commit 476783a

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

test/e2e/crds/v2/tls.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,96 @@ spec:
245245
assert.Equal(GinkgoT(), caCert, tls[0].Client.CA, "client CA should be test-ca-secret")
246246
assert.Equal(GinkgoT(), int64(1), *tls[0].Client.Depth, "client depth should be 1")
247247
})
248+
It("ApisixTls with skip_mtls_uri_regex test", func() {
249+
const host = "api6.com"
250+
const skipMtlsUriRegex = "/ip.*"
251+
252+
By("generate mTLS certificates")
253+
caCertBytes, serverCertBytes, serverKeyBytes, _, _ := s.GenerateMACert(GinkgoT(), []string{host})
254+
caCert := caCertBytes.String()
255+
serverCert := serverCertBytes.String()
256+
serverKey := serverKeyBytes.String()
257+
258+
By("create server TLS secret")
259+
err := s.NewKubeTlsSecret("test-mtls-server-secret", serverCert, serverKey)
260+
Expect(err).NotTo(HaveOccurred(), "creating server TLS secret")
261+
262+
By("create client CA secret")
263+
err = s.NewClientCASecret("test-client-ca-secret", caCert, "")
264+
Expect(err).NotTo(HaveOccurred(), "creating client CA secret")
265+
266+
const apisixTlsSpec = `
267+
apiVersion: apisix.apache.org/v2
268+
kind: ApisixTls
269+
metadata:
270+
name: test-mtls-skip-regex
271+
spec:
272+
ingressClassName: %s
273+
hosts:
274+
- %s
275+
secret:
276+
name: test-mtls-server-secret
277+
namespace: %s
278+
client:
279+
caSecret:
280+
name: test-client-ca-secret
281+
namespace: %s
282+
depth: 10
283+
skip_mtls_uri_regex:
284+
- %s
285+
`
286+
287+
By("apply ApisixTls with mTLS and skip_mtls_uri_regex")
288+
var apisixTls apiv2.ApisixTls
289+
tlsSpec := fmt.Sprintf(apisixTlsSpec, s.Namespace(), host, s.Namespace(), s.Namespace(), skipMtlsUriRegex)
290+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "test-mtls-skip-regex"}, &apisixTls, tlsSpec)
291+
292+
By("verify mTLS configuration with skip_mtls_uri_regex")
293+
Eventually(func() bool {
294+
tls, err := s.DefaultDataplaneResource().SSL().List(context.Background())
295+
if err != nil {
296+
return false
297+
}
298+
if len(tls) != 1 {
299+
return false
300+
}
301+
return tls[0].Client != nil &&
302+
tls[0].Client.CA != "" &&
303+
len(tls[0].Client.SkipMtlsURIRegex) > 0 &&
304+
tls[0].Client.SkipMtlsURIRegex[0] == skipMtlsUriRegex
305+
}).WithTimeout(30 * time.Second).ProbeEvery(1 * time.Second).Should(BeTrue())
306+
307+
By("test HTTPS request to path matching skip_mtls_uri_regex without client cert")
308+
Eventually(func() int {
309+
return s.NewAPISIXHttpsClient(host).
310+
GET("/ip").
311+
WithHost(host).
312+
Expect().
313+
Raw().StatusCode
314+
}).WithTimeout(30 * time.Second).ProbeEvery(1 * time.Second).Should(Equal(http.StatusOK))
315+
316+
By("test HTTPS request to non-matching path without client cert should fail")
317+
Eventually(func() bool {
318+
resp := s.NewAPISIXHttpsClient(host).
319+
GET("/get").
320+
WithHost(host).
321+
Expect().
322+
Raw()
323+
return resp.StatusCode == http.StatusBadRequest ||
324+
resp.StatusCode == http.StatusForbidden ||
325+
resp.StatusCode >= 500
326+
}).WithTimeout(30 * time.Second).ProbeEvery(1 * time.Second).Should(BeTrue())
327+
328+
// Verify the configuration details
329+
tls, err := s.DefaultDataplaneResource().SSL().List(context.Background())
330+
assert.Nil(GinkgoT(), err, "list tls error")
331+
assert.Len(GinkgoT(), tls, 1, "tls number not expect")
332+
assert.NotNil(GinkgoT(), tls[0].Client, "client configuration should not be nil")
333+
assert.NotEmpty(GinkgoT(), tls[0].Client.CA, "client CA should not be empty")
334+
assert.Equal(GinkgoT(), caCert, tls[0].Client.CA, "client CA should match")
335+
assert.Equal(GinkgoT(), int64(10), *tls[0].Client.Depth, "client depth should be 10")
336+
assert.Contains(GinkgoT(), tls[0].Client.SkipMtlsURIRegex, skipMtlsUriRegex, "skip_mtls_uri_regex should be set")
337+
})
248338

249339
})
250340
})

0 commit comments

Comments
 (0)