Skip to content

Commit 1df64cf

Browse files
postgresql_default_privileges: Allow empty privileges list (#118)
* allow empty list for postgresql_default_privileges * add test for empty list in postgresql_default_privileges * update docs for empty list in postgresql_default_privileges * Duplicate new test step at the end to assert it revokes correctly. Co-authored-by: Cyril Gaudin <[email protected]>
1 parent c9f451f commit 1df64cf

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

postgresql/resource_postgresql_default_privileges.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ func resourcePostgreSQLDefaultPrivileges() *schema.Resource {
5757
}, false),
5858
Description: "The PostgreSQL object type to set the default privileges on (one of: table, sequence, function, type)",
5959
},
60-
"privileges": &schema.Schema{
60+
"privileges": {
6161
Type: schema.TypeSet,
6262
Required: true,
6363
Elem: &schema.Schema{Type: schema.TypeString},
6464
Set: schema.HashString,
65-
MinItems: 1,
6665
Description: "The list of privileges to apply as default privileges",
6766
},
6867
"with_grant_option": {
@@ -183,6 +182,7 @@ func readRoleDefaultPrivileges(txn *sql.Tx, d *schema.ResourceData) error {
183182
owner := d.Get("owner").(string)
184183
pgSchema := d.Get("schema").(string)
185184
objectType := d.Get("object_type").(string)
185+
privilegesInput := d.Get("privileges").(*schema.Set).List()
186186

187187
if err := pgLockRole(txn, owner); err != nil {
188188
return err
@@ -226,11 +226,13 @@ func readRoleDefaultPrivileges(txn *sql.Tx, d *schema.ResourceData) error {
226226
return fmt.Errorf("could not read default privileges: %w", err)
227227
}
228228

229-
// We consider no privileges as "not exists"
229+
// We consider no privileges as "not exists" unless no privileges were provided as input
230230
if len(privileges) == 0 {
231231
log.Printf("[DEBUG] no default privileges for role %s in schema %s", role, pgSchema)
232-
d.SetId("")
233-
return nil
232+
if len(privilegesInput) != 0 {
233+
d.SetId("")
234+
return nil
235+
}
234236
}
235237

236238
privilegesSet := pgArrayToSet(privileges)
@@ -249,6 +251,11 @@ func grantRoleDefaultPrivileges(txn *sql.Tx, d *schema.ResourceData) error {
249251
privileges = append(privileges, priv.(string))
250252
}
251253

254+
if len(privileges) == 0 {
255+
log.Printf("[DEBUG] no default privileges to grant for role %s, owner %s in database: %s,", d.Get("role").(string), d.Get("owner").(string), d.Get("database").(string))
256+
return nil
257+
}
258+
252259
var inSchema string
253260

254261
// If a schema is specified we need to build the part of the query string to action this

postgresql/resource_postgresql_default_privileges_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ resource "postgresql_default_privileges" "test_ro" {
4848
},
4949
Providers: testAccProviders,
5050
Steps: []resource.TestStep{
51+
{
52+
Config: fmt.Sprintf(tfConfig, `[]`),
53+
Check: resource.ComposeTestCheckFunc(
54+
func(*terraform.State) error {
55+
tables := []string{"test_schema.test_table"}
56+
// To test default privileges, we need to create a table
57+
// after having apply the state.
58+
dropFunc := createTestTables(t, dbSuffix, tables, "")
59+
defer dropFunc()
60+
61+
return testCheckTablesPrivileges(t, dbName, roleName, tables, []string{})
62+
},
63+
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "object_type", "table"),
64+
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "with_grant_option", fmt.Sprintf("%t", withGrant)),
65+
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "privileges.#", "0"),
66+
),
67+
},
5168
{
5269
Config: fmt.Sprintf(tfConfig, `["SELECT"]`),
5370
Check: resource.ComposeTestCheckFunc(
@@ -83,6 +100,23 @@ resource "postgresql_default_privileges" "test_ro" {
83100
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "privileges.1759376126", "UPDATE"),
84101
),
85102
},
103+
{
104+
Config: fmt.Sprintf(tfConfig, `[]`),
105+
Check: resource.ComposeTestCheckFunc(
106+
func(*terraform.State) error {
107+
tables := []string{"test_schema.test_table"}
108+
// To test default privileges, we need to create a table
109+
// after having apply the state.
110+
dropFunc := createTestTables(t, dbSuffix, tables, "")
111+
defer dropFunc()
112+
113+
return testCheckTablesPrivileges(t, dbName, roleName, tables, []string{})
114+
},
115+
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "object_type", "table"),
116+
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "with_grant_option", fmt.Sprintf("%t", withGrant)),
117+
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "privileges.#", "0"),
118+
),
119+
},
86120
},
87121
})
88122
})

website/docs/r/postgresql_default_privileges.html.markdown

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ resource "postgresql_default_privileges" "read_only_tables" {
3131
* `role` - (Required) The name of the role to which grant default privileges on.
3232
* `database` - (Required) The database to grant default privileges for this role.
3333
* `owner` - (Required) Role for which apply default privileges (You can change default privileges only for objects that will be created by yourself or by roles that you are a member of).
34-
* `schema` - (Required) The database schema to set default privileges for this role.
34+
* `schema` - (Optional) The database schema to set default privileges for this role.
3535
* `object_type` - (Required) The PostgreSQL object type to set the default privileges on (one of: table, sequence, function, type).
36-
* `privileges` - (Required) The list of privileges to apply as default privileges.
36+
* `privileges` - (Required) The list of privileges to apply as default privileges. An empty list could be provided to revoke all default privileges for this role.
37+
38+
39+
## Examples
40+
41+
Revoke default privileges for functions for "public" role:
42+
43+
```hcl
44+
resource "postgresql_default_priviliges" "revoke_public" {
45+
database = postgresql_database.example_db.name
46+
role = "public"
47+
owner = "object_owner"
48+
object_type = "function"
49+
privileges = []
50+
}
51+
```

0 commit comments

Comments
 (0)