Skip to content
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

chore: add validity check for logging #161

Merged
merged 8 commits into from
Oct 11, 2024
Merged
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
19 changes: 13 additions & 6 deletions copernicusmarine/command_line_interface/group_login.py
Original file line number Diff line number Diff line change
@@ -72,10 +72,16 @@ def cli_login() -> None:
help="Flag to skip confirmation before overwriting configuration file.",
)
@click.option(
"--skip-if-user-logged-in",
"--check-credentials-valid",
is_flag=True,
default=False,
help="Flag to skip the logging process if the user is already logged in.",
help="Flag to check if the credentials are valid. "
"No other action will be performed. "
"The validity will be check in this order: "
"1. Check if the credentials are valid with the provided username and password."
"2. Check if the credentials are valid in the configuration file."
"3. Check if the credentials are valid in the environment variables."
"When any is found not valid, will return False immediately.",
)
@click.option(
"--log-level",
@@ -92,18 +98,19 @@ def login(
password: Optional[str],
configuration_file_directory: pathlib.Path,
overwrite_configuration_file: bool,
skip_if_user_logged_in: bool,
check_credentials_valid: bool,
log_level: str = "INFO",
) -> None:
if log_level == "QUIET":
logger.disabled = True
logger.setLevel(level="CRITICAL")
else:
logger.setLevel(level=log_level)
login_function(
if not login_function(
username=username,
password=password,
configuration_file_directory=configuration_file_directory,
overwrite_configuration_file=overwrite_configuration_file,
skip_if_user_logged_in=skip_if_user_logged_in,
)
check_credentials_valid=check_credentials_valid,
):
exit(1)
58 changes: 57 additions & 1 deletion copernicusmarine/core_functions/credentials_utils.py
Original file line number Diff line number Diff line change
@@ -33,6 +33,11 @@
DEFAULT_CLIENT_CREDENTIALS_FILEPATH = (
DEFAULT_CLIENT_BASE_DIRECTORY / DEFAULT_CLIENT_CREDENTIALS_FILENAME
)
RECOVER_YOUR_CREDENTIALS_MESSAGE = (
"Learn how to recover your credentials at: "
"https://help.marine.copernicus.eu/en/articles/"
"4444552-i-forgot-my-username-or-my-password-what-should-i-do"
)


class CredentialsCannotBeNone(Exception):
@@ -114,7 +119,7 @@ def _retrieve_credential_from_prompt(
" https://data.marine.copernicus.eu/register"
)
return click.prompt(
"copernicusmarine " + credential_type, hide_input=hide_input
"Copernicus Marine " + credential_type, hide_input=hide_input
)


@@ -206,9 +211,60 @@ def copernicusmarine_configuration_file_exists(
return configuration_filename.exists()


def copernicusmarine_credentials_are_valid(
configuration_file_directory: pathlib.Path,
username: Optional[str],
password: Optional[str],
):
if username and password:
if _check_credentials_with_cas(username, password):
logger.info("Valid credentials from input username and password.")
return True
else:
logger.info(
"Invalid credentials from input username and password."
)
logger.info(RECOVER_YOUR_CREDENTIALS_MESSAGE)
return False
elif (
COPERNICUSMARINE_SERVICE_USERNAME and COPERNICUSMARINE_SERVICE_PASSWORD
):
if _check_credentials_with_cas(
COPERNICUSMARINE_SERVICE_USERNAME,
COPERNICUSMARINE_SERVICE_PASSWORD,
):
logger.info(
"Valid credentials from environment variables: "
"COPERNICUSMARINE_SERVICE_USERNAME and "
"COPERNICUSMARINE_SERVICE_PASSWORD."
)
return True
else:
logger.info(
"Invalid credentials from environment variables: "
"COPERNICUSMARINE_SERVICE_USERNAME and "
"COPERNICUSMARINE_SERVICE_PASSWORD."
)
logger.info(RECOVER_YOUR_CREDENTIALS_MESSAGE)
return False
elif copernicusmarine_configuration_file_is_valid(
configuration_file_directory
):
logger.info("Valid credentials from configuration file.")
return True
else:
logger.info("Invalid credentials from configuration file.")
logger.info(RECOVER_YOUR_CREDENTIALS_MESSAGE)
return False


def copernicusmarine_configuration_file_is_valid(
configuration_file_directory: pathlib.Path,
) -> bool:
if not copernicusmarine_configuration_file_exists(
configuration_file_directory
):
return False
configuration_filename = pathlib.Path(
configuration_file_directory / DEFAULT_CLIENT_CREDENTIALS_FILENAME
)
30 changes: 11 additions & 19 deletions copernicusmarine/core_functions/login.py
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
from typing import Optional

from copernicusmarine.core_functions.credentials_utils import (
copernicusmarine_configuration_file_exists,
copernicusmarine_configuration_file_is_valid,
RECOVER_YOUR_CREDENTIALS_MESSAGE,
copernicusmarine_credentials_are_valid,
credentials_file_builder,
)

@@ -16,19 +16,15 @@ def login_function(
password: Optional[str],
configuration_file_directory: pathlib.Path,
overwrite_configuration_file: bool,
skip_if_user_logged_in: bool,
check_credentials_valid: bool,
) -> bool:
if (
skip_if_user_logged_in
and copernicusmarine_configuration_file_exists(
configuration_file_directory
)
and copernicusmarine_configuration_file_is_valid(
configuration_file_directory
)
):
logger.info("You are already logged in. Skipping login.")
return True
if check_credentials_valid:
if copernicusmarine_credentials_are_valid(
configuration_file_directory, username, password
):
return True
else:
return False
credentials_file = credentials_file_builder(
username=username,
password=password,
@@ -42,9 +38,5 @@ def login_function(
logger.info(
"Invalid credentials. No configuration file have been modified."
)
logger.info(
"Learn how to recover your credentials at: "
"https://help.marine.copernicus.eu/en/articles/"
"4444552-i-forgot-my-username-or-my-password-what-should-i-do"
)
logger.info(RECOVER_YOUR_CREDENTIALS_MESSAGE)
return False
12 changes: 9 additions & 3 deletions copernicusmarine/python_interface/login.py
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ def login(
password: Optional[str] = None,
configuration_file_directory: pathlib.Path = DEFAULT_CLIENT_BASE_DIRECTORY,
overwrite_configuration_file: bool = False,
skip_if_user_logged_in: bool = False,
check_credentials_valid: bool = False,
) -> bool:
"""
Create a configuration file with your Copernicus Marine credentials.
@@ -30,12 +30,18 @@ def login(
overwrite_configuration_file : bool
Flag to skip confirmation before overwriting the configuration file.
skip_if_user_logged_in : bool
Flag to skip the logging process if the user is already logged in.
Flag to check if the credentials are valid.
No other action will be performed.
The validity will be check in this order:
1. Check if the credentials are valid with the provided username and password.
2. Check if the credentials are valid in the configuration file.
3. Check if the credentials are valid in the environment variables.
When any is found not valid, will return False immediately.,
""" # noqa
return login_function(
username=username,
password=password,
configuration_file_directory=configuration_file_directory,
overwrite_configuration_file=overwrite_configuration_file,
skip_if_user_logged_in=skip_if_user_logged_in,
check_credentials_valid=check_credentials_valid,
)
12 changes: 10 additions & 2 deletions tests/__snapshots__/test_help_command_interface.ambr
Original file line number Diff line number Diff line change
@@ -189,8 +189,16 @@
' -overwrite, --overwrite-configuration-file',
' Flag to skip confirmation before overwriting',
' configuration file.',
' --skip-if-user-logged-in Flag to skip the logging process if the user',
' is already logged in.',
' --check-credentials-valid Flag to check if the credentials are valid.',
' No other action will be performed. The',
' validity will be check in this order: 1.',
' Check if the credentials are valid with the',
' provided username and password.2. Check if',
' the credentials are valid in the',
' configuration file.3. Check if the',
' credentials are valid in the environment',
' variables.When any is found not valid, will',
' return False immediately.',
' --log-level [DEBUG|INFO|WARN|ERROR|CRITICAL|QUIET]',
' Set the details printed to console by the',
' command (based on standard logging library).',
Loading