-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Improve private registry support (tolerate not implemented…
… fields in DOCKER_AUTH_CONFIG) (#647) Continuing #562, got some feedback regarding an issue with unsupported use cases. In this PR we will try to: 1. Map the use cases 2. Raise a warning regarding unsupported uses cases (hopefully they will be added later) 3. Address/Fix the issue where unsupported JSON schema for `DOCKER_AUTH_CONFIG` leads to an error As always any feedback will be much appreciated. _Please note this PR does not implement all use-cases just does a better job at preparing and handling them for now_
- Loading branch information
1 parent
df07586
commit 766c382
Showing
6 changed files
with
232 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import base64 as base64 | ||
import json as json | ||
from collections import namedtuple | ||
from logging import warning | ||
from typing import Optional | ||
|
||
DockerAuthInfo = namedtuple("DockerAuthInfo", ["registry", "username", "password"]) | ||
|
||
_AUTH_WARNINGS = { | ||
"credHelpers": "DOCKER_AUTH_CONFIG is experimental, credHelpers not supported yet", | ||
"credsStore": "DOCKER_AUTH_CONFIG is experimental, credsStore not supported yet", | ||
} | ||
|
||
|
||
def process_docker_auth_config_encoded(auth_config_dict: dict) -> list[DockerAuthInfo]: | ||
""" | ||
Process the auths config. | ||
Example: | ||
{ | ||
"auths": { | ||
"https://index.docker.io/v1/": { | ||
"auth": "dXNlcm5hbWU6cGFzc3dvcmQ=" | ||
} | ||
} | ||
} | ||
Returns a list of DockerAuthInfo objects. | ||
""" | ||
auth_info: list[DockerAuthInfo] = [] | ||
|
||
auths = auth_config_dict.get("auths") | ||
for registry, auth in auths.items(): | ||
auth_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: | ||
""" | ||
Process the credHelpers config. | ||
Example: | ||
{ | ||
"credHelpers": { | ||
"<aws_account_id>.dkr.ecr.<region>.amazonaws.com": "ecr-login" | ||
} | ||
} | ||
This is not supported yet. | ||
""" | ||
if "credHelpers" in _AUTH_WARNINGS: | ||
warning(_AUTH_WARNINGS.pop("credHelpers")) | ||
|
||
|
||
def process_docker_auth_config_store(auth_config_dict: dict) -> None: | ||
""" | ||
Process the credsStore config. | ||
Example: | ||
{ | ||
"credsStore": "ecr-login" | ||
} | ||
This is not supported yet. | ||
""" | ||
if "credsStore" in _AUTH_WARNINGS: | ||
warning(_AUTH_WARNINGS.pop("credsStore")) | ||
|
||
|
||
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) | ||
if "credHelpers" in auth_config: | ||
process_docker_auth_config_cred_helpers(auth_config_dict) | ||
if "credsStore" in auth_config: | ||
process_docker_auth_config_store(auth_config_dict) | ||
if "auths" in auth_config: | ||
return process_docker_auth_config_encoded(auth_config_dict) | ||
|
||
except (json.JSONDecodeError, KeyError, ValueError) as exp: | ||
raise ValueError("Could not parse docker auth config") from exp | ||
|
||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import json | ||
import pytest | ||
|
||
from testcontainers.core.auth import parse_docker_auth_config, DockerAuthInfo | ||
|
||
|
||
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 len(auth_info) == 1 | ||
assert auth_info[0] == DockerAuthInfo( | ||
registry="https://index.docker.io/v1/", | ||
username="username", | ||
password="password", | ||
) | ||
|
||
|
||
def test_parse_docker_auth_config_cred_helpers(): | ||
auth_dict = {"credHelpers": {"<aws_account_id>.dkr.ecr.<region>.amazonaws.com": "ecr-login"}} | ||
auth_config_json = json.dumps(auth_dict) | ||
assert parse_docker_auth_config(auth_config_json) is None | ||
|
||
|
||
def test_parse_docker_auth_config_store(): | ||
auth_dict = {"credsStore": "ecr-login"} | ||
auth_config_json = json.dumps(auth_dict) | ||
assert parse_docker_auth_config(auth_config_json) is None | ||
|
||
|
||
def test_parse_docker_auth_config_encoded_multiple(): | ||
auth_dict = { | ||
"auths": { | ||
"localhost:5000": {"auth": "dXNlcjE6cGFzczE=="}, | ||
"https://example.com": {"auth": "dXNlcl9uZXc6cGFzc19uZXc=="}, | ||
"example2.com": {"auth": "YWJjOjEyMw==="}, | ||
} | ||
} | ||
auth_config_json = json.dumps(auth_dict) | ||
auth_info = parse_docker_auth_config(auth_config_json) | ||
assert len(auth_info) == 3 | ||
assert auth_info[0] == DockerAuthInfo( | ||
registry="localhost:5000", | ||
username="user1", | ||
password="pass1", | ||
) | ||
assert auth_info[1] == DockerAuthInfo( | ||
registry="https://example.com", | ||
username="user_new", | ||
password="pass_new", | ||
) | ||
assert auth_info[2] == DockerAuthInfo( | ||
registry="example2.com", | ||
username="abc", | ||
password="123", | ||
) | ||
|
||
|
||
def test_parse_docker_auth_config_unknown(): | ||
auth_config_str = '{"key": "value"}' | ||
assert parse_docker_auth_config(auth_config_str) is None | ||
|
||
|
||
def test_parse_docker_auth_config_error(): | ||
auth_config_str = "bad//string" | ||
with pytest.raises(ValueError): | ||
parse_docker_auth_config(auth_config_str) | ||
|
||
|
||
def test_parse_docker_auth_all(): | ||
test_dict = { | ||
"auths": { | ||
"localhost:5000": {"auth": "dXNlcjE6cGFzczE=="}, | ||
}, | ||
"credHelpers": {"<aws_account_id>.dkr.ecr.<region>.amazonaws.com": "ecr-login"}, | ||
"credsStore": "ecr-login", | ||
} | ||
auth_config_json = json.dumps(test_dict) | ||
assert parse_docker_auth_config(auth_config_json) == [ | ||
DockerAuthInfo( | ||
registry="localhost:5000", | ||
username="user1", | ||
password="pass1", | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.