Repository: victorskl/yawsso (language: python)

Issue:
Rename profile on sync
Not sure if a wider problem but the dotnet sdk doesn't support assuming a role with the source_profile is an SSO config. So yawsso sounds like a great workaround but the sdk decided to prioritize config file over the credentials file. When yawsso syncs I have the source_profile in both files and it still fails.  

Feature request to rename a profile on sync so I cound have `[profile rolesso]` in config and on sync it becomes `[role]` in the credentials file.

Maybe `yawsso -p rolesso:role`? From the docs, profile names "can use letters, numbers, hyphens ( - ), and underscores ( _ ), but no spaces."

Relates to https://github.com/aws/aws-sdk-net/issues/1853


Discussion / hints:
@supergibbs Yep; sound ok to me. Just hoping; folk won't intentionally name the _normal_ profile with colon in it! Bec it'd activate "rename" logic; if I understand correctly. Otherwise we could have _a dedicated flag_ or _a subcommand_ for this particular use case -- _rename profile on sync_ -- feature. How do you think?

My org and I, myself do not use `source_profile`; TBH. So it's all up to community user feedback. Likewise, happy to support this feature. 

Meanwhile, let me read the related issue pointer (thanks) to understand the context better.
Ya, I included a snippet from the docs that seems to say colons aren't allowed in profile names. I think this would be better so the rest of the functionality would still work without change. A flag or subcommand would limit to one profile probably or be harder to use. Thanks! 
> colons aren't allowed in profile names.

Ah; right. Colon should work then. I agree harder to use for flag or subcommand.

Thanks for PR; I will review and test it today or tomorrow. If all good, I will release it right after.  
Released

- Please upgrade like: `pip install -U yawsso==0.7.0`
- Added [user guide for this feature](https://github.com/victorskl/yawsso#rename-profile-on-sync)



Fix patch:
diff --git a/README.md b/README.md
index 82ddc47..74b3d8b 100644
--- a/README.md
+++ b/README.md
@@ -183,6 +183,11 @@ yawsso login --this
 yawsso login --profile dev --this
 ```
 
+> 👉 Login using named profile dev and sync as foo. See above for more details on renaming, limited to one profile. 
+```
+yawsso login --profile dev:foo
+```
+
 #### Login then Export token
 
 - Exporting access token also support with login subcommand as follows: 
diff --git a/yawsso/cli.py b/yawsso/cli.py
index 9382d42..b140f16 100644
--- a/yawsso/cli.py
+++ b/yawsso/cli.py
@@ -468,6 +468,8 @@ def main():
 
     config = read_config(aws_config_file)
 
+    profiles_new_name = dict()
+
     if args.command:
         if args.command == "version":
             logger.info(version_help)
@@ -475,10 +477,15 @@ def main():
 
         elif args.command == "login":
             login_profile = "default"
+            login_profile_new_name = ""
             cmd_aws_sso_login = f"{aws_bin} sso login"
 
             if args.profile:
-                login_profile = args.profile
+                if ":" in args.profile:
+                    login_profile, login_profile_new_name = args.profile.split(":")
+                    profiles_new_name[login_profile] = login_profile_new_name
+                else:
+                    login_profile = args.profile
 
             cmd_aws_sso_login = f"{cmd_aws_sso_login} --profile={login_profile}"
 
@@ -491,16 +498,16 @@ def main():
             # Specific use case: making `yawsso login -e` or `yawsso login --profile NAME -e`
             # to login, sync, print cred then exit
             if export_vars:
-                credentials = update_profile(login_profile, config)
+                credentials = update_profile(login_profile, config, login_profile_new_name)
                 get_export_vars(login_profile, credentials)
                 exit(0)
 
             if args.this:
-                update_profile(login_profile, config)
+                update_profile(login_profile, config, login_profile_new_name)
                 exit(0)
 
             if login_profile == "default" and not export_vars:
-                update_profile("default", config)
+                update_profile("default", config, login_profile_new_name)
 
             # otherwise continue with sync all named profiles below
 
@@ -524,7 +531,6 @@ def main():
 
     global profiles
     profiles = named_profiles
-    profiles_new_name = dict()
 
     if args.profiles:
         profiles = []


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index c347c4d..8859bb6 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -804,3 +804,13 @@ def test_rename_profile_not_found(self):
         with ArgvContext(program, '-p', f"{uuid.uuid4().hex}:new_name", '-t'):
             cli.main()
         self.assertEqual(len(cli.profiles), 0)
+
+    def test_login_command_rename(self):   
+        when(cli).poll(contains('aws sso login'), ...).thenReturn(True)
+        with ArgvContext(program, '-t', 'login', '--profile', 'dev:dev_renamed'):
+            cli.main()
+        cred = cli.read_config(self.credentials.name)
+        new_tok = cred['dev_renamed']['aws_session_token']
+        self.assertNotEqual(new_tok, 'tok')
+        self.assertEqual(new_tok, 'VeryLongBase664String==')
+        verify(cli, times=1).poll(...)
\ No newline at end of file