fix(agentgh): require https for creds, pin gh to github.com, map signal exits
This commit is contained in:
@@ -200,8 +200,8 @@ KV path and API URL; each app caches its own installation token. The app JWT is
|
||||
RS256 via `crypto/rsa` (iat now-60s, exp now+9m, iss app_id) — no JWT
|
||||
dependency. `whoami` reads the slug from `GET /app` (JWT) and the bot user id
|
||||
from `GET /users/<slug>[bot]` (installation token). `agentgh gh` runs `gh` via
|
||||
`os/exec` with `GH_TOKEN` added to (and `GH_TOKEN`/`GITHUB_TOKEN` stripped from)
|
||||
the child env, and propagates its exit code; `DisableFlagParsing` plus
|
||||
`os/exec` with `GH_TOKEN` and `GH_HOST=github.com` set in the child env (other
|
||||
GitHub token/host vars stripped), and propagates its exit code (128+signal); `DisableFlagParsing` plus
|
||||
`TraverseChildren` lets `--app` precede `gh` while gh's flags pass untouched.
|
||||
|
||||
## Gotchas
|
||||
|
||||
@@ -11,7 +11,7 @@ ARCH ?= $(shell go env GOARCH)
|
||||
# shell for-loop over $(BINARIES).
|
||||
pkgpath = ./cmd/$$b
|
||||
|
||||
.PHONY: all build test lint pre-commit fmt clean install completions rpm rpm-package patch minor major _tag
|
||||
.PHONY: all build test lint fmt clean install completions rpm rpm-package patch minor major _tag
|
||||
|
||||
all: build
|
||||
|
||||
@@ -30,9 +30,6 @@ test:
|
||||
lint:
|
||||
golangci-lint run ./...
|
||||
|
||||
pre-commit:
|
||||
pre-commit run --all-files
|
||||
|
||||
fmt:
|
||||
gofmt -w .
|
||||
|
||||
|
||||
@@ -455,7 +455,7 @@ git config credential.https://github.com.helper '!agentgh credential'
|
||||
|
||||
`--app` (`coder`|`reviewer`, env `AGENTGH_APP`, default `coder`) selects the app
|
||||
for every subcommand; `AGENTGH_KV_PATH` overrides the KV path outright.
|
||||
`credential get` answers only for host `github.com` (`username=x-access-token`);
|
||||
`credential get` answers only for `https://github.com` (`username=x-access-token`);
|
||||
`store`/`erase` are no-ops. `agentgh gh` exits with gh's exit code. Errors name
|
||||
the failing stage: AppRole login, KV read denied, private key parse, JWT sign or
|
||||
token mint.
|
||||
|
||||
+12
-3
@@ -17,6 +17,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"git.unkin.net/unkin/agent-tools/internal/agent"
|
||||
|
||||
@@ -126,7 +127,7 @@ func credentialGet(stdin io.Reader, stdout io.Writer, load loader) error {
|
||||
if err := sc.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if req["host"] != githubHost {
|
||||
if req["protocol"] != "https" || req["host"] != githubHost {
|
||||
return nil
|
||||
}
|
||||
tok, err := token(load)
|
||||
@@ -172,6 +173,11 @@ func newGHCmd(load loader) *cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
var strippedEnv = map[string]bool{
|
||||
"GH_TOKEN": true, "GITHUB_TOKEN": true, "GH_HOST": true,
|
||||
"GH_ENTERPRISE_TOKEN": true, "GITHUB_ENTERPRISE_TOKEN": true,
|
||||
}
|
||||
|
||||
// runGH passes the token through the child env only, never argv.
|
||||
func runGH(args []string, tok string, stdin io.Reader, stdout, stderr io.Writer) error {
|
||||
bin, err := exec.LookPath("gh")
|
||||
@@ -181,14 +187,17 @@ func runGH(args []string, tok string, stdin io.Reader, stdout, stderr io.Writer)
|
||||
c := exec.Command(bin, args...)
|
||||
c.Stdin, c.Stdout, c.Stderr = stdin, stdout, stderr
|
||||
for _, kv := range os.Environ() {
|
||||
if !strings.HasPrefix(kv, "GH_TOKEN=") && !strings.HasPrefix(kv, "GITHUB_TOKEN=") {
|
||||
if k, _, _ := strings.Cut(kv, "="); !strippedEnv[k] {
|
||||
c.Env = append(c.Env, kv)
|
||||
}
|
||||
}
|
||||
c.Env = append(c.Env, "GH_TOKEN="+tok)
|
||||
c.Env = append(c.Env, "GH_TOKEN="+tok, "GH_HOST="+githubHost)
|
||||
err = c.Run()
|
||||
var ee *exec.ExitError
|
||||
if errors.As(err, &ee) {
|
||||
if ws, ok := ee.Sys().(syscall.WaitStatus); ok && ws.Signaled() {
|
||||
return exitCode(128 + int(ws.Signal()))
|
||||
}
|
||||
return exitCode(ee.ExitCode())
|
||||
}
|
||||
return err
|
||||
|
||||
@@ -29,6 +29,8 @@ func TestCredentialGetHostFiltering(t *testing.T) {
|
||||
"protocol=https\nhost=github.com.evil.example\n\n",
|
||||
"protocol=https\nhost=api.github.com\n\n",
|
||||
"protocol=https\n\n",
|
||||
"protocol=http\nhost=github.com\n\n",
|
||||
"host=github.com\n\n",
|
||||
} {
|
||||
var out bytes.Buffer
|
||||
if err := credentialGet(strings.NewReader(in), &out, load); err != nil || out.Len() != 0 {
|
||||
@@ -133,16 +135,18 @@ func fakeGH(t *testing.T, script string) string {
|
||||
}
|
||||
|
||||
func TestRunGHPassesTokenInEnvOnly(t *testing.T) {
|
||||
dir := fakeGH(t, `printf '%s\n' "$@" > "$(dirname "$0")/argv"; echo "GH_TOKEN=$GH_TOKEN GITHUB_TOKEN=$GITHUB_TOKEN"; cat; exit 7`)
|
||||
t.Setenv("GH_TOKEN", "stale")
|
||||
t.Setenv("GITHUB_TOKEN", "stale")
|
||||
dir := fakeGH(t, `printf '%s\n' "$@" > "$(dirname "$0")/argv"; echo "GH_TOKEN=$GH_TOKEN GITHUB_TOKEN=$GITHUB_TOKEN GH_HOST=$GH_HOST GH_ENTERPRISE_TOKEN=$GH_ENTERPRISE_TOKEN GITHUB_ENTERPRISE_TOKEN=$GITHUB_ENTERPRISE_TOKEN"; cat; exit 7`)
|
||||
for _, k := range []string{"GH_TOKEN", "GITHUB_TOKEN", "GH_ENTERPRISE_TOKEN", "GITHUB_ENTERPRISE_TOKEN"} {
|
||||
t.Setenv(k, "stale")
|
||||
}
|
||||
t.Setenv("GH_HOST", "ghe.example")
|
||||
var out bytes.Buffer
|
||||
err := runGH([]string{"pr", "list", "--repo", "o/r"}, "ghs_secret", strings.NewReader("piped\n"), &out, &out)
|
||||
var code exitCode
|
||||
if !errors.As(err, &code) || code != 7 {
|
||||
t.Errorf("err = %v, want exit code 7", err)
|
||||
}
|
||||
if out.String() != "GH_TOKEN=ghs_secret GITHUB_TOKEN=\npiped\n" {
|
||||
if out.String() != "GH_TOKEN=ghs_secret GITHUB_TOKEN= GH_HOST=github.com GH_ENTERPRISE_TOKEN= GITHUB_ENTERPRISE_TOKEN=\npiped\n" {
|
||||
t.Errorf("child saw %q", out.String())
|
||||
}
|
||||
argv, _ := os.ReadFile(filepath.Join(dir, "argv"))
|
||||
@@ -159,3 +163,12 @@ func TestGHCommandPassesFlagsThroughWithApp(t *testing.T) {
|
||||
t.Errorf("out=%q err=%v", out, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunGHSignalExitCode(t *testing.T) {
|
||||
fakeGH(t, `kill -TERM $$`)
|
||||
err := runGH(nil, "tok", strings.NewReader(""), &bytes.Buffer{}, &bytes.Buffer{})
|
||||
var code exitCode
|
||||
if !errors.As(err, &code) || code != 128+15 {
|
||||
t.Errorf("err = %v, want exit code 143", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user