-
Notifications
You must be signed in to change notification settings - Fork 37
feat: add generic OIDC detector #304
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
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
Some comments aren't visible on the classic Files Changed page.
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
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,41 @@ | ||
| # Copyright 2026 Cloudsmith Ltd | ||
| """Generic fallback OIDC detector. | ||
|
|
||
| Reads an OIDC token from the ``CLOUDSMITH_OIDC_TOKEN`` environment variable. | ||
| Works for Jenkins (with the credentials binding plugin), or any custom CI/CD | ||
| system that can inject an OIDC token via an environment variable. | ||
|
|
||
| References: | ||
| https://docs.cloudsmith.com/authentication/setup-jenkins-to-authenticate-to-cloudsmith-using-oidc | ||
| https://plugins.jenkins.io/credentials-binding/ | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
cloudsmith-iduffy marked this conversation as resolved.
|
||
|
|
||
| import os | ||
|
|
||
| from .base import EnvironmentDetector | ||
|
|
||
| TOKEN_ENV_VAR = "CLOUDSMITH_OIDC_TOKEN" | ||
|
|
||
|
|
||
| class GenericDetector(EnvironmentDetector): | ||
| """Generic fallback: reads the OIDC token from CLOUDSMITH_OIDC_TOKEN. | ||
|
|
||
| Works for Jenkins (with the credentials binding plugin), or any custom | ||
| CI/CD system that can inject an OIDC token via an environment variable. | ||
| """ | ||
|
|
||
| name = "Generic" | ||
|
|
||
| def detect(self) -> bool: | ||
| return bool((os.environ.get(TOKEN_ENV_VAR) or "").strip()) | ||
|
|
||
| def get_token(self) -> str: | ||
| token = (os.environ.get(TOKEN_ENV_VAR) or "").strip() | ||
| if not token: | ||
| raise ValueError( | ||
| f"Generic OIDC detector selected but {TOKEN_ENV_VAR} is not " | ||
| "set. Set it to the OIDC JWT to exchange for a Cloudsmith token." | ||
| ) | ||
| 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,72 @@ | ||
| """Tests for the generic fallback 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.generic import GenericDetector | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def generic_env(): | ||
| env = { | ||
| "CLOUDSMITH_OIDC_TOKEN": "the-jwt", | ||
| } | ||
| with mock.patch.dict("os.environ", env, clear=True): | ||
| yield env | ||
|
|
||
|
|
||
| class TestDetect: | ||
| def test_detects_when_token_present(self, generic_env): | ||
| detector = GenericDetector(context=CredentialContext()) | ||
| assert detector.detect() is True | ||
|
|
||
| def test_not_detected_when_unset(self): | ||
| with mock.patch.dict("os.environ", {}, clear=True): | ||
| detector = GenericDetector(context=CredentialContext()) | ||
| assert detector.detect() is False | ||
|
|
||
| def test_not_detected_when_token_empty(self, generic_env): | ||
| generic_env["CLOUDSMITH_OIDC_TOKEN"] = "" | ||
| with mock.patch.dict("os.environ", generic_env, clear=True): | ||
| detector = GenericDetector(context=CredentialContext()) | ||
| assert detector.detect() is False | ||
|
cloudsmith-iduffy marked this conversation as resolved.
|
||
|
|
||
| def test_not_detected_when_token_whitespace_only(self, generic_env): | ||
| generic_env["CLOUDSMITH_OIDC_TOKEN"] = " \t\n" | ||
| with mock.patch.dict("os.environ", generic_env, clear=True): | ||
| detector = GenericDetector(context=CredentialContext()) | ||
| assert detector.detect() is False | ||
|
|
||
|
|
||
| class TestGetToken: | ||
| def test_returns_token(self, generic_env): | ||
| detector = GenericDetector(context=CredentialContext()) | ||
| assert detector.get_token() == "the-jwt" | ||
|
|
||
| def test_strips_surrounding_whitespace(self, generic_env): | ||
| generic_env["CLOUDSMITH_OIDC_TOKEN"] = " the-jwt\n" | ||
| with mock.patch.dict("os.environ", generic_env, clear=True): | ||
| detector = GenericDetector(context=CredentialContext()) | ||
| assert detector.get_token() == "the-jwt" | ||
|
|
||
| def test_raises_when_token_missing(self): | ||
| with mock.patch.dict("os.environ", {}, clear=True): | ||
| detector = GenericDetector(context=CredentialContext()) | ||
| with pytest.raises(ValueError, match="CLOUDSMITH_OIDC_TOKEN"): | ||
| detector.get_token() | ||
|
|
||
| def test_raises_when_token_whitespace_only(self, generic_env): | ||
| generic_env["CLOUDSMITH_OIDC_TOKEN"] = " " | ||
| with mock.patch.dict("os.environ", generic_env, clear=True): | ||
| detector = GenericDetector(context=CredentialContext()) | ||
| with pytest.raises(ValueError, match="CLOUDSMITH_OIDC_TOKEN"): | ||
| detector.get_token() | ||
|
|
||
|
|
||
| class TestIntegration: | ||
| def test_detect_environment_selects_generic(self, generic_env): | ||
| detector = detect_environment(CredentialContext()) | ||
| assert isinstance(detector, GenericDetector) | ||
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.