data_encrypt2.t 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. repeat_each(1);
  19. no_long_string();
  20. no_root_location();
  21. no_shuffle();
  22. log_level("info");
  23. add_block_preprocessor(sub {
  24. my ($block) = @_;
  25. if (!defined $block->request) {
  26. $block->set_value("request", "GET /t");
  27. }
  28. });
  29. run_tests();
  30. __DATA__
  31. === TEST 1: data encryption work well with plugins that not the auth plugins
  32. --- yaml_config
  33. apisix:
  34. data_encryption:
  35. enable_encrypt_fields: true
  36. keyring:
  37. - edd1c9f0985e76a2
  38. --- config
  39. location /t {
  40. content_by_lua_block {
  41. local json = require("toolkit.json")
  42. local t = require("lib.test_admin").test
  43. local code, body = t('/apisix/admin/routes/1',
  44. ngx.HTTP_PUT,
  45. [[{
  46. "plugins": {
  47. "clickhouse-logger": {
  48. "user": "default",
  49. "password": "abc123",
  50. "database": "default",
  51. "logtable": "t",
  52. "endpoint_addr": "http://127.0.0.1:1980/clickhouse_logger_server",
  53. "batch_max_size":1,
  54. "inactive_timeout":1
  55. }
  56. },
  57. "upstream": {
  58. "nodes": {
  59. "127.0.0.1:1982": 1
  60. },
  61. "type": "roundrobin"
  62. },
  63. "uri": "/opentracing"
  64. }]]
  65. )
  66. ngx.sleep(0.5)
  67. -- get plugin conf from admin api, password is decrypted
  68. local code, message, res = t('/apisix/admin/routes/1',
  69. ngx.HTTP_GET
  70. )
  71. res = json.decode(res)
  72. if code >= 300 then
  73. ngx.status = code
  74. ngx.say(message)
  75. return
  76. end
  77. ngx.say(res.value.plugins["clickhouse-logger"].password)
  78. -- get plugin conf from etcd, password is encrypted
  79. local etcd = require("apisix.core.etcd")
  80. local res = assert(etcd.get('/routes/1'))
  81. ngx.say(res.body.node.value.plugins["clickhouse-logger"].password)
  82. }
  83. }
  84. --- response_body
  85. abc123
  86. 7ipXoKyiZZUAgf3WWNPI5A==
  87. === TEST 2: verify
  88. --- yaml_config
  89. apisix:
  90. data_encryption:
  91. enable_encrypt_fields: true
  92. keyring:
  93. - edd1c9f0985e76a2
  94. --- request
  95. GET /opentracing
  96. --- response_body
  97. opentracing
  98. --- error_log
  99. clickhouse body: INSERT INTO t FORMAT JSONEachRow
  100. clickhouse headers: x-clickhouse-key:abc123
  101. clickhouse headers: x-clickhouse-user:default
  102. clickhouse headers: x-clickhouse-database:default
  103. --- wait: 5
  104. === TEST 3: POST and get list
  105. --- yaml_config
  106. apisix:
  107. data_encryption:
  108. enable_encrypt_fields: true
  109. keyring:
  110. - edd1c9f0985e76a2
  111. --- config
  112. location /t {
  113. content_by_lua_block {
  114. local json = require("toolkit.json")
  115. local t = require("lib.test_admin").test
  116. local code, body = t('/apisix/admin/routes',
  117. ngx.HTTP_POST,
  118. [[{
  119. "plugins": {
  120. "clickhouse-logger": {
  121. "user": "default",
  122. "password": "abc123",
  123. "database": "default",
  124. "logtable": "t",
  125. "endpoint_addr": "http://127.0.0.1:1980/clickhouse_logger_server",
  126. "batch_max_size":1,
  127. "inactive_timeout":1
  128. }
  129. },
  130. "upstream": {
  131. "nodes": {
  132. "127.0.0.1:1982": 1
  133. },
  134. "type": "roundrobin"
  135. },
  136. "uri": "/opentracing"
  137. }]]
  138. )
  139. ngx.sleep(0.1)
  140. -- get plugin conf from admin api, password is decrypted
  141. local code, message, res = t('/apisix/admin/routes',
  142. ngx.HTTP_GET
  143. )
  144. res = json.decode(res)
  145. if code >= 300 then
  146. ngx.status = code
  147. ngx.say(message)
  148. return
  149. end
  150. ngx.say(res.list[1].value.plugins["clickhouse-logger"].password)
  151. -- get plugin conf from etcd, password is encrypted
  152. local etcd = require("apisix.core.etcd")
  153. local id = res.list[1].value.id
  154. local key = "/routes/" .. id
  155. local res = assert(etcd.get(key))
  156. ngx.say(res.body.node.value.plugins["clickhouse-logger"].password)
  157. }
  158. }
  159. --- response_body
  160. abc123
  161. 7ipXoKyiZZUAgf3WWNPI5A==
  162. === TEST 4: PATCH
  163. --- yaml_config
  164. apisix:
  165. data_encryption:
  166. enable_encrypt_fields: true
  167. keyring:
  168. - edd1c9f0985e76a2
  169. --- config
  170. location /t {
  171. content_by_lua_block {
  172. local json = require("toolkit.json")
  173. local t = require("lib.test_admin").test
  174. local code, body = t('/apisix/admin/routes/1',
  175. ngx.HTTP_PUT,
  176. [[{
  177. "plugins": {
  178. "clickhouse-logger": {
  179. "user": "default",
  180. "password": "abc123",
  181. "database": "default",
  182. "logtable": "t",
  183. "endpoint_addr": "http://127.0.0.1:1980/clickhouse_logger_server",
  184. "batch_max_size":1,
  185. "inactive_timeout":1
  186. }
  187. },
  188. "upstream": {
  189. "nodes": {
  190. "127.0.0.1:1982": 1
  191. },
  192. "type": "roundrobin"
  193. },
  194. "uri": "/opentracing"
  195. }]]
  196. )
  197. ngx.sleep(0.1)
  198. local code, body = t('/apisix/admin/routes/1/plugins',
  199. ngx.HTTP_PATCH,
  200. [[{
  201. "clickhouse-logger": {
  202. "user": "default",
  203. "password": "def456",
  204. "database": "default",
  205. "logtable": "t",
  206. "endpoint_addr": "http://127.0.0.1:1980/clickhouse_logger_server",
  207. "batch_max_size":1,
  208. "inactive_timeout":1
  209. }
  210. }]]
  211. )
  212. ngx.sleep(0.1)
  213. -- get plugin conf from admin api, password is decrypted
  214. local code, message, res = t('/apisix/admin/routes/1',
  215. ngx.HTTP_GET
  216. )
  217. res = json.decode(res)
  218. if code >= 300 then
  219. ngx.status = code
  220. ngx.say(message)
  221. return
  222. end
  223. ngx.say(res.value.plugins["clickhouse-logger"].password)
  224. -- get plugin conf from etcd, password is encrypted
  225. local etcd = require("apisix.core.etcd")
  226. local res = assert(etcd.get('/routes/1'))
  227. ngx.say(res.body.node.value.plugins["clickhouse-logger"].password)
  228. }
  229. }
  230. --- response_body
  231. def456
  232. 3hlZu5mwUbqROm+cy0Vi9A==
  233. === TEST 5: data encryption work well with services
  234. --- yaml_config
  235. apisix:
  236. data_encryption:
  237. enable_encrypt_fields: true
  238. keyring:
  239. - edd1c9f0985e76a2
  240. --- config
  241. location /t {
  242. content_by_lua_block {
  243. local json = require("toolkit.json")
  244. local t = require("lib.test_admin").test
  245. local code, body = t('/apisix/admin/services/1',
  246. ngx.HTTP_PUT,
  247. [[{
  248. "plugins": {
  249. "clickhouse-logger": {
  250. "user": "default",
  251. "password": "abc123",
  252. "database": "default",
  253. "logtable": "t",
  254. "endpoint_addr": "http://127.0.0.1:1980/clickhouse_logger_server",
  255. "batch_max_size":1,
  256. "inactive_timeout":1
  257. }
  258. },
  259. "upstream": {
  260. "nodes": {
  261. "127.0.0.1:1982": 1
  262. },
  263. "type": "roundrobin"
  264. }
  265. }]]
  266. )
  267. if code >= 300 then
  268. ngx.status = code
  269. return
  270. end
  271. ngx.sleep(0.1)
  272. local code, body = t('/apisix/admin/routes/1',
  273. ngx.HTTP_PUT,
  274. [[{
  275. "service_id": "1",
  276. "uri": "/opentracing"
  277. }]]
  278. )
  279. if code >= 300 then
  280. ngx.status = code
  281. return
  282. end
  283. ngx.sleep(0.1)
  284. -- get plugin conf from admin api, password is decrypted
  285. local code, message, res = t('/apisix/admin/services/1',
  286. ngx.HTTP_GET
  287. )
  288. res = json.decode(res)
  289. if code >= 300 then
  290. ngx.status = code
  291. ngx.say(message)
  292. return
  293. end
  294. ngx.say(res.value.plugins["clickhouse-logger"].password)
  295. -- get plugin conf from etcd, password is encrypted
  296. local etcd = require("apisix.core.etcd")
  297. local res = assert(etcd.get('/services/1'))
  298. ngx.say(res.body.node.value.plugins["clickhouse-logger"].password)
  299. }
  300. }
  301. --- response_body
  302. abc123
  303. 7ipXoKyiZZUAgf3WWNPI5A==
  304. === TEST 6: verify
  305. --- yaml_config
  306. apisix:
  307. data_encryption:
  308. enable_encrypt_fields: true
  309. keyring:
  310. - edd1c9f0985e76a2
  311. --- request
  312. GET /opentracing
  313. --- response_body
  314. opentracing
  315. --- error_log
  316. clickhouse body: INSERT INTO t FORMAT JSONEachRow
  317. clickhouse headers: x-clickhouse-key:abc123
  318. clickhouse headers: x-clickhouse-user:default
  319. clickhouse headers: x-clickhouse-database:default
  320. --- wait: 5
  321. === TEST 7: data encryption work well with plugin_configs
  322. --- yaml_config
  323. apisix:
  324. data_encryption:
  325. enable_encrypt_fields: true
  326. keyring:
  327. - edd1c9f0985e76a2
  328. --- config
  329. location /t {
  330. content_by_lua_block {
  331. local json = require("toolkit.json")
  332. local t = require("lib.test_admin").test
  333. local code, err = t('/apisix/admin/plugin_configs/1',
  334. ngx.HTTP_PUT,
  335. [[{
  336. "plugins": {
  337. "clickhouse-logger": {
  338. "user": "default",
  339. "password": "abc123",
  340. "database": "default",
  341. "logtable": "t",
  342. "endpoint_addr": "http://127.0.0.1:1980/clickhouse_logger_server",
  343. "batch_max_size":1,
  344. "inactive_timeout":1
  345. }
  346. }
  347. }]]
  348. )
  349. if code >= 300 then
  350. ngx.status = code
  351. return
  352. end
  353. ngx.sleep(0.1)
  354. local code, body = t('/apisix/admin/routes/1',
  355. ngx.HTTP_PUT,
  356. [[{
  357. "plugin_config_id": 1,
  358. "uri": "/opentracing",
  359. "upstream": {
  360. "nodes": {
  361. "127.0.0.1:1982": 1
  362. },
  363. "type": "roundrobin"
  364. }
  365. }]]
  366. )
  367. if code >= 300 then
  368. ngx.status = code
  369. return
  370. end
  371. ngx.sleep(0.1)
  372. -- get plugin conf from admin api, password is decrypted
  373. local code, message, res = t('/apisix/admin/plugin_configs/1',
  374. ngx.HTTP_GET
  375. )
  376. res = json.decode(res)
  377. if code >= 300 then
  378. ngx.status = code
  379. ngx.say(message)
  380. return
  381. end
  382. ngx.say(res.value.plugins["clickhouse-logger"].password)
  383. -- get plugin conf from etcd, password is encrypted
  384. local etcd = require("apisix.core.etcd")
  385. local res = assert(etcd.get('/plugin_configs/1'))
  386. ngx.say(res.body.node.value.plugins["clickhouse-logger"].password)
  387. }
  388. }
  389. --- response_body
  390. abc123
  391. 7ipXoKyiZZUAgf3WWNPI5A==
  392. === TEST 8: verify
  393. --- yaml_config
  394. apisix:
  395. data_encryption:
  396. enable_encrypt_fields: true
  397. keyring:
  398. - edd1c9f0985e76a2
  399. --- request
  400. GET /opentracing
  401. --- response_body
  402. opentracing
  403. --- error_log
  404. clickhouse body: INSERT INTO t FORMAT JSONEachRow
  405. clickhouse headers: x-clickhouse-key:abc123
  406. clickhouse headers: x-clickhouse-user:default
  407. clickhouse headers: x-clickhouse-database:default
  408. --- wait: 5
  409. === TEST 9: data encryption work well with global rule
  410. --- yaml_config
  411. apisix:
  412. data_encryption:
  413. enable_encrypt_fields: true
  414. keyring:
  415. - edd1c9f0985e76a2
  416. --- config
  417. location /t {
  418. content_by_lua_block {
  419. local json = require("toolkit.json")
  420. local t = require("lib.test_admin").test
  421. local code, body = t('/apisix/admin/consumers',
  422. ngx.HTTP_PUT,
  423. [[{
  424. "username": "test",
  425. "plugins": {
  426. "basic-auth": {
  427. "username": "test",
  428. "password": "test"
  429. }
  430. },
  431. "desc": "test description"
  432. }]]
  433. )
  434. if code >= 300 then
  435. ngx.status = code
  436. return
  437. end
  438. local code, body = t('/apisix/admin/routes/1',
  439. ngx.HTTP_PUT,
  440. [[{
  441. "uri": "/hello",
  442. "upstream": {
  443. "type": "roundrobin",
  444. "nodes": {
  445. "127.0.0.1:1980": 1
  446. }
  447. }
  448. }]]
  449. )
  450. if code >= 300 then
  451. ngx.status = code
  452. return
  453. end
  454. local code, body = t('/apisix/admin/global_rules/1',
  455. ngx.HTTP_PUT,
  456. [[{
  457. "plugins": {
  458. "basic-auth": {}
  459. }
  460. }]]
  461. )
  462. if code >= 300 then
  463. ngx.status = code
  464. return
  465. end
  466. -- sleep for data sync
  467. ngx.sleep(0.5)
  468. -- get plugin conf from admin api, password is decrypted
  469. local code, message, res = t('/apisix/admin/consumers/test',
  470. ngx.HTTP_GET
  471. )
  472. res = json.decode(res)
  473. if code >= 300 then
  474. ngx.status = code
  475. ngx.say(message)
  476. return
  477. end
  478. ngx.say(res.value.plugins["basic-auth"].password)
  479. -- get plugin conf from etcd, password is encrypted
  480. local etcd = require("apisix.core.etcd")
  481. local res = assert(etcd.get('/consumers/test'))
  482. ngx.say(res.body.node.value.plugins["basic-auth"].password)
  483. -- hit the route with authorization
  484. local code, body = t('/hello',
  485. ngx.HTTP_PUT,
  486. nil,
  487. nil,
  488. {Authorization = "Basic dGVzdDp0ZXN0"}
  489. )
  490. if code ~= 200 then
  491. ngx.status = code
  492. return
  493. end
  494. -- delete global rule
  495. t('/apisix/admin/global_rules/1',
  496. ngx.HTTP_DELETE
  497. )
  498. ngx.say(body)
  499. }
  500. }
  501. --- request
  502. GET /t
  503. --- response_body
  504. test
  505. 9QKrmTT3TkWGvjlIoe5XXw==
  506. passed
  507. === TEST 10: data encryption work well with consumer groups
  508. --- yaml_config
  509. apisix:
  510. data_encryption:
  511. enable_encrypt_fields: true
  512. keyring:
  513. - edd1c9f0985e76a2
  514. --- config
  515. location /t {
  516. content_by_lua_block {
  517. local json = require("toolkit.json")
  518. local t = require("lib.test_admin").test
  519. local etcd = require("apisix.core.etcd")
  520. local code, body = t('/apisix/admin/consumer_groups/company_a',
  521. ngx.HTTP_PUT,
  522. [[{
  523. "plugins": {
  524. "limit-count": {
  525. "count": 2,
  526. "time_window": 60,
  527. "rejected_code": 503,
  528. "key": "remote_addr"
  529. }
  530. }
  531. }]]
  532. )
  533. if code >= 300 then
  534. ngx.status = code
  535. ngx.say(body)
  536. return
  537. end
  538. ngx.sleep(0.1)
  539. local code, body = t('/apisix/admin/consumers/foobar',
  540. ngx.HTTP_PUT,
  541. [[{
  542. "username": "foobar",
  543. "plugins": {
  544. "key-auth": {
  545. "key": "auth-two"
  546. }
  547. },
  548. "group_id": "company_a"
  549. }]]
  550. )
  551. if code >= 300 then
  552. ngx.status = code
  553. ngx.say(body)
  554. return
  555. end
  556. ngx.sleep(0.1)
  557. -- get plugin conf from admin api, key is decrypted
  558. local code, message, res = t('/apisix/admin/consumers/foobar',
  559. ngx.HTTP_GET
  560. )
  561. res = json.decode(res)
  562. if code >= 300 then
  563. ngx.status = code
  564. ngx.say(message)
  565. return
  566. end
  567. ngx.say(res.value.plugins["key-auth"].key)
  568. -- get plugin conf from etcd, key is encrypted
  569. local etcd = require("apisix.core.etcd")
  570. local res = assert(etcd.get('/consumers/foobar'))
  571. ngx.say(res.body.node.value.plugins["key-auth"].key)
  572. }
  573. }
  574. --- response_body
  575. auth-two
  576. vU/ZHVJw7b0XscDJ1Fhtig==
  577. === TEST 11: verify data encryption
  578. --- yaml_config
  579. apisix:
  580. data_encryption:
  581. enable_encrypt_fields: true
  582. keyring:
  583. - edd1c9f0985e76a2
  584. --- config
  585. location /t {
  586. content_by_lua_block {
  587. local json = require "t.toolkit.json"
  588. local t = require("lib.test_admin").test
  589. local code, err = t('/apisix/admin/routes/1',
  590. ngx.HTTP_PUT,
  591. [[{
  592. "uri": "/hello",
  593. "upstream": {
  594. "nodes": {
  595. "127.0.0.1:1980": 1
  596. },
  597. "type": "roundrobin"
  598. },
  599. "plugins": {
  600. "key-auth": {}
  601. }
  602. }]]
  603. )
  604. if code > 300 then
  605. ngx.log(ngx.ERR, err)
  606. return
  607. end
  608. ngx.sleep(0.1)
  609. local http = require "resty.http"
  610. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  611. .. "/hello"
  612. local ress = {}
  613. for i = 1, 3 do
  614. local httpc = http.new()
  615. local res, err = httpc:request_uri(uri, {
  616. method = "GET",
  617. headers = {
  618. ["apikey"] = "auth-two"
  619. }
  620. })
  621. if not res then
  622. ngx.say(err)
  623. return
  624. end
  625. table.insert(ress, res.status)
  626. end
  627. ngx.say(json.encode(ress))
  628. }
  629. }
  630. --- response_body
  631. [200,200,503]
  632. === TEST 12: verify whether print warning log when disable data_encryption
  633. --- yaml_config
  634. apisix:
  635. data_encryption:
  636. enable_encrypt_fields: false
  637. --- config
  638. location /t {
  639. content_by_lua_block {
  640. local t = require("lib.test_admin").test
  641. local code, body = t('/apisix/admin/routes/2',
  642. ngx.HTTP_PUT,
  643. [[{
  644. "uri": "/hello",
  645. "upstream": {
  646. "nodes": {
  647. "127.0.0.1:1980": 1
  648. },
  649. "type": "roundrobin"
  650. },
  651. "plugins": {
  652. "limit-count": {
  653. "count": 2,
  654. "time_window": 60,
  655. "rejected_code": 503,
  656. "key": "remote_addr"
  657. }
  658. }
  659. }]]
  660. )
  661. if code > 300 then
  662. ngx.status = code
  663. return
  664. end
  665. ngx.say(body)
  666. }
  667. }
  668. --- reponse_body
  669. passed
  670. --- no_error_log
  671. failed to get schema for plugin