|
| 1 | +use Test::Nginx::Socket::Lua; |
| 2 | + |
| 3 | +log_level('info'); |
| 4 | +no_long_string(); |
| 5 | +repeat_each(1); |
| 6 | + |
| 7 | +my $enable_tls = $ENV{ETCD_ENABLE_MTLS}; |
| 8 | +if (defined($enable_tls) && $enable_tls eq "TRUE") { |
| 9 | + plan 'no_plan'; |
| 10 | +} else { |
| 11 | + plan(skip_all => "etcd is not capable for mTLS connection"); |
| 12 | +} |
| 13 | + |
| 14 | +my $main_config = <<'_EOC_'; |
| 15 | + thread_pool grpc-client-nginx-module threads=1; |
| 16 | +_EOC_ |
| 17 | + |
| 18 | +my $http_config = <<'_EOC_'; |
| 19 | + lua_socket_log_errors off; |
| 20 | + lua_package_path 'lib/?.lua;/usr/share/lua/5.1/?.lua;;'; |
| 21 | + init_by_lua_block { |
| 22 | + local cjson = require("cjson.safe") |
| 23 | +
|
| 24 | + function check_res(data, err, val, status) |
| 25 | + if err then |
| 26 | + ngx.say("err: ", err) |
| 27 | + ngx.exit(200) |
| 28 | + end |
| 29 | +
|
| 30 | + if val then |
| 31 | + if data.body.kvs==nil then |
| 32 | + ngx.exit(404) |
| 33 | + end |
| 34 | + if data.body.kvs and val ~= data.body.kvs[1].value then |
| 35 | + ngx.say("failed to check value") |
| 36 | + ngx.log(ngx.ERR, "failed to check value, got: ", data.body.kvs[1].value, |
| 37 | + ", expect: ", val) |
| 38 | + ngx.exit(200) |
| 39 | + else |
| 40 | + ngx.say("checked val as expect: ", val) |
| 41 | + end |
| 42 | + end |
| 43 | +
|
| 44 | + if status and status ~= data.status then |
| 45 | + ngx.exit(data.status) |
| 46 | + end |
| 47 | + end |
| 48 | +
|
| 49 | + function new_etcd(ssl_verify, ssl_cert_path, ssl_key_path, trusted_ca) |
| 50 | + return require "resty.etcd" .new({ |
| 51 | + protocol = "v3", |
| 52 | + api_prefix = "/v3", |
| 53 | + http_host = { |
| 54 | + "https://127.0.0.1:12379", |
| 55 | + }, |
| 56 | + use_grpc = true, |
| 57 | + ssl_verify = ssl_verify, |
| 58 | + ssl_cert_path = ssl_cert_path or "t/certs/mtls_client.crt", |
| 59 | + ssl_key_path = ssl_key_path or "t/certs/mtls_client.key", |
| 60 | + trusted_ca = trusted_ca, |
| 61 | + }) |
| 62 | + end |
| 63 | + } |
| 64 | +_EOC_ |
| 65 | + |
| 66 | +add_block_preprocessor(sub { |
| 67 | + my ($block) = @_; |
| 68 | + |
| 69 | + if (!$block->request) { |
| 70 | + $block->set_value("request", "GET /t"); |
| 71 | + } |
| 72 | + |
| 73 | + if (!$block->http_config) { |
| 74 | + $block->set_value("http_config", $http_config); |
| 75 | + } |
| 76 | + |
| 77 | + if (!$block->main_config) { |
| 78 | + $block->set_value("main_config", $main_config); |
| 79 | + } |
| 80 | + |
| 81 | + if (!$block->no_error_log && !$block->error_log) { |
| 82 | + $block->set_value("no_error_log", "[error]\n[alert]"); |
| 83 | + } |
| 84 | +}); |
| 85 | + |
| 86 | +run_tests(); |
| 87 | + |
| 88 | +__DATA__ |
| 89 | +
|
| 90 | +=== TEST 1: TLS no verify |
| 91 | +--- config |
| 92 | + location /t { |
| 93 | + content_by_lua_block { |
| 94 | + local etcd, err = new_etcd(false) |
| 95 | + check_res(etcd, err) |
| 96 | +
|
| 97 | + local res, err = etcd:set("/test", { a='abc'}) |
| 98 | + check_res(res, err) |
| 99 | + ngx.say("done") |
| 100 | + } |
| 101 | + } |
| 102 | +--- response_body |
| 103 | +done |
| 104 | +
|
| 105 | +
|
| 106 | +
|
| 107 | +=== TEST 2: TLS verify |
| 108 | +--- config |
| 109 | + location /t { |
| 110 | + content_by_lua_block { |
| 111 | + local etcd, err = new_etcd(true) |
| 112 | + check_res(etcd, err) |
| 113 | +
|
| 114 | + local res, err = etcd:set("/test", { a='abc'}) |
| 115 | + check_res(res, err) |
| 116 | + ngx.say("done") |
| 117 | + } |
| 118 | + } |
| 119 | +--- response_body_like eval |
| 120 | +qr/cannot validate certificate/ |
| 121 | +
|
| 122 | +
|
| 123 | +
|
| 124 | +=== TEST 3: bad client certificate |
| 125 | +--- config |
| 126 | + location /t { |
| 127 | + content_by_lua_block { |
| 128 | + local etcd, err = new_etcd(false, "t/certs/etcd.pem", "t/certs/etcd.key") |
| 129 | + check_res(etcd, err) |
| 130 | +
|
| 131 | + local res, err = etcd:set("/test", { a='abc'}) |
| 132 | + check_res(res, err) |
| 133 | + ngx.say("done") |
| 134 | + } |
| 135 | + } |
| 136 | +--- response_body_like eval |
| 137 | +qr/bad certificate/ |
0 commit comments