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

Bug fixes related to temporary credentials #11

Merged
merged 1 commit into from
Jul 16, 2021
Merged
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
25 changes: 14 additions & 11 deletions redshift/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,21 @@ func resolveCredentials(d *schema.ResourceData) (string, string, error) {
if (!ok) || username == nil {
return "", "", fmt.Errorf("Username is required")
}
if password, passwordIsSet := d.GetOk("password"); passwordIsSet {
password, passwordIsSet := d.GetOk("password")
_, clusterIdentifierIsSet := d.GetOk("temporary_credentials.0.cluster_identifier")
if !passwordIsSet && !clusterIdentifierIsSet {
return "", "", fmt.Errorf("password or temporary_credentials must be configured")
}
if passwordIsSet {
if password.(string) != "" {
log.Println("[DEBUG] using password authentication")
return username.(string), password.(string), nil
}
}
log.Println("[DEBUG] using temporary credentials authentication")
dbUser, password, err := temporaryCredentials(username.(string), d)
dbUser, dbPassword, err := temporaryCredentials(username.(string), d)
log.Printf("[DEBUG] got temporary credentials with username %s\n", dbUser)
return dbUser, password, err
return dbUser, dbPassword, err
}

// temporaryCredentials gets temporary credentials using GetClusterCredentials
Expand All @@ -172,21 +177,19 @@ func temporaryCredentials(username string, d *schema.ResourceData) (string, stri
if err != nil {
return "", "", err
}

config, ok := d.Get("temporary_credentials").([]interface{})
if (!ok) || config == nil {
clusterIdentifier, clusterIdentifierIsSet := d.GetOk("temporary_credentials.0.cluster_identifier")
if !clusterIdentifierIsSet {
return "", "", fmt.Errorf("temporary_credentials not configured")
}
c := config[0].(map[string]interface{})
input := &redshift.GetClusterCredentialsInput{
ClusterIdentifier: aws.String(c["cluster_identifier"].(string)),
ClusterIdentifier: aws.String(clusterIdentifier.(string)),
DbName: aws.String(d.Get("database").(string)),
DbUser: aws.String(username),
}
if autoCreateUser, ok := c["auto_create_user"]; ok && autoCreateUser != nil {
if autoCreateUser, ok := d.GetOk("temporary_credentials.0.auto_create_user"); ok {
input.AutoCreate = aws.Bool(autoCreateUser.(bool))
}
if dbGroups, ok := c["db_groups"]; ok {
if dbGroups, ok := d.GetOk("temporary_credentials.0.db_groups"); ok {
if dbGroups != nil {
dbGroupsList := dbGroups.(*schema.Set).List()
if len(dbGroupsList) > 0 {
Expand All @@ -200,7 +203,7 @@ func temporaryCredentials(username string, d *schema.ResourceData) (string, stri
}
}
}
if durationSeconds, ok := c["duration_seconds"]; ok {
if durationSeconds, ok := d.GetOk("temporary_credentials.0.duration_seconds"); ok {
duration := durationSeconds.(int)
if duration > 0 {
input.DurationSeconds = aws.Int32(int32(duration))
Expand Down
5 changes: 5 additions & 0 deletions redshift/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"
"testing"

"github.com/aws/aws-sdk-go-v2/config"
Expand Down Expand Up @@ -91,6 +92,10 @@ func TestAccRedshiftTemporaryCredentials(t *testing.T) {
redshift_password := os.Getenv("REDSHIFT_PASSWORD")
defer os.Setenv("REDSHIFT_PASSWORD", redshift_password)
os.Unsetenv("REDSHIFT_PASSWORD")
rawUsername := os.Getenv("REDSHIFT_USER")
defer os.Setenv("REDSHIFT_USER", rawUsername)
username := strings.ToLower(permanentUsername(rawUsername))
os.Setenv("REDSHIFT_USER", username)
initTemporaryCredentialsProvider(t, provider)
client, ok := provider.Meta().(*Client)
if !ok {
Expand Down