Skip to main content

Directory accounts as API callers: LDAP and Active Directory

Callers sign in with their directory password: ldap-auth for entries named by account, ldap-auth-advanced for Active Directory with a service account search.

By Akif F. Güven Security

What you will have at the end

Two Routes on which a caller signs in with the user name and password of their directory account, over LDAPS, and is refused when the directory refuses them: a wrong password, a disabled account, or an account that is not mapped to a Consumer. directory-api uses ldap-auth, which binds as the user under a base DN and suits a directory whose entries are named by the account name. people-api uses ldap-auth-advanced, which searches Active Directory for the account name with a service account and then binds as the person, the way entries named CN=First Last need. The requests are records in Traffic Explorer with the Consumer beside the Route, and the bind password of the service account is masked in the audit log.

This is about callers of your APIs. Signing console users in with their directory accounts is set up separately, for the console itself, and is not touched here.

Prerequisites

  • A directory the gateway can reach. The lab runs a Samba domain controller as a stand-in for Active Directory, g8-samba-ad, LDAP on 389 and LDAPS on 636, with the test tree OU=g8-test,DC=ad,DC=g8dev,DC=lab: people under OU=Users-Dirty, a branch office under an OU with a comma in its name, groups under OU=Groups-Dirty, and a service account svc-g8-bind under OU=Service. One measured fact about it first: it refuses a simple bind over plain LDAP, Transport encryption required, as a Windows domain controller with LDAP signing required does, so every bind below is over LDAPS.
  • An echo backend, so the article can show what the backend receives; both Routes forward to httpbin-backend with a proxy-rewrite.
  • Traffic logging turned on under Traffic Explorer › Settings, and a role that may manage Routes and Consumers. The screenshots below were taken as nora.becker.

Part one: ldap-auth, the bind under a base DN

Open the Route directory-api, Plugins, Add plugin, ldap-auth:

  • LDAP URI: g8-samba-ad:636, host and port, not a URL. The note says why the port is written even with TLS on: without one the connection goes to 389.
  • Base DN: OU=Users-Dirty,OU=g8-test,DC=ad,DC=g8dev,DC=lab. No search runs: the sign-in name is built as <UID>=<user name>,<base DN>, so the note is right that users in deeper OUs cannot sign in.
  • UID: cn, the attribute the entries are named by. The More note under it is the sentence this article turns on: on Active Directory user entries are named by cn, so the sign-in name has to equal the entry’s CN; sAMAccountName does not form a DN.
  • Use TLS on. TLS verify checks the certificate the directory presents; on this install the engine does not hold the lab’s CA, so with it on every sign-in was refused with unable to verify the first certificate in the engine’s log, and it is off for the measurement. On an installation whose engine trusts your CA, leave it on.
  • Realm: directory-api, sent back on every refusal. Hide credentials: on. Left off, the directory password travels to the backend with the request.

The saved ldap-auth Plugin on directory-api, opened for editing: LDAP URI g8-samba-ad:636 with the note about the port, Base DN OU=Users-Dirty,OU=g8-test,DC=ad,DC=g8dev,DC=lab with the note that users in deeper OUs cannot sign in, UID cn, Use TLS on and TLS verify off No search: the name is assembled from UID, user name and base DN, and the directory is asked to bind as it.

Select Add plugin. Then the Consumer: the directory accepting a bind is not enough, the DN must also belong to a Consumer. Open Consumers, the Consumer upn_alt, the Credentials tab, New credential, choose the Plugin ldap-auth, and write the User DN. The note under the field is exact: it must equal <UID>=<user name>,<base DN> exactly, letter case included, and any other spelling of the same DN gets the same 401 as a wrong password.

The Authentication card of the saved credential of upn_alt: Plugin ldap-auth, User DN cn=upn.alt,OU=Users-Dirty,OU=g8-test,DC=ad,DC=g8dev,DC=lab The DN as the Plugin will build it, lowercase cn included. The Consumer is found by this string.

Six requests with curl -u user:password:

### no credentials at all
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="directory-api"
{"message":"Missing authorization in request"}
### upn.alt, an entry named by its account name, with the right password
HTTP/1.1 200 OK
### what the backend received for upn.alt
{'X-Consumer-Username': ['upn_alt'], 'X-Credential-Identifier': ['upn-alt-directory']}
### upn.alt with a wrong password
HTTP/1.1 401 Unauthorized
{"message":"Invalid user authorization"}
### uac.disabled, a disabled account, with the right password
HTTP/1.1 401 Unauthorized
{"message":"Invalid user authorization"}
### uac.neverexpire, an enabled account with no Consumer, with the right password
HTTP/1.1 401 Unauthorized
{"message":"Invalid user authorization"}
### ahmet.yilmaz, whose entry is named Ahmet Yilmaz, with the right password
HTTP/1.1 401 Unauthorized
{"message":"Invalid user authorization"}

The backend received the Consumer’s name and the credential’s id and no Authorization header. The client gets one sentence for every refusal; the reasons are in the engine’s log, where the directory’s own code tells a wrong password, data 52e, from a disabled account, data 533: the directory checked the password of uac.disabled and then refused the account.

The last line is the Active Directory problem. Ahmet’s entry is CN=Ahmet Yilmaz,OU=Users-Dirty,…, named by display name, while he signs in everywhere else as ahmet.yilmaz, his sAMAccountName; the DN cn=ahmet.yilmaz,… does not exist. Signing in as Ahmet Yilmaz was refused as well, by the directory, with invalid credentials in the engine’s log: the Plugin removes whitespace from the user name before it builds the DN, so the name it bound as was cn=AhmetYilmaz,…, which does not exist either. ldap-auth is right for a directory whose entries are named by the account name. For people in Active Directory, the next part.

