Skip to content

Commit 403e935

Browse files
committed
Git based commands, only use GITHUB_TOKEN when interacting with GitHub's API
This change also removes the submodules git update as this is not used anymore, therefore does not need updating with the new GetGitAuth signature. Signed-off-by: James Rawlings <[email protected]>
1 parent 922b5a2 commit 403e935

File tree

9 files changed

+73
-222
lines changed

9 files changed

+73
-222
lines changed

pkg/advisory/data_session.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ func NewDataSession(ctx context.Context, opts DataSessionOptions) (*DataSession,
5151

5252
ds.githubClient = opts.GitHubClient
5353

54+
gitAuth, err := wgit.GetGitAuth(opts.Distro.Absolute.AdvisoriesHTTPSCloneURL())
55+
if err != nil {
56+
return nil, fmt.Errorf("getting git auth: %w", err)
57+
}
58+
5459
// clone advisories repo
5560
repo, err := git.PlainCloneContext(ctx, tempDir, false, &git.CloneOptions{
5661
URL: opts.Distro.Absolute.AdvisoriesHTTPSCloneURL(),
57-
Auth: wgit.GetGitAuth(),
62+
Auth: gitAuth,
5863
})
5964
if err != nil {
6065
return nil, fmt.Errorf("cloning advisories repo: %w", err)
@@ -168,9 +173,14 @@ func (ds DataSession) Modified() bool {
168173
// Push pushes the changes made during the session to the remote advisories
169174
// repository.
170175
func (ds DataSession) Push(ctx context.Context) error {
171-
err := ds.repo.PushContext(ctx, &git.PushOptions{
176+
gitAuth, err := wgit.GetGitAuth(ds.distro.Absolute.AdvisoriesHTTPSCloneURL())
177+
if err != nil {
178+
return fmt.Errorf("getting git auth: %w", err)
179+
}
180+
181+
err = ds.repo.PushContext(ctx, &git.PushOptions{
172182
RemoteURL: ds.distro.Absolute.AdvisoriesHTTPSCloneURL(),
173-
Auth: wgit.GetGitAuth(),
183+
Auth: gitAuth,
174184
})
175185
if err != nil {
176186
return fmt.Errorf("pushing changes: %w", err)

pkg/git/git.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package git
22

33
import (
44
"fmt"
5+
"log/slog"
56
"net/url"
67
"os"
78
"os/exec"
89
"strings"
910
"time"
1011

12+
"github.com/chainguard-dev/clog"
13+
1114
"github.com/go-git/go-git/v5/plumbing"
1215
"github.com/go-git/go-git/v5/plumbing/object"
1316
"github.com/go-git/go-git/v5/plumbing/storer"
@@ -19,20 +22,33 @@ import (
1922
gitHttp "github.com/go-git/go-git/v5/plumbing/transport/http"
2023
)
2124

22-
func GetGitAuth() *gitHttp.BasicAuth {
25+
func GetGitAuth(gitURL string) (*gitHttp.BasicAuth, error) {
26+
logger := clog.NewLogger(slog.Default()) // TODO: plumb through context, everywhere
27+
28+
parsedURL, err := ParseGitURL(gitURL)
29+
if err != nil {
30+
return nil, fmt.Errorf("failed to parse git URL %q: %w", gitURL, err)
31+
}
32+
33+
// Only use GITHUB_TOKEN for github.com URLs
34+
if parsedURL.Host != "github.com" {
35+
logger.Warnf("host %q is not github.com, not using GITHUB_TOKEN for authentication", parsedURL.Host)
36+
return nil, nil
37+
}
38+
2339
gitToken := os.Getenv("GITHUB_TOKEN")
2440

2541
if gitToken == "" {
2642
// If the token is empty, there's no way we can return a usable authentication
2743
// anyway. Whereas if we return nil, and don't auth, we have a chance at
2844
// succeeding with access of a public repo.
29-
return nil
45+
return &gitHttp.BasicAuth{}, nil
3046
}
3147

3248
return &gitHttp.BasicAuth{
3349
Username: "abc123",
3450
Password: gitToken,
35-
}
51+
}, nil
3652
}
3753

3854
type URL struct {
@@ -182,7 +198,10 @@ func TempClone(gitURL, hash string, useAuth bool) (repoDir string, err error) {
182198

183199
var auth transport.AuthMethod
184200
if useAuth {
185-
auth = GetGitAuth()
201+
auth, err = GetGitAuth(gitURL)
202+
if err != nil {
203+
return dir, fmt.Errorf("unable to get git auth: %w", err)
204+
}
186205
}
187206

188207
repo, err := git.PlainClone(dir, false, &git.CloneOptions{

pkg/git/submodules/testdata/multiple_submodules/.gitmodules

Lines changed: 0 additions & 13 deletions
This file was deleted.

pkg/git/submodules/update.go

Lines changed: 0 additions & 116 deletions
This file was deleted.

pkg/git/submodules/update_test.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

pkg/git/tag.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ func PushTag(dir, tagName string) error {
5151
}
5252
remoteURL := fmt.Sprintf("https://github.com/%s/%s.git", gitURL.Organisation, gitURL.Name)
5353

54+
gitAuth, err := GetGitAuth(remoteURL)
55+
if err != nil {
56+
return fmt.Errorf("failed to get git auth: %w", err)
57+
}
58+
5459
po := &git.PushOptions{
5560
RemoteName: "origin",
5661
RemoteURL: remoteURL,
5762
RefSpecs: []config.RefSpec{config.RefSpec(fmt.Sprintf("refs/tags/%s:refs/tags/%s", tagName, tagName))},
58-
Auth: GetGitAuth(),
63+
Auth: gitAuth,
5964
}
6065

6166
err = r.Push(po)

pkg/update/deps/cleanup.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ func gitCheckout(p *config.Pipeline, dir string, mutations map[string]string) er
4040
return err
4141
}
4242

43+
gitAuth, err := wgit.GetGitAuth(repoValue)
44+
if err != nil {
45+
return fmt.Errorf("failed to get git auth: %w", err)
46+
}
47+
4348
cloneOpts := &git.CloneOptions{
4449
URL: repoValue,
4550
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/tags/%s", evaluatedTag)),
4651
Progress: os.Stdout,
4752
RecurseSubmodules: git.NoRecurseSubmodules,
4853
Depth: 1,
49-
Auth: wgit.GetGitAuth(),
54+
Auth: gitAuth,
5055
}
5156

5257
log.Printf("cloning sources from %s tag %s into a temporary directory '%s', this may take a while", repoValue, dir, evaluatedTag)

pkg/update/package.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ func (o *PackageOptions) UpdatePackageCmd(ctx context.Context) error {
5353
defer os.Remove(tempDir)
5454
}
5555

56+
gitAuth, err := wolfigit.GetGitAuth(o.TargetRepo)
57+
if err != nil {
58+
return fmt.Errorf("failed to get git auth: %w", err)
59+
}
60+
5661
cloneOpts := &git.CloneOptions{
5762
URL: o.TargetRepo,
5863
Progress: os.Stdout,
5964
RecurseSubmodules: git.NoRecurseSubmodules,
60-
Auth: wolfigit.GetGitAuth(),
65+
Auth: gitAuth,
6166
Depth: 1,
6267
}
6368

@@ -119,12 +124,18 @@ func (o *PackageOptions) updateAdvisories(ctx context.Context, repo *git.Reposit
119124
if err != nil {
120125
return err
121126
}
127+
128+
gitAuth, err := wolfigit.GetGitAuth(gitURL.RawURL)
129+
if err != nil {
130+
return fmt.Errorf("failed to get git auth: %w", err)
131+
}
132+
122133
// checkout repo into tmp dir so we know we are working on a clean HEAD
123134
cloneOpts := &git.CloneOptions{
124135
URL: gitURL.RawURL,
125136
RecurseSubmodules: git.NoRecurseSubmodules,
126137
ShallowSubmodules: true,
127-
Auth: wolfigit.GetGitAuth(),
138+
Auth: gitAuth,
128139
Tags: git.AllTags,
129140
Depth: 20,
130141
}

0 commit comments

Comments
 (0)