Skip to content

Commit 83faa10

Browse files
feat: add GitLab CI OIDC detector
Add GitLab CI to OIDC credential auto-discovery. When running in GitLab CI/CD, the CLI reads the OIDC token from CLOUDSMITH_OIDC_TOKEN (configured via id_tokens in .gitlab-ci.yml, with legacy fallbacks to CI_JOB_JWT_V2 and CI_JOB_JWT) and exchanges it for a Cloudsmith access token. Works out of the box with no extra dependencies. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 894bf81 commit 83faa10

5 files changed

Lines changed: 140 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515
- Added Azure DevOps to OIDC credential auto-discovery. When running in an Azure DevOps pipeline, the CLI fetches an OIDC token from the `SYSTEM_OIDCREQUESTURI` endpoint using the pipeline's `SYSTEM_ACCESSTOKEN` and exchanges it for a Cloudsmith access token. Works out of the box with no extra dependencies.
1616
- Added GitHub Actions to OIDC credential auto-discovery. When running in GitHub Actions (with `id-token: write` permission), the CLI fetches an OIDC token from the Actions runtime endpoint and exchanges it for a Cloudsmith access token. Works out of the box with no extra dependencies.
1717
- Added a generic fallback to OIDC credential auto-discovery. When no dedicated environment is detected, the CLI reads an OIDC token from the `CLOUDSMITH_OIDC_TOKEN` environment variable (useful for Jenkins or any custom CI/CD) and exchanges it for a Cloudsmith access token. Works out of the box with no extra dependencies.
18+
- Added GitLab CI to OIDC credential auto-discovery. When running in GitLab CI/CD, the CLI reads the OIDC token from the `CLOUDSMITH_OIDC_TOKEN` environment variable (configured via `id_tokens` in `.gitlab-ci.yml`) and exchanges it for a Cloudsmith access token. Works out of the box with no extra dependencies.
1819

1920
## [1.18.0] - 2026-06-09
2021

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ In Azure DevOps Pipelines, OIDC credential discovery works out of the box with n
192192

