-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: Fetch secrets from upstream ssl config #13062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,22 @@ | |||||
| # See the License for the specific language governing permissions and | ||||||
| # limitations under the License. | ||||||
| # | ||||||
|
|
||||||
| BEGIN { | ||||||
| sub set_env_from_file { | ||||||
| my ($env_name, $file_path) = @_; | ||||||
|
|
||||||
| open my $fh, '<', $file_path or die $!; | ||||||
| my $content = do { local $/; <$fh> }; | ||||||
| close $fh; | ||||||
|
|
||||||
| $ENV{$env_name} = $content; | ||||||
| } | ||||||
| # set env | ||||||
| set_env_from_file('MTLS_CERT_VAR', 't/certs/mtls_client.crt'); | ||||||
| set_env_from_file('MTLS_KEY_VAR', 't/certs/mtls_client.key'); | ||||||
| } | ||||||
|
|
||||||
| use t::APISIX; | ||||||
|
|
||||||
| my $nginx_binary = $ENV{'TEST_NGINX_BINARY'} || 'nginx'; | ||||||
|
|
@@ -38,7 +54,17 @@ run_tests(); | |||||
|
|
||||||
| __DATA__ | ||||||
|
|
||||||
| === TEST 1: tls without key | ||||||
| === TEST 1: store cert and key in vault | ||||||
| --- exec | ||||||
| VAULT_TOKEN='root' VAULT_ADDR='http://0.0.0.0:8200' vault kv put kv/apisix/ssl \ | ||||||
| mtls_client.crt=@t/certs/mtls_client.crt \ | ||||||
| mtls_client.key=@t/certs/mtls_client.key | ||||||
| --- response_body | ||||||
| Success! Data written to: kv/apisix/ssl | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 2: tls without key | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -77,7 +103,7 @@ GET /t | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 2: tls with bad key | ||||||
| === TEST 3: tls with bad key | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -119,7 +145,7 @@ decrypt ssl key failed | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 3: encrypt key by default | ||||||
| === TEST 4: encrypt key by default | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -248,7 +274,7 @@ false | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 4: hit | ||||||
| === TEST 5: hit | ||||||
| --- upstream_server_config | ||||||
| ssl_client_certificate ../../certs/mtls_ca.crt; | ||||||
| ssl_verify_client on; | ||||||
|
|
@@ -259,7 +285,7 @@ hello world | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 5: wrong cert | ||||||
| === TEST 6: wrong cert | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -300,7 +326,7 @@ passed | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 6: hit | ||||||
| === TEST 7: hit | ||||||
| --- upstream_server_config | ||||||
| ssl_client_certificate ../../certs/mtls_ca.crt; | ||||||
| ssl_verify_client on; | ||||||
|
|
@@ -312,7 +338,7 @@ client SSL certificate verify error | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 7: clean old data | ||||||
| === TEST 8: clean old data | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -333,7 +359,7 @@ GET /t | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 8: don't encrypt key | ||||||
| === TEST 9: don't encrypt key | ||||||
| --- yaml_config | ||||||
| apisix: | ||||||
| node_listen: 1984 | ||||||
|
|
@@ -467,7 +493,7 @@ true | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 9: bind upstream | ||||||
| === TEST 10: bind upstream | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -494,7 +520,7 @@ GET /t | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 10: hit | ||||||
| === TEST 11: hit | ||||||
| --- upstream_server_config | ||||||
| ssl_client_certificate ../../certs/mtls_ca.crt; | ||||||
| ssl_verify_client on; | ||||||
|
|
@@ -505,7 +531,7 @@ GET /server_port | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 11: bind service | ||||||
| === TEST 12: bind service | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -532,7 +558,7 @@ GET /t | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 12: hit | ||||||
| === TEST 13: hit | ||||||
| --- upstream_server_config | ||||||
| ssl_client_certificate ../../certs/mtls_ca.crt; | ||||||
| ssl_verify_client on; | ||||||
|
|
@@ -543,7 +569,7 @@ hello world | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 13: get cert by tls.client_cert_id | ||||||
| === TEST 14: get cert by tls.client_cert_id | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -598,7 +624,7 @@ GET /t | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 14: hit | ||||||
| === TEST 15: hit | ||||||
| --- upstream_server_config | ||||||
| ssl_client_certificate ../../certs/mtls_ca.crt; | ||||||
| ssl_verify_client on; | ||||||
|
|
@@ -609,7 +635,7 @@ hello world | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 15: change ssl object type | ||||||
| === TEST 16: change ssl object type | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -640,7 +666,7 @@ GET /t | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 16: hit, ssl object type mismatch | ||||||
| === TEST 17: hit, ssl object type mismatch | ||||||
| --- upstream_server_config | ||||||
| ssl_client_certificate ../../certs/mtls_ca.crt; | ||||||
| ssl_verify_client on; | ||||||
|
|
@@ -652,7 +678,7 @@ failed to get ssl cert: ssl type should be 'client' | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 17: delete ssl object | ||||||
| === TEST 18: delete ssl object | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -673,7 +699,7 @@ GET /t | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 18: hit, ssl object not exits | ||||||
| === TEST 19: hit, ssl object not exits | ||||||
| --- upstream_server_config | ||||||
| ssl_client_certificate ../../certs/mtls_ca.crt; | ||||||
| ssl_verify_client on; | ||||||
|
|
@@ -685,7 +711,7 @@ failed to get ssl cert: ssl id [1] not exits | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 19: `tls.verify` only | ||||||
| === TEST 20: `tls.verify` only | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -723,7 +749,7 @@ passed | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 20: hit | ||||||
| === TEST 21: hit | ||||||
| When only `tls.verify` is present, the matching logic related to | ||||||
| `client_cert`, `client_key` or `client_cert_id` should not be entered | ||||||
| --- request | ||||||
|
|
@@ -733,7 +759,7 @@ hello world | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 21: set `verify` with `client_cert`, `client_key` | ||||||
| === TEST 22: set `verify` with `client_cert`, `client_key` | ||||||
| --- config | ||||||
| location /t { | ||||||
| content_by_lua_block { | ||||||
|
|
@@ -774,7 +800,7 @@ passed | |||||
|
|
||||||
|
|
||||||
|
|
||||||
| === TEST 22: hit | ||||||
| === TEST 23: hit | ||||||
| `tls.verify` does not affect the parsing of `client_cert`, `client_key` | ||||||
| --- upstream_server_config | ||||||
| ssl_client_certificate ../../certs/mtls_ca.crt; | ||||||
|
|
@@ -783,3 +809,125 @@ passed | |||||
| GET /hello | ||||||
| --- response_body | ||||||
| hello world | ||||||
|
|
||||||
| === TEST 24: get cert by tls.client_cert_id with secrets using secrets | ||||||
|
||||||
| === TEST 24: get cert by tls.client_cert_id with secrets using secrets | |
| === TEST 24: get cert by tls.client_cert_id with $secret:// refs |
Copilot
AI
Mar 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file writes the cert/key into Vault and then later references $secret://vault/test/..., but it never configures the Vault secret backend in APISIX (e.g., via PUT /apisix/admin/secrets/vault/test with uri/prefix/token like other tests do). Without that, secret resolution will fail and APISIX will keep the $secret://... strings, causing the mTLS handshake to fail for reasons unrelated to the upstream client_cert_id fix. Add a step to create the Vault secret config before using $secret://vault/test/....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apisix_secret.fetch_secretsonly returnsnilwhen the input is missing or not a table; for a validupstream_ssltable it will always return a (deep-copied) table even if individual secret lookups fail (it leaves the original$env:///$secret://string in place and logs). As a result, theif not new_upstream_sslbranch is effectively unreachable and won’t prevent the later TLS/decrypt errors this PR is trying to avoid. Consider removing this check, or updating the secrets API/usage to surface lookup failures (e.g., returnnil, error explicitly detect unresolved secret refs after substitution) and handle that here.