lxc launch ubuntu:24.04 openldap
lxc list -c=n -f=json|jq -r '.[]|select(.name=="openldap")|.status'
|
|
# install `psql` & `odbc` driver
cat <<'EXE' | lxc exec openldap -- bash
apt-get update
apt-get install -y postgresql-client odbc-postgresql unixodbc
apt-get clean
EXE
|
|
# extract lxd bridge network ip using `ip address show` command
LXD_BRIDGE_IP="$(ip addr show lxdbr0 | awk '/inet / {print $2}' | cut -d '/' -f 1)"
cat << EXE | lxc exec openldap -- bash
echo "${LXD_BRIDGE_IP} host.lxd.internal"|tee -a /etc/hosts
ping -c5 host.lxd.internal
EXE
|
|
# psql connection test
cat <<'EXE' | lxc exec openldap -- bash
export PGPASSWORD='sadaqah!';echo
cat <<'SQL' | psql -dopenldap -Uopenldap -hhost.lxd.internal
SELECT current_database(), CURRENT_USER, CURRENT_TIMESTAMP;
SQL
EXE
|
|
# `PostgreSQL` odbc driver config
cat <<'EXE' | lxc exec openldap -- bash
cat <<'INI' | tee -a /etc/odbcinst.ini >/dev/null
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbcw.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so
INI
cat /etc/odbcinst.ini
EXE
|
|
# `ldap` odbc datasource config
cat <<'EXE' | lxc exec openldap -- bash
cat <<'INI' | tee -a /etc/odbc.ini >/dev/null
[ldap]
Description = OpenLDAP PostgreSQL DB
Servername = host.lxd.internal
Driver = PostgreSQL
Database = openldap
Username = openldap
Password = sadaqah!
Port = 5432
INI
cat /etc/odbc.ini
EXE
|
|
# `ldap` odbc datasource test
cat <<'SQL' | lxc exec openldap -- isql -v ldap openldap sadaqah!
SELECT current_database(), CURRENT_USER, CURRENT_TIMESTAMP;
SQL
|
|
lxc snapshot openldap base:2.6.7
|
lxc publish openldap/base:2.6.7 --alias openldap/base:2.6.7
|
lxc restore openldap base:2.6.7
|
|
# install `slapd` and `ldap-utils`
cat <<'EXE' | lxc exec openldap -- bash
apt-get update
echo 'slapd slapd/password1 password sadaqah!' | debconf-set-selections
echo 'slapd slapd/password2 password sadaqah!' | debconf-set-selections
DEBIAN_FRONTEND=noninteractive apt-get install -y slapd ldap-utils
ls -lah /usr/lib/ldap | grep back_sql
apt-get clean
EXE
|
|
cat <<'EXE' | lxc exec openldap -- bash
OLC_LOG_LEVEL=$(mktemp -u)
cat <<'INI' | tee ${OLC_LOG_LEVEL} >/dev/null
dn: cn=config
changetype: modify
replace: olcLogLevel
olcLogLevel: stats config
INI
ldapadd -Y EXTERNAL -H ldapi:/// -f ${OLC_LOG_LEVEL}
EXE
|
|
echo 'systemctl restart slapd' | lxc exec openldap -- bash
echo 'tail -n10 -f /var/log/syslog' | lxc exec openldap -- bash
echo "ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=config '(olcLogLevel=*)'" | lxc exec openldap -- bash
echo "ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=config '(olcRootDN=*)' olcRootDN" | lxc exec openldap -- bash
|
|
# checking ldap `back-sql` module
cat <<'EXE' | lxc exec openldap -- bash
ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=module{0},cn=config
printf '\n=============================================================\n'
ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=module{0},cn=config | grep -i back_sql
EXE
|
|
# sql backend module load
cat <<'EXE' | lxc exec openldap -- bash
BACK_SQL_INI=$(mktemp -u)
cat <<'INI' | tee ${BACK_SQL_INI} >/dev/null
dn: cn=module{0},cn=config
changetype: modify
add: olcModuleLoad
olcModuleLoad: back_sql
INI
ldapadd -Y EXTERNAL -H ldapi:/// -f ${BACK_SQL_INI}
EXE
|
|
# admin password added using `slappasswd`
# slappasswd -h {SSHA} -s sadaqah!
cat <<'EXE' | lxc exec openldap -- bash
ADMIN_PASS_INI=$(mktemp -u)
cat <<'INI' | tee ${ADMIN_PASS_INI} >/dev/null
dn: olcDatabase={0}config,cn=config
changetype: modify
replace: olcRootPW
olcRootPW: {SSHA}Pg5ICKylRb08GzjwP9/M7gi8SQ8re9BA
INI
ldapmodify -Y EXTERNAL -H ldapi:/// -f ${ADMIN_PASS_INI}
EXE
|
|
# postgresql sql backend configuration
cat <<'EXE' | lxc exec openldap -- bash
PG_BACK_SQL=$(mktemp -u)
cat <<'INI' | tee ${PG_BACK_SQL} >/dev/null
dn: olcDatabase={1}sql,cn=config
objectClass: olcDatabaseConfig
objectClass: olcSqlConfig
olcDatabase: {1}sql
olcDbSqlBaseObject: dc=chorke,dc=org
olcDbSqlDBName: openldap
olcDbSqlDBUser: openldap
olcDbSqlDBPass:: c2FkYXFhaCE=
olcDbSqlDBURI: postgresql://host.lxd.internal/openldap
olcDbSqlUpperCase: FALSE
olcSuffix: dc=chorke,dc=org
INI
ldapadd -Y EXTERNAL -H ldapi:/// -f ${PG_BACK_SQL}
EXE
echo "ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=config '(objectClass=olcSqlConfig)'" | lxc exec openldap -- bash
echo 'ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=module{0},cn=config'|lxc exec openldap -- bash
|
|
lxc snapshot openldap ldap:2.6.7
|
lxc publish openldap/slapd:2.6.7 --alias openldap/ldap:2.6.7
|
lxc restore openldap ldap:2.6.7
|