diff --git a/go.mod b/go.mod index 3bb5313d0..7d7f1652c 100644 --- a/go.mod +++ b/go.mod @@ -95,7 +95,7 @@ require ( github.com/BurntSushi/toml v1.6.0 github.com/cedar-policy/cedar-go v1.8.0 github.com/coreos/go-oidc/v3 v3.21.0 - github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.6 + github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.8 github.com/go-git/go-billy/v6 v6.0.0-beta.1 github.com/go-git/go-git/v6 v6.0.0-beta.1 github.com/go-telegram/bot v1.27.0 diff --git a/go.sum b/go.sum index ad270a501..b00d83896 100644 --- a/go.sum +++ b/go.sum @@ -54,8 +54,8 @@ github.com/docker/go-connections v0.8.2 h1:9d9DEokx5W7qlr0shkFXIxEA7+TTD+llwzcyk github.com/docker/go-connections v0.8.2/go.mod h1:no1qkHdjq7kLMGUXYAduOhYPSJxxvgWBh7ogVvptn3Q= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.6 h1:co7vn6GgImYEGO/KPlhPRJT9i5EPWgOKcB9q7bNJlFQ= -github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.6/go.mod h1:MrWO4PN3/VYC5xF8eyI8U82Nv/agTHcN4IUzheqA90c= +github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.8 h1:ay2XEj0yW3uzsNwN3KXS/c4nlneHONFKaHlrVhu2WCQ= +github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.8/go.mod h1:4G6VgFAp9eaQkwy7DbZbOfWRD/Mjvc/TnEtQBRRMEsA= github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= github.com/ebitengine/purego v0.11.1 h1:2zpWRSQNVKN4eKsKO9eM1ILDgWfYMY9GwqRmK6XeQ/0= diff --git a/internal/container/kit.go b/internal/container/kit.go index 83c7e2736..507700118 100644 --- a/internal/container/kit.go +++ b/internal/container/kit.go @@ -9,6 +9,7 @@ import ( "path" "strings" + "github.com/docker/sandbox-kit-spec/v3/spec" "github.com/moby/moby/api/pkg/stdcopy" "github.com/moby/moby/api/types/container" "github.com/moby/moby/api/types/mount" @@ -81,10 +82,8 @@ func StartKit(ctx context.Context, cli *client.Client, s KitSpec) (string, error if _, err := cli.ContainerStart(ctx, created.ID, client.ContainerStartOptions{}); err != nil { return fail(fmt.Errorf("start kit container: %w", err)) } - for i, h := range s.Launch.Install { - if err := runHook(ctx, cli, created.ID, h); err != nil { - return fail(fmt.Errorf("install hook %d: %w", i+1, err)) - } + if err := installKit(ctx, cli, created.ID, s.Launch); err != nil { + return fail(err) } if s.InstallDone != nil { s.InstallDone() @@ -95,10 +94,15 @@ func StartKit(ctx context.Context, cli *client.Client, s KitSpec) (string, error } } for _, f := range s.Launch.Context { - if err := writeKitFile(ctx, cli, created.ID, "0", f.Path, f.Content, f.Mode, nil); err != nil { + if err := writeKitContext(ctx, cli, created.ID, f); err != nil { return fail(fmt.Errorf("write agent context %s: %w", f.Path, err)) } } + for _, skill := range s.Launch.Skills { + if err := stageKitSkill(ctx, cli, created.ID, s.Launch.Harness.User, skill); err != nil { + return fail(err) + } + } for i, h := range s.Launch.Startup { if err := runHook(ctx, cli, created.ID, h); err != nil { return fail(fmt.Errorf("startup hook %d: %w", i+1, err)) @@ -107,6 +111,82 @@ func StartKit(ctx context.Context, cli *client.Client, s KitSpec) (string, error return created.ID, nil } +func installKit(ctx context.Context, cli *client.Client, id string, launch kit.Launch) error { + for _, f := range launch.InstallFiles { + if err := writeKitFile(ctx, cli, id, launch.Harness.User, f.Path, f.Content, f.Mode, f.Overwrite); err != nil { + return fmt.Errorf("write install credential file %s: %w", f.Path, err) + } + } + for i, h := range launch.Install { + if err := runHook(ctx, cli, id, h); err != nil { + return fmt.Errorf("install hook %d: %w", i+1, err) + } + } + for _, f := range launch.InstallFiles { + code, out, err := ExecIn(ctx, cli, id, launch.Harness.User, []string{"KIT_PATH=" + f.Path}, []string{"sh", "-c", `rm -f -- "$KIT_PATH"`}) + if err != nil { + return fmt.Errorf("remove install credential file %s: %w", f.Path, err) + } + if code != 0 { + return fmt.Errorf("remove install credential file %s: exit %d: %s", f.Path, code, out) + } + } + return nil +} + +func stageKitSkill(ctx context.Context, cli *client.Client, id, user string, skill kit.Skill) error { + code, out, err := ExecIn(ctx, cli, id, "0", []string{"SKILL_SOURCE=" + skill.Source, "SKILL_TARGET=" + skill.Target, "SKILL_USER=" + user}, []string{"sh", "-ec", ` +[ -d "$SKILL_SOURCE" ] && [ -f "$SKILL_SOURCE/SKILL.md" ] || exit 1 +[ "$SKILL_SOURCE" != "$SKILL_TARGET" ] || exit 0 +at="$SKILL_TARGET" +while [ "$at" != / ]; do + [ ! -L "$at" ] || exit 1 + at=$(dirname "$at") +done +[ ! -e "$SKILL_TARGET" ] || exit 0 +mkdir -p "$(dirname "$SKILL_TARGET")" +cp -R "$SKILL_SOURCE" "$SKILL_TARGET" +chown -R "$SKILL_USER" "$SKILL_TARGET" +`}) + if err != nil { + return fmt.Errorf("stage bundled skill %s: %w", skill.Source, err) + } + if code != 0 { + return fmt.Errorf("stage bundled skill %s: exit %d: %s", skill.Source, code, out) + } + return nil +} + +// Replace only Archie's managed guidance, retaining the image's instructions. +func writeKitContext(ctx context.Context, cli *client.Client, id string, f spec.File) error { + code, out, err := ExecIn(ctx, cli, id, "0", []string{"KIT_PATH=" + f.Path, "KIT_CONTENT=" + base64.StdEncoding.EncodeToString([]byte(f.Content))}, []string{"sh", "-ec", ` +at="$KIT_PATH" +while [ "$at" != / ]; do + [ ! -L "$at" ] || exit 1 + at=$(dirname "$at") +done +[ ! -e "$KIT_PATH" ] || [ -f "$KIT_PATH" ] || exit 1 +mkdir -p "$(dirname "$KIT_PATH")" +tmp=$(mktemp "$KIT_PATH.XXXXXX") +trap 'rm -f "$tmp"' 0 +if [ -f "$KIT_PATH" ]; then + awk '/^$/ { managed=1; next } /^$/ { managed=0; next } !managed { print }' "$KIT_PATH" > "$tmp" +fi +printf '%s\n' '' >> "$tmp" +printf %s "$KIT_CONTENT" | base64 -d >> "$tmp" +printf '\n%s\n' '' >> "$tmp" +chmod 0644 "$tmp" +mv "$tmp" "$KIT_PATH" +`}) + if err != nil { + return err + } + if code != 0 { + return fmt.Errorf("exited %d: %s", code, out) + } + return nil +} + // RemoveKitVolumes removes an execution's Kit volumes once the execution // has ended; a retry of the same execution reuses them until then. func RemoveKitVolumes(ctx context.Context, cli *client.Client, volumes []kit.Volume) error { diff --git a/internal/container/kit_test.go b/internal/container/kit_test.go new file mode 100644 index 000000000..1f2fe9115 --- /dev/null +++ b/internal/container/kit_test.go @@ -0,0 +1,82 @@ +package container + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/docker/sandbox-kit-spec/v3/spec" + "github.com/moby/moby/client" + + "github.com/samcharles93/archie-core/internal/domain/agentrun" + "github.com/samcharles93/archie-core/internal/infrastructure/kit" +) + +func TestKitRuntimeDiscovery(t *testing.T) { + if testing.Short() { + t.Skip("requires Docker") + } + cli, err := client.New(client.FromEnv) + if err != nil { + t.Fatal(err) + } + defer cli.Close() + shared := t.TempDir() + if err := os.Chmod(shared, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(shared, "SKILL.md"), []byte("shared body"), 0o644); err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name string + symlink bool + }{ + {name: "preserves and replaces managed guidance"}, + {name: "refuses symlink guidance", symlink: true}, + } { + t.Run(tc.name, func(t *testing.T) { + script := `test -f /tmp/install.json; mkdir -p /tmp/discovery /tmp/bundle; printf 'SKILL body' > /tmp/bundle/SKILL.md; printf 'image instructions\n' > /tmp/discovery/AGENTS.md` + if tc.symlink { + script += `; mv /tmp/discovery/AGENTS.md /tmp/other; ln -s /tmp/other /tmp/discovery/AGENTS.md` + } + launch := kit.Launch{ + Harness: agentrun.HarnessSpec{User: "0"}, Install: []kit.Hook{{User: "0", Argv: []string{"sh", "-ec", script}}}, InstallFiles: []spec.File{{Path: "/tmp/install.json", Content: "install sentinel"}}, + Files: []spec.File{{Path: "/tmp/runtime.json", Content: "runtime sentinel"}}, + Startup: []kit.Hook{{User: "0", Argv: []string{"sh", "-ec", "test ! -e /tmp/install.json; test -f /tmp/runtime.json"}}}, + Context: []spec.File{{Path: "/tmp/discovery/AGENTS.md", Content: "first guidance"}}, Skills: []kit.Skill{{Source: "/tmp/bundle", Target: "/tmp/skills/bundle"}}, + } + id, err := StartKit(t.Context(), cli, KitSpec{Image: "alpine:3", Worker: []string{"tail", "-f", "/dev/null"}, Binds: []string{shared + ":/tmp/skills/shared:ro,z"}, Launch: launch}) + if tc.symlink { + if err == nil { + _, _ = cli.ContainerRemove(context.Background(), id, client.ContainerRemoveOptions{Force: true}) + t.Fatal("symlink was followed") + } + return + } + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + _, _ = cli.ContainerRemove(context.Background(), id, client.ContainerRemoveOptions{Force: true}) + }) + if err := writeKitContext(t.Context(), cli, id, spec.File{Path: "/tmp/discovery/AGENTS.md", Content: "replacement guidance"}); err != nil { + t.Fatal(err) + } + code, out, err := ExecIn(t.Context(), cli, id, "0", nil, []string{"sh", "-ec", "cat /tmp/discovery/AGENTS.md; cat /tmp/skills/bundle/SKILL.md; cat /tmp/skills/shared/SKILL.md; if echo overwrite > /tmp/skills/shared/SKILL.md 2>/dev/null; then exit 1; fi"}) + if err != nil || code != 0 { + t.Fatalf("read discovery: %d %q %v", code, out, err) + } + for _, want := range []string{"image instructions", "replacement guidance", "SKILL body", "shared body"} { + if !strings.Contains(out, want) { + t.Errorf("missing %q in %q", want, out) + } + } + if strings.Contains(out, "first guidance") || strings.Count(out, "") != 1 { + t.Fatalf("managed guidance was not replaced: %q", out) + } + }) + } +} diff --git a/internal/infrastructure/egress/credential_phases_test.go b/internal/infrastructure/egress/credential_phases_test.go new file mode 100644 index 000000000..ddceb0226 --- /dev/null +++ b/internal/infrastructure/egress/credential_phases_test.go @@ -0,0 +1,74 @@ +package egress + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/docker/sandbox-kit-spec/v3/spec" + + "github.com/samcharles93/archie-core/internal/domain/harnesssecret" +) + +type phaseOAuthStore struct{} + +func (phaseOAuthStore) GetHarnessSecret(context.Context, string, string) (harnesssecret.Secret, error) { + return harnesssecret.Secret{AccessToken: "real-oauth"}, nil +} +func (phaseOAuthStore) PutHarnessSecret(context.Context, harnesssecret.Secret) error { return nil } + +func TestCredentialPhaseEnforcement(t *testing.T) { + t.Parallel() + cases := []struct { + name, phase string + install, runtime bool + revoked bool + }{ + {"install scalar", `"install"`, true, false, false}, + {"runtime scalar", `"runtime"`, false, true, false}, + {"both", `["install","runtime"]`, true, true, false}, + {"revoked", `["install","runtime"]`, false, false, true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var phases spec.Phases + if err := json.Unmarshal([]byte(tc.phase), &phases); err != nil { + t.Fatal(err) + } + creds := []spec.CredentialCapability{ + {Service: "key", Phase: phases, APIKey: &spec.APIKey{Inject: []spec.Inject{{Domain: "api.example", Header: "X-Key"}}}}, + {Service: "oauth", Phase: phases, OAuth: &spec.OAuth{TokenEndpoint: &spec.TokenEndpoint{Host: "token.example", Path: "/token"}, ResourceHosts: []string{"api.example"}, Sentinels: &spec.Sentinels{AccessToken: "sentinel"}}}, + } + proxy := NewProxy(nil, ProxyOptions{Resolver: ResolverFunc(func(context.Context, string, string) (string, error) { + if tc.revoked { + return "", ErrUnbound + } + return "real-key", nil + }), OAuthStore: phaseOAuthStore{}}) + session := &Session{token: "run", org: "org", injections: compileInjections(creds, nil), oauth: compileOAuthRules(creds, nil)} + for _, runtime := range []bool{false, true} { + session.atRun.Store(runtime) + want := tc.install + if runtime { + want = tc.runtime + } + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "http://api.example/data", nil) + req.Header.Set("Authorization", "Bearer sentinel") + if err := proxy.inject(context.Background(), session, req, "api.example", 80); err != nil { + t.Fatal(err) + } + if err := proxy.injectOAuth(context.Background(), session, req, "api.example", 80); err != nil { + t.Fatal(err) + } + if got := req.Header.Get("X-Key") == "real-key"; got != want { + t.Errorf("runtime=%v API key injected=%v, want %v", runtime, got, want) + } + if got := req.Header.Get("Authorization") == "Bearer real-oauth"; got != want { + t.Errorf("runtime=%v OAuth injected=%v, want %v", runtime, got, want) + } + } + }) + } +} diff --git a/internal/infrastructure/egress/credentials.go b/internal/infrastructure/egress/credentials.go index 4fa83202a..6e8e8c95f 100644 --- a/internal/infrastructure/egress/credentials.go +++ b/internal/infrastructure/egress/credentials.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/http" + "slices" "strings" "github.com/docker/sandbox-kit-spec/v3/spec" @@ -50,7 +51,7 @@ func (f ResolverFunc) Resolve(ctx context.Context, credential, service string) ( type injection struct { service string required bool - runtime bool + phases spec.Phases domain pattern header string format string @@ -76,7 +77,7 @@ func compileInjections(creds []spec.CredentialCapability, bound map[string]Crede out = append(out, injection{ service: c.Service, required: c.Required, - runtime: c.Phase == "runtime", + phases: slices.Clone(c.Phase), domain: compilePattern(rule.Domain), header: rule.Header, format: format, @@ -92,9 +93,9 @@ func compileInjections(creds []spec.CredentialCapability, bound map[string]Crede // admitted. Only rules for this phase and this host apply, so a key is never // sent anywhere its Kit did not name. func (p *Proxy) inject(ctx context.Context, s *Session, r *http.Request, host string, port int) error { - atRuntime := s.atRun.Load() + phase := s.phase() for _, rule := range s.injections { - if rule.runtime != atRuntime || !rule.domain.matches(normalizeHost(host), port) { + if !slices.Contains(rule.phases, phase) || !rule.domain.matches(normalizeHost(host), port) { continue } if p.resolver == nil { diff --git a/internal/infrastructure/egress/oauth.go b/internal/infrastructure/egress/oauth.go index deafd9929..bd03eecef 100644 --- a/internal/infrastructure/egress/oauth.go +++ b/internal/infrastructure/egress/oauth.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "net/http" + "slices" "strconv" "strings" "time" @@ -36,7 +37,7 @@ type OAuthStore interface { type oauthRule struct { service string required bool - runtime bool + phases spec.Phases tokenHost pattern tokenPath string resourceHosts []pattern @@ -61,7 +62,7 @@ func compileOAuthRules(creds []spec.CredentialCapability, bound map[string]Crede continue } rule := oauthRule{ - service: c.Service, required: c.Required, runtime: c.Phase == "runtime", + service: c.Service, required: c.Required, phases: slices.Clone(c.Phase), tokenHost: compilePattern(c.OAuth.TokenEndpoint.Host), tokenPath: c.OAuth.TokenEndpoint.Path, } for _, h := range c.OAuth.ResourceHosts { @@ -79,14 +80,13 @@ func compileOAuthRules(creds []spec.CredentialCapability, bound map[string]Crede } // tokenEndpointRule returns the session's rule whose token endpoint r is a -// POST to, for the request's current phase. +// POST to. The caller refuses requests outside its granted phases. func tokenEndpointRule(s *Session, r *http.Request, host string, port int) (oauthRule, bool) { if r.Method != http.MethodPost { return oauthRule{}, false } - atRuntime := s.atRun.Load() for _, rule := range s.oauth { - if rule.runtime != atRuntime || !rule.tokenHost.matches(normalizeHost(host), port) { + if !rule.tokenHost.matches(normalizeHost(host), port) { continue } if rule.tokenPath != "" && r.URL.Path != rule.tokenPath { @@ -127,9 +127,9 @@ func (p *Proxy) granted(ctx context.Context, s *Session, rule oauthRule) (bool, // request to one of a granted rule's resource hosts. A request not carrying // the sentinel never touches the store. func (p *Proxy) injectOAuth(ctx context.Context, s *Session, r *http.Request, host string, port int) error { - atRuntime := s.atRun.Load() + phase := s.phase() for _, rule := range s.oauth { - if rule.runtime != atRuntime || rule.sentinels.AccessToken == "" || + if !slices.Contains(rule.phases, phase) || rule.sentinels.AccessToken == "" || !matchesAny(rule.resourceHosts, normalizeHost(host), port) || !headerCarries(r.Header, rule.sentinels.AccessToken) { continue } diff --git a/internal/infrastructure/egress/proxy.go b/internal/infrastructure/egress/proxy.go index 28a270fa1..db4a2d1c7 100644 --- a/internal/infrastructure/egress/proxy.go +++ b/internal/infrastructure/egress/proxy.go @@ -10,6 +10,7 @@ import ( "net" "net/http" "net/http/httputil" + "slices" "strconv" "strings" "sync" @@ -50,6 +51,13 @@ func (s *Session) Token() string { return s.token } // egress. It is one-way: install egress never reopens. func (s *Session) EnterRuntime() { s.atRun.Store(true) } +func (s *Session) phase() string { + if s.atRun.Load() { + return "runtime" + } + return "install" +} + func (s *Session) rules() rules { if s.atRun.Load() { return s.runtime @@ -253,6 +261,10 @@ func (p *Proxy) serveConnect(w http.ResponseWriter, r *http.Request, s *Session) func (p *Proxy) forward(ctx context.Context, w http.ResponseWriter, r *http.Request, s *Session, scheme, host string, port int) { target := net.JoinHostPort(host, strconv.Itoa(port)) if rule, ok := tokenEndpointRule(s, r, host, port); ok { + if !slices.Contains(rule.phases, s.phase()) { + http.Error(w, "credential "+rule.service+" is not available in this phase", http.StatusForbidden) + return + } granted, err := p.granted(ctx, s, rule) if err != nil { http.Error(w, "egress to "+target+": "+err.Error(), http.StatusBadGateway) diff --git a/internal/infrastructure/egress/proxy_test.go b/internal/infrastructure/egress/proxy_test.go index a9f042b20..aa725f0ac 100644 --- a/internal/infrastructure/egress/proxy_test.go +++ b/internal/infrastructure/egress/proxy_test.go @@ -9,6 +9,8 @@ import ( "sync/atomic" "testing" + "github.com/docker/sandbox-kit-spec/v3/spec" + "github.com/samcharles93/archie-core/internal/domain/harnesssecret" ) @@ -30,8 +32,10 @@ func TestTokenEndpointRefusedWhenNotGranted(t *testing.T) { name string required bool status int + install bool }{ {name: "an optional ungranted credential is refused", status: http.StatusForbidden}, + {name: "runtime token endpoint refused during install", install: true, status: http.StatusForbidden}, {name: "a required ungranted credential is refused", required: true, status: http.StatusBadGateway}, } @@ -57,11 +61,11 @@ func TestTokenEndpointRefusedWhenNotGranted(t *testing.T) { token: "run-token", org: "org-sys", oauth: []oauthRule{{ - service: "linear", required: tc.required, runtime: true, + service: "linear", required: tc.required, phases: spec.Phases{"runtime"}, tokenHost: compilePattern("token.example"), tokenPath: "/token", }}, } - session.atRun.Store(true) + session.atRun.Store(!tc.install) req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "http://token.example/token", strings.NewReader("grant_type=authorization_code")) rec := httptest.NewRecorder() diff --git a/internal/infrastructure/kit/kit.go b/internal/infrastructure/kit/kit.go index eef6417e9..3d5e26d78 100644 --- a/internal/infrastructure/kit/kit.go +++ b/internal/infrastructure/kit/kit.go @@ -3,19 +3,17 @@ package kit import ( + "context" "errors" "fmt" - "strings" + "github.com/docker/sandbox-kit-spec/v3/fetch" "github.com/docker/sandbox-kit-spec/v3/spec" ) const ( typeCredential = "com.docker.sandbox/credential@1" typeAgentSessions = "com.docker.sandbox/agent-sessions@1" - - // contextPath is where the composed agent-context body is staged. - contextPath = "/usr/share/archie/agent-context.md" ) // supported is every capability type archie provides. Anything else is @@ -28,6 +26,7 @@ var supported = map[string]bool{ "com.docker.sandbox/lifecycle@1": true, typeAgentSessions: true, "com.docker.sandbox/agent-skills@1": true, + "com.docker.sandbox/agent-skill@1": true, "com.docker.sandbox/agent-context@1": true, "com.docker.sandbox/resources@1": true, } @@ -42,38 +41,12 @@ var forgeServices = map[string]bool{ "bitbucket": true, } -// Skip is an optional capability archie does not provide, recorded so the -// step that ran without it can say so. -type Skip struct { - Type string - Reason string -} - -// Refusal is a required capability archie will not provide. -type Refusal struct { - Type string - Reason string -} - -// RefusedError carries every refusal, so an operator fixes a Kit in one pass. -type RefusedError struct { - Refusals []Refusal -} - -func (e *RefusedError) Error() string { - parts := make([]string, len(e.Refusals)) - for i, r := range e.Refusals { - parts[i] = r.Type + ": " + r.Reason - } - return "kit refused: " + strings.Join(parts, "; ") -} - // Plan is what archie launches: the composed descriptor, the capabilities // it provides, and what it skipped. type Plan struct { Descriptor *spec.Descriptor Capabilities []spec.Capability - Skipped []Skip + Skipped []spec.SelectionRecord Sessions *spec.AgentSessions ContextSources []spec.ContextSource } @@ -96,46 +69,37 @@ func DecodePublished(annotations map[string]string) (*spec.Descriptor, error) { return d, nil } -// Admit applies archie's capability support to one descriptor. Every -// required capability archie will not provide is collected into a -// RefusedError; optional ones are skipped. -func Admit(d *spec.Descriptor) (*Plan, error) { - plan := &Plan{Descriptor: d} - var refusals []Refusal - for _, c := range d.Capabilities { - reason, err := refusalReason(c) +// SelectCapability applies Archie's runtime policy before composition. +func SelectCapability(_ context.Context, _ spec.Descriptor, c spec.Capability) spec.CapabilityDecision { + reason, err := refusalReason(c) + if err != nil { + return spec.CapabilityDecision{Message: err.Error()} + } + return spec.CapabilityDecision{Accepted: reason == "", Message: reason} +} + +// FromResolved reads the profile's selected Kit declarations: +// a workload Kit or a published Kit set, never a bare mixin. archie drives a +// harness headlessly, so it must carry an agent-sessions prompt verb. +func FromResolved(resolved *fetch.Resolved) (*Plan, error) { + if resolved.Descriptor.Kind != spec.KindWorkload { + return nil, errors.New("kit is a mixin: publish a Kit set composing it with a workload Kit, and name the set") + } + plan := &Plan{Descriptor: resolved.Descriptor, Capabilities: resolved.Descriptor.Capabilities} + for _, selection := range resolved.Selections { + plan.Skipped = append(plan.Skipped, selection.Selection.Skipped...) + } + for _, unit := range resolved.Kits { + contexts, err := spec.AgentContextsOf(unit.Descriptor.Capabilities) if err != nil { return nil, err } - switch { - case reason == "": - plan.Capabilities = append(plan.Capabilities, c) - case c.Optional: - plan.Skipped = append(plan.Skipped, Skip{Type: c.Type, Reason: reason}) - default: - refusals = append(refusals, Refusal{Type: c.Type, Reason: reason}) + for _, ac := range contexts { + if ac.ContentFile != "" || ac.Content != "" { + plan.ContextSources = append(plan.ContextSources, spec.ContextSource{Reference: unit.Reference, Path: ac.ContentFile, Content: ac.Content}) + } } } - if len(refusals) > 0 { - return nil, &RefusedError{Refusals: refusals} - } - return plan, nil -} - -// MergeOptions are the options every composition archie runs is merged with. -var MergeOptions = spec.MergeOptions{ContextPath: contextPath} - -// FromMerge admits the profile's Kit as the Kit spec's fetch package read it: -// a workload Kit or a published Kit set, never a bare mixin. archie drives a -// harness headlessly, so it must carry an agent-sessions prompt verb. -func FromMerge(merged *spec.MergeResult) (*Plan, error) { - if merged.Descriptor.Kind != spec.KindWorkload { - return nil, errors.New("kit is a mixin: publish a Kit set composing it with a workload Kit, and name the set") - } - plan, err := Admit(merged.Descriptor) - if err != nil { - return nil, err - } sessions, err := spec.AgentSessionsOf(plan.Capabilities) if err != nil { return nil, err @@ -144,7 +108,6 @@ func FromMerge(merged *spec.MergeResult) (*Plan, error) { return nil, fmt.Errorf("kit composition declares no %s prompt verb: archie cannot run it headlessly", typeAgentSessions) } plan.Sessions = sessions - plan.ContextSources = merged.ContextSources return plan, nil } diff --git a/internal/infrastructure/kit/kit_test.go b/internal/infrastructure/kit/kit_test.go new file mode 100644 index 000000000..067e79569 --- /dev/null +++ b/internal/infrastructure/kit/kit_test.go @@ -0,0 +1,214 @@ +package kit + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "slices" + "strconv" + "strings" + "testing" + + "github.com/docker/sandbox-kit-spec/v3/fetch" + "github.com/docker/sandbox-kit-spec/v3/spec" + "github.com/opencontainers/go-digest" + + "github.com/samcharles93/archie-core/internal/infrastructure/egress" +) + +func TestKitCapabilitySelection(t *testing.T) { + t.Parallel() + cases := []struct { + name string + optional bool + refused string + wantError bool + }{ + {"optional unknown group", true, "com.example/unavailable@1", false}, + {"required unknown group", false, "com.example/unavailable@1", true}, + {"optional forge group", true, spec.CapabilityCredential, false}, + {"required forge group", false, spec.CapabilityCredential, true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + rejected := spec.Capability{Type: tc.refused} + if tc.refused == spec.CapabilityCredential { + rejected.Config = map[string]any{"service": "github", "phase": "runtime", "apiKey": map[string]any{"name": "GH_TOKEN"}} + } + value := "resolved-value" + descriptor := spec.Descriptor{SchemaVersion: spec.SchemaVersion, Kind: spec.KindWorkload, Args: map[string]spec.Arg{"setting": {Default: &value, Env: "SETTING"}}, Capabilities: []spec.Capability{ + {Type: spec.CapabilityAgentSessions, Config: map[string]any{"prompt": []string{"agent", "${{ kit.args.setting }}", "{{.Prompt}}"}}}, + {Type: spec.CapabilityAgentContext, Config: map[string]any{"filename": "AGENTS.md", "content": "retained guidance"}}, + {Group: &spec.CapabilityGroup{Optional: tc.optional, Capabilities: []spec.Capability{ + rejected, + {Type: spec.CapabilityLifecycle, Config: map[string]any{"startup": []any{map[string]any{"command": "touch /must-not-run"}}, "files": []any{map[string]any{"path": "/must-not-write", "content": "rejected"}}}}, + {Type: spec.CapabilityAgentContext, Config: map[string]any{"content": "must not appear"}}, + }}}, + }} + client, ref := kitRegistry(t, descriptor) + resolved, err := client.Resolve(t.Context(), []fetch.Request{{Reference: ref}}, fetch.WithCapabilitySelector(SelectCapability)) + if tc.wantError { + if err == nil || !strings.Contains(err.Error(), "required capability selection rejected") { + t.Fatalf("required group error=%v", err) + } + return + } + if err != nil { + t.Fatal(err) + } + plan, err := FromResolved(resolved) + if err != nil { + t.Fatal(err) + } + launch, err := Assemble(plan, ImageConfig{}, LaunchParams{}) + if err != nil { + t.Fatal(err) + } + if len(launch.Startup) != 0 || len(launch.Files) != 0 { + t.Fatalf("rejected group effects survived: %+v", launch) + } + if len(plan.ContextSources) != 1 || plan.ContextSources[0].Content != "retained guidance" { + t.Fatalf("context sources=%+v", plan.ContextSources) + } + if resolved.ContainerEnv["SETTING"] != value || plan.Sessions.Prompt[1] != value { + t.Fatalf("argument exports or prompt not resolved: %+v", resolved) + } + if len(plan.Skipped) != 1 { + t.Fatalf("skipped=%+v", plan.Skipped) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if _, err := client.Resolve(ctx, []fetch.Request{{Reference: ref}}, fetch.WithCapabilitySelector(SelectCapability)); !errors.Is(err, context.Canceled) { + t.Fatalf("canceled resolve=%v", err) + } + }) + } +} + +func kitRegistry(t *testing.T, descriptor spec.Descriptor) (*fetch.Client, string) { + t.Helper() + raw, err := json.Marshal(descriptor) + if err != nil { + t.Fatal(err) + } + manifest, err := json.Marshal(map[string]any{ + "schemaVersion": 2, "mediaType": "application/vnd.oci.image.manifest.v1+json", + "config": map[string]any{"mediaType": "application/vnd.oci.image.config.v1+json", "digest": digest.FromString("config"), "size": 2}, + "layers": []any{map[string]any{"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip", "digest": digest.FromString("layer"), "size": 1}}, + "annotations": map[string]string{spec.AnnotationDescriptor: string(raw)}, + }) + if err != nil { + t.Fatal(err) + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.Contains(r.URL.Path, "/manifests/") { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/vnd.oci.image.manifest.v1+json") + w.Header().Set("Docker-Content-Digest", digest.FromBytes(manifest).String()) + w.Header().Set("Content-Length", strconv.Itoa(len(manifest))) + if r.Method != http.MethodHead { + _, _ = w.Write(manifest) + } + })) + t.Cleanup(server.Close) + client, err := fetch.New() + if err != nil { + t.Fatal(err) + } + return client, strings.TrimPrefix(server.URL, "http://") + "/kit:1.0.0" +} + +func TestKitContextDiscovery(t *testing.T) { + t.Parallel() + for _, tc := range []struct{ name, directory, want string }{ + {"legacy", "", "/archie/AGENTS.md"}, + {"explicit", "/home/agent/.codex", "/home/agent/.codex/AGENTS.md"}, + } { + t.Run(tc.name, func(t *testing.T) { + descriptor := spec.Descriptor{SchemaVersion: spec.SchemaVersion, Kind: spec.KindWorkload, Capabilities: []spec.Capability{ + {Type: spec.CapabilityAgentSessions, Config: map[string]any{"prompt": []string{"agent", "{{.Prompt}}"}}}, + {Type: spec.CapabilityAgentContext, Config: map[string]any{"filename": "AGENTS.md", "directory": tc.directory, "content": "guidance"}}, + }} + if tc.directory == "" { + delete(descriptor.Capabilities[1].Config, "directory") + } + client, ref := kitRegistry(t, descriptor) + resolved, err := client.Resolve(t.Context(), []fetch.Request{{Reference: ref}}, fetch.WithCapabilitySelector(SelectCapability)) + if err != nil { + t.Fatal(err) + } + plan, err := FromResolved(resolved) + if err != nil { + t.Fatal(err) + } + files, err := contextFiles(plan) + if err != nil { + t.Fatal(err) + } + if got := files[len(files)-1].Path; got != tc.want { + t.Fatalf("discovery path=%q, want %q", got, tc.want) + } + }) + } +} + +func TestKitCredentialExposureByPhase(t *testing.T) { + t.Parallel() + for _, tc := range []struct { + name string + phases spec.Phases + install, runtime bool + }{ + {"install", spec.Phases{"install"}, true, false}, + {"runtime", spec.Phases{"runtime"}, false, true}, + {"both", spec.Phases{"install", "runtime"}, true, true}, + } { + t.Run(tc.name, func(t *testing.T) { + credential := spec.Credential{Service: "provider", Phase: tc.phases, APIKey: &spec.APIKey{Name: "PROVIDER_KEY"}} + entry, err := spec.CapabilityWithConfig(spec.Capability{Type: spec.CapabilityCredential}, credential) + if err != nil { + t.Fatal(err) + } + plan := &Plan{Capabilities: []spec.Capability{*entry, {Type: spec.CapabilityLifecycle, Config: map[string]any{ + "install": []any{map[string]any{"command": "true", "env": []string{"PROVIDER_KEY"}}}, + "startup": []any{map[string]any{"command": "true", "env": []string{"PROVIDER_KEY"}}}, + }}}} + launch, err := Assemble(plan, ImageConfig{}, LaunchParams{}) + if err != nil { + t.Fatal(err) + } + for _, check := range []struct { + name string + env []string + want bool + }{ + {"install", launch.Install[0].Env, tc.install}, {"startup", launch.Startup[0].Env, tc.runtime}, {"harness", launch.Harness.Env, tc.runtime}, + } { + if got := slices.Contains(check.env, "PROVIDER_KEY="+egress.Sentinel); got != check.want { + t.Errorf("%s sentinel exposed=%v, want %v", check.name, got, check.want) + } + } + credential.APIKey = nil + credential.OAuth = &spec.OAuth{Sentinels: &spec.Sentinels{AccessToken: "access-sentinel"}, CredentialFile: &spec.CredentialFile{Path: "/tmp/provider.json", Structure: map[string]any{"token": "{{.AccessToken}}"}}} + entry, err = spec.CapabilityWithConfig(spec.Capability{Type: spec.CapabilityCredential}, credential) + if err != nil { + t.Fatal(err) + } + plan.Capabilities[0] = *entry + launch, err = Assemble(plan, ImageConfig{}, LaunchParams{Bound: map[string]egress.CredentialKind{"provider": egress.CredentialOAuth}}) + if err != nil { + t.Fatal(err) + } + if got := len(launch.InstallFiles) > 0; got != tc.install { + t.Errorf("install file present=%v, want %v", got, tc.install) + } + if got := len(launch.Files) > 0; got != tc.runtime { + t.Errorf("runtime file present=%v, want %v", got, tc.runtime) + } + }) + } +} diff --git a/internal/infrastructure/kit/launch.go b/internal/infrastructure/kit/launch.go index bf3c74d2c..847b58bc9 100644 --- a/internal/infrastructure/kit/launch.go +++ b/internal/infrastructure/kit/launch.go @@ -85,22 +85,34 @@ type Volume struct { // Launch is everything archie needs to run a composed Kit for one run. type Launch struct { - Harness agentrun.HarnessSpec - Install []Hook - Startup []Hook - Files []spec.File + Harness agentrun.HarnessSpec + Install []Hook + Startup []Hook + Files []spec.File + InstallFiles []spec.File // Context is the agent-context profile and the inline bodies it points // at, written as root so a run can read but not change them. Context []spec.File + Skills []Skill Volumes []Volume } +// Skill exposes an image-bundled directory at an agent's discovery path. +type Skill struct { + Source string + Target string +} + // Assemble derives a Kit container's launch from its admitted plan and the // workload image's config. The harness never runs as root, and no hook sees // a variable it did not declare beyond the image-derived baseline and the // egress settings every sandbox process needs. func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) { - creds, err := spec.CredentialsOf(p.Capabilities) + installCreds, err := spec.CredentialsOfPhase(p.Capabilities, "install") + if err != nil { + return Launch{}, err + } + runtimeCreds, err := spec.CredentialsOfPhase(p.Capabilities, "runtime") if err != nil { return Launch{}, err } @@ -114,15 +126,13 @@ func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) { } proxyEnv := egress.ProxyEnv(params.ProxyToken, params.CAPath) - runtimeVars := map[string]string{"WORKSPACE_DIR": WorkspaceDir} - for _, c := range creds { - runtimeVars[credentialModeVar(c.Service)] = credentialMode(params.Bound[c.Service]) - } + installVars := credentialVars(installCreds, params.Bound) + runtimeVars := credentialVars(runtimeCreds, params.Bound) l := Launch{Harness: agentrun.HarnessSpec{ User: harnessUser(img.User), Launch: slices.Concat(img.Entrypoint, img.Cmd), - Env: slices.Concat(img.Env, []string{"WORKSPACE_DIR=" + WorkspaceDir}, proxyEnv, egress.SentinelEnv(creds)), + Env: slices.Concat(img.Env, []string{"WORKSPACE_DIR=" + WorkspaceDir}, proxyEnv, egress.SentinelEnv(runtimeCreds)), }} if s := p.Sessions; s != nil { l.Harness.Prompt, l.Harness.Resume, l.Harness.Continue = s.Prompt, s.Resume, s.Continue @@ -133,7 +143,7 @@ func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) { for _, h := range lifecycle.Install { l.Install = append(l.Install, Hook{ Argv: slices.Clone(h.Command), User: orDefault(h.User, installHookUser), - Env: slices.Concat(baseline, declared(h.Env, runtimeVars)), + Env: slices.Concat(baseline, declared(h.Env, installVars)), }) } for _, h := range lifecycle.Startup { @@ -144,7 +154,11 @@ func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) { } l.Files = lifecycle.Files } - credential, err := credentialFiles(creds, params.Bound, params.OAuth) + l.InstallFiles, err = credentialFiles(installCreds, params.Bound, params.OAuth) + if err != nil { + return Launch{}, err + } + credential, err := credentialFiles(runtimeCreds, params.Bound, params.OAuth) if err != nil { return Launch{}, err } @@ -155,9 +169,43 @@ func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) { if l.Context, err = contextFiles(p); err != nil { return Launch{}, err } + l.Skills, err = bundledSkills(p.Capabilities) + if err != nil { + return Launch{}, err + } return l, nil } +func bundledSkills(capabilities []spec.Capability) ([]Skill, error) { + bundles, err := spec.AgentSkillRequestsOf(capabilities) + if err != nil { + return nil, err + } + discovery, err := spec.AgentSkillsOf(capabilities) + if err != nil { + return nil, err + } + var skills []Skill + for _, destination := range discovery { + for _, bundle := range bundles { + skills = append(skills, Skill{Source: bundle.Path, Target: path.Join(destination.Path, spec.AgentSkillName(bundle.AgentSkill))}) + } + } + return skills, nil +} + +func credentialVars(creds []spec.CredentialCapability, bound map[string]egress.CredentialKind) map[string]string { + vars := map[string]string{"WORKSPACE_DIR": WorkspaceDir} + for _, c := range creds { + vars[credentialModeVar(c.Service)] = credentialMode(bound[c.Service]) + } + for _, entry := range egress.SentinelEnv(creds) { + name, value, _ := strings.Cut(entry, "=") + vars[name] = value + } + return vars +} + // credentialFiles renders the credential file for each OAuth-bound // credential that declares one. func credentialFiles(creds []spec.CredentialCapability, bound map[string]egress.CredentialKind, oauth map[string]OAuthFacts) ([]spec.File, error) { @@ -182,7 +230,10 @@ func contextFiles(p *Plan) ([]spec.File, error) { if err != nil || ac == nil || ac.Filename == "" { return nil, err } - root := path.Dir(WorkspaceDir) + root := ac.Directory + if root == "" { + root = path.Dir(WorkspaceDir) + } var files []spec.File var index strings.Builder index.WriteString("# Archie\n\nArchie runs this environment. Each task's rules are in its prompt.\n") diff --git a/internal/infrastructure/kitrun/launcher.go b/internal/infrastructure/kitrun/launcher.go index c67a1365d..3c5d779a6 100644 --- a/internal/infrastructure/kitrun/launcher.go +++ b/internal/infrastructure/kitrun/launcher.go @@ -10,7 +10,11 @@ import ( "errors" "fmt" "maps" + "os" + "path" + "path/filepath" "slices" + "strings" "sync" "github.com/docker/sandbox-kit-spec/v3/fetch" @@ -376,22 +380,28 @@ type kitNeeds struct { // read fetches and admits the profile's Kit, checks it can meet the // workflow's gate retries, and decodes what it asks of the run. func (l *Launcher) read(ctx context.Context, ref string, gateRetries int) (kitNeeds, error) { - merged, err := l.Fetch.Assemble(ctx, []fetch.Request{{Reference: ref}}, kit.MergeOptions) + img, err := l.imageConfig(ctx, ref) + if err != nil { + return kitNeeds{}, err + } + environment := make(map[string]string) + for _, entry := range img.Env { + name, value, _ := strings.Cut(entry, "=") + environment[name] = value + } + resolved, err := l.Fetch.Resolve(ctx, []fetch.Request{{Reference: ref}}, fetch.WithCapabilitySelector(kit.SelectCapability), fetch.WithEnvironment(environment, nil)) if err != nil { return kitNeeds{}, fmt.Errorf("read kit: %w", err) } - k := kitNeeds{} - if k.plan, err = kit.FromMerge(merged.MergeResult); err != nil { + k := kitNeeds{img: img} + if k.plan, err = kit.FromResolved(resolved); err != nil { return kitNeeds{}, err } if err := kit.ValidateNeeds(k.plan, gateRetries); err != nil { return kitNeeds{}, err } - if k.img, err = l.imageConfig(ctx, ref); err != nil { - return kitNeeds{}, err - } - for _, name := range slices.Sorted(maps.Keys(merged.Env)) { - k.img.Env = append(k.img.Env, name+"="+merged.Env[name]) + for _, name := range slices.Sorted(maps.Keys(resolved.ContainerEnv)) { + k.img.Env = append(k.img.Env, name+"="+resolved.ContainerEnv[name]) } if k.network, err = spec.NetworkPolicyOf(k.plan.Capabilities); err != nil { return kitNeeds{}, err @@ -465,17 +475,23 @@ func (l *Launcher) imageConfig(ctx context.Context, ref string) (kit.ImageConfig }, nil } -// skillsBinds mounts the operator's shared skills store read-only at each -// path the Kit asks for it, whatever mode the Kit asks for. With no -// skills_dir configured the host withholds the mount. +// Mount skills individually to preserve bundled discovery. The shared SELinux +// label lets separate run containers read the same read-only store. func skillsBinds(asks []spec.AgentSkillsCapability, sharedDir string) []string { if sharedDir == "" { return nil } var binds []string + store := skill.StoreDir(sharedDir) + entries, err := os.ReadDir(store) + if err != nil { + return nil + } for _, a := range asks { - if a.Path != "" { - binds = append(binds, skill.StoreDir(sharedDir)+":"+a.Path+":ro") + for _, entry := range entries { + if entry.IsDir() { + binds = append(binds, filepath.Join(store, entry.Name())+":"+path.Join(a.Path, entry.Name())+":ro,z") + } } } return binds diff --git a/internal/infrastructure/kitrun/launcher_test.go b/internal/infrastructure/kitrun/launcher_test.go index a3438f8cb..8c0aa08b2 100644 --- a/internal/infrastructure/kitrun/launcher_test.go +++ b/internal/infrastructure/kitrun/launcher_test.go @@ -1,12 +1,15 @@ package kitrun import ( + "os" + "path/filepath" "testing" "github.com/docker/sandbox-kit-spec/v3/spec" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/egress" + "github.com/samcharles93/archie-core/internal/skill" ) // A setup session has no task and no identity grant, so it must bind only the @@ -72,3 +75,31 @@ func TestOrgCredentialKinds(t *testing.T) { }) } } + +func TestSharedSkillsDoNotMaskImageDiscovery(t *testing.T) { + t.Parallel() + root := t.TempDir() + store := skill.StoreDir(root) + if err := os.MkdirAll(filepath.Join(store, "shared"), 0o700); err != nil { + t.Fatal(err) + } + if err := os.Symlink("/etc", filepath.Join(store, "escape")); err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name, root string + want int + }{ + {"shared", root, 1}, {"disabled", "", 0}, {"missing", filepath.Join(root, "missing"), 0}, + } { + t.Run(tc.name, func(t *testing.T) { + binds := skillsBinds([]spec.AgentSkillsCapability{{Path: "/agent/skills"}}, tc.root) + if len(binds) != tc.want { + t.Fatalf("binds=%v", binds) + } + if len(binds) > 0 && binds[0] != filepath.Join(store, "shared")+":/agent/skills/shared:ro,z" { + t.Fatalf("discovery directory masked or writable: %v", binds) + } + }) + } +} diff --git a/internal/worktree/http_test.go b/internal/worktree/http_test.go new file mode 100644 index 000000000..ea9d053b7 --- /dev/null +++ b/internal/worktree/http_test.go @@ -0,0 +1,144 @@ +package worktree + +import ( + "net/http" + "net/http/cgi" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + git "github.com/go-git/go-git/v6" + gitconfig "github.com/go-git/go-git/v6/config" + "github.com/go-git/go-git/v6/plumbing" +) + +func TestForgeHTTPAuthentication(t *testing.T) { + t.Parallel() + root := t.TempDir() + bare, err := git.PlainInit(filepath.Join(root, "org", "repo.git"), true) + if err != nil { + t.Fatal(err) + } + if err := bare.Storer.SetReference(plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName("main"))); err != nil { + t.Fatal(err) + } + cfg, err := bare.Config() + if err != nil { + t.Fatal(err) + } + cfg.Raw.Section("http").SetOption("receivepack", "true") + if err := bare.SetConfig(cfg); err != nil { + t.Fatal(err) + } + seedDir := t.TempDir() + seed, err := git.PlainInit(seedDir, false) + if err != nil { + t.Fatal(err) + } + seedConfig, err := seed.Config() + if err != nil { + t.Fatal(err) + } + seedConfig.Raw.Section("commit").SetOption("gpgsign", "false") + if err := seed.SetConfig(seedConfig); err != nil { + t.Fatal(err) + } + if err := seed.Storer.SetReference(plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName("main"))); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(seedDir, "seed.txt"), []byte("seed"), 0o600); err != nil { + t.Fatal(err) + } + manager := &Manager{BotUser: "archie", BotEmail: "archie@example.test", Token: "secret-for-origin"} + if _, err := manager.CommitAll(t.Context(), seedDir, "seed"); err != nil { + t.Fatal(err) + } + if _, err := seed.CreateRemote(&gitconfig.RemoteConfig{Name: "origin", URLs: []string{filepath.Join(root, "org", "repo.git")}}); err != nil { + t.Fatal(err) + } + if err := seed.PushContext(t.Context(), &git.PushOptions{}); err != nil { + t.Fatal(err) + } + gitPath, err := exec.LookPath("git") + if err != nil { + t.Fatal(err) + } + backend := &cgi.Handler{Path: gitPath, Args: []string{"http-backend"}, Env: []string{"GIT_PROJECT_ROOT=" + root, "GIT_HTTP_EXPORT_ALL=1"}} + for _, tc := range []struct { + name string + redirect, foreign, dumb bool + }{ + {name: "direct"}, {name: "same origin", redirect: true}, {name: "cross origin", foreign: true}, {name: "dumb HTTP", dumb: true}, + } { + t.Run(tc.name, func(t *testing.T) { + target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("Authorization") != "" { + t.Error("forge credential reached another origin") + } + w.WriteHeader(http.StatusUnauthorized) + })) + defer target.Close() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if tc.foreign { + http.Redirect(w, r, target.URL+r.URL.RequestURI(), http.StatusTemporaryRedirect) + return + } + user, password, ok := r.BasicAuth() + if !ok || user != "archie" || password != manager.Token { + http.Error(w, "authentication required", http.StatusUnauthorized) + return + } + if tc.dumb { + w.Header().Set("Content-Type", "text/plain") + _, _ = w.Write([]byte("not a smart Git endpoint")) + return + } + if tc.redirect && !strings.HasPrefix(r.URL.Path, "/redirect/") { + http.Redirect(w, r, "/redirect"+r.URL.RequestURI(), http.StatusTemporaryRedirect) + return + } + r.URL.Path = strings.TrimPrefix(r.URL.Path, "/redirect") + backend.ServeHTTP(w, r) + })) + defer server.Close() + m := *manager + m.BaseURL = server.URL + m.WorkDir = t.TempDir() + dir, branch, err := m.Prepare(t.Context(), "org", "repo", "main", 1, "test", "", "", Fresh) + if tc.foreign || tc.dumb { + if err == nil { + t.Fatal("unsupported or unauthenticated endpoint succeeded") + } + expected := "origin boundary" + if tc.dumb { + expected = "dumb HTTP protocol is not supported" + } + if !strings.Contains(err.Error(), expected) || strings.Contains(err.Error(), m.Token) { + t.Fatalf("not actionable or leaked token: %v", err) + } + return + } + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "change.txt"), []byte("change"), 0o600); err != nil { + t.Fatal(err) + } + if _, err := m.CommitAll(t.Context(), dir, "change"); err != nil { + t.Fatal(err) + } + if err := m.Push(t.Context(), dir, branch); err != nil { + t.Fatal(err) + } + if _, err := bare.Reference(plumbing.NewBranchReferenceName(branch), true); err != nil { + t.Fatalf("pushed branch missing: %v", err) + } + if _, _, err := m.Prepare(t.Context(), "org", "repo", "main", 1, "test", "", "", Target(branch)); err != nil { + t.Fatalf("authenticated fetch: %v", err) + } + }) + } +}