Skip to content

Commit 9135556

Browse files
feat: allow degradation (#9345)
1 parent 1434335 commit 9135556

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

Diff for: apisix/plugins/forward-auth.lua

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ local schema = {
2323
type = "object",
2424
properties = {
2525
uri = {type = "string"},
26+
allow_degradation = {type = "boolean", default = false},
2627
ssl_verify = {
2728
type = "boolean",
2829
default = true,
@@ -118,9 +119,9 @@ function _M.access(conf, ctx)
118119
httpc:set_timeout(conf.timeout)
119120

120121
local res, err = httpc:request_uri(conf.uri, params)
121-
122-
-- block by default when authorization service is unavailable
123-
if not res then
122+
if not res and conf.allow_degradation then
123+
return
124+
elseif not res then
124125
core.log.error("failed to process forward auth, err: ", err)
125126
return 403
126127
end

Diff for: docs/en/latest/plugins/forward-auth.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ This Plugin moves the authentication and authorization logic to a dedicated exte
4747
| timeout | integer | False | 3000ms | [1, 60000]ms | Timeout for the authorization service HTTP call. |
4848
| keepalive | boolean | False | true | | When set to `true`, keeps the connection alive for multiple requests. |
4949
| keepalive_timeout | integer | False | 60000ms | [1000, ...]ms | Idle time after which the connection is closed. |
50-
| keepalive_pool | integer | False | 5 | [1, ...]ms | Connection pool limit. |
50+
| keepalive_pool | integer | False | 5 | [1, ...]ms | Connection pool limit. |
51+
| allow_degradation | boolean | False | false | | When set to `true`, allows authentication to be skipped when authentication server is unavailable. |
52+
5153

5254
## Data definition
5355

Diff for: docs/zh/latest/plugins/forward-auth.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ description: 本文介绍了关于 Apache APISIX `forward-auth` 插件的基本
4747
| keepalive | boolean || true | [true, false] | HTTP 长连接。 |
4848
| keepalive_timeout | integer || 60000ms | [1000, ...]ms | 长连接超时时间。 |
4949
| keepalive_pool | integer || 5 | [1, ...]ms | 长连接池大小。 |
50+
| allow_degradation | boolean || false | | 当设置为 `true` 时,允许在身份验证服务器不可用时跳过身份验证。 |
5051

5152
## 数据定义
5253

Diff for: t/plugin/forward-auth.t

+70-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,55 @@ property "request_method" validation failed: matches none of the enum values
206206
"uri": "/ping"
207207
}]],
208208
},
209+
{
210+
url = "/apisix/admin/routes/4",
211+
data = [[{
212+
"plugins": {
213+
"serverless-pre-function": {
214+
"phase": "rewrite",
215+
"functions" : ["return function() require(\"apisix.core\").response.exit(444); end"]
216+
}
217+
},
218+
"upstream_id": "u1",
219+
"uri": "/crashed-auth"
220+
}]],
221+
},
222+
{
223+
url = "/apisix/admin/routes/5",
224+
data = [[{
225+
"plugins": {
226+
"forward-auth": {
227+
"uri": "http://127.0.0.1:1984/crashed-auth",
228+
"request_headers": ["Authorization"],
229+
"upstream_headers": ["X-User-ID"],
230+
"client_headers": ["Location"]
231+
}
232+
},
233+
"upstream_id": "u1",
234+
"uri": "/nodegr"
235+
}]],
236+
},
237+
{
238+
url = "/apisix/admin/routes/6",
239+
data = [[{
240+
"uri": "/get",
241+
"plugins": {
242+
"forward-auth": {
243+
"uri": "http://127.0.0.1:1984/crashed-auth",
244+
"request_headers": ["Authorization"],
245+
"upstream_headers": ["X-User-ID"],
246+
"client_headers": ["Location"],
247+
"allow_degradation": true
248+
}
249+
},
250+
"upstream": {
251+
"nodes": {
252+
"httpbin.org:80": 1
253+
},
254+
"type": "roundrobin"
255+
}
256+
}]],
257+
}
209258
}
210259
211260
local t = require("lib.test_admin").test
@@ -217,7 +266,7 @@ property "request_method" validation failed: matches none of the enum values
217266
}
218267
}
219268
--- response_body eval
220-
"201passed\n" x 6
269+
"201passed\n" x 9
221270
222271
223272
@@ -305,3 +354,23 @@ POST /ping
305354
--- error_code: 403
306355
--- response_headers
307356
Location: http://example.com/auth
357+
358+
359+
360+
=== TEST 11: hit route (unavailable auth server, expect failure)
361+
--- request
362+
GET /nodegr
363+
--- more_headers
364+
Authorization: 111
365+
--- error_code: 403
366+
--- error_log
367+
failed to process forward auth, err: closed
368+
369+
370+
371+
=== TEST 12: hit route (unavailable auth server, allow degradation)
372+
--- request
373+
GET /get
374+
--- more_headers
375+
Authorization: 111
376+
--- error_code: 200

0 commit comments

Comments
 (0)