Skip to content

Commit 217779c

Browse files
committed
Fix http authentication for git resolver
1 parent a2198ad commit 217779c

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

examples/v1/pipelineruns/no-ci/git-resolver-custom-secret.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ spec:
3131
# my-secret-token should be created in the namespace where the
3232
# pipelinerun is created and contain a GitHub personal access
3333
# token in the token key of the secret.
34-
- name: token
34+
# Can be created with the command:
35+
# kubectl create secret generic my-secret-token --from-literal token=$RAW_TOKEN
36+
- name: gitToken
3537
value: my-secret-token
36-
- name: tokenKey
38+
- name: gitTokenKey
3739
value: token
3840
params:
3941
- name: url

pkg/resolution/resolver/git/repository.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (r remote) clone(ctx context.Context) (*repository, func(), error) {
4747
os.RemoveAll(tmpDir)
4848
}
4949

50-
repo := repository{
50+
repo := &repository{
5151
url: r.url,
5252
username: r.username,
5353
password: r.password,
@@ -62,7 +62,7 @@ func (r remote) clone(ctx context.Context) (*repository, func(), error) {
6262
}
6363
return nil, cleanupFunc, err
6464
}
65-
return &repo, cleanupFunc, nil
65+
return repo, cleanupFunc, nil
6666
}
6767

6868
type repository struct {
@@ -105,19 +105,18 @@ func (repo *repository) execGit(ctx context.Context, subCmd string, args ...stri
105105
// We need to configure which directory contains the cloned repository since `cd`ing
106106
// into the repository directory is not concurrency-safe
107107
configArgs := []string{"-C", repo.directory}
108+
108109
env := []string{"GIT_TERMINAL_PROMPT=false"}
109-
if subCmd == "clone" {
110-
// NOTE: Since this is only HTTP basic auth, authentication only supports http
111-
// cloning, while unauthenticated cloning works for any other protocol supported
112-
// by the git binary which doesn't require authentication.
113-
if repo.username != "" && repo.password != "" {
114-
token := base64.URLEncoding.EncodeToString([]byte(repo.username + ":" + repo.password))
115-
env = append(
116-
env,
117-
"GIT_AUTH_HEADER=Authorization=Basic "+token,
118-
)
119-
configArgs = append(configArgs, "--config-env", "http.extraHeader=GIT_AUTH_HEADER")
120-
}
110+
// NOTE: Since this is only HTTP basic auth, authentication is only supported for http
111+
// cloning, while unauthenticated cloning is supported for any other protocol supported
112+
// by git which doesn't require authentication.
113+
if repo.username != "" && repo.password != "" {
114+
token := base64.URLEncoding.EncodeToString([]byte(repo.username + ":" + repo.password))
115+
env = append(
116+
env,
117+
"GIT_AUTH_HEADER=Authorization: Basic "+token,
118+
)
119+
configArgs = append(configArgs, "--config-env", "http.extraHeader=GIT_AUTH_HEADER")
121120
}
122121
cmd := repo.executor(ctx, "git", append(configArgs, args...)...)
123122
cmd.Env = append(cmd.Environ(), env...)

pkg/resolution/resolver/git/repository_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestClone(t *testing.T) {
7171
if test.username != "" {
7272
token := base64.URLEncoding.EncodeToString([]byte(test.username + ":" + test.password))
7373
expectedCmd = append(expectedCmd, "--config-env", "http.extraHeader=GIT_AUTH_HEADER")
74-
expectedEnv = append(expectedEnv, "GIT_AUTH_HEADER=Authorization=Basic "+token)
74+
expectedEnv = append(expectedEnv, "GIT_AUTH_HEADER=Authorization: Basic "+token)
7575
}
7676
expectedCmd = append(expectedCmd, "clone", test.url, repo.directory, "--depth=1", "--no-checkout")
7777

test/resolvers_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,59 @@ spec:
471471
}
472472
}
473473

474+
func TestGitResolver_HTTPAuth(t *testing.T) {
475+
ctx := t.Context()
476+
c, namespace := setup(ctx, t, gitFeatureFlags)
477+
478+
t.Parallel()
479+
480+
knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
481+
defer tearDown(ctx, t, c, namespace)
482+
483+
giteaClusterHostname, tokenSecretName := setupGitea(ctx, t, c, namespace)
484+
485+
requestUrl := fmt.Sprintf("http://%s/%s/%s", net.JoinHostPort(giteaClusterHostname, "3000"), scmRemoteOrg, scmRemoteRepo)
486+
487+
trName := helpers.ObjectNameForTest(t)
488+
tr := parse.MustParseV1TaskRun(t, fmt.Sprintf(`
489+
metadata:
490+
name: %s
491+
namespace: %s
492+
spec:
493+
taskRef:
494+
resolver: git
495+
params:
496+
- name: url
497+
value: %s
498+
- name: revision
499+
value: %s
500+
- name: pathInRepo
501+
value: %s
502+
- name: gitToken
503+
value: %s
504+
- name: gitTokenKey
505+
value: %s
506+
`,
507+
trName,
508+
namespace,
509+
requestUrl,
510+
scmRemoteBranch,
511+
scmRemoteTaskPath,
512+
tokenSecretName,
513+
scmTokenSecretKey,
514+
))
515+
516+
_, err := c.V1TaskRunClient.Create(ctx, tr, metav1.CreateOptions{})
517+
if err != nil {
518+
t.Fatalf("Failed to create TaskRun: %v", err)
519+
}
520+
521+
t.Logf("Waiting for TaskRun %s in namespace %s to complete", trName, namespace)
522+
if err := WaitForTaskRunState(ctx, c, trName, TaskRunSucceed(trName), "TaskRunSuccess", v1Version); err != nil {
523+
t.Fatalf("Error waiting for TaskRun %s to finish: %s", trName, err)
524+
}
525+
}
526+
474527
func TestGitResolver_API(t *testing.T) {
475528
ctx := t.Context()
476529
c, namespace := setup(ctx, t, gitFeatureFlags)

0 commit comments

Comments
 (0)