Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements endpoint override optional argument #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions aws_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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{
Expand Down
10 changes: 5 additions & 5 deletions aws_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 5 additions & 4 deletions cmd/unicreds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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():
Expand Down