feat(sdk/python): add OIDC auth support for Python SDK#1201
feat(sdk/python): add OIDC auth support for Python SDK#1201arpad-csepi wants to merge 13 commits intomainfrom
Conversation
|
The latest Buf updates on your PR. Results from workflow Buf CI / verify-proto (pull_request).
|
4855aa0 to
efd355c
Compare
f0e9314 to
c93c2a8
Compare
Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Signed-off-by: Tibor Kircsi <tkircsi@cisco.com> Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Signed-off-by: Árpád Csepi <csepi.arpad@outlook.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: Árpád Csepi <21104922+arpad-csepi@users.noreply.github.com>
c93c2a8 to
2a44a8d
Compare
Signed-off-by: Tibor Kircsi <tkircsi@cisco.com>
| import json | ||
| import os | ||
| import tempfile | ||
| import unittest |
Check notice
Code scanning / CodeQL
Module is imported with 'import' and 'import from' Note test
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 3 days ago
To fix the problem, keep only one style of importing the unittest module and adjust uses of mock accordingly. Since unittest is already imported as a module and is also used as unittest.TestCase and unittest.main(), the cleanest fix is to remove from unittest import mock and refer to unittest.mock everywhere instead of mock.
Concretely, in sdk/dir-py/agntcy/dir_sdk/client/test_oidc_auth.py:
- Remove the line
from unittest import mock. - Replace all occurrences of
mock.withunittest.mock.. The shown code usesmock.patch.dictandmock.patch, so these should becomeunittest.mock.patch.dictandunittest.mock.patch. This preserves behavior while eliminating the duplicate import style.
No additional methods, definitions, or imports are needed beyond updating these references.
| @@ -8,7 +8,6 @@ | ||
| import tempfile | ||
| import unittest | ||
| from datetime import UTC, datetime, timedelta | ||
| from unittest import mock | ||
|
|
||
| from agntcy.dir_sdk.client import Client, Config | ||
| from agntcy.dir_sdk.client.oauth_pkce import OAuthTokenHolder | ||
| @@ -17,7 +16,7 @@ | ||
|
|
||
| class OIDCAuthConfigTests(unittest.TestCase): | ||
| def test_load_from_env_uses_auth_token(self) -> None: | ||
| with mock.patch.dict( | ||
| with unittest.mock.patch.dict( | ||
| "os.environ", | ||
| { | ||
| "DIRECTORY_CLIENT_AUTH_TOKEN": "primary-token", | ||
| @@ -30,7 +29,7 @@ | ||
| self.assertEqual(config.oidc_access_token, "primary-token") | ||
|
|
||
| def test_load_from_env_ignores_legacy_token_names(self) -> None: | ||
| with mock.patch.dict( | ||
| with unittest.mock.patch.dict( | ||
| "os.environ", | ||
| { | ||
| "DIRECTORY_CLIENT_OIDC_ACCESS_TOKEN": "legacy-token", | ||
| @@ -51,7 +50,7 @@ | ||
|
|
||
| def test_token_cache_uses_dirctl_path(self) -> None: | ||
| with tempfile.TemporaryDirectory() as tmp_dir: | ||
| with mock.patch.dict("os.environ", {"XDG_CONFIG_HOME": tmp_dir}, clear=True): | ||
| with unittest.mock.patch.dict("os.environ", {"XDG_CONFIG_HOME": tmp_dir}, clear=True): | ||
| cache = TokenCache() | ||
|
|
||
| self.assertEqual( | ||
| @@ -69,13 +68,13 @@ | ||
| ) | ||
|
|
||
| with ( | ||
| mock.patch( | ||
| unittest.mock.patch( | ||
| "agntcy.dir_sdk.client.client.fetch_openid_configuration", | ||
| ) as fetch_mock, | ||
| mock.patch( | ||
| unittest.mock.patch( | ||
| "agntcy.dir_sdk.client.client.run_loopback_pkce_login", | ||
| ) as login_mock, | ||
| mock.patch( | ||
| unittest.mock.patch( | ||
| "agntcy.dir_sdk.client.client.TokenCache.get_valid_token", | ||
| return_value=None, | ||
| ), |
This PR adds OAuth 2.0 / OIDC authentication to the Directory Python SDK so gRPC calls can send a Bearer access token when auth_mode is oauth_pkce.
Supports:
Configuration is extended with OIDC/OAuth fields: issuer, client id/secret, redirect URI, callback port, auth timeout, scopes, optional static access token, and machine client settings (id, secret, secret file, scopes, optional token endpoint) with the corresponding
DIRECTORY_CLIENT_*environment variable wiring for these options.