Skip to content

Commit 83ab40d

Browse files
authored
[Auth] Fix incorrect argument type in translate_secret_token (mlrun#9162)
### 📝 Description <!-- A short summary of what this PR does. --> <!-- Include any relevant context or background information. --> Fix an issue where `translate_secret_token` expected to get a list of secret tokens, but got a dict from `extract_and_validate_tokens_info`. --- ### 🛠️ Changes Made <!-- - Key changes (e.g., added feature X, refactored Y, fixed Z) --> - Make `translate_secret_token` private function and refactor to correctly handle dict instead of list of tokens - Removed mocking of `translate_secret_token` in test. --- ### ✅ Checklist - [ ] I updated the documentation (if applicable) - [x] I have tested the changes in this PR - [ ] I confirmed whether my changes are covered by system tests - [ ] If yes, I ran all relevant system tests and ensured they passed before submitting this PR - [ ] I updated existing system tests and/or added new ones if needed to cover my changes - [ ] If I introduced a deprecation: - [ ] I followed the [Deprecation Guidelines](./DEPRECATION.md) - [ ] I updated the relevant Jira ticket for documentation --- ### 🧪 Testing <!-- - How it was tested (unit tests, manual, integration) --> <!-- - Any special cases covered. --> - Removed mock in test - Reproduced the issue, then saw it resolved after fix --- ### 🔗 References - Ticket link: https://iguazio.atlassian.net/browse/ML-11920 - Design docs links: - External links: --- ### 🚨 Breaking Changes? - [ ] Yes (explain below) - [ ] No <!-- If yes, describe what needs to be changed downstream: --> --- ### 🔍️ Additional Notes <!-- Anything else reviewers should know (follow-up tasks, known issues, affected areas etc.). --> <!-- ### 📸 Screenshots / Logs -->
1 parent 5243ab3 commit 83ab40d

File tree

2 files changed

+40
-49
lines changed

2 files changed

+40
-49
lines changed

mlrun/auth/utils.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -245,40 +245,12 @@ def load_and_prepare_secret_tokens(
245245
authenticated_id=auth_user_id,
246246
filter_by_authenticated_id=True,
247247
)
248-
secret_tokens = translate_secret_tokens(
248+
secret_tokens = _translate_secret_tokens(
249249
validated_tokens, raise_on_error=raise_on_error
250250
)
251251
return secret_tokens
252252

253253

254-
def translate_secret_tokens(
255-
tokens_list: list[dict[str, typing.Any]], raise_on_error: bool = True
256-
) -> list[mlrun.common.schemas.SecretToken]:
257-
"""
258-
Translate a list of validated token dictionaries into SecretToken objects.
259-
260-
Each dictionary in the list must be validated and contain the required fields
261-
for SecretToken creation. If an entry fails to translate, behavior depends on
262-
``raise_on_error``: raise an exception or log a warning.
263-
264-
:param tokens_list: List of validated token dictionaries.
265-
:param raise_on_error: Whether to raise exceptions on translation errors.
266-
:return: List of SecretToken objects created from the input dictionaries.
267-
:rtype: list[mlrun.common.schemas.SecretToken]
268-
"""
269-
token_file = os.path.expanduser(mlconf.auth_with_oauth_token.token_file)
270-
tokens = []
271-
for token in tokens_list:
272-
try:
273-
tokens.append(mlrun.common.schemas.SecretToken(**token))
274-
except Exception as exc:
275-
mlrun.utils.helpers.raise_or_log_error(
276-
f"Failed to create SecretToken from entry in {token_file}: {exc}",
277-
raise_on_error,
278-
)
279-
return tokens
280-
281-
282254
def extract_and_validate_tokens_info(
283255
secret_tokens: list[mlrun.common.schemas.SecretToken],
284256
authenticated_id: str,
@@ -374,3 +346,36 @@ def _decode_offline_token(token: str) -> dict:
374346
raise mlrun.errors.MLRunInvalidArgumentError(
375347
"Unexpected error decoding token"
376348
) from exc
349+
350+
351+
def _translate_secret_tokens(
352+
tokens_dict: dict[str, dict[str, typing.Any]], raise_on_error: bool = True
353+
) -> list[mlrun.common.schemas.SecretToken]:
354+
"""
355+
Translate a dictionary of validated token data into SecretToken objects.
356+
357+
The dictionary is keyed by token name, with values containing token data
358+
(including the token string). If an entry fails to translate, behavior depends
359+
on ``raise_on_error``: raise an exception or log a warning.
360+
361+
:param tokens_dict: Dictionary of validated token data, keyed by token name.
362+
:param raise_on_error: Whether to raise exceptions on translation errors.
363+
:return: List of SecretToken objects created from the input dictionary.
364+
:rtype: list[mlrun.common.schemas.SecretToken]
365+
"""
366+
token_file = os.path.expanduser(mlconf.auth_with_oauth_token.token_file)
367+
tokens = []
368+
for token_name, token_data in tokens_dict.items():
369+
try:
370+
tokens.append(
371+
mlrun.common.schemas.SecretToken(
372+
name=token_name,
373+
token=token_data["token"],
374+
)
375+
)
376+
except Exception as exc:
377+
mlrun.utils.helpers.raise_or_log_error(
378+
f"Failed to create SecretToken from entry in {token_file}: {exc}",
379+
raise_on_error,
380+
)
381+
return tokens

tests/auth/test_auth_utils.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -239,26 +239,12 @@ def test_load_and_prepare_secret_tokens_valid(
239239
path = _write_file(tmp_path, "tokens.yml", content)
240240
monkeypatch.setattr(config.auth_with_oauth_token, "token_file", path)
241241

242-
# Mock translate_secret_tokens to handle the dict returned by extract_and_validate_tokens_info
243-
def mock_translate_secret_tokens(tokens_dict, raise_on_error=True):
244-
return [
245-
mlrun.common.schemas.SecretToken(name=name, token=info["token"])
246-
for name, info in tokens_dict.items()
247-
]
248-
249-
with patch.object(
250-
mlrun.auth.utils,
251-
"translate_secret_tokens",
252-
side_effect=mock_translate_secret_tokens,
253-
):
254-
secret_tokens = mlrun.auth.utils.load_and_prepare_secret_tokens(
255-
auth_user_id=auth_user_id
256-
)
257-
assert isinstance(secret_tokens, list)
258-
assert len(secret_tokens) == expected_count
259-
assert all(
260-
isinstance(t, mlrun.common.schemas.SecretToken) for t in secret_tokens
261-
)
242+
secret_tokens = mlrun.auth.utils.load_and_prepare_secret_tokens(
243+
auth_user_id=auth_user_id
244+
)
245+
assert isinstance(secret_tokens, list)
246+
assert len(secret_tokens) == expected_count
247+
assert all(isinstance(t, mlrun.common.schemas.SecretToken) for t in secret_tokens)
262248

263249

264250
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)