Skip to content

Commit 7b8ee11

Browse files
init arg should take priority over env var
Signed-off-by: Kevin <[email protected]>
1 parent 2f98a27 commit 7b8ee11

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/codeflare_sdk/cluster/auth.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
global config_path
3333
config_path = None
3434

35+
WORKBENCH_CA_CERT_PATH = "/etc/pki/tls/custom-certs/ca-bundle.crt"
36+
3537

3638
class Authentication(metaclass=abc.ABCMeta):
3739
"""
@@ -81,7 +83,7 @@ def __init__(
8183
token: str,
8284
server: str,
8385
skip_tls: bool = False,
84-
ca_cert_path: str = "/etc/pki/tls/custom-certs/ca-bundle.crt",
86+
ca_cert_path: str = None,
8587
):
8688
"""
8789
Initialize a TokenAuthentication object that requires a value for `token`, the API Token
@@ -91,7 +93,17 @@ def __init__(
9193
self.token = token
9294
self.server = server
9395
self.skip_tls = skip_tls
94-
self.ca_cert_path = ca_cert_path
96+
self.ca_cert_path = self._gen_ca_cert_path(ca_cert_path)
97+
98+
def _gen_ca_cert_path(self, ca_cert_path: str):
99+
if ca_cert_path is not None:
100+
return ca_cert_path
101+
elif "CF_SDK_CA_CERT_PATH" in os.environ:
102+
return os.environ.get("CF_SDK_CA_CERT_PATH")
103+
elif os.path.exists(WORKBENCH_CA_CERT_PATH):
104+
return WORKBENCH_CA_CERT_PATH
105+
else:
106+
return None
95107

96108
def login(self) -> str:
97109
"""
@@ -106,13 +118,9 @@ def login(self) -> str:
106118
configuration.api_key_prefix["authorization"] = "Bearer"
107119
configuration.host = self.server
108120
configuration.api_key["authorization"] = self.token
109-
ca_path_env = os.environ.get("CF_SDK_CA_CERT_PATH", self.ca_cert_path)
110-
111-
if self.skip_tls == False:
112-
if ca_path_env != self.ca_cert_path:
113-
self.ca_cert_path = ca_path_env
114121

115-
if self.ca_cert_path == None:
122+
if not self.skip_tls:
123+
if self.ca_cert_path is None:
116124
configuration.ssl_ca_cert = None
117125
elif os.path.isfile(self.ca_cert_path):
118126
print(

tests/unit_test.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,21 @@ def test_token_auth_creation():
123123
assert token_auth.token == "token"
124124
assert token_auth.server == "server"
125125
assert token_auth.skip_tls == False
126-
assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt"
126+
assert token_auth.ca_cert_path == None
127127

128128
token_auth = TokenAuthentication(token="token", server="server", skip_tls=True)
129129
assert token_auth.token == "token"
130130
assert token_auth.server == "server"
131131
assert token_auth.skip_tls == True
132-
assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt"
132+
assert token_auth.ca_cert_path == None
133133

134+
os.environ["CF_SDK_CA_CERT_PATH"] = f"/etc/pki/tls/custom-certs/ca-bundle.crt"
134135
token_auth = TokenAuthentication(token="token", server="server", skip_tls=False)
135136
assert token_auth.token == "token"
136137
assert token_auth.server == "server"
137138
assert token_auth.skip_tls == False
138139
assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt"
140+
os.environ.pop("CF_SDK_CA_CERT_PATH")
139141

140142
token_auth = TokenAuthentication(
141143
token="token",

0 commit comments

Comments
 (0)