diff --git a/Dockerfile b/Dockerfile index aa672e0..b07dd25 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,7 +27,7 @@ RUN LC_ALL=C DEBIAN_FRONTEND=noninteractive apt-get install -y slapd ldap-utils # Expose ldap default port EXPOSE 389 -# Create TSL certificats directory +# Create TLS certificats directory RUN mkdir /etc/ldap/ssl # Add config directory diff --git a/slapd.sh b/slapd.sh index ab2d798..b8febef 100755 --- a/slapd.sh +++ b/slapd.sh @@ -11,10 +11,14 @@ set -x : LDAP_DOMAIN=${LDAP_DOMAIN} : LDAP_ORGANISATION=${LDAP_ORGANISATION} -############ Base config ############ + if [ ! -e /var/lib/ldap/docker_bootstrapped ]; then status "configuring slapd for first run" + # permission error on /etc/ldap/slapd.conf if not set?! :'( + adduser openldap root + + ############ Base config ############ cat <> /etc/dhosts + + fi + + # Replication + # todo + + # Other config files + for f in $(find /etc/ldap/config -maxdepth 1 -name \*.ldif -type f); do + status "Processing file ${f}" + ldapmodify -Y EXTERNAL -H ldapi:/// -f $f -Q + done + + kill -INT `cat /run/slapd/slapd.pid` + touch /var/lib/ldap/docker_bootstrapped else status "found already-configured slapd" fi -############ Dynamic config ############ -slapd -h "ldap:/// ldapi:///" -u openldap -g openldap -chown -R openldap:openldap /etc/ldap - -# TLS -if [ -e /etc/ldap/ssl/ldap.crt ] && [ -e /etc/ldap/ssl/ldap.key ] && [ -e /etc/ldap/ssl/ca.crt ]; then - status "certificates found" - - chmod 600 /etc/ldap/ssl/ldap.key - - # create DHParamFile if not found - [ -f /etc/ldap/ssl/dhparam.pem ] || openssl dhparam -out /etc/ldap/ssl/dhparam.pem 2048 - - ldapmodify -Y EXTERNAL -H ldapi:/// -f /etc/ldap/config/auto/tls.ldif - - # add fake dnsmasq route to certificate cn - cn=$(openssl x509 -in /etc/ldap/ssl/ldap.crt -subject -noout | sed -n 's/.*CN=\(.*\).\^*/\1/p') - echo "127.0.0.1 " $cn >> /etc/dhosts - -fi - -# Replication -# todo - -# Other config files -for f in $(find /etc/ldap/config -maxdepth 1 -name \*.ldif -type f); do - status "Processing file ${f}" - ldapmodify -Y EXTERNAL -H ldapi:/// -f $f -done - -pkill slapd - status "starting slapd on default port 389" set -x exec /usr/sbin/slapd -h "ldap:///" -u openldap -g openldap -d -1 diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..12afda7 --- /dev/null +++ b/test.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +# Tests +error=0 +ok=0 + +echo_start () { + echo "------- Start $* test -------" +} + +echo_error () { + echo "\n$(tput setaf 1)/!\ $* failed$(tput sgr0)\n" + error=`expr $error + 1` +} + +echo_ok () { + echo "\n--> $* ok\n" + ok=`expr $ok + 1` +} + +run_test () { + fction=$1 + out=test/test.out + + echo_start $fction + + #./test/$1.sh | tee $out 2>&1 + ./test/$1.sh > $out 2>&1 + + if [ "$(grep -c "$2" $out)" -eq 0 ]; then + echo_error $fction + else + echo_ok $fction + fi + + rm $out +} + + +./test/tools/prepare.sh +run_test build "Successfully built" +run_test run-simple "dn: dc=example,dc=com" +run_test run-tls "dn: dc=example,dc=com" +./test/tools/end.sh + +echo "------- Test finished -------" +echo $error " failed " $ok " passed" + diff --git a/test/build.sh b/test/build.sh new file mode 100755 index 0000000..f7b7944 --- /dev/null +++ b/test/build.sh @@ -0,0 +1,4 @@ +#!/bin/sh +sudo docker.io build -t openldap-test . +#sudo docker.io build --no-cache=true -t openldap-test . + diff --git a/test/delete-container.sh b/test/delete-container.sh new file mode 100644 index 0000000..6b890d8 --- /dev/null +++ b/test/delete-container.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# remove openldap test containers +sudo docker.io ps -a > testcontainers.out + +if [ "$(grep -c "openldap-test-container" ./testcontainers.out)" -ne 0 ]; then + sudo docker.io stop openldap-test-container + sudo docker.io rm openldap-test-container +fi + +rm testcontainers.out diff --git a/test/end.sh b/test/end.sh new file mode 100644 index 0000000..f98fa92 --- /dev/null +++ b/test/end.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +./test/delete-container.sh diff --git a/test/prepare.sh b/test/prepare.sh new file mode 100644 index 0000000..f98fa92 --- /dev/null +++ b/test/prepare.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +./test/delete-container.sh diff --git a/test/run-simple.sh b/test/run-simple.sh new file mode 100755 index 0000000..c6f18c1 --- /dev/null +++ b/test/run-simple.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +docker.io run --name openldap-test-container -p 65389:389 -d openldap-test +sleep 5 +ldapsearch -x -h localhost -p 65389 -b dc=example,dc=com + +$(pwd)/test/tools/delete-container.sh diff --git a/test/run-tls.sh b/test/run-tls.sh new file mode 100755 index 0000000..45d2a29 --- /dev/null +++ b/test/run-tls.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +docker.io run --name openldap-test-container --dns=127.0.0.1 -v `pwd`/test/ssl:/etc/ldap/ssl -p 65389:389 -d openldap-test + +cert=$(echo `pwd`/test/ssl/ldap.crt) +certCN=$(openssl x509 -in $cert -subject -noout | sed -n 's/.*CN=\(.*\)\/*\(.*\)/\1/p') +addLine=$(echo "127.0.0.1" $certCN) + +echo $addLine >> /etc/hosts +cp /etc/ldap/ldap.conf /etc/ldap/ldap.conf.old +sed -i 's,TLS_CACERT.*,TLS_CACERT '"$cert"',g' /etc/ldap/ldap.conf + + +sleep 5 +ldapsearch -x -h $certCN -p 65389 -b dc=example,dc=com -ZZ + +sed -i '/'"$addLine"'/d' /etc/hosts +cp /etc/ldap/ldap.conf.old /etc/ldap/ldap.conf +rm /etc/ldap/ldap.conf.old + +$(pwd)/test/tools/delete-container.sh diff --git a/tests/ssl/ca.crt b/test/ssl/ca.crt similarity index 100% rename from tests/ssl/ca.crt rename to test/ssl/ca.crt diff --git a/test/ssl/dhparam.pem b/test/ssl/dhparam.pem new file mode 100644 index 0000000..a3e1a7e --- /dev/null +++ b/test/ssl/dhparam.pem @@ -0,0 +1,8 @@ +-----BEGIN DH PARAMETERS----- +MIIBCAKCAQEAwC8hQ6nZ2kNNmZAGGYN8++rUvNlDjKqdwWubUnqY08ng6FfGcouL +VSvgsF3LeERW/h4hrkgN983QjwrbBOrNp+7B59lhCs6Acvi87dXf3iaGNy4Gca43 +ERVkAJ7IWdXydyb9COANRtmBb1JvvYMAeVeMdofk8EcOW/kUV2adAQKluAcVhgRQ +Pesp5i6Lv1kN5zVHDGkrJz5h0Mzi35aYia0gSnVCqEzmU7Omnz/gXY3Jdx91ym5Y +2dTZuUZgIhco2bfPbhDl/1g0a1PWz7rxw24KJloNZC3nEt3JqIto83GsgaUtYxFT +EWINSpghTjl4Z0CGCamJ6HXsNJGaVUXuGwIBAg== +-----END DH PARAMETERS----- diff --git a/tests/ssl/ldap.crt b/test/ssl/ldap.crt similarity index 100% rename from tests/ssl/ldap.crt rename to test/ssl/ldap.crt diff --git a/tests/ssl/ldap.key b/test/ssl/ldap.key similarity index 100% rename from tests/ssl/ldap.key rename to test/ssl/ldap.key diff --git a/test/tools/delete-container.sh b/test/tools/delete-container.sh new file mode 100755 index 0000000..6d5e28f --- /dev/null +++ b/test/tools/delete-container.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# remove openldap test container +sudo docker.io ps -a > testcontainer.out + +if [ "$(grep -c "openldap-test-container" ./testcontainer.out)" -ne 0 ]; then + sudo docker.io stop openldap-test-container + sudo docker.io rm openldap-test-container +fi + +rm testcontainer.out diff --git a/test/tools/end.sh b/test/tools/end.sh new file mode 100755 index 0000000..75fec9d --- /dev/null +++ b/test/tools/end.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +./test/tools/delete-container.sh diff --git a/test/tools/prepare.sh b/test/tools/prepare.sh new file mode 100755 index 0000000..75fec9d --- /dev/null +++ b/test/tools/prepare.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +./test/tools/delete-container.sh diff --git a/tests.sh b/tests.sh deleted file mode 100755 index 520c6d9..0000000 --- a/tests.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -# Test script -out=tests.out - -echo_error () { - echo "$*: $(tput setaf 1)failed$(tput sgr0)" -} - -echo_ok () { - echo "$*: ok" -} - -run_test () { - fction=$1 - ./tests/$1.sh | tee $out -} - -run_test build - -if [ $(grep -c "Successfully built" ./tests.out) -eq 0 ]; then - echo_error $fction -else - echo_ok $fction -fi - -rm $out - -run_test run-simple diff --git a/tests/build.sh b/tests/build.sh deleted file mode 100755 index c987ff3..0000000 --- a/tests/build.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -sudo docker.io build --no-cache=true -t openldap . - diff --git a/tests/run-basic.sh b/tests/run-basic.sh deleted file mode 100755 index 7ab4e14..0000000 --- a/tests/run-basic.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -sudo docker.io run -p 389:389 -d openldap diff --git a/tests/run-simple.sh b/tests/run-simple.sh deleted file mode 100755 index 7ab4e14..0000000 --- a/tests/run-simple.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -sudo docker.io run -p 389:389 -d openldap diff --git a/tests/run-tls.sh b/tests/run-tls.sh deleted file mode 100755 index 16c35c1..0000000 --- a/tests/run-tls.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -sudo docker.io run --dns=127.0.0.1 -v ./ssl:/etc/ldap/ssl -p 389:389 -d openldap