-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from yftacherzog/RHTAPWATCH-825-use-oidc-auth
chore(RHTAPWATCH-825): authenticate with oidc
- Loading branch information
Showing
9 changed files
with
499 additions
and
401 deletions.
There are no files selected for viewing
This file contains 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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,32 @@ | ||
"""Authenticate with ODCS using OIDC""" | ||
|
||
from typing import Callable | ||
|
||
from authlib.integrations.requests_client import OAuth2Session # type: ignore | ||
from odcs.client.odcs import ODCS, AuthMech # type: ignore | ||
|
||
|
||
def get_odcs_session( | ||
client_id: str, | ||
client_secret: str, | ||
odcs_server: str = "https://odcs.engineering.redhat.com", | ||
oidc_token_url: str = ( | ||
"https://auth.redhat.com/auth/realms/EmployeeIDP/protocol/openid-connect/token" | ||
), | ||
session_fetcher: Callable[[str, str, str, str], OAuth2Session] = OAuth2Session, | ||
) -> ODCS: | ||
"""Authenticate using OIDC and return an authenticated ODCS client""" | ||
oidc_client = session_fetcher( | ||
client_id, client_secret, "client_secret_basic", "openid" | ||
) | ||
|
||
try: | ||
token = oidc_client.fetch_token( | ||
url=oidc_token_url, grant_type="client_credentials" | ||
) | ||
except Exception as ex: | ||
raise RuntimeError("Failed fetching OIDC token") from ex | ||
|
||
return ODCS( | ||
odcs_server, auth_mech=AuthMech.OpenIDC, openidc_token=token["access_token"] | ||
) |
This file contains 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 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,38 @@ | ||
"""Test odcs_session""" | ||
|
||
from unittest.mock import create_autospec, sentinel | ||
|
||
import pytest | ||
from authlib.integrations.requests_client import OAuth2Session # type: ignore | ||
|
||
from generate_compose.odcs_session import get_odcs_session | ||
|
||
|
||
class TestODCSSession: | ||
"""Test odcs_session.py""" | ||
|
||
def test_get_odcs_session(self) -> None: | ||
"""test get_odcs_session""" | ||
mock = create_autospec(OAuth2Session) | ||
mock.return_value.fetch_token.return_value = {"access_token": sentinel.token} | ||
odcs = get_odcs_session( | ||
client_id="some-client", | ||
client_secret="some-secret", | ||
odcs_server=sentinel.url, | ||
session_fetcher=mock, | ||
) | ||
assert odcs.server_url == sentinel.url | ||
assert odcs._openidc_token == sentinel.token # pylint: disable=protected-access | ||
|
||
def test_get_odcs_session_fail_fetching_token(self) -> None: | ||
"""test get_odcs_session fails with proper message""" | ||
mock = create_autospec(OAuth2Session) | ||
mock.return_value.fetch_token.side_effect = Exception | ||
with pytest.raises(RuntimeError) as ex: | ||
get_odcs_session( | ||
client_id="some-client", | ||
client_secret="some-secret", | ||
odcs_server=sentinel.url, | ||
session_fetcher=mock, | ||
) | ||
assert str(ex.value) == "Failed fetching OIDC token" |