ssl.t 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. BEGIN {
  18. sub set_env_from_file {
  19. my ($env_name, $file_path) = @_;
  20. open my $fh, '<', $file_path or die $!;
  21. my $content = do { local $/; <$fh> };
  22. close $fh;
  23. $ENV{$env_name} = $content;
  24. }
  25. # set env
  26. set_env_from_file('TEST_CERT', 't/certs/apisix.crt');
  27. set_env_from_file('TEST_KEY', 't/certs/apisix.key');
  28. set_env_from_file('TEST2_CERT', 't/certs/test2.crt');
  29. set_env_from_file('TEST2_KEY', 't/certs/test2.key');
  30. }
  31. use t::APISIX 'no_plan';
  32. log_level('info');
  33. no_root_location();
  34. sub set_env_from_file {
  35. my ($env_name, $file_path) = @_;
  36. open my $fh, '<', $file_path or die $!;
  37. my $content = do { local $/; <$fh> };
  38. close $fh;
  39. $ENV{$env_name} = $content;
  40. }
  41. add_block_preprocessor(sub {
  42. my ($block) = @_;
  43. if (!$block->request) {
  44. $block->set_value("request", "GET /t");
  45. }
  46. });
  47. run_tests;
  48. __DATA__
  49. === TEST 1: store two certs and keys in vault
  50. --- exec
  51. VAULT_TOKEN='root' VAULT_ADDR='http://0.0.0.0:8200' vault kv put kv/apisix/ssl \
  52. test.com.crt=@t/certs/apisix.crt \
  53. test.com.key=@t/certs/apisix.key \
  54. test.com.2.crt=@t/certs/test2.crt \
  55. test.com.2.key=@t/certs/test2.key
  56. --- response_body
  57. Success! Data written to: kv/apisix/ssl
  58. === TEST 2: set secret
  59. --- config
  60. location /t {
  61. content_by_lua_block {
  62. local t = require("lib.test_admin").test
  63. local code, body = t('/apisix/admin/secrets/vault/test',
  64. ngx.HTTP_PUT,
  65. [[{
  66. "uri": "http://0.0.0.0:8200",
  67. "prefix": "kv/apisix",
  68. "token": "root"
  69. }]],
  70. [[{
  71. "key": "/apisix/secrets/vault/test",
  72. "value": {
  73. "uri": "http://0.0.0.0:8200",
  74. "prefix": "kv/apisix",
  75. "token": "root"
  76. }
  77. }]]
  78. )
  79. ngx.status = code
  80. ngx.say(body)
  81. }
  82. }
  83. --- request
  84. GET /t
  85. --- response_body
  86. passed
  87. === TEST 3: set ssl with two certs and keys in vault
  88. --- config
  89. location /t {
  90. content_by_lua_block {
  91. local core = require("apisix.core")
  92. local t = require("lib.test_admin")
  93. local data = {
  94. snis = {"test.com"},
  95. key = "$secret://vault/test/ssl/test.com.key",
  96. cert = "$secret://vault/test/ssl/test.com.crt",
  97. keys = {"$secret://vault/test/ssl/test.com.2.key"},
  98. certs = {"$secret://vault/test/ssl/test.com.2.crt"}
  99. }
  100. local code, body = t.test('/apisix/admin/ssls/1',
  101. ngx.HTTP_PUT,
  102. core.json.encode(data),
  103. [[{
  104. "value": {
  105. "snis": ["test.com"],
  106. "key": "$secret://vault/test/ssl/test.com.key",
  107. "cert": "$secret://vault/test/ssl/test.com.crt",
  108. "keys": ["$secret://vault/test/ssl/test.com.2.key"],
  109. "certs": ["$secret://vault/test/ssl/test.com.2.crt"]
  110. },
  111. "key": "/apisix/ssls/1"
  112. }]]
  113. )
  114. ngx.status = code
  115. ngx.say(body)
  116. }
  117. }
  118. --- request
  119. GET /t
  120. --- response_body
  121. passed
  122. === TEST 4: set route
  123. --- config
  124. location /t {
  125. content_by_lua_block {
  126. local t = require("lib.test_admin").test
  127. local code, body = t('/apisix/admin/routes/2',
  128. ngx.HTTP_PUT,
  129. [[{
  130. "upstream": {
  131. "nodes": {
  132. "127.0.0.1:1980": 1
  133. },
  134. "type": "roundrobin"
  135. },
  136. "uri": "/hello"
  137. }]]
  138. )
  139. if code >= 300 then
  140. ngx.status = code
  141. end
  142. ngx.say(body)
  143. }
  144. }
  145. --- request
  146. GET /t
  147. --- response_body
  148. passed
  149. === TEST 5: access to https with test.com
  150. --- exec
  151. curl -s -k https://test.com:1994/hello
  152. --- response_body
  153. hello world
  154. --- error_log
  155. fetching data from secret uri
  156. fetching data from secret uri
  157. fetching data from secret uri
  158. fetching data from secret uri
  159. === TEST 6: set ssl with two certs and keys in env
  160. --- config
  161. location /t {
  162. content_by_lua_block {
  163. local core = require("apisix.core")
  164. local t = require("lib.test_admin")
  165. local data = {
  166. snis = {"test.com"},
  167. key = "$env://TEST_KEY",
  168. cert = "$env://TEST_CERT",
  169. keys = {"$env://TEST2_KEY"},
  170. certs = {"$env://TEST2_CERT"}
  171. }
  172. local code, body = t.test('/apisix/admin/ssls/1',
  173. ngx.HTTP_PUT,
  174. core.json.encode(data),
  175. [[{
  176. "value": {
  177. "snis": ["test.com"],
  178. "key": "$env://TEST_KEY",
  179. "cert": "$env://TEST_CERT",
  180. "keys": ["$env://TEST2_KEY"],
  181. "certs": ["$env://TEST2_CERT"]
  182. },
  183. "key": "/apisix/ssls/1"
  184. }]]
  185. )
  186. ngx.status = code
  187. ngx.say(body)
  188. }
  189. }
  190. --- request
  191. GET /t
  192. --- response_body
  193. passed
  194. === TEST 7: access to https with test.com
  195. --- exec
  196. curl -s -k https://test.com:1994/hello
  197. --- response_body
  198. hello world
  199. --- error_log
  200. fetching data from env uri
  201. fetching data from env uri
  202. fetching data from env uri
  203. fetching data from env uri