|
| 1 | +use t::APISIX_NGINX 'no_plan'; |
| 2 | + |
| 3 | +repeat_each(2); |
| 4 | + |
| 5 | +add_block_preprocessor(sub { |
| 6 | + my ($block) = @_; |
| 7 | + |
| 8 | + if (!$block->http_config) { |
| 9 | + my $http_config = <<'_EOC_'; |
| 10 | + server { |
| 11 | + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl; |
| 12 | + server_name admin.apisix.dev; |
| 13 | + ssl_certificate ../../certs/mtls_server.crt; |
| 14 | + ssl_certificate_key ../../certs/mtls_server.key; |
| 15 | + ssl_client_certificate ../../certs/mtls_ca.crt; |
| 16 | + ssl_verify_client on; |
| 17 | +
|
| 18 | + server_tokens off; |
| 19 | +
|
| 20 | + location /foo { |
| 21 | + return 200 'ok\n'; |
| 22 | + } |
| 23 | + } |
| 24 | +
|
| 25 | +_EOC_ |
| 26 | + |
| 27 | + $block->set_value("http_config", $http_config); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +run_tests; |
| 32 | + |
| 33 | +__DATA__ |
| 34 | +
|
| 35 | +=== TEST 1: only set client certificate |
| 36 | +--- config |
| 37 | + location /t { |
| 38 | + access_by_lua_block { |
| 39 | + local upstream = require("resty.apisix.upstream") |
| 40 | + local ssl = require("ngx.ssl") |
| 41 | +
|
| 42 | + local f = assert(io.open("t/certs/mtls_client.crt")) |
| 43 | + local cert_data = f:read("*a") |
| 44 | + f:close() |
| 45 | +
|
| 46 | + local cert = assert(ssl.parse_pem_cert(cert_data)) |
| 47 | +
|
| 48 | + f = assert(io.open("t/certs/mtls_client.key")) |
| 49 | + local key_data = f:read("*a") |
| 50 | + f:close() |
| 51 | +
|
| 52 | + local key = assert(ssl.parse_pem_priv_key(key_data)) |
| 53 | +
|
| 54 | + local ok, err = upstream.set_cert_and_key(cert, key) |
| 55 | + if not ok then |
| 56 | + ngx.say("set_cert_and_key failed: ", err) |
| 57 | + end |
| 58 | + } |
| 59 | +
|
| 60 | + proxy_ssl_trusted_certificate ../../certs/mtls_client.crt; |
| 61 | + proxy_ssl_verify on; |
| 62 | + proxy_ssl_name admin.apisix.dev; |
| 63 | + proxy_pass https://unix:$TEST_NGINX_HTML_DIR/nginx.sock:/foo; |
| 64 | + } |
| 65 | +--- error_code: 502 |
| 66 | +--- error_log |
| 67 | +unable to verify the first certificate |
| 68 | +
|
| 69 | +
|
| 70 | +
|
| 71 | +=== TEST 2: send valid client certificate |
| 72 | +--- config |
| 73 | + location /t { |
| 74 | + access_by_lua_block { |
| 75 | + local upstream = require("resty.apisix.upstream") |
| 76 | + local ssl = require("ngx.ssl") |
| 77 | +
|
| 78 | + local f = assert(io.open("t/certs/mtls_client.crt")) |
| 79 | + local cert_data = f:read("*a") |
| 80 | + f:close() |
| 81 | +
|
| 82 | + local cert = assert(ssl.parse_pem_cert(cert_data)) |
| 83 | +
|
| 84 | + f = assert(io.open("t/certs/mtls_client.key")) |
| 85 | + local key_data = f:read("*a") |
| 86 | + f:close() |
| 87 | +
|
| 88 | + local key = assert(ssl.parse_pem_priv_key(key_data)) |
| 89 | +
|
| 90 | + local ok, err = upstream.set_cert_and_key(cert, key) |
| 91 | + if not ok then |
| 92 | + ngx.say("set_cert_and_key failed: ", err) |
| 93 | + end |
| 94 | +
|
| 95 | + f = assert(io.open("t/certs/mtls_ca.crt")) |
| 96 | + local ca_data = f:read("*a") |
| 97 | + f:close() |
| 98 | +
|
| 99 | + local ca_cert = assert(ssl.parse_pem_cert(ca_data)) |
| 100 | +
|
| 101 | + local openssl_x509_store = require "resty.openssl.x509.store" |
| 102 | + local openssl_x509 = require "resty.openssl.x509" |
| 103 | + local trust_store, err = openssl_x509_store.new() |
| 104 | + if err then |
| 105 | + ngx.log(ngx.ERR, "failed to create trust store: ", err) |
| 106 | + ngx.exit(500) |
| 107 | + end |
| 108 | +
|
| 109 | + local x509, err = openssl_x509.new(ca_data, "PEM") |
| 110 | +
|
| 111 | + local _, err = trust_store:add(x509) |
| 112 | + if err then |
| 113 | + ngx.log(ngx.ERR, "failed to add ca cert to trust store: ", err) |
| 114 | + ngx.exit(500) |
| 115 | + end |
| 116 | +
|
| 117 | + local ok, err = upstream.set_ssl_trusted_store(trust_store) |
| 118 | + if not ok then |
| 119 | + ngx.log(ngx.ERR, "set_ssl_trusted_store failed: ", err) |
| 120 | + ngx.exit(500) |
| 121 | + end |
| 122 | + } |
| 123 | +
|
| 124 | + proxy_ssl_trusted_certificate ../../certs/mtls_client.crt; |
| 125 | + proxy_ssl_verify on; |
| 126 | + proxy_ssl_name admin.apisix.dev; |
| 127 | + proxy_pass https://unix:$TEST_NGINX_HTML_DIR/nginx.sock:/foo; |
| 128 | + } |
| 129 | +
|
| 130 | +--- response_body |
| 131 | +ok |
0 commit comments