Skip to content

Commit

Permalink
Limit the formats of value labels to string only
Browse files Browse the repository at this point in the history
Signed-off-by: imjoey <majunjiev@gmail.com>
  • Loading branch information
imjoey committed Sep 25, 2020
1 parent 508cd8c commit 07ae2c7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
16 changes: 15 additions & 1 deletion apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ local remote_addr_def = {
}


local label_value_def = {
description = "value of label",
type = "string",
pattern = [[^[a-zA-Z0-9-_.]+$]],
maxLength = 64,
minLength = 1
}
_M.label_value_def = label_value_def


local health_checker = {
type = "object",
properties = {
Expand Down Expand Up @@ -334,7 +344,11 @@ local upstream_schema = {
},
labels = {
description = "key/value pairs to specify attributes",
type = "table"
type = "object",
patternProperties = {
[".*"] = label_value_def
},
maxProperties = 16
},
pass_host = {
description = "mod of host passing",
Expand Down
58 changes: 45 additions & 13 deletions t/admin/upstream.t
Original file line number Diff line number Diff line change
Expand Up @@ -1653,9 +1653,9 @@ GET /t
},
"type": "roundrobin",
"labels": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
"build":"16",
"env":"prodution",
"version":"v2"
}
}]],
[[{
Expand All @@ -1666,9 +1666,9 @@ GET /t
},
"type": "roundrobin",
"labels": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
"build":"16",
"env":"prodution",
"version":"v2"
}
},
"key": "/apisix/upstreams/1"
Expand Down Expand Up @@ -1706,9 +1706,9 @@ passed
},
"type": "roundrobin",
"labels": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
"version":"v2",
"build":"16",
"env":"prodution"
}
},
"key": "/apisix/upstreams/1"
Expand Down Expand Up @@ -1739,7 +1739,7 @@ passed
ngx.HTTP_PATCH,
[[{
"labels": {
"key1": "new-value1"
"build": "17"
}
}]],
[[{
Expand All @@ -1750,9 +1750,9 @@ passed
},
"type": "roundrobin",
"labels": {
"key1": "new-value1",
"key2": "value2",
"key3": "value3"
"version":"v2",
"build":"17",
"env":"prodution"
}
},
"key": "/apisix/upstreams/1"
Expand All @@ -1771,3 +1771,35 @@ GET /t
passed
--- no_error_log
[error]



=== TEST 53: invalid format of label value: set upstream
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_PUT,
[[{
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin",
"labels": {
"env": ["prodution", "release"]
}
}]]
)

ngx.status = code
ngx.print(body)
}
}
--- request
GET /t
--- error_code: 400
--- response_body
{"error_msg":"invalid configuration: property \"labels\" validation failed: failed to validate env (matching \".*\"): wrong type: expected string, got table"}
--- no_error_log
[error]

0 comments on commit 07ae2c7

Please sign in to comment.