Rotating the master key
The master key is the gateway's admin credential. When SPANWAY_SALT_KEY is not set, it is also the key that encrypts secrets stored in the database, such as provider credentials added through the Admin UI. That is why rotating it takes a little care. Virtual keys are not affected by a master key rotation, because they are stored as hashes and do not depend on the master key.
When you would use this
The master key was pasted into a shared chat by mistake. You generate a new one, set the old one as the migration key for one restart so stored provider credentials are re-encrypted, confirm the log line that says the migration finished, and remove the old key. Applications keep working throughout because their virtual keys never changed, and the Admin UI accepts the new master key at the next sign-in.
Before you start
Check whether SPANWAY_SALT_KEY is set on your gateway. If it is set, stored secrets are encrypted with the salt key and you can change the master key freely. Replace the value of SPANWAY_MASTER_KEY wherever it is defined and restart every instance. If it is not set, stored secrets are encrypted with the current master key, and changing the master key alone would leave the gateway unable to decrypt them. Use the steps below. The master key has no control in the Admin UI, it lives in the environment or the config file.
Step by step
-
Generate a new master key.
openssl rand -hex 32 -
Set the new value in
SPANWAY_MASTER_KEYand the old value inSPANWAY_MIGRATE_FROM_MASTER_KEY, in the shell export, the container or deployment environment, or.env. Keepgeneral_settings.master_keypointed at the environment.general_settings:master_key: os.environ/SPANWAY_MASTER_KEY -
Restart the gateway. At startup it finds every value encrypted with the old key and re-encrypts it. The migration covers model credentials, stored credentials, config values (including stored environment variables), guardrail settings, gateway and organization single sign-on settings (including organization client secrets), cache settings, config overrides, MCP server credentials, headers and environment values, MCP OAuth clients, MCP per-user credentials and environment values, SSO identity assertions, search tool settings, eval remote scorer signing secrets, workflow trigger signing secrets and sealed trigger inputs, Admin UI two-factor secrets, and the encrypted parts of team, key and user metadata, the deleted team and deleted key archives included.
-
Read the log. Look for the two lines below, then remove
SPANWAY_MIGRATE_FROM_MASTER_KEYand restart once more.Re-encrypting N stored value(s) from the SPANWAY_MIGRATE_FROM_MASTER_KEY key to the new master key.Done re-encrypting N stored value(s) with the new master key. You may now delete the SPANWAY_MIGRATE_FROM_MASTER_KEY environment variable. -
Confirm with the new key. Sign in to the Admin UI with the new master key, open Models + Endpoints and check that a model with stored credentials still answers in the Playground, or call a management route and a model with curl.
curl http://localhost:4000/key/list -H "Authorization: Bearer $SPANWAY_API_KEY" -
Set a salt key so the next rotation needs no migration. Generate a
SPANWAY_SALT_KEY, set it, and follow the same migration once so that stored secrets are re-encrypted under it, as step 5 of the production checklist describes.
What you should see
The first log line counts the values the migration found and the second says it finished. Requests with virtual keys never stop. The old master key answers 401 on management routes after the restart, and the new one answers 200. If some values changed while the migration was running, the log says how many remain and asks you to keep SPANWAY_MIGRATE_FROM_MASTER_KEY set and restart again. If the salt key is set, or no database is connected, the gateway logs that there is nothing to migrate. A finished migration writes one audit row, rotated on Spanway_Config with object_id master_key, changed_by master_key_migration and the number of values re-encrypted, never a key.
Rotate it over the API
A proxy admin can also rotate the master key without a restart migration, by regenerating the master key itself. Use it when you cannot change the environment and restart twice, for example in an emergency on a single instance.
-
Call
POST /key/regeneratewith the current master key askeyand the new value asnew_master_key.curl -X POST http://localhost:4000/key/regenerate \-H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \-d "{\"key\": \"$SPANWAY_MASTER_KEY\", \"new_master_key\": \"sk-$(openssl rand -hex 32)\"}" -
Expected result. The answer carries the new master key in
key. The gateway re-encrypted the stored secrets with it, unlessSPANWAY_SALT_KEYis set, in which case the stored secrets stay encrypted with the salt key and nothing is re-encrypted. An audit rowrotatedonSpanway_Configwithobject_idmaster_keyrecords who did it and whether values were re-encrypted. -
Put the new value in
SPANWAY_MASTER_KEYwherever it is defined before the next restart, and restart every instance. Until you do, a restart brings back the old master key while the stored secrets are encrypted with the new one.
Without new_master_key the call answers 400 New master key is required..
Rotating virtual keys
To rotate an application key rather than the master key, call POST /key/regenerate (or POST /key/{key}/regenerate), or click Regenerate Key on the key's detail view. The key keeps its settings, budget and spend history and gets a new value. Keys can also rotate on a schedule with auto_rotate and rotation_interval. See virtual keys.
Proxy refuses to start
The gateway checks its master key before serving traffic and exits with a message that starts with Spanway proxy refused to start: in three cases.
| Message | Cause |
|---|---|
no master key is set, so every request would be accepted without authentication. | Neither SPANWAY_MASTER_KEY nor general_settings.master_key is set. |
the master key is empty. | The value is an empty or blank string. |
the master key is a publicly known default. | The value is a key published in older examples and known to everyone. |
The message is printed when the process exits and lists numbered steps for your setup. If the key comes from the config file, the first step is to read it from the environment instead. Then generate a key and put it in place of the current value wherever it is set.
echo "SPANWAY_MASTER_KEY=sk-$(openssl rand -hex 32)" | tee -a .env
A value already exported in the environment wins over .env, so replace the export rather than only adding a line to the file. If the old weak key encrypted secrets in your database, the message also tells you to set SPANWAY_MIGRATE_FROM_MASTER_KEY to the old key so that the next start re-encrypts them, as described above.
For a throwaway local setup only, SPANWAY_DANGEROUSLY_PERMIT_WEAK_OR_UNSET_MASTER_KEY=true or general_settings.dangerously_permit_weak_or_unset_master_key: true lets the gateway start anyway, with a warning in the log. With no master key set, the gateway then accepts every request without authentication and treats the caller as an internal user. Without the override, a gateway that somehow runs with no master key refuses every request with a 401. Never use the override on a gateway other people can reach.
Errors
| Error | Cause | Fix |
|---|---|---|
Spanway proxy refused to start: ... | The master key is unset, empty or a known default. | Follow the table above and set a generated key. |
Could not migrate stored values from the SPANWAY_MIGRATE_FROM_MASTER_KEY key (...). Values still encrypted with the previous key cannot be read until the migration succeeds... | The startup migration failed, usually because the database was unreachable. | Keep SPANWAY_MIGRATE_FROM_MASTER_KEY set and restart once the database is reachable. |
SPANWAY_MIGRATE_FROM_MASTER_KEY is still set, but nothing in the database is left to migrate from that key... | The migration already finished on an earlier start, or another instance did it. | Remove SPANWAY_MIGRATE_FROM_MASTER_KEY. |
400 New master key is required. | POST /key/regenerate named the master key without new_master_key. | Send new_master_key. |
| Provider calls fail with a decryption error after the rotation | The master key changed without SPANWAY_MIGRATE_FROM_MASTER_KEY, so stored credentials cannot be read. | Set the old key in SPANWAY_MIGRATE_FROM_MASTER_KEY and restart, then remove it. |
| Log says values remain after the migration | Rows were written while the migration ran. | Keep SPANWAY_MIGRATE_FROM_MASTER_KEY set and restart once more. |
| Log says there is nothing to migrate | SPANWAY_SALT_KEY is set, or no database is connected. | Nothing to do when the salt key is set. Check DATABASE_URL otherwise. |
401 on management routes with the new key | An instance still runs with the old key, or the export was not replaced. | Restart every instance and check echo $SPANWAY_MASTER_KEY on each. |
| Admin UI sign-in with the master key fails | The browser kept a session minted under the old key. | Sign out and sign in again with the new key. |
Logs
Every line below is a WARNING, so it shows at the default log level.
| Line | Meaning | Action |
|---|---|---|
Re-encrypting N stored value(s) from the SPANWAY_MIGRATE_FROM_MASTER_KEY key to the new master key. | The startup migration found values to move. | Wait for the next line. |
Done re-encrypting N stored value(s) with the new master key... | The migration finished. | Remove SPANWAY_MIGRATE_FROM_MASTER_KEY and restart. |
Re-encrypted N stored value(s), but M are still encrypted with the previous key... | Some rows changed while it ran. | Keep the variable set and restart again. |
SPANWAY_MIGRATE_FROM_MASTER_KEY is set, but SPANWAY_SALT_KEY is what encrypts your stored values... | The salt key encrypts stored values, so nothing needed moving. | Remove SPANWAY_MIGRATE_FROM_MASTER_KEY. |
SPANWAY_MIGRATE_FROM_MASTER_KEY is still set, but nothing in the database is left to migrate... | Nothing is encrypted with the old key any more. | Remove the variable. |
Could not migrate stored values from the SPANWAY_MIGRATE_FROM_MASTER_KEY key (...)... | The migration failed. | Fix the database connection and restart with the variable still set. |
The log reference lists them too.
Related
- Production checklist for
SPANWAY_SALT_KEYand the other startup settings. - Virtual keys for rotating application keys.
- Leaked keys and key expiry for keys reported as leaked.
- Security settings for the rest of the gateway's hardening switches.
- Settings reference for every
general_settingskey.