From b7b38be3f5b4c0cc2f6d669184d1ef8e7a2a9ab8 Mon Sep 17 00:00:00 2001 From: Michael Tibben Date: Mon, 20 Feb 2023 20:18:30 +1100 Subject: [PATCH 1/2] Use ini lib to create ini output --- cli/export.go | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/cli/export.go b/cli/export.go index f7d576060..3bfcfa905 100644 --- a/cli/export.go +++ b/cli/export.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "log" "os" "time" @@ -12,6 +13,7 @@ import ( "github.com/99designs/keyring" "github.com/alecthomas/kingpin" "github.com/aws/aws-sdk-go-v2/aws" + ini "gopkg.in/ini.v1" ) type ExportCommandInput struct { @@ -109,7 +111,7 @@ func ExportCommand(input ExportCommandInput, f *vault.ConfigFile, keyring keyrin if input.Format == FormatTypeExportJSON { return printJSON(input, credsProvider) } else if input.Format == FormatTypeExportINI { - return printINI(input, credsProvider) + return printINI(credsProvider, input.ProfileName, config.Region) } else if input.Format == FormatTypeExportEnv { return printEnv(input, credsProvider, "export ") } else { @@ -154,22 +156,39 @@ func printJSON(input ExportCommandInput, credsProvider aws.CredentialsProvider) return nil } -func printINI(input ExportCommandInput, credsProvider aws.CredentialsProvider) error { +func mustNewKey(s *ini.Section, name, val string) { + _, err := s.NewKey(name, val) + if err != nil { + log.Fatalln("Failed to create ini key:", err.Error()) + } +} + +func printINI(credsProvider aws.CredentialsProvider, profilename, region string) error { creds, err := credsProvider.Retrieve(context.TODO()) if err != nil { - return fmt.Errorf("Failed to get credentials for %s: %w", input.ProfileName, err) + return fmt.Errorf("Failed to get credentials for %s: %w", profilename, err) } - fmt.Printf("[profile %s]\n", input.ProfileName) - fmt.Printf("aws_access_key_id=%s\n", creds.AccessKeyID) - fmt.Printf("aws_secret_access_key=%s\n", creds.SecretAccessKey) + f := ini.Empty() + s, err := f.NewSection("profile " + profilename) + if err != nil { + return fmt.Errorf("Failed to create ini section: %w", err) + } + mustNewKey(s, "aws_access_key_id", creds.AccessKeyID) + mustNewKey(s, "aws_secret_access_key", creds.SecretAccessKey) if creds.SessionToken != "" { - fmt.Printf("aws_session_token=%s\n", creds.SessionToken) + mustNewKey(s, "aws_session_token", creds.SessionToken) } if creds.CanExpire { - fmt.Printf(";aws_credential_expiration=%s\n", iso8601.Format(creds.Expires)) + mustNewKey(s, "aws_credential_expiration", iso8601.Format(creds.Expires)) } + + _, err = f.WriteTo(os.Stdout) + if err != nil { + return fmt.Errorf("Failed to output ini: %w", err) + } + return nil } From eb1a23adaa2f02de79ff89a7e3d81bcf9acac86b Mon Sep 17 00:00:00 2001 From: Michael Tibben Date: Mon, 20 Feb 2023 20:22:45 +1100 Subject: [PATCH 2/2] Add region to export output --- cli/export.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/cli/export.go b/cli/export.go index 3bfcfa905..51c4cf861 100644 --- a/cli/export.go +++ b/cli/export.go @@ -113,9 +113,9 @@ func ExportCommand(input ExportCommandInput, f *vault.ConfigFile, keyring keyrin } else if input.Format == FormatTypeExportINI { return printINI(credsProvider, input.ProfileName, config.Region) } else if input.Format == FormatTypeExportEnv { - return printEnv(input, credsProvider, "export ") + return printEnv(input, credsProvider, config.Region, "export ") } else { - return printEnv(input, credsProvider, "") + return printEnv(input, credsProvider, config.Region, "") } } @@ -157,9 +157,11 @@ func printJSON(input ExportCommandInput, credsProvider aws.CredentialsProvider) } func mustNewKey(s *ini.Section, name, val string) { - _, err := s.NewKey(name, val) - if err != nil { - log.Fatalln("Failed to create ini key:", err.Error()) + if val != "" { + _, err := s.NewKey(name, val) + if err != nil { + log.Fatalln("Failed to create ini key:", err.Error()) + } } } @@ -177,12 +179,11 @@ func printINI(credsProvider aws.CredentialsProvider, profilename, region string) mustNewKey(s, "aws_access_key_id", creds.AccessKeyID) mustNewKey(s, "aws_secret_access_key", creds.SecretAccessKey) - if creds.SessionToken != "" { - mustNewKey(s, "aws_session_token", creds.SessionToken) - } + mustNewKey(s, "aws_session_token", creds.SessionToken) if creds.CanExpire { mustNewKey(s, "aws_credential_expiration", iso8601.Format(creds.Expires)) } + mustNewKey(s, "region", region) _, err = f.WriteTo(os.Stdout) if err != nil { @@ -192,7 +193,7 @@ func printINI(credsProvider aws.CredentialsProvider, profilename, region string) return nil } -func printEnv(input ExportCommandInput, credsProvider aws.CredentialsProvider, prefix string) error { +func printEnv(input ExportCommandInput, credsProvider aws.CredentialsProvider, region, prefix string) error { creds, err := credsProvider.Retrieve(context.TODO()) if err != nil { return fmt.Errorf("Failed to get credentials for %s: %w", input.ProfileName, err) @@ -207,5 +208,9 @@ func printEnv(input ExportCommandInput, credsProvider aws.CredentialsProvider, p if creds.CanExpire { fmt.Printf("%sAWS_CREDENTIAL_EXPIRATION=%s\n", prefix, iso8601.Format(creds.Expires)) } + if region != "" { + fmt.Printf("%sAWS_REGION=%s\n", prefix, region) + } + return nil }