Skip to content

Commit

Permalink
init arg should take priority over env var
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin <[email protected]>
  • Loading branch information
KPostOffice authored and openshift-merge-bot[bot] committed May 23, 2024
1 parent 2f98a27 commit 7b8ee11
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
24 changes: 16 additions & 8 deletions src/codeflare_sdk/cluster/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
global config_path
config_path = None

WORKBENCH_CA_CERT_PATH = "/etc/pki/tls/custom-certs/ca-bundle.crt"


class Authentication(metaclass=abc.ABCMeta):
"""
Expand Down Expand Up @@ -81,7 +83,7 @@ def __init__(
token: str,
server: str,
skip_tls: bool = False,
ca_cert_path: str = "/etc/pki/tls/custom-certs/ca-bundle.crt",
ca_cert_path: str = None,
):
"""
Initialize a TokenAuthentication object that requires a value for `token`, the API Token
Expand All @@ -91,7 +93,17 @@ def __init__(
self.token = token
self.server = server
self.skip_tls = skip_tls
self.ca_cert_path = ca_cert_path
self.ca_cert_path = self._gen_ca_cert_path(ca_cert_path)

def _gen_ca_cert_path(self, ca_cert_path: str):
if ca_cert_path is not None:
return ca_cert_path
elif "CF_SDK_CA_CERT_PATH" in os.environ:
return os.environ.get("CF_SDK_CA_CERT_PATH")
elif os.path.exists(WORKBENCH_CA_CERT_PATH):
return WORKBENCH_CA_CERT_PATH
else:
return None

def login(self) -> str:
"""
Expand All @@ -106,13 +118,9 @@ def login(self) -> str:
configuration.api_key_prefix["authorization"] = "Bearer"
configuration.host = self.server
configuration.api_key["authorization"] = self.token
ca_path_env = os.environ.get("CF_SDK_CA_CERT_PATH", self.ca_cert_path)

if self.skip_tls == False:
if ca_path_env != self.ca_cert_path:
self.ca_cert_path = ca_path_env

if self.ca_cert_path == None:
if not self.skip_tls:
if self.ca_cert_path is None:
configuration.ssl_ca_cert = None
elif os.path.isfile(self.ca_cert_path):
print(
Expand Down
6 changes: 4 additions & 2 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,21 @@ def test_token_auth_creation():
assert token_auth.token == "token"
assert token_auth.server == "server"
assert token_auth.skip_tls == False
assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt"
assert token_auth.ca_cert_path == None

token_auth = TokenAuthentication(token="token", server="server", skip_tls=True)
assert token_auth.token == "token"
assert token_auth.server == "server"
assert token_auth.skip_tls == True
assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt"
assert token_auth.ca_cert_path == None

os.environ["CF_SDK_CA_CERT_PATH"] = f"/etc/pki/tls/custom-certs/ca-bundle.crt"
token_auth = TokenAuthentication(token="token", server="server", skip_tls=False)
assert token_auth.token == "token"
assert token_auth.server == "server"
assert token_auth.skip_tls == False
assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt"
os.environ.pop("CF_SDK_CA_CERT_PATH")

token_auth = TokenAuthentication(
token="token",
Expand Down

0 comments on commit 7b8ee11

Please sign in to comment.