Skip to content
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

fix(core): Typing in auth #691

Merged
merged 3 commits into from
Sep 8, 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
15 changes: 9 additions & 6 deletions core/testcontainers/core/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json as json
from collections import namedtuple
from logging import warning
from typing import Optional
from typing import Any, Optional

DockerAuthInfo = namedtuple("DockerAuthInfo", ["registry", "username", "password"])

Expand All @@ -12,7 +12,7 @@
}


def process_docker_auth_config_encoded(auth_config_dict: dict) -> list[DockerAuthInfo]:
def process_docker_auth_config_encoded(auth_config_dict: dict[str, dict[str, dict[str, Any]]]) -> list[DockerAuthInfo]:
"""
Process the auths config.

Expand All @@ -30,16 +30,19 @@
auth_info: list[DockerAuthInfo] = []

auths = auth_config_dict.get("auths")
if not auths:
raise KeyError("No auths found in the docker auth config")

Check warning on line 34 in core/testcontainers/core/auth.py

View check run for this annotation

Codecov / codecov/patch

core/testcontainers/core/auth.py#L34

Added line #L34 was not covered by tests

for registry, auth in auths.items():
auth_str = auth.get("auth")
auth_str = str(auth.get("auth"))
auth_str = base64.b64decode(auth_str).decode("utf-8")
username, password = auth_str.split(":")
auth_info.append(DockerAuthInfo(registry, username, password))

return auth_info


def process_docker_auth_config_cred_helpers(auth_config_dict: dict) -> None:
def process_docker_auth_config_cred_helpers(auth_config_dict: dict[str, Any]) -> None:
"""
Process the credHelpers config.

Expand All @@ -56,7 +59,7 @@
warning(_AUTH_WARNINGS.pop("credHelpers"))


def process_docker_auth_config_store(auth_config_dict: dict) -> None:
def process_docker_auth_config_store(auth_config_dict: dict[str, Any]) -> None:
"""
Process the credsStore config.

Expand All @@ -74,7 +77,7 @@
def parse_docker_auth_config(auth_config: str) -> Optional[list[DockerAuthInfo]]:
"""Parse the docker auth config from a string and handle the different formats."""
try:
auth_config_dict: dict = json.loads(auth_config)
auth_config_dict: dict[str, Any] = json.loads(auth_config)
if "credHelpers" in auth_config:
process_docker_auth_config_cred_helpers(auth_config_dict)
if "credsStore" in auth_config:
Expand Down
2 changes: 2 additions & 0 deletions core/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
def test_parse_docker_auth_config_encoded():
auth_config_json = '{"auths":{"https://index.docker.io/v1/":{"auth":"dXNlcm5hbWU6cGFzc3dvcmQ="}}}'
auth_info = parse_docker_auth_config(auth_config_json)
assert auth_info
assert len(auth_info) == 1
assert auth_info[0] == DockerAuthInfo(
registry="https://index.docker.io/v1/",
Expand Down Expand Up @@ -37,6 +38,7 @@ def test_parse_docker_auth_config_encoded_multiple():
}
auth_config_json = json.dumps(auth_dict)
auth_info = parse_docker_auth_config(auth_config_json)
assert auth_info
assert len(auth_info) == 3
assert auth_info[0] == DockerAuthInfo(
registry="localhost:5000",
Expand Down