diff --git a/src/planet_auth_utils/commands/cli/prompts.py b/src/planet_auth_utils/commands/cli/prompts.py index e667998..bbfc7d9 100644 --- a/src/planet_auth_utils/commands/cli/prompts.py +++ b/src/planet_auth_utils/commands/cli/prompts.py @@ -16,16 +16,41 @@ # We use prompt_toolkit for major prompts # and click or simple code for others. +import os import click from typing import Optional # from prompt_toolkit.shortcuts import input_dialog, radiolist_dialog, yes_no_dialog -# from planet_auth_utils.builtins import Builtins from planet_auth_utils.plauth_user_config import PlanetAuthUserConfigEnhanced from planet_auth_utils.constants import EnvironmentVariables +def _warn_env_overrides_selection(selected_profile_name: str): + # If we detect a current environment that we expect will override + # what is being saved to the config file, warn the user. + # See https://github.com/pylint-dev/pylint/issues/5091 regarding the pylint disabled warning. + env_profile = os.getenv(EnvironmentVariables.AUTH_PROFILE, None) # pylint: disable=E1507 + if env_profile and str.lower(env_profile) != str.lower(selected_profile_name): + print( + f'Warning: Environment variable "{EnvironmentVariables.AUTH_PROFILE}" is set to "{env_profile}". This will override saved settings.' + ) + + env_client_id = os.getenv(EnvironmentVariables.AUTH_CLIENT_ID, None) # pylint: disable=E1507 + env_client_secret = os.getenv(EnvironmentVariables.AUTH_CLIENT_SECRET, None) # pylint: disable=E1507 + if env_client_id and env_client_secret: + print( + f'Warning: Environment variables "{EnvironmentVariables.AUTH_CLIENT_ID}" and "{EnvironmentVariables.AUTH_CLIENT_SECRET}" are set.' + " These will always take priority over the saved settings." + ) + + env_api_key = os.getenv(EnvironmentVariables.AUTH_API_KEY, None) # pylint: disable=E1507 + if env_api_key: # and str.lower(_PL_API_KEY_ADHOC_PROFILE_NAME) != str.lower(selected_profile_name): + print( + f'Warning: Environment variable "{EnvironmentVariables.AUTH_API_KEY}" is set. This will always take priority over the saved settings.' + ) + + def prompt_and_change_user_default_profile_if_different( candidate_profile_name: str, change_default_selection: Optional[bool] = None ): @@ -34,6 +59,7 @@ def prompt_and_change_user_default_profile_if_different( saved_profile_name = config_file.lazy_get(EnvironmentVariables.AUTH_PROFILE) except FileNotFoundError: saved_profile_name = None # config_file.effective_conf_value(EnvironmentVariables.AUTH_PROFILE, fallback_value=Builtins.builtin_default_profile_name()) + selected_profile_name = saved_profile_name if not saved_profile_name: # Always write a preference if none is saved. @@ -56,7 +82,9 @@ def prompt_and_change_user_default_profile_if_different( ) if do_change_default: + selected_profile_name = candidate_profile_name config_file.update_data({EnvironmentVariables.AUTH_PROFILE: candidate_profile_name}) config_file.save() + _warn_env_overrides_selection(selected_profile_name) return do_change_default diff --git a/src/planet_auth_utils/plauth_factory.py b/src/planet_auth_utils/plauth_factory.py index 96c045c..e7ba187 100644 --- a/src/planet_auth_utils/plauth_factory.py +++ b/src/planet_auth_utils/plauth_factory.py @@ -38,6 +38,7 @@ from planet_auth.logging.auth_logger import getAuthLogger auth_logger = getAuthLogger() +_PL_API_KEY_ADHOC_PROFILE_NAME = "_PL_API_KEY" class PlanetAuthFactory: @@ -218,7 +219,7 @@ def _init_context_from_api_key(api_key: str) -> Auth: "api_key": api_key, "bearer_token_prefix": PlanetLegacyRequestAuthenticator.TOKEN_PREFIX, } - adhoc_profile_name = "_PL_API_KEY" + adhoc_profile_name = _PL_API_KEY_ADHOC_PROFILE_NAME auth_logger.debug(msg="Initializing Auth from API key") plauth_context = Auth.initialize_from_config_dict( client_config=constructed_client_config_dict,