Skip to content

Commit 53e9192

Browse files
Bobbins228KPostOffice
authored andcommitted
Added env variable option for setting ca-cert path
1 parent 46c54f5 commit 53e9192

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

Diff for: src/codeflare_sdk/cluster/auth.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(
8181
token: str,
8282
server: str,
8383
skip_tls: bool = False,
84-
ca_cert_path: str = None,
84+
ca_cert_path: str = "/etc/pki/tls/custom-certs/ca-bundle.crt",
8585
):
8686
"""
8787
Initialize a TokenAuthentication object that requires a value for `token`, the API Token
@@ -101,19 +101,29 @@ def login(self) -> str:
101101
"""
102102
global config_path
103103
global api_client
104-
odh_ca_path = "/etc/pki/tls/custom-certs/ca-bundle.crt"
105104
try:
106105
configuration = client.Configuration()
107106
configuration.api_key_prefix["authorization"] = "Bearer"
108107
configuration.host = self.server
109108
configuration.api_key["authorization"] = self.token
110-
if self.skip_tls == False and self.ca_cert_path == None:
111-
if os.path.isfile(odh_ca_path):
112-
print(f"Authenticated with certificate located at {odh_ca_path}")
113-
configuration.ssl_ca_cert = odh_ca_path
109+
ca_path_env = os.environ.get("CA_CERT_PATH")
110+
111+
if self.skip_tls == False:
112+
if ca_path_env != None:
113+
self.ca_cert_path = ca_path_env
114+
115+
if self.ca_cert_path == None:
116+
configuration.ssl_ca_cert = None
117+
elif os.path.isfile(self.ca_cert_path):
118+
print(
119+
f"Authenticated with certificate located at {self.ca_cert_path}"
120+
)
121+
configuration.ssl_ca_cert = self.ca_cert_path
122+
else:
123+
raise FileNotFoundError(
124+
f"Certificate file not found at {self.ca_cert_path}"
125+
)
114126
configuration.verify_ssl = True
115-
elif self.skip_tls == False:
116-
configuration.ssl_ca_cert = self.ca_cert_path
117127
else:
118128
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
119129
print("Insecure request warnings have been disabled")

Diff for: tests/auth-test.crt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@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3Isqu2XQhEVaBFvj8Yu0h83e3D8LeQToC3mVMF4yy5BZ9Ty
18+
K66YGlGQgszWEUFPEdsB8Dj/iJMhkWXuyc3u/w0s3t7rXeMYYgr+xrEeK+g0oyB5
19+
xeZuMjd567Znmu5oMw==
20+
-----END CERTIFICATE-----

Diff for: tests/unit_test.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,30 @@ 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 == None
126+
assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt"
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 == None
132+
assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt"
133133

134134
token_auth = TokenAuthentication(token="token", server="server", skip_tls=False)
135135
assert token_auth.token == "token"
136136
assert token_auth.server == "server"
137137
assert token_auth.skip_tls == False
138-
assert token_auth.ca_cert_path == None
138+
assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt"
139139

140140
token_auth = TokenAuthentication(
141-
token="token", server="server", skip_tls=False, ca_cert_path="path/to/cert"
141+
token="token",
142+
server="server",
143+
skip_tls=False,
144+
ca_cert_path=f"{parent}/tests/auth-test.crt",
142145
)
143146
assert token_auth.token == "token"
144147
assert token_auth.server == "server"
145148
assert token_auth.skip_tls == False
146-
assert token_auth.ca_cert_path == "path/to/cert"
149+
assert token_auth.ca_cert_path == f"{parent}/tests/auth-test.crt"
147150

148151
except Exception:
149152
assert 0 == 1
@@ -174,7 +177,15 @@ def test_token_auth_login_tls(mocker):
174177
token="testtoken",
175178
server="testserver:6443",
176179
skip_tls=False,
177-
ca_cert_path="path/to/cert",
180+
ca_cert_path=f"{parent}/tests/auth-test.crt",
181+
)
182+
assert token_auth.login() == ("Logged into testserver:6443")
183+
184+
os.environ["CA_CERT_PATH"] = f"{parent}/tests/auth-test.crt"
185+
token_auth = TokenAuthentication(
186+
token="testtoken",
187+
server="testserver:6443",
188+
skip_tls=False,
178189
)
179190
assert token_auth.login() == ("Logged into testserver:6443")
180191

0 commit comments

Comments
 (0)