From 2cd3e8cf13a4da16dd65fce057253157d288337a Mon Sep 17 00:00:00 2001 From: Trey Dockendorf Date: Sat, 16 Nov 2024 10:04:26 -0500 Subject: [PATCH] Reuse session when running kcadm commands Fixes #327 --- templates/kcadm-wrapper.sh.erb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/templates/kcadm-wrapper.sh.erb b/templates/kcadm-wrapper.sh.erb index d627f8ce..c0809320 100644 --- a/templates/kcadm-wrapper.sh.erb +++ b/templates/kcadm-wrapper.sh.erb @@ -1,5 +1,14 @@ #!/bin/bash KCADM="<%= scope['keycloak::install_base'] %>/bin/kcadm.sh" +CONFIG="/root/.keycloak/kcadm.config" +EXPIRES=$(/usr/bin/sed -n -r 's|.*"refreshExpiresAt" : ([0-9]*).*|\1|p' ~/.keycloak/kcadm.config) +NOW=$(/usr/bin/date +%s%3N) -${KCADM} "$@" --no-config --server '<%= scope['keycloak::wrapper_server'] %>' --realm master --user '<%= scope['keycloak::admin_user'] %>' --password '<%= scope['keycloak::admin_user_password'] %>' +if [ ! -f "$CONFIG" ]; then + ${KCADM} config credentials --server '<%= scope['keycloak::wrapper_server'] %>' --realm master --user '<%= scope['keycloak::admin_user'] %>' --password '<%= scope['keycloak::admin_user_password'] %>' +elif [ "$EXPIRES" -gt "$NOW" ]; then + ${KCADM} config credentials --server '<%= scope['keycloak::wrapper_server'] %>' --realm master --user '<%= scope['keycloak::admin_user'] %>' --password '<%= scope['keycloak::admin_user_password'] %>' +fi + +${KCADM} "$@" --server '<%= scope['keycloak::wrapper_server'] %>' --realm master --user '<%= scope['keycloak::admin_user'] %>' --password '<%= scope['keycloak::admin_user_password'] %>'