Skip to content

Commit 16b9d7e

Browse files
feat(standalone): reject configurations when configured with unknown plugin (#13046)
1 parent cee2d5d commit 16b9d7e

File tree

5 files changed

+274
-10
lines changed

5 files changed

+274
-10
lines changed

apisix/plugin.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,8 @@ _M.stream_check_schema = stream_check_schema
11301130

11311131
function _M.plugin_checker(item, schema_type)
11321132
if item.plugins then
1133-
local ok, err = check_schema(item.plugins, schema_type, true)
1133+
local skip_disabled_plugins = not (core.config.type == "yaml" or core.config.type == "json")
1134+
local ok, err = check_schema(item.plugins, schema_type, skip_disabled_plugins)
11341135

11351136
if ok and enable_gde() then
11361137
-- decrypt conf
@@ -1147,7 +1148,11 @@ end
11471148

11481149
function _M.stream_plugin_checker(item, in_cp)
11491150
if item.plugins then
1150-
return stream_check_schema(item.plugins, nil, not in_cp)
1151+
local skip_disabled_plugins = not in_cp
1152+
if core.config.type == "yaml" or core.config.type == "json" then
1153+
skip_disabled_plugins = false
1154+
end
1155+
return stream_check_schema(item.plugins, nil, skip_disabled_plugins)
11511156
end
11521157

11531158
return true
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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+
18+
use t::APISIX 'no_plan';
19+
20+
repeat_each(1);
21+
no_long_string();
22+
no_root_location();
23+
24+
add_block_preprocessor(sub {
25+
my ($block) = @_;
26+
27+
if (!defined $block->yaml_config) {
28+
$block->set_value("yaml_config", <<'_EOC_');
29+
apisix:
30+
admin_key:
31+
- name: admin
32+
key: edd1c9f034335f136f87ad84b625c8f1
33+
role: admin
34+
deployment:
35+
role: data_plane
36+
role_data_plane:
37+
config_provider: yaml
38+
_EOC_
39+
}
40+
41+
$block->set_value("stream_enable", 1);
42+
});
43+
44+
run_tests();
45+
46+
__DATA__
47+
48+
=== TEST 1: missing plugin on route blocks route matching
49+
--- extra_yaml_config
50+
plugins:
51+
- redirect
52+
--- apisix_yaml
53+
routes:
54+
- id: 1
55+
uri: /hello
56+
plugins:
57+
openid-connect:
58+
client_id: x
59+
client_secret: x
60+
discovery: x
61+
scope: openid email
62+
bearer_only: false
63+
realm: x
64+
upstream:
65+
type: roundrobin
66+
nodes:
67+
"127.0.0.1:1980": 1
68+
#END
69+
--- request
70+
GET /hello
71+
--- error_code: 404
72+
--- error_log
73+
unknown plugin [openid-connect]
74+
75+
76+
77+
=== TEST 2: missing plugin on stream route blocks stream matching
78+
--- extra_yaml_config
79+
stream_plugins:
80+
- ip-restriction
81+
--- apisix_yaml
82+
stream_routes:
83+
- id: 1
84+
server_port: 1985
85+
plugins:
86+
syslog:
87+
host: 127.0.0.1
88+
port: 514
89+
upstream:
90+
type: roundrobin
91+
nodes:
92+
"127.0.0.1:1995": 1
93+
#END
94+
--- config
95+
location /stream_request {
96+
content_by_lua_block {
97+
ngx.sleep(1) -- wait for the stream route to take effect
98+
99+
local tcp_request = function(host, port)
100+
local sock, err = ngx.socket.tcp()
101+
assert(sock, err)
102+
103+
local ok, err = sock:connect(host, port)
104+
if not ok then
105+
ngx.say("connect to stream server error: ", err)
106+
return
107+
end
108+
local bytes, err = sock:send("mmm")
109+
if not bytes then
110+
ngx.say("send stream request error: ", err)
111+
return
112+
end
113+
114+
local data, err = sock:receive("*a")
115+
if not data then
116+
sock:close()
117+
ngx.say("receive stream response error: ", err)
118+
return
119+
end
120+
sock:close()
121+
ngx.print(data)
122+
end
123+
124+
tcp_request("127.0.0.1", 1985)
125+
}
126+
}
127+
--- request
128+
GET /stream_request
129+
--- response_body
130+
receive stream response error: connection reset by peer
131+
--- error_log
132+
unknown plugin [syslog]
133+
134+
135+
136+
=== TEST 3: missing plugin on route blocks route matching (json)
137+
--- yaml_config
138+
apisix:
139+
admin_key:
140+
- name: admin
141+
key: edd1c9f034335f136f87ad84b625c8f1
142+
role: admin
143+
deployment:
144+
role: data_plane
145+
role_data_plane:
146+
config_provider: json
147+
--- extra_yaml_config
148+
plugins:
149+
- redirect
150+
--- apisix_json
151+
{
152+
"routes": [
153+
{
154+
"id": "1",
155+
"uri": "/hello",
156+
"plugins": {
157+
"openid-connect": {
158+
"client_id": "x",
159+
"client_secret": "x",
160+
"discovery": "x",
161+
"scope": "openid email",
162+
"bearer_only": false,
163+
"realm": "x"
164+
}
165+
},
166+
"upstream": {
167+
"type": "roundrobin",
168+
"nodes": {
169+
"127.0.0.1:1980": 1
170+
}
171+
}
172+
}
173+
]
174+
}
175+
--- request
176+
GET /hello
177+
--- error_code: 404
178+
--- error_log
179+
unknown plugin [openid-connect]
180+
181+
182+
183+
=== TEST 4: missing plugin on stream route blocks stream matching (json)
184+
--- yaml_config
185+
apisix:
186+
admin_key:
187+
- name: admin
188+
key: edd1c9f034335f136f87ad84b625c8f1
189+
role: admin
190+
deployment:
191+
role: data_plane
192+
role_data_plane:
193+
config_provider: json
194+
--- extra_yaml_config
195+
stream_plugins:
196+
- ip-restriction
197+
--- apisix_json
198+
{
199+
"stream_routes": [
200+
{
201+
"id": "1",
202+
"server_port": 1985,
203+
"plugins": {
204+
"syslog": {
205+
"host": "127.0.0.1",
206+
"port": 514
207+
}
208+
},
209+
"upstream": {
210+
"type": "roundrobin",
211+
"nodes": {
212+
"127.0.0.1:1995": 1
213+
}
214+
}
215+
}
216+
]
217+
}
218+
--- config
219+
location /stream_request {
220+
content_by_lua_block {
221+
ngx.sleep(1) -- wait for the stream route to take effect
222+
223+
local tcp_request = function(host, port)
224+
local sock, err = ngx.socket.tcp()
225+
assert(sock, err)
226+
227+
local ok, err = sock:connect(host, port)
228+
if not ok then
229+
ngx.say("connect to stream server error: ", err)
230+
return
231+
end
232+
local bytes, err = sock:send("mmm")
233+
if not bytes then
234+
ngx.say("send stream request error: ", err)
235+
return
236+
end
237+
238+
local data, err = sock:receive("*a")
239+
if not data then
240+
sock:close()
241+
ngx.say("receive stream response error: ", err)
242+
return
243+
end
244+
sock:close()
245+
ngx.print(data)
246+
end
247+
248+
tcp_request("127.0.0.1", 1985)
249+
}
250+
}
251+
--- request
252+
GET /stream_request
253+
--- response_body
254+
receive stream response error: connection reset by peer
255+
--- error_log
256+
unknown plugin [syslog]

t/config-center-yaml/plugin.t

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ plugins:
158158
--- request
159159
GET /apisix/prometheus/metrics
160160
--- error_code: 404
161+
--- error_log
162+
err:unknown plugin [ip-restriction]
161163
162164
163165
@@ -226,7 +228,7 @@ stream_plugins:
226228
--- request
227229
GET /t
228230
--- response_body
229-
hello world
231+
{"error_msg":"404 Route Not Found"}
230232
--- error_log
231233
use config_provider: yaml
232234
load(): new plugins: {}
@@ -261,8 +263,6 @@ plugins:
261263
--- request
262264
GET /t
263265
--- response_body
264-
hello world
265-
--- no_error_log
266-
[error]
266+
{"error_msg":"404 Route Not Found"}
267267
--- error_log
268-
skipping check schema for disabled or unknown plugin [ip-restriction]. Enable the plugin or modify configuration
268+
failed to check item data of [routes] err:unknown plugin [ip-restriction]

t/config-center-yaml/route.t

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,11 @@ routes:
138138
#END
139139
--- request
140140
GET /hello
141+
--- error_code: 404
141142
--- response_body
142-
hello world
143+
{"error_msg":"404 Route Not Found"}
144+
--- error_log
145+
failed to check item data of [routes] err:unknown plugin
143146
144147
145148

t/config-center-yaml/stream-route.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ stream_routes:
9999
"127.0.0.1:1995": 1
100100
type: roundrobin
101101
#END
102-
--- stream_response
103-
hello world
102+
--- error_log
103+
err:unknown plugin [x-rewrite]
104104
105105
106106

0 commit comments

Comments
 (0)