From b18b305fdd2c653608cb4a3bdbe4d87312f5e2e3 Mon Sep 17 00:00:00 2001 From: Russell Freeman Date: Mon, 25 Mar 2024 22:50:38 +0000 Subject: [PATCH 1/4] Add PowerShell-flavor export command --- cmd/aws-sso-creds/exportps/cli.go | 39 +++++++++++++++++++++++++++++++ cmd/aws-sso-creds/main.go | 2 ++ 2 files changed, 41 insertions(+) create mode 100644 cmd/aws-sso-creds/exportps/cli.go diff --git a/cmd/aws-sso-creds/exportps/cli.go b/cmd/aws-sso-creds/exportps/cli.go new file mode 100644 index 0000000..df152a7 --- /dev/null +++ b/cmd/aws-sso-creds/exportps/cli.go @@ -0,0 +1,39 @@ +package exportps + +import ( + "fmt" + + "github.com/jaxxstorm/aws-sso-creds/pkg/credentials" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func Command() *cobra.Command { + command := &cobra.Command{ + Use: "export-ps", + Short: "Generates a set of powershell environment assignments to define the AWS temporary creds to your environment", + Long: "Generates a set of powershell environment assignments to define the AWS temporary creds to your environment", + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + + cmd.SilenceUsage = true + + profile := viper.GetString("profile") + homeDir := viper.GetString("home-directory") + + creds, _, err := credentials.GetSSOCredentials(profile, homeDir) + + if err != nil { + return err + } + + fmt.Printf("$env:AWS_ACCESS_KEY_ID=%s\n", *creds.RoleCredentials.AccessKeyId) + fmt.Printf("$env:AWS_SECRET_ACCESS_KEY=%s\n", *creds.RoleCredentials.SecretAccessKey) + fmt.Printf("$env:AWS_SESSION_TOKEN=%s\n", *creds.RoleCredentials.SessionToken) + + return nil + }, + } + + return command +} diff --git a/cmd/aws-sso-creds/main.go b/cmd/aws-sso-creds/main.go index b2dfb0d..6ec69e0 100644 --- a/cmd/aws-sso-creds/main.go +++ b/cmd/aws-sso-creds/main.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/viper" "github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/export" + "github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/exportps" "github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/get" "github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/helper" "github.com/jaxxstorm/aws-sso-creds/cmd/aws-sso-creds/list" @@ -29,6 +30,7 @@ func configureCLI() *cobra.Command { rootCommand.AddCommand(set.Command()) rootCommand.AddCommand(version.Command()) rootCommand.AddCommand(export.Command()) + rootCommand.AddCommand(exportps.Command()) rootCommand.AddCommand(list.Command()) rootCommand.AddCommand(helper.Command()) From 7aa8c9e159ad14288336be1736ae0efde42fb6fb Mon Sep 17 00:00:00 2001 From: Russell Freeman Date: Mon, 25 Mar 2024 23:01:06 +0000 Subject: [PATCH 2/4] Documentation --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 8c1b773..e24deec 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,23 @@ export AWS_SECRET_ACCESS_KEY= export AWS_SESSION_TOKEN= ``` +#### PowerShell more your thing? + +If you're using PowerShell, you can use `export-ps` to generate PowerShell assignments, instead. + +```powershell +> aws-sso-creds export-ps +$env:AWS_ACCESS_KEY_ID= +$env:AWS_SECRET_ACCESS_KEY= +$env:AWS_SESSION_TOKEN= +``` + +Used with `Invoke-Expression` :- + +```powershell +> aws-sso-creds export-ps | Invoke-Expression +``` + ## List accounts You can also list the accounts you have available within AWS SSO: From 3a09ba492d69cf139da90747bddf5e63acf1b2c6 Mon Sep 17 00:00:00 2001 From: Russ Freeman Date: Tue, 26 Mar 2024 09:58:22 +0000 Subject: [PATCH 3/4] Quotes, mate --- cmd/aws-sso-creds/exportps/cli.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/aws-sso-creds/exportps/cli.go b/cmd/aws-sso-creds/exportps/cli.go index df152a7..b9ac39e 100644 --- a/cmd/aws-sso-creds/exportps/cli.go +++ b/cmd/aws-sso-creds/exportps/cli.go @@ -27,9 +27,9 @@ func Command() *cobra.Command { return err } - fmt.Printf("$env:AWS_ACCESS_KEY_ID=%s\n", *creds.RoleCredentials.AccessKeyId) - fmt.Printf("$env:AWS_SECRET_ACCESS_KEY=%s\n", *creds.RoleCredentials.SecretAccessKey) - fmt.Printf("$env:AWS_SESSION_TOKEN=%s\n", *creds.RoleCredentials.SessionToken) + fmt.Printf("$env:AWS_ACCESS_KEY_ID='%s'\n", *creds.RoleCredentials.AccessKeyId) + fmt.Printf("$env:AWS_SECRET_ACCESS_KEY='%s'\n", *creds.RoleCredentials.SecretAccessKey) + fmt.Printf("$env:AWS_SESSION_TOKEN='%s'\n", *creds.RoleCredentials.SessionToken) return nil }, From 039cc64ea2c2aabbbc8bf441e5d7f1a13f49279e Mon Sep 17 00:00:00 2001 From: Russ Freeman Date: Tue, 26 Mar 2024 11:00:04 +0000 Subject: [PATCH 4/4] Sync docs --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e24deec..7395960 100644 --- a/README.md +++ b/README.md @@ -84,12 +84,12 @@ If you're using PowerShell, you can use `export-ps` to generate PowerShell assig ```powershell > aws-sso-creds export-ps -$env:AWS_ACCESS_KEY_ID= -$env:AWS_SECRET_ACCESS_KEY= -$env:AWS_SESSION_TOKEN= +$env:AWS_ACCESS_KEY_ID='' +$env:AWS_SECRET_ACCESS_KEY='' +$env:AWS_SESSION_TOKEN='' ``` -Used with `Invoke-Expression` :- +Use it with `Invoke-Expression` :- ```powershell > aws-sso-creds export-ps | Invoke-Expression