16
16
import requests
17
17
18
18
import oras .auth
19
+ import oras .auth .utils
19
20
import oras .container
20
21
import oras .decorator as decorator
21
22
import oras .defaults
@@ -84,6 +85,10 @@ def __init__(
84
85
auth_backend , self .session , insecure , tls_verify = tls_verify
85
86
)
86
87
88
+ # Load all authentication configs once during initialization
89
+ # This avoids re-reading the docker config file for each operation
90
+ self .auth ._auths = oras .auth .utils .load_configs ()
91
+
87
92
def __repr__ (self ) -> str :
88
93
return str (self )
89
94
@@ -249,6 +254,8 @@ def _parse_manifest_ref(self, ref: str) -> Tuple[str, str]:
249
254
path_content .content = oras .defaults .unknown_config_media_type
250
255
return path_content .path , path_content .content
251
256
257
+ @decorator .ensure_container (1 )
258
+ @decorator .ensure_auth (1 )
252
259
def upload_blob (
253
260
self ,
254
261
blob : str ,
@@ -276,7 +283,6 @@ def upload_blob(
276
283
:type chunk_size: int
277
284
"""
278
285
blob = os .path .abspath (blob )
279
- container = self .get_container (container )
280
286
281
287
if self .blob_exists (layer , container ):
282
288
logger .debug (f'layer already exists: { layer ["digest" ]} ' )
@@ -306,7 +312,8 @@ def upload_blob(
306
312
response .status_code = 200
307
313
return response
308
314
309
- @decorator .ensure_container
315
+ @decorator .ensure_container ()
316
+ @decorator .ensure_auth ()
310
317
def delete_tag (self , container : container_type , tag : str ) -> bool :
311
318
"""
312
319
Delete a tag for a container.
@@ -340,7 +347,8 @@ def delete_tag(self, container: container_type, tag: str) -> bool:
340
347
raise RuntimeError ("Delete was not successful: {response.json()}" )
341
348
return True
342
349
343
- @decorator .ensure_container
350
+ @decorator .ensure_container ()
351
+ @decorator .ensure_auth ()
344
352
def get_tags (self , container : container_type , N = None ) -> List [str ]:
345
353
"""
346
354
Retrieve tags for a package.
@@ -404,7 +412,8 @@ def _do_paginated_request(
404
412
# use link + base url to continue with next page
405
413
url = urllib .parse .urljoin (base_url , link )
406
414
407
- @decorator .ensure_container
415
+ @decorator .ensure_container ()
416
+ @decorator .ensure_auth ()
408
417
def get_blob (
409
418
self ,
410
419
container : container_type ,
@@ -440,7 +449,8 @@ def get_container(self, name: container_type) -> oras.container.Container:
440
449
return oras .container .Container (name , registry = self .hostname )
441
450
442
451
# Functions to be deprecated in favor of exposed ones
443
- @decorator .ensure_container
452
+ @decorator .ensure_container ()
453
+ @decorator .ensure_auth ()
444
454
def _download_blob (
445
455
self , container : container_type , digest : str , outfile : str
446
456
) -> str :
@@ -485,7 +495,8 @@ def _upload_blob(
485
495
)
486
496
return self .upload_blob (blob , container , layer , do_chunked )
487
497
488
- @decorator .ensure_container
498
+ @decorator .ensure_container ()
499
+ @decorator .ensure_auth ()
489
500
def download_blob (
490
501
self , container : container_type , digest : str , outfile : str
491
502
) -> str :
@@ -516,6 +527,8 @@ def download_blob(
516
527
raise e
517
528
return outfile
518
529
530
+ @decorator .ensure_container (1 )
531
+ @decorator .ensure_auth (1 )
519
532
def put_upload (
520
533
self ,
521
534
blob : str ,
@@ -561,6 +574,8 @@ def put_upload(
561
574
)
562
575
return response
563
576
577
+ @decorator .ensure_container (1 )
578
+ @decorator .ensure_auth (1 )
564
579
def blob_exists (self , layer : dict , container : oras .container .Container ) -> bool :
565
580
"""
566
581
Check if a layer already exists in the registry.
@@ -600,6 +615,8 @@ def _get_location(
600
615
session_url = f"{ prefix } { session_url } "
601
616
return session_url
602
617
618
+ @decorator .ensure_container (1 )
619
+ @decorator .ensure_auth (1 )
603
620
def chunked_upload (
604
621
self ,
605
622
blob : str ,
@@ -688,6 +705,8 @@ def _parse_response_errors(self, response: requests.Response):
688
705
except Exception :
689
706
pass
690
707
708
+ @decorator .ensure_container (1 )
709
+ @decorator .ensure_auth (1 )
691
710
def upload_manifest (
692
711
self ,
693
712
manifest : dict ,
@@ -751,9 +770,13 @@ def push(
751
770
"""
752
771
container = self .get_container (target )
753
772
files = files or []
754
- self .auth .load_configs (
755
- container , configs = [config_path ] if config_path else None
756
- )
773
+
774
+ # If a custom config path is provided, load those configs
775
+ if config_path :
776
+ self .auth .load_configs (container , configs = [config_path ])
777
+ else :
778
+ # Use the already loaded auths with ensure_auth pattern
779
+ self .auth .ensure_auth_for_container (container )
757
780
758
781
# Prepare a new manifest
759
782
manifest = oras .oci .NewManifest ()
@@ -891,9 +914,13 @@ def pull(
891
914
:type target: str
892
915
"""
893
916
container = self .get_container (target )
894
- self .auth .load_configs (
895
- container , configs = [config_path ] if config_path else None
896
- )
917
+
918
+ # If a custom config path is provided, load those configs
919
+ if config_path :
920
+ self .auth .load_configs (container , configs = [config_path ])
921
+ else :
922
+ # Use the already loaded auths with ensure_auth pattern
923
+ self .auth .ensure_auth_for_container (container )
897
924
manifest = self .get_manifest (container , allowed_media_type )
898
925
outdir = outdir or oras .utils .get_tmpdir ()
899
926
overwrite = overwrite
@@ -932,7 +959,8 @@ def pull(
932
959
files .append (outfile )
933
960
return files
934
961
935
- @decorator .ensure_container
962
+ @decorator .ensure_container ()
963
+ @decorator .ensure_auth ()
936
964
def get_manifest (
937
965
self ,
938
966
container : container_type ,
@@ -946,10 +974,6 @@ def get_manifest(
946
974
:param allowed_media_type: one or more allowed media types
947
975
:type allowed_media_type: str
948
976
"""
949
- # Load authentication configs for the container's registry
950
- # This ensures credentials are available for authenticated registries
951
- self .auth .load_configs (container )
952
-
953
977
if not allowed_media_type :
954
978
allowed_media_type = [oras .defaults .default_manifest_media_type ]
955
979
headers = {"Accept" : ";" .join (allowed_media_type )}
0 commit comments