1
1
import time
2
2
from io import BytesIO
3
3
from tarfile import TarFile , TarInfo
4
- from typing import TYPE_CHECKING , Optional
4
+ from typing import TYPE_CHECKING , Any , Optional
5
5
6
6
import bcrypt
7
7
from requests import get
@@ -25,7 +25,7 @@ def __init__(
25
25
port : int = 5000 ,
26
26
username : Optional [str ] = None ,
27
27
password : Optional [str ] = None ,
28
- ** kwargs ,
28
+ ** kwargs : Any ,
29
29
) -> None :
30
30
super ().__init__ (image = image , ** kwargs )
31
31
self .port : int = port
@@ -35,6 +35,8 @@ def __init__(
35
35
36
36
def _copy_credentials (self ) -> None :
37
37
# Create credentials and write them to the container
38
+ if self .password is None :
39
+ raise ValueError ("Password cannot be None" )
38
40
hashed_password : str = bcrypt .hashpw (
39
41
self .password .encode ("utf-8" ),
40
42
bcrypt .gensalt (rounds = 12 , prefix = b"2a" ),
@@ -44,7 +46,7 @@ def _copy_credentials(self) -> None:
44
46
with BytesIO () as tar_archive_object , TarFile (fileobj = tar_archive_object , mode = "w" ) as tmp_tarfile :
45
47
tarinfo : TarInfo = TarInfo (name = self .credentials_path )
46
48
tarinfo .size = len (content )
47
- tarinfo .mtime = time .time ()
49
+ tarinfo .mtime = int ( time .time () )
48
50
49
51
tmp_tarfile .addfile (tarinfo , BytesIO (content ))
50
52
tar_archive_object .seek (0 )
@@ -54,12 +56,13 @@ def _copy_credentials(self) -> None:
54
56
def _readiness_probe (self ) -> None :
55
57
url : str = f"http://{ self .get_registry ()} /v2"
56
58
if self .username and self .password :
57
- response : Response = get (url , auth = HTTPBasicAuth (self .username , self .password ), timeout = 1 )
59
+ auth_response : Response = get (url , auth = HTTPBasicAuth (self .username , self .password ), timeout = 1 )
60
+ auth_response .raise_for_status ()
58
61
else :
59
62
response : Response = get (url , timeout = 1 )
60
- response .raise_for_status ()
63
+ response .raise_for_status ()
61
64
62
- def start (self ):
65
+ def start (self ) -> "DockerRegistryContainer" :
63
66
if self .username and self .password :
64
67
self .with_env ("REGISTRY_AUTH_HTPASSWD_REALM" , "local-registry" )
65
68
self .with_env ("REGISTRY_AUTH_HTPASSWD_PATH" , self .credentials_path )
0 commit comments