Skip to content

Commit

Permalink
Fix - preventing from non-idempotent reapplying grants for entities w…
Browse files Browse the repository at this point in the history
…ith ascii chars 33-126 in their names
  • Loading branch information
rg00d committed Nov 14, 2022
1 parent 0c99152 commit 5adf97d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [v1.0.3](https://github.com/brainly/terraform-provider-redshift/tree/v1.0.3) (2022-11-15)

[Full Changelog](https://github.com/brainly/terraform-provider-redshift/compare/v1.0.2...v1.0.3)

**Fixed bugs:**

- Fix non-idempotent reapplying grants for entities with ascii chars 33-126 in their names ([rg00d](https://github.com/rg00d))

## [v1.0.2](https://github.com/brainly/terraform-provider-redshift/tree/v1.0.2) (2022-09-02)

[Full Changelog](https://github.com/brainly/terraform-provider-redshift/compare/v1.0.1...v1.0.2)
Expand Down
56 changes: 28 additions & 28 deletions redshift/resource_redshift_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func readDatabaseGrants(db *DBConnection, d *schema.ResourceData) error {
entityName = d.Get(grantUserAttr).(string)
query = `
SELECT
decode(charindex('C',split_part(split_part(regexp_replace(array_to_string(db.datacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), u.usename||'=', 2) ,'/',1)), 0,0,1) as create,
decode(charindex('T',split_part(split_part(regexp_replace(array_to_string(db.datacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), u.usename||'=', 2) ,'/',1)), 0,0,1) as temporary
decode(charindex('C',split_part(split_part(replace(regexp_replace(array_to_string(db.datacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), '"', ''), u.usename||'=', 2) ,'/',1)), 0,0,1) as create,
decode(charindex('T',split_part(split_part(replace(regexp_replace(array_to_string(db.datacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), '"', ''), u.usename||'=', 2) ,'/',1)), 0,0,1) as temporary
FROM pg_database db, pg_user u
WHERE
db.datname=$1
Expand All @@ -221,8 +221,8 @@ func readDatabaseGrants(db *DBConnection, d *schema.ResourceData) error {
entityName = d.Get(grantGroupAttr).(string)
query = `
SELECT
decode(charindex('C',split_part(split_part(array_to_string(db.datacl, '|'),'group ' || gr.groname,2 ) ,'/',1)), 0,0,1) as create,
decode(charindex('T',split_part(split_part(array_to_string(db.datacl, '|'),'group ' || gr.groname,2 ) ,'/',1)), 0,0,1) as temporary
decode(charindex('C',split_part(split_part(replace(array_to_string(db.datacl, '|'), '"', ''),'group ' || gr.groname,2 ) ,'/',1)), 0,0,1) as create,
decode(charindex('T',split_part(split_part(replace(array_to_string(db.datacl, '|'), '"', ''),'group ' || gr.groname,2 ) ,'/',1)), 0,0,1) as temporary
FROM pg_database db, pg_group gr
WHERE
db.datname=$1
Expand Down Expand Up @@ -256,8 +256,8 @@ func readSchemaGrants(db *DBConnection, d *schema.ResourceData) error {
entityName = d.Get(grantUserAttr).(string)
query = `
SELECT
decode(charindex('C',split_part(split_part(regexp_replace(array_to_string(ns.nspacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), u.usename||'=', 2) ,'/',1)), 0,0,1) as create,
decode(charindex('U',split_part(split_part(regexp_replace(array_to_string(ns.nspacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), u.usename||'=', 2) ,'/',1)), 0,0,1) as usage
decode(charindex('C',split_part(split_part(replace(regexp_replace(array_to_string(ns.nspacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), '"', ''), u.usename||'=', 2) ,'/',1)), 0,0,1) as create,
decode(charindex('U',split_part(split_part(replace(regexp_replace(array_to_string(ns.nspacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), '"', ''), u.usename||'=', 2) ,'/',1)), 0,0,1) as usage
FROM pg_namespace ns, pg_user u
WHERE
ns.nspname=$1
Expand All @@ -267,8 +267,8 @@ func readSchemaGrants(db *DBConnection, d *schema.ResourceData) error {
entityName = d.Get(grantGroupAttr).(string)
query = `
SELECT
decode(charindex('C',split_part(split_part(array_to_string(ns.nspacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), 0,0,1) as create,
decode(charindex('U',split_part(split_part(array_to_string(ns.nspacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), 0,0,1) as usage
decode(charindex('C',split_part(split_part(replace(array_to_string(ns.nspacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), 0,0,1) as create,
decode(charindex('U',split_part(split_part(replace(array_to_string(ns.nspacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), 0,0,1) as usage
FROM pg_namespace ns, pg_group gr
WHERE
ns.nspname=$1
Expand Down Expand Up @@ -300,14 +300,14 @@ func readTableGrants(db *DBConnection, d *schema.ResourceData) error {
query = `
SELECT
relname,
decode(charindex('r',split_part(split_part(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as select,
decode(charindex('w',split_part(split_part(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as update,
decode(charindex('a',split_part(split_part(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as insert,
decode(charindex('d',split_part(split_part(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as delete,
decode(charindex('D',split_part(split_part(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as drop,
decode(charindex('x',split_part(split_part(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as references,
decode(charindex('R',split_part(split_part(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as rule,
decode(charindex('t',split_part(split_part(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as trigger
decode(charindex('r',split_part(split_part(replace(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), '"', ''), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as select,
decode(charindex('w',split_part(split_part(replace(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), '"', ''), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as update,
decode(charindex('a',split_part(split_part(replace(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), '"', ''), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as insert,
decode(charindex('d',split_part(split_part(replace(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), '"', ''), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as delete,
decode(charindex('D',split_part(split_part(replace(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), '"', ''), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as drop,
decode(charindex('x',split_part(split_part(replace(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), '"', ''), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as references,
decode(charindex('R',split_part(split_part(replace(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), '"', ''), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as rule,
decode(charindex('t',split_part(split_part(replace(regexp_replace(array_to_string(relacl, '|'),'group '||u.usename), '"', ''), u.usename||'=', 2) ,'/',1)),null,0,0,0,1) as trigger
FROM pg_user u, pg_class cl
JOIN pg_namespace nsp ON nsp.oid = cl.relnamespace
WHERE
Expand All @@ -320,14 +320,14 @@ func readTableGrants(db *DBConnection, d *schema.ResourceData) error {
query = `
SELECT
relname,
decode(charindex('r',split_part(split_part(array_to_string(relacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as select,
decode(charindex('w',split_part(split_part(array_to_string(relacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as update,
decode(charindex('a',split_part(split_part(array_to_string(relacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as insert,
decode(charindex('d',split_part(split_part(array_to_string(relacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as delete,
decode(charindex('D',split_part(split_part(array_to_string(relacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as drop,
decode(charindex('x',split_part(split_part(array_to_string(relacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as references,
decode(charindex('R',split_part(split_part(array_to_string(relacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as rule,
decode(charindex('t',split_part(split_part(array_to_string(relacl, '|'),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as trigger
decode(charindex('r',split_part(split_part(replace(array_to_string(relacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as select,
decode(charindex('w',split_part(split_part(replace(array_to_string(relacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as update,
decode(charindex('a',split_part(split_part(replace(array_to_string(relacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as insert,
decode(charindex('d',split_part(split_part(replace(array_to_string(relacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as delete,
decode(charindex('D',split_part(split_part(replace(array_to_string(relacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as drop,
decode(charindex('x',split_part(split_part(replace(array_to_string(relacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as references,
decode(charindex('R',split_part(split_part(replace(array_to_string(relacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as rule,
decode(charindex('t',split_part(split_part(replace(array_to_string(relacl, '|'), '"', ''),'group ' || gr.groname || '=',2 ) ,'/',1)), null,0, 0,0, 1) as trigger
FROM pg_group gr, pg_class cl
JOIN pg_namespace nsp ON nsp.oid = cl.relnamespace
WHERE
Expand Down Expand Up @@ -404,7 +404,7 @@ func readCallableGrants(db *DBConnection, d *schema.ResourceData) error {
query = `
SELECT
proname,
decode(nvl(charindex('X',split_part(split_part(regexp_replace(array_to_string(pr.proacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), u.usename||'=', 2) ,'/',1)), 0), 0,0,1) as execute
decode(nvl(charindex('X',split_part(split_part(regexp_replace(replace(array_to_string(pr.proacl, '|'), '"', ''),'group '||u.usename,'__avoidGroupPrivs__'), u.usename||'=', 2) ,'/',1)), 0), 0,0,1) as execute
FROM pg_proc_info pr
JOIN pg_namespace nsp ON nsp.oid = pr.pronamespace,
pg_user u
Expand All @@ -418,7 +418,7 @@ func readCallableGrants(db *DBConnection, d *schema.ResourceData) error {
query = `
SELECT
proname,
decode(nvl(charindex('X',split_part(split_part(array_to_string(pr.proacl, '|'),'group ' || gr.groname,2 ) ,'/',1)), 0), 0,0,1) as execute
decode(nvl(charindex('X',split_part(split_part(replace(array_to_string(pr.proacl, '|'), '"', ''),'group ' || gr.groname,2 ) ,'/',1)), 0), 0,0,1) as execute
FROM pg_proc_info pr
JOIN pg_namespace nsp ON nsp.oid = pr.pronamespace,
pg_group gr
Expand Down Expand Up @@ -480,7 +480,7 @@ func readLanguageGrants(db *DBConnection, d *schema.ResourceData) error {
query = `
SELECT
lanname,
decode(nvl(charindex('U',split_part(split_part(regexp_replace(array_to_string(lg.lanacl, '|'),'group '||u.usename,'__avoidGroupPrivs__'), u.usename||'=', 2) ,'/',1)), 0), 0,0,1) as usage
decode(nvl(charindex('U',split_part(split_part(regexp_replace(replace(array_to_string(lg.lanacl, '|'), '"', ''),'group '||u.usename,'__avoidGroupPrivs__'), u.usename||'=', 2) ,'/',1)), 0), 0,0,1) as usage
FROM pg_language lg, pg_user u
WHERE
u.usename=$1
Expand All @@ -490,7 +490,7 @@ func readLanguageGrants(db *DBConnection, d *schema.ResourceData) error {
query = `
SELECT
lanname,
decode(nvl(charindex('U',split_part(split_part(array_to_string(lg.lanacl, '|'),'group ' || gr.groname,2 ) ,'/',1)), 0), 0,0,1) as usage
decode(nvl(charindex('U',split_part(split_part(replace(array_to_string(lg.lanacl, '|'), '"', ''),'group ' || gr.groname,2 ) ,'/',1)), 0), 0,0,1) as usage
FROM pg_language lg, pg_group gr
WHERE
gr.groname=$1
Expand Down

0 comments on commit 5adf97d

Please sign in to comment.