Skip to content

Commit

Permalink
fix(core): Typing in auth (#691)
Browse files Browse the repository at this point in the history
Lets try and continue the journey from #305 
(the goal is to get core marked as typed)

```
poetry run mypy --config-file pyproject.toml core/tests/test_auth.py core/testcontainers/core/auth.py
Success: no issues found in 2 source files
```
  • Loading branch information
Tranquility2 authored Sep 8, 2024
1 parent 62bd0de commit 66726b6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
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 @@ def process_docker_auth_config_encoded(auth_config_dict: dict) -> list[DockerAut
auth_info: list[DockerAuthInfo] = []

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

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 @@ def process_docker_auth_config_cred_helpers(auth_config_dict: dict) -> None:
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 process_docker_auth_config_store(auth_config_dict: dict) -> None:
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

0 comments on commit 66726b6

Please sign in to comment.