Skip to content
Session Secret Rotation

Session Secret Rotation

SESSION_SECRET is the root secret from which the gateway derives, with HKDF-SHA256, the HMAC and AES-256 keys for session cookies and the separate keys for bridge tickets (see Authentication and sessions). Anyone holding it can mint a valid session for any user on any of the gateway’s hosts.

Rotate it on a fixed schedule (at least yearly) and immediately when you suspect it has been exposed.


What rotation does

After the gateway restarts with a new SESSION_SECRET, every existing session cookie and every outstanding bridge ticket becomes undecodable:

  • The gateway logs session decode error for each request that carries an old cookie and treats the request as unauthenticated.
  • On the dashboard, page loads show the login page and API calls return 401; the dashboard then sends the user through /auth/login.
  • On the dev and test hosts, requests are redirected to the dashboard’s /auth/login with the original URL, and the user returns to it through the bridge after signing in.
  • /auth/logout with an old cookie returns 500 and increments gateway_session_store_errors_total{op="clear"}. Signing in again replaces the cookie.

No data is lost. Workspaces, PVCs and Workspace resources are unaffected, and users keep their identity across the new login, because the owner identity is derived from the IdP’s stable sub claim. If the IdP’s single sign-on session is still active, the new login completes without a password prompt.

Break-glass sessions use the same cookie and secret, and end too. A break-glass administrator signs back in with the break-glass button (an audited login); the sessions on the dev and test hosts are restored when they open the IDE or the test application again.

Do not rotate while the identity provider is down unless you mean to: signed-in users cannot sign back in until it returns. Only administrators in the break-glass mapping can, with the client-certificate gate on.

Rotation is also the only way to end every gateway session at once, for example to apply an urgent group or admin change immediately (see Group propagation lag).


Procedure

1. Generate a new secret

NEW_SECRET=$(openssl rand -hex 32)   # 64 hexadecimal characters
test -n "$NEW_SECRET" && echo "secret generated"

The gateway requires at least 32 bytes and refuses to start with a shorter value. Do not print the secret into shared terminals or logs.

2. Update the gateway Secret

The gateway reads SESSION_SECRET from the key of the same name in the Secret named by the chart’s gateway.oidc.existingSecret value (default kornercode-gateway-secrets), injected as an environment variable (see config/helm/templates/gateway/deployment.yaml).

Option A: kubectl, for single-cluster or local installations:

kubectl -n <gateway-namespace> patch secret kornercode-gateway-secrets \
  --type=merge \
  -p "{\"stringData\":{\"SESSION_SECRET\":\"$NEW_SECRET\"}}"

Option B: GitOps, for managed installations: update the Secret through your secret management mechanism (Sealed Secrets, External Secrets, Vault or similar), commit, and let your sync tool apply it.

3. Restart the gateway

Environment variables are read at startup, so the new value takes effect only when the gateway pods restart:

kubectl -n <gateway-namespace> rollout restart deployment/kornercode-gateway
kubectl -n <gateway-namespace> rollout status deployment/kornercode-gateway --timeout=120s

With several replicas, old and new pods serve requests side by side during the rollout. A user whose requests alternate between them may be asked to sign in more than once. Once the rollout completes, all pods use the new secret.

4. Verify

# Expect "session decode error" warnings while active users sign in again;
# they stop once their browsers hold new cookies.
kubectl -n <gateway-namespace> logs deployment/kornercode-gateway --tail=200 | grep "session decode error"

# Sign in to the dashboard yourself and open an IDE to exercise the
# login, callback and bridge paths.

# The session error counter should not keep increasing. The gateway image
# has no shell, so read the metrics through a port-forward.
kubectl -n <gateway-namespace> port-forward deployment/kornercode-gateway 9090:9090 &
curl -s http://localhost:9090/metrics | grep gateway_session_store_errors_total

gateway_session_store_errors_total{op="clear"} may rise while users with old cookies click Logout; op="save" should not.

5. Clean up

  • If the new secret passed through an insecure channel during the rotation (chat message, ticket comment, shared terminal), assume it is exposed and rotate again.
  • Record the rotation date wherever you track credential rotations.

Emergency rotation

When a compromise is in progress, invalidate all sessions as fast as possible instead of rolling the pods one by one:

kubectl -n <gateway-namespace> patch secret kornercode-gateway-secrets \
  --type=merge -p "{\"stringData\":{\"SESSION_SECRET\":\"$NEW_SECRET\"}}"
kubectl -n <gateway-namespace> delete pod -l app=kornercode-gateway --grace-period=0 --force

This drops in-flight requests and makes the gateway unavailable until the new pods are ready (typically under a minute), but no pod keeps accepting sessions signed with the old secret.


Limitations

The gateway accepts a single secret. There is no grace period during which cookies signed with the previous secret remain valid, so every rotation signs all users out. gorilla/sessions supports multiple key pairs for this purpose, but the gateway does not expose that option.


Related controls

  • The gateway refuses to start if SESSION_SECRET is shorter than 32 bytes.
  • Logout reports a failure when the session cannot be cleared instead of silently succeeding, which is why /auth/logout returns 500 for old cookies after a rotation.
  • Active sessions are extended once they are older than a quarter of SESSION_MAX_AGE_SECONDS, so users are not signed out in the middle of a working day. Rotation is unaffected by this: it invalidates extended sessions as well.