Skip to content

Commit 87addd5

Browse files
authored
Update connector to use new resource helpers. (#5)
* Update connector to use new resource helpers. Refactor teams to not use normal parent recursion * Better strategy for setting parent team * Restore removed methods * Update flag usage for new BATON prefixed env vars * Bump sdk to v0.0.12 * Lint fix
1 parent c2462d8 commit 87addd5

File tree

19 files changed

+476
-343
lines changed

19 files changed

+476
-343
lines changed

cmd/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func validateConfig(ctx context.Context, cfg *config) error {
2828

2929
// cmdFlags sets the cmdFlags required for the connector.
3030
func cmdFlags(cmd *cobra.Command) {
31-
cmd.PersistentFlags().String("token", "", "The GitHub access token used to connect to the Github API. ($C1_TOKEN)")
32-
cmd.PersistentFlags().StringSlice("orgs", []string{}, "Limit syncing to specific organizations. ($C1_ORGS)")
33-
cmd.PersistentFlags().String("instance-url", "", `The GitHub instance URL to connect to. ($C1_INSTANCE_URL) (default "https://github.com")`)
31+
cmd.PersistentFlags().String("token", "", "The GitHub access token used to connect to the Github API. ($BATON_TOKEN)")
32+
cmd.PersistentFlags().StringSlice("orgs", []string{}, "Limit syncing to specific organizations. ($BATON_ORGS)")
33+
cmd.PersistentFlags().String("instance-url", "", `The GitHub instance URL to connect to. ($BATON_INSTANCE_URL) (default "https://github.com")`)
3434
}

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func run(ctx context.Context, cfg *config) error {
6262
return err
6363
}
6464

65-
r, err := sdk.NewConnectorRunner(ctx, c, cfg.C1zPath, sdk.WithSlidingMemoryLimiter(50))
65+
r, err := sdk.NewConnectorRunner(ctx, c, cfg.C1zPath)
6666
if err != nil {
6767
l.Error("error creating connector runner", zap.Error(err))
6868
return err

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/conductorone/baton-github
33
go 1.19
44

55
require (
6-
github.com/conductorone/baton-sdk v0.0.10
6+
github.com/conductorone/baton-sdk v0.0.12
77
github.com/google/go-github/v41 v41.0.0
88
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
99
github.com/spf13/cobra v1.6.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
8989
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
9090
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
9191
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
92-
github.com/conductorone/baton-sdk v0.0.10 h1:C+6Z/X4GeycHdj5gMYWbSKSLKEI0eOfYXP+paR2gTMM=
93-
github.com/conductorone/baton-sdk v0.0.10/go.mod h1:jPdcy08LmTIPzgZcSOo7mviSAG0NUbjavg/1LpCTeOI=
92+
github.com/conductorone/baton-sdk v0.0.12 h1:hvFzVHr5aeSV4tiNNvn+lqpk16I6HGu77p61nUSJt9Y=
93+
github.com/conductorone/baton-sdk v0.0.12/go.mod h1:jPdcy08LmTIPzgZcSOo7mviSAG0NUbjavg/1LpCTeOI=
9494
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
9595
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9696
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

pkg/connector/connector.go

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package connector
33
import (
44
"context"
55
"fmt"
6-
"io"
76
"net/http"
8-
"net/url"
97
"strings"
108

119
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
@@ -63,65 +61,6 @@ func (gh *Github) ResourceSyncers(ctx context.Context) []connectorbuilder.Resour
6361
}
6462
}
6563

66-
// validateAssetUrl takes an input URL and validates that it is a URL that we are permitted to fetch assets from/
67-
// It enforces https and that the URL hostname is.
68-
func (gh *Github) validateAssetUrl(assetUrl string) error {
69-
if assetUrl == "" {
70-
return fmt.Errorf("asset url must be set")
71-
}
72-
73-
parsedUrl, err := url.Parse(assetUrl)
74-
if err != nil {
75-
return err
76-
}
77-
78-
if parsedUrl.Scheme != "https" {
79-
return fmt.Errorf("asset url must be https")
80-
}
81-
82-
if gh.instanceURL == "" {
83-
for _, domain := range ValidAssetDomains {
84-
if strings.HasPrefix(parsedUrl.Hostname(), domain) {
85-
return nil
86-
}
87-
}
88-
} else {
89-
parsedInstance, err := url.Parse(gh.instanceURL)
90-
if err != nil {
91-
return err
92-
}
93-
94-
if strings.HasSuffix(parsedUrl.Hostname(), parsedInstance.Hostname()) {
95-
return nil
96-
}
97-
}
98-
99-
return fmt.Errorf("invalid asset url")
100-
}
101-
102-
// GetAsset takes an input AssetRef and attempts to fetch it using the connector's authenticated http client
103-
// It streams a response, always starting with a metadata object, following by chunked payloads for the asset.
104-
func (gh *Github) Asset(ctx context.Context, asset *v2.AssetRef) (string, io.ReadCloser, error) {
105-
if asset == nil {
106-
return "", nil, fmt.Errorf("asset must be provided")
107-
}
108-
err := gh.validateAssetUrl(asset.Id)
109-
if err != nil {
110-
return "", nil, err
111-
}
112-
113-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, asset.GetId(), nil)
114-
if err != nil {
115-
return "", nil, err
116-
}
117-
resp, err := gh.client.Client().Do(req)
118-
if err != nil {
119-
return "", nil, err
120-
}
121-
122-
return resp.Header.Get("Content-Type"), resp.Body, nil
123-
}
124-
12564
// Metadata returns metadata about the connector.
12665
func (gh *Github) Metadata(ctx context.Context) (*v2.ConnectorMetadata, error) {
12766
return &v2.ConnectorMetadata{
@@ -207,7 +146,7 @@ func newGithubClient(ctx context.Context, instanceURL string, accessToken string
207146
return github.NewClient(tc), nil
208147
}
209148

210-
// New returns the v2 version of the github connector.
149+
// New returns the GitHub connector configured to sync against the instance URL.
211150
func New(ctx context.Context, githubOrgs []string, instanceURL, accessToken string) (*Github, error) {
212151
client, err := newGithubClient(ctx, instanceURL, accessToken)
213152
if err != nil {

pkg/connector/helpers.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package connector
22

33
import (
4+
"context"
45
"fmt"
56
"strconv"
67
"strings"
@@ -16,9 +17,18 @@ import (
1617

1718
var titleCaser = cases.Title(language.English)
1819

19-
func getOrgName(rID *v2.ResourceId) string {
20-
ret, _, _ := strings.Cut(rID.Resource, ":")
21-
return ret
20+
func getOrgName(ctx context.Context, c *github.Client, orgID *v2.ResourceId) (string, error) {
21+
oID, err := strconv.ParseInt(orgID.Resource, 10, 64)
22+
if err != nil {
23+
return "", err
24+
}
25+
26+
org, _, err := c.Organizations.GetByID(ctx, oID)
27+
if err != nil {
28+
return "", err
29+
}
30+
31+
return org.GetLogin(), nil
2232
}
2333

2434
func v1AnnotationsForResourceType(resourceTypeID string) annotations.Annotations {

pkg/connector/org.go

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ func (o *orgResourceType) List(
7979
continue
8080
}
8181

82+
orgResource, err := sdk.NewResource(org.GetLogin(), resourceTypeOrg, parentResourceID, org.GetID())
83+
if err != nil {
84+
return nil, "", nil, err
85+
}
86+
8287
var annos annotations.Annotations
8388
annos.Append(&v2.ExternalLink{
8489
Url: org.GetHTMLURL(),
@@ -90,16 +95,9 @@ func (o *orgResourceType) List(
9095
annos.Append(&v2.ChildResourceType{ResourceTypeId: resourceTypeTeam.Id})
9196
annos.Append(&v2.ChildResourceType{ResourceTypeId: resourceTypeRepository.Id})
9297

93-
resourceID, err := sdk.NewResourceID(resourceTypeOrg, parentResourceID, org.GetLogin())
94-
if err != nil {
95-
return nil, "", nil, err
96-
}
98+
orgResource.Annotations = annos
9799

98-
ret = append(ret, &v2.Resource{
99-
Id: resourceID,
100-
DisplayName: org.GetLogin(),
101-
Annotations: annos,
102-
})
100+
ret = append(ret, orgResource)
103101
}
104102

105103
return ret, pageToken, reqAnnos, nil
@@ -116,16 +114,13 @@ func (o *orgResourceType) Entitlements(
116114
annos.Append(&v2.V1Identifier{
117115
Id: fmt.Sprintf("org:%s:role:%s", resource.Id, level),
118116
})
119-
rv = append(rv, &v2.Entitlement{
120-
Id: sdk.NewEntitlementID(resource, level),
121-
Resource: resource,
122-
DisplayName: fmt.Sprintf("%s Org %s", resource.DisplayName, titleCaser.String(level)),
123-
Description: fmt.Sprintf("Access to %s org in Github", resource.DisplayName),
124-
Annotations: annos,
125-
GrantableTo: []*v2.ResourceType{resourceTypeUser},
126-
Purpose: v2.Entitlement_PURPOSE_VALUE_PERMISSION,
127-
Slug: level,
128-
})
117+
118+
en := sdk.NewPermissionEntitlement(resource, level, resourceTypeUser)
119+
en.DisplayName = fmt.Sprintf("%s Org %s", resource.DisplayName, titleCaser.String(level))
120+
en.Description = fmt.Sprintf("Access to %s org in Github", resource.DisplayName)
121+
en.Annotations = annos
122+
123+
rv = append(rv, en)
129124
}
130125

131126
return rv, "", nil, nil
@@ -148,7 +143,10 @@ func (o *orgResourceType) Grants(
148143
},
149144
}
150145

151-
orgName := getOrgName(resource.Id)
146+
orgName, err := getOrgName(ctx, o.client, resource.Id)
147+
if err != nil {
148+
return nil, "", nil, err
149+
}
152150

153151
users, resp, err := o.client.Organizations.ListMembers(ctx, orgName, &opts)
154152
if err != nil {
@@ -188,17 +186,9 @@ func (o *orgResourceType) Grants(
188186
Id: fmt.Sprintf("org-grant:%s:%d:%s", resource.Id.Resource, user.GetID(), roleName),
189187
})
190188

191-
en := &v2.Entitlement{
192-
Id: sdk.NewEntitlementID(resource, roleName),
193-
Resource: resource,
194-
}
195-
196-
rv = append(rv, &v2.Grant{
197-
Id: sdk.NewGrantID(en, ur),
198-
Entitlement: en,
199-
Annotations: annos,
200-
Principal: ur,
201-
})
189+
grant := sdk.NewGrant(resource, roleName, ur.Id)
190+
grant.Annotations = annos
191+
rv = append(rv, grant)
202192
default:
203193
ctxzap.Extract(ctx).Warn("Unknown Github Role Name",
204194
zap.String("role_name", roleName),

pkg/connector/repository.go

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var repoAccessLevels = []string{
2929
}
3030

3131
// repositoryResource returns a new connector resource for a Github repository.
32-
func repositoryResource(ctx context.Context, orgName string, repo *github.Repository, parentResourceID *v2.ResourceId) (*v2.Resource, error) {
32+
func repositoryResource(ctx context.Context, repo *github.Repository, parentResourceID *v2.ResourceId) (*v2.Resource, error) {
3333
var annos annotations.Annotations
3434
annos.Append(&v2.ExternalLink{
3535
Url: repo.GetHTMLURL(),
@@ -38,17 +38,14 @@ func repositoryResource(ctx context.Context, orgName string, repo *github.Reposi
3838
Id: fmt.Sprintf("repo:%d", repo.GetID()),
3939
})
4040

41-
resourceID, err := sdk.NewResourceID(resourceTypeRepository, parentResourceID, repo.GetID())
41+
ret, err := sdk.NewResource(repo.GetName(), resourceTypeRepository, parentResourceID, repo.GetID())
4242
if err != nil {
4343
return nil, err
4444
}
4545

46-
return &v2.Resource{
47-
Id: resourceID,
48-
DisplayName: repo.GetName(),
49-
ParentResourceId: parentResourceID,
50-
Annotations: annos,
51-
}, nil
46+
ret.Annotations = annos
47+
48+
return ret, nil
5249
}
5350

5451
type repositoryResourceType struct {
@@ -70,7 +67,10 @@ func (o *repositoryResourceType) List(ctx context.Context, parentID *v2.Resource
7067
return nil, "", nil, err
7168
}
7269

73-
orgName := getOrgName(parentID)
70+
orgName, err := getOrgName(ctx, o.client, parentID)
71+
if err != nil {
72+
return nil, "", nil, err
73+
}
7474

7575
opts := &github.RepositoryListByOrgOptions{
7676
ListOptions: github.ListOptions{
@@ -96,7 +96,7 @@ func (o *repositoryResourceType) List(ctx context.Context, parentID *v2.Resource
9696

9797
rv := make([]*v2.Resource, 0, len(repos))
9898
for _, repo := range repos {
99-
rr, err := repositoryResource(ctx, orgName, repo, parentID)
99+
rr, err := repositoryResource(ctx, repo, parentID)
100100
if err != nil {
101101
return nil, "", nil, err
102102
}
@@ -113,16 +113,13 @@ func (o *repositoryResourceType) Entitlements(_ context.Context, resource *v2.Re
113113
annos.Append(&v2.V1Identifier{
114114
Id: fmt.Sprintf("repo:%s:role:%s", resource.Id, level),
115115
})
116-
rv = append(rv, &v2.Entitlement{
117-
Id: sdk.NewEntitlementID(resource, level),
118-
Resource: resource,
119-
DisplayName: fmt.Sprintf("%s Repo %s", resource.DisplayName, titleCaser.String(level)),
120-
Description: fmt.Sprintf("Access to %s repository in Github", resource.DisplayName),
121-
Annotations: annos,
122-
GrantableTo: []*v2.ResourceType{resourceTypeUser, resourceTypeTeam},
123-
Purpose: v2.Entitlement_PURPOSE_VALUE_PERMISSION,
124-
Slug: level,
125-
})
116+
117+
en := sdk.NewPermissionEntitlement(resource, level, resourceTypeUser, resourceTypeTeam)
118+
en.DisplayName = fmt.Sprintf("%s Repo %s", resource.DisplayName, titleCaser.String(level))
119+
en.Description = fmt.Sprintf("Access to %s repository in Github", resource.DisplayName)
120+
en.Annotations = annos
121+
122+
rv = append(rv, en)
126123
}
127124

128125
return rv, "", nil, nil
@@ -138,7 +135,10 @@ func (o *repositoryResourceType) Grants(
138135
return nil, "", nil, err
139136
}
140137

141-
orgName := getOrgName(resource.Id)
138+
orgName, err := getOrgName(ctx, o.client, resource.ParentResourceId)
139+
if err != nil {
140+
return nil, "", nil, err
141+
}
142142

143143
var rv []*v2.Grant
144144
var reqAnnos annotations.Annotations
@@ -191,17 +191,10 @@ func (o *repositoryResourceType) Grants(
191191
return nil, "", nil, err
192192
}
193193

194-
en := &v2.Entitlement{
195-
Id: sdk.NewEntitlementID(resource, permission),
196-
Resource: resource,
197-
}
194+
grant := sdk.NewGrant(resource, permission, ur.Id)
195+
grant.Annotations = annos
198196

199-
rv = append(rv, &v2.Grant{
200-
Entitlement: en,
201-
Id: sdk.NewGrantID(en, ur),
202-
Principal: ur,
203-
Annotations: annos,
204-
})
197+
rv = append(rv, grant)
205198
}
206199
}
207200

@@ -237,22 +230,15 @@ func (o *repositoryResourceType) Grants(
237230
Id: fmt.Sprintf("repo-grant:%s:%d:%s", resource.Id.Resource, team.GetID(), permission),
238231
})
239232

240-
tr, err := teamResource(ctx, orgName, team, resource.ParentResourceId)
233+
tr, err := teamResource(team, resource.ParentResourceId)
241234
if err != nil {
242235
return nil, "", nil, err
243236
}
244237

245-
en := &v2.Entitlement{
246-
Id: sdk.NewEntitlementID(resource, permission),
247-
Resource: resource,
248-
}
238+
grant := sdk.NewGrant(resource, permission, tr.Id)
239+
grant.Annotations = annos
249240

250-
rv = append(rv, &v2.Grant{
251-
Entitlement: en,
252-
Id: sdk.NewGrantID(en, tr),
253-
Principal: tr,
254-
Annotations: annos,
255-
})
241+
rv = append(rv, grant)
256242
}
257243
}
258244
default:

0 commit comments

Comments
 (0)