This repository was archived by the owner on Jan 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathprovider.go
More file actions
235 lines (226 loc) · 8.48 KB
/
provider.go
File metadata and controls
235 lines (226 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package redshift
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/redshift"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
const (
defaultProviderMaxOpenConnections = 20
)
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Description: "Name of Redshift server address to connect to.",
Required: true,
DefaultFunc: schema.EnvDefaultFunc("REDSHIFT_HOST", ""),
},
"username": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDSHIFT_USER", "root"),
Description: "Redshift user name to connect as.",
},
"password": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDSHIFT_PASSWORD", nil),
Description: "Password to be used if the Redshift server demands password authentication.",
Sensitive: true,
ConflictsWith: []string{
"temporary_credentials",
},
},
"port": {
Type: schema.TypeInt,
Description: "The Redshift port number to connect to at the server host.",
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDSHIFT_PORT", 5439),
},
"sslmode": {
Type: schema.TypeString,
Description: "This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the Redshift server. Valid values are `require` (default, always SSL, also skip verification), `verify-ca` (always SSL, verify that the certificate presented by the server was signed by a trusted CA), `verify-full` (always SSL, verify that the certification presented by the server was signed by a trusted CA and the server host name matches the one in the certificate), `disable` (no SSL).",
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("REDSHIFT_SSLMODE", "require"),
ValidateFunc: validation.StringInSlice([]string{
"require",
"disable",
"verify-ca",
"verify-full",
}, false),
},
"database": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the database to connect to. The default is `redshift`.",
DefaultFunc: schema.EnvDefaultFunc("REDSHIFT_DATABASE", "redshift"),
},
"max_connections": {
Type: schema.TypeInt,
Optional: true,
Default: defaultProviderMaxOpenConnections,
Description: "Maximum number of connections to establish to the database. Zero means unlimited.",
ValidateFunc: validation.IntAtLeast(-1),
},
"temporary_credentials": {
Type: schema.TypeList,
Optional: true,
Description: "Configuration for obtaining a temporary password using redshift:GetClusterCredentials",
MaxItems: 1,
ConflictsWith: []string{
"password",
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cluster_identifier": {
Type: schema.TypeString,
Required: true,
Description: "The unique identifier of the cluster that contains the database for which you are requesting credentials. This parameter is case sensitive.",
ValidateFunc: validation.StringLenBetween(1, 2147483647),
},
"auto_create_user": {
Type: schema.TypeBool,
Optional: true,
Description: "Create a database user with the name specified for the user if one does not exist.",
Default: false,
},
"db_groups": {
Type: schema.TypeSet,
Set: schema.HashString,
Optional: true,
Description: "A list of the names of existing database groups that the user will join for the current session, in addition to any group memberships for an existing user. If not specified, a new user is added only to PUBLIC.",
MaxItems: 2147483647,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: dbGroupValidate,
},
},
"duration_seconds": {
Type: schema.TypeInt,
Optional: true,
Description: "The number of seconds until the returned temporary password expires.",
ValidateFunc: validation.IntBetween(900, 3600),
},
},
},
},
},
ResourcesMap: map[string]*schema.Resource{
"redshift_user": redshiftUser(),
"redshift_group": redshiftGroup(),
"redshift_schema": redshiftSchema(),
"redshift_privilege": redshiftPrivilege(),
"redshift_default_privileges": redshiftDefaultPrivileges(),
"redshift_grant": redshiftGrant(),
"redshift_database": redshiftDatabase(),
"redshift_datashare": redshiftDatashare(),
"redshift_datashare_privilege": redshiftDatasharePrivilege(),
},
DataSourcesMap: map[string]*schema.Resource{
"redshift_user": dataSourceRedshiftUser(),
"redshift_group": dataSourceRedshiftGroup(),
"redshift_schema": dataSourceRedshiftSchema(),
"redshift_database": dataSourceRedshiftDatabase(),
"redshift_namespace": dataSourceRedshiftNamespace(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
username, password, err := resolveCredentials(d)
if err != nil {
return nil, err
}
config := Config{
Host: d.Get("host").(string),
Port: d.Get("port").(int),
Username: username,
Password: password,
Database: d.Get("database").(string),
SSLMode: d.Get("sslmode").(string),
MaxConns: d.Get("max_connections").(int),
}
log.Println("[DEBUG] creating database client")
client := config.NewClient(d.Get("database").(string))
log.Println("[DEBUG] created database client")
return client, nil
}
func resolveCredentials(d *schema.ResourceData) (string, string, error) {
username, ok := d.GetOk("username")
if (!ok) || username == nil {
return "", "", fmt.Errorf("Username is required")
}
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, dbPassword, err := temporaryCredentials(username.(string), d)
log.Printf("[DEBUG] got temporary credentials with username %s\n", dbUser)
return dbUser, dbPassword, err
}
// temporaryCredentials gets temporary credentials using GetClusterCredentials
func temporaryCredentials(username string, d *schema.ResourceData) (string, string, error) {
sdkClient, err := redshiftSdkClient(d)
if err != nil {
return "", "", err
}
clusterIdentifier, clusterIdentifierIsSet := d.GetOk("temporary_credentials.0.cluster_identifier")
if !clusterIdentifierIsSet {
return "", "", fmt.Errorf("temporary_credentials not configured")
}
input := &redshift.GetClusterCredentialsInput{
ClusterIdentifier: aws.String(clusterIdentifier.(string)),
DbName: aws.String(d.Get("database").(string)),
DbUser: aws.String(username),
}
if autoCreateUser, ok := d.GetOk("temporary_credentials.0.auto_create_user"); ok {
input.AutoCreate = aws.Bool(autoCreateUser.(bool))
}
if dbGroups, ok := d.GetOk("temporary_credentials.0.db_groups"); ok {
if dbGroups != nil {
dbGroupsList := dbGroups.(*schema.Set).List()
if len(dbGroupsList) > 0 {
var groups []string
for _, group := range dbGroupsList {
if group.(string) != "" {
groups = append(groups, group.(string))
}
}
input.DbGroups = groups
}
}
}
if durationSeconds, ok := d.GetOk("temporary_credentials.0.duration_seconds"); ok {
duration := durationSeconds.(int)
if duration > 0 {
input.DurationSeconds = aws.Int32(int32(duration))
}
}
log.Println("[DEBUG] making GetClusterCredentials request")
response, err := sdkClient.GetClusterCredentials(context.TODO(), input)
if err != nil {
return "", "", err
}
return aws.ToString(response.DbUser), aws.ToString(response.DbPassword), nil
}
func redshiftSdkClient(d *schema.ResourceData) (*redshift.Client, error) {
config, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, err
}
return redshift.NewFromConfig(config), nil
}