Skip to content

Commit

Permalink
minor: 降低 https 探测的默认超时时间
Browse files Browse the repository at this point in the history
  • Loading branch information
shabbywu committed Jan 26, 2024
1 parent 4f2bb06 commit 8c832f1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion moby_distribution/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions moby_distribution/registry/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ 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}",
username=username,
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
Expand All @@ -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__(
Expand Down
9 changes: 2 additions & 7 deletions moby_distribution/spec/endpoint.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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?,
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "Apache-2.0"
Expand Down
6 changes: 3 additions & 3 deletions tests/spec/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8c832f1

Please sign in to comment.