Skip to content

Commit

Permalink
fix: error logins are now more readable and pythonic (#254)
Browse files Browse the repository at this point in the history
invalid credentials as errors and if aborted does not exit in python and doesn't raise either
  • Loading branch information
renaudjester authored Dec 16, 2024
1 parent 81fceff commit 6fa9e36
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
46 changes: 31 additions & 15 deletions copernicusmarine/core_functions/credentials_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
from netrc import netrc
from platform import system
from typing import Literal, Optional, Tuple
from typing import Literal, Optional

import click
import lxml.html
Expand Down Expand Up @@ -296,7 +296,7 @@ def copernicusmarine_credentials_are_valid(
logger.info("Valid credentials from input username and password.")
return True
else:
logger.info(
logger.error(
"Invalid credentials from input username and password."
)
logger.info(RECOVER_YOUR_CREDENTIALS_MESSAGE)
Expand All @@ -315,7 +315,7 @@ def copernicusmarine_credentials_are_valid(
)
return True
else:
logger.info(
logger.error(
"Invalid credentials from environment variables: "
"COPERNICUSMARINE_SERVICE_USERNAME and "
"COPERNICUSMARINE_SERVICE_PASSWORD."
Expand All @@ -335,7 +335,7 @@ def copernicusmarine_credentials_are_valid(
logger.info("Valid credentials from configuration file.")
return True
else:
logger.info("Invalid credentials from configuration file.")
logger.error("Invalid credentials from configuration file.")
logger.info(RECOVER_YOUR_CREDENTIALS_MESSAGE)
elif configuration_file:
logger.info(
Expand All @@ -361,7 +361,7 @@ def create_copernicusmarine_configuration_file(
password: str,
configuration_file_directory: pathlib.Path,
force_overwrite: bool,
) -> pathlib.Path:
) -> tuple[Optional[pathlib.Path], bool]:
configuration_lines = [
"[credentials]\n",
f"username={username}\n",
Expand All @@ -371,18 +371,21 @@ def create_copernicusmarine_configuration_file(
configuration_file_directory / DEFAULT_CLIENT_CREDENTIALS_FILENAME
)
if configuration_filename.exists() and not force_overwrite:
click.confirm(
f"File {configuration_filename} already exists, overwrite it ?",
abort=True,
confirmed = click.confirm(
f"File {configuration_filename} already exists, overwrite it ?"
)
if not confirmed:
logger.error("Abort")
return None, True

configuration_file_directory.mkdir(parents=True, exist_ok=True)
configuration_file = open(configuration_filename, "w")
configuration_string = base64.b64encode(
"".join(configuration_lines).encode("ascii", "strict")
).decode("utf8")
configuration_file.write(configuration_string)
configuration_file.close()
return configuration_filename
return configuration_filename, False


def _check_credentials_with_old_cas(username: str, password: str) -> bool:
Expand Down Expand Up @@ -540,7 +543,7 @@ def get_and_check_username_password(
username: Optional[str],
password: Optional[str],
credentials_file: Optional[pathlib.Path],
) -> Tuple[str, str]:
) -> tuple[str, str]:
username, password = get_username_password(
username=username, password=password, credentials_file=credentials_file
)
Expand All @@ -563,7 +566,7 @@ def get_username_password(
username: Optional[str],
password: Optional[str],
credentials_file: Optional[pathlib.Path],
) -> Tuple[str, str]:
) -> tuple[str, str]:
username = get_credential(
username,
"username",
Expand Down Expand Up @@ -602,7 +605,14 @@ def credentials_file_builder(
password: Optional[str],
configuration_file_directory: pathlib.Path,
force_overwrite: bool,
) -> Optional[pathlib.Path]:
) -> tuple[Optional[pathlib.Path], bool]:
"""
Returns:
a path to the configuration file: if none, the credentials are not valid
a boolean that indicates
if the user aborted the creation of the configuration file
"""
username = _get_credential_from_environment_variable_or_prompt(
username, "username", False
)
Expand All @@ -613,11 +623,17 @@ def credentials_file_builder(
_are_copernicus_marine_credentials_valid(username, password)
)
if copernicus_marine_credentials_are_valid:
configuration_file = create_copernicusmarine_configuration_file(
(
configuration_file,
has_been_aborted,
) = create_copernicusmarine_configuration_file(
username=username,
password=password,
configuration_file_directory=configuration_file_directory,
force_overwrite=force_overwrite,
)
return configuration_file
return None
if has_been_aborted:
return configuration_file, has_been_aborted
if configuration_file:
return configuration_file, False
return None, False
10 changes: 6 additions & 4 deletions copernicusmarine/core_functions/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ def login_function(
return True
else:
return False
credentials_file = credentials_file_builder(
credentials_file, has_been_aborted = credentials_file_builder(
username=username,
password=password,
configuration_file_directory=configuration_file_directory,
force_overwrite=force_overwrite,
)
if has_been_aborted:
logger.info("No configuration file have been modified.")
return False
if credentials_file is not None:
logger.info(f"Credentials file stored in {credentials_file}.")
return True
else:
logger.info(
"Invalid credentials. No configuration file have been modified."
)
logger.error("Invalid credentials.")
logger.info("No configuration file have been modified.")
logger.info(RECOVER_YOUR_CREDENTIALS_MESSAGE)
return False
6 changes: 3 additions & 3 deletions copernicusmarine/core_functions/sessions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import ssl
from typing import Any, List, Literal, Optional, Tuple
from typing import Any, Literal, Optional

import boto3
import botocore
Expand Down Expand Up @@ -50,10 +50,10 @@ def get_ssl_context() -> Optional[ssl.SSLContext]:

def get_configured_boto3_session(
endpoint_url: str,
operation_type: List[Literal["ListObjects", "HeadObject", "GetObject"]],
operation_type: list[Literal["ListObjects", "HeadObject", "GetObject"]],
username: Optional[str] = None,
return_ressources: bool = False,
) -> Tuple[Any, Any]:
) -> tuple[Any, Any]:
config_boto3 = botocore.config.Config(
s3={"addressing_style": "virtual"},
signature_version=botocore.UNSIGNED,
Expand Down

0 comments on commit 6fa9e36

Please sign in to comment.