Repository: cevoaustralia/aws-google-auth

Issue:
Running twice results in malformed credentials file
On Ubuntu with version 0.0.12. Two runs of aws-google-auth results in  ~/.aws/credentials with two "[None]" sections. The config parser of aws-cli throws error configparser.DuplicateSectionError.

Discussion:

Patch:
diff --git a/README.rst b/README.rst
index fe68570..bac4d06 100644
--- a/README.rst
+++ b/README.rst
@@ -110,7 +110,8 @@ Usage
       -d DURATION, --duration DURATION
                             Credential duration ($DURATION)
       -p PROFILE, --profile PROFILE
-                            AWS profile ($AWS_PROFILE)
+                            AWS profile (defaults to value of $AWS_PROFILE,
+                            then falls back to 'sts')
       -a ASK_ROLE, --ask-role ASK_ROLE
                             Set true to always pick the role
       -r AWS_ROLE_ARN, --role-arn AWS_ROLE_ARN
diff --git a/aws_google_auth/__init__.py b/aws_google_auth/__init__.py
index 7b35df2..2d3bf2e 100644
--- a/aws_google_auth/__init__.py
+++ b/aws_google_auth/__init__.py
@@ -460,9 +460,8 @@ def cli(cli_args):
                 DurationSeconds=config.duration)
 
     print("Credentials Expiration: " + format(token['Credentials']['Expiration'].astimezone(get_localzone())))
-    if config.profile is None:
-        print_exports(token)
 
+    print_exports(token)
     _store(config, token)
 
 
@@ -482,6 +481,7 @@ def print_exports(token):
 def _store(config, aws_session_token):
 
     def store_config(profile, config_location, storer):
+        assert (profile is not None), "Can not store config/credentials if the AWS_PROFILE is None."
         config_file = configparser.RawConfigParser()
         config_file.read(config_location)
 
diff --git a/aws_google_auth/prepare.py b/aws_google_auth/prepare.py
index 5dca8df..bb86ad8 100644
--- a/aws_google_auth/prepare.py
+++ b/aws_google_auth/prepare.py
@@ -18,7 +18,8 @@ def get_prepared_config(
     def default_if_none(value, default):
         return value if value is not None else default
 
-    google_config.profile = default_if_none(profile, google_config.profile)
+    # If no profile is specified, default to "sts" so we don't clobber the user's default.
+    google_config.profile = default_if_none(profile, google_config.profile) or "sts"
     _create_base_aws_cli_config_files_if_needed(google_config)
 
     if google_config.profile is not None:


Test patch:
diff --git a/aws_google_auth/tests/test_persist_profile.py b/aws_google_auth/tests/test_persist_profile.py
index 19d3f43..feb8156 100644
--- a/aws_google_auth/tests/test_persist_profile.py
+++ b/aws_google_auth/tests/test_persist_profile.py
@@ -107,5 +107,5 @@ class TestPersistConfig(unittest.TestCase):
         self.assertEquals(config.google_sp_id, None)
         self.assertEquals(config.duration, 3600)
         self.assertEquals(config.ask_role, False)
-        self.assertEquals(config.profile, None)
+        self.assertEquals(config.profile, "sts")
         self.assertEquals(config.role_arn, None)