Skip to content

Part 2: Rotating service-user key pairs

Workload identity removes the stored credential wherever the client supports it, and so does External OAuth for people. Some clients support neither. Plenty of ingestion and BI tools, and older drivers, only do key-pair authentication. For those users this part makes the key a rotated, machine-handled secret: a job generates it, registers it in Snowflake, proves it works, and stores it in Key Vault, where the consumer reads it. No person handles the private key.

The job is the GitHub Actions workflow from part 1, signed in to Snowflake as the managed identity's service user. The reference repository's kvrotate implements it; this page explains the design so you can build your own.

Named key pairs, not RSA_PUBLIC_KEY

Snowflake users used to have exactly two key slots, RSA_PUBLIC_KEY and RSA_PUBLIC_KEY_2, and rotation meant alternating between them. Snowflake now recommends named key pairs instead:

ALTER USER SVC_LOADER ADD KEY PAIR KVROTATE_20261111T173637Z
  PUBLIC_KEY = 'MIIBIjANBgkqh...'
  COMMENT = 'kvrotate: kv-example-data/svc-loader-private-key';

SHOW USER KEY PAIRS FOR USER SVC_LOADER;   -- name, fingerprint, status, expires_at, comment, ...

ALTER USER SVC_LOADER REMOVE KEY PAIR KVROTATE_20261111T173558Z;

What changes for a rotation job:

  • Keys have names and comments. The job can tell its own keys from ones a person added. kvrotate names them <PREFIX>_<UTC timestamp>, comments them with the Key Vault secret they belong to, and only ever removes keys that match both. In our account the legacy slots appeared in SHOW as PUBLIC_KEY_1 and PUBLIC_KEY_2; the job never touches them.
  • Several per user. Old and new keys can coexist for as long as consumers need, instead of exactly two. We observed a limit of 10 per user; the Snowflake pages we read don't state it, so check before relying on it.
  • Optional DAYS_TO_EXPIRY. Snowflake can stop accepting a key on its own, as a backstop if the job stops running.
  • SHOW returns the fingerprint (SHA256:<base64> in our account), so the job can confirm Snowflake stored the key it generated.

Snowflake also has ALTER USER ... ROTATE KEY PAIR, which replaces a key and keeps the old one valid for a grace period. We didn't use it, because the old key's lifetime should depend on whether consumers have moved to the new one, not on a fixed timer. Removal is a separate step, described under Pruning below.

One privilege

The job's role needs exactly one grant per managed user:

GRANT MODIFY PROGRAMMATIC AUTHENTICATION METHODS ON USER SVC_LOADER TO ROLE KVROTATE;

We checked what that grant allows, signed in as the service user with USE SECONDARY ROLES NONE:

Statement Result
ADD KEY PAIR, REMOVE KEY PAIR, SHOW USER KEY PAIRS on a granted user allowed
The same on a user without the grant Insufficient privileges to operate on user
ALTER USER <granted user> SET DEFAULT_ROLE = ... denied

The role can manage the user's keys and nothing else. It can't change their roles, grants or defaults, and it doesn't own them. Onboarding a new service user to rotation is one GRANT, which a Snowflake administrator can review on its own.

The basic version: one rotation by hand

Before automating it, do one rotation by hand. Every automated step below is one of these, plus checks.

umask 077 && cd "$(mktemp -d)"     # private, throwaway directory
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out key.p8
openssl pkey -in key.p8 -pubout | sed '1d;$d' | tr -d '\n' > pub.txt   # base64 body only
-- paste the contents of pub.txt
ALTER USER SVC_LOADER ADD KEY PAIR MANUAL_20261111 PUBLIC_KEY = '<pub.txt>';
SHOW USER KEY PAIRS FOR USER SVC_LOADER;
# --file keeps the private key off the command line and out of shell history
az keyvault secret set --vault-name <vault> --name svc-loader-private-key --file key.p8 -o none
rm -rf "$PWD"

Once every consumer has picked up the new Key Vault version, remove the old key with ALTER USER SVC_LOADER REMOVE KEY PAIR <old name>. The rest of this page is about making that safe to run unattended.

The order of operations

Consumers read the latest version of a Key Vault secret. So whatever Key Vault serves must already work in Snowflake, and the key it replaced must keep working until consumers have picked up the new one.

flowchart TD
  A["SHOW USER KEY PAIRS refuse at 10, or on a name clash"] --> B["Generate RSA key pair"]
  B --> C["ADD KEY PAIR name PUBLIC_KEY = ..."]
  C --> D{"SHOW: fingerprint matches?"}
  D -- no --> R["REMOVE KEY PAIR name"]
  D -- yes --> E{"Sign in as the user with the new key?"}
  E -- no --> R
  E -- yes --> F["Store private key in Key Vault tags: user, fingerprint, key name"]
  F -- fails --> R
  F -- ok --> G["Done. The old key stays until prune."]
  1. List first. Refuse if the user already has 10 key pairs. The fix is to prune, not to delete something to make room.
  2. Add, then confirm the fingerprint from SHOW. A key that was mangled on the way in (quoting or encoding problems) fails here, not at a consumer.
  3. Sign in with the new key (authenticator="SNOWFLAKE_JWT") and check current_user(). That proves the key works end to end before anything depends on it. kvrotate tries three times, a few seconds apart, in case Snowflake doesn't accept a newly added key immediately.
  4. Then store it in Key Vault, tagged with the Snowflake user, the public key's fingerprint and the key pair's name. Those tags are how pruning later knows which Snowflake key each Key Vault version holds.
  5. Any failure after the add removes the new key, so a failed run leaves Snowflake as it was. One exception: if the Key Vault write itself reports an error, the write may still have succeeded (a lost response, say). The job checks Key Vault's latest version first, and keeps the key if Key Vault already serves it, or if it can't tell. Removing it then would leave consumers holding a key Snowflake rejects. The run still reports failure.

