Skip to main content

Credential encryption migration

The gateway encrypts every secret it stores in the database, such as provider API keys, SSO client secrets and callback tokens. Older gateways wrote those values in a legacy format. The encryption migration re-encrypts them in place as AES-256-GCM, and a read-only check tells you how many legacy values are left, so you can prove to an auditor that none remain.

What this is for​

Changing the encryption format is not something you want to do by hand, one table at a time. The migration walks every storage location for you, decrypts each legacy value and writes it back in the new format. It is safe to run more than once, because values that are already migrated are skipped, and it never destroys a value it cannot decrypt. The check gives you one number, residual_legacy, which is 0 when the job is done.

Scenario​

A company has run the gateway for a year and is preparing for a security review. The reviewer asks for evidence that all stored credentials are encrypted with AES-256-GCM. The platform admin turns on the new format, runs a dry run to see what will change, runs the migration, and files the check report showing residual_legacy: 0.

Before you start​

  1. Set a salt key, if you have not already, so the encryption key no longer depends on the master key. Step 5 of the production checklist explains SPANWAY_SALT_KEY.

  2. Turn on the new format in the config file and restart the gateway. From then on every new secret is written as AES-256-GCM.

    general_settings:
    encryption_algorithm: aes-256-gcm

The migration needs a database connection and a proxy admin key. The Credential Encryption tab on the Admin Settings page appears only for a proxy admin who is not view-only, and the API refuses every other caller with a 403.

Steps​

  1. See how much is left to do. In the Admin UI open Settings, then Admin Settings, pick the Credential Encryption tab and click Check Residual Legacy. From the command line:

    curl http://localhost:4000/credentials/migrate-encryption/check \
    -H "Authorization: Bearer $SPANWAY_API_KEY"
  2. Rehearse the migration. Click Dry Run Migration. Nothing is written, and the report shows what the real run would touch. From the command line add dry_run=true:

    curl -X POST "http://localhost:4000/credentials/migrate-encryption?dry_run=true" \
    -H "Authorization: Bearer $SPANWAY_API_KEY"
  3. Run the migration. Click Migrate to AES-256-GCM, read the confirmation, which says that every stored credential still in the legacy format is decrypted and written back as AES-256-GCM and that values that cannot be decrypted are left as they are, and click Migrate. From the command line:

    curl -X POST http://localhost:4000/credentials/migrate-encryption \
    -H "Authorization: Bearer $SPANWAY_API_KEY"
  4. Record the result. Click Check Residual Legacy again, or repeat step 1, and keep the response with your compliance evidence.

The same three actions are available from the CLI as spanwayctl encryption migrate, with --check and --dry-run.

Expected result​

Each action answers with a report. The Admin UI shows it as a banner, No legacy ciphertext remains or N legacy values still to migrate, above a table with one row per storage location and the columns Scanned, AES-256-GCM, Migrated, Legacy, Plaintext and Undecryptable. The API returns the same numbers:

{
"status": "success",
"dry_run": false,
"report": {
"residual_legacy": 0,
"total_undecryptable": 0,
"locations": {
"credentials": {"scanned": 12, "migrated": 12, "already_v2": 0, "plaintext": 0, "undecryptable": 0, "legacy": 0},
"sso_config": {"scanned": 1, "migrated": 1, "already_v2": 0, "plaintext": 0, "undecryptable": 0, "legacy": 0}
}
}
}
FieldMeaning
scannedValues looked at in that location.
migratedValues rewritten as AES-256-GCM by this run.
already_v2Values that were already AES-256-GCM and were skipped.
legacyValues still in the legacy format. On a check or a dry run this is what a real run would migrate.
plaintextValues stored without encryption, which have nothing to migrate.
undecryptableValues that could not be decrypted with the current key and were left untouched.
residual_legacyThe sum of legacy across every location. 0 is the attestation that nothing is left.

The report has one entry per storage location:

LocationWhat it holds
model_tableProvider keys and other secrets of deployments added in the Admin UI or over the API.
credentialsStored provider credentials.
mcp_serverMCP server credentials, static headers and environment variables.
mcp_user_credentialsEach user's own MCP server credentials and OAuth tokens.
mcp_user_env_varsEach user's own MCP environment variables.
search_toolsSearch tool API keys.
user_provider_credentialsEach user's personal provider connection, such as a GitHub Copilot sign-in.
sso_identity_assertionsSign-in assertions kept for MCP servers that act on a user's behalf.
mfa_secretsTwo-factor authentication secrets.
agentsRegistered agents' provider keys, the header values sent to them and their kill switch webhook secrets.
config_environment_variablesEnvironment variables saved in the database config.
team.callback_vars, verification_token.callback_varsLogging callback secrets set on teams and on virtual keys.
vantage_settings, cloudzero_settingsCost export API keys and tokens.
sso_configThe SSO client secret.

If the gateway cannot read one of these locations, for example because the database dropped the connection, the check and the migration stop with a 503 instead of reporting that location as empty, so residual_legacy: 0 always means every location was read. A real run writes an audit row on Spanway_Config with the object credential_encryption_migration, who ran it and the counts, which you can file next to the check report. A master key rotation re-encrypts every one of these locations too.

Errors​

What you seeWhyFix
400 Encryption migration requires general_settings.encryption_algorithm: 'aes-256-gcm'. Current value: None.The new format is not turned on, so a migration would rewrite values in the legacy format again.Add encryption_algorithm: aes-256-gcm to general_settings and restart.
400 Cannot migrate covered tables: no salt key / master key is set. Set SPANWAY_SALT_KEY before migrating.The gateway has no key to encrypt with.Set SPANWAY_SALT_KEY (or a master key) and restart.
403 Admin-only endpoint. Not allowed to access this.The key is not a proxy admin key.Use a proxy admin key, or ask an admin to run it.
500 DB not connected. This endpoint needs a databaseThe gateway has no database, so there is nothing to migrate.Set DATABASE_URL.
503 Could not read <location>: <reason>. The scan stopped, so this run attests nothing. ...A storage location could not be read.Fix the database connection and run it again.
undecryptable values in the reportThose values were encrypted with a different master key or salt key than the gateway runs with now.Restore the key they were written with, or re-enter those secrets through the Admin UI, then run the migration again.
residual_legacy stays above 0 after a runNew legacy values were written while the migration ran, or the run stopped early.Run the migration again. It skips everything already migrated.

Logs​

LevelMessageMeaningAction
WARNINGscan: <location> unavailable: <reason>A location could not be read, and the call answers 503.Fix the database connection and run again.
WARNINGSkipping <table> row <id> callback_vars (transform failed): <reason>One team or key's callback secrets could not be rewritten and were left as they were.Re-enter that team's or key's callback settings.
ERRORFailed to re-encrypt credential <name>: <reason>One stored credential could not be rewritten.Re-enter the credential on Models + Endpoints, LLM Credentials.
WARNINGrotate_user_provider_credentials_master_key: could not decrypt the connection of user_id=<id> for <name>, skippingA personal connection was written with another key.Ask that user to connect again.
INFOrotate_<store>_master_key: rotated <n> row(s), skipped <n>One store finished.Normal.