Skip to content

Commit

Permalink
Fixed case sensitivity for principals in databricks_grants and `dat…
Browse files Browse the repository at this point in the history
…abricks_grant` (#3708)

* make principal case sensitive

* fix acceptance tests
  • Loading branch information
nkvuong authored Jun 26, 2024
1 parent 505a451 commit dfbcb5c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions catalog/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func diffPermissionsForPrincipal(principal string, desired catalog.PermissionsLi
// diffs change sets for principal
configured := map[string]*schema.Set{}
for _, v := range desired.PrivilegeAssignments {
if strings.EqualFold(v.Principal, principal) {
configured[strings.ToLower(v.Principal)] = permissions.SliceToSet(v.Privileges)
if v.Principal == principal {
configured[v.Principal] = permissions.SliceToSet(v.Privileges)
}
}
// existing permissions that needs removal for principal
remote := map[string]*schema.Set{}
for _, v := range existing.PrivilegeAssignments {
if strings.EqualFold(v.Principal, principal) {
remote[strings.ToLower(v.Principal)] = permissions.SliceToSet(v.Privileges)
if v.Principal == principal {
remote[v.Principal] = permissions.SliceToSet(v.Privileges)
}
}
// STEP 1: detect overlaps
Expand Down Expand Up @@ -87,7 +87,7 @@ func filterPermissionsForPrincipal(in catalog.PermissionsList, principal string)
grantsForPrincipal := []permissions.UnityCatalogPrivilegeAssignment{}
for _, v := range in.PrivilegeAssignments {
privileges := []string{}
if strings.EqualFold(v.Principal, principal) {
if v.Principal == principal {
for _, p := range v.Privileges {
privileges = append(privileges, p.String())
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func parseSecurableId(d *schema.ResourceData) (string, string, string, error) {
func ResourceGrant() common.Resource {
s := common.StructToSchema(permissions.UnityCatalogPrivilegeAssignment{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
common.CustomizeSchemaPath(m, "principal").SetForceNew().SetCustomSuppressDiff(common.EqualFoldDiffSuppress)
common.CustomizeSchemaPath(m, "principal").SetForceNew()

// set custom hash function for privileges
common.MustSchemaPath(m, "privileges").Set = func(i any) int {
Expand Down
6 changes: 3 additions & 3 deletions catalog/resource_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ func TestResourceGrantPermissionsList_Diff_CaseSensitive(t *testing.T) {
catalog.PermissionsList{ // config
PrivilegeAssignments: []catalog.PrivilegeAssignment{
{
Principal: "a",
Principal: "A",
Privileges: []catalog.Privilege{"a"},
},
{
Expand All @@ -550,7 +550,7 @@ func TestResourceGrantPermissionsList_Diff_CaseSensitive(t *testing.T) {
catalog.PermissionsList{
PrivilegeAssignments: []catalog.PrivilegeAssignment{ // platform
{
Principal: "A",
Principal: "a",
Privileges: []catalog.Privilege{"a"},
},
{
Expand All @@ -560,7 +560,7 @@ func TestResourceGrantPermissionsList_Diff_CaseSensitive(t *testing.T) {
},
},
)
assert.Len(t, diff, 0)
assert.Len(t, diff, 1)
}

func TestResourceGrantPermissionsList_Diff_ExternallyAddedPriv(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions catalog/resource_grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func diffPermissions(pl catalog.PermissionsList, existing catalog.PermissionsLis
// diffs change sets
configured := map[string]*schema.Set{}
for _, v := range pl.PrivilegeAssignments {
configured[strings.ToLower(v.Principal)] = permissions.SliceToSet(v.Privileges)
configured[v.Principal] = permissions.SliceToSet(v.Privileges)
}
// existing permissions that needs removal
remote := map[string]*schema.Set{}
for _, v := range existing.PrivilegeAssignments {
remote[strings.ToLower(v.Principal)] = permissions.SliceToSet(v.Privileges)
remote[v.Principal] = permissions.SliceToSet(v.Privileges)
}
// STEP 1: detect overlaps
for principal, confPrivs := range configured {
Expand Down
2 changes: 1 addition & 1 deletion catalog/resource_grants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func TestPermissionsList_Diff_CaseSensitivePrincipal(t *testing.T) {
},
},
)
assert.Len(t, diff, 0)
assert.Len(t, diff, 2)
}

func TestPermissionsList_Diff_LocalRemoteDiff(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/acceptance/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestUcAccGrant(t *testing.T) {
}, step{
Template: strings.ReplaceAll(grantTemplate, "%s", "{env.TEST_DATA_SCI_GROUP}"),
}, step{
Template: strings.ReplaceAll(strings.ReplaceAll(grantTemplate, "ALL_PRIVILEGES", "ALL PRIVILEGES"), `"%s"`, `upper("{env.TEST_DATA_SCI_GROUP}")`),
Template: strings.ReplaceAll(strings.ReplaceAll(grantTemplate, "ALL_PRIVILEGES", "ALL PRIVILEGES"), "%s", "{env.TEST_DATA_ENG_GROUP}"),
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/acceptance/grants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestUcAccGrants(t *testing.T) {
}, step{
Template: strings.ReplaceAll(grantsTemplate, "%s", "{env.TEST_DATA_SCI_GROUP}"),
}, step{
Template: strings.ReplaceAll(strings.ReplaceAll(grantsTemplate, "ALL_PRIVILEGES", "ALL PRIVILEGES"), `"%s"`, `upper("{env.TEST_DATA_SCI_GROUP}")`),
Template: strings.ReplaceAll(strings.ReplaceAll(grantsTemplate, "ALL_PRIVILEGES", "ALL PRIVILEGES"), "%s", "{env.TEST_DATA_ENG_GROUP}"),
})
}

Expand Down

0 comments on commit dfbcb5c

Please sign in to comment.