diff --git a/src/codeflare_sdk/cluster/auth.py b/src/codeflare_sdk/cluster/auth.py index 85db3d61d..92fa1549d 100644 --- a/src/codeflare_sdk/cluster/auth.py +++ b/src/codeflare_sdk/cluster/auth.py @@ -21,6 +21,8 @@ import abc from kubernetes import client, config +import os +from ..utils.kube_api_helpers import _kube_api_error_handling global api_client api_client = None @@ -161,8 +163,23 @@ def config_check() -> str: """ global config_path global api_client + home_directory = os.path.expanduser("~") if config_path == None and api_client == None: - config.load_kube_config() + if os.path.isfile("%s/.kube/config" % home_directory): + try: + config.load_kube_config() + except Exception as e: # pragma: no cover + _kube_api_error_handling(e) + elif "KUBERNETES_PORT" in os.environ: + try: + config.load_incluster_config() + except Exception as e: # pragma: no cover + _kube_api_error_handling(e) + else: + raise PermissionError( + "Action not permitted, have you put in correct/up-to-date auth credentials?" + ) + if config_path != None and api_client == None: return config_path diff --git a/tests/unit_test.py b/tests/unit_test.py index ac126016f..58f55bfa1 100644 --- a/tests/unit_test.py +++ b/tests/unit_test.py @@ -37,6 +37,8 @@ TokenAuthentication, Authentication, KubeConfigFileAuthentication, + config_check, + api_config_handler, ) from codeflare_sdk.utils.pretty_print import ( print_no_resources_found, @@ -150,6 +152,46 @@ def test_token_auth_login_tls(mocker): assert token_auth.login() == ("Logged into testserver:6443") +def test_config_check_no_config_file(mocker): + mocker.patch("os.path.expanduser", return_value="/mock/home/directory") + mocker.patch("os.path.isfile", return_value=False) + mocker.patch("codeflare_sdk.cluster.auth.config_path", None) + mocker.patch("codeflare_sdk.cluster.auth.api_client", None) + + with pytest.raises(PermissionError) as e: + config_check() + + +def test_config_check_with_incluster_config(mocker): + mocker.patch("os.path.expanduser", return_value="/mock/home/directory") + mocker.patch("os.path.isfile", return_value=False) + mocker.patch.dict(os.environ, {"KUBERNETES_PORT": "number"}) + mocker.patch("kubernetes.config.load_incluster_config", side_effect=None) + mocker.patch("codeflare_sdk.cluster.auth.config_path", None) + mocker.patch("codeflare_sdk.cluster.auth.api_client", None) + + result = config_check() + assert result == None + + +def test_config_check_with_existing_config_file(mocker): + mocker.patch("os.path.expanduser", return_value="/mock/home/directory") + mocker.patch("os.path.isfile", return_value=True) + mocker.patch("kubernetes.config.load_kube_config", side_effect=None) + mocker.patch("codeflare_sdk.cluster.auth.config_path", None) + mocker.patch("codeflare_sdk.cluster.auth.api_client", None) + + result = config_check() + assert result == None + + +def test_config_check_with_config_path_and_no_api_client(mocker): + mocker.patch("codeflare_sdk.cluster.auth.config_path", "/mock/config/path") + mocker.patch("codeflare_sdk.cluster.auth.api_client", None) + result = config_check() + assert result == "/mock/config/path" + + def test_load_kube_config(mocker): mocker.patch.object(config, "load_kube_config") kube_config_auth = KubeConfigFileAuthentication( @@ -162,6 +204,10 @@ def test_load_kube_config(mocker): == "Loaded user config file at path %s" % kube_config_auth.kube_config_path ) + kube_config_auth = KubeConfigFileAuthentication(kube_config_path=None) + response = kube_config_auth.load_kube_config() + assert response == "Please specify a config file path" + def test_auth_coverage(): abstract = Authentication()