Skip to content

Commit d92a3ca

Browse files
committed
Before revoking a privilege from a schema, check to ensure role exists.
1 parent 6c91676 commit d92a3ca

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

builtin/providers/postgresql/resource_postgresql_schema.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,18 @@ func setSchemaPolicy(txn *sql.Tx, d *schema.ResourceData) error {
350350
for _, p := range dropped {
351351
pMap := p.(map[string]interface{})
352352
rolePolicy := schemaPolicyToACL(pMap)
353-
queries = append(queries, rolePolicy.Revokes(schemaName)...)
353+
354+
var foundUser bool
355+
err := txn.QueryRow(`SELECT TRUE FROM pg_catalog.pg_user WHERE usename = $1`, rolePolicy.Role).Scan(&foundUser)
356+
switch {
357+
case err == sql.ErrNoRows:
358+
// Don't execute this role's REVOKEs because the role
359+
// was dropped first and therefore doesn't exist.
360+
case err != nil:
361+
return errwrap.Wrapf("Error reading schema: {{err}}", err)
362+
default:
363+
queries = append(queries, rolePolicy.Revokes(schemaName)...)
364+
}
354365
}
355366

356367
for _, p := range added {

builtin/providers/postgresql/resource_postgresql_schema_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ func TestAccPostgresqlSchema_AddPolicy(t *testing.T) {
6969
resource.TestCheckResourceAttr("postgresql_role.policy_move", "name", "policy_move"),
7070

7171
resource.TestCheckResourceAttr("postgresql_role.all_with_grantstay", "name", "all_with_grantstay"),
72-
// resource.TestCheckResourceAttr("postgresql_role.all_with_grantdrop", "name", "all_with_grantdrop"),
72+
resource.TestCheckResourceAttr("postgresql_role.all_with_grantdrop", "name", "all_with_grantdrop"),
7373

7474
resource.TestCheckResourceAttr("postgresql_schema.test4", "name", "test4"),
7575
resource.TestCheckResourceAttr("postgresql_schema.test4", "owner", "all_without_grant_stay"),
76-
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.#", "6"),
76+
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.#", "7"),
7777
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.create", "false"),
7878
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.create_with_grant", "true"),
7979
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.108605972.role", "all_with_grantstay"),
@@ -99,11 +99,11 @@ func TestAccPostgresqlSchema_AddPolicy(t *testing.T) {
9999
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.role", "policy_compose"),
100100
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.usage", "false"),
101101
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.3959936977.usage_with_grant", "true"),
102-
// resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.create", "false"),
103-
// resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.create_with_grant", "true"),
104-
// resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.role", "all_with_grantdrop"),
105-
// resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.usage", "false"),
106-
// resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.usage_with_grant", "true"),
102+
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.create", "false"),
103+
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.create_with_grant", "true"),
104+
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.role", "all_with_grantdrop"),
105+
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.usage", "false"),
106+
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.4178211897.usage_with_grant", "true"),
107107
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.create", "true"),
108108
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.create_with_grant", "false"),
109109
resource.TestCheckResourceAttr("postgresql_schema.test4", "policy.815478369.role", "policy_compose"),
@@ -298,9 +298,9 @@ resource "postgresql_role" "all_with_grantstay" {
298298
name = "all_with_grantstay"
299299
}
300300
301-
// resource "postgresql_role" "all_with_grantdrop" {
302-
// name = "all_with_grantdrop"
303-
// }
301+
resource "postgresql_role" "all_with_grantdrop" {
302+
name = "all_with_grantdrop"
303+
}
304304
305305
resource "postgresql_schema" "test4" {
306306
name = "test4"
@@ -336,11 +336,11 @@ resource "postgresql_schema" "test4" {
336336
role = "${postgresql_role.all_with_grantstay.name}"
337337
}
338338
339-
// policy {
340-
// create_with_grant = true
341-
// usage_with_grant = true
342-
// role = "${postgresql_role.all_with_grantdrop.name}"
343-
// }
339+
policy {
340+
create_with_grant = true
341+
usage_with_grant = true
342+
role = "${postgresql_role.all_with_grantdrop.name}"
343+
}
344344
345345
policy {
346346
create_with_grant = true

0 commit comments

Comments
 (0)