Mutual TLS Authentication

Protect Admin API

Why use it

Mutual TLS authentication provides a better way to prevent unauthorized access to APISIX.

The clients will provide their certificates to the server and the server will check whether the cert is signed by the supplied CA and decide whether to serve the request.

How to configure

  1. Generate self-signed key pairs, including ca, server, client key pairs.

  2. Modify configuration items in conf/config.yaml:

conf/config.yaml

  1. admin_listen:
  2. ip: 127.0.0.1
  3. port: 9180
  4. https_admin: true
  5. admin_api_mtls:
  6. admin_ssl_ca_cert: "/data/certs/mtls_ca.crt" # Path of your self-signed ca cert.
  7. admin_ssl_cert: "/data/certs/mtls_server.crt" # Path of your self-signed server side cert.
  8. admin_ssl_cert_key: "/data/certs/mtls_server.key" # Path of your self-signed server side key.
  1. Run command:
  1. apisix init
  2. apisix reload

How client calls

Please replace the following certificate paths and domain name with your real ones.

  • Note: The same CA certificate as the server needs to be used *
Mutual TLS Authentication - 图1note

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

  1. admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
  1. curl --cacert /data/certs/mtls_ca.crt --key /data/certs/mtls_client.key --cert /data/certs/mtls_client.crt https://admin.apisix.dev:9180/apisix/admin/routes -H "X-API-KEY: $admin_key"

etcd with mTLS

How to configure

You need to configure etcd.tls for APISIX to work on an etcd cluster with mTLS enabled as shown below:

conf/config.yaml

  1. deployment:
  2. role: traditional
  3. role_traditional:
  4. config_provider: etcd
  5. etcd:
  6. tls:
  7. cert: /data/certs/etcd_client.pem # path of certificate used by the etcd client
  8. key: /data/certs/etcd_client.key # path of key used by the etcd client

If APISIX does not trust the CA certificate that used by etcd server, we need to set up the CA certificate.

conf/config.yaml

  1. apisix:
  2. ssl:
  3. ssl_trusted_certificate: /path/to/certs/ca-certificates.crt # path of CA certificate used by the etcd server

Protect Route

Why use it

Using mTLS is a way to verify clients cryptographically. It is useful and important in cases where you want to have encrypted and secure traffic in both directions.

  • Note: the mTLS protection only happens in HTTPS. If your route can also be accessed via HTTP, you should add additional protection in HTTP or disable the access via HTTP.*

How to configure

We provide a tutorial that explains in detail how to configure mTLS between the client and APISIX.

When configuring ssl, use parameter client.ca and client.depth to configure the root CA that signing client certificates and the max length of certificate chain. Please refer to Admin API for details.

Here is an example shell script to create SSL with mTLS (id is 1, changes admin API url if needed):

  1. curl http://127.0.0.1:9180/apisix/admin/ssls/1 \
  2. -H "X-API-KEY: $admin_key" -X PUT -d '
  3. {
  4. "cert": "'"$(cat t/certs/mtls_server.crt)"'",
  5. "key": "'"$(cat t/certs/mtls_server.key)"'",
  6. "snis": [
  7. "admin.apisix.dev"
  8. ],
  9. "client": {
  10. "ca": "'"$(cat t/certs/mtls_ca.crt)"'",
  11. "depth": 10
  12. }
  13. }'

Send a request to verify:

  1. curl --resolve 'mtls.test.com:<APISIX_HTTPS_PORT>:<APISIX_URL>' "https://<APISIX_URL>:<APISIX_HTTPS_PORT>/hello" -k --cert ./client.pem --key ./client.key
  2. * Added admin.apisix.dev:9443:127.0.0.1 to DNS cache
  3. * Hostname admin.apisix.dev was found in DNS cache
  4. * Trying 127.0.0.1:9443...
  5. * Connected to admin.apisix.dev (127.0.0.1) port 9443 (#0)
  6. * ALPN: offers h2
  7. * ALPN: offers http/1.1
  8. * CAfile: t/certs/mtls_ca.crt
  9. * CApath: none
  10. * [CONN-0-0][CF-SSL] (304) (OUT), TLS handshake, Client hello (1):
  11. * [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, Server hello (2):
  12. * [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, Unknown (8):
  13. * [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, Request CERT (13):
  14. * [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, Certificate (11):
  15. * [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, CERT verify (15):
  16. * [CONN-0-0][CF-SSL] (304) (IN), TLS handshake, Finished (20):
  17. * [CONN-0-0][CF-SSL] (304) (OUT), TLS handshake, Certificate (11):
  18. * [CONN-0-0][CF-SSL] (304) (OUT), TLS handshake, CERT verify (15):
  19. * [CONN-0-0][CF-SSL] (304) (OUT), TLS handshake, Finished (20):
  20. * SSL connection using TLSv1.3 / AEAD-AES256-GCM-SHA384
  21. * ALPN: server accepted h2
  22. * Server certificate:
  23. * subject: C=cn; ST=GuangDong; L=ZhuHai; CN=admin.apisix.dev; OU=ops
  24. * start date: Dec 1 10:17:24 2022 GMT
  25. * expire date: Aug 18 10:17:24 2042 GMT
  26. * subjectAltName: host "admin.apisix.dev" matched cert's "admin.apisix.dev"
  27. * issuer: C=cn; ST=GuangDong; L=ZhuHai; CN=ca.apisix.dev; OU=ops
  28. * SSL certificate verify ok.
  29. * Using HTTP2, server supports multiplexing
  30. * Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
  31. * h2h3 [:method: GET]
  32. * h2h3 [:path: /hello]
  33. * h2h3 [:scheme: https]
  34. * h2h3 [:authority: admin.apisix.dev:9443]
  35. * h2h3 [user-agent: curl/7.87.0]
  36. * h2h3 [accept: */*]
  37. * Using Stream ID: 1 (easy handle 0x13000bc00)
  38. > GET /hello HTTP/2
  39. > Host: admin.apisix.dev:9443
  40. > user-agent: curl/7.87.0
  41. > accept: */*

Please make sure that the SNI fits the certificate domain.

mTLS Between APISIX and Upstream

Why use it

Sometimes the upstream requires mTLS. In this situation, the APISIX acts as the client, it needs to provide client certificate to communicate with upstream.

How to configure

When configuring upstreams, we could use parameter tls.client_cert and tls.client_key to configure the client certificate APISIX used to communicate with upstreams. Please refer to Admin API for details.

This feature requires APISIX to run on APISIX-Runtime.

Here is a similar shell script to patch a existed upstream with mTLS (changes admin API url if needed):

  1. curl http://127.0.0.1:9180/apisix/admin/upstreams/1 \
  2. -H "X-API-KEY: $admin_key" -X PATCH -d '
  3. {
  4. "tls": {
  5. "client_cert": "'"$(cat t/certs/mtls_client.crt)"'",
  6. "client_key": "'"$(cat t/certs/mtls_client.key)"'"
  7. }
  8. }'