Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit 8ae4453

Browse files
committed
Fix conflicting superuser and syslog_access fields
1 parent 32cd1c8 commit 8ae4453

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ It's published on the [Terraform registry](https://registry.terraform.io/provide
66

77
## Requirements
88

9-
- [Terraform](https://www.terraform.io/downloads.html) 0.12.x
10-
- [Go](https://golang.org/doc/install) 1.16 (to build the provider plugin)
9+
- [Terraform](https://www.terraform.io/downloads.html) >= 1.0
10+
- [Go](https://golang.org/doc/install) 1.17 (to build the provider plugin)
1111

1212
## Building The Provider
1313

docs/data-sources/user.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ data "redshift_user" "user" {
3333

3434
- **connection_limit** (Number) The maximum number of database connections the user is permitted to have open concurrently. The limit isn't enforced for superusers.
3535
- **create_database** (Boolean) Indicates whether the user is allowed to create new databases.
36+
- **session_timeout** (Number) The maximum time in seconds that a session remains inactive or idle. The range is 60 seconds (one minute) to 1,728,000 seconds (20 days). If no session timeout is set for the user, the cluster setting applies.
3637
- **superuser** (Boolean) Indicates whether the user is a superuser with all database privileges.
3738
- **syslog_access** (String) A clause that specifies the level of access that the user has to the Amazon Redshift system tables and views. If `RESTRICTED` (default) is specified, the user can see only the rows generated by that user in user-visible system tables and views. If `UNRESTRICTED` is specified, the user can see all rows in user-visible system tables and views, including rows generated by another user. `UNRESTRICTED` doesn't give a regular user access to superuser-visible tables. Only superusers can see superuser-visible tables.
3839
- **valid_until** (String) Date and time after which the user's password is no longer valid. By default the password has no time limit.

docs/resources/user.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ resource "redshift_user" "user_with_unrestricted_syslog" {
3838
- **create_database** (Boolean) Allows the user to create new databases. By default user can't create new databases.
3939
- **id** (String) The ID of this resource.
4040
- **password** (String, Sensitive) Sets the user's password. Users can change their own passwords, unless the password is disabled. To disable password, omit this parameter or set it to `null`.
41+
- **session_timeout** (Number) The maximum time in seconds that a session remains inactive or idle. The range is 60 seconds (one minute) to 1,728,000 seconds (20 days). If no session timeout is set for the user, the cluster setting applies.
4142
- **superuser** (Boolean) Determine whether the user is a superuser with all database privileges.
4243
- **syslog_access** (String) A clause that specifies the level of access that the user has to the Amazon Redshift system tables and views. If `RESTRICTED` (default) is specified, the user can see only the rows generated by that user in user-visible system tables and views. If `UNRESTRICTED` is specified, the user can see all rows in user-visible system tables and views, including rows generated by another user. `UNRESTRICTED` doesn't give a regular user access to superuser-visible tables. Only superusers can see superuser-visible tables.
4344
- **valid_until** (String) Sets a date and time after which the user's password is no longer valid. By default the password has no time limit.

redshift/resource_redshift_user.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,23 @@ Amazon Redshift user accounts can only be created and dropped by a database supe
5454
),
5555
Exists: RedshiftResourceExistsFunc(resourceRedshiftUserExists),
5656
Importer: &schema.ResourceImporter{
57-
State: schema.ImportStatePassthrough,
57+
StateContext: schema.ImportStatePassthroughContext,
5858
},
5959
CustomizeDiff: func(_ context.Context, d *schema.ResourceDiff, p interface{}) error {
6060
isSuperuser := d.Get(userSuperuserAttr).(bool)
61-
isPasswordKnown := d.NewValueKnown(userPasswordAttr)
6261

62+
isPasswordKnown := d.NewValueKnown(userPasswordAttr)
6363
password, hasPassword := d.GetOk(userPasswordAttr)
6464
if isSuperuser && isPasswordKnown && (!hasPassword || password.(string) == "") {
6565
return fmt.Errorf("Users that are superusers must define a password.")
6666
}
6767

68+
isSyslogAccessKnown := d.NewValueKnown(userSyslogAccessAttr)
69+
syslogAccess, hasSyslogAccess := d.GetOk(userSyslogAccessAttr)
70+
if isSuperuser && isSyslogAccessKnown && hasSyslogAccess && syslogAccess != defaultUserSuperuserSyslogAccess {
71+
return fmt.Errorf("Superusers must have syslog access set to %s.", defaultUserSuperuserSyslogAccess)
72+
}
73+
6874
return nil
6975
},
7076

@@ -118,11 +124,10 @@ Amazon Redshift user accounts can only be created and dropped by a database supe
118124
},
119125
},
120126
userSuperuserAttr: {
121-
ConflictsWith: []string{userSyslogAccessAttr},
122-
Type: schema.TypeBool,
123-
Optional: true,
124-
Default: false,
125-
Description: `Determine whether the user is a superuser with all database privileges.`,
127+
Type: schema.TypeBool,
128+
Optional: true,
129+
Default: false,
130+
Description: `Determine whether the user is a superuser with all database privileges.`,
126131
},
127132
userSessionTimeoutAttr: {
128133
Type: schema.TypeInt,

redshift/resource_redshift_user_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,63 @@ resource "redshift_user" "superuser" {
234234
})
235235
}
236236

237+
func TestAccRedshiftUser_SuperuserSyslogAccess(t *testing.T) {
238+
tests := map[string]struct {
239+
isSuperuser bool
240+
syslogAccess string
241+
expectError *regexp.Regexp
242+
}{
243+
"(not superuser) UNRESTRICTED syslog access": {
244+
isSuperuser: false,
245+
syslogAccess: defaultUserSuperuserSyslogAccess,
246+
},
247+
"(not superuser) RESTRICTED syslog access": {
248+
isSuperuser: false,
249+
syslogAccess: defaultUserSyslogAccess,
250+
},
251+
"(superuser) RESTRICTED syslog access": {
252+
isSuperuser: true,
253+
syslogAccess: defaultUserSyslogAccess,
254+
expectError: regexp.MustCompile("Superusers must have syslog access set to UNRESTRICTED."),
255+
},
256+
"(superuser) UNRESTRICTED syslog access": {
257+
isSuperuser: true,
258+
syslogAccess: defaultUserSuperuserSyslogAccess,
259+
},
260+
}
261+
262+
for name, test := range tests {
263+
t.Run(name, func(t *testing.T) {
264+
userName := strings.ReplaceAll(acctest.RandomWithPrefix("tf_acc_superuser"), "-", "_")
265+
config := fmt.Sprintf(`
266+
locals {
267+
is_superuser = %[2]t
268+
}
269+
270+
resource "redshift_user" "superuser" {
271+
name = %[1]q
272+
superuser = local.is_superuser
273+
password = "foobar12355#"
274+
syslog_access = %[3]q
275+
}
276+
`, userName, test.isSuperuser, test.syslogAccess)
277+
278+
resource.Test(t, resource.TestCase{
279+
PreCheck: func() { testAccPreCheck(t) },
280+
Providers: testAccProviders,
281+
CheckDestroy: testAccCheckRedshiftUserDestroy,
282+
Steps: []resource.TestStep{
283+
{
284+
Config: config,
285+
ExpectError: test.expectError,
286+
},
287+
},
288+
})
289+
})
290+
}
291+
292+
}
293+
237294
func TestAccRedshiftUser_SuperuserUnknownPassword(t *testing.T) {
238295
userName := strings.ReplaceAll(acctest.RandomWithPrefix("tf_acc_superuser"), "-", "_")
239296
config := fmt.Sprintf(`

0 commit comments

Comments
 (0)