Part two: ldap-auth-advanced, a search and then a bind

Open the Route people-api, Plugins, Add plugin, ldap-auth-advanced. Under Basics:

  • LDAP URI: g8-samba-ad:636 and Use LDAPS on. Use StartTLS, under Behavior, is the other way to TLS, from a plain connection upgraded; not both.
  • Base DN: OU=g8-test,DC=ad,DC=g8dev,DC=lab. This time it is where the search starts, and every entry below it is searched: both OUs of people, the branch office with the comma in its name included.
  • Bind DN and LDAP password: the service account the search runs as, CN=svc-g8-bind,OU=Service,…, and its password, stored on the engine and shown only to roles that manage the Route. Left empty, the search binds anonymously, which a domain controller refuses.
  • Attribute: sAMAccountName, the attribute the user name is matched against, as (sAMAccountName=ahmet.yilmaz). The note says what happens when a name matches more than one entry: the sign-in is refused, so the attribute must identify one person.
  • Header type: basic, so the client sends an ordinary Authorization: Basic header and an unauthenticated request is answered with Basic in WWW-Authenticate; the other value is ldap. Realm: people-api.
  • Consumer required stays on. Its note is the policy decision of this Plugin: on, a user the directory accepts is still refused unless a Consumer holds their DN as a credential; off, the directory alone decides, and no Consumer is attached to the request.
  • SSL verify is off for the same reason as above, the lab CA.

The ldap-auth-advanced form on people-api: LDAP URI g8-samba-ad:636, Use LDAPS on, Base DN OU=g8-test,DC=ad,DC=g8dev,DC=lab, Bind DN CN=svc-g8-bind,OU=Service,OU=g8-test,DC=ad,DC=g8dev,DC=lab and the LDAP password masked with the note that it is stored on the engine A service account searches, the person binds. The password of the service account never leaves the engine.

Select Add plugin. On the Consumer ahmet_yilmaz, a credential of the Plugin ldap-auth-advanced with the User DN as the directory holds it, which is what the search returns: CN=Ahmet Yilmaz,OU=Users-Dirty,OU=g8-test,DC=ad,DC=g8dev,DC=lab.

The Authentication card of the credential of ahmet_yilmaz: Plugin ldap-auth-advanced, User DN CN=Ahmet Yilmaz,OU=Users-Dirty,OU=g8-test,DC=ad,DC=g8dev,DC=lab The entry’s own DN, display name and all. The person signs in as ahmet.yilmaz; the search turns that into this.

### no credentials at all
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="people-api"
{"message":"Authorization required"}
### ahmet.yilmaz with the right password
HTTP/1.1 200 OK
### what the backend received
{'Authorization': 'Basic (the credentials, passed on)', 'X-Consumer-Username': ['ahmet_yilmaz'], 'X-Credential-Identifier': ['ahmet-ad']}
### ahmet.yilmaz with a wrong password
HTTP/1.1 401 Unauthorized
### uac.disabled, a disabled account, with the right password
HTTP/1.1 401 Unauthorized
### sube.kadikoy, an account in another OU with no Consumer, Consumer required on
HTTP/1.1 401 Unauthorized

Ahmet is in, as the Consumer ahmet_yilmaz. Two things to read off the backend’s view: the Consumer and credential headers are there, and so is the Authorization header, because this Plugin has no setting to hide it; a backend that must not see directory passwords needs the header removed on the Route, with proxy-rewrite, which was not measured here. The engine’s log names each refusal: user authentication failed for the wrong password and the disabled account, and no Consumer maps to the authenticated user_dn for the branch-office account the directory accepted.

Switch Consumer required off and the last case changes:

### Consumer required switched off: sube.kadikoy again
HTTP/1.1 200 OK
### what the backend received for sube.kadikoy
{'Authorization': '(present)'}
### SVC$backup, a service account with a dollar sign in its name
HTTP/1.1 200 OK
### uac.disabled, Consumer required off
HTTP/1.1 401 Unauthorized

Every account the directory accepts is in, from any OU under the base DN, with no Consumer attached, so no per-Consumer rate limit or ACL applies; the disabled account is still out, because the directory still decides. The switch was turned back on after the measurement.

What Traffic Explorer shows

Every request is a record. A request that signed in as a Consumer carries the Consumer’s name beside the Route, people-api · ahmet_yilmaz; a request accepted with Consumer required off carries the Route alone; a refusal is a 401 answered by the gateway.

Traffic Explorer filtered to the path /people/orders: 401 records with gateway as the Upstream node, 200 records for people-api with the backend's address, and two 200 records whose Route column reads people-api · ahmet_yilmaz Who signed in, when it was a Consumer. The rows without a name are the ones the directory alone let through.

What the audit log keeps

Adding either Plugin is a Route update; adding a credential is a credential.created record naming the Consumer. The ldap-auth-advanced records carry the bind password as •••• on both sides, and the switch of Consumer required is its own record, one line changed.

The Change section of the audit record of the people-api Route update: the ldap-auth-advanced block with ldap_password as four dots on both sides and consumer_required changed from false to true One switch, one record. The service account’s password is the one value the log never holds.

What to do next

Written by
Akif F. Güven
Engineer, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.