Skip to content

Commit

Permalink
Merge pull request #83 from adorsys/35-use-keycloak-config-cli-to-bui…
Browse files Browse the repository at this point in the history
…ld-our-kc-environment-on-aws

Use keycloak config cli to build our kc environment on aws
  • Loading branch information
forkimenjeckayang authored Sep 12, 2024
2 parents bca263e + aa6851f commit bae5be0
Show file tree
Hide file tree
Showing 3 changed files with 2,795 additions and 1 deletion.
15 changes: 14 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,17 @@ KC_DB_OPTS="--db postgres --db-url jdbc:postgresql://kc-ssi-instance-1.clh0lvey1
# For AWS: Remember to provide cert and key pem files ($WORK_DIR/../env/.env)
KC_START="start --hostname-strict=false --https-port=$KEYCLOAK_HTTPS_PORT --https-certificate-file=$KC_SERVER_CERT --https-certificate-key-file=$KC_SERVER_KEY"
# For local env
# KC_START=start-dev
# KC_START=start-dev

# Keycloak config CLI
REPO_URL="https://github.com/adorsys/keycloak-config-cli.git"
KC_CLI_DIR=$WORK_DIR/config
KC_CLI_JAR_FILE=keycloak-config-cli.jar
KEYCLOAK_URL=https://kc-ssi.solutions.adorsys.com/
# Use this url when running locally
# KEYCLOAK_URL=https://localhost:8443
KC_REALM_FILE=$KC_CLI_DIR/realm.json
KC_CLI_PROJECT_DIR=$KC_CLI_DIR/keycloak-config-cli
KC_KEYSTORE_PATH=/opt/keycloak/target/kc_keystore.pkcs12
# Running locally(without the image) use this path for Keystore file
# KC_KEYSTORE_PATH=$TARGET_DIR/kc_keystore.pkcs12
51 changes: 51 additions & 0 deletions config/export_kc_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

# Variables
source load_env.sh

# Check if the CLI project folder already exits, if so remove and clone again...
if [ -d "$KC_CLI_PROJECT_DIR" ]; then
echo "Directory $KC_CLI_PROJECT_DIR exists. Removing it..."
rm -rf "$KC_CLI_PROJECT_DIR" || { echo "Failed to remove directory $KC_CLI_PROJECT_DIR"; exit 1; }
else
echo "Directory does not exist"
fi

# Clone the main branch of the Git repository
echo "Cloning repository from $REPO_URL..."
cd $KC_CLI_DIR && git clone --branch main "$REPO_URL" || { echo "Failed to clone repository"; exit 1; }

# Navigate to cloned dir and build CLI tool
cd "$KC_CLI_PROJECT_DIR" && ./mvnw clean install -DskipTests || { echo "Failed to build the CLI tool"; exit 1; }

# Check if JAR file is created in the target directory
if ls target/*.jar 1> /dev/null 2>&1; then
echo "Build successful! JAR file created."
else
echo "Build failed! No JAR file found."
exit 1
fi

# Define a temporary file to store the modified realm.json
MODIFIED_REALM_JSON="modified_realm.json"

# Replace the placeholders 'KEYCLOAK_KEYSTORE_PATH','KEYCLOAK_KEYSTORE_PASSWORD' and 'CLIENT_SECRETin' in the realm.json file with the actual value from the .env
sed -e "s|KC_KEYSTORE_PATH|$KC_KEYSTORE_PATH|g" \
-e "s|KEYCLOAK_KEYSTORE_PASSWORD|$KEYCLOAK_KEYSTORE_PASSWORD|g" \
-e "s|CLIENT_SECRET|$CLIENT_SECRET|g" \
$KC_REALM_FILE > $MODIFIED_REALM_JSON

# Run the JAR file with the specified parameters
echo "Running the JAR file..."
java -jar target/$KC_CLI_JAR_FILE \
-Dimport-realm="true" \
-Dforce="true" \
--keycloak.url="$KEYCLOAK_URL" \
--keycloak.user="$KEYCLOAK_ADMIN" \
--keycloak.password="$KEYCLOAK_ADMIN_PASSWORD" \
--keycloak.ssl-verify="true" \
--import.files.locations="$MODIFIED_REALM_JSON" || { echo "Failed to run the JAR file"; exit 1; }
echo "Script completed successfully."

# If everything is successful, delete the modified realm file
rm -f "$MODIFIED_REALM_JSON"
Loading

0 comments on commit bae5be0

Please sign in to comment.