Skip to content

fix: crash for additional keys in credentials.json #115

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 4 commits into from
Nov 15, 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
31 changes: 19 additions & 12 deletions core/src/stackit/core/authorization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional
from typing import Optional

from requests.auth import AuthBase

Expand All @@ -22,17 +22,14 @@ def __init__(
STACKIT_SERVICE_ACCOUNT_TOKEN: str = None,
STACKIT_SERVICE_ACCOUNT_KEY_PATH: str = None,
STACKIT_PRIVATE_KEY_PATH: str = None,
**kwargs,
):
self.service_account_mail = STACKIT_SERVICE_ACCOUNT_EMAIL
self.service_account_token = STACKIT_SERVICE_ACCOUNT_TOKEN
self.service_account_key_path = STACKIT_SERVICE_ACCOUNT_KEY_PATH
self.private_key_path = STACKIT_PRIVATE_KEY_PATH


def either_this_or_that(this: Any, that: Any) -> Optional[Any]:
return this if this else that


class Authorization:
DEFAULT_CREDENTIALS_FILE_PATH = ".stackit/credentials.json"
service_account_mail: Optional[str] = None
Expand All @@ -45,16 +42,26 @@ class Authorization:

def __init__(self, configuration: Configuration):
credentials = self.__read_credentials_file(configuration.credentials_file_path)
self.service_account_mail = either_this_or_that(
configuration.service_account_mail, credentials.service_account_mail
self.service_account_mail = (
configuration.service_account_mail
if configuration.service_account_mail is not None
else credentials.service_account_mail
)
self.service_account_token = (
configuration.service_account_token
if configuration.service_account_token is not None
else credentials.service_account_token
)
self.service_account_token = either_this_or_that(
configuration.service_account_token, credentials.service_account_token
self.service_account_key_path = (
configuration.service_account_key_path
if configuration.service_account_key_path is not None
else credentials.service_account_key_path
)
self.service_account_key_path = either_this_or_that(
configuration.service_account_key_path, credentials.service_account_key_path
self.private_key_path = (
configuration.private_key_path
if configuration.private_key_path is not None
else credentials.private_key_path
)
self.private_key_path = either_this_or_that(configuration.private_key_path, credentials.private_key_path)
self.auth_method = configuration.custom_auth
self.token_endpoint = configuration.token_endpoint
self.__read_keys()
Expand Down
23 changes: 22 additions & 1 deletion core/tests/core/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def credentials_file_json():
}"""


@pytest.fixture
def credentials_file_json_with_unused_arguments():
return """{
"STACKIT_SERVICE_ACCOUNT_EMAIL": "email",
"STACKIT_PRIVATE_KEY_PATH": "/path/to/private.key",
"STACKIT_SERVICE_ACCOUNT_TOKEN": "token",
"STACKIT_SERVICE_ACCOUNT_KEY_PATH": "/path/to/account.key",
"STACKIT_SERVICE_ACCOUNT_TOKEN_UNUSED": "unused"
}"""


@pytest.fixture
def service_account_key_file_json():
"""
Expand Down Expand Up @@ -160,6 +171,7 @@ def mock_open_function(


class TestAuth:

def test_token_auth_is_selected_when_token_is_given(self, empty_credentials_file_json):
with patch("builtins.open", mock_open(read_data=empty_credentials_file_json)):
config = Configuration(service_account_token="token")
Expand All @@ -186,9 +198,18 @@ def mockreturn():
auth = Authorization(config)
assert auth.auth_method is None

@pytest.mark.parametrize(
"credentials_file_json_fixture",
[
("credentials_file_json_with_unused_arguments"),
("credentials_file_json"),
],
)
def test_valid_credentials_file_is_parsed(
self, credentials_file_json, service_account_key_file_json, private_key_file
self, request, credentials_file_json_fixture, service_account_key_file_json, private_key_file
):
credentials_file_json = request.getfixturevalue(credentials_file_json_fixture)

with patch(
"builtins.open",
lambda filepath, *args, **kwargs: mock_open_function(
Expand Down
Loading