> ## Documentation Index
> Fetch the complete documentation index at: https://docs.scanoss.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Keyless CI with OIDC

> Let GitHub Actions and GitLab CI jobs sign in to Earnie with the identity their CI platform gives them, so no long-lived API key is stored in the pipeline.

**CI token exchange** lets a pipeline sign in to Earnie without a stored secret. Each GitHub Actions or GitLab CI job can ask its platform for a signed **OIDC token** (OpenID Connect token) that says which repository or project the job belongs to and which ref it runs on. Earnie checks that token against rules an Admin wrote, and answers with an API key that expires in minutes.

Use it when you don't want an `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](https://docs.scanoss.com/en/latest/earnie/administration/service-accounts.md), 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](https://docs.scanoss.com/en/latest/earnie/administration/service-accounts.md) and [Team and roles](https://docs.scanoss.com/en/latest/earnie/administration/team-and-roles.md). The job runs the [Earnie CLI](https://docs.scanoss.com/en/latest/earnie/using-earnie/earnie-cli.md), or the Earnie GitHub action or GitLab template, which use it.

## How It Works

1. The job asks GitHub or GitLab for an OIDC token whose audience is your Earnie address.
2. `earnie auth login --oidc` sends that token to Earnie.
3. Earnie confirms the token is genuine, unexpired, and meant for your Earnie, and finds the **trust rule** whose conditions the token meets.
4. Earnie returns an API key held by the rule's service account. The CLI stores it where `--api-key-stdin` would, and the commands that follow use it.

A trust rule only chooses which service account a job may use. The account decides what the key can do, and a rule can't grant more than its account holds.

## 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.

1. Go to **Settings → Service accounts**.
2. In **CI token exchange**, switch on **Allow CI token exchange**. It saves at once.

Without the permission to change it, you see only whether it is on or off.

## 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.

1. Go to **Settings → Service accounts** and select the service account.
2. Under **CI trust rules**, select **Add trust rule**.
3. Enter a **Name**. It appears in this list and in the audit log.
4. Choose the **CI platform**, fill in its conditions, and select **Add rule**.

Earnie then shows the workflow or job text that runs `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

| Field | Required | What it matches |
| - | - | - |
| **Repository** | Yes | The `owner/name` of the repository. Compared without regard to case. |
| **Repository ID** | No, but recommended | GitHub's numeric ID for the repository. |
| **Ref** | No | The full ref the job runs on, such as `refs/heads/main` or `refs/tags/v*`. Empty accepts any ref. |
| **Environment** | No | The GitHub deployment environment the job targets. Empty accepts any environment, including none. |

> [!NOTE]
>
> **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`.

GitHub's token issuer is fixed, so you don't enter one.

### GitLab CI Conditions

| Field | Required | What it matches |
| - | - | - |
| **GitLab URL** | Yes | The issuer: `https://gitlab.com`, or the URL of your self-managed GitLab. `https` only, with no path. |
| **Project path** | Yes | The full path with its groups, such as `group/subgroup/app`. |
| **Ref** | No | The full ref the job runs on. Empty accepts any ref. |

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.

> [!NOTE]
>
> **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.

If the token meets rules of more than one service account, Earnie refuses it, because nobody meant that. If it meets several rules of one account, the oldest rule issues the key.

### Audience

The **Audience** is the `aud` 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 returns `400`.

Choose a lifetime that covers the job. Once it passes, the key stops working with `401 key_expired`. See [One Token, One Exchange](#one-token-one-exchange) for how each platform renews it.

## Running It in GitHub Actions

Give the job `id-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:

```yaml theme={null}
name: Earnie
on:
  pull_request:
  push:
    branches: [main]
permissions:
  contents: read
  id-token: write
jobs:
  earnie:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}
          fetch-depth: 0
      - uses: scanoss/earnie-cli@vX.Y.Z
        with:
          api-url: https://your-company.earnie.dev
```

Replace `vX.Y.Z` with the CLI release tag. See [Earnie CLI](https://docs.scanoss.com/en/latest/earnie/using-earnie/earnie-cli.md#github-actions) 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:

```yaml theme={null}
      - run: earnie auth login --oidc --api-url https://your-company.earnie.dev
```

> [!NOTE]
>
> **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, with `aud` set to your Earnie address. The CLI reads it from `EARNIE_ID_TOKEN`:

```yaml theme={null}
include:
  - remote: https://raw.githubusercontent.com/scanoss/earnie-cli/vX.Y.Z/gitlab/earnie-scan.yml
    inputs:
      oidc: true

