-
Notifications
You must be signed in to change notification settings - Fork 1
[INC-300] baton-github: optimize Grants() for org Resource #97
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 all commits
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 |
|---|---|---|
|
|
@@ -187,72 +187,78 @@ func (o *orgResourceType) Grants( | |
| return nil, "", nil, err | ||
| } | ||
|
|
||
| opts := github.ListMembersOptions{ | ||
| ListOptions: github.ListOptions{ | ||
| Page: page, | ||
| PerPage: maxPageSize, | ||
| }, | ||
| } | ||
| var ( | ||
| reqAnnos annotations.Annotations | ||
| pageToken string | ||
| rv = []*v2.Grant{} | ||
| ) | ||
|
|
||
| orgName, err := o.orgCache.GetOrgName(ctx, resource.Id) | ||
| if err != nil { | ||
| return nil, "", nil, err | ||
| } | ||
| switch rId := bag.ResourceTypeID(); rId { | ||
| case resourceTypeOrg.Id: | ||
| bag.Pop() | ||
| bag.Push(pagination.PageState{ | ||
| ResourceTypeID: orgRoleAdmin, | ||
| }) | ||
| bag.Push(pagination.PageState{ | ||
| ResourceTypeID: orgRoleMember, | ||
| }) | ||
| case orgRoleAdmin, orgRoleMember: | ||
|
|
||
| users, resp, err := o.client.Organizations.ListMembers(ctx, orgName, &opts) | ||
| if err != nil { | ||
| if isNotFoundError(resp) { | ||
| return nil, "", nil, uhttp.WrapErrors(codes.NotFound, fmt.Sprintf("org: %s not found", orgName)) | ||
| orgName, err := o.orgCache.GetOrgName(ctx, resource.Id) | ||
| if err != nil { | ||
| return nil, "", nil, err | ||
| } | ||
| errMsg := "github-connectorv2: failed to list org members" | ||
| if isRatelimited(resp) { | ||
| return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err) | ||
| opts := github.ListMembersOptions{ | ||
| Role: rId, | ||
| ListOptions: github.ListOptions{ | ||
| Page: page, | ||
| PerPage: maxPageSize, | ||
| }, | ||
| } | ||
| return nil, "", nil, fmt.Errorf("%s: %w", errMsg, err) | ||
| } | ||
|
|
||
| nextPage, reqAnnos, err := parseResp(resp) | ||
| if err != nil { | ||
| return nil, "", nil, fmt.Errorf("github-connectorv2: failed to parse response: %w", err) | ||
| } | ||
|
|
||
| pageToken, err := bag.NextToken(nextPage) | ||
| if err != nil { | ||
| return nil, "", nil, err | ||
| } | ||
|
|
||
| var rv []*v2.Grant | ||
| for _, user := range users { | ||
| membership, _, err := o.client.Organizations.GetOrgMembership(ctx, user.GetLogin(), orgName) | ||
| users, resp, err := o.client.Organizations.ListMembers(ctx, orgName, &opts) | ||
| if err != nil { | ||
| return nil, "", nil, fmt.Errorf("github-connectorv2: failed to get org memberships for user: %w", err) | ||
| if isNotFoundError(resp) { | ||
| return nil, "", nil, uhttp.WrapErrors(codes.NotFound, fmt.Sprintf("org: %s not found", orgName)) | ||
| } | ||
| errMsg := "github-connectorv2: failed to list org members" | ||
| if isRatelimited(resp) { | ||
| return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err) | ||
| } | ||
| return nil, "", nil, fmt.Errorf("%s: %w", errMsg, err) | ||
| } | ||
| if membership.GetState() == "pending" { | ||
| continue | ||
|
|
||
| var nextPage string | ||
| nextPage, reqAnnos, err = parseResp(resp) | ||
| if err != nil { | ||
| return nil, "", nil, fmt.Errorf("github-connectorv2: failed to parse response: %w", err) | ||
| } | ||
|
|
||
| ur, err := userResource(ctx, user, user.GetEmail(), nil) | ||
| err = bag.Next(nextPage) | ||
| if err != nil { | ||
| return nil, "", nil, err | ||
| } | ||
|
|
||
| roleName := strings.ToLower(membership.GetRole()) | ||
| switch roleName { | ||
| case orgRoleAdmin: | ||
| rv = append(rv, o.orgRoleGrant(orgRoleAdmin, resource, ur.Id, user.GetID())) | ||
| rv = append(rv, o.orgRoleGrant(orgRoleMember, resource, ur.Id, user.GetID())) | ||
|
|
||
| case orgRoleMember: | ||
| rv = append(rv, o.orgRoleGrant(orgRoleMember, resource, ur.Id, user.GetID())) | ||
| for _, user := range users { | ||
| ur, err := userResource(ctx, user, user.GetEmail(), nil) | ||
| if err != nil { | ||
| return nil, "", nil, err | ||
| } | ||
|
|
||
| default: | ||
| ctxzap.Extract(ctx).Warn("Unknown GitHub Role Name", | ||
| zap.String("role_name", roleName), | ||
| zap.String("github_username", user.GetLogin()), | ||
| ) | ||
| if rId == orgRoleAdmin { | ||
| rv = append(rv, o.orgRoleGrant(orgRoleMember, resource, ur.Id, user.GetID())) | ||
| } | ||
| rv = append(rv, o.orgRoleGrant(rId, resource, ur.Id, user.GetID())) | ||
|
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. Bug: Pending Memberships Included in GrantsThe optimization removed the check for pending organization memberships. Previously, users with pending invitations were explicitly skipped. The new code, using role-filtered Locations (1) |
||
| } | ||
| default: | ||
| ctxzap.Extract(ctx).Warn("Unknown GitHub Role Name", | ||
| zap.String("role_name", rId), | ||
| ) | ||
| } | ||
|
|
||
| pageToken, err = bag.Marshal() | ||
| if err != nil { | ||
| return nil, "", nil, err | ||
| } | ||
| return rv, pageToken, reqAnnos, nil | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to use bag. But looks like we need to push
resourceTypesto bag. bag.push(resourceTypes)How to handle pagination nicely? in this case.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The resource.Id should contain the resource type, right? You should be able to push page tokens for different resources and resource types, then pop stuff off and switch based on the resource type. See https://github.com/ConductorOne/baton-microsoft-entra/blob/main/pkg/connector/enterprise_applications.go#L267 for an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes need to use pagination bag
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a little confusing. I push
adminandmemberasresourceTypeId, but it works.Thanks!