Skip to content

Commit

Permalink
Add support for workspace ID in prepareResourceId
Browse files Browse the repository at this point in the history
  • Loading branch information
ggreer committed Dec 20, 2024
1 parent b27117b commit 393a768
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/connector/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (a *accountBuilder) Grants(ctx context.Context, resource *v2.Resource, pTok
// rule set contains role and its principals, each one with resource type and resource id seperated by "/"
if strings.Contains(ruleSet.Role, MarketplaceAdminRole) {
for _, p := range ruleSet.Principals {
resourceId, err := prepareResourceID(ctx, a.client, p)
resourceId, err := prepareResourceId(ctx, a.client, "", p)
if err != nil {
return nil, "", nil, fmt.Errorf("databricks-connector: failed to prepare resource id for principal %s: %w", p, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/connector/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (g *groupBuilder) Grants(ctx context.Context, resource *v2.Resource, pToken

for _, ruleSet := range ruleSets {
for _, p := range ruleSet.Principals {
resourceId, err := prepareResourceID(ctx, g.client, p)
resourceId, err := prepareResourceId(ctx, g.client, workspaceId, p)
if err != nil {
return nil, "", nil, fmt.Errorf("databricks-connector: failed to prepare resource id for principal %s: %w", p, err)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/connector/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func annotationsForUserResourceType() annotations.Annotations {
return annos
}

// prepareResourceID prepares a resource ID for a user, group, or service principal.
// prepareResourceId prepares a resource ID for a user, group, or service principal.
// It's used when we need to parse results from listing rule sets.
func prepareResourceID(ctx context.Context, c *databricks.Client, principal string) (*v2.ResourceId, error) {
func prepareResourceId(ctx context.Context, c *databricks.Client, workspaceId string, principal string) (*v2.ResourceId, error) {
pp := strings.Split(principal, "/")
if len(pp) != 2 {
return nil, fmt.Errorf("invalid principal format: %s", principal)
Expand All @@ -77,21 +77,21 @@ func prepareResourceID(ctx context.Context, c *databricks.Client, principal stri

switch principalType {
case UsersType:
userID, _, err := c.FindUserID(ctx, principal)
userID, _, err := c.FindUserID(ctx, workspaceId, principal)
if err != nil {
return nil, fmt.Errorf("failed to find user %s: %w", principal, err)
}

resourceId = &v2.ResourceId{ResourceType: userResourceType.Id, Resource: userID}
case GroupsType:
groupID, _, err := c.FindGroupID(ctx, principal)
groupID, _, err := c.FindGroupID(ctx, workspaceId, principal)
if err != nil {
return nil, fmt.Errorf("failed to find group %s: %w", principal, err)
}

resourceId = &v2.ResourceId{ResourceType: groupResourceType.Id, Resource: groupID}
case ServicePrincipalsType:
servicePrincipalID, _, err := c.FindServicePrincipalID(ctx, principal)
servicePrincipalID, _, err := c.FindServicePrincipalID(ctx, workspaceId, principal)
if err != nil {
return nil, fmt.Errorf("failed to find service principal %s: %w", principal, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/connector/service-principals.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (s *servicePrincipalBuilder) Grants(ctx context.Context, resource *v2.Resou
var rv []*v2.Grant
for _, ruleSet := range ruleSets {
for _, p := range ruleSet.Principals {
resourceId, err := prepareResourceID(ctx, s.client, p)
resourceId, err := prepareResourceId(ctx, s.client, workspaceId, p)
if err != nil {
return nil, "", nil, fmt.Errorf("databricks-connector: failed to prepare resource id for principal %s: %w", p, err)
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/databricks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func (c *Client) UpdateUser(ctx context.Context, workspaceId string, user *User)

func (c *Client) FindUserID(
ctx context.Context,
workspaceId string,
username string,
) (
string,
Expand All @@ -179,7 +180,7 @@ func (c *Client) FindUserID(
) {
users, _, ratelimitData, err := c.ListUsers(
ctx,
"",
workspaceId,
&PaginationVars{Count: 1},
NewFilterVars(fmt.Sprintf("userName eq '%s'", username)),
)
Expand Down Expand Up @@ -282,6 +283,7 @@ func (c *Client) UpdateGroup(ctx context.Context, workspaceId string, group *Gro

func (c *Client) FindGroupID(
ctx context.Context,
workspaceId string,
displayName string,
) (
string,
Expand All @@ -290,7 +292,7 @@ func (c *Client) FindGroupID(
) {
groups, _, ratelimitData, err := c.ListGroups(
ctx,
"",
workspaceId,
&PaginationVars{Count: 1},
NewFilterVars(fmt.Sprintf("displayName eq '%s'", displayName)),
)
Expand Down Expand Up @@ -399,6 +401,7 @@ func (c *Client) UpdateServicePrincipal(

func (c *Client) FindServicePrincipalID(
ctx context.Context,
workspaceId string,
appID string,
) (
string,
Expand All @@ -407,7 +410,7 @@ func (c *Client) FindServicePrincipalID(
) {
servicePrincipals, _, ratelimitData, err := c.ListServicePrincipals(
ctx,
"",
workspaceId,
&PaginationVars{Count: 1},
NewFilterVars(fmt.Sprintf("applicationId eq '%s'", appID)),
)
Expand Down

0 comments on commit 393a768

Please sign in to comment.