You are here

docker.sh in Lightweight Directory Access Protocol (LDAP) 8.4

Same filename and directory in other branches
  1. 8.3 docs/hogwarts/docker.sh
#!/bin/bash

LDAP_DOMAIN=hogwarts.edu
LDAP_DN=dc=hogwarts,dc=edu
LDIF_FILE=hogwarts.people.ldif
SLAPD=slapd
DOCKER_PORT=9389
DOCKER_NAME=hogwarts_ldap
DOCKER_IP=127.0.0.1

read -p "Bind method ([service_account], user, anon_user): " METHOD
METHOD=${METHOD:-service_account}

echo "Stopping all LDAP docker instances"
array=( service_account user anon_user )
for i in "${array[@]}"
do
    CID_SERVICE=`docker ps --filter "name=${DOCKER_NAME}.${i}" --format "{{.ID}}"`
    if [ $CID_SERVICE ]
        then
            docker stop $CID_SERVICE
    fi
done

LDAP_CID=`docker ps -a --filter "name=${DOCKER_NAME}.${METHOD}" --format "{{.ID}}"`
if [ $LDAP_CID ]
	then
		echo "Removing existing $DOCKER_NAME with $METHOD"
		docker rm $LDAP_CID
fi

echo "Starting $DOCKER_NAME with $METHOD"
LDAP_CID=$(docker run -e LDAP_TLS=false -e LDAP_DOMAIN="$LDAP_DOMAIN" -p $DOCKER_PORT:389 --name="${DOCKER_NAME}.${METHOD}" -d osixia/openldap)

if [ -z "$LDAP_CID" ]
	then
	echo "No LDAP CID. Exiting."
	exit
fi

docker cp $LDIF_FILE $LDAP_CID:/$LDIF_FILE
docker cp grants.${METHOD}.ldif $LDAP_CID:/grants.ldif

sleep 3
echo "Importing user and group structure"
# The admin user is provided by the docker container.
ldapadd -h $DOCKER_IP -p $DOCKER_PORT -x -D "cn=admin,$LDAP_DN" -w admin -f $LDIF_FILE

echo "Adding permissions for chosen binding method"
docker exec -it $LDAP_CID ldapmodify -Y EXTERNAL -H ldapi:/// -f /grants.ldif

echo "==================="
echo "Querying directory:"
echo "==================="
if [ "$METHOD" == "service_account" ]
    then
    echo "Searching LDAP (service account credentials)"
    ldapsearch -x -h $DOCKER_IP -p $DOCKER_PORT -b $LDAP_DN -D "cn=admin,$LDAP_DN" -w admin "(cn=hgranger)" dn
elif [ "$METHOD" == "user" ]
    then
    echo "Searching LDAP (user credentials)"
    ldapsearch -x -h $DOCKER_IP -p $DOCKER_PORT -b $LDAP_DN -D "cn=hpotter,ou=people,$LDAP_DN" -w pass "(cn=hgranger)" dn
elif [ "$METHOD" == "anon_user" ]
    then
    echo "Searching LDAP (user credentials)"
    ldapsearch -x -h $DOCKER_IP -p $DOCKER_PORT -b $LDAP_DN "(cn=hgranger)" dn
fi

File

docs/hogwarts/docker.sh
View source
  1. #!/bin/bash
  2. LDAP_DOMAIN=hogwarts.edu
  3. LDAP_DN=dc=hogwarts,dc=edu
  4. LDIF_FILE=hogwarts.people.ldif
  5. SLAPD=slapd
  6. DOCKER_PORT=9389
  7. DOCKER_NAME=hogwarts_ldap
  8. DOCKER_IP=127.0.0.1
  9. read -p "Bind method ([service_account], user, anon_user): " METHOD
  10. METHOD=${METHOD:-service_account}
  11. echo "Stopping all LDAP docker instances"
  12. array=( service_account user anon_user )
  13. for i in "${array[@]}"
  14. do
  15. CID_SERVICE=`docker ps --filter "name=${DOCKER_NAME}.${i}" --format "{{.ID}}"`
  16. if [ $CID_SERVICE ]
  17. then
  18. docker stop $CID_SERVICE
  19. fi
  20. done
  21. LDAP_CID=`docker ps -a --filter "name=${DOCKER_NAME}.${METHOD}" --format "{{.ID}}"`
  22. if [ $LDAP_CID ]
  23. then
  24. echo "Removing existing $DOCKER_NAME with $METHOD"
  25. docker rm $LDAP_CID
  26. fi
  27. echo "Starting $DOCKER_NAME with $METHOD"
  28. LDAP_CID=$(docker run -e LDAP_TLS=false -e LDAP_DOMAIN="$LDAP_DOMAIN" -p $DOCKER_PORT:389 --name="${DOCKER_NAME}.${METHOD}" -d osixia/openldap)
  29. if [ -z "$LDAP_CID" ]
  30. then
  31. echo "No LDAP CID. Exiting."
  32. exit
  33. fi
  34. docker cp $LDIF_FILE $LDAP_CID:/$LDIF_FILE
  35. docker cp grants.${METHOD}.ldif $LDAP_CID:/grants.ldif
  36. sleep 3
  37. echo "Importing user and group structure"
  38. # The admin user is provided by the docker container.
  39. ldapadd -h $DOCKER_IP -p $DOCKER_PORT -x -D "cn=admin,$LDAP_DN" -w admin -f $LDIF_FILE
  40. echo "Adding permissions for chosen binding method"
  41. docker exec -it $LDAP_CID ldapmodify -Y EXTERNAL -H ldapi:/// -f /grants.ldif
  42. echo "==================="
  43. echo "Querying directory:"
  44. echo "==================="
  45. if [ "$METHOD" == "service_account" ]
  46. then
  47. echo "Searching LDAP (service account credentials)"
  48. ldapsearch -x -h $DOCKER_IP -p $DOCKER_PORT -b $LDAP_DN -D "cn=admin,$LDAP_DN" -w admin "(cn=hgranger)" dn
  49. elif [ "$METHOD" == "user" ]
  50. then
  51. echo "Searching LDAP (user credentials)"
  52. ldapsearch -x -h $DOCKER_IP -p $DOCKER_PORT -b $LDAP_DN -D "cn=hpotter,ou=people,$LDAP_DN" -w pass "(cn=hgranger)" dn
  53. elif [ "$METHOD" == "anon_user" ]
  54. then
  55. echo "Searching LDAP (user credentials)"
  56. ldapsearch -x -h $DOCKER_IP -p $DOCKER_PORT -b $LDAP_DN "(cn=hgranger)" dn
  57. fi