Skip to content

Part 1: Automation signs in as a managed identity

Automation has no person to sign in, so it usually gets a Snowflake service user with a password or key pair, and that secret becomes something else to store and rotate. Snowflake's workload identity federation removes it. The service user names an external identity it trusts, and the workload proves it is that identity with a token from its own cloud. For Azure, that identity is a managed identity, and the token comes from Entra.

This part sets up a Snowflake service user that trusts a user-assigned managed identity, and signs in as it from GitHub Actions. Part 2 uses that sign-in to rotate other users' keys.

How the trust works

sequenceDiagram
  participant J as GitHub Actions job
  participant E as Entra ID
  participant S as Snowflake
  J->>J: request OIDC token (aud api://AzureADTokenExchange)
  J->>E: client assertion = GitHub token, client_id = managed identity
  E->>E: federated credential on the identity matches iss + sub + aud
  E-->>J: token for Snowflake's Entra app, as the managed identity
  J->>S: connect(authenticator="WORKLOAD_IDENTITY", provider AZURE)
  S->>S: issuer = tenant v2.0, subject = identity's object ID
  S-->>J: session as the service user

There are two separate trusts, and each fails in its own way:

  1. Entra trusts GitHub. A federated credential on the managed identity accepts GitHub OIDC tokens with one specific subject. This is the same mechanism azure/login uses, and it needs no Snowflake involvement.
  2. Snowflake trusts the managed identity. The service user's WORKLOAD_IDENTITY names the tenant's v2 issuer and the identity's object ID. The token the connector presents is issued for Snowflake's own Entra application, so that application must exist in your tenant.

Snowflake's application in your tenant

Snowflake publishes a multi-tenant Entra application, "Snowflake Workload Identity", with app ID fd3f753b-eed3-462c-b6a7-a4b5bb650aad. A managed identity can only get a token for an application that has a service principal in its tenant. Without one, the sign-in fails with AADSTS500011: The resource principal named api://fd3f753b-... was not found.

Create the service principal once per tenant:

az ad sp create --id fd3f753b-eed3-462c-b6a7-a4b5bb650aad

The reference repository does this in Bicep, behind a flag. It requests no permissions; it is the tenant's acknowledgement that the application exists. Treat it as tenant-wide shared infrastructure: don't delete it when you tear down one project.

The service user

USE ROLE USERADMIN;
CREATE ROLE IF NOT EXISTS KVROTATE;
GRANT ROLE KVROTATE TO ROLE USERADMIN;

CREATE USER IF NOT EXISTS SVC_KVROTATE
  TYPE = SERVICE
  WORKLOAD_IDENTITY = (
    TYPE = AZURE
    ISSUER = 'https://login.microsoftonline.com/<tenant-id>/v2.0'
    SUBJECT = '<managed identity object (principal) ID>'
  )
  DEFAULT_ROLE = KVROTATE;
GRANT ROLE KVROTATE TO USER SVC_KVROTATE;
  • SUBJECT is the object ID, not the client ID. az identity show returns both. principalId is the one Entra puts in the token's sub. clientId goes in AZURE_CLIENT_ID on the client side. Mixing them up is the most common failure in this whole setup.
  • ISSUER ends in /v2.0. Snowflake's application gets v2 tokens regardless of your own apps' settings.
  • TYPE = SERVICE users can't sign in with a password or SSO, so the user can't be repurposed as a human login.
  • The role rolls up to USERADMIN rather than SYSADMIN, because what it will manage (other users' credentials) is user administration. That also keeps it out of reach of everyone who holds SYSADMIN, which in most accounts is many more people than hold USERADMIN.

Signing in from GitHub Actions

The Snowflake Python connector signs in as a managed identity with authenticator="WORKLOAD_IDENTITY". Its Azure provider normally asks the instance metadata service (IMDS, 169.254.169.254) for the identity's token. A GitHub runner isn't an Azure resource and has no IMDS, so the connector waits and then fails with Azure attestation failed ... deadline has elapsed.

The provider also reads the workload identity convention from AKS: three environment variables and a file holding a federated token, which it exchanges at Entra for the managed identity's token.

Variable Value
AZURE_CLIENT_ID the managed identity's client ID
AZURE_TENANT_ID your tenant ID
AZURE_FEDERATED_TOKEN_FILE path to a file holding a GitHub OIDC token with audience api://AzureADTokenExchange

We verified this with snowflake-connector-python 5.2.0. Snowflake's documentation describes only the IMDS path, so pin the connector version and retest when you upgrade.

Setup, in this order

  1. A GitHub environment limited to main. Create it, with a deployment branch policy allowing only main, before the next step. The federated credential trusts any job that declares the environment, so until the policy exists, any branch could act as the identity.
  2. A federated credential on the managed identity whose subject names that environment:

    SUB_PREFIX=$(gh api "repos/<owner>/<repo>/actions/oidc/customization/sub" -q .sub_claim_prefix)
    
    az identity federated-credential create -g <rg> --identity-name <identity> \
      --name github-snowflake-prod \
      --issuer https://token.actions.githubusercontent.com \
      --subject "${SUB_PREFIX}:environment:snowflake-prod" \
      --audiences api://AzureADTokenExchange
    
  3. Environment variables (AZURE_CLIENT_ID, AZURE_TENANT_ID, and your Snowflake account and user) on the environment. None of them are secrets.

Don't build the subject from the repository name. Repositories that use GitHub's immutable subject format, the default for new ones, send repo:<owner>@<owner-id>/<repo>@<repo-id>:environment:<env>. Older ones send repo:<owner>/<repo>:environment:<env>. A credential written in the old format against a new repository fails with AADSTS700213: No matching federated identity record found, and the error message shows the subject GitHub actually sent. The numeric IDs are the point: a repository renamed, or deleted and recreated under the same name, can't inherit the identity.

The basic version

The reference repository's examples/ folder has the smallest version that works: a workflow (github-actions-minimal.yml) and a script (wif_connect.py). The workflow needs permissions: id-token: write and environment: snowflake-prod, and runs the script. The script does three things:

# 1. Ask GitHub for this job's OIDC token, with Entra's token-exchange audience.
url = os.environ["ACTIONS_ID_TOKEN_REQUEST_URL"] + "&audience=api://AzureADTokenExchange"
request = urllib.request.Request(
    url, headers={"Authorization": f"Bearer {os.environ['ACTIONS_ID_TOKEN_REQUEST_TOKEN']}"})
token = json.load(urllib.request.urlopen(request, timeout=30))["value"]

with tempfile.TemporaryDirectory() as tmp:
    # 2. Save it where the connector looks (mkstemp: a new name, mode 0600).
    fd, path = tempfile.mkstemp(dir=tmp)
    with os.fdopen(fd, "w") as f:
        f.write(token)
    os.environ["AZURE_FEDERATED_TOKEN_FILE"] = path

    # 3. Sign in. The connector exchanges the GitHub token for the identity's token.
    with snowflake.connector.connect(
        account=os.environ["SNOWFLAKE_ACCOUNT"],
        user=os.environ["SNOWFLAKE_USER"],          # e.g. SVC_KVROTATE
        authenticator="WORKLOAD_IDENTITY",
        workload_identity_provider="AZURE",
    ) as conn:
        print(conn.cursor().execute("select current_user(), current_role()").fetchone())

Use the connector, not the Snowflake CLI, for this path

With Snowflake CLI 3.33.0, snow with --authenticator WORKLOAD_IDENTITY failed with a MissingDependencyError on the runner. The Python connector worked. Check again with your CLI version before relying on either.

Hardening

The basic version is enough to learn the flow. Before a job like this does real work, add the following. kvrotate's workflow (.github/workflows/kvrotate.yml) does all of it.

  • Check your GitHub plan. Deployment branch policies on private repositories need GitHub Pro, Team or Enterprise. On a Free private repository, anyone who can push a branch can run a job as the identity.
  • Run it from a private repository. Public run logs would show your tenant, vaults, apps and users. None of those are secrets, but together they map your environment.
  • Fetch a fresh GitHub token before each sign-in. GitHub's OIDC tokens are short-lived, so a job that signs in more than once shouldn't write the file once at the start. kvrotate's refresh_github_federated_token rewrites it atomically before every sign-in.
  • Pin everything. Actions to commit SHAs. The connector and all its dependencies by hash (uv pip compile --generate-hashes, then pip install --require-hashes --no-deps), into a per-job venv.
  • Per-job credential directories. Put the token file in a fresh 0700 directory (mktemp -d, in /dev/shm so it stays in memory), give az its own profile (AZURE_CONFIG_DIR under $RUNNER_TEMP), and delete both in an if: always() step. This matters most on self-hosted runners, which keep their disk between jobs.

Network policies

Workload identity proves who is connecting. A Snowflake network policy checks where the connection comes from, and both must pass. GitHub-hosted runners connect from shared, changing Azure addresses, so an account whose policy allows only known networks rejects them with 390422 ... is not allowed to access Snowflake.

Option What it costs you
A self-hosted runner inside a network the policy already allows You operate the runner. Anyone who controls that machine during a job can act as the managed identity.
A user-level network policy with Snowflake's managed rule SNOWFLAKE.NETWORK_SECURITY.GITHUBACTIONS_GLOBAL, attached to the service user only It allows every GitHub customer's runners, so the network check becomes "any GitHub job". The identity check still applies.
Larger GitHub runners with static IPs Paid, and organization-level.

We verified with the first option. If you choose it:

  • Attach self-hosted runners to private repositories only. On a public repository, a fork's pull request can run code on your machine.
  • Target the runner with a label only it has, not just self-hosted, Linux or X64, so the job can't land on a different machine.
  • Run the runner as a dedicated non-root user, and prefer ephemeral runners (--ephemeral, one job per registration).
  • Use the per-job credential directories from Hardening. Without them az shares ~/.azure with the machine's user: the job would see their login, and its az logout would end it.

The managed-rule option fits the reference repository's default of GitHub-hosted runners, but we haven't run it. In our account the rule existed under that name; check yours with SHOW NETWORK RULES IN SCHEMA SNOWFLAKE.NETWORK_SECURITY. Part 2 covers the one extra setting it needs.

Check it

From the job, with no other privileges in play:

USE SECONDARY ROLES NONE;
SELECT current_user(), current_role(), current_secondary_roles();
-- expect SVC_KVROTATE, KVROTATE, and no secondary roles

USE SECONDARY ROLES NONE matters for anything that changes state: it guarantees the statements run with exactly the role you granted, even if someone later grants this user more.

Next: Part 2, rotating service-user key pairs.