Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/conductorone/baton-sdk v0.3.8
github.com/deckarep/golang-set/v2 v2.7.0
github.com/ennyjfrick/ruleguard-logfatal v0.0.2
github.com/golang-jwt/jwt/v5 v5.2.2
github.com/google/go-github/v63 v63.0.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/migueleliasweb/go-github-mock v0.0.23
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ github.com/go-toolsmith/astequal v1.0.3 h1:+LVdyRatFS+XO78SGV4I3TCEA0AC7fKEGma+f
github.com/go-toolsmith/astequal v1.0.3/go.mod h1:9Ai4UglvtR+4up+bAD4+hCj7iTo4m/OXVTSLnCyTAx4=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/conf.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion pkg/config/config.go
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",
Expand All @@ -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"),
)
Comment on lines +24 to +27
Copy link
Contributor

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

)

//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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

205 changes: 199 additions & 6 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package connector

Check failure on line 1 in pkg/connector/connector.go

View workflow job for this annotation

GitHub Actions / go-lint

: # github.com/conductorone/baton-github/pkg/connector [github.com/conductorone/baton-github/pkg/connector.test]

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"
Expand Down Expand Up @@ -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),
}
}

Expand All @@ -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
Expand All @@ -99,7 +121,6 @@
if resp.NextPage == 0 {
break
}

page = resp.NextPage
}
}
Expand Down Expand Up @@ -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)))
Expand All @@ -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
Expand All @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -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
}
23 changes: 17 additions & 6 deletions pkg/connector/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
Expand All @@ -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),
}
}

Expand Down
Loading
Loading