Skip to content

Commit fd852bb

Browse files
author
Crazy-xyr
committed
fix: plugin.enable_data_encryption initialization issue
1 parent 7411c7d commit fd852bb

File tree

4 files changed

+240
-5
lines changed

4 files changed

+240
-5
lines changed

apisix/admin/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ local function run()
235235
end
236236

237237
if code then
238-
if method == "get" and plugin.enable_data_encryption then
238+
if code == 200 and method == "get" and plugin.enable_gde() then --if nil, initialization it
239239
if seg_res == "consumers" or seg_res == "credentials" then
240240
utils.decrypt_params(plugin.decrypt_conf, data, core.schema.TYPE_CONSUMER)
241241
elseif seg_res == "plugin_metadata" then

apisix/plugin.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,15 +936,18 @@ end
936936

937937
local enable_data_encryption
938938
local function enable_gde()
939-
if enable_data_encryption == nil then
940-
enable_data_encryption =
939+
if enable_data_encryption ~= nil then
940+
return enable_data_encryption
941+
end
942+
943+
enable_data_encryption =
941944
core.table.try_read_attr(local_conf, "apisix", "data_encryption",
942945
"enable_encrypt_fields") and (core.config.type == "etcd")
943-
_M.enable_data_encryption = enable_data_encryption
944-
end
946+
_M.enable_data_encryption = enable_data_encryption
945947

946948
return enable_data_encryption
947949
end
950+
_M.enable_gde = enable_gde
948951

949952

950953
local function get_plugin_schema_for_gde(name, schema_type)

