Skip to content
Application identity

Configure Fly.io Workload Identity

Authenticate Fly Machines to Keycard with the Machine's OIDC token.

A Fly Machine gets a short-lived OIDC token from the Machines API, with your Zone as its audience. It sends the token to Keycard along with the Client ID of the Application credential it authenticates as. Keycard checks the token’s signature with your organization’s public keys, then checks its audience, subject, and claims.

  • Familiarity with the concepts in Workload Identity, and with Fly.io’s OpenID Connect docs
  • A Keycard Zone and an Application for the app
  • Your Zone issuer URL, from Settings → Connection in Keycard Console
  • The Keycard CLI, signed in with keycard auth signin --org <org-id>
  • A Fly.io app, and your organization slug, as fly launch prints it: Created app '<app-name>' in organization '<org-slug>'. It’s also in your dashboard URL, fly.io/dashboard/<org-slug>. For a personal organization, fly orgs list shows personal, which never appears in tokens.

Each Fly.io organization has its own issuer. Create one Provider per organization:

Terminal window
keycard agent api /zones/<zone-id>/providers --org <org-id> -X POST -d '{
"name": "Fly.io <org-slug>",
"identifier": "https://oidc.fly.io/<org-slug>",
"protocols": {
"oauth2": {
"issuer": "https://oidc.fly.io/<org-slug>"
}
}
}'

Keycard discovers the organization’s signing keys from the issuer.

  1. Add the credential to the Application

    Terminal window
    keycard agent api /zones/<zone-id>/application-credentials --org <org-id> -X POST -d '{
    "type": "token",
    "version": 2,
    "application_id": "<application-id>",
    "provider_id": "<fly-provider-id>",
    "subject": "<org-slug>:<app-name>:*"
    }'
    Field Value
    subject The app’s Machines. The last segment is the Machine’s name, which differs per Machine, so end with * to trust every Machine in the app. To trust one named Machine, use <org-slug>:<app-name>:<machine-name>.
    allowed_audience Omitted, so it defaults to your Zone issuer URL. The Machine requests its token with that audience.
    authentication_condition Optional. The issuer already limits tokens to your organization.

    Fly.io tokens also carry app_id, machine_id, image, image_digest, and region. For an example token, see Fly.io’s OpenID Connect docs.

  2. Copy the credential’s Client ID

    The response’s identifier is the credential’s Client ID. The app sends it as client_id. It is not a secret.

  1. Set environment variables

    None of these values are secrets, so set them in fly.toml:

    [env]
    KEYCARD_ISSUER = "<keycard-issuer-url>"
    KEYCARD_CLIENT_ID = "<client-id>"
    KEYCARD_RESOURCE_URL = "<resource-url>"

    fly deploy applies fly.toml. Machines you created with fly machine run aren’t part of a Fly Launch deploy, so neither fly deploy nor a plain fly secrets set reaches them. Stage the values on the app, then update each Machine:

    Terminal window
    fly secrets set --stage \
    KEYCARD_ISSUER="<keycard-issuer-url>" \
    KEYCARD_CLIENT_ID="<client-id>" \
    KEYCARD_RESOURCE_URL="<resource-url>"
    fly machine update <machine-id> --yes

    fly machine list shows each Machine’s ID. For a scheduled Machine, add --skip-start so the update doesn’t start an extra run; the Machine picks up the values at its next scheduled start.

  2. Request a Fly.io token and exchange it

    Request a token from the Machines API at /.fly/api with your Zone issuer URL as the audience. Then send it as client_assertion, with the credential’s Client ID as client_id, in a client_credentials grant:

    Terminal window
    pip install keycardai-oauth
    import asyncio
    import os
    from keycardai.oauth import AsyncClient, GrantType
    from keycardai.oauth.server import FlyTokenSource
    async def get_access_token() -> str:
    issuer = os.environ["KEYCARD_ISSUER"]
    fly_token = await FlyTokenSource(audience=issuer).identity_token()
    async with AsyncClient(issuer) as client:
    response = await client.client_credentials_grant(
    client_assertion=fly_token,
    client_assertion_type=GrantType.JWT_BEARER_CLIENT_ASSERTION,
    client_id=os.environ["KEYCARD_CLIENT_ID"],
    resource=os.environ["KEYCARD_RESOURCE_URL"],
    )
    return response.access_token
    access_token = asyncio.run(get_access_token())

    Request a new token for each exchange. Fly.io tokens expire after 10 minutes.

Deploy with fly deploy, or update a fly machine run Machine as in Configure the app, and run the app. The exchange returns an access token, and a credentials:issue event shows on the Application’s Activity tab in Keycard Console.

Keycard returns invalid_client with Invalid client assertion when the issuer, audience, subject, or condition doesn’t match. The Audit Log records details on the failure. Request a token on a Machine and decode it to compare its claims against the Provider and credential. The image needs curl:

Terminal window
fly ssh console --machine <machine-id> -C "curl -sSf --unix-socket /.fly/api -X POST http://localhost/v1/tokens/oidc -H 'Content-Type: application/json' -d '{\"aud\": \"<keycard-issuer-url>\"}'" \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {iss, aud, sub}'
  • iss doesn’t match the Provider: the Provider uses personal or the organization’s display name. Use the slug in iss.
  • sub doesn’t match: the app was renamed, or the subject names one Machine and the app runs others. End the subject with *.
  • aud differs from the Zone issuer URL: the token was requested without aud, so Fly.io set its default, https://fly.io/<org-slug>. Request it with aud set to KEYCARD_ISSUER exactly.
  • Wrong Client ID: KEYCARD_CLIENT_ID isn’t the Client ID of a Workload Identity credential in this Zone, or KEYCARD_ISSUER names another Zone.
  • Connection refused on /.fly/api: the code isn’t running on a Fly Machine. The Machines API socket exists only inside one.