Day 6: OpenLDAP
Goal: Build your own OpenLDAP image, fill the directory
dc=notes,dc=labwith users and groups, and understand how bind, search and access rights work. · Duration: approx. 60 min · Prerequisite: Day 5
Briefing
- An LDAP directory is a tree. Every entry has a
unique name, the DN
(
uid=carla,ou=people,dc=notes,dc=lab), and attributes according to object classes. - Bind means logging in: with DN and password. After
that, you search from a starting point (base) with a
filter (
(uid=carla)). - Groups are
groupOfNamesentries withmemberattributes. The memberOf overlay additionally records the groups on the user. - Keycloak logs in as its own bind user
cn=keycloak,ou=system, never as admin.
dc=notes,dc=lab
├── ou=people uid=carla, uid=dirk, uid=emil
├── ou=groups cn=notes-schreiber (carla), cn=notes-leser (dirk, emil)
└── ou=system cn=keycloak (bind user)
Drill 1: The Image
Well-maintained images for OpenLDAP have become rare. The lab builds
one from the Debian 13 package slapd:
source ~/kc-lab/setup/env.sh
mkdir -p ~/kc-lab/tag06
cp -R ~/kc-lab/code/ldap ~/kc-lab/ldap && cd ~/kc-lab/ldap
grep -E '^FROM|slapd ldap-utils' Dockerfile
grep -E 'slapadd|exec slapd' entrypoint.sh
docker compose build -q
docker compose up -d
sleep 5
docker compose logs ldap 2>&1 | grep -E 'Erster Start|slapd starting|OpenLDAP' \
| cut -c1-80Expected (abridged):
FROM debian:13.7-slim
slapd ldap-utils \
slapadd -n 0 -F /etc/ldap/slapd.d -l /tmp/config.ldif
slapadd -n 1 -F /etc/ldap/slapd.d -l /tmp/base.ldif
exec slapd -d "${LDAP_LOG_LEVEL:-256}" -h "ldap:/// ldapi:///" \
Image lab/openldap:2.6.10 Built
Container ldap-ldap-1 Started
ldap-1 | Erster Start: lege dc=notes,dc=lab an
ldap-1 | 6ae7b47a.023c8451 0x71b7e9f9f140 @(#) $OpenLDAP: slapd 2.6.10+dfsg-1 (May 29 2
ldap-1 | Debian OpenLDAP Maintainers <[email protected]>
ldap-1 | 6ae7b47a.03fec1d2 0x71b7e9f9f140 slapd starting
On the first start (“Erster Start”), entrypoint.sh
writes the configuration (cn=config, database 0) and the
basic skeleton (database 1) directly with slapadd. The
passwords from compose.yaml end up in the directory only as
hashes.
Save your progress? Sign up for free
Drill 2: The Tree
L="-x -H ldap://localhost -D cn=admin,dc=notes,dc=lab -w lab-ldap-pw-123"
ldapsearch $L -b dc=notes,dc=lab -LLL dn
ldapsearch -x -H ldap://localhost -b dc=notes,dc=lab -LLL dnExpected:
dn: dc=notes,dc=lab
dn: ou=people,dc=notes,dc=lab
dn: ou=groups,dc=notes,dc=lab
dn: ou=system,dc=notes,dc=lab
dn: cn=keycloak,ou=system,dc=notes,dc=lab
No such object (32)
-x is a simple bind with a password, -LLL
leaves out comments. The admin sees everything. Without a bind
(anonymous), you see nothing, not even whether the tree exists.
Save your progress? Sign up for free
Drill 3: Creating Users
cd ~/kc-lab/code/ldap/seed
grep -v '^#' benutzer.ldif | sed -n '1,8p'
ldapadd $L -f benutzer.ldif
for u in carla dirk emil; do
ldappasswd $L -s lab-user-pw-123 uid=$u,ou=people,dc=notes,dc=lab && echo "$u ok"
doneExpected:
dn: uid=carla,ou=people,dc=notes,dc=lab
objectClass: inetOrgPerson
uid: carla
cn: Carla Verzeichnis
givenName: Carla
sn: Verzeichnis
mail: [email protected]
departmentNumber: Vertrieb
adding new entry "uid=carla,ou=people,dc=notes,dc=lab"
adding new entry "uid=dirk,ou=people,dc=notes,dc=lab"
adding new entry "uid=emil,ou=people,dc=notes,dc=lab"
carla ok
dirk ok
emil ok
The LDIF file contains no password. ldappasswd sets it
through the “Password Modify” extension, and the server stores it as a
hash.
Save your progress? Sign up for free
Drill 4: Groups and memberOf
ldapadd $L -f gruppen.ldif
ldapsearch $L -b ou=groups,dc=notes,dc=lab -LLL '(objectClass=groupOfNames)' cn member
ldapsearch $L -b ou=people,dc=notes,dc=lab -LLL '(uid=carla)' uid departmentNumber \
memberOfExpected:
adding new entry "cn=notes-schreiber,ou=groups,dc=notes,dc=lab"
adding new entry "cn=notes-leser,ou=groups,dc=notes,dc=lab"
dn: cn=notes-schreiber,ou=groups,dc=notes,dc=lab
cn: notes-schreiber
member: uid=carla,ou=people,dc=notes,dc=lab
dn: cn=notes-leser,ou=groups,dc=notes,dc=lab
cn: notes-leser
member: uid=dirk,ou=people,dc=notes,dc=lab
member: uid=emil,ou=people,dc=notes,dc=lab
dn: uid=carla,ou=people,dc=notes,dc=lab
uid: carla
departmentNumber: Vertrieb
memberOf: cn=notes-schreiber,ou=groups,dc=notes,dc=lab
You never wrote memberOf. The overlay maintains it as
soon as a user is listed in a group as member. Keycloak can
read groups from either direction (Day 8).
Save your progress? Sign up for free
Drill 5: Logging In and Permissions
ldapwhoami -x -H ldap://localhost -D uid=carla,ou=people,dc=notes,dc=lab \
-w lab-user-pw-123
ldapwhoami -x -H ldap://localhost -D uid=carla,ou=people,dc=notes,dc=lab -w falsch
ldapsearch $L -b ou=people,dc=notes,dc=lab -LLL '(uid=carla)' userPassword \
| grep userPassword | cut -d' ' -f2 | base64 -d | cut -c1-6; echo
ldapsearch -x -H ldap://localhost -D uid=carla,ou=people,dc=notes,dc=lab \
-w lab-user-pw-123 -b ou=people,dc=notes,dc=lab -LLL '(uid=dirk)' uid userPasswordExpected:
dn:uid=carla,ou=people,dc=notes,dc=lab
ldap_bind: Invalid credentials (49)
{SSHA}
dn: uid=dirk,ou=people,dc=notes,dc=lab
uid: dirk
carla can log in and read other users, but not their password hash.
The rules (olcAccess) are in the configuration:
docker compose -f ~/kc-lab/ldap/compose.yaml exec ldap ldapsearch -Q -Y EXTERNAL \
-H ldapi:/// -b cn=config -LLL '(olcDatabase={1}mdb)' olcAccessExpected:
dn: olcDatabase={1}mdb,cn=config
olcAccess: {0}to attrs=userPassword by dn.exact=cn=keycloak,ou=system,dc=notes
,dc=lab write by self write by anonymous auth by * none
olcAccess: {1}to * by dn.exact=cn=keycloak,ou=system,dc=notes,dc=lab write by
users read by * none
Passwords: Keycloak may write (for Day 7), everyone may write their
own, anonymous users may only check them for logging in. Everything
else: Keycloak writes, logged-in users read. -Y EXTERNAL
via ldapi:/// is access as root in the container, without a
password.
Save your progress? Sign up for free
Checkpoint
ldapsearch $L -b ou=people,dc=notes,dc=lab -LLL '(objectClass=inetOrgPerson)' uid \
| grep '^uid'Expected: three users.
uid: carla
uid: dirk
uid: emil
ldapwhoami -x -H ldap://localhost -D cn=keycloak,ou=system,dc=notes,dc=lab \
-w lab-bind-pw-123Expected: The bind user for Keycloak works.
dn:cn=keycloak,ou=system,dc=notes,dc=lab
ldapsearch -x -H ldap://localhost -D cn=keycloak,ou=system,dc=notes,dc=lab \
-w lab-bind-pw-123 -b dc=notes,dc=lab -LLL \
'(memberOf=cn=notes-leser,ou=groups,dc=notes,dc=lab)' uid | grep '^uid'Expected: members of a group via memberOf.
uid: dirk
uid: emil
Challenge
Create the user frida (department Vertrieb, sales), give
her a password and add her to the group notes-schreiber
without deleting and recreating the group.
Hint: For the group you need ldapmodify
with changetype: modify and add: member.
Solution
cd ~/kc-lab/tag06
cat > frida.ldif <<'EOF'
dn: uid=frida,ou=people,dc=notes,dc=lab
objectClass: inetOrgPerson
uid: frida
cn: Frida Verzeichnis
givenName: Frida
sn: Verzeichnis
mail: [email protected]
departmentNumber: Vertrieb
dn: cn=notes-schreiber,ou=groups,dc=notes,dc=lab
changetype: modify
add: member
member: uid=frida,ou=people,dc=notes,dc=lab
EOF
ldapmodify -a $L -f frida.ldif
ldappasswd $L -s lab-user-pw-123 uid=frida,ou=people,dc=notes,dc=lab
ldapsearch $L -b ou=people,dc=notes,dc=lab -LLL '(uid=frida)' memberOfExpected:
adding new entry "uid=frida,ou=people,dc=notes,dc=lab"
modifying entry "cn=notes-schreiber,ou=groups,dc=notes,dc=lab"
dn: uid=frida,ou=people,dc=notes,dc=lab
memberOf: cn=notes-schreiber,ou=groups,dc=notes,dc=lab
ldapmodify -a creates entries without
changetype as new and applies the others as modifications.
One file, two kinds of change.
Save your progress? Sign up for free
Enterprise Note
- LDAPS or StartTLS: During a bind, passwords travel over the wire in plain text. In production, you only speak LDAP encrypted (port 636 or StartTLS on 389).
- A bind user with few rights: Keycloak only gets what it needs; write access only if Keycloak is supposed to write passwords or attributes back (Day 7).
- High availability: OpenLDAP replicates with syncrepl (also multi-provider). Keycloak can try several LDAP URLs one after another.
- Active Directory is an LDAP directory too, with its own attributes and quirks (Day 9).