From 8c832f195857967e220815caab43ede757a8e27f Mon Sep 17 00:00:00 2001 From: shabbywu Date: Fri, 26 Jan 2024 17:35:16 +0800 Subject: [PATCH] =?UTF-8?q?minor:=20=E9=99=8D=E4=BD=8E=20https=20=E6=8E=A2?= =?UTF-8?q?=E6=B5=8B=E7=9A=84=E9=BB=98=E8=AE=A4=E8=B6=85=E6=97=B6=E6=97=B6?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- moby_distribution/__init__.py | 2 +- moby_distribution/registry/client.py | 8 +++++--- moby_distribution/spec/endpoint.py | 9 ++------- pyproject.toml | 2 +- tests/spec/test_endpoint.py | 6 +++--- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/moby_distribution/__init__.py b/moby_distribution/__init__.py index 64fd988..ab52a04 100644 --- a/moby_distribution/__init__.py +++ b/moby_distribution/__init__.py @@ -7,7 +7,7 @@ from moby_distribution.spec.image_json import ImageJSON from moby_distribution.spec.manifest import ManifestSchema1, ManifestSchema2, OCIManifestSchema1 -__version__ = "0.5.8" +__version__ = "0.5.9" __ALL__ = [ "DockerRegistryV2Client", "Blob", diff --git a/moby_distribution/registry/client.py b/moby_distribution/registry/client.py index 16a6136..7da4ae8 100644 --- a/moby_distribution/registry/client.py +++ b/moby_distribution/registry/client.py @@ -28,10 +28,12 @@ def from_api_endpoint( username: Optional[str] = None, password: Optional[str] = None, authenticator_class: Type[BaseAuthentication] = UniversalAuthentication, + default_timeout: TypeTimeout = 60 * 10, + https_detect_timeout: float = 10, ): https_scheme = "https://" http_scheme = "http://" - enable_https, certificate_valid = api_endpoint.is_secure_repository() + enable_https, certificate_valid = api_endpoint.is_secure_repository(timeout=https_detect_timeout) if enable_https: client = cls( api_base_url=f"{https_scheme}{api_endpoint.api_base_url}", @@ -39,7 +41,7 @@ def from_api_endpoint( password=password, verify_certificate=certificate_valid, authenticator_class=authenticator_class, - default_timeout=api_endpoint.default_timeout, + default_timeout=default_timeout, ) if certificate_valid or client.ping(): return client @@ -49,7 +51,7 @@ def from_api_endpoint( password=password, verify_certificate=False, authenticator_class=authenticator_class, - default_timeout=api_endpoint.default_timeout, + default_timeout=default_timeout, ) def __init__( diff --git a/moby_distribution/spec/endpoint.py b/moby_distribution/spec/endpoint.py index 5976d79..7484b4e 100644 --- a/moby_distribution/spec/endpoint.py +++ b/moby_distribution/spec/endpoint.py @@ -1,7 +1,7 @@ import re import socket import ssl -from typing import Pattern, Tuple, Union +from typing import Optional, Pattern, Tuple from pydantic import BaseModel @@ -10,9 +10,8 @@ class APIEndpoint(BaseModel): version: int = 2 url: str official: bool = False - default_timeout: Union[Tuple[float, float], float] = 60 - def is_secure_repository(self) -> Tuple[bool, bool]: + def is_secure_repository(self, *, timeout: Optional[float] = None) -> Tuple[bool, bool]: """Detect if the repository is secure returns Tuple[bool, bool], the first one mean if the server support https?, @@ -25,10 +24,6 @@ def is_secure_repository(self) -> Tuple[bool, bool]: parts = match.groupdict() hostname = parts.get("domain") or parts.get("ipv4") or parts.get("ipv6") port = int(parts.get("port") or 443) - if isinstance(self.default_timeout, tuple): - timeout = sum(self.default_timeout) - else: - timeout = self.default_timeout try: context = ssl.create_default_context() connection = socket.create_connection((hostname, port), timeout=timeout) diff --git a/pyproject.toml b/pyproject.toml index 17f85cc..20e22d7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "moby-distribution" -version = "0.5.8" +version = "0.5.9" description = "Yet another moby(docker) distribution implement by python." authors = ["shabbywu "] license = "Apache-2.0" diff --git a/tests/spec/test_endpoint.py b/tests/spec/test_endpoint.py index db125af..ca59539 100644 --- a/tests/spec/test_endpoint.py +++ b/tests/spec/test_endpoint.py @@ -132,9 +132,9 @@ def test_is_secure_repository(server, expected): def test_is_secure_repository_timeout(blocking_https_server): with pytest.raises(socket.timeout): - assert APIEndpoint( - url=f"{blocking_https_server[0]}:{blocking_https_server[1]}", default_timeout=10 - ).is_secure_repository() + assert APIEndpoint(url=f"{blocking_https_server[0]}:{blocking_https_server[1]}").is_secure_repository( + timeout=10.0 + ) @pytest.mark.parametrize(