Skip to content

Commit cf31470

Browse files
Merge branch 'main' into carl/feedback-3--config-priority
2 parents d2806e9 + 34e3262 commit cf31470

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

src/planet_auth_utils/commands/cli/profile_cmd.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,13 @@ def cmd_profile_edit():
219219
raise AuthException("Function not implemented")
220220

221221

222-
@cmd_profile.command("copy")
222+
# Separte help since the docstring here is developer oriented, not user oriented.
223+
@cmd_profile.command(
224+
"copy",
225+
help="Copy an existing profile to create a new profile. Only the persistent"
226+
" profile configuration will be copied. User access tokens initialized"
227+
" via a call to `login` will not be copied.",
228+
)
223229
@click.argument("src")
224230
@click.argument("dst")
225231
@opt_sops()
@@ -228,18 +234,20 @@ def cmd_profile_copy(sops, src, dst):
228234
"""
229235
Copy an existing profile to create a new profile. Only the persistent
230236
profile configuration will be copied. User access tokens initialized
231-
via a call to `login` will not be copied. Note: Depending on the
232-
type of [planet_auth.AuthClient] configured in the source profile,
233-
the new profile may have long term credentials (e.g. OAuth
234-
client credential secrets, API keys. etc.). External support files,
235-
such as public/private keypair files, are not copied.
237+
via a call to `login` will not be copied.
238+
239+
Note: Depending on the type of [planet_auth.AuthClient] configured in
240+
the source profile, the new profile may have long term credentials
241+
(e.g. OAuth client credential secrets, API keys. etc.).
242+
243+
Note: External support files, such as public/private keypair files,
244+
are not copied.
236245
237246
This command will work with built-in as well as custom profiles,
238247
so it is possible to bootstrap profiles to manage multiple user
239248
identities with an otherwise default client profile:
240249
```
241-
profile copy default <my_new_profile>
242-
profile copy legacy <my_new_profile>
250+
profile copy my_app_builtin_default <my_new_profile>
243251
```
244252
245253
"""

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)