Skip to content

Commit

Permalink
Pass locally defined scopes to ClientCredentialsAuthenticator
Browse files Browse the repository at this point in the history
Signed-off-by: franco-bocci <[email protected]>
  • Loading branch information
franco-bocci committed Mar 21, 2023
1 parent 34f80ba commit 9fb69d6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 3 additions & 1 deletion flytekit/clients/auth/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ def __init__(
client_secret: str,
cfg_store: ClientConfigStore,
header_key: str = None,
scopes: typing.List[str] = None,
):
if not client_id or not client_secret:
raise ValueError("Client ID and Client SECRET both are required.")
cfg = cfg_store.get_client_config()
self._token_endpoint = cfg.token_endpoint
self._scopes = cfg.scopes
# Use scopes from `flytekit.configuration.PlatformConfig` if passed
self._scopes = scopes or cfg.scopes
self._client_id = client_id
self._client_secret = client_secret
super().__init__(endpoint, cfg.header_key or header_key)
Expand Down
1 change: 1 addition & 0 deletions flytekit/clients/auth_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def get_authenticator(cfg: PlatformConfig, cfg_store: ClientConfigStore) -> Auth
client_id=cfg.client_id,
client_secret=cfg.client_credentials_secret,
cfg_store=cfg_store,
scopes=cfg.scopes,
)
elif cfg_auth == AuthType.EXTERNAL_PROCESS or cfg_auth == AuthType.EXTERNALCOMMAND:
client_cfg = None
Expand Down
21 changes: 20 additions & 1 deletion tests/flytekit/unit/clients/auth/test_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_get_token(mock_requests):


@patch("flytekit.clients.auth.authenticator.requests")
def test_client_creds_authenticator(mock_requests):
def test_client_creds_authenticator_without_custom_scopes(mock_requests):
authn = ClientCredentialsAuthenticator(
ENDPOINT, client_id="client", client_secret="secret", cfg_store=static_cfg_store
)
Expand All @@ -92,4 +92,23 @@ def test_client_creds_authenticator(mock_requests):
response.json.return_value = json.loads("""{"access_token": "abc", "expires_in": 60}""")
mock_requests.post.return_value = response
authn.refresh_credentials()
expected_scopes = static_cfg_store.get_client_config().scopes

assert authn._creds
assert authn._scopes == expected_scopes


@patch("flytekit.clients.auth.authenticator.requests")
def test_client_creds_authenticator_with_custom_scopes(mock_requests):
expected_scopes = ["foo", "baz"]
authn = ClientCredentialsAuthenticator(
ENDPOINT, client_id="client", client_secret="secret", cfg_store=static_cfg_store, scopes=expected_scopes,
)
response = MagicMock()
response.status_code = 200
response.json.return_value = json.loads("""{"access_token": "abc", "expires_in": 60}""")
mock_requests.post.return_value = response
authn.refresh_credentials()

assert authn._creds
assert authn._scopes == expected_scopes

0 comments on commit 9fb69d6

Please sign in to comment.