104 lines
4.9 KiB
Bash
Executable File
104 lines
4.9 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
set -euo pipefail
|
|
|
|
apk add --no-cache curl jq kubectl >/dev/null
|
|
|
|
KC_URL="http://keycloak.sso.svc.cluster.local"
|
|
ACCESS_TOKEN=""
|
|
for attempt in 1 2 3 4 5; do
|
|
TOKEN_JSON="$(curl -sS -X POST "$KC_URL/realms/master/protocol/openid-connect/token" \
|
|
-H 'Content-Type: application/x-www-form-urlencoded' \
|
|
-d "grant_type=password" \
|
|
-d "client_id=admin-cli" \
|
|
-d "username=${KEYCLOAK_ADMIN}" \
|
|
-d "password=${KEYCLOAK_ADMIN_PASSWORD}" || true)"
|
|
ACCESS_TOKEN="$(echo "$TOKEN_JSON" | jq -r '.access_token' 2>/dev/null || true)"
|
|
if [ -n "$ACCESS_TOKEN" ] && [ "$ACCESS_TOKEN" != "null" ]; then
|
|
break
|
|
fi
|
|
echo "Keycloak token request failed (attempt ${attempt})" >&2
|
|
sleep $((attempt * 2))
|
|
done
|
|
if [ -z "$ACCESS_TOKEN" ] || [ "$ACCESS_TOKEN" = "null" ]; then
|
|
echo "Failed to fetch Keycloak admin token" >&2
|
|
exit 1
|
|
fi
|
|
|
|
CLIENT_QUERY="$(curl -sS -H "Authorization: Bearer ${ACCESS_TOKEN}" \
|
|
"$KC_URL/admin/realms/atlas/clients?clientId=vault-oidc" || true)"
|
|
CLIENT_ID="$(echo "$CLIENT_QUERY" | jq -r '.[0].id' 2>/dev/null || true)"
|
|
|
|
if [ -z "$CLIENT_ID" ] || [ "$CLIENT_ID" = "null" ]; then
|
|
create_payload='{"clientId":"vault-oidc","enabled":true,"protocol":"openid-connect","publicClient":false,"standardFlowEnabled":true,"implicitFlowEnabled":false,"directAccessGrantsEnabled":false,"serviceAccountsEnabled":false,"redirectUris":["https://secret.bstein.dev/ui/vault/auth/oidc/oidc/callback","http://localhost:8250/oidc/callback"],"webOrigins":["https://secret.bstein.dev"],"rootUrl":"https://secret.bstein.dev","baseUrl":"/"}'
|
|
status="$(curl -sS -o /dev/null -w "%{http_code}" -X POST \
|
|
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
|
|
-H 'Content-Type: application/json' \
|
|
-d "${create_payload}" \
|
|
"$KC_URL/admin/realms/atlas/clients")"
|
|
if [ "$status" != "201" ] && [ "$status" != "204" ]; then
|
|
echo "Keycloak client create failed (status ${status})" >&2
|
|
exit 1
|
|
fi
|
|
CLIENT_QUERY="$(curl -sS -H "Authorization: Bearer ${ACCESS_TOKEN}" \
|
|
"$KC_URL/admin/realms/atlas/clients?clientId=vault-oidc" || true)"
|
|
CLIENT_ID="$(echo "$CLIENT_QUERY" | jq -r '.[0].id' 2>/dev/null || true)"
|
|
fi
|
|
|
|
if [ -z "$CLIENT_ID" ] || [ "$CLIENT_ID" = "null" ]; then
|
|
echo "Keycloak client vault-oidc not found" >&2
|
|
exit 1
|
|
fi
|
|
|
|
SCOPE_ID="$(curl -sS -H "Authorization: Bearer ${ACCESS_TOKEN}" \
|
|
"$KC_URL/admin/realms/atlas/client-scopes?search=groups" | jq -r '.[] | select(.name=="groups") | .id' 2>/dev/null | head -n1 || true)"
|
|
if [ -z "$SCOPE_ID" ] || [ "$SCOPE_ID" = "null" ]; then
|
|
echo "Keycloak client scope groups not found" >&2
|
|
exit 1
|
|
fi
|
|
|
|
DEFAULT_SCOPES="$(curl -sS -H "Authorization: Bearer ${ACCESS_TOKEN}" \
|
|
"$KC_URL/admin/realms/atlas/clients/${CLIENT_ID}/default-client-scopes" || true)"
|
|
OPTIONAL_SCOPES="$(curl -sS -H "Authorization: Bearer ${ACCESS_TOKEN}" \
|
|
"$KC_URL/admin/realms/atlas/clients/${CLIENT_ID}/optional-client-scopes" || true)"
|
|
|
|
if ! echo "$DEFAULT_SCOPES" | jq -e '.[] | select(.name=="groups")' >/dev/null 2>&1 \
|
|
&& ! echo "$OPTIONAL_SCOPES" | jq -e '.[] | select(.name=="groups")' >/dev/null 2>&1; then
|
|
status="$(curl -sS -o /dev/null -w "%{http_code}" -X PUT \
|
|
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
|
|
"$KC_URL/admin/realms/atlas/clients/${CLIENT_ID}/optional-client-scopes/${SCOPE_ID}")"
|
|
if [ "$status" != "200" ] && [ "$status" != "201" ] && [ "$status" != "204" ]; then
|
|
status="$(curl -sS -o /dev/null -w "%{http_code}" -X POST \
|
|
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
|
|
"$KC_URL/admin/realms/atlas/clients/${CLIENT_ID}/optional-client-scopes/${SCOPE_ID}")"
|
|
if [ "$status" != "200" ] && [ "$status" != "201" ] && [ "$status" != "204" ]; then
|
|
echo "Failed to attach groups client scope to vault-oidc (status ${status})" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
CLIENT_SECRET="$(curl -sS -H "Authorization: Bearer ${ACCESS_TOKEN}" \
|
|
"$KC_URL/admin/realms/atlas/clients/${CLIENT_ID}/client-secret" | jq -r '.value' 2>/dev/null || true)"
|
|
if [ -z "$CLIENT_SECRET" ] || [ "$CLIENT_SECRET" = "null" ]; then
|
|
echo "Keycloak client secret not found" >&2
|
|
exit 1
|
|
fi
|
|
|
|
kubectl -n vault create secret generic vault-oidc-config \
|
|
--from-literal=discovery_url="https://sso.bstein.dev/realms/atlas" \
|
|
--from-literal=client_id="vault-oidc" \
|
|
--from-literal=client_secret="${CLIENT_SECRET}" \
|
|
--from-literal=default_role="admin" \
|
|
--from-literal=scopes="openid profile email groups" \
|
|
--from-literal=user_claim="preferred_username" \
|
|
--from-literal=groups_claim="groups" \
|
|
--from-literal=redirect_uris="https://secret.bstein.dev/ui/vault/auth/oidc/oidc/callback,http://localhost:8250/oidc/callback" \
|
|
--from-literal=bound_audiences="vault-oidc" \
|
|
--from-literal=admin_group="admin" \
|
|
--from-literal=admin_policies="default,vault-admin" \
|
|
--from-literal=dev_group="dev" \
|
|
--from-literal=dev_policies="default,dev-kv" \
|
|
--from-literal=user_group="dev" \
|
|
--from-literal=user_policies="default,dev-kv" \
|
|
--dry-run=client -o yaml | kubectl -n vault apply -f - >/dev/null
|