Skip to content

fix iam auth from file, add tests #435

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 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added tests/iam/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions tests/iam/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from ydb.iam.auth import BaseJWTCredentials
from unittest.mock import patch, mock_open

CONTENT1 = '{"service_account_id":"my_sa", "id":"123", "private_key":"pppp", "user_account_id":"ua_id"}'
CONTENT2 = '{"id":"123", "private_key":"pppp", "user_account_id":"ua_id"}'


class FakeAuth(BaseJWTCredentials):
def __init__(self, account_id, key_id, private_key, iam_endpoint=None, iam_channel_credentials=None):
self.account_id = account_id
self.key_id = key_id
self.private_key = private_key
self.iam_endpoint = iam_endpoint
self.iam_channel_credentials = iam_channel_credentials

def __eq__(self, other):
return self.__dict__ == other.__dict__ if type(self) == type(other) else False


@patch("builtins.open", new_callable=mock_open, read_data=CONTENT1)
def test_auth_from_file(mock_file):
r1 = FakeAuth.from_file("my_file.json", iam_endpoint="my_iam_address", iam_channel_credentials="my_creds")
mock_file.assert_called_with("my_file.json", "r")

r2 = FakeAuth.from_content(CONTENT1, iam_endpoint="my_iam_address", iam_channel_credentials="my_creds")
assert r1 == r2
assert r1.__dict__ == {
"account_id": "my_sa",
"key_id": "123",
"private_key": "pppp",
"iam_endpoint": "my_iam_address",
"iam_channel_credentials": "my_creds",
}

r3 = FakeAuth.from_content(CONTENT2)

assert r3.__dict__ == {
"account_id": "ua_id",
"key_id": "123",
"private_key": "pppp",
"iam_endpoint": None,
"iam_channel_credentials": None,
}
4 changes: 1 addition & 3 deletions ydb/iam/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ def from_file(cls, key_file, iam_endpoint=None, iam_channel_credentials=None):
with open(os.path.expanduser(key_file), "r") as r:
key = r.read()

return BaseJWTCredentials.from_content(
cls, key, iam_endpoint=iam_endpoint, iam_channel_credentials=iam_channel_credentials
)
return cls.from_content(key, iam_endpoint=iam_endpoint, iam_channel_credentials=iam_channel_credentials)

@classmethod
def from_content(cls, key, iam_endpoint=None, iam_channel_credentials=None):
Expand Down
Loading