t/admin/plugin-metadata3.t

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
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+
19+
repeat_each(1);
20+
no_long_string();
21+
no_root_location();
22+
no_shuffle();
23+
log_level("info");
24+
25+
add_block_preprocessor(sub {
26+
my ($block) = @_;
27+
28+
# setup default conf.yaml
29+
my $extra_yaml_config = $block->extra_yaml_config // <<_EOC_;
30+
apisix:
31+
data_encryption:
32+
enable_encrypt_fields: true
33+
keyring:
34+
- qeddd145sfvddff3
35+
- edd1c9f0985e76a2
36+
_EOC_
37+
38+
$block->set_value("extra_yaml_config", $extra_yaml_config);
39+
40+
if (!$block->request) {
41+
$block->set_value("request", "GET /t");
42+
}
43+
44+
if (!$block->no_error_log && !$block->error_log) {
45+
$block->set_value("no_error_log", "[error]\n[alert]");
46+
}
47+
});
48+
49+
run_tests;
50+
51+
__DATA__
52+
53+
=== TEST 1: First get not exist plugin metadata when plugin.enable_data_encryption is nil
54+
--- config
55+
location /t {
56+
content_by_lua_block {
57+
local core = require("apisix.core")
58+
local plugin = require("apisix.plugin")
59+
local t = require("lib.test_admin").test
60+
local code, body = t('/apisix/admin/plugin_metadata/http-logger',
61+
ngx.HTTP_GET
62+
)
63+
64+
local_conf, err = core.config.local_conf(true)
65+
local enable_data_encryption =
66+
core.table.try_read_attr(local_conf, "apisix", "data_encryption",
67+
"enable_encrypt_fields") and (core.config.type == "etcd")
68+
69+
ngx.status = code
70+
ngx.say(enable_data_encryption)
71+
ngx.say(plugin.enable_data_encryption) -- When no plugin configuration in the init phase. enable_data_encryption is not initialized
72+
ngx.say(body)
73+
}
74+
}
75+
--- request
76+
GET /t
77+
--- error_code: 404
78+
--- response_body_like
79+
true
80+
nil
81+
\{"message":"Key not found"\}
82+
83+
84+
85+
=== TEST 2: add example-plugin metadata
86+
--- config
87+
location /t {
88+
content_by_lua_block {
89+
local plugin = require("apisix.plugin")
90+
local t = require("lib.test_admin").test
91+
local code, body = t('/apisix/admin/plugin_metadata/example-plugin',
92+
ngx.HTTP_PUT,
93+
[[{
94+
"skey": "val",
95+
"ikey": 1
96+
}]],
97+
[[{
98+
"value": {
99+
"skey": "val",
100+
"ikey": 1
101+
},
102+
"key": "/apisix/plugin_metadata/example-plugin"
103+
}]]
104+
)
105+
106+
ngx.status = 200
107+
ngx.say(plugin.enable_data_encryption) -- Trigger plugin.enable_data_encryption to synchronize the conf configuration
108+
ngx.say(body)
109+
}
110+
}
111+
--- request
112+
GET /t
113+
--- response_body
114+
true
115+
passed
116+
117+
118+
119+
=== TEST 3: Second get not exist plugin metadata when plugin.enable_data_encryption is true
120+
--- config
121+
location /t {
122+
content_by_lua_block {
123+
local t = require("lib.test_admin").test
124+
local code, body = t('/apisix/admin/plugin_metadata/http-logger',
125+
ngx.HTTP_GET
126+
)
127+
128+
ngx.status = code
129+
ngx.say(body)
130+
}
131+
}
132+
--- request
133+
GET /t
134+
--- error_code: 404
135+
--- response_body_like
136+
{"message":"Key not found"}
137+
138+
139+
140+
=== TEST 4: update example-plugin metadata
141+
--- config
142+
location /t {
143+
content_by_lua_block {
144+
local t = require("lib.test_admin").test
145+
local code, body = t('/apisix/admin/plugin_metadata/example-plugin',
146+
ngx.HTTP_PUT,
147+
[[{
148+
"skey": "val2",
149+
"ikey": 2
150+
}]],
151+
[[{
152+
"value": {
153+
"skey": "val2",
154+
"ikey": 2
155+
},
156+
"key": "/apisix/plugin_metadata/example-plugin"
157+
}]]
158+
)
159+
160+
ngx.status = code
161+
ngx.say(body)
162+
}
163+
}
164+
--- request
165+
GET /t
166+
--- response_body
167+
passed
168+
169+
170+
171+
=== TEST 5: get plugin metadata
172+
--- config
173+
location /t {
174+
content_by_lua_block {
175+
local t = require("lib.test_admin").test
176+
local code, body = t('/apisix/admin/plugin_metadata/example-plugin',
177+
ngx.HTTP_GET
178+
)
179+
180+
ngx.status = code
181+
ngx.say(body)
182+
}
183+
}
184+
--- request
185+
GET /t
186+
--- response_body
187+
passed
188+
189+
190+
191+
=== TEST 6: delete plugin metadata
192+
--- config
193+
location /t {
194+
content_by_lua_block {
195+
local t = require("lib.test_admin").test
196+
local code, body = t('/apisix/admin/plugin_metadata/example-plugin',
197+
ngx.HTTP_DELETE
198+
)
199+
200+
ngx.status = code
201+
ngx.say(body)
202+
}
203+
}
204+
--- request
205+
GET /t
206+
--- response_body
207+
passed
208+
209+
210+
211+
=== TEST 7: get deleted example-plugin metadata
212+
--- config
213+
location /t {
214+
content_by_lua_block {
215+
local plugin = require("apisix.plugin")
216+
local t = require("lib.test_admin").test
217+
local code, body = t('/apisix/admin/plugin_metadata/example-plugin',
218+
ngx.HTTP_GET
219+
)
220+
221+
ngx.status = code
222+
ngx.say(plugin.enable_data_encryption) -- When no plugin configuration in the init phase. enable_data_encryption is not initialized
223+
ngx.say(body)
224+
}
225+
}
226+
--- request
227+
GET /t
228+
--- error_code: 404
229+
--- response_body_like
230+
nil
231+
\{"message":"Key not found"\}

test-nginx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 44276aa08b981ea0d03be7d557dd9e1a3641305c

0 commit comments

Comments
 (0)