-
Notifications
You must be signed in to change notification settings - Fork 1
[BB-508] baton-github: use github app to sync #63
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 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,15 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/conductorone/baton-sdk/pkg/field" | ||
| ) | ||
|
|
||
| var ( | ||
| accessTokenField = field.StringField( | ||
| "token", | ||
| field.WithDescription("The GitHub access token used to connect to the GitHub API."), | ||
| field.WithRequired(true), | ||
| ) | ||
| orgsField = field.StringSliceField( | ||
| "orgs", | ||
|
|
@@ -18,11 +19,30 @@ var ( | |
| "instance-url", | ||
| field.WithDescription(`The GitHub instance URL to connect to. (default "https://github.com")`), | ||
| ) | ||
| appIDField = field.StringField( | ||
| "app-id", | ||
| field.WithDescription("The GitHub App to connect to."), | ||
| ) | ||
| appPrivateKey = field.StringField( | ||
| "app-privatekey", | ||
| field.WithDescription("The private key used to connect to the GitHub App"), | ||
| ) | ||
| ) | ||
|
|
||
| //go:generate go run ./gen | ||
| var Config = field.NewConfiguration([]field.SchemaField{ | ||
| accessTokenField, | ||
| orgsField, | ||
| instanceUrlField, | ||
| appIDField, | ||
| appPrivateKey, | ||
| }) | ||
|
|
||
| func ValidateConfig(cfg *Github) error { | ||
| apiKey := cfg.GetString(accessTokenField.FieldName) | ||
| appKey := cfg.GetString(appIDField.FieldName) | ||
| if len(apiKey) == 0 && len(appKey) == 0 { | ||
| return fmt.Errorf("api-key or app-privatekey is missing") | ||
| } | ||
| return nil | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,25 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rsa" | ||
| "crypto/x509" | ||
| "encoding/json" | ||
| "encoding/pem" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
|
|
||
| cfg "github.com/conductorone/baton-github/pkg/config" | ||
| v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" | ||
| "github.com/conductorone/baton-sdk/pkg/annotations" | ||
| "github.com/conductorone/baton-sdk/pkg/connectorbuilder" | ||
| "github.com/conductorone/baton-sdk/pkg/uhttp" | ||
| jwtv5 "github.com/golang-jwt/jwt/v5" | ||
| "github.com/google/go-github/v63/github" | ||
| "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" | ||
| "github.com/shurcooL/githubv4" | ||
|
|
@@ -58,14 +66,24 @@ | |
| graphqlClient *githubv4.Client | ||
| hasSAMLEnabled *bool | ||
| orgCache *orgNameCache | ||
| app gitHubApp | ||
| } | ||
|
|
||
| // gitHubApp has app clients that are used by the GitHub App. | ||
| // Each app has a single JWT token shared across all organizations. | ||
| // However, each organization has its own unique installation token. | ||
| type gitHubApp struct { | ||
| appJWTClient *github.Client | ||
| appInstallationClient map[int64]*github.Client | ||
| graphqlClient map[int64]*githubv4.Client | ||
| } | ||
|
|
||
| func (gh *GitHub) ResourceSyncers(ctx context.Context) []connectorbuilder.ResourceSyncer { | ||
| return []connectorbuilder.ResourceSyncer{ | ||
| orgBuilder(gh.client, gh.orgCache, gh.orgs), | ||
| teamBuilder(gh.client, gh.orgCache), | ||
| userBuilder(gh.client, gh.hasSAMLEnabled, gh.graphqlClient, gh.orgCache), | ||
| repositoryBuilder(gh.client, gh.orgCache), | ||
| orgBuilder(gh.client, gh.app, gh.orgCache, gh.orgs), | ||
| teamBuilder(gh.client, gh.app, gh.orgCache), | ||
| userBuilder(gh.client, gh.app, gh.hasSAMLEnabled, gh.graphqlClient, gh.orgCache), | ||
| repositoryBuilder(gh.client, gh.app, gh.orgCache), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -78,6 +96,10 @@ | |
|
|
||
| // Validate hits the GitHub API to validate that the configured credentials are still valid. | ||
| func (gh *GitHub) Validate(ctx context.Context) (annotations.Annotations, error) { | ||
| if gh.app.appJWTClient != nil { | ||
| return gh.validateAppCredentials(ctx) | ||
| } | ||
|
|
||
| page := 0 | ||
| orgLogins := gh.orgs | ||
| filterOrgs := true | ||
|
|
@@ -99,7 +121,6 @@ | |
| if resp.NextPage == 0 { | ||
| break | ||
| } | ||
|
|
||
| page = resp.NextPage | ||
| } | ||
| } | ||
|
|
@@ -132,6 +153,62 @@ | |
| return nil, nil | ||
| } | ||
|
|
||
| func (gh *GitHub) validateAppCredentials(ctx context.Context) (annotations.Annotations, error) { | ||
| iResp, err := getAllInstallations(ctx, gh.app.appJWTClient) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| for _, o := range gh.orgs { | ||
| if _, ok := iResp.names[o]; !ok { | ||
| return nil, fmt.Errorf("access token must be an admin on the %s organization", o) | ||
| } | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| type getAllInstallationsResp struct { | ||
| ids map[int64]struct{} | ||
| installationIds map[int64]*github.Installation | ||
| names map[string]struct{} | ||
| } | ||
|
|
||
| func getAllInstallations(ctx context.Context, c *github.Client) (getAllInstallationsResp, error) { | ||
| var ( | ||
| installationsMap = make(map[int64]struct{}) | ||
| installationsIDMap = make(map[int64]*github.Installation) | ||
| installationsNameMap = make(map[string]struct{}) | ||
| page = 0 | ||
| ) | ||
| for { | ||
| installations, resp, err := c.Apps.ListInstallations(ctx, &github.ListOptions{Page: page}) | ||
| if err != nil { | ||
| return getAllInstallationsResp{}, fmt.Errorf("github-connector: failed to retrieve org: %w", err) | ||
| } | ||
| if resp.StatusCode == http.StatusUnauthorized { | ||
| return getAllInstallationsResp{}, status.Error(codes.Unauthenticated, "github token is not authorized") | ||
| } | ||
| for _, installation := range installations { | ||
| if installation.GetAccount().GetType() != "Organization" { | ||
| continue | ||
| } | ||
| installationsMap[installation.GetAccount().GetID()] = struct{}{} | ||
| installationsIDMap[installation.GetID()] = installation | ||
| installationsNameMap[installation.GetAccount().GetLogin()] = struct{}{} | ||
| } | ||
|
|
||
| if resp.NextPage == 0 { | ||
| break | ||
| } | ||
| page = resp.NextPage | ||
| } | ||
| return getAllInstallationsResp{ | ||
| ids: installationsMap, | ||
| installationIds: installationsIDMap, | ||
| names: installationsNameMap, | ||
| }, nil | ||
| } | ||
|
|
||
| // newGitHubClient returns a new GitHub API client authenticated with an access token via oauth2. | ||
| func newGitHubClient(ctx context.Context, instanceURL string, accessToken string) (*github.Client, error) { | ||
| httpClient, err := uhttp.NewClient(ctx, uhttp.WithLogger(true, ctxzap.Extract(ctx))) | ||
|
|
@@ -157,6 +234,19 @@ | |
|
|
||
| // New returns the GitHub connector configured to sync against the instance URL. | ||
| func New(ctx context.Context, ghc *cfg.Github) (*GitHub, error) { | ||
| if ghc.Token == "" { | ||
| app, err := newAppClient(ctx, ghc) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &GitHub{ | ||
| instanceURL: ghc.InstanceUrl, | ||
| orgs: ghc.Orgs, | ||
| app: app, | ||
| orgCache: newOrgNameCache(nil, app.appInstallationClient), | ||
| }, nil | ||
| } | ||
|
|
||
| client, err := newGitHubClient(ctx, ghc.InstanceUrl, ghc.Token) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
@@ -170,12 +260,56 @@ | |
| instanceURL: ghc.InstanceUrl, | ||
| orgs: ghc.Orgs, | ||
| graphqlClient: graphqlClient, | ||
| orgCache: newOrgNameCache(client), | ||
| orgCache: newOrgNameCache(client, nil), | ||
| } | ||
|
|
||
| return gh, nil | ||
| } | ||
|
|
||
| func newAppClient(ctx context.Context, ghc *cfg.Github) (gitHubApp, error) { | ||
| key, err := loadPrivateKeyFromString(ghc.AppPrivatekey) | ||
| if err != nil { | ||
| return gitHubApp{}, err | ||
| } | ||
| now := time.Now() | ||
| token, err := jwtv5.NewWithClaims(jwtv5.SigningMethodRS256, jwtv5.MapClaims{ | ||
| "iat": now.Unix() - 60, // issued at | ||
| "exp": now.Add(time.Minute * 10).Unix(), // expires | ||
| "iss": ghc.AppId, // GitHub App ID | ||
| }).SignedString(key) | ||
| if err != nil { | ||
| return gitHubApp{}, err | ||
| } | ||
|
|
||
| client, err := newGitHubClient(ctx, ghc.InstanceUrl, token) | ||
| if err != nil { | ||
| return gitHubApp{}, err | ||
| } | ||
|
|
||
| iResp, err := getAllInstallations(ctx, client) | ||
| if err != nil { | ||
| return gitHubApp{}, err | ||
| } | ||
|
|
||
| var ( | ||
| installationsClient = make(map[int64]*github.Client, len(iResp.installationIds)) | ||
| installationsGLClient = make(map[int64]*githubv4.Client, len(iResp.ids)) | ||
| ) | ||
| for id, installation := range iResp.installationIds { | ||
| c, glc, err := getInstallationClient(ctx, ghc.InstanceUrl, id, token) | ||
| if err != nil { | ||
| return gitHubApp{}, err | ||
| } | ||
| installationsClient[installation.GetAccount().GetID()] = c | ||
| installationsGLClient[installation.GetAccount().GetID()] = glc | ||
| } | ||
| return gitHubApp{ | ||
| appJWTClient: client, | ||
| appInstallationClient: installationsClient, | ||
|
Contributor
Author
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. if many organizations are using the same app, they should have different tokens and clients. |
||
| graphqlClient: installationsGLClient, | ||
| }, nil | ||
| } | ||
|
|
||
| func newGitHubGraphqlClient(ctx context.Context, instanceURL string, accessToken string) (*githubv4.Client, error) { | ||
| httpClient, err := uhttp.NewClient(ctx, uhttp.WithLogger(true, ctxzap.Extract(ctx))) | ||
| if err != nil { | ||
|
|
@@ -203,3 +337,62 @@ | |
|
|
||
| return githubv4.NewClient(tc), nil | ||
| } | ||
|
|
||
| func loadPrivateKeyFromString(p string) (*rsa.PrivateKey, error) { | ||
| block, _ := pem.Decode([]byte(p)) | ||
| if block == nil || (block.Type != "PRIVATE KEY" && block.Type != "RSA PRIVATE KEY") { | ||
| return nil, errors.New("invalid private key PEM format") | ||
| } | ||
|
|
||
| // PKCS8 format | ||
| if block.Type == "PRIVATE KEY" { | ||
| key, err := x509.ParsePKCS8PrivateKey(block.Bytes) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| rsaKey, ok := key.(*rsa.PrivateKey) | ||
| if !ok { | ||
| return nil, errors.New("not an RSA private key") | ||
| } | ||
| return rsaKey, nil | ||
| } | ||
|
|
||
| // PKCS1 format | ||
| return x509.ParsePKCS1PrivateKey(block.Bytes) | ||
| } | ||
|
|
||
| func getInstallationClient(ctx context.Context, instanceURL string, orgId int64, jwtToken string) (*github.Client, *githubv4.Client, error) { | ||
| url := fmt.Sprintf("https://api.github.com/app/installations/%d/access_tokens", orgId) | ||
| req, _ := http.NewRequestWithContext(ctx, http.MethodPost, url, nil) | ||
| req.Header.Set("Authorization", "Bearer "+jwtToken) | ||
| req.Header.Set("Accept", "application/vnd.github+json") | ||
|
|
||
| resp, err := http.DefaultClient.Do(req) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusCreated { | ||
| body, _ := io.ReadAll(resp.Body) | ||
| return nil, nil, fmt.Errorf("GitHub API error: %s", body) | ||
| } | ||
|
|
||
| var result struct { | ||
| Token string `json:"token"` | ||
| } | ||
| err = json.NewDecoder(resp.Body).Decode(&result) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| client, err := newGitHubClient(ctx, instanceURL, result.Token) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| gcclient, err := newGitHubGraphqlClient(ctx, instanceURL, result.Token) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| return client, gcclient, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,8 +26,9 @@ func titleCase(s string) string { | |
|
|
||
| type orgNameCache struct { | ||
| sync.RWMutex | ||
| c *github.Client | ||
| orgNames map[string]string | ||
| c *github.Client | ||
| appClients map[int64]*github.Client | ||
| orgNames map[string]string | ||
| } | ||
|
|
||
| func (o *orgNameCache) GetOrgName(ctx context.Context, orgID *v2.ResourceId) (string, error) { | ||
|
|
@@ -50,7 +51,16 @@ func (o *orgNameCache) GetOrgName(ctx context.Context, orgID *v2.ResourceId) (st | |
| return "", err | ||
| } | ||
|
|
||
| org, _, err := o.c.Organizations.GetByID(ctx, oID) | ||
| client := o.c | ||
| if len(o.appClients) != 0 { | ||
| var ok bool | ||
| client, ok = o.appClients[oID] | ||
| if !ok { | ||
| return "", fmt.Errorf("organization: %d doesn't exist", oID) | ||
| } | ||
| } | ||
|
Comment on lines
+55
to
+61
Contributor
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. I'm not sure if its okay to cache like this, we generally don't rely on state like this, I think there can be times when we resume a sync |
||
|
|
||
| org, _, err := client.Organizations.GetByID(ctx, oID) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
@@ -60,10 +70,11 @@ func (o *orgNameCache) GetOrgName(ctx context.Context, orgID *v2.ResourceId) (st | |
| return org.GetLogin(), nil | ||
| } | ||
|
|
||
| func newOrgNameCache(c *github.Client) *orgNameCache { | ||
| func newOrgNameCache(c *github.Client, appClients map[int64]*github.Client) *orgNameCache { | ||
| return &orgNameCache{ | ||
| c: c, | ||
| orgNames: make(map[string]string), | ||
| c: c, | ||
| appClients: appClients, | ||
| orgNames: make(map[string]string), | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
we might want to have it use the file instead of taking it as a string