Skip to content

Commit

Permalink
fix: Quote identifiers in column grant/revoke statements
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonjackson authored and kda-jt committed Nov 28, 2022
1 parent 502474d commit 12214d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
8 changes: 8 additions & 0 deletions postgresql/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ func setToPgIdentList(schema string, idents *schema.Set) string {
return strings.Join(quotedIdents, ",")
}

func setToPgIdentListWithoutSchema(idents *schema.Set) string {
quotedIdents := make([]string, idents.Len())
for i, ident := range idents.List() {
quotedIdents[i] = pq.QuoteIdentifier(ident.(string))
}
return strings.Join(quotedIdents, ",")
}

func setToPgIdentSimpleList(idents *schema.Set) string {
quotedIdents := make([]string, idents.Len())
for i, ident := range idents.List() {
Expand Down
8 changes: 2 additions & 6 deletions postgresql/resource_postgresql_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,14 +554,10 @@ func createGrantQuery(d *schema.ResourceData, privileges []string) string {
)
case "COLUMN":
objects := d.Get("objects").(*schema.Set)
columns := []string{}
for _, col := range d.Get("columns").(*schema.Set).List() {
columns = append(columns, col.(string))
}
query = fmt.Sprintf(
"GRANT %s (%s) ON TABLE %s TO %s",
strings.Join(privileges, ","),
strings.Join(columns, ","),
setToPgIdentListWithoutSchema(d.Get("columns").(*schema.Set)),
setToPgIdentList(d.Get("schema").(string), objects),
pq.QuoteIdentifier(d.Get("role").(string)),
)
Expand Down Expand Up @@ -634,7 +630,7 @@ func createRevokeQuery(d *schema.ResourceData) string {
query = fmt.Sprintf(
"REVOKE %s (%s) ON TABLE %s FROM %s",
setToPgIdentSimpleList(privileges),
setToPgIdentSimpleList(columns),
setToPgIdentListWithoutSchema(columns),
setToPgIdentList(d.Get("schema").(string), objects),
pq.QuoteIdentifier(d.Get("role").(string)),
)
Expand Down
4 changes: 2 additions & 2 deletions postgresql/resource_postgresql_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestCreateGrantQuery(t *testing.T) {
"role": roleName,
}),
privileges: []string{"SELECT"},
expected: fmt.Sprintf(`GRANT SELECT (col2,col1) ON TABLE %[1]s."o1" TO %s`, pq.QuoteIdentifier(databaseName), pq.QuoteIdentifier(roleName)),
expected: fmt.Sprintf(`GRANT SELECT (%[2]s,%[3]s) ON TABLE %[1]s."o1" TO %[4]s`, pq.QuoteIdentifier(databaseName), pq.QuoteIdentifier("col2"), pq.QuoteIdentifier("col1"), pq.QuoteIdentifier(roleName)),
},
{
resource: schema.TestResourceDataRaw(t, resourcePostgreSQLGrant().Schema, map[string]interface{}{
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestCreateRevokeQuery(t *testing.T) {
"role": roleName,
"privileges": []interface{}{"SELECT"},
}),
expected: fmt.Sprintf(`REVOKE SELECT (col2,col1) ON TABLE %[1]s."o1" FROM %s`, pq.QuoteIdentifier(databaseName), pq.QuoteIdentifier(roleName)),
expected: fmt.Sprintf(`REVOKE SELECT ("col2","col1") ON TABLE %[1]s."o1" FROM %s`, pq.QuoteIdentifier(databaseName), pq.QuoteIdentifier(roleName)),
},
{
resource: schema.TestResourceDataRaw(t, resourcePostgreSQLGrant().Schema, map[string]interface{}{
Expand Down

0 comments on commit 12214d4

Please sign in to comment.