ssl-protocols.t 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. repeat_each(1);
  19. log_level('info');
  20. no_root_location();
  21. no_shuffle();
  22. my $openssl_bin = $ENV{OPENSSL_BIN};
  23. if (! -x $openssl_bin) {
  24. $ENV{OPENSSL_BIN} = '/usr/local/openresty/openssl3/bin/openssl';
  25. if (! -x $ENV{OPENSSL_BIN}) {
  26. plan(skip_all => "openssl3 not installed");
  27. }
  28. }
  29. plan('no_plan');
  30. add_block_preprocessor(sub {
  31. my ($block) = @_;
  32. my $yaml_config = $block->yaml_config // <<_EOC_;
  33. deployment:
  34. role: traditional
  35. role_traditional:
  36. config_provider: etcd
  37. admin:
  38. admin_key: null
  39. apisix:
  40. node_listen: 1984
  41. proxy_mode: http&stream
  42. stream_proxy:
  43. tcp:
  44. - 9100
  45. enable_resolv_search_opt: false
  46. ssl:
  47. ssl_protocols: TLSv1.1 TLSv1.2 TLSv1.3
  48. ssl_ciphers: ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA
  49. _EOC_
  50. $block->set_value("yaml_config", $yaml_config);
  51. });
  52. run_tests();
  53. __DATA__
  54. === TEST 1: set route
  55. --- config
  56. location /t {
  57. content_by_lua_block {
  58. local t = require("lib.test_admin").test
  59. local code, body = t('/apisix/admin/routes/1',
  60. ngx.HTTP_PUT,
  61. [[{
  62. "upstream": {
  63. "nodes": {
  64. "127.0.0.1:1980": 1
  65. },
  66. "type": "roundrobin"
  67. },
  68. "uris": ["/hello", "/world"]
  69. }]]
  70. )
  71. if code >= 300 then
  72. ngx.status = code
  73. ngx.say(message)
  74. return
  75. end
  76. ngx.say(body)
  77. }
  78. }
  79. --- request
  80. GET /t
  81. --- response_body
  82. passed
  83. === TEST 2: create ssl for test.com (unset ssl_protocols)
  84. --- config
  85. location /t {
  86. content_by_lua_block {
  87. local core = require("apisix.core")
  88. local t = require("lib.test_admin")
  89. local ssl_cert = t.read_file("t/certs/apisix.crt")
  90. local ssl_key = t.read_file("t/certs/apisix.key")
  91. local data = {cert = ssl_cert, key = ssl_key, sni = "test.com"}
  92. local code, body = t.test('/apisix/admin/ssls/1',
  93. ngx.HTTP_PUT,
  94. core.json.encode(data),
  95. [[{
  96. "value": {
  97. "sni": "test.com",
  98. "ssl_protocols": null,
  99. },
  100. "key": "/apisix/ssls/1"
  101. }]]
  102. )
  103. ngx.status = code
  104. ngx.say(body)
  105. }
  106. }
  107. --- request
  108. GET /t
  109. --- response_body
  110. passed
  111. === TEST 3: Successfully, access test.com with TLSv1.3
  112. --- exec
  113. echo -n "Q" | $OPENSSL_BIN s_client -connect 127.0.0.1:1994 -servername test.com -tls1_3 2>&1 | cat
  114. --- response_body eval
  115. qr/Server certificate/
  116. === TEST 4: Successfully, access test.com with TLSv1.2
  117. --- exec
  118. curl -k -v --tls-max 1.2 --tlsv1.2 --resolve "test.com:1994:127.0.0.1" https://test.com:1994/hello 2>&1 | cat
  119. --- response_body eval
  120. qr/TLSv1\.2 \(IN\), TLS handshake, Server hello(?s).*hello world/
  121. === TEST 5: Successfully, access test.com with TLSv1.1
  122. --- exec
  123. echo -n "Q" | $OPENSSL_BIN s_client -connect 127.0.0.1:1994 -servername test.com -tls1_1 2>&1 | cat
  124. --- response_body eval
  125. qr/Server certificate/
  126. === TEST 6: set TLSv1.2 and TLSv1.3 for test.com
  127. --- config
  128. location /t {
  129. content_by_lua_block {
  130. local core = require("apisix.core")
  131. local t = require("lib.test_admin")
  132. local ssl_cert = t.read_file("t/certs/apisix.crt")
  133. local ssl_key = t.read_file("t/certs/apisix.key")
  134. local data = {cert = ssl_cert, key = ssl_key, sni = "test.com", ssl_protocols = {"TLSv1.2", "TLSv1.3"}}
  135. local code, body = t.test('/apisix/admin/ssls/1',
  136. ngx.HTTP_PUT,
  137. core.json.encode(data),
  138. [[{
  139. "value": {
  140. "sni": "test.com",
  141. "ssl_protocols": ["TLSv1.2", "TLSv1.3"],
  142. },
  143. "key": "/apisix/ssls/1"
  144. }]]
  145. )
  146. ngx.status = code
  147. ngx.say(body)
  148. }
  149. }
  150. --- request
  151. GET /t
  152. --- response_body
  153. passed
  154. === TEST 7: Set TLSv1.3 for the test2.com
  155. --- config
  156. location /t {
  157. content_by_lua_block {
  158. local core = require("apisix.core")
  159. local t = require("lib.test_admin")
  160. local ssl_cert = t.read_file("t/certs/test2.crt")
  161. local ssl_key = t.read_file("t/certs/test2.key")
  162. local data = {cert = ssl_cert, key = ssl_key, sni = "test2.com", ssl_protocols = {"TLSv1.3"}}
  163. local code, body = t.test('/apisix/admin/ssls/2',
  164. ngx.HTTP_PUT,
  165. core.json.encode(data),
  166. [[{
  167. "value": {
  168. "sni": "test2.com"
  169. },
  170. "key": "/apisix/ssls/2"
  171. }]]
  172. )
  173. ngx.status = code
  174. ngx.say(body)
  175. }
  176. }
  177. --- response_body
  178. passed
  179. --- request
  180. GET /t
  181. === TEST 8: Successfully, access test.com with TLSv1.3
  182. --- exec
  183. echo -n "Q" | $OPENSSL_BIN s_client -connect 127.0.0.1:1994 -servername test.com -tls1_3 2>&1 | cat
  184. --- response_body eval
  185. qr/Server certificate/
  186. === TEST 9: Successfully, access test.com with TLSv1.2
  187. --- exec
  188. curl -k -v --tls-max 1.2 --tlsv1.2 --resolve "test.com:1994:127.0.0.1" https://test.com:1994/hello 2>&1 | cat
  189. --- response_body eval
  190. qr/TLSv1\.2 \(IN\), TLS handshake, Server hello(?s).*hello world/
  191. === TEST 10: Successfully, access test2.com with TLSv1.3
  192. --- exec
  193. echo -n "Q" | $OPENSSL_BIN s_client -connect 127.0.0.1:1994 -servername test2.com -tls1_3 2>&1 | cat
  194. --- response_body eval
  195. qr/Server certificate/
  196. === TEST 11: Failed, access test2.com with TLSv1.2
  197. --- exec
  198. curl -k -v --tls-max 1.2 --tlsv1.2 --resolve "test2.com:1994:127.0.0.1" https://test2.com:1994/hello 2>&1 | cat
  199. --- response_body eval
  200. qr/TLSv1\.2 \(IN\), TLS alert/
  201. === TEST 12: set TLSv1.1 for test.com
  202. --- config
  203. location /t {
  204. content_by_lua_block {
  205. local core = require("apisix.core")
  206. local t = require("lib.test_admin")
  207. local ssl_cert = t.read_file("t/certs/apisix.crt")
  208. local ssl_key = t.read_file("t/certs/apisix.key")
  209. local data = {cert = ssl_cert, key = ssl_key, sni = "test.com", ssl_protocols = {"TLSv1.1"}}
  210. local code, body = t.test('/apisix/admin/ssls/1',
  211. ngx.HTTP_PUT,
  212. core.json.encode(data),
  213. [[{
  214. "value": {
  215. "sni": "test.com",
  216. "ssl_protocols": ["TLSv1.1"],
  217. },
  218. "key": "/apisix/ssls/1"
  219. }]]
  220. )
  221. ngx.status = code
  222. ngx.say(body)
  223. }
  224. }
  225. --- request
  226. GET /t
  227. --- response_body
  228. passed
  229. === TEST 13: Successfully, access test.com with TLSv1.1
  230. --- exec
  231. echo -n "Q" | $OPENSSL_BIN s_client -connect 127.0.0.1:1994 -servername test.com -tls1_1 2>&1 | cat
  232. --- response_body eval
  233. qr/Server certificate/
  234. === TEST 14: Failed, access test.com with TLSv1.3
  235. --- exec
  236. echo -n "Q" | $OPENSSL_BIN s_client -connect 127.0.0.1:1994 -servername test.com -tls1_3 2>&1 | cat
  237. --- response_body eval
  238. qr/tlsv1 alert/