consumer-plugin2.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 'no_plan';
  18. log_level('info');
  19. repeat_each(1);
  20. no_long_string();
  21. no_root_location();
  22. add_block_preprocessor(sub {
  23. my ($block) = @_;
  24. if (!$block->request) {
  25. $block->set_value("request", "GET /t");
  26. }
  27. if (!$block->response_body) {
  28. $block->set_value("response_body", "passed\n");
  29. }
  30. if (!$block->no_error_log && !$block->error_log) {
  31. $block->set_value("no_error_log", "[error]\n[alert]");
  32. }
  33. });
  34. our $debug_config = t::APISIX::read_file("conf/debug.yaml");
  35. $debug_config =~ s/basic:\n enable: false/basic:\n enable: true/;
  36. $debug_config =~ s/hook_conf:\n enable: false/hook_conf:\n enable: true/;
  37. run_tests;
  38. __DATA__
  39. === TEST 1: configure non-auth plugins in the consumer and run it's rewrite phase
  40. --- config
  41. location /t {
  42. content_by_lua_block {
  43. local t = require("lib.test_admin").test
  44. local code, body = t('/apisix/admin/consumers/jack',
  45. ngx.HTTP_PUT,
  46. [[{
  47. "username": "jack",
  48. "plugins": {
  49. "key-auth": {
  50. "key": "auth-jack"
  51. },
  52. "proxy-rewrite": {
  53. "uri": "/uri/plugin_proxy_rewrite",
  54. "headers": {
  55. "X-Api-Engine": "APISIX",
  56. "X-CONSUMER-ID": "1"
  57. }
  58. }
  59. }
  60. }]]
  61. )
  62. local code, body = t('/apisix/admin/routes/1',
  63. ngx.HTTP_PUT,
  64. [[{
  65. "plugins": {
  66. "key-auth": {}
  67. },
  68. "upstream": {
  69. "nodes": {
  70. "127.0.0.1:1980": 1
  71. },
  72. "type": "roundrobin"
  73. },
  74. "uri": "/hello"
  75. }]]
  76. )
  77. if code >= 300 then
  78. ngx.status = code
  79. end
  80. ngx.say(body)
  81. }
  82. }
  83. --- response_body
  84. passed
  85. === TEST 2: hit routes
  86. --- request
  87. GET /hello
  88. --- more_headers
  89. apikey: auth-jack
  90. --- response_body
  91. uri: /uri/plugin_proxy_rewrite
  92. apikey: auth-jack
  93. host: localhost
  94. x-api-engine: APISIX
  95. x-consumer-id: 1
  96. x-consumer-username: jack
  97. x-real-ip: 127.0.0.1
  98. === TEST 3: trace plugins info for debug
  99. --- debug_config eval: $::debug_config
  100. --- config
  101. location /t {
  102. content_by_lua_block {
  103. local json = require("toolkit.json")
  104. local ngx_re = require("ngx.re")
  105. local http = require "resty.http"
  106. local httpc = http.new()
  107. local headers = {}
  108. headers["apikey"] = "auth-jack"
  109. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  110. local res, err = httpc:request_uri(uri, {
  111. method = "GET",
  112. headers = headers,
  113. })
  114. local debug_header = res.headers["Apisix-Plugins"]
  115. local arr = ngx_re.split(debug_header, ", ")
  116. local hash = {}
  117. for i, v in ipairs(arr) do
  118. hash[v] = true
  119. end
  120. ngx.status = res.status
  121. ngx.say(json.encode(hash))
  122. }
  123. }
  124. --- response_body
  125. {"key-auth":true,"proxy-rewrite":true}
  126. === TEST 4: configure non-auth plugins in the route and run it's rewrite phase
  127. --- config
  128. location /t {
  129. content_by_lua_block {
  130. local t = require("lib.test_admin").test
  131. local code, body = t('/apisix/admin/consumers/jack',
  132. ngx.HTTP_PUT,
  133. [[{
  134. "username": "jack",
  135. "plugins": {
  136. "key-auth": {
  137. "key": "auth-jack"
  138. }
  139. }
  140. }]]
  141. )
  142. local code, body = t('/apisix/admin/routes/1',
  143. ngx.HTTP_PUT,
  144. [[{
  145. "plugins": {
  146. "key-auth": {},
  147. "proxy-rewrite": {
  148. "uri": "/uri/plugin_proxy_rewrite",
  149. "headers": {
  150. "X-Api-Engine": "APISIX",
  151. "X-CONSUMER-ID": "1"
  152. }
  153. }
  154. },
  155. "upstream": {
  156. "nodes": {
  157. "127.0.0.1:1980": 1
  158. },
  159. "type": "roundrobin"
  160. },
  161. "uri": "/hello"
  162. }]]
  163. )
  164. if code >= 300 then
  165. ngx.status = code
  166. end
  167. ngx.say(body)
  168. }
  169. }
  170. --- response_body
  171. passed
  172. === TEST 5: hit routes
  173. --- request
  174. GET /hello
  175. --- more_headers
  176. apikey: auth-jack
  177. --- response_body
  178. uri: /uri/plugin_proxy_rewrite
  179. apikey: auth-jack
  180. host: localhost
  181. x-api-engine: APISIX
  182. x-consumer-id: 1
  183. x-consumer-username: jack
  184. x-real-ip: 127.0.0.1
  185. === TEST 6: trace plugins info for debug
  186. --- debug_config eval: $::debug_config
  187. --- config
  188. location /t {
  189. content_by_lua_block {
  190. local json = require("toolkit.json")
  191. local ngx_re = require("ngx.re")
  192. local http = require "resty.http"
  193. local httpc = http.new()
  194. local headers = {}
  195. headers["apikey"] = "auth-jack"
  196. local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
  197. local res, err = httpc:request_uri(uri, {
  198. method = "GET",
  199. headers = headers,
  200. })
  201. local debug_header = res.headers["Apisix-Plugins"]
  202. local arr = ngx_re.split(debug_header, ", ")
  203. local hash = {}
  204. for i, v in ipairs(arr) do
  205. hash[v] = true
  206. end
  207. ngx.status = res.status
  208. ngx.say(json.encode(hash))
  209. }
  210. }
  211. --- response_body
  212. {"key-auth":true,"proxy-rewrite":true}
  213. === TEST 7: configure non-auth plugins in the consumer and run it's rewrite phase
  214. --- config
  215. location /t {
  216. content_by_lua_block {
  217. local t = require("lib.test_admin").test
  218. local code, body = t('/apisix/admin/consumers/jack',
  219. ngx.HTTP_PUT,
  220. [[{
  221. "username": "jack",
  222. "plugins": {
  223. "key-auth": {
  224. "key": "auth-jack"
  225. },
  226. "ip-restriction": {
  227. "blacklist": [
  228. "127.0.0.0/24"
  229. ]
  230. }
  231. }
  232. }]]
  233. )
  234. if code >= 300 then
  235. ngx.status = code
  236. ngx.say(body)
  237. return
  238. end
  239. local code, body = t('/apisix/admin/routes/1',
  240. ngx.HTTP_PUT,
  241. [[{
  242. "plugins": {
  243. "key-auth": {}
  244. },
  245. "upstream": {
  246. "nodes": {
  247. "127.0.0.1:1980": 1
  248. },
  249. "type": "roundrobin"
  250. },
  251. "uri": "/hello"
  252. }]]
  253. )
  254. if code >= 300 then
  255. ngx.status = code
  256. end
  257. ngx.say(body)
  258. }
  259. }
  260. --- response_body
  261. passed
  262. === TEST 8: hit routes and ip-restriction work well
  263. --- request
  264. GET /hello
  265. --- more_headers
  266. apikey: auth-jack
  267. --- error_code: 403
  268. --- response_body
  269. {"message":"Your IP address is not allowed"}
  270. === TEST 9: use the latest consumer modifiedIndex as lrucache key
  271. --- config
  272. location /t {
  273. content_by_lua_block {
  274. local t = require("lib.test_admin").test
  275. local code, body = t('/apisix/admin/consumers',
  276. ngx.HTTP_PUT,
  277. [[{
  278. "username": "foo",
  279. "plugins": {
  280. "basic-auth": {
  281. "username": "foo",
  282. "password": "bar"
  283. }
  284. }
  285. }]]
  286. )
  287. if code >= 300 then
  288. ngx.status = code
  289. ngx.say(body)
  290. return
  291. end
  292. local code, body = t('/apisix/admin/plugin_configs/1',
  293. ngx.HTTP_PUT,
  294. [[{
  295. "plugins": {
  296. "ip-restriction": {
  297. "whitelist": ["1.1.1.1"]
  298. },
  299. "basic-auth": {}
  300. }
  301. }]]
  302. )
  303. if code >= 300 then
  304. ngx.status = code
  305. ngx.say(body)
  306. return
  307. end
  308. local code, body = t('/apisix/admin/routes/1',
  309. ngx.HTTP_PUT,
  310. [[{
  311. "plugin_config_id": "1",
  312. "upstream": {
  313. "nodes": {
  314. "127.0.0.1:1980": 1
  315. },
  316. "type": "roundrobin"
  317. },
  318. "uris": ["/hello"]
  319. }]]
  320. )
  321. if code >= 300 then
  322. ngx.status = code
  323. ngx.say(body)
  324. return
  325. end
  326. ngx.sleep(0.5)
  327. local http = require "resty.http"
  328. local httpc = http.new()
  329. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  330. .. "/hello"
  331. local headers = {
  332. ["Authorization"] = "Basic Zm9vOmJhcg=="
  333. }
  334. local res, err = httpc:request_uri(uri, {headers = headers})
  335. ngx.print(res.body)
  336. local code, body = t('/apisix/admin/plugin_configs/1',
  337. ngx.HTTP_PUT,
  338. [[{
  339. "plugins": {
  340. "ip-restriction": {
  341. "whitelist": ["1.1.1.1", "127.0.0.1"]
  342. },
  343. "basic-auth": {}
  344. }
  345. }]]
  346. )
  347. if code >= 300 then
  348. ngx.status = code
  349. ngx.say(body)
  350. return
  351. end
  352. ngx.sleep(0.5)
  353. local res, err = httpc:request_uri(uri, {headers = headers})
  354. if not res then
  355. ngx.say(err)
  356. return
  357. end
  358. ngx.print(res.body)
  359. local code, body = t('/apisix/admin/consumers',
  360. ngx.HTTP_PUT,
  361. [[{
  362. "username": "foo",
  363. "plugins": {
  364. "basic-auth": {
  365. "username": "foo",
  366. "password": "bala"
  367. }
  368. }
  369. }]]
  370. )
  371. if code >= 300 then
  372. ngx.status = code
  373. ngx.say(body)
  374. return
  375. end
  376. ngx.sleep(0.5)
  377. local headers = {
  378. ["Authorization"] = "Basic Zm9vOmJhbGE="
  379. }
  380. local res, err = httpc:request_uri(uri, {headers = headers})
  381. if not res then
  382. ngx.say(err)
  383. return
  384. end
  385. ngx.print(res.body)
  386. }
  387. }
  388. --- response_body
  389. {"message":"Your IP address is not allowed"}
  390. hello world
  391. hello world
  392. === TEST 10: consumer should work if the etcd connection failed during starting
  393. --- extra_init_by_lua
  394. local etcd_apisix = require("apisix.core.etcd")
  395. etcd_apisix.get_etcd_syncer = function ()
  396. return nil, "", "ouch"
  397. end
  398. --- config
  399. location /t {
  400. content_by_lua_block {
  401. local http = require "resty.http"
  402. local httpc = http.new()
  403. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  404. .. "/hello"
  405. local headers = {
  406. ["Authorization"] = "Basic Zm9vOmJhbGE="
  407. }
  408. local res, err = httpc:request_uri(uri, {headers = headers})
  409. if not res then
  410. ngx.say(err)
  411. return
  412. end
  413. ngx.print(res.body)
  414. }
  415. }
  416. --- response_body
  417. hello world
  418. --- error_log
  419. failed to fetch data from etcd