-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_keycloak_token.sh
More file actions
executable file
·305 lines (274 loc) · 7.31 KB
/
Copy pathget_keycloak_token.sh
File metadata and controls
executable file
·305 lines (274 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage: ./deployment/scripts/auth/get_keycloak_token.sh [options] USERNAME [PASSWORD]
Options:
--field NAME Response field to print: id_token (default), access_token, refresh_token, json
--scheme SCHEME URL scheme: http (default) or https
--host HOST Keycloak host (default: localhost)
--port PORT Keycloak host port (default: $KEYCLOAK_HOST_PORT or 8082)
--realm NAME Keycloak realm (default: $KEYCLOAK_REALM or envector)
--client-id ID OAuth client ID (default: $KEYCLOAK_CLIENT_ID or envector-cli)
--client-secret S OAuth client secret (optional)
--scopes VALUE OAuth scopes (default: $KEYCLOAK_TOKEN_SCOPES or "openid profile email" or "openid profile email audit:read audit:export")
--cacert PATH CA certificate to trust for TLS
--resolve VALUE curl --resolve value such as keycloak.local.test:443:192.168.49.2
--insecure Skip TLS certificate verification (useful for local CA bootstrap)
--timeout SECONDS Wait timeout for Keycloak readiness (default: 30)
-h, --help Show this help
Examples:
./deployment/scripts/auth/get_keycloak_token.sh app password
./deployment/scripts/auth/get_keycloak_token.sh --field json ops password
./deployment/scripts/auth/get_keycloak_token.sh --scheme https --host keycloak.local.test --port 443 --insecure app password
EOF
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "Missing required command: $1" >&2
exit 1
}
}
json_backend=""
python_cmd=""
setup_json_backend() {
if command -v jq >/dev/null 2>&1; then
json_backend="jq"
return
fi
if command -v python3 >/dev/null 2>&1; then
json_backend="python"
python_cmd="$(command -v python3)"
return
fi
if command -v python >/dev/null 2>&1; then
json_backend="python"
python_cmd="$(command -v python)"
return
fi
echo "Missing required command: jq or python3 or python" >&2
exit 1
}
json_get_field() {
local payload="$1"
local field_name="$2"
if [[ "$json_backend" == "jq" ]]; then
jq -r --arg field "$field_name" '.[$field] // empty' <<<"$payload"
return
fi
printf '%s' "$payload" | "$python_cmd" -c '
import json
import sys
field = sys.argv[1]
data = json.load(sys.stdin)
value = data.get(field, "")
if value is None:
value = ""
if value != "":
print(value)
' "$field_name"
}
format_host_for_url() {
local value="$1"
if [[ "$value" == *:* && "$value" != \[*\] ]]; then
printf '[%s]\n' "$value"
return
fi
printf '%s\n' "$value"
}
is_loopback_host() {
case "$1" in
127.0.0.1|localhost|::1)
return 0
;;
*)
return 1
;;
esac
}
field="id_token"
scheme="${KEYCLOAK_SCHEME:-http}"
host="${KEYCLOAK_HOST:-localhost}"
port="${KEYCLOAK_HOST_PORT:-8082}"
realm="${KEYCLOAK_REALM:-envector}"
client_id="${KEYCLOAK_CLIENT_ID:-envector-cli}"
client_secret="${KEYCLOAK_CLIENT_SECRET:-}"
scopes="${KEYCLOAK_TOKEN_SCOPES:-openid profile email}"
timeout_seconds=30
ca_cert=""
resolve_value=""
insecure=false
forward_local_http_as_https="${KEYCLOAK_FORWARD_LOCAL_HTTP_AS_HTTPS:-true}"
while (($#)); do
case "$1" in
--field)
[[ $# -ge 2 ]] || { echo "--field requires a value" >&2; exit 1; }
field="$2"
shift 2
;;
--scheme)
[[ $# -ge 2 ]] || { echo "--scheme requires a value" >&2; exit 1; }
scheme="$2"
shift 2
;;
--host)
[[ $# -ge 2 ]] || { echo "--host requires a value" >&2; exit 1; }
host="$2"
shift 2
;;
--port)
[[ $# -ge 2 ]] || { echo "--port requires a value" >&2; exit 1; }
port="$2"
shift 2
;;
--realm)
[[ $# -ge 2 ]] || { echo "--realm requires a value" >&2; exit 1; }
realm="$2"
shift 2
;;
--client-id)
[[ $# -ge 2 ]] || { echo "--client-id requires a value" >&2; exit 1; }
client_id="$2"
shift 2
;;
--client-secret)
[[ $# -ge 2 ]] || { echo "--client-secret requires a value" >&2; exit 1; }
client_secret="$2"
shift 2
;;
--scopes)
[[ $# -ge 2 ]] || { echo "--scopes requires a value" >&2; exit 1; }
scopes="$2"
shift 2
;;
--cacert)
[[ $# -ge 2 ]] || { echo "--cacert requires a value" >&2; exit 1; }
ca_cert="$2"
shift 2
;;
--resolve)
[[ $# -ge 2 ]] || { echo "--resolve requires a value" >&2; exit 1; }
resolve_value="$2"
shift 2
;;
--insecure)
insecure=true
shift
;;
--timeout)
[[ $# -ge 2 ]] || { echo "--timeout requires a value" >&2; exit 1; }
timeout_seconds="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
-*)
echo "Unknown option: $1" >&2
usage >&2
exit 1
;;
*)
break
;;
esac
done
require_cmd curl
setup_json_backend
username="${1:-}"
password="${2:-password}"
if [[ -z "$username" ]]; then
echo "USERNAME is required" >&2
usage >&2
exit 1
fi
case "$field" in
id_token|access_token|refresh_token|json) ;;
*)
echo "--field must be one of: id_token, access_token, refresh_token, json" >&2
exit 1
;;
esac
case "$scheme" in
http|https) ;;
*)
echo "--scheme must be one of: http, https" >&2
exit 1
;;
esac
common_curl_args=()
if "$insecure"; then
common_curl_args+=(-k)
fi
if [[ "$scheme" == "http" ]] && [[ "$forward_local_http_as_https" == "true" ]] && is_loopback_host "$host"; then
common_curl_args+=(-H "X-Forwarded-Proto: https")
fi
if [[ -n "$ca_cert" ]]; then
common_curl_args+=(--cacert "$ca_cert")
fi
if [[ -n "$resolve_value" ]]; then
common_curl_args+=(--resolve "$resolve_value")
fi
run_curl() {
if ((${#common_curl_args[@]})); then
curl "${common_curl_args[@]}" "$@"
return
fi
curl "$@"
}
base_url="${scheme}://$(format_host_for_url "$host"):${port}"
discovery_url="${base_url}/realms/${realm}/.well-known/openid-configuration"
token_url="${base_url}/realms/${realm}/protocol/openid-connect/token"
start_time=$(date +%s)
until run_curl -fsS "$discovery_url" >/dev/null 2>&1; do
now=$(date +%s)
if (( now - start_time >= timeout_seconds )); then
echo "Timed out waiting for Keycloak discovery endpoint: $discovery_url" >&2
exit 1
fi
sleep 1
done
response_file="$(mktemp)"
curl_args=()
if ((${#common_curl_args[@]})); then
curl_args+=("${common_curl_args[@]}")
fi
curl_args+=(
-sS
-w '%{http_code}'
-o "$response_file"
-X POST "$token_url"
-H "content-type: application/x-www-form-urlencoded"
--data-urlencode "grant_type=password"
--data-urlencode "scope=${scopes}"
--data-urlencode "username=${username}"
--data-urlencode "password=${password}"
--data-urlencode "client_id=${client_id}"
)
if [[ -n "$client_secret" ]]; then
curl_args+=(--data-urlencode "client_secret=${client_secret}")
fi
http_status="$(curl "${curl_args[@]}")"
response="$(cat "$response_file")"
rm -f "$response_file"
if [[ "$http_status" != "200" ]]; then
echo "Keycloak token request failed: HTTP ${http_status}" >&2
printf '%s\n' "$response" >&2
exit 1
fi
if [[ "$field" == "json" ]]; then
printf '%s\n' "$response"
exit 0
fi
token="$(json_get_field "$response" "$field")"
if [[ -z "$token" ]]; then
echo "Keycloak response did not contain ${field}" >&2
printf '%s\n' "$response" >&2
exit 1
fi
printf '%s\n' "$token"