Skip to content

Commit 89be82f

Browse files
committed
Use oras registry_config and registry url parse to finalize login
1 parent 42dbb01 commit 89be82f

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

charon/config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def __init__(self, data: Dict):
3535
self.__client_key: str = data.get("client_key", None)
3636
self.__client_key_pass_file: str = data.get("client_key_pass_file", None)
3737
self.__root_ca: str = data.get("root_ca", "/etc/pki/tls/certs/ca-bundle.crt")
38+
self.__quay_radas_registry_config: str = data.get(
39+
"quay_radas_registry_config", os.path.join(os.getenv("HOME", ""), ".oras/config.json")
40+
)
3841
self.__radas_sign_timeout_retry_count: int = data.get("radas_sign_timeout_retry_count", 10)
3942
self.__radas_sign_timeout_retry_interval: int = data.get(
4043
"radas_sign_timeout_retry_interval", 60
@@ -62,6 +65,11 @@ def validate(self) -> bool:
6265
if self.__root_ca and not os.access(self.__root_ca, os.R_OK):
6366
logger.error("The root ca file is not valid!")
6467
return False
68+
if self.__quay_radas_registry_config and not os.access(
69+
self.__quay_radas_registry_config, os.R_OK
70+
):
71+
logger.error("The quay registry config for oras is not valid!")
72+
return False
6573
return True
6674

6775
def umb_target(self) -> str:
@@ -91,6 +99,9 @@ def client_key_password(self) -> str:
9199
def root_ca(self) -> str:
92100
return self.__root_ca
93101

102+
def quay_radas_registry_config(self) -> str:
103+
return self.__quay_radas_registry_config
104+
94105
def radas_sign_timeout_retry_count(self) -> int:
95106
return self.__radas_sign_timeout_retry_count
96107

charon/pkgs/oras_client.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import logging
1919
from charon.config import get_config
2020
from typing import List
21+
from urllib.parse import urlparse
2122

2223
logger = logging.getLogger(__name__)
2324

@@ -31,21 +32,24 @@ def __init__(self):
3132
self.conf = get_config()
3233
self.client = oras.client.OrasClient()
3334

34-
def login_if_needed(self) -> None:
35+
def login_if_needed(self, registry: str) -> None:
3536
"""
36-
If quay_radas_auth_enabled is true, call login to authenticate.
37+
If quay_radas_registry_config is provided, call login to authenticate.
3738
"""
39+
if not registry.startswith("http://") and not registry.startswith("https://"):
40+
registry = "https://" + registry
41+
registry = urlparse(registry).netloc
3842

39-
if self.conf and self.conf.is_quay_radas_auth_enabled():
40-
logger.info("Logging in to registry.")
43+
rconf = self.conf.get_radas_config() if self.conf else None
44+
if rconf and rconf.quay_radas_registry_config():
45+
logger.info("Logging in to registry: %s", registry)
4146
res = self.client.login(
42-
hostname=self.conf.get_quay_radas_registry(),
43-
username=self.conf.get_quay_radas_username(),
44-
password=self.conf.get_quay_radas_password(),
47+
hostname=registry,
48+
config_path=rconf.quay_radas_registry_config(),
4549
)
4650
logger.info(res)
4751
else:
48-
logger.info("Registry auth not enabled, skip login.")
52+
logger.info("Registry config is not provided, skip login.")
4953

5054
def pull(self, result_reference_url: str, sign_result_loc: str) -> List[str]:
5155
"""
@@ -58,7 +62,7 @@ def pull(self, result_reference_url: str, sign_result_loc: str) -> List[str]:
5862
"""
5963
files = []
6064
try:
61-
self.login_if_needed()
65+
self.login_if_needed(registry=result_reference_url)
6266
files = self.client.pull(target=result_reference_url, outdir=sign_result_loc)
6367
logger.info("Pull file from %s to %s", result_reference_url, sign_result_loc)
6468
except Exception as e:

0 commit comments

Comments
 (0)