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

Commit 86d5653

Browse files
committed
Make the 'public' value for group case insensitive
1 parent 23b3d4a commit 86d5653

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

docs/resources/grant.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ resource "redshift_grant" "user" {
3838
3939
# Granting permission to PUBLIC (GRANT ... TO PUBLIC)
4040
resource "redshift_grant" "public" {
41-
group = "public" // "public" here indicates we want grant TO PUBLIC, not "public" group.
42-
41+
group = "public" // "public" or "PUBLIC" (it is case insensitive for this case) here indicates we want grant TO PUBLIC, not "public" group which cannot even be created in Redshift (keyword).
4342
schema = "my_schema"
4443
object_type = "schema"
4544
privileges = ["usage"]
@@ -56,7 +55,7 @@ resource "redshift_grant" "public" {
5655

5756
### Optional
5857

59-
- **group** (String) The name of the group to grant privileges on. Either `group` or `user` parameter must be set. Settings the group name to `public` will result in a `GRANT ... TO PUBLIC` statement.
58+
- **group** (String) The name of the group to grant privileges on. Either `group` or `user` parameter must be set. Settings the group name to `public` or `PUBLIC` (it is case insensitive in this case) will result in a `GRANT ... TO PUBLIC` statement.
6059
- **id** (String) The ID of this resource.
6160
- **objects** (Set of String) The objects upon which to grant the privileges. An empty list (the default) means to grant permissions on all objects of the specified type. Ignored when `object_type` is one of (`database`, `schema`).
6261
- **schema** (String) The database schema to grant privileges on.

examples/resources/redshift_grant/resource.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ resource "redshift_grant" "user" {
2323

2424
# Granting permission to PUBLIC (GRANT ... TO PUBLIC)
2525
resource "redshift_grant" "public" {
26-
group = "public" // "public" here indicates we want grant TO PUBLIC, not "public" group.
27-
26+
group = "public" // "public" or "PUBLIC" (it is case insensitive for this case) here indicates we want grant TO PUBLIC, not "public" group which cannot even be created in Redshift (keyword).
2827
schema = "my_schema"
2928
object_type = "schema"
3029
privileges = ["usage"]

redshift/resource_redshift_grant.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"fmt"
66
"log"
7+
"regexp"
78
"strings"
89

910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -62,13 +63,21 @@ Defines access privileges for users and groups. Privileges include access optio
6263
ForceNew: true,
6364
ExactlyOneOf: []string{grantUserAttr, grantGroupAttr},
6465
Description: "The name of the user to grant privileges on. Either `user` or `group` parameter must be set.",
66+
ValidateFunc: validation.StringDoesNotMatch(regexp.MustCompile("^(?i)public$"), "User name cannot be 'public'. To use GRANT ... TO PUBLIC set the group name to 'public' instead."),
6567
},
6668
grantGroupAttr: {
6769
Type: schema.TypeString,
6870
Optional: true,
6971
ForceNew: true,
7072
ExactlyOneOf: []string{grantUserAttr, grantGroupAttr},
71-
Description: "The name of the group to grant privileges on. Either `group` or `user` parameter must be set. Settings the group name to `public` will result in a `GRANT ... TO PUBLIC` statement.",
73+
Description: "The name of the group to grant privileges on. Either `group` or `user` parameter must be set. Settings the group name to `public` or `PUBLIC` (it is case insensitive in this case) will result in a `GRANT ... TO PUBLIC` statement.",
74+
StateFunc: func(val interface{}) string {
75+
name := val.(string)
76+
if strings.ToLower(name) == grantToPublicName {
77+
return strings.ToLower(name)
78+
}
79+
return name
80+
},
7281
},
7382
grantSchemaAttr: {
7483
Type: schema.TypeString,
@@ -235,7 +244,7 @@ func readDatabaseGrants(db *DBConnection, d *schema.ResourceData) error {
235244
queryArgs := []interface{}{db.client.databaseName, entityName}
236245

237246
// Handle GRANT TO PUBLIC
238-
if entityName == grantToPublicName {
247+
if isGrantToPublic(d) {
239248
query = `
240249
SELECT
241250
decode(charindex('C',split_part(split_part(regexp_replace(replace(array_to_string(db.datacl, '|'), '"', ''),'[^|+]=','__avoidUserPrivs__'), '=', 2) ,'/',1)), 0,0,1) as create,
@@ -296,7 +305,7 @@ func readSchemaGrants(db *DBConnection, d *schema.ResourceData) error {
296305
queryArgs := []interface{}{schemaName, entityName}
297306

298307
// Handle GRANT TO PUBLIC
299-
if entityName == grantToPublicName {
308+
if isGrantToPublic(d) {
300309
query = `
301310
SELECT
302311
decode(charindex('C',split_part(split_part(regexp_replace(replace(array_to_string(ns.nspacl, '|'), '"', ''),'[^|+]=','__avoidUserPrivs__'), '=', 2) ,'/',1)), 0,0,1) as create,
@@ -376,7 +385,7 @@ func readTableGrants(db *DBConnection, d *schema.ResourceData) error {
376385
pq.Array(grantObjectTypesCodes["table"]), entityName, schemaName,
377386
}
378387

379-
if entityName == grantToPublicName {
388+
if isGrantToPublic(d) {
380389
query = `
381390
SELECT
382391
relname,
@@ -498,7 +507,7 @@ func readCallableGrants(db *DBConnection, d *schema.ResourceData) error {
498507
schemaName, entityName, pq.Array(grantObjectTypesCodes[objectType]),
499508
}
500509

501-
if entityName == grantToPublicName {
510+
if isGrantToPublic(d) {
502511
query = `
503512
SELECT
504513
proname,
@@ -586,7 +595,7 @@ func readLanguageGrants(db *DBConnection, d *schema.ResourceData) error {
586595
queryArgs := []interface{}{entityName}
587596

588597
// Handle GRANT TO PUBLIC
589-
if entityName == grantToPublicName {
598+
if isGrantToPublic(d) {
590599
query = `
591600
SELECT
592601
lanname,
@@ -659,7 +668,7 @@ func createGrantsRevokeQuery(d *schema.ResourceData, databaseName string) string
659668
}
660669

661670
fromEntityName := pq.QuoteIdentifier(entityName)
662-
if entityName == grantToPublicName {
671+
if isGrantToPublic(d) {
663672
toWhomIndicator = ""
664673
fromEntityName = "PUBLIC"
665674
}
@@ -745,7 +754,7 @@ func createGrantsQuery(d *schema.ResourceData, databaseName string) string {
745754
}
746755

747756
toEntityName := pq.QuoteIdentifier(entityName)
748-
if entityName == grantToPublicName {
757+
if isGrantToPublic(d) {
749758
toWhomIndicator = ""
750759
toEntityName = "PUBLIC"
751760
}
@@ -815,11 +824,26 @@ func createGrantsQuery(d *schema.ResourceData, databaseName string) string {
815824
return query
816825
}
817826

827+
func isGrantToPublic(d *schema.ResourceData) bool {
828+
if _, isGroup := d.GetOk(grantGroupAttr); isGroup {
829+
entityName := d.Get(grantGroupAttr).(string)
830+
831+
return strings.ToLower(entityName) == grantToPublicName
832+
}
833+
834+
return false
835+
}
836+
818837
func generateGrantID(d *schema.ResourceData) string {
819838
parts := []string{}
820839

821840
if _, isGroup := d.GetOk(grantGroupAttr); isGroup {
822-
parts = append(parts, fmt.Sprintf("gn:%s", d.Get(grantGroupAttr).(string)))
841+
name := d.Get(grantGroupAttr).(string)
842+
if isGrantToPublic(d) {
843+
name = strings.ToLower(name)
844+
}
845+
846+
parts = append(parts, fmt.Sprintf("gn:%s", name))
823847
}
824848

825849
if _, isUser := d.GetOk(grantUserAttr); isUser {

redshift/resource_redshift_grant_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ resource "redshift_schema" "test" {
1919
}
2020
2121
resource "redshift_grant" "public" {
22-
group = "public"
22+
group = "PUBLIC"
2323
2424
schema = %[1]q
2525
object_type = "schema"

0 commit comments

Comments
 (0)