Skip to content

Commit

Permalink
updating wif logic for determining role updates
Browse files Browse the repository at this point in the history
The prior check was lead to custom roles being updated during every wif
creation call if the permission set provided was not in the exact order
that is returned by GCP- emperically found to be alphabetical. With this
change, this assumption is no longer necassary.
  • Loading branch information
renan-campos committed Sep 16, 2024
1 parent 5217d70 commit 82efbbb
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions cmd/ocm/gcp/gcp-client-shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -252,7 +251,7 @@ func (c *shim) createOrUpdateRoles(
}

// Update role if permissions have changed
if !reflect.DeepEqual(existingRole.IncludedPermissions, permissions) {
if c.roleRequiresUpdate(permissions, existingRole.IncludedPermissions) {
existingRole.IncludedPermissions = permissions
_, err := c.updateRole(ctx, existingRole, c.fmtRoleResourceId(role))
if err != nil {
Expand All @@ -264,6 +263,25 @@ func (c *shim) createOrUpdateRoles(
return nil
}

func (c *shim) roleRequiresUpdate(
newPermissions []string,
existingPermissions []string,
) bool {
permissionMap := map[string]bool{}
for _, permission := range existingPermissions {
permissionMap[permission] = true
}
if len(permissionMap) != len(newPermissions) {
return true
}
for _, permission := range newPermissions {
if !permissionMap[permission] {
return true
}
}
return false
}

func (c *shim) bindRolesToServiceAccount(
ctx context.Context,
serviceAccount *cmv1.WifServiceAccount,
Expand Down

0 comments on commit 82efbbb

Please sign in to comment.