Skip to content

feedback MR 7 - warn the user about unexpected env conflicts. #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/planet_auth_utils/commands/cli/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand All @@ -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.
Expand All @@ -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
3 changes: 2 additions & 1 deletion src/planet_auth_utils/plauth_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down