Write the private key only to a 0600 temporary file that is deleted after use, never to a path in the repository, and never print it. The public key isn't secret; the job can log it.

Pruning

Pruning is a separate command, run by a person or on its own schedule:

  1. Read every enabled Key Vault version of the secret, and of its staged copy if staging is configured. Collect their fingerprint tags. A consumer might be pinned to an older version, so every enabled version counts, not just the latest. Disable a version in Key Vault to release its key.
  2. SHOW USER KEY PAIRS, and select keys the job created for this secret (name pattern and comment) whose fingerprint no enabled version references.
  3. REMOVE KEY PAIR each one.

"Is anything still using this key?" becomes "does Key Vault still serve it?", which the job can answer itself. A dry run prints exactly what would be removed. If the key Key Vault serves isn't active in Snowflake, prune removes nothing and fails, because someone changed Snowflake by hand and removing more keys could remove the last one that works. Run rotate to recover.

In the verified run, two rotations left two key pairs on the user. Prune removed the older one, the only key Key Vault no longer served.

Releasing old keys without a person

The rule above protects every enabled version, so on its own a scheduled rotation never releases anything. Someone has to disable the old version first. Unattended, keys pile up until the user reaches its limit and rotation refuses.

So pruning gets one more step that runs before the rest: disable versions that a newer enabled version replaced at least N days ago (kvrotate's retire_after_days). Two details matter:

  • Count from replacement, not creation. A key that was current for six months was cached by consumers until the day it was replaced. Counting from its creation date would cut those consumers off the moment it rotated. Counting from replacement gives every consumer the same N days to re-read Key Vault.
  • Never the newest enabled version. It has no replacement, so it never qualifies, and Key Vault always has something to serve.

Choose N longer than your slowest consumer's cache or restart cycle. The dry run shows the versions it would disable and the keys that would release, so you can check the first scheduled prune before it runs.

When the job's network can't reach the target

The step that signs in as the target user (step 3 above) connects as that user, so the target's network policy applies too. If you use GITHUBACTIONS_GLOBAL for the rotation's own service user (part 1), either attach the same policy to each target user or skip the sign-in check (verify_login = false in kvrotate). Skipping it keeps the fingerprint check, which catches a mangled key, but not a key Snowflake refuses for another reason. A self-hosted runner inside an allowed network avoids the trade-off.

Hardening the job

This job can mint the managed identity's tokens and write to Key Vault. Treat its supply chain accordingly:

  • Pin actions to commit SHAs, and let Dependabot propose updates.
  • Pin Python dependencies by hash. The workflow installs the Snowflake connector from a lock file generated with uv pip compile --generate-hashes, and installs with pip install --require-hashes --no-deps. Every transitive package is pinned, and a changed artifact fails the install.
  • Only workflow_dispatch and schedule triggers. Never pull_request or pull_request_target. The GitHub environment's branch policy would block them, but don't rely on that alone.
  • Protect main. Anyone who can merge a change to the workflow can use the identity. Require reviews, and add CODEOWNERS for the workflow and the rotation config.

Other secrets go to a second identity

kvrotate also rotates random values (signing keys, webhook secrets) and Entra application client secrets into Key Vault. Random values need only Key Vault, so they run in the same job as the Snowflake keys. Entra client secrets need the Microsoft Graph application permission Application.ReadWrite.OwnedBy, with the identity made an owner of each application it rotates.

Give that work its own managed identity, GitHub environment and config file. The reference workflow calls these lanes: snowflake and entra. The Snowflake lane's identity then holds no Graph permission at all.

One identity for everything is possible, and very sensitive

kvrotate will run every secret type as one identity if you put them in one config file. That identity would hold:

  • OwnedBy. Its writes are limited to applications it owns, but it can read every application and service principal in the tenant, and create new applications.
  • MODIFY PROGRAMMATIC AUTHENTICATION METHODS on every rotated Snowflake user. It can add its own key to each one and sign in as that user.
  • Write access to every vault holding either kind of secret.

Whoever gets that identity, through a merged workflow change, a compromised runner or a stray federated credential, gets all of it. The Graph grant also needs a Privileged Role Administrator or Global Administrator, and that approval would hold up Snowflake rotation too. Splitting costs a second identity and environment, and runs the same code.

If both lanes write to one vault, assign Key Vault Secrets Officer on each secret instead of on the vault, so neither identity can overwrite the other's secrets. Separate vaults are simpler.

We verified the Snowflake lane on 2026-11-17 as an identity with no Graph app role, whose only Azure access was Secrets Officer on two individual secrets. It signed in to Snowflake, rotated a random value and a registered key pair twice, and pruned the old key. The secret must exist before you can assign a role on it, so we created each with a placeholder and disabled that version before the first rotation.

We verified the Entra lane the same day as a second identity that owned a throwaway application. Owning the application wasn't enough on its own: before OwnedBy was granted, even listing that application's credentials failed with "Insufficient privileges". After the grant, the identity created two client secrets, stored each in Key Vault, and deleted the older one once its Key Vault version was disabled.

Next: Troubleshooting.