Skip to main content

Secret managers

Config files reference secrets with os.environ/NAME. By default that reads the process environment, but a secret manager can be put in front of it so that the same reference resolves to a value fetched from your organisation's secret store. The gateway can also write virtual keys into the secret store, so applications read their gateway key from the same place as their other secrets.

A real-world scenario​

A team on AWS wants provider keys in AWS Secrets Manager and wants each new virtual key written there too, so applications fetch their gateway key with the same tooling they use for database passwords. They set key_management_system: aws_secret_manager with store_virtual_keys, and every key created afterwards appears in Secrets Manager under spanway/<alias>.

Steps​

  1. Add the general_settings block from Configuration with access_mode: read_and_write and store_virtual_keys: true, and restart the gateway with an AWS identity that may read and write those secrets.

  2. Create a key with an alias, in the Admin UI under Virtual Keys with + Create New Key, or with the API.

    curl -X POST http://localhost:4000/key/generate \
    -H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \
    -d '{"key_alias": "billing-service"}'
  3. Read it back from the store.

    aws secretsmanager get-secret-value --secret-id spanway/billing-service

Expected result​

The secret spanway/billing-service holds the new key, and models whose api_key names a hosted key keep working with the value from Secrets Manager.

Supported secret managers​

key_management_systemStoreLicense
aws_secret_managerAWS Secrets ManagerStarter
aws_kmsAWS KMS, for decrypting encrypted valuesStarter, and Enterprise for USE_AWS_KMS
google_secret_managerGoogle Secret ManagerEnterprise
google_kmsGoogle Cloud KMSStarter
azure_key_vaultAzure Key VaultStarter
hashicorp_vaultHashiCorp Vault, see HashiCorp VaultEnterprise
cyberarkCyberArk Conjur, see CyberArkEnterprise
customA Python class you provideStarter
localThe process environment (default)Starter

Google Secret Manager, HashiCorp Vault and CyberArk Conjur need an Enterprise license. Without one the gateway refuses to load them at startup and names the license in the error. AWS Secrets Manager, AWS KMS as a key_management_system, Google Cloud KMS and Azure Key Vault work on Starter, while decrypting the environment with USE_AWS_KMS needs Enterprise, and so does writing virtual keys to AWS Secrets Manager. See Enterprise for how to add a license.

Configuration​

general_settings:
key_management_system: aws_secret_manager
key_management_settings:
hosted_keys: ["OPENAI_API_KEY", "ANTHROPIC_API_KEY"]
access_mode: read_only

model_list:
- model_name: claude-sonnet-5-5
spanway_params:
model: anthropic/claude-sonnet-5-5
api_key: os.environ/ANTHROPIC_API_KEY
key_management_settings keyDefaultMeaning
hosted_keysunset, every os.environ/ nameNames that are looked up in the secret manager. Other names fall back to the environment.
access_moderead_onlyread_only, write_only or read_and_write. Reads need read_only or read_and_write. Writing, rotating and deleting virtual keys need write_only or read_and_write, so the default never writes to the store.
store_virtual_keysfalseWrite newly created virtual keys to the secret manager. Needs a write access mode.
prefix_for_stored_virtual_keysspanway/Prefix for stored keys.
primary_secret_nameunsetRead all secrets from one secret that holds a JSON object.
description, tags, kms_key_idunsetMetadata and encryption key for secrets the gateway writes.
custom_secret_managerunsetThe class to load when key_management_system is custom, for example my_secret_manager.InMemorySecretManager.
aws_region_nameunsetAWS region for Secrets Manager calls.
aws_role_name, aws_session_name, aws_external_idunsetAn IAM role to assume for Secrets Manager, with an optional session name and the external id that cross-account roles ask for.
aws_profile_name, aws_web_identity_token, aws_sts_endpointunsetA named AWS profile, a web identity token for OIDC or IRSA, and a custom STS endpoint such as a VPC endpoint.
replica_regionsunsetExtra AWS regions each new secret is copied to. A failed copy does not fail key creation. AWS Secrets Manager only.

The same table with types is in the settings reference.

