Skip to content

Commit c61f87e

Browse files
committed
working container. Made it very easy to dynamically enable/disable starttls.
1 parent 5448206 commit c61f87e

25 files changed

+168
-128
lines changed

Dockerfile

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
FROM debian:buster-slim
2-
MAINTAINER Rafael Römhild <[email protected]>
2+
MAINTAINER Jason Kulatunga <[email protected]>
3+
4+
# Configuration Env Variables with defaults
5+
ENV DATA_DIR="/opt/openldap/bootstrap/data"
6+
ENV CONFIG_DIR="/opt/openldap/bootstrap/config"
7+
ENV LDAP_DOMAIN=planetexpress.com
8+
ENV LDAP_ORGANISATION="Planet Express, Inc."
9+
ENV LDAP_BINDDN="cn=admin,dc=planetexpress,dc=com"
10+
ENV LDAP_SECRET=GoodNewsEveryone
11+
ENV LDAP_SSL_KEY="/etc/ldap/ssl/ldap.key"
12+
ENV LDAP_SSL_CERT="/etc/ldap/ssl/ldap.crt"
13+
ENV LDAP_FORCE_STARTTLS="false"
314

415
# Install slapd and requirements
516
RUN apt-get update \
@@ -10,23 +21,20 @@ RUN apt-get update \
1021
ldap-utils \
1122
openssl \
1223
ca-certificates \
13-
tini \
1424
&& rm -rf /var/lib/apt/lists/* \
1525
&& mkdir /etc/ldap/ssl /bootstrap
1626

17-
# ADD bootstrap files
18-
ADD ./bootstrap /bootstrap
27+
# Add s6-overlay
28+
ADD https://github.com/just-containers/s6-overlay/releases/download/v2.2.0.1/s6-overlay-amd64-installer /tmp/
29+
RUN chmod +x /tmp/s6-overlay-amd64-installer && /tmp/s6-overlay-amd64-installer /
1930

20-
# Initialize LDAP with data
21-
RUN /bin/bash /bootstrap/slapd-init.sh
31+
# ADD rootfs files
32+
ADD ./rootfs /
2233

2334
VOLUME ["/etc/ldap/slapd.d", "/etc/ldap/ssl", "/var/lib/ldap", "/run/slapd"]
2435

25-
EXPOSE 389 636
36+
EXPOSE 10389 10636
2637

27-
USER openldap
28-
29-
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/sbin/slapd"]
30-
CMD ["-h", "ldapi:/// ldap://0.0.0.0:10389 ldaps://0.0.0.0:10636", "-d", "256"]
38+
CMD ["/init"]
3139

3240
HEALTHCHECK CMD ldapsearch -H ldap://127.0.0.1:10389 -D cn=admin,dc=planetexpress,dc=com -w GoodNewsEveryone -b cn=admin,dc=planetexpress,dc=com

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ docker pull rroemhild/test-openldap
2929
docker run --rm -p 10389:10389 -p 10636:10636 rroemhild/test-openldap
3030
```
3131

32+
## Testing
33+
34+
```
35+
# List all Users
36+
ldapsearch -H ldap://localhost:10389 -x -b "ou=people,dc=planetexpress,dc=com" -D "cn=admin,dc=planetexpress,dc=com" -w GoodNewsEveryone "(objectClass=inetOrgPerson)"
37+
38+
# Request StartTLS
39+
ldapsearch -H ldap://localhost:10389 -Z -x -b "ou=people,dc=planetexpress,dc=com" -D "cn=admin,dc=planetexpress,dc=com" -w GoodNewsEveryone "(objectClass=inetOrgPerson)"
40+
41+
# Enforce StartTLS
42+
ldapsearch -H ldap://localhost:10389 -ZZ -x -b "ou=people,dc=planetexpress,dc=com" -D "cn=admin,dc=planetexpress,dc=com" -w GoodNewsEveryone "(objectClass=inetOrgPerson)"
43+
44+
# Enforce StartTLS with self-signed cert
45+
LDAPTLS_REQCERT=never ldapsearch -H ldap://localhost:10389 -ZZ -x -b "ou=people,dc=planetexpress,dc=com" -D "cn=admin,dc=planetexpress,dc=com" -w GoodNewsEveryone "(objectClass=inetOrgPerson)"
46+
47+
48+
```
49+
3250
## Exposed ports
3351

3452
* 10389 (ldap)

bootstrap/slapd-init.sh

Lines changed: 0 additions & 112 deletions
This file was deleted.

docker-compose.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ services:
55
container_name: ldap
66
# use the image tag to pull directly from the repo
77
# image: rroemhild/test-openldap
8+
environment:
9+
LDAP_FORCE_STARTTLS: "true"
810

911
# use build tag to use the local repo
1012
build:
@@ -13,8 +15,8 @@ services:
1315
ports:
1416
- '10389:10389'
1517
- '10636:10636'
16-
volumes:
17-
- data_volume:/var/lib/ldap/
18-
19-
volumes:
20-
data_volume:
18+
# volumes:
19+
# - data_volume:/var/lib/ldap/
20+
#
21+
#volumes:
22+
# data_volume:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/with-contenv bash
2+
set -eux
3+
4+
echo "Reconfigure slapd..."
5+
cat <<EOL | debconf-set-selections
6+
slapd slapd/internal/generated_adminpw password ${LDAP_SECRET}
7+
slapd slapd/internal/adminpw password ${LDAP_SECRET}
8+
slapd slapd/password2 password ${LDAP_SECRET}
9+
slapd slapd/password1 password ${LDAP_SECRET}
10+
slapd slapd/dump_database_destdir string /var/backups/slapd-VERSION
11+
slapd slapd/domain string ${LDAP_DOMAIN}
12+
slapd shared/organization string ${LDAP_ORGANISATION}
13+
slapd slapd/backend string HDB
14+
slapd slapd/purge_database boolean true
15+
slapd slapd/move_old_database boolean true
16+
slapd slapd/allow_ldap_v2 boolean false
17+
slapd slapd/no_configuration boolean false
18+
slapd slapd/dump_database select when needed
19+
EOL
20+
21+
DEBIAN_FRONTEND=noninteractive dpkg-reconfigure slapd
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/with-contenv bash
2+
set -eux
3+
4+
if [[ -f "$LDAP_SSL_KEY" ]] && [[ -f "$LDAP_SSL_CERT" ]]; then
5+
echo "TLS Certificates already present. Using provided certificates"
6+
7+
# TODO: validate the provided certs match domain
8+
9+
else
10+
echo "Make self-signed certificate for ${LDAP_DOMAIN}..."
11+
openssl req -subj "/CN=${LDAP_DOMAIN}" \
12+
-new \
13+
-newkey rsa:2048 \
14+
-days 365 \
15+
-nodes \
16+
-x509 \
17+
-keyout ${LDAP_SSL_KEY} \
18+
-out ${LDAP_SSL_CERT}
19+
20+
chmod 600 ${LDAP_SSL_KEY}
21+
fi
22+
23+
24+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/with-contenv bash
2+
set -eux
3+
4+
chown -R openldap:openldap /etc/ldap
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/with-contenv bash
2+
set -eux
3+
4+
configure_tls() {
5+
echo "Configure TLS..."
6+
ldapmodify -Y EXTERNAL -H ldapi:/// -f ${CONFIG_DIR}/tls.ldif -Q
7+
}
8+
9+
10+
configure_logging() {
11+
echo "Configure logging..."
12+
ldapmodify -Y EXTERNAL -H ldapi:/// -f ${CONFIG_DIR}/logging.ldif -Q
13+
}
14+
15+
configure_msad_features(){
16+
echo "Configure MS-AD Extensions"
17+
ldapmodify -Y EXTERNAL -H ldapi:/// -f ${CONFIG_DIR}/msad.ldif -Q
18+
}
19+
20+
configure_admin_config_pw(){
21+
echo "Configure admin config password..."
22+
adminpw=$(slappasswd -h {SSHA} -s "${LDAP_SECRET}")
23+
adminpw=$(printf '%s\n' "$adminpw" | sed -e 's/[\/&]/\\&/g')
24+
sed -i s/ADMINPW/${adminpw}/g ${CONFIG_DIR}/configadminpw.ldif
25+
ldapmodify -Y EXTERNAL -H ldapi:/// -f ${CONFIG_DIR}/configadminpw.ldif -Q
26+
}
27+
28+
configure_memberof_overlay(){
29+
echo "Configure memberOf overlay..."
30+
ldapmodify -Y EXTERNAL -H ldapi:/// -f ${CONFIG_DIR}/memberof.ldif -Q
31+
}
32+
33+
force_starttls(){
34+
echo "Force StartTLS..."
35+
ldapmodify -Y EXTERNAL -H ldapi:/// -f ${CONFIG_DIR}/force-starttls.ldif -Q
36+
}
37+
38+
load_initial_data() {
39+
echo "Load data..."
40+
local data=$(find ${DATA_DIR} -maxdepth 1 -name \*_\*.ldif -type f | sort)
41+
for ldif in ${data}; do
42+
echo "Processing file ${ldif}..."
43+
ldapadd -x -H ldapi:/// \
44+
-D ${LDAP_BINDDN} \
45+
-w ${LDAP_SECRET} \
46+
-f ${ldif}
47+
done
48+
}
49+
50+
51+
## Init
52+
53+
54+
slapd -h "ldapi:///" -u openldap -g openldap
55+
56+
configure_msad_features
57+
configure_tls
58+
configure_logging
59+
configure_memberof_overlay
60+
configure_admin_config_pw
61+
load_initial_data
62+
if [ "$LDAP_FORCE_STARTTLS" == "true" ]; then
63+
force_starttls
64+
fi
65+
66+
# Shutdown openldap daemon
67+
kill -INT `cat /run/slapd/slapd.pid` && sleep 1

rootfs/etc/services.d/slapd/run

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/with-contenv bash
2+
3+
echo "starting slapd"
4+
/usr/sbin/slapd -h "ldapi:/// ldap://0.0.0.0:10389 ldaps://0.0.0.0:10636" -d 256
File renamed without changes.

0 commit comments

Comments
 (0)