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

INTMDB-521: Add additional error handlers to STS login logic #1000

Merged
merged 4 commits into from
Jan 9, 2023
Merged
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
25 changes: 18 additions & 7 deletions mongodbatlas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
awsSecretAccessKey := d.Get("aws_secret_access_key").(string)
awsSessionToken := d.Get("aws_session_token").(string)
endpoint := d.Get("sts_endpoint").(string)
config, _ = configureCredentialsSTS(&config, secret, region, awsAccessKeyID, awsSecretAccessKey, awsSessionToken, endpoint)
var err error
config, err = configureCredentialsSTS(&config, secret, region, awsAccessKeyID, awsSecretAccessKey, awsSessionToken, endpoint)
if err != nil {
return nil, diag.FromErr(err)
}
}

return config.NewClient(ctx)
Expand All @@ -305,6 +309,7 @@ func configureCredentialsSTS(config *Config, secret, region, awsAccessKeyID, aws
ep, err := endpoints.GetSTSRegionalEndpoint("regional")
if err != nil {
fmt.Printf("GetSTSRegionalEndpoint error: %s", err)
return *config, err
}

sess := session.Must(session.NewSession(&aws.Config{
Expand All @@ -318,25 +323,31 @@ func configureCredentialsSTS(config *Config, secret, region, awsAccessKeyID, aws

_, err = sess.Config.Credentials.Get()
if err != nil {
fmt.Printf("Session get credentils error: %s", err)
fmt.Printf("Session get credentials error: %s", err)
return *config, err
}
_, err = creds.Get()
if err != nil {
fmt.Printf("STS get credentials error: %s", err)
return *config, err
}
secretString, err := secretsManagerGetSecretValue(sess, &aws.Config{Credentials: creds, Region: aws.String(region)}, secret)
if err != nil {
fmt.Printf("Get Secrets error: %s", err)
return *config, err
}
secretString := secretsManagerGetSecretValue(sess, &aws.Config{Credentials: creds, Region: aws.String(region)}, secret)

var secretData SecretData
err = json.Unmarshal([]byte(secretString), &secretData)
if err != nil {
return *config, nil
return *config, err
}
config.PublicKey = secretData.PublicKey
config.PrivateKey = secretData.PrivateKey
return *config, nil
}

func secretsManagerGetSecretValue(sess *session.Session, creds *aws.Config, secret string) string {
func secretsManagerGetSecretValue(sess *session.Session, creds *aws.Config, secret string) (string, error) {
svc := secretsmanager.New(sess, creds)
input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(secret),
Expand All @@ -363,11 +374,11 @@ func secretsManagerGetSecretValue(sess *session.Session, creds *aws.Config, secr
} else {
fmt.Println(err.Error())
}
return ""
return "", err
}

fmt.Println(result)
return *result.SecretString
return *result.SecretString, err
}

func encodeStateID(values map[string]string) string {
Expand Down