Skip to content

Commit

Permalink
Merge pull request #1146 from 99designs/add-region
Browse files Browse the repository at this point in the history
Add region to `aws-vault export` output
  • Loading branch information
mtibben committed Feb 20, 2023
2 parents 017dcf7 + eb1a23a commit 795b13a
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions cli/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"

Expand All @@ -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 {
Expand Down Expand Up @@ -109,11 +111,11 @@ 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 ")
return printEnv(input, credsProvider, config.Region, "export ")
} else {
return printEnv(input, credsProvider, "")
return printEnv(input, credsProvider, config.Region, "")
}
}

Expand Down Expand Up @@ -154,26 +156,44 @@ 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) {
if val != "" {
_, 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)

if creds.SessionToken != "" {
fmt.Printf("aws_session_token=%s\n", creds.SessionToken)
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)
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))
}
mustNewKey(s, "region", region)

_, err = f.WriteTo(os.Stdout)
if err != nil {
return fmt.Errorf("Failed to output ini: %w", err)
}

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)
Expand All @@ -188,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
}

0 comments on commit 795b13a

Please sign in to comment.