Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse session when running kcadm commands #328

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions files/kcadm-wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# shellcheck source=/dev/null
. /opt/keycloak/conf/kcadm-wrapper.conf

EXPIRES=$(/usr/bin/sed -n -r 's|.*"refreshExpiresAt" : ([0-9]*).*|\1|p' "$CONFIG" || echo "0")
NOW=$(/usr/bin/date +%s%3N)

if [ ! -f "$CONFIG" ] || [ "$EXPIRES" -gt "$NOW" ]; then
Copy link
Contributor

@nblock nblock Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be: if [ ! -f "$CONFIG" ] || [ "$EXPIRES" -lt "$NOW" ]; then otherwise a full login is performed with each invocation.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that, I've updated. Hoping to test locally sometime today with some extra logging in a branch to fully validate this new logic.

${KCADM} config credentials --config "$CONFIG" --server "$SERVER" --realm "$REALM" --user "$ADMIN_USER" --password "$PASSWORD"
fi

${KCADM} "$@" --config "$CONFIG"
25 changes: 20 additions & 5 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,33 @@
}
}

# Template uses:
# - $keycloak::install_base
# - $keycloak::admin_user
# - $keycloak::admin_user_password
$wrapper_conf = {
'KCADM' => "${keycloak::install_base}/bin/kcadm.sh",
'CONFIG' => "${keycloak::conf_dir}/kcadm.config",
'SERVER' => $keycloak::wrapper_server,
'REALM' => 'master',
'ADMIN_USER' => $keycloak::admin_user,
'PASSWORD' => $keycloak::admin_user_password,
}
file { 'kcadm-wrapper.conf':
ensure => 'file',
path => $keycloak::wrapper_conf,
owner => $keycloak::user,
group => $keycloak::group,
mode => '0640',
content => epp('keycloak/shell_vars.epp', { 'vars' => $wrapper_conf }),
show_diff => false,
}

file { 'kcadm-wrapper.sh':
ensure => 'file',
path => $keycloak::wrapper_path,
owner => $keycloak::user,
group => $keycloak::group,
mode => '0750',
content => template('keycloak/kcadm-wrapper.sh.erb'),
source => 'puppet:///modules/keycloak/kcadm-wrapper.sh',
show_diff => false,
require => File['kcadm-wrapper.conf'],
}

file { $keycloak::conf_dir:
Expand Down
3 changes: 2 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@
Optional[Stdlib::Absolutepath] $service_environment_file = undef,
Stdlib::Filemode $conf_dir_mode = '0755',
Boolean $conf_dir_purge = true,
Array $conf_dir_purge_ignore = ['cache-ispn.xml', 'README.md', 'truststore.jks'],
Array $conf_dir_purge_ignore = ['cache-ispn.xml', 'README.md', 'truststore.jks', 'kcadm.config'],
Keycloak::Configs $configs = {},
Hash[String, Variant[String[1],Boolean,Array]] $extra_configs = {},
Variant[Stdlib::Host, Stdlib::HTTPUrl, Stdlib::HTTPSUrl, Enum['unset','UNSET']] $hostname = $facts['networking']['fqdn'],
Expand Down Expand Up @@ -330,6 +330,7 @@
$tmp_dir = "${install_base}/tmp"
$providers_dir = "${install_base}/providers"
$wrapper_path = "${keycloak::install_base}/bin/kcadm-wrapper.sh"
$wrapper_conf = "${conf_dir}/kcadm-wrapper.conf"

$default_config = {
'hostname' => $hostname,
Expand Down
3 changes: 2 additions & 1 deletion spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@
owner: 'keycloak',
group: 'keycloak',
mode: '0750',
content: %r{.*},
source: 'puppet:///modules/keycloak/kcadm-wrapper.sh',
show_diff: 'false',
require: 'File[kcadm-wrapper.conf]',
)
end

Expand Down
5 changes: 0 additions & 5 deletions templates/kcadm-wrapper.sh.erb

This file was deleted.

8 changes: 8 additions & 0 deletions templates/shell_vars.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%- |
Hash[String, String] $vars
| -%>
# This file is managed by Puppet, DO NOT EDIT

<% $vars.each |$key, $value| { -%>
<%= $key %>='<%= $value %>'
<% } -%>