From 9f8d3fe80a870ebb47d37894d59be2d5fc7aee01 Mon Sep 17 00:00:00 2001 From: Leandro Forain Date: Thu, 6 Oct 2022 16:00:56 +0000 Subject: [PATCH] Implements endpoint override optional argument --- README.md | 1 + aws_config.go | 14 +++++++------- aws_config_test.go | 10 +++++----- cmd/unicreds/main.go | 9 +++++---- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index d362cd7..05b59fc 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Flags: -r, --region=REGION Configure the AWS region -p, --profile=PROFILE Configure the AWS profile -R, --role=ROLE Specify an AWS role ARN to assume + -e, --endpoint=ENDPOINT Override default endpoint (e.g. Localstack) -t, --table="credential-store" DynamoDB table. -k, --alias="alias/credstash" KMS key alias. diff --git a/aws_config.go b/aws_config.go index 230ff70..fbee096 100644 --- a/aws_config.go +++ b/aws_config.go @@ -15,7 +15,7 @@ const ( // SetAwsConfig configure the AWS region with a fallback for discovery // on EC2 hosts. -func SetAwsConfig(region, profile *string, role *string) (err error) { +func SetAwsConfig(region, profile *string, role *string, endpoint *string) (err error) { if region == nil { // Try to get our region based on instance metadata region, err = getRegion() @@ -30,21 +30,21 @@ func SetAwsConfig(region, profile *string, role *string) (err error) { return fmt.Errorf("Must provide a region flag when specifying a profile") } - setAwsConfig(region, profile, role) + setAwsConfig(region, profile, role, endpoint) return nil } -func setAwsConfig(region, profile, role *string) { - log.WithFields(log.Fields{"region": aws.StringValue(region), "profile": aws.StringValue(profile)}).Debug("Configure AWS") +func setAwsConfig(region, profile, role *string, endpoint *string) { + log.WithFields(log.Fields{"region": aws.StringValue(region), "profile": aws.StringValue(profile), "endpoint": aws.StringValue(endpoint)}).Debug("Configure AWS") - sess := getAwsSession(region, profile, role) + sess := getAwsSession(region, profile, role, endpoint) SetDynamoDBSession(sess) SetKMSSession(sess) } -func getAwsSession(region, profile, role *string) *session.Session { - config := aws.Config{Region: region} +func getAwsSession(region, profile, role *string, endpoint *string) *session.Session { + config := aws.Config{Region: region, Endpoint: endpoint} // If no role is supplied, use the shared AWS config sess := session.Must(session.NewSessionWithOptions(session.Options{ diff --git a/aws_config_test.go b/aws_config_test.go index 3309af4..9035870 100644 --- a/aws_config_test.go +++ b/aws_config_test.go @@ -9,18 +9,18 @@ import ( func TestConfig(t *testing.T) { - err := SetAwsConfig(nil, nil, nil) + err := SetAwsConfig(nil, nil, nil, nil) assert.Nil(t, err) - err = SetAwsConfig(aws.String(""), aws.String(""), aws.String("")) + err = SetAwsConfig(aws.String(""), aws.String(""), aws.String(""), aws.String("")) assert.Nil(t, err) - err = SetAwsConfig(aws.String(""), aws.String("wolfeidau"), aws.String("")) + err = SetAwsConfig(aws.String(""), aws.String("wolfeidau"), aws.String(""), aws.String("")) assert.Error(t, err) - err = SetAwsConfig(aws.String("us-west-2"), aws.String("wolfeidau"), aws.String("")) + err = SetAwsConfig(aws.String("us-west-2"), aws.String("wolfeidau"), aws.String(""), aws.String("")) assert.Nil(t, err) - err = SetAwsConfig(aws.String("us-west-2"), aws.String("wolfeidau"), aws.String("role")) + err = SetAwsConfig(aws.String("us-west-2"), aws.String("wolfeidau"), aws.String("role"), aws.String("localstack")) assert.Nil(t, err) } diff --git a/cmd/unicreds/main.go b/cmd/unicreds/main.go index fdad4be..b373b14 100644 --- a/cmd/unicreds/main.go +++ b/cmd/unicreds/main.go @@ -21,9 +21,10 @@ var ( debug = app.Flag("debug", "Enable debug mode.").Short('d').Bool() logJSON = app.Flag("json", "Output results in JSON").Short('j').Bool() - region = app.Flag("region", "Configure the AWS region").Short('r').String() - profile = app.Flag("profile", "Configure the AWS profile").Short('p').String() - role = app.Flag("role", "Specify an AWS role ARN to assume").Short('R').String() + region = app.Flag("region", "Configure the AWS region").Short('r').String() + profile = app.Flag("profile", "Configure the AWS profile").Short('p').String() + role = app.Flag("role", "Specify an AWS role ARN to assume").Short('R').String() + endpoint = app.Flag("endpoint", "Override default endpoint (e.g. Localstack)").Short('e').String() dynamoTable = app.Flag("table", "DynamoDB table.").Default("credential-store").OverrideDefaultFromEnvar("UNICREDS_TABLE").Short('t').String() alias = app.Flag("alias", "KMS key alias.").Default("alias/credstash").OverrideDefaultFromEnvar("UNICREDS_ALIAS").Short('k').String() @@ -80,7 +81,7 @@ func main() { log.SetLevel(log.DebugLevel) } - unicreds.SetAwsConfig(region, profile, role) + unicreds.SetAwsConfig(region, profile, role, endpoint) switch command { case cmdSetup.FullCommand():