LDAP Auth
The ldap-auth plugin verifies HTTP Basic credentials with an LDAP directory and maps the authenticated directory user to a consumer. The consumer identity lets consumer-scoped plugins, rate limits, and analytics apply after authentication.
The plugin is best suited to directories where user distinguished names follow a predictable pattern. For nested directory structures or names that cannot be constructed from the user name, use LDAP Auth Advanced.
Examples
The examples show how to authenticate and map a directory user to a consumer, then prevent the reusable directory password from reaching the upstream.
Set Up OpenLDAP and a Test Upstream
Choose the environment for the isolated OpenLDAP directory and the upstream that reports whether it received the credential header and consumer identity.
This fixture uses plaintext LDAP only for local evaluation. For production, use LDAPS with use_tls: true, enable tls_verify, and configure every gateway instance to trust the directory certificate's issuing CA.
Create and enter a working directory:
mkdir ldap-auth-example
cd ldap-auth-example
Create the directory bootstrap data:
dn: ou=users,dc=example,dc=org
objectClass: organizationalUnit
ou: users
dn: uid=johndoe,ou=users,dc=example,dc=org
objectClass: inetOrgPerson
cn: John Doe
sn: Doe
uid: johndoe
mail: johndoe@example.org
userPassword: john-secret
Create the test upstream:
import json
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def do_GET(self):
if self.path != "/ldap-check":
self.send_error(404)
return
body = json.dumps({
"authorization": "present" if self.headers.get("Authorization") else "absent",
"consumer": self.headers.get("X-Consumer-Username", ""),
}).encode() + b"\n"
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *_):
pass
ThreadingHTTPServer(("0.0.0.0", 8000), Handler).serve_forever()
- Docker
- Kubernetes
Set GATEWAY_CONTAINER to the running APISIX or API7 Gateway container. Create a dedicated network and connect the gateway:
export GATEWAY_CONTAINER=replace-with-gateway-container-name
docker network create gateway-ldap-auth-net
docker network connect gateway-ldap-auth-net "$GATEWAY_CONTAINER"
Start OpenLDAP with the test user:
docker run -d \
--name ldap-auth-openldap \
--hostname ldap-auth-openldap \
--network gateway-ldap-auth-net \
-e LDAP_DOMAIN=example.org \
-e LDAP_BASE_DN=dc=example,dc=org \
-e 'LDAP_ORGANISATION=Example Organization' \
-e LDAP_ADMIN_PASSWORD=admin-secret \
-e LDAP_CREATE_PEOPLE_OU=false \
-e LDAP_CREATE_GROUPS_OU=false \
-v "$PWD/ldap-users.ldif:/docker-entrypoint-initdb.d/20-users.ldif:ro" \
chrroessner/openldap:2.6.15-debian13.7
Wait until the container is healthy, then verify the test entry:
for attempt in $(seq 1 30); do
if [ "$(docker inspect --format '{{.State.Health.Status}}' ldap-auth-openldap)" = "healthy" ]; then
break
fi
sleep 1
done
test "$(docker inspect --format '{{.State.Health.Status}}' ldap-auth-openldap)" = "healthy"
docker inspect --format '{{.State.Health.Status}}' ldap-auth-openldap
docker exec ldap-auth-openldap ldapsearch -LLL -x \
-H ldap://127.0.0.1:389 \
-D 'cn=admin,dc=example,dc=org' \
-w admin-secret \
-b 'ou=users,dc=example,dc=org' \
'(uid=johndoe)' dn uid
The directory is ready and returns the test user's DN:
healthy
dn: uid=johndoe,ou=users,dc=example,dc=org
uid: johndoe
Start the test upstream:
docker run -d \
--name ldap-auth-upstream \
--network gateway-ldap-auth-net \
-v "$PWD/ldap-upstream.py:/ldap-upstream.py:ro" \
python:3.13.15-alpine3.24 \
python -u /ldap-upstream.py
Set up the Ingress Controller and gateway in the aic namespace before continuing.
Create ConfigMaps from the directory data and upstream script:
kubectl create configmap ldap-auth-users -n aic \
--from-file=20-users.ldif=ldap-users.ldif \
--dry-run=client -o yaml | kubectl apply -f -
kubectl create configmap ldap-auth-upstream-script -n aic \
--from-file=ldap-upstream.py \
--dry-run=client -o yaml | kubectl apply -f -
Deploy OpenLDAP and the inspection upstream:
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: aic
name: ldap-auth-openldap
spec:
replicas: 1
selector:
matchLabels:
app: ldap-auth-openldap
template:
metadata:
labels:
app: ldap-auth-openldap
spec:
containers:
- name: ldap
image: chrroessner/openldap:2.6.15-debian13.7
env:
- name: LDAP_DOMAIN
value: example.org
- name: LDAP_BASE_DN
value: dc=example,dc=org
- name: LDAP_ORGANISATION
value: Example Organization
- name: LDAP_ADMIN_PASSWORD
value: admin-secret
- name: LDAP_CREATE_PEOPLE_OU
value: "false"
- name: LDAP_CREATE_GROUPS_OU
value: "false"
ports:
- containerPort: 389
volumeMounts:
- name: users
mountPath: /docker-entrypoint-initdb.d/20-users.ldif
subPath: 20-users.ldif
readOnly: true
readinessProbe:
tcpSocket:
port: 389
volumes:
- name: users
configMap:
name: ldap-auth-users
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: ldap-auth-openldap
spec:
selector:
app: ldap-auth-openldap
ports:
- port: 389
targetPort: 389
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: aic
name: ldap-auth-upstream
spec:
replicas: 1
selector:
matchLabels:
app: ldap-auth-upstream
template:
metadata:
labels:
app: ldap-auth-upstream
spec:
containers:
- name: upstream
image: python:3.13.15-alpine3.24
command: ["python", "-u", "/app/ldap-upstream.py"]
ports:
- containerPort: 8000
volumeMounts:
- name: script
mountPath: /app/ldap-upstream.py
subPath: ldap-upstream.py
readOnly: true
volumes:
- name: script
configMap:
name: ldap-auth-upstream-script
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: ldap-auth-upstream
spec:
selector:
app: ldap-auth-upstream
ports:
- port: 8000
targetPort: 8000
Apply the upstream configuration:
kubectl apply -f ldap-auth-upstreams.yaml
kubectl rollout status -n aic deployment/ldap-auth-openldap
kubectl rollout status -n aic deployment/ldap-auth-upstream
Authenticate and Map a Directory User
Create a consumer for the LDAP entry and protect the upstream route.
- Admin API
- ADC
- Ingress Controller
Create the consumer:
curl "http://127.0.0.1:9180/apisix/admin/consumers/ldap-john" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"username": "ldap-john",
"plugins": {
"ldap-auth": {
"user_dn": "uid=johndoe,ou=users,dc=example,dc=org"
}
}
}'
Create the route:
curl "http://127.0.0.1:9180/apisix/admin/routes/ldap-auth-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"uri": "/ldap-check",
"plugins": {
"ldap-auth": {
"ldap_uri": "ldap-auth-openldap:389",
"base_dn": "ou=users,dc=example,dc=org",
"uid": "uid"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"ldap-auth-upstream:8000": 1
}
}
}'
Configure ADC to connect to the target backend. The remaining ADC commands read the connection settings from these environment variables:
- APISIX
- API7 Gateway
export ADC_BACKEND=apisix
export ADC_SERVER="http://127.0.0.1:9180"
export ADC_TOKEN="${ADMIN_API_KEY}"
export ADC_BACKEND=api7ee
export ADC_SERVER="https://{DASHBOARD_ADDR}"
export ADC_TOKEN="{DASHBOARD_TOKEN}"
export ADC_GATEWAY_GROUP="{GATEWAY_GROUP}"
Create the consumer, service, and route:
consumers:
- username: ldap-john
labels:
docs-example: ldap-auth
plugins:
ldap-auth:
user_dn: uid=johndoe,ou=users,dc=example,dc=org
services:
- name: ldap-auth-service
labels:
docs-example: ldap-auth
routes:
- name: ldap-auth-route
uris:
- /ldap-check
plugins:
ldap-auth:
ldap_uri: ldap-auth-openldap:389
base_dn: ou=users,dc=example,dc=org
uid: uid
upstream:
type: roundrobin
nodes:
- host: ldap-auth-upstream
port: 8000
weight: 1
Preview the labeled resources and confirm that the diff contains no unintended changes or deletions:
adc diff -f adc.yaml \
--include-resource-type service \
--include-resource-type consumer \
--label-selector docs-example=ldap-auth
Synchronize the reviewed resources:
adc sync -f adc.yaml \
--include-resource-type service \
--include-resource-type consumer \
--label-selector docs-example=ldap-auth
- Gateway API
- APISIX CRD
The Gateway API Consumer resource does not expose LDAP Auth credentials. Use the APISIX CRD path for this plugin.
apiVersion: v1
kind: Secret
metadata:
namespace: aic
name: ldap-john
type: Opaque
stringData:
user_dn: uid=johndoe,ou=users,dc=example,dc=org
---
apiVersion: apisix.apache.org/v2
kind: ApisixConsumer
metadata:
namespace: aic
name: ldap-john
spec:
ingressClassName: apisix
authParameter:
ldapAuth:
secretRef:
name: ldap-john
---
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: ldap-auth-route
spec:
ingressClassName: apisix
plugins:
- name: ldap-auth
enable: true
config:
ldap_uri: ldap-auth-openldap:389
base_dn: ou=users,dc=example,dc=org
uid: uid
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: ldap-auth-route
spec:
ingressClassName: apisix
http:
- name: ldap-auth-route
match:
paths:
- /ldap-check
backends:
- serviceName: ldap-auth-upstream
servicePort: 8000
plugin_config_name: ldap-auth-route
Apply the configuration:
kubectl apply -f ldap-auth-ic.yaml
Send a request without credentials:
curl --include "http://127.0.0.1:9080/ldap-check"
The plugin challenges the client and rejects the request before it reaches the upstream:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="ldap"
...
{"message":"Missing authorization in request"}
Send a request with the correct user name and an incorrect password:
curl --include "http://127.0.0.1:9080/ldap-check" \
--user johndoe:wrong-secret
The directory rejects the bind:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="ldap"
...
{"message":"Invalid user authorization"}
Send the valid directory credentials:
curl "http://127.0.0.1:9080/ldap-check" \
--user johndoe:john-secret
The request reaches the upstream with the matched consumer identity. The Admin API and ADC workflows use the configured consumer name, and the default configuration also forwards the credential header:
{"authorization": "present", "consumer": "ldap-john"}
Ingress Controller derives the consumer username from the resource namespace and name. For the aic namespace and ldap-john resource used above, the response contains:
{"authorization": "present", "consumer": "aic_ldap-john"}
Stop Forwarding Directory Credentials
The HTTP Basic header contains the directory password, which is commonly reusable outside this API. Credential removal was introduced in API7 Gateway 3.10.6 and APISIX 3.18.0. API7 Gateway 3.9 does not support this option.
- Admin API
- ADC
- Ingress Controller
Replace the plugin configuration on the existing route:
curl "http://127.0.0.1:9180/apisix/admin/routes/ldap-auth-route/plugins/ldap-auth" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"ldap_uri": "ldap-auth-openldap:389",
"base_dn": "ou=users,dc=example,dc=org",
"uid": "uid",
"hide_credentials": true
}'
In adc.yaml, enable credential removal on the existing route:
plugins:
ldap-auth:
ldap_uri: ldap-auth-openldap:389
base_dn: ou=users,dc=example,dc=org
uid: uid
hide_credentials: true
Preview the updated service:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=ldap-auth
Synchronize the reviewed service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=ldap-auth
- Gateway API
- APISIX CRD
The Gateway API Consumer resource does not expose LDAP Auth credentials. Use the APISIX CRD path for this plugin.
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: ldap-auth-route
spec:
ingressClassName: apisix
plugins:
- name: ldap-auth
enable: true
config:
ldap_uri: ldap-auth-openldap:389
base_dn: ou=users,dc=example,dc=org
uid: uid
hide_credentials: true
Apply the updated plugin configuration:
kubectl apply -f ldap-auth-ic.yaml
Send the valid credentials again:
curl "http://127.0.0.1:9080/ldap-check" \
--user johndoe:john-secret
The consumer remains attached, but the upstream no longer receives the credential header. The Admin API and ADC workflows return:
{"authorization": "absent", "consumer": "ldap-john"}
The Ingress Controller workflow returns the derived consumer username:
{"authorization": "absent", "consumer": "aic_ldap-john"}
When to Use LDAP Auth Advanced
LDAP Auth constructs the user's distinguished name directly from the configured user attribute, client-supplied user name, and base DN. LDAP Auth Advanced instead searches the directory to resolve the user's distinguished name and can authenticate without a consumer mapping. It is available in API7 Gateway 3.10.5 and later and in APISIX 3.18.0 and later.
Limitations
Missing or malformed credentials, directory connection failures, rejected binds, and missing consumer mappings return 401 Unauthorized.
The plugin removes whitespace from the decoded user name and password, and a password containing a colon is truncated after its first segment.
DN escaping was introduced in API7 Gateway 3.10.6 and APISIX 3.18.0. API7 Gateway 3.9 does not escape structural characters in the user name. On that version, use directory user names without those characters.