Skip to content

Commit 34e3262

Browse files
feedback MR 7 - warn the user about unexpected env conflicts. (#36)
* warn the user about unexpected env conflicts.
1 parent fe78e93 commit 34e3262

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/planet_auth_utils/commands/cli/prompts.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,41 @@
1616
# We use prompt_toolkit for major prompts
1717
# and click or simple code for others.
1818

19+
import os
1920
import click
2021
from typing import Optional
2122

2223
# from prompt_toolkit.shortcuts import input_dialog, radiolist_dialog, yes_no_dialog
2324

24-
# from planet_auth_utils.builtins import Builtins
2525
from planet_auth_utils.plauth_user_config import PlanetAuthUserConfigEnhanced
2626
from planet_auth_utils.constants import EnvironmentVariables
2727

2828

29+
def _warn_env_overrides_selection(selected_profile_name: str):
30+
# If we detect a current environment that we expect will override
31+
# what is being saved to the config file, warn the user.
32+
# See https://github.com/pylint-dev/pylint/issues/5091 regarding the pylint disabled warning.
33+
env_profile = os.getenv(EnvironmentVariables.AUTH_PROFILE, None) # pylint: disable=E1507
34+
if env_profile and str.lower(env_profile) != str.lower(selected_profile_name):
35+
print(
36+
f'Warning: Environment variable "{EnvironmentVariables.AUTH_PROFILE}" is set to "{env_profile}". This will override saved settings.'
37+
)
38+
39+
env_client_id = os.getenv(EnvironmentVariables.AUTH_CLIENT_ID, None) # pylint: disable=E1507
40+
env_client_secret = os.getenv(EnvironmentVariables.AUTH_CLIENT_SECRET, None) # pylint: disable=E1507
41+
if env_client_id and env_client_secret:
42+
print(
43+
f'Warning: Environment variables "{EnvironmentVariables.AUTH_CLIENT_ID}" and "{EnvironmentVariables.AUTH_CLIENT_SECRET}" are set.'
44+
" These will always take priority over the saved settings."
45+
)
46+
47+
env_api_key = os.getenv(EnvironmentVariables.AUTH_API_KEY, None) # pylint: disable=E1507
48+
if env_api_key: # and str.lower(_PL_API_KEY_ADHOC_PROFILE_NAME) != str.lower(selected_profile_name):
49+
print(
50+
f'Warning: Environment variable "{EnvironmentVariables.AUTH_API_KEY}" is set. This will always take priority over the saved settings.'
51+
)
52+
53+
2954
def prompt_and_change_user_default_profile_if_different(
3055
candidate_profile_name: str, change_default_selection: Optional[bool] = None
3156
):
@@ -34,6 +59,7 @@ def prompt_and_change_user_default_profile_if_different(
3459
saved_profile_name = config_file.lazy_get(EnvironmentVariables.AUTH_PROFILE)
3560
except FileNotFoundError:
3661
saved_profile_name = None # config_file.effective_conf_value(EnvironmentVariables.AUTH_PROFILE, fallback_value=Builtins.builtin_default_profile_name())
62+
selected_profile_name = saved_profile_name
3763

3864
if not saved_profile_name:
3965
# Always write a preference if none is saved.
@@ -56,7 +82,9 @@ def prompt_and_change_user_default_profile_if_different(
5682
)
5783

5884
if do_change_default:
85+
selected_profile_name = candidate_profile_name
5986
config_file.update_data({EnvironmentVariables.AUTH_PROFILE: candidate_profile_name})
6087
config_file.save()
6188

89+
_warn_env_overrides_selection(selected_profile_name)
6290
return do_change_default

src/planet_auth_utils/plauth_factory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from planet_auth.logging.auth_logger import getAuthLogger
3939

4040
auth_logger = getAuthLogger()
41+
_PL_API_KEY_ADHOC_PROFILE_NAME = "_PL_API_KEY"
4142

4243

4344
class PlanetAuthFactory:
@@ -218,7 +219,7 @@ def _init_context_from_api_key(api_key: str) -> Auth:
218219
"api_key": api_key,
219220
"bearer_token_prefix": PlanetLegacyRequestAuthenticator.TOKEN_PREFIX,
220221
}
221-
adhoc_profile_name = "_PL_API_KEY"
222+
adhoc_profile_name = _PL_API_KEY_ADHOC_PROFILE_NAME
222223
auth_logger.debug(msg="Initializing Auth from API key")
223224
plauth_context = Auth.initialize_from_config_dict(
224225
client_config=constructed_client_config_dict,

0 commit comments

Comments
 (0)