Skip to content

Commit

Permalink
feat: get schema by schema_type (#2509)
Browse files Browse the repository at this point in the history
* get schema by schema_type

* Fix setting non standard global variable JSON_ schema

* Fix setting non standard global variable JSON_ schema

* Modify the usage of ngx module

* Fix format issues caused by branch conflicts

* Modify the test case to return data
  • Loading branch information
Jaycean authored Oct 29, 2020
1 parent 1dfc4cd commit d2aa1d8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
7 changes: 6 additions & 1 deletion apisix/admin/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local type = type
local table_remove = table.remove
local table_sort = table.sort
local table_insert = table.insert

local get_uri_args = ngx.req.get_uri_args

local _M = {}

Expand Down Expand Up @@ -109,7 +109,12 @@ function _M.get(name)
return 400, {error_msg = "failed to load plugin " .. name}
end

local arg = get_uri_args()
local json_schema = plugin.schema
if arg and arg["schema_type"] == "consumer" then
json_schema = plugin.consumer_schema
end

if not json_schema then
return 400, {error_msg = "not found schema"}
end
Expand Down
1 change: 1 addition & 0 deletions apisix/plugins/basic-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ local _M = {
type = 'auth',
name = plugin_name,
schema = schema,
consumer_schema = consumer_schema
}

function _M.check_schema(conf, schema_type)
Expand Down
1 change: 1 addition & 0 deletions apisix/plugins/hmac-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ local _M = {
type = 'auth',
name = plugin_name,
schema = schema,
consumer_schema = consumer_schema
}

local hmac_funcs = {
Expand Down
21 changes: 21 additions & 0 deletions t/admin/plugins.t
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,24 @@ GET /apisix/admin/plugins/prometheus
{"properties":{"disable":{"type":"boolean"}},"additionalProperties":false,"type":"object"}
--- no_error_log
[error]
=== TEST 6: get plugin basic-auth schema
--- request
GET /apisix/admin/plugins/basic-auth
--- response_body
{"properties":{"disable":{"type":"boolean"}},"title":"work with route or service object","additionalProperties":false,"type":"object"}
--- no_error_log
[error]
=== TEST 7: get plugin basic-auth schema by schema_type
--- request
GET /apisix/admin/plugins/basic-auth?schema_type=consumer
--- response_body
{"title":"work with consumer object","additionalProperties":false,"required":["username","password"],"properties":{"username":{"type":"string"},"password":{"type":"string"}},"type":"object"}
--- no_error_log
[error]
31 changes: 31 additions & 0 deletions t/plugin/basic-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,34 @@ GET /t
{"error_msg":"invalid plugins configuration: invalid plugin conf \"blah\" for plugin [basic-auth]"}
--- no_error_log
[error]



=== TEST 12: get the default schema
--- request
GET /apisix/admin/schema/plugins/basic-auth
--- response_body
{"properties":{"disable":{"type":"boolean"}},"title":"work with route or service object","additionalProperties":false,"type":"object"}
--- no_error_log
[error]



=== TEST 13: get the schema by schema_type
--- request
GET /apisix/admin/schema/plugins/basic-auth?schema_type=consumer
--- response_body
{"title":"work with consumer object","additionalProperties":false,"required":["username","password"],"properties":{"username":{"type":"string"},"password":{"type":"string"}},"type":"object"}
--- no_error_log
[error]



=== TEST 14: get the schema by error schema_type
--- request
GET /apisix/admin/schema/plugins/basic-auth?schema_type=consumer123123
--- response_body
{"properties":{"disable":{"type":"boolean"}},"title":"work with route or service object","additionalProperties":false,"type":"object"}
--- no_error_log
[error]

30 changes: 30 additions & 0 deletions t/plugin/hmac-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -1258,3 +1258,33 @@ x-hmac-access-key: my-access-key4
x-custom-header-a: asld$%dfasf
--- no_error_log
[error]



=== TEST 32: get the default schema
--- request
GET /apisix/admin/schema/plugins/hmac-auth
--- response_body
{"properties":{"disable":{"type":"boolean"}},"title":"work with route or service object","additionalProperties":false,"type":"object"}
--- no_error_log
[error]



=== TEST 33: get the schema by schema_type
--- request
GET /apisix/admin/schema/plugins/hmac-auth?schema_type=consumer
--- response_body
{"title":"work with consumer object","additionalProperties":false,"required":["access_key","secret_key"],"properties":{"clock_skew":{"default":0,"type":"integer"},"keep_headers":{"title":"whether to keep the http request header","default":false,"type":"boolean"},"secret_key":{"minLength":1,"maxLength":256,"type":"string"},"algorithm":{"type":"string","default":"hmac-sha256","enum":["hmac-sha1","hmac-sha256","hmac-sha512"]},"signed_headers":{"items":{"minLength":1,"maxLength":50,"type":"string"},"type":"array"},"access_key":{"minLength":1,"maxLength":256,"type":"string"}},"type":"object"}
--- no_error_log
[error]



=== TEST 34: get the schema by error schema_type
--- request
GET /apisix/admin/schema/plugins/hmac-auth?schema_type=consumer123123
--- response_body
{"properties":{"disable":{"type":"boolean"}},"title":"work with route or service object","additionalProperties":false,"type":"object"}
--- no_error_log
[error]

0 comments on commit d2aa1d8

Please sign in to comment.