Add LDAP Bind Authentication to a Route with username and password protection. The plugin
checks for valid credentials in the Proxy-Authorization
and Authorization
headers
(in that order).
This plugin is the open-source version of the LDAP Authentication Advanced plugin, which provides additional features such as LDAP searches for group and consumer mapping:
- Ability to authenticate based on username or custom ID.
- The ability to bind to an enterprise LDAP directory with a password.
- The ability to authenticate/authorize using a group base DN and specific group member or group name attributes.
Configuration Reference
This plugin is compatible with DB-less mode.
In DB-less mode, you configure Kong Gateway declaratively. Therefore, the Admin API is mostly read-only. The only tasks it can perform are all related to handling the declarative config, including:
- Setting a target's health status in the load balancer
- Validating configurations against schemas
- Uploading the declarative configuration using the
/config
endpoint
Example plugin configuration
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter | Description |
---|---|
name
required Type: string |
The name of the plugin, in this case ldap-auth . |
instance_name
optional Type: string |
An optional custom name to identify an instance of the plugin, for example Useful when running the same plugin in multiple contexts, for example, on multiple services. |
route.name or route.id
optional Type: string |
The name or ID of the route the plugin targets. Set one of these parameters if adding the plugin to a route through the top-level Not required if using |
enabled
optional Type: boolean Default value: |
Whether this plugin will be applied. |
config.hide_credentials
required Type: boolean Default value: |
An optional boolean value telling the plugin to hide the credential to the upstream server. It will be removed by Kong before proxying the request. |
config.ldap_host
required Type: string |
Host on which the LDAP server is running. |
config.ldap_port
required Type: number Default value: |
TCP port where the LDAP server is listening. 389 is the default
port for non-SSL LDAP and AD. 636 is the port required for SSL LDAP and AD. If |
config.start_tls
required Type: boolean Default value: |
Set it to |
config.ldaps
required Type: boolean Default value: |
Set to |
config.base_dn
required Type: string |
Base DN as the starting point for the search; e.g., “dc=example,dc=com”. |
config.verify_ldap_host
required Type: boolean Default value: |
Set to |
config.attribute
required Type: string |
Attribute to be used to search the user; e.g., “cn”. |
config.cache_ttl
required Type: number Default value: |
Cache expiry time in seconds. |
config.timeout
optional Type: number Default value: |
An optional timeout in milliseconds when waiting for connection with LDAP server. |
config.keepalive
optional Type: number Default value: |
An optional value in milliseconds that defines how long an idle connection to LDAP server will live before being closed. |
config.anonymous
optional Type: string |
An optional string (consumer UUID or username) value to use as an “anonymous” consumer if authentication fails. If empty (default null), the request fails with an authentication failure |
config.header_type
optional Type: string Default value: |
An optional string to use as part of the Authorization header. By default, a valid Authorization header looks like this: |
config.header_type
option was introduced in Kong 0.12.0. Previous versions of this plugin behave as if ldap
was set for this value. Usage
To authenticate a user, the client must set credentials in either the
Proxy-Authorization
or Authorization
header in the following format:
credentials := [ldap | LDAP] base64(username:password)
The Authorization header would look something like:
Authorization: ldap dGxibGVzc2luZzpLMG5nU3RyMG5n
The plugin validates the user against the LDAP server and caches the
credentials for future requests for the duration specified in
config.cache_ttl
.
You can set the header type ldap
to any string (such as basic
) using
config.header_type
.
Upstream Headers
When a client has been authenticated, the plugin appends some headers to the request before proxying it to the upstream service, so that you can identify the consumer in your code:
X-Consumer-ID
: The ID of the consumer in Kong.X-Consumer-Custom-ID
: Thecustom_id
of the consumer (if set).X-Consumer-Username
: Theusername
of the consumer (if set).X-Credential-Identifier
: The identifier of the credential (only if the consumer is not theanonymous
consumer).X-Anonymous-Consumer
: Is set totrue
if authentication fails, and theanonymous
consumer is set instead.
You can use this information on your side to implement additional logic.
You can use the X-Consumer-ID
value to query the Kong Admin API and retrieve
more information about the consumer.
Using Service Directory Mapping on the CLI
When using only RBAC Token authorization, Service Directory Mapping to Kong Roles does not take effect. If you need to use CLI access with your Service Directory mapping, you can use the same authentication mechanism that Kong Manager uses to secure browser sessions.
Authenticate User Session
Retrieve a secure cookie session with the authorized LDAP user credentials:
$ curl -c /tmp/cookie http://localhost:8001/auth \
-H 'Kong-Admin-User: <LDAP_USERNAME>' \
--user <LDAP_USERNAME>:<LDAP_PASSWORD>
Now the cookie is stored at /tmp/cookie
and can be read for future requests:
$ curl -c /tmp/cookie -b /tmp/cookie http://localhost:8001/consumers \
-H 'Kong-Admin-User: <LDAP_USERNAME>'
Because Kong Manager is a browser application, if any HTTP responses see the Set-Cookie
header, then it will automatically attach it to future requests. This is why it is helpful to utilize cURL’s cookie engine or HTTPie sessions. If storing the session is not desired, then the Set-Cookie
header value can be copied directly from the /auth
response and used with subsequent requests.
Changelog
Kong Gateway 3.0.x
- The deprecated
X-Credential-Username
header has been removed.
Kong Gateway 2.5.x
- Added support for setting the
ldap_port
. Previously, this parameter was documented but did not exist in the plugin.