Skip to content

Commit 511dd7b

Browse files
committed
add test
1 parent 392b0f4 commit 511dd7b

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

t/upstream_mtls2.t

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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: send 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+
f = assert(io.open("t/certs/mtls_ca.crt"))
60+
local ca_data = f:read("*a")
61+
f:close()
62+
63+
local ca_cert = assert(ssl.parse_pem_cert(ca_data))
64+
65+
local openssl_x509_store = require "resty.openssl.x509.store"
66+
local openssl_x509 = require "resty.openssl.x509"
67+
local trust_store, err = openssl_x509_store.new()
68+
if err then
69+
ngx.log(ngx.ERR, "failed to create trust store: ", err)
70+
ngx.exit(500)
71+
end
72+
73+
local x509, err = openssl_x509.new(ca_data, "PEM")
74+
75+
local _, err = trust_store:add(x509)
76+
if err then
77+
ngx.log(ngx.ERR, "failed to add ca cert to trust store: ", err)
78+
ngx.exit(500)
79+
end
80+
81+
local ok, err = upstream.set_ssl_trusted_store(trust_store)
82+
if not ok then
83+
ngx.log(ngx.ERR, "set_ssl_trusted_store failed: ", err)
84+
ngx.exit(500)
85+
end
86+
}
87+
88+
proxy_ssl_trusted_certificate ../../certs/mtls_client.crt;
89+
proxy_ssl_verify on;
90+
proxy_ssl_name admin.apisix.dev;
91+
proxy_pass https://unix:$TEST_NGINX_HTML_DIR/nginx.sock:/foo;
92+
}
93+
94+
--- response_body
95+
ok

0 commit comments

Comments
 (0)