-
Notifications
You must be signed in to change notification settings - Fork 37
feat: add CircleCI OIDC detector #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af314d7
feat: add CircleCI OIDC detector
cloudsmith-iduffy df15ca1
Merge branch 'master' into iduffy/circleci
BartoszBlizniak 8a15ad9
Merge branch 'master' into iduffy/circleci
cloudsmith-iduffy ee54840
chore: re-trigger CI (flaky CodeQL default-setup auth error)
cloudsmith-iduffy b38130b
Merge remote-tracking branch 'origin/master' into iduffy/circleci
cloudsmith-iduffy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
cloudsmith_cli/core/credentials/oidc/detectors/circleci.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Copyright 2026 Cloudsmith Ltd | ||
| """CircleCI OIDC detector. | ||
|
|
||
| Reads OIDC token from the ``CIRCLE_OIDC_TOKEN_V2`` or ``CIRCLE_OIDC_TOKEN`` | ||
| environment variables set by CircleCI's OIDC support. | ||
|
|
||
| References: | ||
| https://circleci.com/docs/guides/permissions-authentication/openid-connect-tokens/ | ||
| https://docs.cloudsmith.com/integrations/integrating-with-circleci | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| from .base import EnvironmentDetector | ||
|
|
||
|
|
||
| class CircleCIDetector(EnvironmentDetector): | ||
| """Detects CircleCI and reads OIDC token from environment variable.""" | ||
|
|
||
| name = "CircleCI" | ||
|
|
||
| def detect(self) -> bool: | ||
| return os.environ.get("CIRCLECI") == "true" and bool( | ||
| os.environ.get("CIRCLE_OIDC_TOKEN_V2") | ||
| or os.environ.get("CIRCLE_OIDC_TOKEN") | ||
| ) | ||
|
|
||
| def get_token(self) -> str: | ||
| token = os.environ.get("CIRCLE_OIDC_TOKEN_V2") or os.environ.get( | ||
| "CIRCLE_OIDC_TOKEN" | ||
| ) | ||
| if not token: | ||
| raise ValueError( | ||
| "CircleCI detected but neither CIRCLE_OIDC_TOKEN_V2 nor " | ||
| "CIRCLE_OIDC_TOKEN is set" | ||
| ) | ||
| return token | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """Tests for the CircleCI OIDC detector.""" | ||
|
|
||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from cloudsmith_cli.core.credentials.models import CredentialContext | ||
| from cloudsmith_cli.core.credentials.oidc.detectors import detect_environment | ||
| from cloudsmith_cli.core.credentials.oidc.detectors.circleci import CircleCIDetector | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def circleci_env(): | ||
| env = { | ||
| "CIRCLECI": "true", | ||
| "CIRCLE_OIDC_TOKEN_V2": "the-v2-jwt", | ||
| "CIRCLE_OIDC_TOKEN": "the-v1-jwt", | ||
| } | ||
| with mock.patch.dict("os.environ", env, clear=True): | ||
| yield env | ||
|
|
||
|
|
||
| class TestDetect: | ||
| def test_detects_when_circleci_and_v2_token_present(self, circleci_env): | ||
| detector = CircleCIDetector(context=CredentialContext()) | ||
| assert detector.detect() is True | ||
|
|
||
| def test_detects_with_only_v1_token(self, circleci_env): | ||
| del circleci_env["CIRCLE_OIDC_TOKEN_V2"] | ||
| with mock.patch.dict("os.environ", circleci_env, clear=True): | ||
| detector = CircleCIDetector(context=CredentialContext()) | ||
| assert detector.detect() is True | ||
|
|
||
| def test_not_detected_when_unset(self): | ||
| with mock.patch.dict("os.environ", {}, clear=True): | ||
| detector = CircleCIDetector(context=CredentialContext()) | ||
| assert detector.detect() is False | ||
|
|
||
| def test_not_detected_when_circleci_flag_missing(self, circleci_env): | ||
| del circleci_env["CIRCLECI"] | ||
| with mock.patch.dict("os.environ", circleci_env, clear=True): | ||
| detector = CircleCIDetector(context=CredentialContext()) | ||
| assert detector.detect() is False | ||
|
|
||
| def test_not_detected_without_any_token(self, circleci_env): | ||
| del circleci_env["CIRCLE_OIDC_TOKEN_V2"] | ||
| del circleci_env["CIRCLE_OIDC_TOKEN"] | ||
| with mock.patch.dict("os.environ", circleci_env, clear=True): | ||
| detector = CircleCIDetector(context=CredentialContext()) | ||
| assert detector.detect() is False | ||
|
|
||
|
|
||
| class TestGetToken: | ||
| def test_prefers_v2_token(self, circleci_env): | ||
| detector = CircleCIDetector(context=CredentialContext()) | ||
| assert detector.get_token() == "the-v2-jwt" | ||
|
|
||
| def test_falls_back_to_v1_token(self, circleci_env): | ||
| del circleci_env["CIRCLE_OIDC_TOKEN_V2"] | ||
| with mock.patch.dict("os.environ", circleci_env, clear=True): | ||
| detector = CircleCIDetector(context=CredentialContext()) | ||
| assert detector.get_token() == "the-v1-jwt" | ||
|
|
||
| def test_raises_when_no_token(self, circleci_env): | ||
| del circleci_env["CIRCLE_OIDC_TOKEN_V2"] | ||
| del circleci_env["CIRCLE_OIDC_TOKEN"] | ||
| with mock.patch.dict("os.environ", circleci_env, clear=True): | ||
| detector = CircleCIDetector(context=CredentialContext()) | ||
| with pytest.raises(ValueError, match="CIRCLE_OIDC_TOKEN_V2"): | ||
| detector.get_token() | ||
|
|
||
|
|
||
| class TestIntegration: | ||
| def test_detect_environment_selects_circleci(self, circleci_env): | ||
| detector = detect_environment(CredentialContext()) | ||
| assert isinstance(detector, CircleCIDetector) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.