Per-team storage settings​

A team can store its virtual keys in a different place of the same store. Set secret_manager_settings in the team's metadata with any of namespace, mount, path_prefix and data. HashiCorp Vault honours all four, so a team's keys can live in its own namespace or mount. Other stores ignore them. Setting secret_manager_settings needs a license with the scoped_integrations feature, see Enterprise.

curl -X POST http://localhost:4000/team/update \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \
-d '{"team_id": "<team_id>", "metadata": {"secret_manager_settings": {"namespace": "billing", "mount": "kv-billing"}}}'

Encrypted environment values with AWS KMS​

Set USE_AWS_KMS=True to decrypt encrypted environment values at startup. Every variable whose name starts with SPANWAY_SECRET_AWS_KMS_, or whose value starts with aws_kms/, is decrypted with AWS KMS and stored without the prefix, so SPANWAY_SECRET_AWS_KMS_OPENAI_API_KEY becomes OPENAI_API_KEY. It needs AWS_REGION_NAME and an Enterprise license, and the gateway does not start without them.

Each store authenticates with its own environment variables or cloud identity, for example the AWS credential chain for Secrets Manager and GOOGLE_APPLICATION_CREDENTIALS for Google.

How lookups work​

When the config is loaded, every os.environ/NAME value is resolved through the configured manager. Lookups are cached, and HashiCorp Vault keeps a value for one day by default (see HashiCorp Vault), so a changed secret is picked up after the cache expires or a restart, and hosted_keys limits which names go to the store so unrelated variables stay local. Credentials added through the Admin UI are stored encrypted in the database rather than in the secret manager, see configuration.

Storing virtual keys​

With store_virtual_keys: true and a write access mode, /key/generate writes each new key to the store under <prefix><key_alias>. Applications then fetch their gateway key with the same tooling they use for database passwords, and rotating a key updates the stored value.

Custom secret managers​

Set key_management_system: custom and point custom_secret_manager at a class that implements the base secret manager interface, with async_read_secret and async_write_secret methods, to integrate any other store.

Errors​

What you seeWhat it meansFix
A provider answers 401, or a model has no key, and the log shows Defaulting to os.environ value for key=The lookup in the store failed, so the gateway used the environment variable of the same name, which may be empty. The gateway keeps running.Create the secret and grant read access, then restart.
New keys are not written to the storeaccess_mode is read_only (the default), or store_virtual_keys is off.Set access_mode: read_and_write and store_virtual_keys: true.
The gateway fails to start with Google Secret Manager requires an Enterprise License, Hashicorp secret manager is only available for premium users or CyberArk secret manager is only available for premium usersThe store needs an Enterprise license and none is set.Set SPANWAY_LICENSE to your license key and restart, or use a store that works on Starter.
The gateway fails to start with AWSKeyManagementService V2 is an Enterprise Feature. Please add a valid SPANWAY_LICENSE to your environment.USE_AWS_KMS=True is set without a license.Add SPANWAY_LICENSE, or remove USE_AWS_KMS.
The gateway fails to start with Missing required environment variable - AWS_REGION_NAMEAWS KMS or USE_AWS_KMS is used without a region.Set AWS_REGION_NAME.
The gateway fails to start with Missing required environment variable - GOOGLE_KMS_RESOURCE_NAMEgoogle_kms is used without the key name.Set GOOGLE_KMS_RESOURCE_NAME to the full KMS key resource name.
A provider answers 401 after you changed a secretThe gateway still holds the old value in its cache.Restart, or wait for the cache to expire (one day by default for HashiCorp Vault).

Logs​

LevelLineMeaningAction
ERRORDefaulting to os.environ value for key=<NAME>. An exception occurred - ...A lookup failed and the environment value was used instead. The secret value is never logged.Fix the secret or the identity's access.
WARNINGCyberArk SSL verification is disabled...Conjur certificates are not checked.Normal only in a lab. Turn CYBERARK_SSL_VERIFY back on.
WARNINGCyberArk Conjur does not support direct secret deletion...A deleted virtual key stays in Conjur.Remove the variable with a Conjur policy if needed.