-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure Git to use bearer token auth mechanism
Tekton prepares the creds to be used through basic auth, which fails when the Bitbucket server has basic auth disabled. Fixes #683
- Loading branch information
1 parent
a54edfc
commit 7fe1271
Showing
6 changed files
with
105 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/opendevstack/pipeline/internal/command" | ||
"github.com/opendevstack/pipeline/pkg/logging" | ||
) | ||
|
||
// gitCheckoutParams holds the parameters configuring the checkout. | ||
type gitCheckoutParams struct { | ||
repoURL string | ||
bitbucketAccessToken string | ||
recurseSubmodules string | ||
depth string | ||
gitFullRef string | ||
} | ||
|
||
// gitCheckout encapsulates the steps required to perform a Git checkout | ||
func gitCheckout(p gitCheckoutParams) (err error) { | ||
steps := [][]string{ | ||
{"init"}, | ||
// Even though Tekton prepares credentials to be used for each task, | ||
// we set the auth explicitly here. The motivation is that Tekton uses | ||
// basic auth to pass the username/token, which fails in environments | ||
// that have basic auth disabled for Bitbucket. | ||
{"config", | ||
fmt.Sprintf("http.%s.extraHeader", p.repoURL), | ||
fmt.Sprintf("Authorization: Bearer %s", p.bitbucketAccessToken), | ||
}, | ||
{"config", | ||
fmt.Sprintf("http.%s/info/lfs.extraHeader", p.repoURL), | ||
fmt.Sprintf("Authorization: Bearer %s", p.bitbucketAccessToken), | ||
}, | ||
{"remote", "add", "origin", p.repoURL}, | ||
{"fetch", | ||
fmt.Sprintf("--recurse-submodules=%s", p.recurseSubmodules), fmt.Sprintf("--depth=%s", p.depth), | ||
"origin", "--update-head-ok", "--force", p.gitFullRef, | ||
}, | ||
{"checkout", "-f", "FETCH_HEAD"}, | ||
} | ||
for _, args := range steps { | ||
if err == nil { | ||
err = runGitCmd(args...) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// runGitCmd executes git with given args. | ||
func runGitCmd(args ...string) error { | ||
var output bytes.Buffer | ||
err := command.Run("git", args, []string{}, &output, &output) | ||
if err != nil { | ||
return fmt.Errorf("git %v: %w\n%s", args, err, output.String()) | ||
} | ||
return nil | ||
} | ||
|
||
func getCommitSHA(dir string) (string, error) { | ||
content, err := os.ReadFile(filepath.Join(dir, ".git/HEAD")) | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.TrimSpace(string(content)), nil | ||
} | ||
|
||
func gitLfsInUse(logger logging.LeveledLoggerInterface, dir string) (lfs bool, err error) { | ||
stdout, stderr, err := command.RunBufferedInDir("git", []string{"lfs", "ls-files", "--all"}, dir) | ||
if err != nil { | ||
return false, fmt.Errorf("cannot list git lfs files: %s (%w)", stderr, err) | ||
} | ||
return strings.TrimSpace(string(stdout)) != "", err | ||
} | ||
|
||
func gitLfsEnableAndPullFiles(logger logging.LeveledLoggerInterface, dir string) (err error) { | ||
stdout, stderr, err := command.RunBufferedInDir("git", []string{"lfs", "install"}, dir) | ||
if err != nil { | ||
return fmt.Errorf("lfs install: %s (%w)", stderr, err) | ||
} | ||
logger.Infof(string(stdout)) | ||
stdout, stderr, err = command.RunBufferedInDir("git", []string{"lfs", "pull"}, dir) | ||
if err != nil { | ||
return fmt.Errorf("lfs pull: %s (%w)", stderr, err) | ||
} | ||
logger.Infof(string(stdout)) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters