@@ -550,7 +550,7 @@ def set_aws(self, aws: 'AWSBoto'):
550
550
message = "AWS Secret Access Key" , validate = self .__inquirer_check_required )
551
551
552
552
# Check if the provided aws credentials are valid
553
- log ("\n Checking AWS credentials..." )
553
+ log ("\n Checking AWS raw credentials..." )
554
554
555
555
if aws .check_credentials (aws_access_key_id = aws_access_key_id ,
556
556
aws_secret_access_key = aws_secret_access_key ):
@@ -569,7 +569,7 @@ def set_aws(self, aws: 'AWSBoto'):
569
569
region = inquirer .list_input ("Choose your region" ,
570
570
choices = aws_regions )
571
571
572
- log ("\n Checking region..." )
572
+ log ("\n Checking region for raw credentials ..." )
573
573
if aws .check_credentials (aws_access_key_id = aws_access_key_id ,
574
574
aws_secret_access_key = aws_secret_access_key ,
575
575
aws_region_name = region ):
@@ -594,7 +594,7 @@ def set_aws(self, aws: 'AWSBoto'):
594
594
# EXISTING PROFILE CONFIGURATION
595
595
596
596
# Check if the provided aws credentials are valid
597
- log ("\n Checking AWS credentials..." )
597
+ log (f "\n Checking AWS credentials for profile { aws_profile } ..." )
598
598
599
599
if aws .check_credentials (aws_profile = aws_profile ):
600
600
log (' ...AWS credentials are valid\n ' )
@@ -622,7 +622,7 @@ def set_aws(self, aws: 'AWSBoto'):
622
622
default = default_region ,
623
623
choices = aws_regions )
624
624
625
- log ("\n Checking region..." )
625
+ log (f "\n Checking region for profile { aws_profile } ..." )
626
626
if aws .check_credentials (aws_profile = aws_profile ,
627
627
aws_region_name = region ):
628
628
log (' ...region is valid\n ' )
@@ -686,14 +686,14 @@ def __set_aws_config(self, aws_profile_name, region):
686
686
aws_config .read (self .aws_config_file )
687
687
688
688
# 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 } ' )
691
691
692
692
# 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
694
694
695
695
# 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'
697
697
698
698
# Write the config object to the config file
699
699
os .makedirs (self .aws_dir , exist_ok = True , mode = 0o775 )
@@ -739,7 +739,7 @@ def __set_aws_credentials(self,
739
739
# Write the profile with the new secret access key
740
740
aws_credentials [aws_profile_name ]['aws_secret_access_key' ] = aws_secret_access_key
741
741
742
- # Write the config object to the config file
742
+ # Write the credentials in the credentials file
743
743
os .makedirs (self .aws_dir , exist_ok = True , mode = 0o775 )
744
744
with open (self .aws_credentials_file , 'w' ) as f :
745
745
aws_credentials .write (f )
@@ -766,14 +766,19 @@ def __get_region_from_aws_config_file(self, aws_profile):
766
766
767
767
# Read the aws config file
768
768
if hasattr (self , 'aws_config_file' ) and os .path .exists (self .aws_config_file ):
769
+ # Read the config file
769
770
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
770
780
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
777
782
778
783
except Exception :
779
784
print_error ()
@@ -1457,6 +1462,35 @@ def check_credentials(self,
1457
1462
# Credentials are valid
1458
1463
return True
1459
1464
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
+
1460
1494
except Exception :
1461
1495
print_error (msg = "Invalid AWS credentials" )
1462
1496
return False
@@ -6602,7 +6636,7 @@ def subcmd_ssh(self, cfg: ConfigManager, aws: AWSBoto):
6602
6636
def subcmd_credentials (self , cfg : ConfigManager , aws : AWSBoto ):
6603
6637
'''Check AWS credentials'''
6604
6638
6605
- log ("\n Checking AWS credentials..." )
6639
+ log (f "\n Checking AWS credentials for profile { cfg . aws_profile } ..." )
6606
6640
6607
6641
if aws .check_credentials (aws_profile = cfg .aws_profile ):
6608
6642
log (' ...AWS credentials are valid\n ' )
0 commit comments