Skip to content

Commit

Permalink
bugfix: id can be string object, which contains ^[a-zA-Z0-9-_]+$. (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
membphis authored and SaberMaster committed Jun 30, 2020
1 parent de01733 commit d5b8eaf
Show file tree
Hide file tree
Showing 13 changed files with 448 additions and 52 deletions.
2 changes: 2 additions & 0 deletions apisix/admin/global_rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ local function check_conf(id, conf, need_id)
return nil, {error_msg = "wrong route id"}
end

conf.id = id

core.log.info("schema: ", core.json.delay_encode(core.schema.global_rule))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.global_rule, conf)
Expand Down
2 changes: 2 additions & 0 deletions apisix/admin/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ local function check_conf(id, conf, need_id)
return nil, {error_msg = "wrong route id"}
end

conf.id = id

core.log.info("schema: ", core.json.delay_encode(core.schema.route))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.route, conf)
Expand Down
4 changes: 2 additions & 2 deletions apisix/admin/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ local schema_plugin = require("apisix.admin.plugins").check_schema
local upstreams = require("apisix.admin.upstreams")
local tostring = tostring
local ipairs = ipairs
local tonumber = tonumber
local type = type


Expand All @@ -47,6 +46,7 @@ local function check_conf(id, conf, need_id)
return nil, {error_msg = "wrong service id"}
end

conf.id = id

core.log.info("schema: ", core.json.delay_encode(core.schema.service))
core.log.info("conf : ", core.json.delay_encode(conf))
Expand All @@ -55,7 +55,7 @@ local function check_conf(id, conf, need_id)
return nil, {error_msg = "invalid configuration: " .. err}
end

if need_id and not tonumber(id) then
if need_id and not id then
return nil, {error_msg = "wrong type of service id"}
end

Expand Down
44 changes: 2 additions & 42 deletions apisix/admin/ssl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
-- limitations under the License.
--
local core = require("apisix.core")
local schema_plugin = require("apisix.admin.plugins").check_schema
local tostring = tostring
local aes = require "resty.aes"
local ngx_encode_base64 = ngx.encode_base64
Expand Down Expand Up @@ -46,54 +45,15 @@ local function check_conf(id, conf, need_id)
return nil, {error_msg = "wrong ssl id"}
end

conf.id = id

core.log.info("schema: ", core.json.delay_encode(core.schema.ssl))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.ssl, conf)
if not ok then
return nil, {error_msg = "invalid configuration: " .. err}
end

local upstream_id = conf.upstream_id
if upstream_id then
local key = "/upstreams/" .. upstream_id
local res, err = core.etcd.get(key)
if not res then
return nil, {error_msg = "failed to fetch upstream info by "
.. "upstream id [" .. upstream_id .. "]: "
.. err}
end

if res.status ~= 200 then
return nil, {error_msg = "failed to fetch upstream info by "
.. "upstream id [" .. upstream_id .. "], "
.. "response code: " .. res.status}
end
end

local service_id = conf.service_id
if service_id then
local key = "/services/" .. service_id
local res, err = core.etcd.get(key)
if not res then
return nil, {error_msg = "failed to fetch service info by "
.. "service id [" .. service_id .. "]: "
.. err}
end

if res.status ~= 200 then
return nil, {error_msg = "failed to fetch service info by "
.. "service id [" .. service_id .. "], "
.. "response code: " .. res.status}
end
end

if conf.plugins then
local ok, err = schema_plugin(conf.plugins)
if not ok then
return nil, {error_msg = err}
end
end

return need_id and id or true
end

Expand Down
10 changes: 6 additions & 4 deletions apisix/admin/stream_routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ local function check_conf(id, conf, need_id)

id = id or conf.id
if need_id and not id then
return nil, {error_msg = "missing stream stream route id"}
return nil, {error_msg = "missing stream route id"}
end

if not need_id and id then
return nil, {error_msg = "wrong stream stream route id, do not need it"}
return nil, {error_msg = "wrong stream route id, do not need it"}
end

if need_id and conf.id and tostring(conf.id) ~= tostring(id) then
return nil, {error_msg = "wrong stream stream route id"}
return nil, {error_msg = "wrong stream route id"}
end

conf.id = id

core.log.info("schema: ", core.json.delay_encode(core.schema.stream_route))
core.log.info("conf : ", core.json.delay_encode(conf))
local ok, err = core.schema.check(core.schema.stream_route, conf)
Expand Down Expand Up @@ -129,7 +131,7 @@ end

function _M.delete(id)
if not id then
return 400, {error_msg = "missing stream stream route id"}
return 400, {error_msg = "missing stream route id"}
end

local key = "/stream_routes/" .. id
Expand Down
4 changes: 1 addition & 3 deletions apisix/admin/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ local function check_conf(id, conf, need_id)
end

-- let schema check id
if id and not conf.id then
conf.id = id
end
conf.id = id

core.log.info("schema: ", core.json.delay_encode(core.schema.upstream))
core.log.info("conf : ", core.json.delay_encode(conf))
Expand Down
3 changes: 3 additions & 0 deletions apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ _M.upstream = upstream_schema
_M.ssl = {
type = "object",
properties = {
id = id_schema,
cert = {
type = "string", minLength = 128, maxLength = 64*1024
},
Expand Down Expand Up @@ -533,6 +534,7 @@ _M.proto = {
_M.global_rule = {
type = "object",
properties = {
id = id_schema,
plugins = plugins_schema
},
required = {"plugins"},
Expand All @@ -543,6 +545,7 @@ _M.global_rule = {
_M.stream_route = {
type = "object",
properties = {
id = id_schema,
remote_addr = remote_addr_def,
server_addr = {
description = "server IP",
Expand Down
56 changes: 56 additions & 0 deletions t/admin/global-rules.t
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,59 @@ GET /t
{"error_msg":"invalid configuration: property \"plugins\" is required"}
--- no_error_log
[error]



=== TEST 9: string id
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/global_rules/a-b-c-ABC_0123',
ngx.HTTP_PUT,
[[{
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 10: string id(DELETE)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/global_rules/a-b-c-ABC_0123',
ngx.HTTP_DELETE
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
86 changes: 86 additions & 0 deletions t/admin/routes.t
Original file line number Diff line number Diff line change
Expand Up @@ -1772,3 +1772,89 @@ GET /t
passed
--- no_error_log
[error]



=== TEST 48: string id
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/a-b-c-ABC_0123',
ngx.HTTP_PUT,
[[{
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin"
},
"uri": "/index.html"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 49: string id(delete)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/a-b-c-ABC_0123',
ngx.HTTP_DELETE
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 50: invalid string id
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/*invalid',
ngx.HTTP_PUT,
[[{
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin"
},
"uri": "/index.html"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- no_error_log
[error]
31 changes: 31 additions & 0 deletions t/admin/services.t
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,34 @@ GET /t
passed
--- no_error_log
[error]



=== TEST 27: invalid string id
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/services/*invalid',
ngx.HTTP_PUT,
[[{
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin"
}
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 400
--- no_error_log
[error]
Loading

0 comments on commit d5b8eaf

Please sign in to comment.