Skip to content

Commit 1da66e4

Browse files
authored
Use SDK Id funcs for generating connector object IDs (#3)
* Use SDK Id funcs for generating connector object IDs * Drop unused ID formatting functions * Fix lint
1 parent 9619812 commit 1da66e4

File tree

18 files changed

+773
-112
lines changed

18 files changed

+773
-112
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.dylib
77
build/
88
*.c1z
9+
*.db
910

1011
# Test binary, built with `go test -c`
1112
*.test

.golangci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ linters-settings:
5757
linters:
5858
disable-all: true
5959
enable:
60-
- deadcode # Finds unused code
6160
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
6261
- gosimple # Linter for Go source code that specializes in simplifying a code
6362
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
6463
- ineffassign # Detects when assignments to existing variables are not used
6564
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
6665
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
6766
- unused # Checks Go code for unused constants, variables, functions and types
68-
- varcheck # Finds unused global variables and constants
6967
- asasalint # Check for pass []any as any in variadic func(...any)
7068
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
7169
- bidichk # Checks for dangerous unicode character sequences

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.4
6+
github.com/conductorone/baton-sdk v0.0.7
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.4 h1:Je19ih9toyocBbmJUIMaoR866v6vVIrr5DpiCb2n/ns=
93-
github.com/conductorone/baton-sdk v0.0.4/go.mod h1:jPdcy08LmTIPzgZcSOo7mviSAG0NUbjavg/1LpCTeOI=
92+
github.com/conductorone/baton-sdk v0.0.7 h1:4+PMrcG/HT9x6UCfHaL8wH6HFaSHhc/rGQWko/92zRc=
93+
github.com/conductorone/baton-sdk v0.0.7/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/helpers.go

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,11 @@ import (
1616

1717
var titleCaser = cases.Title(language.English)
1818

19-
// fmtResourceId returns a new v2.ResourceID given a resource type and resource ID.
20-
func fmtResourceId(rTypeID string, orgName string, id int64) *v2.ResourceId {
21-
return &v2.ResourceId{
22-
ResourceType: rTypeID,
23-
Resource: fmt.Sprintf("%s:%d", orgName, id),
24-
}
25-
}
26-
2719
func getOrgName(rID *v2.ResourceId) string {
2820
ret, _, _ := strings.Cut(rID.Resource, ":")
2921
return ret
3022
}
3123

32-
// fmtResourceRole returns a formatted string that represents a unique ID for an entitlement.
33-
func fmtResourceRole(resourceID *v2.ResourceId, role string) string {
34-
return fmt.Sprintf(
35-
"%s:%s:role:%s",
36-
resourceID.ResourceType,
37-
resourceID.Resource,
38-
role,
39-
)
40-
}
41-
42-
func fmtResourceGrant(resourceID *v2.ResourceId, principalId *v2.ResourceId, permission string) string {
43-
return fmt.Sprintf(
44-
"%s-grant:%s:%s:%s:%s",
45-
resourceID.ResourceType,
46-
resourceID.Resource,
47-
principalId.ResourceType,
48-
principalId.Resource,
49-
permission,
50-
)
51-
}
52-
5324
func v1AnnotationsForResourceType(resourceTypeID string) annotations.Annotations {
5425
annos := annotations.Annotations{}
5526
annos.Append(&v2.V1Identifier{
@@ -59,12 +30,11 @@ func v1AnnotationsForResourceType(resourceTypeID string) annotations.Annotations
5930
return annos
6031
}
6132

62-
// parseResourceToGithub returns the resource type and upstream object ID for a given v2.ResourceID.
33+
// parseResourceToGithub returns the upstream API ID by looking at the last 'part' of the resource ID.
6334
func parseResourceToGithub(id *v2.ResourceId) (int64, error) {
64-
if _, rID, found := strings.Cut(id.Resource, ":"); found {
65-
return strconv.ParseInt(rID, 10, 64)
66-
}
67-
return 0, fmt.Errorf("github-connector: invalid internal id: '%s'", id.Resource)
35+
idParts := strings.Split(id.Resource, ":")
36+
37+
return strconv.ParseInt(idParts[len(idParts)-1], 10, 64)
6838
}
6939

7040
func parsePageToken(i string, resourceID *v2.ResourceId) (*pagination.Bag, int, error) {

pkg/connector/org.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
99
"github.com/conductorone/baton-sdk/pkg/annotations"
1010
"github.com/conductorone/baton-sdk/pkg/pagination"
11+
"github.com/conductorone/baton-sdk/pkg/sdk"
1112
"github.com/google/go-github/v41/github"
1213
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
1314
"go.uber.org/zap"
@@ -35,7 +36,7 @@ func (o *orgResourceType) ResourceType(_ context.Context) *v2.ResourceType {
3536

3637
func (o *orgResourceType) List(
3738
ctx context.Context,
38-
_ *v2.ResourceId,
39+
parentResourceID *v2.ResourceId,
3940
pToken *pagination.Token,
4041
) ([]*v2.Resource, string, annotations.Annotations, error) {
4142
bag, page, err := parsePageToken(pToken.Token, &v2.ResourceId{ResourceType: resourceTypeOrg.Id})
@@ -89,11 +90,13 @@ func (o *orgResourceType) List(
8990
annos.Append(&v2.ChildResourceType{ResourceTypeId: resourceTypeTeam.Id})
9091
annos.Append(&v2.ChildResourceType{ResourceTypeId: resourceTypeRepository.Id})
9192

93+
resourceID, err := sdk.NewResourceID(resourceTypeOrg, parentResourceID, org.GetLogin())
94+
if err != nil {
95+
return nil, "", nil, err
96+
}
97+
9298
ret = append(ret, &v2.Resource{
93-
Id: &v2.ResourceId{
94-
ResourceType: resourceTypeOrg.Id,
95-
Resource: org.GetLogin(),
96-
},
99+
Id: resourceID,
97100
DisplayName: org.GetLogin(),
98101
Annotations: annos,
99102
})
@@ -114,7 +117,7 @@ func (o *orgResourceType) Entitlements(
114117
Id: fmt.Sprintf("org:%s:role:%s", resource.Id, level),
115118
})
116119
rv = append(rv, &v2.Entitlement{
117-
Id: fmtResourceRole(resource.Id, level),
120+
Id: sdk.NewEntitlementID(resource, level),
118121
Resource: resource,
119122
DisplayName: fmt.Sprintf("%s Org %s", resource.DisplayName, titleCaser.String(level)),
120123
Description: fmt.Sprintf("Access to %s org in Github", resource.DisplayName),
@@ -184,12 +187,15 @@ func (o *orgResourceType) Grants(
184187
annos.Append(&v2.V1Identifier{
185188
Id: fmt.Sprintf("org-grant:%s:%d:%s", resource.Id.Resource, user.GetID(), roleName),
186189
})
190+
191+
en := &v2.Entitlement{
192+
Id: sdk.NewEntitlementID(resource, roleName),
193+
Resource: resource,
194+
}
195+
187196
rv = append(rv, &v2.Grant{
188-
Id: fmtResourceGrant(resource.Id, ur.Id, roleName),
189-
Entitlement: &v2.Entitlement{
190-
Id: fmtResourceRole(resource.Id, roleName),
191-
Resource: resource,
192-
},
197+
Id: sdk.NewGrantID(en, ur),
198+
Entitlement: en,
193199
Annotations: annos,
194200
Principal: ur,
195201
})

pkg/connector/repository.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
88
"github.com/conductorone/baton-sdk/pkg/annotations"
99
"github.com/conductorone/baton-sdk/pkg/pagination"
10+
"github.com/conductorone/baton-sdk/pkg/sdk"
1011
"github.com/google/go-github/v41/github"
1112
)
1213

@@ -37,8 +38,13 @@ func repositoryResource(ctx context.Context, orgName string, repo *github.Reposi
3738
Id: fmt.Sprintf("repo:%d", repo.GetID()),
3839
})
3940

41+
resourceID, err := sdk.NewResourceID(resourceTypeRepository, parentResourceID, repo.GetID())
42+
if err != nil {
43+
return nil, err
44+
}
45+
4046
return &v2.Resource{
41-
Id: fmtResourceId(resourceTypeRepository.Id, orgName, repo.GetID()),
47+
Id: resourceID,
4248
DisplayName: repo.GetName(),
4349
ParentResourceId: parentResourceID,
4450
Annotations: annos,
@@ -108,7 +114,7 @@ func (o *repositoryResourceType) Entitlements(_ context.Context, resource *v2.Re
108114
Id: fmt.Sprintf("repo:%s:role:%s", resource.Id, level),
109115
})
110116
rv = append(rv, &v2.Entitlement{
111-
Id: fmtResourceRole(resource.Id, level),
117+
Id: sdk.NewEntitlementID(resource, level),
112118
Resource: resource,
113119
DisplayName: fmt.Sprintf("%s Repo %s", resource.DisplayName, titleCaser.String(level)),
114120
Description: fmt.Sprintf("Access to %s repository in Github", resource.DisplayName),
@@ -185,12 +191,14 @@ func (o *repositoryResourceType) Grants(
185191
return nil, "", nil, err
186192
}
187193

194+
en := &v2.Entitlement{
195+
Id: sdk.NewEntitlementID(resource, permission),
196+
Resource: resource,
197+
}
198+
188199
rv = append(rv, &v2.Grant{
189-
Entitlement: &v2.Entitlement{
190-
Id: fmtResourceRole(resource.Id, permission),
191-
Resource: resource,
192-
},
193-
Id: fmtResourceGrant(resource.Id, ur.Id, permission),
200+
Entitlement: en,
201+
Id: sdk.NewGrantID(en, ur),
194202
Principal: ur,
195203
Annotations: annos,
196204
})
@@ -234,12 +242,14 @@ func (o *repositoryResourceType) Grants(
234242
return nil, "", nil, err
235243
}
236244

245+
en := &v2.Entitlement{
246+
Id: sdk.NewEntitlementID(resource, permission),
247+
Resource: resource,
248+
}
249+
237250
rv = append(rv, &v2.Grant{
238-
Entitlement: &v2.Entitlement{
239-
Id: fmtResourceRole(resource.Id, permission),
240-
Resource: resource,
241-
},
242-
Id: fmtResourceGrant(resource.Id, tr.Id, permission),
251+
Entitlement: en,
252+
Id: sdk.NewGrantID(en, tr),
243253
Principal: tr,
244254
Annotations: annos,
245255
})

pkg/connector/team.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
88
"github.com/conductorone/baton-sdk/pkg/annotations"
99
"github.com/conductorone/baton-sdk/pkg/pagination"
10+
"github.com/conductorone/baton-sdk/pkg/sdk"
1011
"github.com/google/go-github/v41/github"
1112
"google.golang.org/protobuf/types/known/structpb"
1213
)
@@ -39,8 +40,13 @@ func teamResource(ctx context.Context, orgName string, team *github.Team, parent
3940
})
4041
annos.Append(&v2.ChildResourceType{ResourceTypeId: resourceTypeTeam.Id})
4142

43+
resourceID, err := sdk.NewResourceID(resourceTypeTeam, parentResourceID, team.GetID())
44+
if err != nil {
45+
return nil, err
46+
}
47+
4248
return &v2.Resource{
43-
Id: fmtResourceId(resourceTypeTeam.Id, orgName, team.GetID()),
49+
Id: resourceID,
4450
DisplayName: team.GetName(),
4551
Annotations: annos,
4652
ParentResourceId: parentResourceID,
@@ -151,7 +157,7 @@ func (o *teamResourceType) Entitlements(_ context.Context, resource *v2.Resource
151157
Id: fmt.Sprintf("team:%s:role:%s", resource.Id, level),
152158
})
153159
rv = append(rv, &v2.Entitlement{
154-
Id: fmtResourceRole(resource.Id, level),
160+
Id: sdk.NewEntitlementID(resource, level),
155161
Resource: resource,
156162
DisplayName: fmt.Sprintf("%s Team %s", resource.DisplayName, titleCaser.String(level)),
157163
Description: fmt.Sprintf("Access to %s team in Github", resource.DisplayName),
@@ -218,12 +224,14 @@ func (o *teamResourceType) Grants(ctx context.Context, resource *v2.Resource, pT
218224
return nil, "", nil, err
219225
}
220226

227+
en := &v2.Entitlement{
228+
Id: sdk.NewEntitlementID(resource, membership.GetRole()),
229+
Resource: resource,
230+
}
231+
221232
rv = append(rv, &v2.Grant{
222-
Entitlement: &v2.Entitlement{
223-
Id: fmtResourceRole(resource.Id, membership.GetRole()),
224-
Resource: resource,
225-
},
226-
Id: fmtResourceGrant(resource.Id, ur.Id, membership.GetRole()),
233+
Entitlement: en,
234+
Id: sdk.NewGrantID(en, ur),
227235
Principal: ur,
228236
Annotations: annos,
229237
})

pkg/connector/user.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
1010
"github.com/conductorone/baton-sdk/pkg/annotations"
1111
"github.com/conductorone/baton-sdk/pkg/pagination"
12+
"github.com/conductorone/baton-sdk/pkg/sdk"
1213
"github.com/google/go-github/v41/github"
1314
"google.golang.org/protobuf/types/known/structpb"
1415
)
@@ -35,11 +36,13 @@ func userResource(ctx context.Context, user *github.User) (*v2.Resource, error)
3536
Id: strconv.FormatInt(user.GetID(), 10),
3637
})
3738

39+
resourceID, err := sdk.NewResourceID(resourceTypeUser, nil, user.GetID())
40+
if err != nil {
41+
return nil, err
42+
}
43+
3844
return &v2.Resource{
39-
Id: &v2.ResourceId{
40-
ResourceType: resourceTypeUser.Id,
41-
Resource: fmt.Sprintf("%d", user.GetID()),
42-
},
45+
Id: resourceID,
4346
DisplayName: displayName,
4447
Annotations: annos,
4548
}, nil

vendor/github.com/conductorone/baton-sdk/internal/dotc1z/grants.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)