Skip to content

Commit b17feb5

Browse files
authored
feat: extend init configuration validation with jsonschema (#3860)
Signed-off-by: spacewander <[email protected]>
1 parent eeb3d55 commit b17feb5

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

apisix/cli/ops.lua

+126
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ local profile = require("apisix.core.profile")
2323
local template = require("resty.template")
2424
local argparse = require("argparse")
2525
local pl_path = require("pl.path")
26+
local jsonschema = require("jsonschema")
2627

2728
local stderr = io.stderr
2829
local ipairs = ipairs
@@ -144,6 +145,125 @@ local function get_lua_path(conf)
144145
end
145146

146147

148+
local config_schema = {
149+
type = "object",
150+
properties = {
151+
apisix = {
152+
properties = {
153+
config_center = {
154+
enum = {"etcd", "yaml"},
155+
},
156+
proxy_protocol = {
157+
type = "object",
158+
properties = {
159+
listen_http_port = {
160+
type = "integer",
161+
},
162+
listen_https_port = {
163+
type = "integer",
164+
},
165+
enable_tcp_pp = {
166+
type = "boolean",
167+
},
168+
enable_tcp_pp_to_upstream = {
169+
type = "boolean",
170+
},
171+
}
172+
},
173+
port_admin = {
174+
type = "integer",
175+
},
176+
https_admin = {
177+
type = "boolean",
178+
},
179+
stream_proxy = {
180+
type = "object",
181+
properties = {
182+
tcp = {
183+
type = "array",
184+
minItems = 1,
185+
items = {
186+
type = "integer",
187+
}
188+
},
189+
udp = {
190+
type = "array",
191+
minItems = 1,
192+
items = {
193+
type = "integer",
194+
}
195+
},
196+
}
197+
},
198+
dns_resolver = {
199+
type = "array",
200+
minItems = 1,
201+
items = {
202+
type = "string",
203+
}
204+
},
205+
dns_resolver_valid = {
206+
type = "integer",
207+
},
208+
ssl = {
209+
type = "object",
210+
properties = {
211+
ssl_trusted_certificate = {
212+
type = "string",
213+
}
214+
}
215+
},
216+
}
217+
},
218+
nginx_config = {
219+
type = "object",
220+
properties = {
221+
envs = {
222+
type = "array",
223+
minItems = 1,
224+
items = {
225+
type = "string",
226+
}
227+
}
228+
},
229+
},
230+
http = {
231+
type = "object",
232+
properties = {
233+
lua_shared_dicts = {
234+
type = "object",
235+
}
236+
}
237+
},
238+
etcd = {
239+
type = "object",
240+
properties = {
241+
resync_delay = {
242+
type = "integer",
243+
},
244+
user = {
245+
type = "string",
246+
},
247+
password = {
248+
type = "string",
249+
},
250+
tls = {
251+
type = "object",
252+
properties = {
253+
cert = {
254+
type = "string",
255+
},
256+
key = {
257+
type = "string",
258+
},
259+
}
260+
}
261+
}
262+
}
263+
}
264+
}
265+
266+
147267
local function init(env)
148268
if env.is_root_path then
149269
print('Warning! Running apisix under /root is only suitable for '
@@ -158,6 +278,12 @@ local function init(env)
158278
util.die("failed to read local yaml config of apisix: ", err, "\n")
159279
end
160280

281+
local validator = jsonschema.generate_validator(config_schema)
282+
local ok, err = validator(yaml_conf)
283+
if not ok then
284+
util.die("failed to validate config: ", err, "\n")
285+
end
286+
161287
-- check the Admin API token
162288
local checked_admin_key = false
163289
if yaml_conf.apisix.enable_admin and yaml_conf.apisix.allow_admin then

rockspec/apisix-master-0.rockspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ dependencies = {
5050
"luafilesystem = 1.7.0-2",
5151
"lua-tinyyaml = 1.0",
5252
"nginx-lua-prometheus = 0.20201218",
53-
"jsonschema = 0.9.4",
53+
"jsonschema = 0.9.5",
5454
"lua-resty-ipmatcher = 0.6",
5555
"lua-resty-kafka = 0.07",
5656
"lua-resty-logger-socket = 2.0-0",

t/cli/test_validate_config.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ apisix:
2727
' > conf/config.yaml
2828

2929
out=$(make init 2>&1 || true)
30-
if ! echo "$out" | grep 'dns_resolver_valid should be a number'; then
30+
if ! echo "$out" | grep 'property "dns_resolver_valid" validation failed: wrong type: expected integer, got string'; then
3131
echo "failed: dns_resolver_valid should be a number"
3232
exit 1
3333
fi

0 commit comments

Comments
 (0)