Skip to content

Commit e8bab66

Browse files
committed
added working sync secrets
1 parent 70fd634 commit e8bab66

File tree

176 files changed

+68
-62052
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+68
-62052
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ add-dep:
2727
lint:
2828
golangci-lint run
2929

30+
run:
31+
go run ./cmd/baton-github

README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ baton resources
4545

4646
By default, `baton-github` will sync information from any organizations that the provided credential has Administrator permissions on. You can specify exactly which organizations you would like to sync using the `--orgs` flag.
4747

48+
# Sync Secrets
49+
in order to sync secrets, you must use a token created using a github app installed into your organization, more info here:
50+
- [docs](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app)
51+
- [rest api](https://docs.github.com/rest/orgs/personal-access-tokens#list-fine-grained-personal-access-tokens-with-access-to-organization-resources)
52+
4853
# Contributing, Support and Issues
4954

5055
We started Baton because we were tired of taking screenshots and manually building spreadsheets. We welcome contributions, and ideas, no matter how small -- our goal is to make identity and permissions sprawl less painful for everyone. If you have questions, problems, or ideas: Please open a GitHub Issue!
@@ -63,21 +68,25 @@ Usage:
6368
Available Commands:
6469
capabilities Get connector capabilities
6570
completion Generate the autocompletion script for the specified shell
71+
config Get connector config
6672
help Help about any command
6773
6874
Flags:
69-
--client-id string The client ID used to authenticate with ConductorOne ($BATON_CLIENT_ID)
70-
--client-secret string The client secret used to authenticate with ConductorOne ($BATON_CLIENT_SECRET)
71-
-f, --file string The path to the c1z file to sync with ($BATON_FILE) (default "sync.c1z")
72-
-h, --help help for baton-github
73-
--instance-url string The GitHub instance URL to connect to. (default "https://github.com") ($BATON_INSTANCE_URL)
74-
--log-format string The output format for logs: json, console ($BATON_LOG_FORMAT) (default "json")
75-
--log-level string The log level: debug, info, warn, error ($BATON_LOG_LEVEL) (default "info")
76-
--orgs strings Limit syncing to specific organizations. ($BATON_ORGS)
77-
-p, --provisioning This must be set in order for provisioning actions to be enabled ($BATON_PROVISIONING)
78-
--ticketing This must be set to enable ticketing support ($BATON_TICKETING)
79-
--token string required: The GitHub access token used to connect to the GitHub API. ($BATON_TOKEN)
80-
-v, --version version for baton-github
75+
--client-id string The client ID used to authenticate with ConductorOne ($BATON_CLIENT_ID)
76+
--client-secret string The client secret used to authenticate with ConductorOne ($BATON_CLIENT_SECRET)
77+
-f, --file string The path to the c1z file to sync with ($BATON_FILE) (default "sync.c1z")
78+
-h, --help help for baton-github
79+
--instance-url string The GitHub instance URL to connect to. (default "https://github.com") ($BATON_INSTANCE_URL)
80+
--log-format string The output format for logs: json, console ($BATON_LOG_FORMAT) (default "json")
81+
--log-level string The log level: debug, info, warn, error ($BATON_LOG_LEVEL) (default "info")
82+
--orgs strings Limit syncing to specific organizations. ($BATON_ORGS)
83+
--otel-collector-endpoint string The endpoint of the OpenTelemetry collector to send observability data to ($BATON_OTEL_COLLECTOR_ENDPOINT)
84+
-p, --provisioning This must be set in order for provisioning actions to be enabled ($BATON_PROVISIONING)
85+
--skip-full-sync This must be set to skip a full sync ($BATON_SKIP_FULL_SYNC)
86+
--sync-secrets Whether to sync secrets or not ($BATON_SYNC_SECRETS)
87+
--ticketing This must be set to enable ticketing support ($BATON_TICKETING)
88+
--token string required: The GitHub access token used to connect to the GitHub API. ($BATON_TOKEN)
89+
-v, --version version for baton-github
8190
8291
Use "baton-github [command] --help" for more information about a command.
8392
```

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ toolchain go1.23.4
77
require (
88
github.com/conductorone/baton-sdk v0.2.70
99
github.com/deckarep/golang-set/v2 v2.7.0
10-
github.com/google/go-github/v63 v63.0.0
1110
github.com/google/go-github/v69 v69.2.0
1211
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
1312
github.com/migueleliasweb/go-github-mock v0.0.23

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
114114
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
115115
github.com/google/go-github/v59 v59.0.0 h1:7h6bgpF5as0YQLLkEiVqpgtJqjimMYhBkD4jT5aN3VA=
116116
github.com/google/go-github/v59 v59.0.0/go.mod h1:rJU4R0rQHFVFDOkqGWxfLNo6vEk4dv40oDjhV/gH6wM=
117-
github.com/google/go-github/v63 v63.0.0 h1:13xwK/wk9alSokujB9lJkuzdmQuVn2QCPeck76wR3nE=
118-
github.com/google/go-github/v63 v63.0.0/go.mod h1:IqbcrgUmIcEaioWrGYei/09o+ge5vhffGOcxrO0AfmA=
119117
github.com/google/go-github/v69 v69.2.0 h1:wR+Wi/fN2zdUx9YxSmYE0ktiX9IAR/BeePzeaUUbEHE=
120118
github.com/google/go-github/v69 v69.2.0/go.mod h1:xne4jymxLR6Uj9b7J7PyTpkMYstEMMwGZa0Aehh1azM=
121119
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=

pkg/connector/api_token.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,28 @@ import (
1414

1515
func apiTokenResource(ctx context.Context, token *github.PersonalAccessToken) (*v2.Resource, error) {
1616
userId := token.Owner.GetID()
17-
options := []resourceSdk.SecretTraitOption{
17+
18+
options := []resourceSdk.SecretTraitOption{}
19+
options = append(options,
1820
resourceSdk.WithSecretCreatedByID(&v2.ResourceId{
1921
ResourceType: resourceTypeUser.Id,
2022
Resource: strconv.FormatInt(userId, 10),
2123
BatonResource: false,
22-
}),
23-
resourceSdk.WithSecretLastUsedAt(token.TokenLastUsedAt.Time),
24-
resourceSdk.WithSecretCreatedAt(token.AccessGrantedAt.Time),
25-
resourceSdk.WithSecretExpiresAt(token.TokenExpiresAt.Time),
24+
}))
25+
26+
if token.TokenLastUsedAt != nil {
27+
options = append(options, resourceSdk.WithSecretLastUsedAt(token.TokenLastUsedAt.Time))
28+
}
29+
30+
if token.AccessGrantedAt != nil {
31+
options = append(options, resourceSdk.WithSecretCreatedAt(token.AccessGrantedAt.Time))
32+
}
33+
34+
if token.TokenExpiresAt != nil {
35+
options = append(options, resourceSdk.WithSecretExpiresAt(token.TokenExpiresAt.Time))
2636
}
2737
rv, err := resourceSdk.NewSecretResource(
28-
*token.TokenName,
38+
token.GetTokenName(),
2939
resourceTypeApiToken,
3040
token.GetID(),
3141
options,
@@ -113,11 +123,10 @@ func (o *apiTokenResourceType) List(
113123
return rv, pageToken, annotations, nil
114124
}
115125

116-
func apiTokenBuilder(client *github.Client, hasSAMLEnabled *bool, graphqlClient *githubv4.Client, orgCache *orgNameCache) *userResourceType {
117-
return &userResourceType{
118-
resourceType: resourceTypeUser,
126+
func apiTokenBuilder(client *github.Client, hasSAMLEnabled *bool, orgCache *orgNameCache) *apiTokenResourceType {
127+
return &apiTokenResourceType{
128+
resourceType: resourceTypeApiToken,
119129
client: client,
120-
graphqlClient: graphqlClient,
121130
hasSAMLEnabled: hasSAMLEnabled,
122131
orgCache: orgCache,
123132
}

pkg/connector/connector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ type GitHub struct {
7878

7979
func (gh *GitHub) ResourceSyncers(ctx context.Context) []connectorbuilder.ResourceSyncer {
8080
resourceSyncers := []connectorbuilder.ResourceSyncer{
81-
orgBuilder(gh.client, gh.orgCache, gh.orgs),
81+
orgBuilder(gh.client, gh.orgCache, gh.orgs, gh.syncSecrets),
8282
teamBuilder(gh.client, gh.orgCache),
8383
userBuilder(gh.client, gh.hasSAMLEnabled, gh.graphqlClient, gh.orgCache),
8484
repositoryBuilder(gh.client, gh.orgCache),
8585
}
8686

8787
if gh.syncSecrets {
88-
resourceSyncers = append(resourceSyncers, apiTokenBuilder(gh.client, gh.hasSAMLEnabled, gh.graphqlClient, gh.orgCache))
88+
resourceSyncers = append(resourceSyncers, apiTokenBuilder(gh.client, gh.hasSAMLEnabled, gh.orgCache))
8989
}
9090
return resourceSyncers
9191
}

pkg/connector/org.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/google/go-github/v69/github"
1717
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
1818
"go.uber.org/zap"
19+
"google.golang.org/protobuf/proto"
1920
)
2021

2122
const (
@@ -34,25 +35,34 @@ type orgResourceType struct {
3435
client *github.Client
3536
orgs map[string]struct{}
3637
orgCache *orgNameCache
38+
syncSecrets bool
3739
}
3840

3941
func organizationResource(
4042
ctx context.Context,
4143
org *github.Organization,
4244
parentResourceID *v2.ResourceId,
45+
syncSecrets bool,
4346
) (*v2.Resource, error) {
47+
48+
annotations := []proto.Message{
49+
&v2.ExternalLink{Url: org.GetHTMLURL()},
50+
&v2.V1Identifier{Id: fmt.Sprintf("org:%d", org.GetID())},
51+
&v2.ChildResourceType{ResourceTypeId: resourceTypeUser.Id},
52+
&v2.ChildResourceType{ResourceTypeId: resourceTypeTeam.Id},
53+
&v2.ChildResourceType{ResourceTypeId: resourceTypeRepository.Id},
54+
}
55+
if syncSecrets {
56+
annotations = append(annotations, &v2.ChildResourceType{ResourceTypeId: resourceTypeApiToken.Id})
57+
}
58+
4459
return resource.NewResource(
4560
org.GetLogin(),
4661
resourceTypeOrg,
4762
org.GetID(),
4863
resource.WithParentResourceID(parentResourceID),
4964
resource.WithAnnotation(
50-
&v2.ExternalLink{Url: org.GetHTMLURL()},
51-
&v2.V1Identifier{Id: fmt.Sprintf("org:%d", org.GetID())},
52-
&v2.ChildResourceType{ResourceTypeId: resourceTypeUser.Id},
53-
&v2.ChildResourceType{ResourceTypeId: resourceTypeTeam.Id},
54-
&v2.ChildResourceType{ResourceTypeId: resourceTypeRepository.Id},
55-
&v2.ChildResourceType{ResourceTypeId: resourceTypeApiToken.Id},
65+
annotations...,
5666
),
5767
)
5868
}
@@ -112,7 +122,7 @@ func (o *orgResourceType) List(
112122
continue
113123
}
114124

115-
orgResource, err := organizationResource(ctx, org, parentResourceID)
125+
orgResource, err := organizationResource(ctx, org, parentResourceID, o.syncSecrets)
116126
if err != nil {
117127
return nil, "", nil, err
118128
}
@@ -374,7 +384,7 @@ func (o *orgResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotati
374384
return nil, nil
375385
}
376386

377-
func orgBuilder(client *github.Client, orgCache *orgNameCache, orgs []string) *orgResourceType {
387+
func orgBuilder(client *github.Client, orgCache *orgNameCache, orgs []string, syncSecrets bool) *orgResourceType {
378388
orgMap := make(map[string]struct{})
379389

380390
for _, o := range orgs {
@@ -386,5 +396,6 @@ func orgBuilder(client *github.Client, orgCache *orgNameCache, orgs []string) *o
386396
orgs: orgMap,
387397
client: client,
388398
orgCache: orgCache,
399+
syncSecrets: syncSecrets,
389400
}
390401
}

pkg/connector/org_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ func TestOrganization(t *testing.T) {
2323

2424
githubClient := github.NewClient(mgh.Server())
2525
cache := newOrgNameCache(githubClient)
26-
client := orgBuilder(githubClient, cache, nil)
26+
client := orgBuilder(githubClient, cache, nil, false)
2727

28-
organization, _ := organizationResource(ctx, githubOrganization, nil)
28+
organization, _ := organizationResource(ctx, githubOrganization, nil, false)
2929
user, _ := userResource(ctx, githubUser, *githubUser.Email, nil)
3030

3131
entitlement := v2.Entitlement{

pkg/connector/repository_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestRepository(t *testing.T) {
2626
cache := newOrgNameCache(githubClient)
2727
client := repositoryBuilder(githubClient, cache)
2828

29-
organization, _ := organizationResource(ctx, githubOrganization, nil)
29+
organization, _ := organizationResource(ctx, githubOrganization, nil, false)
3030
repository, _ := repositoryResource(ctx, githubRepository, organization.Id)
3131
user, _ := userResource(ctx, githubUser, *githubUser.Email, nil)
3232

pkg/connector/team_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestTeam(t *testing.T) {
2626
cache := newOrgNameCache(githubClient)
2727
client := teamBuilder(githubClient, cache)
2828

29-
organization, _ := organizationResource(ctx, githubOrganization, nil)
29+
organization, _ := organizationResource(ctx, githubOrganization, nil, false)
3030
team, _ := teamResource(githubTeam, organization.Id)
3131
user, _ := userResource(ctx, githubUser, *githubUser.Email, nil)
3232

0 commit comments

Comments
 (0)