Skip to content

Commit

Permalink
Merge pull request #3497 from uselagoon/keycloak-email-realm-settings
Browse files Browse the repository at this point in the history
feat: support changing more settings in keycloak
  • Loading branch information
tobybellwood authored Aug 3, 2023
2 parents 7b5990b + 6e890c7 commit ad72236
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ api-development: build-ui-logs-development

.PHONY: ui-logs-development
ui-logs-development: build-ui-logs-development
IMAGE_REPO=$(CI_BUILD_TAG) docker-compose -p $(CI_BUILD_TAG) --compatibility up -d api api-db actions-handler local-api-data-watcher-pusher ui keycloak keycloak-db broker api-redis logs2notifications local-minio local-minio-upload
IMAGE_REPO=$(CI_BUILD_TAG) docker-compose -p $(CI_BUILD_TAG) --compatibility up -d api api-db actions-handler local-api-data-watcher-pusher ui keycloak keycloak-db broker api-redis logs2notifications local-minio local-minio-upload mailhog

## CI targets

Expand All @@ -362,7 +362,7 @@ STERN_VERSION = v2.6.1
CHART_TESTING_VERSION = v3.9.0
K3D_IMAGE = docker.io/rancher/k3s:v1.26.6-k3s1
TESTS = [nginx,api,features-kubernetes,bulk-deployment,features-kubernetes-2,features-variables,active-standby-kubernetes,tasks,drush,python,gitlab,github,bitbucket,services,workflows]
CHARTS_TREEISH = main
CHARTS_TREEISH = organizations
TASK_IMAGES = task-activestandby

# Symlink the installed kubectl client if the correct version is already
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ services:
image: mailhog/mailhog
platform: linux/amd64
ports:
- 8025
- '32025:8025'
webhooks2tasks:
image: ${IMAGE_REPO:-lagoon}/webhooks2tasks
command: yarn run dev
Expand Down Expand Up @@ -130,9 +130,12 @@ services:
- keycloak-db
ports:
- '8088:8080'
environment:
- KEYCLOAK_ADMIN_EMAIL=admin@example.com
volumes:
- "./services/keycloak/profile.properties:/opt/jboss/keycloak/standalone/configuration/profile.properties"
- "./services/keycloak/startup-scripts:/opt/jboss/startup-scripts"
- "./local-dev/keycloak:/lagoon/keycloak"
keycloak-db:
image: ${IMAGE_REPO:-lagoon}/keycloak-db
ports:
Expand Down
6 changes: 6 additions & 0 deletions local-dev/keycloak/keycloak-realm-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rememberMe": true,
"resetPasswordAllowed": true,
"verifyEmail": false,
"editUsernameAllowed": false
}
16 changes: 16 additions & 0 deletions local-dev/keycloak/keycloak-smtp-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"smtpServer": {
"envelopeFrom": "lagoon@example.com",
"from": "lagoon@example.com",
"fromDisplayName": "Local Lagoon",
"host": "mailhog",
"port": "1025",
"replyTo": "lagoon@example.com",
"replyToDisplayName": "Local Lagoon No-Reply",
"ssl": "false",
"starttls": "false",
"auth": "false",
"user": "not-used-if-auth=false",
"password": "not-used-if-auth=false"
}
}
37 changes: 37 additions & 0 deletions services/keycloak/startup-scripts/00-configure-lagoon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function configure_lagoon_realm {
CLIENT_ID=$(/opt/jboss/keycloak/bin/kcadm.sh get -r lagoon clients?clientId=lagoon-ui --config $CONFIG_PATH | jq -r '.[0]["id"]')
echo '{"protocol":"openid-connect","config":{"id.token.claim":"true","access.token.claim":"true","userinfo.token.claim":"true","user.attribute":"lagoon-uid","claim.name":"lagoon.user_id","jsonType.label":"int","multivalued":""},"name":"Lagoon User ID","protocolMapper":"oidc-usermodel-attribute-mapper"}' | /opt/jboss/keycloak/bin/kcadm.sh create -r ${KEYCLOAK_REALM:-master} clients/$CLIENT_ID/protocol-mappers/models --config $CONFIG_PATH -f -

# don't use KEYCLOAK_REALM_SETTINGS, use the 'configure_realm_settings' way to pass values from a file (inject by configmap/volume mount)
if [ "$KEYCLOAK_REALM_SETTINGS" ]; then
echo Applying extra Realm settings
echo $KEYCLOAK_REALM_SETTINGS | /opt/jboss/keycloak/bin/kcadm.sh update realms/${KEYCLOAK_REALM:-master} --config $CONFIG_PATH -f -
Expand All @@ -90,6 +91,39 @@ function configure_lagoon_realm {
fi
}

function configure_admin_email {
# Configure the admin user with an email address so that email configuration can be enabled in the lagoon realm
# this will always update the email address of the admin user if it is defined
if [ "$KEYCLOAK_ADMIN_EMAIL" != "" ]; then
echo Configuring admin user email to ${KEYCLOAK_ADMIN_EMAIL}
ADMIN_USER_ID=$(/opt/jboss/keycloak/bin/kcadm.sh get users -r master --config $CONFIG_PATH -q username=admin | jq -r '.[0]|.id')
/opt/jboss/keycloak/bin/kcadm.sh update users/${ADMIN_USER_ID} --config $CONFIG_PATH -s "email=${KEYCLOAK_ADMIN_EMAIL}"
fi

}

function configure_smtp_settings {
# this checks if the file containing the json data for email configuration exists
if [ "$KEYCLOAK_ADMIN_EMAIL" == "" ] && [ -f "/lagoon/keycloak/keycloak-smtp-settings.json" ]; then
echo "Admin email must be set to configure lagoon realm email server settings"
return 0
fi
if [ -f "/lagoon/keycloak/keycloak-smtp-settings.json" ]; then
echo Configuring lagoon realm email server settings
/opt/jboss/keycloak/bin/kcadm.sh update realms/lagoon --config $CONFIG_PATH -f /lagoon/keycloak/keycloak-smtp-settings.json
fi

}

function configure_realm_settings {
# this checks if the file containing the json data for realm settings exists
if [ -f "/lagoon/keycloak/keycloak-realm-settings.json" ]; then
echo Configuring lagoon realm settings
/opt/jboss/keycloak/bin/kcadm.sh update realms/lagoon --config $CONFIG_PATH -f /lagoon/keycloak/keycloak-realm-settings.json
fi

}

function configure_opendistro_security_client {

# delete old SearchGuard Clients
Expand Down Expand Up @@ -2431,6 +2465,9 @@ function configure_keycloak {

# Sets the order of migrations, add new ones at the end.
configure_lagoon_realm
configure_admin_email
configure_smtp_settings
configure_realm_settings
configure_opendistro_security_client
configure_api_client
add_group_viewall
Expand Down

0 comments on commit ad72236

Please sign in to comment.