fix(scriptengine): replace-all the GitHub credential helper (#2999)
Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
@@ -357,7 +357,16 @@ gh config set git_protocol https --host github.com 2>/dev/null || true`
|
||||
lines := []string{
|
||||
"# GitHub token authentication",
|
||||
"# Configure git credential helper for GitHub HTTPS authentication",
|
||||
`git config --global credential.https://github.com.helper '!/bin/sh -c "echo username=x-access-token; echo password=${GH_TOKEN:-${GITHUB_TOKEN}}"'`,
|
||||
// --replace-all is required, not cosmetic. `gh auth setup-git` (run
|
||||
// below, and by the no-token branch above) leaves the key
|
||||
// multi-valued: an empty entry that resets the helper chain plus
|
||||
// gh's own helper. A plain `git config --global <key> <value>`
|
||||
// refuses to overwrite a multi-valued key and exits 5, which aborts
|
||||
// the whole prepare script under `set -euo pipefail`. That makes the
|
||||
// no-token branch poison the with-token branch: on a host whose home
|
||||
// directory persists between runs (SSH, unlike an ephemeral Docker
|
||||
// container) exactly one launch succeeds and every later one fails.
|
||||
`git config --global --replace-all credential.https://github.com.helper '!/bin/sh -c "echo username=x-access-token; echo password=${GH_TOKEN:-${GITHUB_TOKEN}}"'`,
|
||||
"# Configure gh CLI to use HTTPS protocol",
|
||||
"gh config set git_protocol https --host github.com 2>/dev/null || true",
|
||||
"# Register gh as git credential helper (backup method)",
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package scriptengine
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -257,4 +259,91 @@ func TestGitHubAuthProvider(t *testing.T) {
|
||||
t.Error("expected gh auth setup-git backup")
|
||||
}
|
||||
})
|
||||
|
||||
// `gh auth setup-git` — emitted by the no-token branch above and by the
|
||||
// with-token branch itself — leaves credential.https://github.com.helper
|
||||
// multi-valued. A plain `git config --global <key> <value>` refuses to
|
||||
// overwrite a multi-valued key and exits 5, aborting the prepare script
|
||||
// under `set -euo pipefail`.
|
||||
t.Run("credential helper write uses --replace-all", func(t *testing.T) {
|
||||
provider := GitHubAuthProvider(map[string]string{"GH_TOKEN": "ghp_test"})
|
||||
vars := provider()
|
||||
setup := vars["github.auth_setup"]
|
||||
if !strings.Contains(setup, "git config --global --replace-all credential.https://github.com.helper") {
|
||||
t.Errorf("credential helper must be written with --replace-all, got %q", setup)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestGitHubAuthCredentialWriteIsIdempotent is a behavioural regression test: it
|
||||
// runs the complete auth setup against a real git binary and a real (temporary)
|
||||
// global config twice. The first setup leaves the key multi-valued, so the
|
||||
// second setup proves that the write handles the persistent state. Asserting
|
||||
// the flag string alone would not catch a future rewrite that drops back to a
|
||||
// single-value write by another spelling — git's exit 5 is the contract.
|
||||
func TestGitHubAuthCredentialWriteIsIdempotent(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("git not available")
|
||||
}
|
||||
if _, err := exec.LookPath("sh"); err != nil {
|
||||
t.Skip("sh not available")
|
||||
}
|
||||
|
||||
home := t.TempDir()
|
||||
bin := t.TempDir()
|
||||
ghPath := filepath.Join(bin, "gh")
|
||||
ghScript := `#!/bin/sh
|
||||
case "$1 $2" in
|
||||
"config set")
|
||||
exit 0
|
||||
;;
|
||||
"auth setup-git")
|
||||
git config --global --add credential.https://github.com.helper ""
|
||||
git config --global --add credential.https://github.com.helper "!/fake/gh auth git-credential"
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
exit 0
|
||||
`
|
||||
if err := os.WriteFile(ghPath, []byte(ghScript), 0o755); err != nil {
|
||||
t.Fatalf("write fake gh: %v", err)
|
||||
}
|
||||
|
||||
env := []string{
|
||||
"HOME=" + home,
|
||||
"XDG_CONFIG_HOME=" + home,
|
||||
"GH_TOKEN=ghp_test",
|
||||
"PATH=" + bin + string(os.PathListSeparator) + os.Getenv("PATH"),
|
||||
}
|
||||
runGit := func(args ...string) (string, error) {
|
||||
cmd := exec.Command("git", args...)
|
||||
cmd.Env = env
|
||||
out, err := cmd.CombinedOutput()
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
setup := GitHubAuthProvider(map[string]string{"GH_TOKEN": "ghp_test"})()["github.auth_setup"]
|
||||
|
||||
// Run the complete setup twice. The fake gh command models the extra values
|
||||
// that `gh auth setup-git` adds after the token helper write.
|
||||
for i := 1; i <= 2; i++ {
|
||||
cmd := exec.Command("sh", "-c", setup)
|
||||
cmd.Env = env
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("run %d: full auth setup failed: %v: %s", i, err, out)
|
||||
}
|
||||
}
|
||||
|
||||
out, err := runGit("config", "--global", "--get-all", "credential.https://github.com.helper")
|
||||
if err != nil {
|
||||
t.Fatalf("reading back config failed: %v: %s", err, out)
|
||||
}
|
||||
values := strings.Split(strings.TrimRight(out, "\r\n"), "\n")
|
||||
if len(values) != 3 {
|
||||
t.Fatalf("expected the full setup to leave three helper values, got %d: %q", len(values), values)
|
||||
}
|
||||
if !strings.Contains(values[0], "x-access-token") ||
|
||||
values[1] != "" || values[2] != "!/fake/gh auth git-credential" {
|
||||
t.Errorf("unexpected helper values after full setup: %q", values)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user