Skip to content

Commit bdb151b

Browse files
Merge pull request #77 from HPCNow/main
froster v0.12.31
2 parents acdde29 + 1d5b522 commit bdb151b

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

Diff for: froster/froster.py

+50-16
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ def set_aws(self, aws: 'AWSBoto'):
550550
message="AWS Secret Access Key", validate=self.__inquirer_check_required)
551551

552552
# Check if the provided aws credentials are valid
553-
log("\nChecking AWS credentials...")
553+
log("\nChecking AWS raw credentials...")
554554

555555
if aws.check_credentials(aws_access_key_id=aws_access_key_id,
556556
aws_secret_access_key=aws_secret_access_key):
@@ -569,7 +569,7 @@ def set_aws(self, aws: 'AWSBoto'):
569569
region = inquirer.list_input("Choose your region",
570570
choices=aws_regions)
571571

572-
log("\nChecking region...")
572+
log("\nChecking region for raw credentials...")
573573
if aws.check_credentials(aws_access_key_id=aws_access_key_id,
574574
aws_secret_access_key=aws_secret_access_key,
575575
aws_region_name=region):
@@ -594,7 +594,7 @@ def set_aws(self, aws: 'AWSBoto'):
594594
# EXISTING PROFILE CONFIGURATION
595595

596596
# Check if the provided aws credentials are valid
597-
log("\nChecking AWS credentials...")
597+
log(f"\nChecking AWS credentials for profile {aws_profile}...")
598598

599599
if aws.check_credentials(aws_profile=aws_profile):
600600
log(' ...AWS credentials are valid\n')
@@ -622,7 +622,7 @@ def set_aws(self, aws: 'AWSBoto'):
622622
default=default_region,
623623
choices=aws_regions)
624624

625-
log("\nChecking region...")
625+
log(f"\nChecking region for profile {aws_profile}...")
626626
if aws.check_credentials(aws_profile=aws_profile,
627627
aws_region_name=region):
628628
log(' ...region is valid\n')
@@ -686,14 +686,14 @@ def __set_aws_config(self, aws_profile_name, region):
686686
aws_config.read(self.aws_config_file)
687687

688688
# If it does not exist, create a new profile in the aws config file
689-
if not aws_config.has_section(aws_profile_name):
690-
aws_config.add_section(aws_profile_name)
689+
if not aws_config.has_section(f'profile {aws_profile_name}'):
690+
aws_config.add_section(f'profile {aws_profile_name}')
691691

692692
# Write the profile with the new region
693-
aws_config[aws_profile_name]['region'] = region
693+
aws_config[f'profile {aws_profile_name}']['region'] = region
694694

695695
# Write the profile with the new output format
696-
aws_config[aws_profile_name]['output'] = 'json'
696+
aws_config[f'profile {aws_profile_name}']['output'] = 'json'
697697

698698
# Write the config object to the config file
699699
os.makedirs(self.aws_dir, exist_ok=True, mode=0o775)
@@ -739,7 +739,7 @@ def __set_aws_credentials(self,
739739
# Write the profile with the new secret access key
740740
aws_credentials[aws_profile_name]['aws_secret_access_key'] = aws_secret_access_key
741741

742-
# Write the config object to the config file
742+
# Write the credentials in the credentials file
743743
os.makedirs(self.aws_dir, exist_ok=True, mode=0o775)
744744
with open(self.aws_credentials_file, 'w') as f:
745745
aws_credentials.write(f)
@@ -766,14 +766,19 @@ def __get_region_from_aws_config_file(self, aws_profile):
766766

767767
# Read the aws config file
768768
if hasattr(self, 'aws_config_file') and os.path.exists(self.aws_config_file):
769+
# Read the config file
769770
config.read(self.aws_config_file)
771+
772+
# Get the region from the config file
773+
if aws_profile == 'default':
774+
region = config.get('default', 'region', fallback=None)
775+
else:
776+
region = config.get(f'profile {aws_profile}', 'region', fallback=None)
777+
else:
778+
# AWS config file does not exists
779+
region = None
770780

771-
# Return the region if it exists
772-
if config.has_section(aws_profile) and config.has_option(aws_profile, 'region'):
773-
return config.get(aws_profile, 'region')
774-
775-
# Return None otherwise
776-
return None
781+
return region
777782

778783
except Exception:
779784
print_error()
@@ -1457,6 +1462,35 @@ def check_credentials(self,
14571462
# Credentials are valid
14581463
return True
14591464

1465+
except botocore.exceptions.NoCredentialsError:
1466+
log(f"Error: No AWS credentials found.")
1467+
return False
1468+
1469+
except botocore.exceptions.EndpointConnectionError:
1470+
log(f"Error: Unable to connect to the AWS S3 endpoint.")
1471+
return False
1472+
1473+
except botocore.exceptions.ClientError as e:
1474+
error_code = e.response.get('Error', {}).get('Code')
1475+
1476+
if error_code == 'RequestTimeTooSkewed':
1477+
log(f"Error: The time difference between S3 storage and your computer is too high:\n{e}")
1478+
elif error_code == 'InvalidAccessKeyId':
1479+
log(f"Error: Invalid AWS Access Key ID\n{e}")
1480+
1481+
elif error_code == 'SignatureDoesNotMatch':
1482+
if "Signature expired" in str(e):
1483+
log(f"Error: Signature expired. The system time of your computer is likely wrong:\n{e}")
1484+
else:
1485+
log(f"Error: Invalid AWS Secret Access Key:\n{e}")
1486+
elif error_code == 'InvalidClientTokenId':
1487+
log(f"Error: Invalid AWS Access Key ID or Secret Access Key !")
1488+
elif error_code == 'ExpiredToken':
1489+
log(f"Error: Your session token has expired")
1490+
else:
1491+
log(f"Error: validating credentials")
1492+
return False
1493+
14601494
except Exception:
14611495
print_error(msg="Invalid AWS credentials")
14621496
return False
@@ -6602,7 +6636,7 @@ def subcmd_ssh(self, cfg: ConfigManager, aws: AWSBoto):
66026636
def subcmd_credentials(self, cfg: ConfigManager, aws: AWSBoto):
66036637
'''Check AWS credentials'''
66046638

6605-
log("\nChecking AWS credentials...")
6639+
log(f"\nChecking AWS credentials for profile {cfg.aws_profile}...")
66066640

66076641
if aws.check_credentials(aws_profile=cfg.aws_profile):
66086642
log(' ...AWS credentials are valid\n')

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "froster"
7-
version = "0.12.30"
7+
version = "0.12.31"
88
description = "Froster is a tool for easy data transfer between local file systems and AWS S3 storage."
99
authors = ["Victor Machado <[email protected]>"]
1010
readme = "README.md"

0 commit comments

Comments
 (0)