-
Notifications
You must be signed in to change notification settings - Fork 3
nhi: add GitHub Apps syncer (APP_REGISTRATION) #171
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
55d8b0f
nhi: add GitHub Apps syncer (APP_REGISTRATION)
pquerna e1332c3
ci: install baton CLI from baton-sdk@v0.11.0 — conductorone/baton is …
pquerna c66bcb6
nhi: clean up GitHub Apps syncer
luisina-santos 79ee5fd
ci: install baton CLI from baton-sdk@v0.11.0
luisina-santos ea15782
fix ci
luisina-santos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" | ||
| resourceSdk "github.com/conductorone/baton-sdk/pkg/types/resource" | ||
| "github.com/google/go-github/v69/github" | ||
| ) | ||
|
|
||
| // appResource builds a GitHub App resource from one organization installation. | ||
| // The org installations endpoint is the only org-wide-enumerable window onto | ||
| // GitHub Apps: each installation row carries the app's identity (app_id, | ||
| // app_slug) plus the installed scope. The resource is keyed by installation ID | ||
| // (unique and stable per org) and carries TRAIT_APP + an APP_REGISTRATION NHI | ||
| // annotation so c1 classifies it as a non-human identity. | ||
| func appResource(ctx context.Context, installation *github.Installation, parentResourceID *v2.ResourceId) (*v2.Resource, error) { | ||
| appSlug := installation.GetAppSlug() | ||
| displayName := appSlug | ||
| if displayName == "" { | ||
| displayName = fmt.Sprintf("app-%d", installation.GetAppID()) | ||
| } | ||
|
|
||
| profile := map[string]interface{}{ | ||
| "app_id": installation.GetAppID(), | ||
| "app_slug": appSlug, | ||
| "installation_id": installation.GetID(), | ||
| } | ||
| if account := installation.GetAccount(); account != nil { | ||
| profile["account_login"] = account.GetLogin() | ||
| } | ||
| if installation.TargetType != nil { | ||
| profile["target_type"] = installation.GetTargetType() | ||
| } | ||
| if installation.RepositorySelection != nil { | ||
| profile["repository_selection"] = installation.GetRepositorySelection() | ||
| } | ||
|
|
||
| opts := []resourceSdk.ResourceOption{ | ||
| resourceSdk.WithParentResourceID(parentResourceID), | ||
| resourceSdk.WithAppTrait(resourceSdk.WithAppProfile(profile)), | ||
| resourceSdk.WithNHIType(v2.NonHumanIdentityTrait_NHI_TYPE_APP_REGISTRATION, "github.app"), | ||
| } | ||
| if installation.HTMLURL != nil { | ||
| opts = append(opts, resourceSdk.WithAnnotation(&v2.ExternalLink{Url: installation.GetHTMLURL()})) | ||
| } | ||
|
|
||
| return resourceSdk.NewResource( | ||
| displayName, | ||
| resourceTypeApp, | ||
| installation.GetID(), | ||
| opts..., | ||
| ) | ||
| } | ||
|
|
||
| type appResourceType struct { | ||
| resourceType *v2.ResourceType | ||
| client *github.Client | ||
| orgCache *orgNameCache | ||
| } | ||
|
|
||
| func (o *appResourceType) ResourceType(_ context.Context) *v2.ResourceType { | ||
| return o.resourceType | ||
| } | ||
|
|
||
| func (o *appResourceType) Entitlements(ctx context.Context, resource *v2.Resource, opts resourceSdk.SyncOpAttrs) ([]*v2.Entitlement, *resourceSdk.SyncOpResults, error) { | ||
| // GitHub Apps are synced read-only as NHI app registrations; no entitlements. | ||
| return nil, &resourceSdk.SyncOpResults{}, nil | ||
| } | ||
|
|
||
| func (o *appResourceType) Grants(ctx context.Context, resource *v2.Resource, opts resourceSdk.SyncOpAttrs) ([]*v2.Grant, *resourceSdk.SyncOpResults, error) { | ||
| // GitHub Apps are synced read-only as NHI app registrations; no grants. | ||
| return nil, &resourceSdk.SyncOpResults{}, nil | ||
| } | ||
|
|
||
| func (o *appResourceType) List( | ||
| ctx context.Context, | ||
| parentID *v2.ResourceId, | ||
| opts resourceSdk.SyncOpAttrs, | ||
| ) ([]*v2.Resource, *resourceSdk.SyncOpResults, error) { | ||
| if parentID == nil { | ||
| return nil, &resourceSdk.SyncOpResults{}, nil | ||
| } | ||
|
|
||
| bag, page, err := parsePageToken(opts.PageToken.Token, &v2.ResourceId{ResourceType: resourceTypeApp.Id}) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| orgName, err := o.orgCache.GetOrgName(ctx, opts.Session, parentID) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| installations, resp, err := o.client.Organizations.ListInstallations(ctx, orgName, &github.ListOptions{ | ||
| Page: page, | ||
| PerPage: opts.PageToken.Size, | ||
| }) | ||
| if err != nil { | ||
| return nil, nil, wrapGitHubError(err, resp, "github-connector: failed to list organization app installations") | ||
| } | ||
|
|
||
| pageToken, annos, err := nextPageToken(bag, resp) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| var rv []*v2.Resource | ||
| for _, installation := range installations.Installations { | ||
| resource, err := appResource(ctx, installation, parentID) | ||
| if err != nil { | ||
| return nil, &resourceSdk.SyncOpResults{ | ||
| NextPageToken: pageToken, | ||
| Annotations: annos, | ||
| }, err | ||
| } | ||
| rv = append(rv, resource) | ||
| } | ||
|
|
||
| return rv, &resourceSdk.SyncOpResults{ | ||
| NextPageToken: pageToken, | ||
| Annotations: annos, | ||
| }, nil | ||
| } | ||
|
|
||
| func AppBuilder(client *github.Client, orgCache *orgNameCache) *appResourceType { | ||
| return &appResourceType{ | ||
| resourceType: resourceTypeApp, | ||
| client: client, | ||
| orgCache: orgCache, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" | ||
| resourceSdk "github.com/conductorone/baton-sdk/pkg/types/resource" | ||
| "github.com/google/go-github/v69/github" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestAppResource(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| installation := &github.Installation{ | ||
| ID: github.Ptr(int64(42)), | ||
| AppID: github.Ptr(int64(7)), | ||
| AppSlug: github.Ptr("octo-app"), | ||
| TargetType: github.Ptr("Organization"), | ||
| RepositorySelection: github.Ptr("all"), | ||
| HTMLURL: github.Ptr("https://github.com/organizations/acme/settings/installations/42"), | ||
| Account: &github.User{Login: github.Ptr("acme")}, | ||
| } | ||
| parent := &v2.ResourceId{ResourceType: resourceTypeOrg.Id, Resource: "1"} | ||
|
|
||
| resource, err := appResource(ctx, installation, parent) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "octo-app", resource.DisplayName) | ||
| require.Equal(t, "42", resource.Id.Resource) | ||
| require.Equal(t, resourceTypeApp.Id, resource.Id.ResourceType) | ||
| require.Equal(t, parent.Resource, resource.ParentResourceId.Resource) | ||
|
|
||
| nhi, err := resourceSdk.GetNonHumanIdentityTrait(resource) | ||
| require.NoError(t, err) | ||
| require.Equal(t, v2.NonHumanIdentityTrait_NHI_TYPE_APP_REGISTRATION, nhi.GetNhiType()) | ||
| require.Equal(t, "github.app", nhi.GetNhiDetail()) | ||
|
|
||
| app, err := resourceSdk.GetAppTrait(resource) | ||
| require.NoError(t, err) | ||
| appID, ok := resourceSdk.GetProfileInt64Value(app.GetProfile(), "app_id") | ||
| require.True(t, ok) | ||
| require.Equal(t, int64(7), appID) | ||
| login, ok := resourceSdk.GetProfileStringValue(app.GetProfile(), "account_login") | ||
| require.True(t, ok) | ||
| require.Equal(t, "acme", login) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🟡 Suggestion: The previous version of this code gracefully degraded on 403/404 by logging a warning and skipping the org, which was important for GitHub App auth where the installation token may lack
organization_administration:read. TheCapabilityPermissionsannotation on the resource type is used for metadata reporting only (SDKconnectorbuilderreads it inGetCapabilities), not for runtime error handling — so a missing permission will now propagate as aPermissionDeniedgRPC error and fail the entire sync. If the intent is to make this permission strictly required, this is fine, but it's a behavior change for App-auth users whose installation token doesn't include Administration read access.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.
make permission required. if the customer doesn't want to sync apps should disable the resource