fix(agentworker,daemon): reconcile worktree ownership before push
Agent containers run as root (no USER in the Dockerfile, no userns-remap), so a commit the agent writes to the bind-mounted worktree lands owned by UID 0 on the host. The daemon then reads those same loose objects to push, running as its own non-root host user, and fails with "openat objects/../..: permission denied". containerEnv() now passes the daemon's own UID/GID as WORKTREE_UID/WORKTREE_GID. hybridTrees.Push -- the single point where a commit's objects are handed back for the actual push -- chowns the worktree to that owner first, via os.Lchown so a legitimately-tracked-but-dangling symlink doesn't abort a push that would otherwise have succeeded. Closes archie-core-1786749700606-6-bf6aa32d.
This commit is contained in:
1 parent
990a7c499c
commit
5da1d5d006
4 files changed
+238
-6
No files matched your search
@@ -3,7 +3,11 @@ package agentworker
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
@@ -34,6 +38,14 @@ type hybridTrees struct {
|
||||
owner, repo string
|
||||
issue int
|
||||
localDir string
|
||||
// worktreeUID and worktreeGID are the daemon's own host UID/GID
|
||||
// (WORKTREE_UID/WORKTREE_GID, set by containerEnv), or -1 when unset.
|
||||
// The agent runs as root inside the container, so a commit it writes
|
||||
// to the bind-mounted worktree lands owned by UID 0 on the host; Push
|
||||
// reconciles ownership back to the daemon's UID before handing off,
|
||||
// since the daemon -- running as its own non-root host user -- is the
|
||||
// one that reads those objects to actually push (archie-core#520).
|
||||
worktreeUID, worktreeGID int
|
||||
}
|
||||
|
||||
func (h *hybridTrees) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (dir, branch string, err error) {
|
||||
@@ -49,6 +61,11 @@ func (h *hybridTrees) CommitAll(ctx context.Context, dir, message string) (bool,
|
||||
}
|
||||
|
||||
func (h *hybridTrees) Push(ctx context.Context, dir, branch string) error {
|
||||
if h.worktreeUID >= 0 && h.worktreeGID >= 0 {
|
||||
if err := chownTree(dir, h.worktreeUID, h.worktreeGID); err != nil {
|
||||
return fmt.Errorf("reconcile worktree ownership before push: %w", err)
|
||||
}
|
||||
}
|
||||
return h.push.Push(ctx, h.owner, h.repo, h.issue, branch)
|
||||
}
|
||||
|
||||
@@ -66,6 +83,39 @@ func (h *hybridTrees) ChangedLines(ctx context.Context, dir, base string) (int,
|
||||
|
||||
var _ workflow.Trees = (*hybridTrees)(nil)
|
||||
|
||||
// chownTree recursively chowns dir to uid:gid so the daemon -- running as
|
||||
// its own host user -- can read loose objects the agent committed as root
|
||||
// inside the container.
|
||||
//
|
||||
// Lchown, not Chown: a repo can legitimately track a symlink whose target
|
||||
// doesn't exist inside the container (a relative link outside the
|
||||
// checkout, or to a tool the image doesn't ship). Chown follows the link
|
||||
// and fails on a dangling target, which would abort a push that would
|
||||
// otherwise have succeeded -- worse than the permission error this is
|
||||
// fixing. Lchown changes the link itself and never touches the target.
|
||||
func chownTree(dir string, uid, gid int) error {
|
||||
return filepath.WalkDir(dir, func(path string, _ fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Lchown(path, uid, gid)
|
||||
})
|
||||
}
|
||||
|
||||
// worktreeOwnerID parses a WORKTREE_UID/WORKTREE_GID environment value,
|
||||
// returning -1 (unset/unconfigured) when it is empty or not a valid
|
||||
// non-negative integer.
|
||||
func worktreeOwnerID(env string) int {
|
||||
if env == "" {
|
||||
return -1
|
||||
}
|
||||
id, err := strconv.Atoi(env)
|
||||
if err != nil || id < 0 {
|
||||
return -1
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
type taskDependencies struct {
|
||||
forge workflow.Forger
|
||||
store store.WorkflowStore
|
||||
@@ -85,12 +135,14 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
|
||||
wf := workflow.Route(req.Task, registry)
|
||||
|
||||
trees := &hybridTrees{
|
||||
push: dependencies.trees,
|
||||
local: &worktree.Manager{WorkDir: workDir},
|
||||
owner: req.Task.Owner,
|
||||
repo: req.Task.Repo,
|
||||
issue: req.Task.IssueNumber,
|
||||
localDir: workDir,
|
||||
push: dependencies.trees,
|
||||
local: &worktree.Manager{WorkDir: workDir},
|
||||
owner: req.Task.Owner,
|
||||
repo: req.Task.Repo,
|
||||
issue: req.Task.IssueNumber,
|
||||
localDir: workDir,
|
||||
worktreeUID: worktreeOwnerID(os.Getenv("WORKTREE_UID")),
|
||||
worktreeGID: worktreeOwnerID(os.Getenv("WORKTREE_GID")),
|
||||
}
|
||||
|
||||
// Start MCP providers and build a local tool registry.
|
||||
|
||||
@@ -28,6 +28,152 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/worktreerpc"
|
||||
)
|
||||
|
||||
// ── regression: archie-core#520 -- worktree ownership must be
|
||||
// reconciled before push, since the agent commits as root inside the
|
||||
// container and the daemon reads those same loose objects as its own
|
||||
// non-root host user ──────────────────────────────────────────────────
|
||||
|
||||
type fakePushTrees struct {
|
||||
pushed bool
|
||||
pushErr error
|
||||
}
|
||||
|
||||
func (f *fakePushTrees) Prepare(context.Context, string, string, string, int, string, string, string) (string, string, error) {
|
||||
panic("unexpected call")
|
||||
}
|
||||
|
||||
func (f *fakePushTrees) Push(context.Context, string, string, int, string) error {
|
||||
f.pushed = true
|
||||
return f.pushErr
|
||||
}
|
||||
|
||||
func TestWorktreeOwnerIDParsesEnvValue(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
env string
|
||||
want int
|
||||
}{
|
||||
{"unset", "", -1},
|
||||
{"zero is a valid uid (root)", "0", 0},
|
||||
{"typical non-root uid", "1000", 1000},
|
||||
{"garbage falls back to unset", "not-a-number", -1},
|
||||
{"negative falls back to unset", "-1", -1},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
if got := worktreeOwnerID(c.env); got != c.want {
|
||||
t.Errorf("worktreeOwnerID(%q) = %d, want %d", c.env, got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPushReconcilesOwnershipWhenConfigured chowns to the test
|
||||
// process's own uid/gid -- a permission no-op that succeeds without root --
|
||||
// so this proves the walk runs and still reaches the underlying push,
|
||||
// without needing privilege to chown to an arbitrary owner.
|
||||
func TestHybridTreesPushReconcilesOwnershipWhenConfigured(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "file.txt"), []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(dir, "sub"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "sub", "nested.txt"), []byte("y"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fake := &fakePushTrees{}
|
||||
trees := &hybridTrees{
|
||||
push: fake,
|
||||
localDir: dir,
|
||||
worktreeUID: os.Getuid(),
|
||||
worktreeGID: os.Getgid(),
|
||||
}
|
||||
|
||||
if err := trees.Push(t.Context(), dir, "branch"); err != nil {
|
||||
t.Fatalf("Push: %v", err)
|
||||
}
|
||||
if !fake.pushed {
|
||||
t.Fatal("Push did not reach the underlying remote push")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPushReconcilesOwnershipWithDanglingSymlink: a repo can
|
||||
// legitimately track a symlink whose target doesn't exist inside the
|
||||
// container (e.g. a relative link to something outside the checkout, or to
|
||||
// a tool the image doesn't ship). Chowning must not follow it -- a
|
||||
// following chown fails on a dangling target, which would abort a push
|
||||
// that would otherwise have succeeded, a regression worse than the bug
|
||||
// being fixed here.
|
||||
func TestHybridTreesPushReconcilesOwnershipWithDanglingSymlink(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.Symlink(filepath.Join(dir, "does-not-exist"), filepath.Join(dir, "broken-link")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fake := &fakePushTrees{}
|
||||
trees := &hybridTrees{
|
||||
push: fake,
|
||||
localDir: dir,
|
||||
worktreeUID: os.Getuid(),
|
||||
worktreeGID: os.Getgid(),
|
||||
}
|
||||
|
||||
if err := trees.Push(t.Context(), dir, "branch"); err != nil {
|
||||
t.Fatalf("Push: %v", err)
|
||||
}
|
||||
if !fake.pushed {
|
||||
t.Fatal("Push did not reach the underlying remote push")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPushSkipsReconciliationWhenNotConfigured: an unset
|
||||
// WORKTREE_UID/GID (archie-core's non-container and test callers) must not
|
||||
// attempt any chown at all -- a nonexistent dir would otherwise fail the
|
||||
// walk, so this proves the walk is skipped entirely, not just tolerant of
|
||||
// this one input.
|
||||
func TestHybridTreesPushSkipsReconciliationWhenNotConfigured(t *testing.T) {
|
||||
fake := &fakePushTrees{}
|
||||
trees := &hybridTrees{
|
||||
push: fake,
|
||||
localDir: filepath.Join(t.TempDir(), "does-not-exist"),
|
||||
worktreeUID: -1,
|
||||
worktreeGID: -1,
|
||||
}
|
||||
|
||||
if err := trees.Push(t.Context(), trees.localDir, "branch"); err != nil {
|
||||
t.Fatalf("Push: %v", err)
|
||||
}
|
||||
if !fake.pushed {
|
||||
t.Fatal("Push did not reach the underlying remote push")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPushSurfacesOwnershipReconciliationFailure: a configured
|
||||
// but broken reconciliation (the dir push is asked to fix vanished) must
|
||||
// fail loudly rather than forward to a push that will only fail later with
|
||||
// a much more confusing git-internal permission error.
|
||||
func TestHybridTreesPushSurfacesOwnershipReconciliationFailure(t *testing.T) {
|
||||
fake := &fakePushTrees{}
|
||||
missing := filepath.Join(t.TempDir(), "does-not-exist")
|
||||
trees := &hybridTrees{
|
||||
push: fake,
|
||||
localDir: missing,
|
||||
worktreeUID: os.Getuid(),
|
||||
worktreeGID: os.Getgid(),
|
||||
}
|
||||
|
||||
err := trees.Push(t.Context(), missing, "branch")
|
||||
if err == nil {
|
||||
t.Fatal("expected an error reconciling ownership of a missing directory")
|
||||
}
|
||||
if fake.pushed {
|
||||
t.Fatal("Push must not reach the underlying remote push when reconciliation fails")
|
||||
}
|
||||
}
|
||||
|
||||
type panicRunner struct{ t *testing.T }
|
||||
|
||||
func (r panicRunner) Run(context.Context, string, agentexec.Request, agentexec.ToolCallReporter) (agentexec.Result, error) {
|
||||
|
||||
@@ -1120,6 +1120,15 @@ func (d *Daemon) containerEnv(task *store.Task) []string {
|
||||
env = append(env, "NATS_TOKEN="+token)
|
||||
}
|
||||
}
|
||||
// The agent process runs as root inside the container (no USER in the
|
||||
// Dockerfile, no userns-remap), so a commit it writes to the bind-mounted
|
||||
// worktree lands owned by UID 0 on the host. The daemon then reads those
|
||||
// same loose objects to push, running as its own non-root host user --
|
||||
// archie-core#520's "permission denied" on .git/objects. Passing the
|
||||
// daemon's own UID/GID lets the agent chown the worktree back to it
|
||||
// before pushing.
|
||||
env = append(env, fmt.Sprintf("WORKTREE_UID=%d", os.Getuid()))
|
||||
env = append(env, fmt.Sprintf("WORKTREE_GID=%d", os.Getgid()))
|
||||
for _, p := range d.configFor(task).Providers {
|
||||
if p.APIKeyEnv != "" {
|
||||
if v := os.Getenv(p.APIKeyEnv); v != "" {
|
||||
|
||||
@@ -3,6 +3,7 @@ package daemon
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"slices"
|
||||
@@ -330,6 +331,30 @@ func TestContainerEnvIncludesConfiguredNATSCredentials(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestContainerEnvIncludesWorktreeOwnership: the agent process inside a
|
||||
// container runs as root (no USER in the Dockerfile, no userns-remap), so
|
||||
// commits it writes to the bind-mounted worktree land owned by UID 0 on the
|
||||
// host. The daemon then reads those same loose objects to push, running as
|
||||
// its own non-root host user -- a UID mismatch that surfaces as
|
||||
// "openat objects/../..: permission denied" (archie-core#520). Passing the
|
||||
// daemon's own UID/GID lets the agent chown the worktree back before
|
||||
// pushing.
|
||||
func TestContainerEnvIncludesWorktreeOwnership(t *testing.T) {
|
||||
d := &Daemon{
|
||||
Cfg: config.NewHolder(config.Config{}),
|
||||
}
|
||||
|
||||
got := d.containerEnv(nil)
|
||||
for _, want := range []string{
|
||||
fmt.Sprintf("WORKTREE_UID=%d", os.Getuid()),
|
||||
fmt.Sprintf("WORKTREE_GID=%d", os.Getgid()),
|
||||
} {
|
||||
if !slices.Contains(got, want) {
|
||||
t.Errorf("containerEnv() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerEnvUsesOnlyOwningIdentityProviderCredential(t *testing.T) {
|
||||
t.Setenv("ROOT_PROVIDER_KEY", "root-secret")
|
||||
t.Setenv("WORKER_PROVIDER_KEY", "worker-secret")
|
||||
|
||||
Reference in new issue
Block a user