Skip to content

Commit

Permalink
test: Clean up Zitadel org before running the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tlater-famedly committed Jul 23, 2024
1 parent b0d3a55 commit 32a0ef9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tests/environment/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ services:
target: /certs
read_only: true

ldap-setup:
image: bitnami/openldap:latest
entrypoint: /ldap-setup/ldap-setup.sh
test-setup:
image: famedly/ldap-sync-testenv
build:
context: ./test-setup
entrypoint: /ldap/ldap-setup.sh
volumes:
- type: bind
source: ./ldap-setup
target: /ldap-setup
source: ./ldap
target: /ldap
read_only: true
- type: bind
source: ./zitadel/service-user.json
target: /zitadel-service-user.json
read_only: true
healthcheck:
test: ["CMD", "test", "-f", "/tmp/ready"]
Expand All @@ -32,6 +38,8 @@ services:
depends_on:
ldap:
condition: 'service_started'
zitadel:
condition: 'service_healthy'

zitadel:
image: ghcr.io/zitadel/zitadel:latest
Expand Down
70 changes: 70 additions & 0 deletions tests/environment/ldap/ldap-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/sh

set -eu

# Script to wait for an ldap server to be up, clean up any existing
# data and then to do some basic initialization.
#
# This is intended for test suite setup, don't use this in production.

LDAP_HOST='ldap://ldap:1389'
LDAP_BASE='dc=example,dc=org'
LDAP_ADMIN='cn=admin,dc=example,dc=org'
LDAP_PASSWORD='adminpassword'

ZITADEL_HOST="http://zitadel:8080"

# echo "Waiting for LDAP to be ready"

# retries=5

# while [ $retries -gt 0 ]; do
# sleep 5
# retries=$((retries - 1))

# if ldapsearch -D "${LDAP_ADMIN}" -w "${LDAP_PASSWORD}" -H "${LDAP_HOST}" -b "${LDAP_BASE}" 'objectclass=*'; then
# break
# fi
# done

echo "Authenticating to Zitadel"
zitadel-tools key2jwt --audience="http://localhost" --key=/zitadel-service-user.json --output=/tmp/jwt.txt
zitadel_token="$(curl \
--request POST \
--url "${ZITADEL_HOST}/oauth/v2/token" \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Host: localhost' \
--data grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
--data scope=openid \
--data scope=urn:zitadel:iam:org:project:id:zitadel:aud \
--data assertion="$(cat /tmp/jwt.txt)")"
zitadel_token="$(echo "${zitadel_token}" | jq --raw-output .access_token | tr -d '\n')"

echo "Deleting Zitadel users"
zitadel_users="$(curl \
--request POST \
--url "${ZITADEL_HOST}/management/v1/users/_search" \
--header "Authorization: Bearer ${zitadel_token}" \
--header 'Host: localhost')"

echo "$zitadel_users"

zitadel_users="$(echo "$zitadel_users" | jq --raw-output '.result[]? | select(.userName | startswith("zitadel-admin")) | .id')"

for id in $zitadel_users; do
echo "Deleting user $id"
curl --request DELETE --url "${ZITADEL_HOST}/management/v1/users/$id" \
--header "Authorization: Bearer ${zitadel_token}" \
--header 'Host: localhost'
done

# echo "Deleting LDAP test data"
# ldapdelete -D "${LDAP_ADMIN}" -w "${LDAP_PASSWORD}" -H "${LDAP_HOST}" -r 'ou=testorg,dc=example,dc=org' || true

# echo "Add LDAP test organizatino"
# ldapadd -D "${LDAP_ADMIN}" -w "${LDAP_PASSWORD}" -H "${LDAP_HOST}" -f /ldap/testorg.ldif

# Signify that the script has completed
echo "ready" > /tmp/ready

sleep 5
3 changes: 3 additions & 0 deletions tests/environment/ldap/testorg.ldif
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dn: ou=testorg,dc=example,dc=org
objectClass: organizationalUnit
ou: testorg
11 changes: 11 additions & 0 deletions tests/environment/test-setup/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM bitnami/openldap:latest

USER root

RUN apt-get update && apt-get upgrade -y && \
apt-get install --yes curl golang-go jq && \
apt-get clean && rm -rf /var/lib/apt/lists /var/cache/apt/archives

RUN GOPATH=/ go install github.com/zitadel/zitadel-tools@latest

USER 1001

0 comments on commit 32a0ef9

Please sign in to comment.