earnie-scan:
  id_tokens:
    EARNIE_ID_TOKEN:
      aud: https://your-company.earnie.dev
```

With the template's `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](#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:

```bash theme={null}
earnie auth login --oidc --oidc-token-env MY_CI_TOKEN
```

Earnie only accepts tokens from the issuers its trust rules name. Today a rule can name GitHub Actions or a GitLab instance.

## One Token, One Exchange

Earnie accepts each OIDC token once. A second attempt with the same token gets `409 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:

| Field | What it holds |
| - | - |
| `platform` | `github-actions`, `gitlab-ci`, or `token-env` |
| `audience` | The audience the token was requested for |
| `service_account` | The account's `id` and `name` |
| `key_id` | The ID of the key Earnie issued |
| `trust_rule_id` | The rule that matched |
| `expires_at` | When the key expires |

`--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](https://docs.scanoss.com/en/latest/earnie/evidence/audit-log.md).

| Event | When | Actor |
| - | - | - |
| `serviceaccount.token_exchanged` | A job got a key. The entry names the rule, the service account, the issuer, the job's repository or project, its ref, and its run. | The issued key, shown as the service account and the key |
| `serviceaccount.token_exchange_refused` | An exchange was refused, with the reason. | Unauthenticated |
| `serviceaccount.trust_rule_created` and `serviceaccount.trust_rule_deleted` | An Admin added or deleted a rule. | The Admin |
| `organization.ci_token_exchange_updated` | An Admin turned CI token exchange on or off. | The Admin |

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](https://docs.scanoss.com/en/latest/earnie/administration/api-keys.md#rotating-a-key). Earnie deletes an exchanged key's record some days after it expires.

## Turning It Off or Removing Access

| To do this | Do this | What happens to live keys |
| - | - | - |
| Stop all CI sign-in | Switch off **Allow CI token exchange** and confirm | Every live key an exchange issued is revoked now. Earnie refuses every new exchange until you turn it on again. |
| Stop one repository or project | Delete its trust rule | Every live key that rule issued stops working at once. The rule stays in the list as **Deleted**. |
| Stop one service account | Disable the service account | Its live keys are revoked, as for any service account. |

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.

| Status | Error code | Exit | What it means and what to do |
| - | - | - | - |
| `400` | `invalid_request` | `8` | The request wasn't a valid token exchange, or the token isn't a JWT. Check that the variable holds the job's OIDC ID token. |
| `401` | `token_exchange_refused` | `4` | Earnie refused the token. The issuer isn't trusted, the token is expired, its signature or audience doesn't verify, no rule matches its claims, rules of more than one service account match, or the service account is disabled. Check the rule's repository or project, ref, and audience under **Settings → Service accounts**. The message says which check failed without naming any rule. |
| `403` | `token_exchange_disabled` | `5` | An Admin hasn't turned on CI token exchange. Ask them to switch it on. |
| `409` | `token_already_exchanged` | `4` | This token was exchanged already. Use a new token. On GitLab, exchange once per job. |
| `429` | | `6` | Too many attempts from this address. Retry after the `Retry-After` header. |
| `503` | `issuer_unreachable` | `6` | Earnie couldn't fetch the issuer's signing keys. Retry later. |
| `404` | | `8` | Your Earnie is older than the CLI and doesn't offer CI token exchange. Ask your administrator to upgrade it. |

The CLI also stops before it contacts Earnie, with exit `8`, when the job can't produce a token:

| Message names | Fix |
| - | - |
| The GitHub Actions job can't request an OIDC token | Add `permissions: id-token: write` to the job. |
| No CI OIDC token source found | On GitLab, add the `id_tokens` entry. Elsewhere, export the token and pass its variable name with `--oidc-token-env`. |
| The token variable is empty | The variable named by `--oidc-token-env` (`EARNIE_ID_TOKEN` by default) holds nothing. Check the job's `id_tokens` entry. |

## 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 over `https`. 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:

```text theme={null}
EARNIE_CI_OIDC_ALLOW_PRIVATE_ISSUERS=true
```

Earnie may then fetch keys from a private address, and a rule may name a plain `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](https://docs.scanoss.com/en/latest/earnie/self-hosting/outbound-network-calls.md).

## What's Next

* [Service accounts](https://docs.scanoss.com/en/latest/earnie/administration/service-accounts.md): create the account a rule points to
* [Earnie CLI](https://docs.scanoss.com/en/latest/earnie/using-earnie/earnie-cli.md#signing-in): the sign-in options and exit codes
* [Audit log](https://docs.scanoss.com/en/latest/earnie/evidence/audit-log.md): find the exchanges a pipeline made


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.