-
Notifications
You must be signed in to change notification settings - Fork 268
fix: ALL implicit privileges equality check #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
09a9f30
edacad1
e3b92a0
add228c
f58fbb3
8dfcf90
db54bfc
54fc593
c64e956
a091370
30d4b45
d288aaf
55875e9
0393157
c35441d
4f65139
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package postgresql | ||
|
|
||
| import ( | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
@@ -45,3 +46,78 @@ func TestQuoteTableName(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestArePrivilegesEqual(t *testing.T) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool 👍 |
||
|
|
||
| type PrivilegesTestObject struct { | ||
| d *schema.ResourceData | ||
| granted *schema.Set | ||
| wanted *schema.Set | ||
| assertion bool | ||
| } | ||
|
|
||
| tt := []PrivilegesTestObject{ | ||
| { | ||
| buildResourceData("database", t), | ||
| buildPrivilegesSet("CONNECT", "CREATE", "TEMPORARY"), | ||
| buildPrivilegesSet("ALL"), | ||
| true, | ||
| }, | ||
| { | ||
| buildResourceData("database", t), | ||
| buildPrivilegesSet("CREATE", "USAGE"), | ||
| buildPrivilegesSet("USAGE"), | ||
| false, | ||
| }, | ||
| { | ||
| buildResourceData("table", t), | ||
| buildPrivilegesSet("SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER"), | ||
| buildPrivilegesSet("ALL"), | ||
| true, | ||
| }, | ||
| { | ||
| buildResourceData("table", t), | ||
| buildPrivilegesSet("SELECT"), | ||
| buildPrivilegesSet("SELECT, INSERT"), | ||
| false, | ||
| }, | ||
| { | ||
| buildResourceData("schema", t), | ||
| buildPrivilegesSet("CREATE", "USAGE"), | ||
| buildPrivilegesSet("ALL"), | ||
| true, | ||
| }, | ||
| { | ||
| buildResourceData("schema", t), | ||
| buildPrivilegesSet("CREATE"), | ||
| buildPrivilegesSet("ALL"), | ||
| false, | ||
| }, | ||
| } | ||
|
|
||
| for _, configuration := range tt { | ||
| err := configuration.d.Set("privileges", configuration.wanted) | ||
| assert.NoError(t, err) | ||
| equal := resourcePrivilegesEqual(configuration.granted, configuration.d) | ||
| assert.Equal(t, configuration.assertion, equal) | ||
| } | ||
| } | ||
|
|
||
| func buildPrivilegesSet(grants ...interface{}) *schema.Set { | ||
| return schema.NewSet(schema.HashString, grants) | ||
| } | ||
|
|
||
| func buildResourceData(objectType string, t *testing.T) *schema.ResourceData { | ||
| var testSchema = map[string]*schema.Schema{ | ||
| "object_type": {Type: schema.TypeString}, | ||
| "privileges": { | ||
| Type: schema.TypeSet, | ||
| Elem: &schema.Schema{Type: schema.TypeString}, | ||
| Set: schema.HashString, | ||
| }, | ||
| } | ||
|
|
||
| m := make(map[string]any) | ||
| m["object_type"] = objectType | ||
| return schema.TestResourceDataRaw(t, testSchema, m) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -268,8 +268,12 @@ func readRoleDefaultPrivileges(txn *sql.Tx, d *schema.ResourceData) error { | |
| } | ||
|
|
||
| privilegesSet := pgArrayToSet(privileges) | ||
| d.Set("privileges", privilegesSet) | ||
| d.SetId(generateDefaultPrivilegesID(d)) | ||
| privilegesEqual := resourcePrivilegesEqual(privilegesSet, d) | ||
|
|
||
| if !privilegesEqual { | ||
| d.Set("privileges", privilegesSet) | ||
| d.SetId(generateDefaultPrivilegesID(d)) | ||
|
||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1139,6 +1139,83 @@ resource "postgresql_grant" "test" { | |
| }) | ||
| } | ||
|
|
||
| func TestAccPostgresqlImplicitGrants(t *testing.T) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💪 |
||
| skipIfNotAcc(t) | ||
|
|
||
| dbSuffix, teardown := setupTestDatabase(t, true, true) | ||
| defer teardown() | ||
|
|
||
| testTables := []string{"test_schema.test_table"} | ||
| createTestTables(t, dbSuffix, testTables, "") | ||
|
|
||
| dbName, roleName := getTestDBNames(dbSuffix) | ||
|
|
||
| // create a TF config with placeholder for privileges | ||
| // it will be filled in each step. | ||
| var testGrant = fmt.Sprintf(` | ||
| resource "postgresql_grant" "test" { | ||
| database = "%s" | ||
| role = "%s" | ||
| schema = "test_schema" | ||
| object_type = "table" | ||
| objects = ["test_table"] | ||
| privileges = %%s | ||
| } | ||
| `, dbName, roleName) | ||
|
|
||
| var testCheckTableGrants = func(grants ...string) resource.TestCheckFunc { | ||
| return func(*terraform.State) error { | ||
| return testCheckTablesPrivileges(t, dbName, roleName, []string{testTables[0]}, grants) | ||
| } | ||
| } | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { | ||
| testAccPreCheck(t) | ||
| testCheckCompatibleVersion(t, featurePrivileges) | ||
| }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: fmt.Sprintf(testGrant, `["ALL"]`), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr( | ||
| "postgresql_grant.test", "id", fmt.Sprintf("%s_%s_test_schema_table_test_table", roleName, dbName), | ||
| ), | ||
| resource.TestCheckResourceAttr("postgresql_grant.test", "objects.#", "1"), | ||
| resource.TestCheckResourceAttr("postgresql_grant.test", "objects.0", "test_table"), | ||
| testCheckTableGrants("SELECT", "INSERT", "UPDATE", "DELETE"), | ||
| ), | ||
| }, | ||
| { | ||
| Config: fmt.Sprintf(testGrant, `["SELECT"]`), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("postgresql_grant.test", "objects.#", "1"), | ||
| resource.TestCheckResourceAttr("postgresql_grant.test", "objects.0", "test_table"), | ||
| testCheckTableGrants("SELECT"), | ||
| ), | ||
| }, | ||
| { | ||
| // Empty list means that privileges will be applied on all tables. | ||
| Config: fmt.Sprintf(testGrant, `["SELECT", "INSERT", "UPDATE", "DELETE"]`), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("postgresql_grant.test", "objects.#", "1"), | ||
| resource.TestCheckResourceAttr("postgresql_grant.test", "objects.0", "test_table"), | ||
| testCheckTableGrants("SELECT", "INSERT", "UPDATE", "DELETE"), | ||
| ), | ||
| }, | ||
| { | ||
| Config: fmt.Sprintf(testGrant, `[]`), | ||
| Destroy: true, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("postgresql_grant.test", "objects.#", "1"), | ||
| resource.TestCheckResourceAttr("postgresql_grant.test", "objects.0", "test_table"), | ||
| testCheckTableGrants(""), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestAccPostgresqlGrantSchema(t *testing.T) { | ||
| // create a TF config with placeholder for privileges | ||
| // it will be filled in each step. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.