Replace a secret of the login service
Summary. Three settings hold a secret of the login service. That service is Keycloak,
the oidc container, and it holds the staff and applicant accounts. Each secret lives in
two places: the deploy tool (Coolify) and the login service itself. A new value in one place
only breaks a sign-in. This runbook replaces a secret in both places, in a fixed order. On
2026-10-12 a new value existed only in a running program and was lost (issue #4211).
The order, for each secret. (1) Make the value. (2) Write it to the deploy tool and read it back. (3) Change the login service. (4) Prove the result. (5) Delete each temporary copy.
The three secrets
| Setting | What it is | What reads it |
|---|---|---|
OPENGOV_KC_ADMIN_CLIENT_SECRET | Secret of the client opengov-rights-admin | The app, for the rights screens. Since PR #4142 the app does not start with the published default. |
OPENGOV_API_CLIENT_SECRET | Secret of the client opengov-api | The login service, at its first start only. Some tools read it from a container. |
KC_BOOTSTRAP_ADMIN_PASSWORD | Password of the administrator of the login service (realm master) | The login service at its first start. The setup job (init-keycloak) signs in with it at each deploy. |
Rules for each step
- Approval. Each step runs on the host or in the deploy tool. The owner approves each step before it runs. Show the owner the command and what it changes.
- No value on a command line. The process list shows each command line. A value stays in
a file with mode 0600 under
/root/rotate, and never goes into a log, an issue or a chat. - No deploy between step 2 and step 3. That deploy gives the containers a value that the
login service refuses. On the sandbox, each landing on
mainstarts a deploy. So the step “Pause the deploys” comes before step 2, and “Resume the deploys” comes after step 4. - The administrator password comes last. The other steps sign in with it.
Before you start
Run these lines on the host, as root, in one bash session. KEY names the secret to replace.
umask 077 && mkdir -p /root/rotate && cd /root/rotate
KEY=OPENGOV_API_CLIENT_SECRET # or one of the two other settings
L="https://login.<base>" # the public address of the login service
APP_UUID="<app uuid>" # its folder name under /data/coolify/applications/
start() { # Returns at the first fault. Each line after it runs only through `&&`.
OIDC=$(docker ps -q -f label=com.docker.compose.project=$APP_UUID -f label=com.docker.compose.service=oidc)
[ "$(printf '%s\n' "$OIDC" | grep -c .)" = 1 ] || { echo "STOP: not exactly one login container"; return 1; }
ADMIN_USER=$(docker exec "$OIDC" sh -c 'printf %s "$KC_BOOTSTRAP_ADMIN_USERNAME"')
docker exec "$OIDC" sh -c "printf %s \"\$$KEY\"" > old # the value now
[ -s old ] || { echo "STOP: the login container holds no $KEY"; return 1; }
docker exec "$OIDC" sh -c 'printf %s "$KC_BOOTSTRAP_ADMIN_PASSWORD"' > admin-now
[ -s admin-now ] || { echo "STOP: the login container holds no administrator password"; return 1; }
APP_ID=$(docker exec coolify-db psql -U coolify -d coolify -At -c "select id from applications where uuid='$APP_UUID'")
[ -n "$APP_ID" ] || { echo "STOP: the deploy tool has no application $APP_UUID"; return 1; }
}
kc() { KC_CLI_PASSWORD="$(cat /root/rotate/admin-now)" docker exec -i -e KC_CLI_PASSWORD "$OIDC" \
/opt/keycloak/bin/kcadm.sh "$@" --no-config --server http://localhost:8080 --realm master --user "$ADMIN_USER"; }
prove() { # $1: the file that holds the value to try. Prints the answer code only.
case "$KEY" in
OPENGOV_API_CLIENT_SECRET) R=opengov; F="grant_type=password&client_id=opengov-api&username=nobody-$RANDOM&password=x&client_secret=" ;;
OPENGOV_KC_ADMIN_CLIENT_SECRET) R=opengov; F="grant_type=client_credentials&client_id=opengov-rights-admin&client_secret=" ;;
KC_BOOTSTRAP_ADMIN_PASSWORD) R=master; F="grant_type=password&client_id=admin-cli&username=$ADMIN_USER&password=" ;;
esac
{ printf %s "$F"; cat "$1"; } | curl -s -o /dev/null -w '%{http_code}\n' --data-binary @- "$L/realms/$R/protocol/openid-connect/token"; }
start && kc get realms/master --fields realm && prove old
# `kc` must print "realm" : "master". `prove old` must give the accept answer of step 4.
The labels select the login container of this deployment only. A name filter matches parts of names, so it can select the login container of another deployment on the same host.
kcadm.sh is the command tool of the login service. It reads the administrator password from
the variable KC_CLI_PASSWORD, which docker exec -e copies from the shell. It works against
http://localhost:8080 in the container, so no step needs the public /admin address.
If start prints STOP, nothing after it ran: stop and ask the owner. If kc or prove old
fails, the containers hold an older value than the login service. Stop and ask the owner. A
deploy gives the containers the values of the deploy tool.
Pause the deploys (the sandbox only)
On the sandbox the workflow post-deploy-smoke.yml starts each deploy. A country has no
automatic deploy, so it skips this step.
First, on your machine, switch the workflow off. A run that started before still deploys, and so does the one run that waits behind it. So wait until no run is open:
gh workflow disable post-deploy-smoke.yml --repo UNCTAD-OpenGov/opengov
gh run list --workflow post-deploy-smoke.yml --repo UNCTAD-OpenGov/opengov --limit 20 \
--json databaseId,status --jq '.[] | select(.status != "completed") | .databaseId'
The list must be empty. Otherwise wait until each run ends, or stop it with
gh run cancel <id> --repo UNCTAD-OpenGov/opengov, and read the list again.
Then, on the host, in the session of “Before you start”:
quiet() { # Returns at the first fault. Needs APP_ID from `start`.
[ -n "$APP_ID" ] || { echo "STOP: no APP_ID. Run start first."; return 1; }
AUTO=$(docker exec coolify-db psql -U coolify -d coolify -At -c \
"select is_auto_deploy_enabled from application_settings where application_id=$APP_ID")
[ "$AUTO" = f ] || { echo "STOP: Coolify's own automatic deploy is on ($AUTO)"; return 1; }
N=$(docker exec coolify-db psql -U coolify -d coolify -At -c \
"select count(*) from application_deployment_queues where application_id='$APP_ID' and status in ('queued','in_progress')")
[ "$N" = 0 ] || { echo "STOP: $N deploys are queued or running. Wait, then run quiet again."; return 1; }
echo "no deploy is queued or running"
}
quiet
Keep the pause short. CI on main keeps running during the pause. After two green CI runs,
the watchdog of scheduled work can report the deploy workflow as dead (the liveness: on-parent rule of post-deploy-smoke.yml). Such a report comes from the pause. Close it after
“Resume the deploys”.
Step 1. Make the value
openssl rand -hex 32 | tr -d '\n' > new # 64 characters, safe in JSON, a form and a URL
Step 2. Write the value to the deploy tool and read it back
Way 1 is the standard way (owner decision, 2026-10-13). It writes through the data model of the deploy tool, over root SSH. The model encrypts the value, as the screen does. The tool keeps two rows for each key, for normal and for preview deploys. Way 1 writes both rows.
cat > set.php <<'PHP'
$key = '__KEY__';
$app = \App\Models\Application::where('uuid', '__APP_UUID__')->value('id');
$v = trim(file_get_contents('/tmp/rotate-value'));
$rows = \App\Models\EnvironmentVariable::where('resourceable_type', 'App\Models\Application')
->where('resourceable_id', $app)->where('key', $key)->get();
echo 'rows: ' . $rows->count() . PHP_EOL;
if ($app && strlen($v) >= __MIN__ && $rows->count() === 2) foreach ($rows as $e) {
if ($e->key === $key && (int) $e->resourceable_id === (int) $app) { $e->value = $v; $e->save(); }
}
foreach ($rows as $e) { $e->refresh(); // read each row back from the database
echo ($e->is_preview ? 'preview' : 'normal') . ' same: ' . ($e->value === $v ? 'yes' : 'no') . PHP_EOL; }
PHP
to_coolify() { # $1: the file that holds the value. $2: the shortest value that it accepts.
sed "s/__KEY__/$KEY/; s/__APP_UUID__/$APP_UUID/; s/__MIN__/$2/" set.php > set-now.php
docker cp "$1" coolify:/tmp/rotate-value && docker cp set-now.php coolify:/tmp/set.php
docker exec -u 0 coolify chown 9999 /tmp/rotate-value /tmp/set.php # tinker runs as uid 9999
docker exec coolify sh -c 'php artisan tinker --execute="$(cat /tmp/set.php)"'
docker exec -u 0 coolify rm -f /tmp/rotate-value /tmp/set.php
}
to_coolify new 32 # a new value has 64 characters
The output must be rows: 2, normal same: yes and preview same: yes. With another
number of rows, the script changes nothing. Then stop and ask the owner.
Way 3 is the fallback, for a person without root SSH. Type the value in the screen of the
deploy tool, in the normal row and in the preview row. Open each row again and compare it
with your copy. Step 3 still needs a shell on the host. Way 2, the API with a write key, is
not used. No key has that power now, and the owner wants none made.
Step 3. Change the login service
For a client secret, CLIENT is opengov-api or opengov-rights-admin:
ID=$(kc get clients -r opengov -q clientId=$CLIENT --fields id --format csv --noquotes)
{ printf '{"secret":"'; cat new; printf '"}'; } | kc update "clients/$ID" -r opengov -f -
kc get "clients/$ID/client-secret/rotated" -r opengov > /dev/null # must fail: not found
# If it succeeds, the login service keeps the old secret. Delete that copy:
# kc delete "clients/$ID/client-secret/rotated" -r opengov
For the administrator password:
AID=$(kc get users -r master -q username="$ADMIN_USER" -q exact=true --fields id --format csv --noquotes)
{ printf '{"type":"password","temporary":false,"value":"'; cat new; printf '"}'; } \
| kc update "users/$AID/reset-password" -r master -n -f -
cp new admin-now # the next kc call signs in with the new value
Step 4. Prove the result
Run prove new first. It must give the accept answer, which also shows that the login
service answers. Then run prove old. It must give any other answer, not one exact code.
| Setting | Accept answer | Answer to the published default on 2026-10-13 |
|---|---|---|
OPENGOV_API_CLIENT_SECRET | 400, invalid_grant: the login service accepts the client and refuses the user | 401, unauthorized_client |
OPENGOV_KC_ADMIN_CLIENT_SECRET | 200 | not 200 |
KC_BOOTSTRAP_ADMIN_PASSWORD | 200 | not 200 |
A client_credentials request does not prove OPENGOV_API_CLIENT_SECRET. The client
opengov-api has no service account, so the login service refuses the two values alike.
Then start a deploy in the deploy tool. The containers get the new value only at a deploy.
Until then, a new OPENGOV_KC_ADMIN_CLIENT_SECRET breaks the rights screens. After the deploy:
https://<app host>/healthanswers 200 and names no blocking part.docker ps -a -f name=init-keycloak --format '{{.Status}}'showsExited (0).- The log of that job has no
kc_admin_password=published-default. - A staff administrator signs in, and the user list of the rights screen loads.
Resume the deploys (the sandbox only)
Do this after the deploy of step 4 ended:
gh workflow enable post-deploy-smoke.yml --repo UNCTAD-OpenGov/opengov # on your machine
Landings on main deploy again. A landing during the pause did not deploy. The deploy of
step 4 built the newest commit of main, so it holds that landing too.
Step 5. Delete each temporary copy
cd / && rm -rf /root/rotate && unset -f kc prove
docker exec coolify ls /tmp # no rotate-value and no set.php
If a step fails
- Step 2 reads back
no. The old value still works in each place. Repeat step 2. - Step 3 fails. The deploy tool has the new value and the login service the old one. Do
not deploy. Repeat step 3. Or write the old value back to the deploy tool with
to_coolify old 1. An old value can be short: a published default has 5 to 25 characters. The output must berows: 2,normal same: yesandpreview same: yes. - Nobody can sign in as administrator. Keycloak’s recovery command
(
kc.sh bootstrap-admin user) makes a temporary administrator. Set the password from the filenewagain, then delete the temporary account. This worked on 2026-10-12.
Why the start check covers only one of the three
The start check of the app (public_credentials_startup_check) refuses the published default
of OPENGOV_KC_ADMIN_CLIENT_SECRET, which the app reads. It leaves out the two others:
- No process of the app reads them, and plain Compose does not give them to the app.
- The deploy tool gives each setting to each container. A check in the app then stops the site for a value that the app does not use. Issue #4211 began with such a stop.
- The login service reads them at its first start only. A check of the setting tells nothing about the live login service.
The setup job signs in with KC_BOOTSTRAP_ADMIN_PASSWORD. When the published default works,
the job writes a warning and adds kc_admin_password=published-default to its report line.
It does not stop. Nothing depends on it, and its realm step brings hardening to old deployments.
Not verified
- Nobody ran the
kclines on a deployment. A local Keycloak 26.7.3 image haskcadm.sh, and its classes readKC_CLI_PASSWORD. The Keycloak guide describes-f -and--no-config. - The labels of the screen for Way 3. Also a Way 1 recipe that adds a key, not changes one.
proveuses the public path/realms/<realm>. If a later change closes it from the internet, run the proof through the SSH tunnel of the login service.- The secrets generator (
scripts/generate_secrets.py) makes noKC_BOOTSTRAP_ADMIN_PASSWORD. Set a value from step 1 before the first start of a new deployment.