Skip to content

Commit a4bacbd

Browse files
authored
feat(ldap-auth): add hide_credentials (#13832)
1 parent 59ab7ef commit a4bacbd

4 files changed

Lines changed: 127 additions & 1 deletion

File tree

apisix/plugins/ldap-auth.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ local schema = {
3131
use_tls = { type = "boolean", default = false },
3232
tls_verify = { type = "boolean", default = false },
3333
uid = { type = "string", default = "cn" },
34+
hide_credentials = { type = "boolean", default = false },
3435
realm = schema_def.get_realm_schema("ldap"),
3536
},
3637
required = {"base_dn","ldap_uri"},
@@ -162,7 +163,13 @@ function _M.rewrite(conf, ctx)
162163
end
163164
consumer_mod.attach_consumer(ctx, consumer, consumer_conf)
164165

165-
core.log.info("hit basic-auth access")
166+
-- the header carries the directory password, which is usually reusable
167+
-- beyond this API, so it should not reach the upstream unless asked for
168+
if conf.hide_credentials then
169+
core.request.set_header(ctx, "Authorization", nil)
170+
end
171+
172+
core.log.info("hit ldap-auth access")
166173
end
167174

168175
return _M

docs/en/latest/plugins/ldap-auth.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ For Route:
5555
| use_tls | boolean | False | `false` | If set to `true` uses TLS. |
5656
| tls_verify| boolean | False | `false` | Whether to verify the server certificate when `use_tls` is enabled; If set to `true`, you must set `ssl_trusted_certificate` in `config.yaml`, and make sure the host of `ldap_uri` matches the host in server certificate. |
5757
| uid | string | False | `cn` | uid attribute. |
58+
| hide_credentials | boolean | False | `false` | If true, do not pass the `Authorization` request header to the Upstream service. The header carries the directory password, which is usually reusable beyond this API. |
5859
| realm | string | False | ldap | The realm to include in the `WWW-Authenticate` header when authentication fails. |
5960

6061
## Enable plugin

docs/zh/latest/plugins/ldap-auth.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Route 端:
5151
| use_tls | boolean || false | 如果设置为 `true` 则表示启用 TLS。 |
5252
| tls_verify| boolean || false | 是否校验 LDAP 服务器的证书。如果设置为 `true`,你必须设置 `config.yaml` 里面的 `ssl_trusted_certificate`,并且确保 `ldap_uri` 里的 host 和服务器证书中的 host 匹配。 |
5353
| uid | string || cn | UID 属性。 |
54+
| hide_credentials | boolean || false | 如果设置为 `true`,则不会将 `Authorization` 请求头传递给上游服务。该请求头中携带的目录密码通常在本 API 之外也可复用。|
5455
| realm | string || ldap |在身份验证失败时,应包含在 `WWW-Authenticate` 标头中的域。|
5556

5657
## 启用插件

t/plugin/ldap-auth.t

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,120 @@ Authorization: Basic Y29tbWEsdXNlcjpjb21tYXBhc3M=
731731
hello world
732732
--- error_log
733733
find consumer commauser
734+
735+
736+
737+
=== TEST 33: enable ldap-auth without hide_credentials
738+
--- config
739+
location /t {
740+
content_by_lua_block {
741+
local t = require("lib.test_admin").test
742+
local code, body = t('/apisix/admin/consumers',
743+
ngx.HTTP_PUT,
744+
[[{
745+
"username": "user01",
746+
"plugins": {
747+
"ldap-auth": {
748+
"user_dn": "cn=user01,ou=users,dc=example,dc=org"
749+
}
750+
}
751+
}]]
752+
)
753+
if code >= 300 then
754+
ngx.status = code
755+
ngx.say(body)
756+
return
757+
end
758+
759+
code, body = t('/apisix/admin/routes/1',
760+
ngx.HTTP_PUT,
761+
[[{
762+
"plugins": {
763+
"ldap-auth": {
764+
"base_dn": "ou=users,dc=example,dc=org",
765+
"ldap_uri": "127.0.0.1:1389",
766+
"uid": "cn"
767+
}
768+
},
769+
"upstream": {
770+
"nodes": {
771+
"127.0.0.1:1980": 1
772+
},
773+
"type": "roundrobin"
774+
},
775+
"uri": "/uri"
776+
}]]
777+
)
778+
779+
if code >= 300 then
780+
ngx.status = code
781+
end
782+
ngx.say(body)
783+
}
784+
}
785+
--- response_body
786+
passed
787+
788+
789+
790+
=== TEST 34: the credentials reach the upstream
791+
--- request
792+
GET /uri
793+
--- more_headers
794+
Authorization: Basic dXNlcjAxOnBhc3N3b3JkMQ==
795+
--- response_body
796+
uri: /uri
797+
authorization: Basic dXNlcjAxOnBhc3N3b3JkMQ==
798+
host: localhost
799+
x-consumer-username: user01
800+
x-real-ip: 127.0.0.1
801+
802+
803+
804+
=== TEST 35: enable ldap-auth with hide_credentials
805+
--- config
806+
location /t {
807+
content_by_lua_block {
808+
local t = require("lib.test_admin").test
809+
local code, body = t('/apisix/admin/routes/1',
810+
ngx.HTTP_PUT,
811+
[[{
812+
"plugins": {
813+
"ldap-auth": {
814+
"base_dn": "ou=users,dc=example,dc=org",
815+
"ldap_uri": "127.0.0.1:1389",
816+
"uid": "cn",
817+
"hide_credentials": true
818+
}
819+
},
820+
"upstream": {
821+
"nodes": {
822+
"127.0.0.1:1980": 1
823+
},
824+
"type": "roundrobin"
825+
},
826+
"uri": "/uri"
827+
}]]
828+
)
829+
830+
if code >= 300 then
831+
ngx.status = code
832+
end
833+
ngx.say(body)
834+
}
835+
}
836+
--- response_body
837+
passed
838+
839+
840+
841+
=== TEST 36: the credentials do not reach the upstream
842+
--- request
843+
GET /uri
844+
--- more_headers
845+
Authorization: Basic dXNlcjAxOnBhc3N3b3JkMQ==
846+
--- response_body
847+
uri: /uri
848+
host: localhost
849+
x-consumer-username: user01
850+
x-real-ip: 127.0.0.1

0 commit comments

Comments
 (0)