metznet-channel/tests/ldap/generated-config.sh

82 lines
5.0 KiB
Bash

#!/usr/bin/env bash
# Build the cn=config and directory structure generated from Scheme
# (%ldap-slapd-config and %ldap-structure in metznet/machines/ldap.scm) into
# build/slapd-config.ldif and build/ldap-structure.ldif, check the structure
# loads under the config, then load the config into a scratch slapd with
# tests/ldap/fixture-data.ldif and check it behaves: logins, password
# changes, ACLs, overlays. Given a slapcat -n 0 dump, also
# checks the dump matches it exactly.
#
# Runs inside the guix builder container (it wipes /var/lib/slapd there):
# docker compose run --rm -T guix bash -lc 'tests/ldap/generated-config.sh [dump.ldif]'
#
# Then check the live server against it from the host:
# scripts/ldap-verify.sh -H ldaps://ldap.metznet.ca -D <admin dn> -y pwfile \
# -b cn=config -X -e build/slapd-config.ldif
# scripts/ldap-verify.sh -H ldaps://ldap.metznet.ca -D <admin dn> -y pwfile \
# -b dc=metznet,dc=ca -X -e build/ldap-structure.ldif
set -uo pipefail
cd "$(dirname "$0")/../.."
dump="${1:-}"
LDIF=$(guix build --no-grafts -L . -e '(@ (metznet machines ldap) %ldap-slapd-config-ldif)' 2>/tmp/build.log | tail -1)
[[ -n "$LDIF" && -f "$LDIF" ]] || { tail -20 /tmp/build.log; exit 2; }
P=$(guix build --no-grafts -L . -e '(@ (metznet packages slapd) openldap-slapd)' 2>/dev/null | tail -1)
OSSL=$(guix build openssl 2>/dev/null | grep -v -- '-doc$' | tail -1)
export PATH=$P/bin:$P/sbin:$P/libexec:$OSSL/bin:$PATH
STRUCTURE=$(guix build --no-grafts -L . -e '(@ (metznet machines ldap) %ldap-structure-ldif)' 2>/tmp/build.log | tail -1)
[[ -n "$STRUCTURE" && -f "$STRUCTURE" ]] || { tail -20 /tmp/build.log; exit 2; }
mkdir -p build
for f in "$LDIF:slapd-config.ldif" "$STRUCTURE:ldap-structure.ldif"; do
rm -f "build/${f#*:}" && cp "${f%%:*}" "build/${f#*:}" && chmod 644 "build/${f#*:}"
done
echo "generated: build/slapd-config.ldif build/ldap-structure.ldif"
H=ldap://127.0.0.1:3890
GUIX=uid=guix,ou=system,ou=accounts,dc=metznet,dc=ca
ALICE=uid=alice,ou=accounts,dc=metznet,dc=ca
pass=0; fail=0
check() { local name="$1"; shift; if "$@" >/tmp/out 2>&1; then pass=$((pass+1)); echo " ok $name"; else fail=$((fail+1)); echo " FAIL $name"; { grep -E '^ *FAIL' /tmp/out || head -8 /tmp/out; } | head -20 | sed 's/^/ /'; fi; }
not() { ! "$@"; }
s() { ldapsearch -x -H $H -LLL -o ldif-wrap=no "$@"; }
attr_visible() { s -D "$1" -w "$2" -b "$3" -s base "(objectClass=*)" "$4" | grep -qi "^$4:"; }
echo "generated LDIF"
if [[ -n "$dump" ]]; then
check "$dump matches it exactly" scripts/ldap-verify.sh -f "$dump" -X -e "$LDIF"
fi
check "groupOfNames patched: member optional" grep -q "NAME 'groupOfNames' DESC 'RFC2256: a group of names (DNs)' SUP top STRUCTURAL MUST ( cn ) MAY ( member \\$" "$LDIF"
echo "load into slapd"
rm -rf /var/lib/slapd /var/run/slapd
mkdir -p /var/lib/slapd/slapd.d /var/lib/slapd/data /var/lib/slapd/modules /var/run/slapd
cp $P/libexec/openldap/*.so* /var/lib/slapd/modules/ 2>/dev/null
openssl req -x509 -newkey rsa:2048 -nodes -keyout /var/lib/slapd/privkey.pem -out /var/lib/slapd/cert.pem -subj /CN=localhost -days 1 2>/dev/null
cp /var/lib/slapd/cert.pem /var/lib/slapd/chain.pem
check "slapadd -n 0" slapadd -n0 -F /var/lib/slapd/slapd.d -l "$LDIF"
check "slaptest" slaptest -F /var/lib/slapd/slapd.d -u
# the structure must load under the generated config (schema-valid, parents
# first) and come back out unchanged
check "slapadd structure" slapadd -n1 -F /var/lib/slapd/slapd.d -l "$STRUCTURE"
check "structure round-trips through slapcat" bash -c "slapcat -n1 -F /var/lib/slapd/slapd.d -o ldif-wrap=no > /tmp/structure-dump.ldif && scripts/ldap-verify.sh -f /tmp/structure-dump.ldif -X -e '$STRUCTURE'"
rm -rf /var/lib/slapd/data && mkdir -p /var/lib/slapd/data
check "slapadd fixture data" slapadd -n1 -F /var/lib/slapd/slapd.d -l tests/ldap/fixture-data.ldif -q
# slapadd checks schema, so this fails if groupOfNames still requires member
check "empty group allowed (patched groupOfNames)" bash -c "printf 'dn: cn=empty,ou=groups,dc=metznet,dc=ca\nobjectClass: groupOfNames\nobjectClass: posixGroupAux\ncn: empty\ngidNumber: 5999\n' | slapadd -n1 -F /var/lib/slapd/slapd.d"
slapd -F /var/lib/slapd/slapd.d -h "$H/"
for i in $(seq 20); do ldapwhoami -x -H $H -D $GUIX -w guixpw >/dev/null 2>&1 && break; sleep 0.5; done
echo "behaviour"
check "alice binds with her password (pam_sss)" ldapwhoami -x -H $H -D $ALICE -w alicepw
check "alice rejected with wrong password" not ldapwhoami -x -H $H -D $ALICE -w nope
check "anonymous search refused (olcRequires authc)" not s -b dc=metznet,dc=ca -s base "(objectClass=*)"
check "guix can read alice sshPublicKey" attr_visible $GUIX guixpw $ALICE sshPublicKey
check "guix can NOT read alice userPassword" not attr_visible $GUIX guixpw $ALICE userPassword
check "guix reads group members (sssd)" attr_visible $GUIX guixpw cn=staff,ou=groups,dc=metznet,dc=ca member
check "alice changes own password (passwd)" ldappasswd -x -H $H -D $ALICE -w alicepw -s alicepw2
check "guix can NOT read cn=config" not attr_visible $GUIX guixpw cn=config cn
kill "$(cat /var/run/slapd/slapd.pid)"
echo; echo "$pass passed, $fail failed"
[ $fail -eq 0 ]