193193
In GitHub Actions, OIDC credential discovery works out of the box with no extra dependencies — the CLI fetches an OIDC token from the Actions runtime when the workflow requests `id-token: write` permission. See the [Cloudsmith GitHub Actions OIDC guide](https://docs.cloudsmith.com/authentication/setup-cloudsmith-to-authenticate-with-oidc-in-github-actions).
194194

195+
#### GitLab CI OIDC Support
196+
197+
In GitLab CI/CD, OIDC credential discovery works out of the box with no extra dependencies. Configure an [`id_tokens`](https://docs.gitlab.com/ci/cloud_services/) entry in your `.gitlab-ci.yml` with an `aud` of `https://api.cloudsmith.io/openid/<your-org>` and expose it as `CLOUDSMITH_OIDC_TOKEN`, and the CLI will pick it up automatically:
198+
199+
```yaml
200+
job:
201+
id_tokens:
202+
CLOUDSMITH_OIDC_TOKEN:
203+
aud: https://api.cloudsmith.io/openid/<your-org>
204+
script:
205+
- cloudsmith push ...
206+
```
207+
208+
See the [Cloudsmith GitLab CI/CD integration guide](https://docs.cloudsmith.com/integrations/integrating-with-gitlab-cicd).
209+
195210
#### Generic OIDC Support (Jenkins, custom CI/CD)
196211

197212
As a fallback for environments without a dedicated detector (for example Jenkins with the [credentials binding plugin](https://plugins.jenkins.io/credentials-binding/), or any custom CI/CD system), set the `CLOUDSMITH_OIDC_TOKEN` environment variable to an OIDC JWT and the CLI will exchange it for a Cloudsmith access token. This detector runs last, so a dedicated environment is always preferred when present. See the [Cloudsmith Jenkins OIDC guide](https://docs.cloudsmith.com/authentication/setup-jenkins-to-authenticate-to-cloudsmith-using-oidc).

cloudsmith_cli/core/credentials/oidc/detectors/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .circleci import CircleCIDetector
1313
from .generic import GenericDetector
1414
from .github_actions import GitHubActionsDetector
15+
from .gitlab_ci import GitLabCIDetector
1516

1617
if TYPE_CHECKING:
1718
from ... import CredentialContext
@@ -23,6 +24,7 @@
2324
AzureDevOpsDetector,
2425
GitHubActionsDetector,
2526
BitbucketPipelinesDetector,
27+
GitLabCIDetector,
2628
AWSDetector,
2729
GenericDetector,
2830
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2026 Cloudsmith Ltd
2+
"""GitLab CI OIDC detector.
3+
4+
Reads an OIDC token from environment variables populated by GitLab's
5+
``id_tokens`` configuration in ``.gitlab-ci.yml``.
6+
7+
References:
8+
https://docs.gitlab.com/ci/cloud_services/
9+
https://docs.cloudsmith.com/integrations/integrating-with-gitlab-cicd
10+
"""
11+
12+
from __future__ import annotations
13+
14+
import os
15+
16+
from .base import EnvironmentDetector
17+
18+
19+
class GitLabCIDetector(EnvironmentDetector):
20+
"""Detects GitLab CI and reads an OIDC token from an environment variable.
21+
22+
GitLab requires users to configure ``id_tokens`` in ``.gitlab-ci.yml``,
23+
minting a token with ``aud`` set to the Cloudsmith OIDC endpoint and
24+
exposing it as ``CLOUDSMITH_OIDC_TOKEN``. The legacy ``CI_JOB_JWT``/
25+
``CI_JOB_JWT_V2`` variables are deliberately not consulted: they were
26+
removed in GitLab 17.0, carry the GitLab instance URL as their audience
27+
(not the Cloudsmith audience the token exchange validates), and were
28+
auto-injected into every job on older instances.
29+
"""
30+
31+
name = "GitLab CI"
32+
33+
TOKEN_ENV_VAR = "CLOUDSMITH_OIDC_TOKEN"
34+
35+
def detect(self) -> bool:
36+
if os.environ.get("GITLAB_CI") != "true":
37+
return False
38+
return bool(os.environ.get(self.TOKEN_ENV_VAR))
39+
40+
def get_token(self) -> str:
41+
token = os.environ.get(self.TOKEN_ENV_VAR)
42+
if token:
43+
return token
44+
raise ValueError(
45+
"GitLab CI detected but no OIDC token found. "
46+
"Configure id_tokens in .gitlab-ci.yml and expose it as "
47+
+ self.TOKEN_ENV_VAR
48+
)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""Tests for the GitLab CI OIDC detector."""
2+
3+
from unittest import mock
4+
5+
import pytest
6+
7+
from cloudsmith_cli.core.credentials.models import CredentialContext
8+
from cloudsmith_cli.core.credentials.oidc.detectors import detect_environment
9+
from cloudsmith_cli.core.credentials.oidc.detectors.gitlab_ci import GitLabCIDetector
10+
11+
12+
@pytest.fixture
13+
def gitlab_env():
14+
env = {
15+
"GITLAB_CI": "true",
16+
"CLOUDSMITH_OIDC_TOKEN": "the-jwt",
17+
}
18+
with mock.patch.dict("os.environ", env, clear=True):
19+
yield env
20+
21+
22+
class TestDetect:
23+
def test_detects_when_gitlab_ci_and_token_present(self, gitlab_env):
24+
detector = GitLabCIDetector(context=CredentialContext())
25+
assert detector.detect() is True
26+
27+
def test_not_detected_when_unset(self):
28+
with mock.patch.dict("os.environ", {}, clear=True):
29+
detector = GitLabCIDetector(context=CredentialContext())
30+
assert detector.detect() is False
31+
32+
def test_not_detected_without_gitlab_ci_flag(self, gitlab_env):
33+
del gitlab_env["GITLAB_CI"]
34+
with mock.patch.dict("os.environ", gitlab_env, clear=True):
35+
detector = GitLabCIDetector(context=CredentialContext())
36+
assert detector.detect() is False
37+
38+
def test_not_detected_when_gitlab_ci_not_true(self, gitlab_env):
39+
gitlab_env["GITLAB_CI"] = "false"
40+
with mock.patch.dict("os.environ", gitlab_env, clear=True):
41+
detector = GitLabCIDetector(context=CredentialContext())
42+
assert detector.detect() is False
43+
44+
def test_not_detected_without_any_token(self, gitlab_env):
45+
del gitlab_env["CLOUDSMITH_OIDC_TOKEN"]
46+
with mock.patch.dict("os.environ", gitlab_env, clear=True):
47+
detector = GitLabCIDetector(context=CredentialContext())
48+
assert detector.detect() is False
49+
50+
def test_not_detected_with_legacy_ci_job_jwt(self, gitlab_env):
51+
del gitlab_env["CLOUDSMITH_OIDC_TOKEN"]
52+
gitlab_env["CI_JOB_JWT_V2"] = "legacy-jwt"
53+
with mock.patch.dict("os.environ", gitlab_env, clear=True):
54+
detector = GitLabCIDetector(context=CredentialContext())
55+
assert detector.detect() is False
56+
57+
58+
class TestGetToken:
59+
def test_returns_token(self, gitlab_env):
60+
detector = GitLabCIDetector(context=CredentialContext())
61+
assert detector.get_token() == "the-jwt"
62+
63+
def test_raises_when_no_token(self, gitlab_env):
64+
del gitlab_env["CLOUDSMITH_OIDC_TOKEN"]
65+
with mock.patch.dict("os.environ", gitlab_env, clear=True):
66+
detector = GitLabCIDetector(context=CredentialContext())
67+
with pytest.raises(ValueError):
68+
detector.get_token()
69+
70+
71+
class TestIntegration:
72+
def test_detect_environment_selects_gitlab_ci(self, gitlab_env):
73+
detector = detect_environment(CredentialContext())
74+
assert isinstance(detector, GitLabCIDetector)

0 commit comments

Comments
 (0)