sk_earnie_ key sitting in a repository secret or a CI/CD variable, where someone has to store it, rotate it, and keep it out of logs. The key a job gets belongs to a service account, so it holds that account’s permissions and projects and nothing more.
Before you start: Only the Admin role can turn CI token exchange on and add trust rules. The service account the rule points to must exist already. See Service accounts and Team and roles. The job runs the Earnie CLI, or the Earnie GitHub action or GitLab template, which use it.
How It Works
- The job asks GitHub or GitLab for an OIDC token whose audience is your Earnie address.
earnie auth login --oidcsends that token to Earnie.- Earnie confirms the token is genuine, unexpired, and meant for your Earnie, and finds the trust rule whose conditions the token meets.
- Earnie returns an API key held by the rule’s service account. The CLI stores it where
--api-key-stdinwould, and the commands that follow use it.
Turning It On
CI token exchange is off for every organisation until an Admin turns it on. While it is off, Earnie refuses every exchange, whatever trust rules exist.- Go to Settings → Service accounts.
- In CI token exchange, switch on Allow CI token exchange. It saves at once.
Adding a Trust Rule
A trust rule lets jobs from one repository or project get a key of one service account. A job must meet every condition the rule sets.- Go to Settings → Service accounts and select the service account.
- Under CI trust rules, select Add trust rule.
- Enter a Name. It appears in this list and in the audit log.
- Choose the CI platform, fill in its conditions, and select Add rule.
earnie auth login --oidc for the rule. Select CI snippet on a rule to see it again.
You can’t edit a rule. To change a condition, delete the rule and add another, so the rule the audit log names is always the one that was in force.
GitHub Actions Conditions
Add the repository ID. If someone deletes a repository and another
account later takes its name, a rule that matches on the name alone would
trust the new owner. The numeric ID doesn’t follow the name. You can read it
as
id in GitHub’s response from
https://api.github.com/repos/OWNER/NAME.GitLab CI Conditions
A rule’s Ref for GitLab is compared with the token’s
ref_path claim, which holds the full ref (refs/heads/main), not the short branch name. GitLab added ref_path in version 16.0. On an older self-managed GitLab, a rule with a Ref never matches.
Matching Rules
Earnie compares every condition exactly, with two exceptions: the GitHub repository ignores case, and a Ref may end in one* to match a prefix, such as refs/heads/release/*. A * anywhere else is refused when you add the rule. A condition the token doesn’t carry doesn’t match. Through the API, a rule that names a condition Earnie doesn’t know is refused with 400, so a misspelt condition can never widen a rule.
Keep rules narrow. A rule with no Ref, or one such as
refs/*, lets
every branch of the repository get the service account’s key, including a
branch a contributor pushed. Use one rule per repository or project. A
repository name takes no wildcard.Audience
The Audience is theaud claim the token must carry. Leave it empty to use your Earnie address, written in lower case with no trailing slash. That is the audience earnie auth login --oidc asks for, so an empty audience works without any other setting. Set a different one only when the job requests its token for that audience itself, and pass the same value with --oidc-audience.
Key Lifetime
Key lifetime (minutes) sets how long each key a rule issues lasts. The default is 15 minutes. You can set a whole number from 1 to 60. Earnie refuses a longer lifetime: the form shows “At most 60 minutes”, and the API returns400.
Choose a lifetime that covers the job. Once it passes, the key stops working with 401 key_expired. See One Token, One Exchange for how each platform renews it.
Running It in GitHub Actions
Give the jobid-token: write, which lets it request its OIDC token, and leave out api-key. The Earnie action signs in with OIDC on its own when api-key is empty and the job can request a token:
vX.Y.Z with the CLI release tag. See Earnie CLI for the action’s inputs.
To run the exchange yourself, call the CLI in a step of a job that has id-token: write, before any command that talks to Earnie:
Match the ref GitHub reports. A job started by a
pull_request event
reports the ref refs/pull/<number>/merge, not the branch. A rule whose
Ref is refs/heads/main doesn’t match it. Use refs/pull/* for pull
request jobs, and a rule with refs/heads/main for pushes to main.Running It in GitLab CI
Declare an ID token on the job, withaud set to your Earnie address. The CLI reads it from EARNIE_ID_TOKEN:
oidc input on, the job signs in with the ID token when EARNIE_ID_TOKEN is set. Without the token, it falls back to the EARNIE_API_KEY variable.
To run the exchange in your own job instead, add earnie auth login --oidc to its script, before the commands that talk to Earnie. If the token has another name, say which variable holds it with --oidc-token-env MY_CI_TOKEN. The option takes the variable’s name, never the token itself.
On a self-managed GitLab, the rule’s GitLab URL must be the instance’s address exactly as the token’s issuer, and Earnie must be able to reach it. See Self-Managed GitLab on a Private Network.
Another CI Platform
Any other CI that can hand a job an OIDC ID token can export it in a variable and name it:One Token, One Exchange
Earnie accepts each OIDC token once. A second attempt with the same token gets409 token_already_exchanged.
- GitHub Actions issues a new token for every request. The CLI asks for a new one each time it signs in. If a later command meets an expired key, the CLI signs in again once by itself and repeats the request.
- GitLab CI gives a job one ID token, and an exchange uses it up. Exchange once per job, and set the rule’s Key lifetime so one key covers the whole job. If the key expires first, the CLI says so and names the fix; it can’t sign in again inside the same job. Run the exchange again in a new job, or raise the lifetime up to 60 minutes.
What the CLI Prints
On success,earnie auth login --oidc prints the service account and when the key expires. It never prints the key. With --format json, the document has an oidc_login member:
--oidc-audience overrides the audience. It defaults to the API URL without a trailing slash. --oidc can’t be combined with --api-key-stdin, and --oidc-audience and --oidc-token-env only apply with --oidc.
What the Audit Log Records
Earnie records every exchange and every refusal in the audit log.
What the job does with the key afterwards is recorded under the same service account and key, so one entry ties the exchange to the job’s work. Refusals are recorded within a limit per client address, so a flood of bad requests can’t fill the log.
Keys from an exchange don’t appear on the API keys page or in the service account’s key list, because there is one for every job. They can’t be rotated. Earnie deletes an exchanged key’s record some days after it expires.
Turning It Off or Removing Access
A revoked key gets
401 key_revoked.
When an Exchange Fails
The API answers with these errors. The CLI turns them into an exit code and a message that names the fix.
The CLI also stops before it contacts Earnie, with exit
8, when the job can’t produce a token:
Self-Managed GitLab on a Private Network
Earnie fetches the signing keys of the issuer a rule names. By default it does so only from public addresses overhttps. If your self-managed GitLab sits on a private address (a private range such as 10.0.0.0/8, or loopback), set this in tenant.env and run ./install.sh again from the bundle directory:
http issuer on a loopback host, for testing. Cloud metadata addresses stay blocked either way. Leave it false, the default, for gitlab.com, GitHub, and any GitLab on a public address.
For what Earnie sends to an issuer, and how to allow it through a firewall, see Outbound network calls.
What’s Next
- Service accounts: create the account a rule points to
- Earnie CLI: the sign-in options and exit codes
- Audit log: find the exchanges a pipeline made