Skip to content

Commit

Permalink
Add tests for git auth autopropagation
Browse files Browse the repository at this point in the history
Also fix bug where git auth was overwriting explicit gomod git auth.

Signed-off-by: Peter Engelbert <[email protected]>
  • Loading branch information
pmengelbert committed Feb 3, 2025
1 parent f275381 commit 58ec093
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 32 deletions.
80 changes: 80 additions & 0 deletions load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,86 @@ func TestSourceFillDefaults(t *testing.T) {
Path: ".",
},
},
{
title: "auto-propagate git auth to gomod auth",
before: Source{
Git: &SourceGit{
URL: "https://github.com/auth/test.git",
Commit: t.Name(),
Auth: GitAuth{
Token: "DALEC_GIT_AUTH_TOKEN_GITHUB",
},
},
Generate: []*SourceGenerator{
{
Gomod: &GeneratorGomod{},
},
},
},
after: Source{
Git: &SourceGit{
URL: "https://github.com/auth/test.git",
Commit: t.Name(),
Auth: GitAuth{
Token: "DALEC_GIT_AUTH_TOKEN_GITHUB",
},
},
Generate: []*SourceGenerator{
{
Gomod: &GeneratorGomod{
Auth: map[string]GomodGitAuth{
"github.com": {
Token: "DALEC_GIT_AUTH_TOKEN_GITHUB",
},
},
},
},
},
},
},
{
title: "don't overwrite existing git auth when auto-propagating",
before: Source{
Git: &SourceGit{
URL: "https://github.com/auth/test.git",
Commit: t.Name(),
Auth: GitAuth{
Token: "DALEC_GIT_AUTH_TOKEN_GITHUB",
},
},
Generate: []*SourceGenerator{
{
Gomod: &GeneratorGomod{
Auth: map[string]GomodGitAuth{
"github.com": {
Token: "ANOTHER_DALEC_GIT_AUTH_TOKEN_GITHUB",
},
},
},
},
},
},
after: Source{
Git: &SourceGit{
URL: "https://github.com/auth/test.git",
Commit: t.Name(),
Auth: GitAuth{
Token: "DALEC_GIT_AUTH_TOKEN_GITHUB",
},
},
Generate: []*SourceGenerator{
{
Gomod: &GeneratorGomod{
Auth: map[string]GomodGitAuth{
"github.com": {
Token: "ANOTHER_DALEC_GIT_AUTH_TOKEN_GITHUB",
},
},
},
},
},
},
},
}

for _, tc := range cases {
Expand Down
5 changes: 5 additions & 0 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,11 @@ func (gm *GeneratorGomod) fillDefaults(host string, authInfo *GitAuth) {
}
)

// Don't overwrite explicitly-specified auth
_, ok := gm.Auth[host]
if ok {
return
}
const defaultUsername = "git"

var gomodAuth GomodGitAuth
Expand Down
32 changes: 0 additions & 32 deletions source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,38 +225,6 @@ func TestSourceGitHTTP(t *testing.T) {
m, ops := getGomodLLBOps(ctx, t, spec)
checkGitAuth(t, m, ops, &src, numSecrets, numSSH)
})

t.Run("gomod auth auto-propagate", func(t *testing.T) {
const (
numSecrets = 1
numSSH = 0
)
src := Source{
Git: &SourceGit{
URL: "https://localhost/test.git",
Commit: t.Name(),
Auth: GitAuth{
Token: "DALEC_GIT_AUTH_TOKEN_GITHUB",
},
},
Generate: []*SourceGenerator{
{
Gomod: &GeneratorGomod{},
},
},
}

const srcName = "foo"
spec := Spec{
Sources: map[string]Source{
srcName: src,
},
}

spec.FillDefaults()
m, ops := getGomodLLBOps(ctx, t, spec)
checkGitAuth(t, m, ops, &src, numSecrets, numSSH)
})
}

func getGomodLLBOps(ctx context.Context, t *testing.T, spec Spec) (map[string]*pb.Op, []*pb.Op) {
Expand Down

0 comments on commit 58ec093

Please sign in to comment.