client-mtls-openresty.t 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. use t::APISIX;
  18. my $nginx_binary = $ENV{'TEST_NGINX_BINARY'} || 'nginx';
  19. my $version = eval { `$nginx_binary -V 2>&1` };
  20. if ($version !~ m/\/apisix-nginx-module/) {
  21. plan('no_plan');
  22. } else {
  23. plan(skip_all => "for vanilla OpenResty only");
  24. }
  25. repeat_each(1);
  26. log_level('info');
  27. no_root_location();
  28. no_shuffle();
  29. add_block_preprocessor(sub {
  30. my ($block) = @_;
  31. });
  32. run_tests();
  33. __DATA__
  34. === TEST 1: set verification
  35. --- config
  36. location /t {
  37. content_by_lua_block {
  38. local t = require("lib.test_admin")
  39. local json = require("toolkit.json")
  40. local ssl_ca_cert = t.read_file("t/certs/mtls_ca.crt")
  41. local ssl_cert = t.read_file("t/certs/mtls_client.crt")
  42. local ssl_key = t.read_file("t/certs/mtls_client.key")
  43. local data = {
  44. upstream = {
  45. type = "roundrobin",
  46. nodes = {
  47. ["127.0.0.1:1980"] = 1,
  48. },
  49. },
  50. uri = "/hello"
  51. }
  52. assert(t.test('/apisix/admin/routes/1',
  53. ngx.HTTP_PUT,
  54. json.encode(data)
  55. ))
  56. local data = {
  57. cert = ssl_cert,
  58. key = ssl_key,
  59. sni = "localhost",
  60. client = {
  61. ca = ssl_ca_cert,
  62. depth = 2,
  63. }
  64. }
  65. local code, body = t.test('/apisix/admin/ssls/1',
  66. ngx.HTTP_PUT,
  67. json.encode(data)
  68. )
  69. if code >= 300 then
  70. ngx.status = code
  71. end
  72. ngx.print(body)
  73. }
  74. }
  75. --- request
  76. GET /t
  77. === TEST 2: hit
  78. --- exec
  79. curl --cert t/certs/mtls_client.crt --key t/certs/mtls_client.key -k https://localhost:1994/hello
  80. --- response_body
  81. hello world
  82. === TEST 3: no client certificate
  83. --- exec
  84. curl -k https://localhost:1994/hello
  85. --- response_body eval
  86. qr/400 Bad Request/
  87. --- error_log
  88. client certificate was not present
  89. === TEST 4: wrong client certificate
  90. --- exec
  91. curl --cert t/certs/apisix.crt --key t/certs/apisix.key -k https://localhost:1994/hello
  92. --- response_body eval
  93. qr/400 Bad Request/
  94. --- error_log eval
  95. qr/client certificate verification is not passed: FAILED:self[- ]signed certificate/
  96. === TEST 5: hit with different host which doesn't require mTLS
  97. --- exec
  98. curl --cert t/certs/mtls_client.crt --key t/certs/mtls_client.key -k https://localhost:1994/hello -H "Host: test.com"
  99. --- response_body
  100. hello world
  101. === TEST 6: set verification (2 ssl objects)
  102. --- config
  103. location /t {
  104. content_by_lua_block {
  105. local t = require("lib.test_admin")
  106. local json = require("toolkit.json")
  107. local ssl_ca_cert = t.read_file("t/certs/mtls_ca.crt")
  108. local ssl_cert = t.read_file("t/certs/mtls_client.crt")
  109. local ssl_key = t.read_file("t/certs/mtls_client.key")
  110. local data = {
  111. upstream = {
  112. type = "roundrobin",
  113. nodes = {
  114. ["127.0.0.1:1980"] = 1,
  115. },
  116. },
  117. uri = "/hello"
  118. }
  119. assert(t.test('/apisix/admin/routes/1',
  120. ngx.HTTP_PUT,
  121. json.encode(data)
  122. ))
  123. local data = {
  124. cert = ssl_cert,
  125. key = ssl_key,
  126. sni = "test.com",
  127. client = {
  128. ca = ssl_ca_cert,
  129. depth = 2,
  130. }
  131. }
  132. local code, body = t.test('/apisix/admin/ssls/1',
  133. ngx.HTTP_PUT,
  134. json.encode(data)
  135. )
  136. if code >= 300 then
  137. ngx.status = code
  138. return
  139. end
  140. local data = {
  141. cert = ssl_cert,
  142. key = ssl_key,
  143. sni = "localhost",
  144. }
  145. local code, body = t.test('/apisix/admin/ssls/2',
  146. ngx.HTTP_PUT,
  147. json.encode(data)
  148. )
  149. if code >= 300 then
  150. ngx.status = code
  151. end
  152. ngx.print(body)
  153. }
  154. }
  155. --- request
  156. GET /t
  157. === TEST 7: hit without mTLS verify, with Host requires mTLS verification
  158. --- exec
  159. curl -k https://localhost:1994/hello -H "Host: test.com"
  160. --- response_body eval
  161. qr/400 Bad Request/
  162. --- error_log
  163. client certificate was not present
  164. === TEST 8: set verification (2 ssl objects, both have mTLS)
  165. --- config
  166. location /t {
  167. content_by_lua_block {
  168. local t = require("lib.test_admin")
  169. local json = require("toolkit.json")
  170. local ssl_ca_cert = t.read_file("t/certs/mtls_ca.crt")
  171. local ssl_ca_cert2 = t.read_file("t/certs/apisix.crt")
  172. local ssl_cert = t.read_file("t/certs/mtls_client.crt")
  173. local ssl_key = t.read_file("t/certs/mtls_client.key")
  174. local data = {
  175. upstream = {
  176. type = "roundrobin",
  177. nodes = {
  178. ["127.0.0.1:1980"] = 1,
  179. },
  180. },
  181. uri = "/hello"
  182. }
  183. assert(t.test('/apisix/admin/routes/1',
  184. ngx.HTTP_PUT,
  185. json.encode(data)
  186. ))
  187. local data = {
  188. cert = ssl_cert,
  189. key = ssl_key,
  190. sni = "localhost",
  191. client = {
  192. ca = ssl_ca_cert,
  193. depth = 2,
  194. }
  195. }
  196. local code, body = t.test('/apisix/admin/ssls/1',
  197. ngx.HTTP_PUT,
  198. json.encode(data)
  199. )
  200. if code >= 300 then
  201. ngx.status = code
  202. return
  203. end
  204. local data = {
  205. cert = ssl_cert,
  206. key = ssl_key,
  207. sni = "test.com",
  208. client = {
  209. ca = ssl_ca_cert2,
  210. depth = 2,
  211. }
  212. }
  213. local code, body = t.test('/apisix/admin/ssls/2',
  214. ngx.HTTP_PUT,
  215. json.encode(data)
  216. )
  217. if code >= 300 then
  218. ngx.status = code
  219. end
  220. ngx.print(body)
  221. }
  222. }
  223. --- request
  224. GET /t
  225. === TEST 9: hit with mTLS verify, with Host requires different mTLS verification
  226. --- exec
  227. curl --cert t/certs/mtls_client.crt --key t/certs/mtls_client.key -k https://localhost:1994/hello -H "Host: test.com"
  228. --- response_body eval
  229. qr/400 Bad Request/
  230. --- error_log
  231. client certificate verified with SNI localhost, but the host is test.com