-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: plugins in multi-auth returns error instead of logging it (#11775)
- Loading branch information
1 parent
528ec89
commit a7524c0
Showing
6 changed files
with
294 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- | ||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||
-- contributor license agreements. See the NOTICE file distributed with | ||
-- this work for additional information regarding copyright ownership. | ||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
-- (the "License"); you may not use this file except in compliance with | ||
-- the License. You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
|
||
local _M = {} | ||
|
||
function _M.is_running_under_multi_auth(ctx) | ||
return ctx._plugin_name == "multi-auth" | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
use t::APISIX 'no_plan'; | ||
|
||
no_long_string(); | ||
no_root_location(); | ||
no_shuffle(); | ||
run_tests; | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: add consumer with basic-auth and key-auth plugins | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
local code, body = t('/apisix/admin/consumers', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"username": "foo", | ||
"plugins": { | ||
"basic-auth": { | ||
"username": "foo", | ||
"password": "bar" | ||
}, | ||
"jwt-auth": { | ||
"key": "user-key", | ||
"secret": "my-secret-key" | ||
} | ||
} | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
passed | ||
|
||
|
||
|
||
=== TEST 2: enable multi auth plugin using admin api | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
local code, body = t('/apisix/admin/routes/1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"plugins": { | ||
"multi-auth": { | ||
"auth_plugins": [ | ||
{ | ||
"basic-auth": {} | ||
}, | ||
{ | ||
"jwt-auth": {} | ||
}, | ||
{ | ||
"hmac-auth": {} | ||
} | ||
] | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
passed | ||
|
||
|
||
|
||
=== TEST 3: invalid basic-auth credentials | ||
--- request | ||
GET /hello | ||
--- more_headers | ||
Authorization: Basic YmFyOmJhcgo= | ||
--- error_code: 401 | ||
--- response_body | ||
{"message":"Authorization Failed"} | ||
--- error_log | ||
basic-auth failed to authenticate the request, code: 401. error: Invalid user authorization | ||
jwt-auth failed to authenticate the request, code: 401. error: JWT token invalid: invalid jwt string | ||
hmac-auth failed to authenticate the request, code: 401. error: client request can't be validated: Authorization header does not start with 'Signature' | ||
|
||
|
||
|
||
=== TEST 4: valid basic-auth creds | ||
--- request | ||
GET /hello | ||
--- more_headers | ||
Authorization: Basic Zm9vOmJhcg== | ||
--- response_body | ||
hello world | ||
--- no_error_log | ||
failed to authenticate the request | ||
|
||
|
||
|
||
=== TEST 5: missing hmac auth authorization header | ||
--- request | ||
GET /hello | ||
--- error_code: 401 | ||
--- response_body | ||
{"message":"Authorization Failed"} | ||
--- error_log | ||
hmac-auth failed to authenticate the request, code: 401. error: client request can't be validated: missing Authorization header | ||
|
||
|
||
|
||
=== TEST 6: hmac auth missing algorithm | ||
--- request | ||
GET /hello | ||
--- more_headers | ||
Authorization: Signature keyId="my-access-key",headers="@request-target date" ,signature="asdf" | ||
Date: Thu, 24 Sep 2020 06:39:52 GMT | ||
--- error_code: 401 | ||
--- response_body | ||
{"message":"Authorization Failed"} | ||
--- error_log | ||
hmac-auth failed to authenticate the request, code: 401. error: client request can't be validated: algorithm missing | ||
|
||
|
||
|
||
=== TEST 7: add consumer with username and jwt-auth plugins | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
local code, body = t('/apisix/admin/consumers', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"username": "jack", | ||
"plugins": { | ||
"jwt-auth": { | ||
"key": "user-key", | ||
"secret": "my-secret-key" | ||
} | ||
} | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
passed | ||
|
||
|
||
|
||
=== TEST 8: test with expired jwt token | ||
--- request | ||
GET /hello | ||
--- error_code: 401 | ||
--- response_body | ||
{"message":"Authorization Failed"} | ||
--- error_log | ||
jwt-auth failed to authenticate the request, code: 401. error: failed to verify jwt: 'exp' claim expired at Tue, 23 Jul 2019 08:28:21 GMT | ||
--- more_headers | ||
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTU2Mzg3MDUwMX0.pPNVvh-TQsdDzorRwa-uuiLYiEBODscp9wv0cwD6c68 | ||
|
||
|
||
|
||
=== TEST 9: test with jwt token containing wrong signature | ||
--- request | ||
GET /hello | ||
--- error_code: 401 | ||
--- response_body | ||
{"message":"Authorization Failed"} | ||
--- error_log | ||
jwt-auth failed to authenticate the request, code: 401. error: failed to verify jwt: signature mismatch: fNtFJnNnJgzbiYmGB0Yjvm-l6A6M4jRV1l4mnVFSYjs | ||
--- more_headers | ||
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTg3OTMxODU0MX0.fNtFJnNnJgzbiYmGB0Yjvm-l6A6M4jRV1l4mnVFSYjs | ||
|
||
|
||
|
||
=== TEST 10: verify jwt-auth | ||
--- request | ||
GET /hello | ||
--- more_headers | ||
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ1c2VyLWtleSIsImV4cCI6MTg3OTMxODU0MX0.fNtFJnNmJgzbiYmGB0Yjvm-l6A6M4jRV1l4mnVFSYjs | ||
--- response_body | ||
hello world | ||
--- no_error_log | ||
failed to authenticate the request |