Skip to content

Commit 8647522

Browse files
authored
Reuse session when running kcadm commands (#328)
* Reuse session when running kcadm commands Fixes #327 * Move to using kcadm-wrapper config file so that script is just a script and not a template * Fix conditional * Remove kcmadm login session when Keycloak service restarts unless using persistent sessions
1 parent 8516850 commit 8647522

File tree

7 files changed

+50
-12
lines changed

7 files changed

+50
-12
lines changed

files/kcadm-wrapper.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# shellcheck source=/dev/null
4+
. /opt/keycloak/conf/kcadm-wrapper.conf
5+
6+
EXPIRES=$(/usr/bin/sed -n -r 's|.*"refreshExpiresAt" : ([0-9]*).*|\1|p' "$CONFIG" 2>/dev/null || echo "0")
7+
NOW=$(/usr/bin/date +%s%3N)
8+
9+
if [ ! -f "$CONFIG" ] || [ "$EXPIRES" -lt "$NOW" ]; then
10+
${KCADM} config credentials --config "$CONFIG" --server "$SERVER" --realm "$REALM" --user "$ADMIN_USER" --password "$PASSWORD"
11+
fi
12+
13+
${KCADM} "$@" --config "$CONFIG"

manifests/config.pp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,33 @@
99
}
1010
}
1111

12-
# Template uses:
13-
# - $keycloak::install_base
14-
# - $keycloak::admin_user
15-
# - $keycloak::admin_user_password
12+
$wrapper_conf = {
13+
'KCADM' => "${keycloak::install_base}/bin/kcadm.sh",
14+
'CONFIG' => $keycloak::login_config,
15+
'SERVER' => $keycloak::wrapper_server,
16+
'REALM' => 'master',
17+
'ADMIN_USER' => $keycloak::admin_user,
18+
'PASSWORD' => $keycloak::admin_user_password,
19+
}
20+
file { 'kcadm-wrapper.conf':
21+
ensure => 'file',
22+
path => $keycloak::wrapper_conf,
23+
owner => $keycloak::user,
24+
group => $keycloak::group,
25+
mode => '0640',
26+
content => epp('keycloak/shell_vars.epp', { 'vars' => $wrapper_conf }),
27+
show_diff => false,
28+
}
29+
1630
file { 'kcadm-wrapper.sh':
1731
ensure => 'file',
1832
path => $keycloak::wrapper_path,
1933
owner => $keycloak::user,
2034
group => $keycloak::group,
2135
mode => '0750',
22-
content => template('keycloak/kcadm-wrapper.sh.erb'),
36+
source => 'puppet:///modules/keycloak/kcadm-wrapper.sh',
2337
show_diff => false,
38+
require => File['kcadm-wrapper.conf'],
2439
}
2540

2641
file { $keycloak::conf_dir:

manifests/init.pp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@
240240
Optional[Stdlib::Absolutepath] $service_environment_file = undef,
241241
Stdlib::Filemode $conf_dir_mode = '0755',
242242
Boolean $conf_dir_purge = true,
243-
Array $conf_dir_purge_ignore = ['cache-ispn.xml', 'README.md', 'truststore.jks'],
243+
Array $conf_dir_purge_ignore = ['cache-ispn.xml', 'README.md', 'truststore.jks', 'kcadm.config'],
244244
Keycloak::Configs $configs = {},
245245
Hash[String, Variant[String[1],Boolean,Array]] $extra_configs = {},
246246
Variant[Stdlib::Host, Stdlib::HTTPUrl, Stdlib::HTTPSUrl, Enum['unset','UNSET']] $hostname = $facts['networking']['fqdn'],
@@ -330,6 +330,8 @@
330330
$tmp_dir = "${install_base}/tmp"
331331
$providers_dir = "${install_base}/providers"
332332
$wrapper_path = "${keycloak::install_base}/bin/kcadm-wrapper.sh"
333+
$wrapper_conf = "${conf_dir}/kcadm-wrapper.conf"
334+
$login_config = "${conf_dir}/kcadm.config"
333335

334336
$default_config = {
335337
'hostname' => $hostname,

spec/classes/init_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@
121121
owner: 'keycloak',
122122
group: 'keycloak',
123123
mode: '0750',
124-
content: %r{.*},
124+
source: 'puppet:///modules/keycloak/kcadm-wrapper.sh',
125125
show_diff: 'false',
126+
require: 'File[kcadm-wrapper.conf]',
126127
)
127128
end
128129

templates/kcadm-wrapper.sh.erb

Lines changed: 0 additions & 5 deletions
This file was deleted.

templates/keycloak.service.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Environment='JAVA_HOME=<%= scope['keycloak::java_home'] %>'
2121
User=<%= scope['keycloak::user'] %>
2222
Group=<%= scope['keycloak::group'] %>
2323
ExecStart=<%= scope['keycloak::service_start_cmd'] %>
24+
# TODO: remove once upgraded from Keycloak 25 to 26
25+
<% unless (scope['keycloak::features'] || []).include?('persistent-user-sessions') -%>
26+
ExecStartPost=-/usr/bin/rm -f <%= scope['keycloak::login_config'] %>
27+
<% end -%>
2428
TimeoutStartSec=600
2529
TimeoutStopSec=600
2630
SuccessExitStatus=0 143

templates/shell_vars.epp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<%- |
2+
Hash[String, String] $vars
3+
| -%>
4+
# This file is managed by Puppet, DO NOT EDIT
5+
6+
<% $vars.each |$key, $value| { -%>
7+
<%= $key %>='<%= $value %>'
8+
<% } -%>

0 commit comments

Comments
 (0)