fix: initialize archive credentials before startup environment scrub

This commit is contained in:
renaesop
2026-11-20 14:26:07 +00:00
parent 3f16151fc8
commit 64a53ea9c1
5 changed files with 95 additions and 7 deletions
+3 -3
View File
@@ -5,9 +5,9 @@ import "os"
// secretEnv lists the credentials read from the environment only at startup:
// ARTEX_PG_DSN and ARTEX_SECRET_KEY by NewManager, the API keys by agent.FromEnv inside New.
// agent.FromEnv also reads ARTEX_LLM_PROXY and ARTEX_LLM_BASE_URL: both are URLs that
// can carry credentials in their userinfo (http://user:pass@proxy:3128). The ARTEX_ARCHIVE_S3_*
// keys are not read by the server, but the container may carry them and the agent's
// shells would inherit them.
// can carry credentials in their userinfo (http://user:pass@proxy:3128). The archive
// store also captures ARTEX_ARCHIVE_S3_* credentials during New, before the scrub,
// so the client can use them without passing them to agent child processes.
var secretEnv = []string{
"ARTEX_PG_DSN", "ARTEX_SECRET_KEY", "ANTHROPIC_API_KEY", "OPENAI_API_KEY",
"ARTEX_LLM_PROXY", "ARTEX_LLM_BASE_URL",
+4 -2
View File
@@ -305,8 +305,10 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
} else {
log.Printf("[engine] no LLM provider configured — engine idle until set via /api/llm or env")
}
// Both env readers have now run (NewManager for the DSN, FromEnv above for the
// keys). Drop the secrets before anything below can spawn a child process:
s.initTaskArchiveStore()
// Startup env readers have now run (NewManager for the DSN, the LLM provider
// and archive store for their credentials). Drop the secrets before anything
// below can spawn a child process:
// restoring runtimes resumes workers that run shells, and MCP discovery starts
// stdio servers.
if s.scrubSecrets {
+1 -1
View File
@@ -149,7 +149,7 @@ func TestRemoteArchiveInterruptionRecovery(t *testing.T) {
t.Fatal(err)
}
if phase != "restore_download_before_install" {
if _, err := installTaskArchiveFiles(m.dir, extracted, task.ID, job.ID); err != nil {
if _, err := installTaskArchiveFiles(m.dir, extracted, task.ID, job.ID, s.execPriv); err != nil {
t.Fatal(err)
}
}
+79
View File
@@ -0,0 +1,79 @@
package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
)
func TestArchiveStoreLoadsCredentialsBeforeSecretScrub(t *testing.T) {
skipWithoutDSN(t)
// New scrubs all startup secrets; restore the test database/key environment
// afterward so the remaining package tests keep their isolated configuration.
for _, key := range secretEnv {
t.Setenv(key, os.Getenv(key))
}
var signed atomic.Bool
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Errorf("unexpected S3 operation: %s", r.Method)
}
signed.Store(strings.Contains(r.Header.Get("Authorization"), "Credential=archive-fixture-key/") &&
r.Header.Get("X-Amz-Security-Token") == "archive-fixture-session")
w.WriteHeader(http.StatusNoContent)
}))
defer endpoint.Close()
t.Setenv("ARTEX_ARCHIVE_STORAGE", "s3")
t.Setenv("ARTEX_ARCHIVE_S3_ENDPOINT", endpoint.URL)
t.Setenv("ARTEX_ARCHIVE_S3_REGION", "us-east-1")
t.Setenv("ARTEX_ARCHIVE_S3_BUCKET", "fixture")
t.Setenv("ARTEX_ARCHIVE_S3_PREFIX", "archives")
t.Setenv("ARTEX_ARCHIVE_S3_PATH_STYLE", "true")
t.Setenv("ARTEX_ARCHIVE_S3_ACCESS_KEY_ID", "archive-fixture-key")
t.Setenv("ARTEX_ARCHIVE_S3_SECRET_ACCESS_KEY", "archive-fixture-secret")
t.Setenv("ARTEX_ARCHIVE_S3_SESSION_TOKEN", "archive-fixture-session")
t.Setenv("ARTEX_ARCHIVE_S3_SSE", "AES256")
t.Setenv("ARTEX_ARCHIVE_S3_KMS_KEY_ID", "")
// If initialization happens after the scrub, the SDK would silently select
// this different ambient identity instead of the dedicated override.
t.Setenv("AWS_ACCESS_KEY_ID", "ambient-fixture-key")
t.Setenv("AWS_SECRET_ACCESS_KEY", "ambient-fixture-secret")
t.Setenv("AWS_SESSION_TOKEN", "")
t.Setenv("AWS_PROFILE", "")
t.Setenv("AWS_DEFAULT_PROFILE", "")
t.Setenv("AWS_CA_BUNDLE", "")
t.Setenv("AWS_CONFIG_FILE", filepath.Join(t.TempDir(), "no-config"))
t.Setenv("AWS_SHARED_CREDENTIALS_FILE", filepath.Join(t.TempDir(), "no-credentials"))
t.Setenv("AWS_EC2_METADATA_DISABLED", "true")
m, err := NewManager(t.TempDir(), "")
if err != nil {
t.Fatal(err)
}
defer m.Close()
ctx, cancel := context.WithCancel(context.Background())
cancel() // Exercise real startup without leaving archive/scheduler workers alive.
s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir(), WithSecretScrub())
s.archiveWG.Wait()
if s.archiveStoreErr != nil || s.archiveStore == nil {
t.Fatalf("S3 initialization failed: %v", s.archiveStoreErr)
}
for _, key := range []string{"ARTEX_ARCHIVE_S3_ACCESS_KEY_ID", "ARTEX_ARCHIVE_S3_SECRET_ACCESS_KEY", "ARTEX_ARCHIVE_S3_SESSION_TOKEN"} {
if _, present := os.LookupEnv(key); present {
t.Fatalf("%s remains in the child-process environment", key)
}
}
requestCtx, stop := context.WithTimeout(context.Background(), 3*time.Second)
defer stop()
if err := s.archiveStore.Delete(requestCtx, s.archiveStore.URI(1, 2, "startup-probe")); err != nil {
t.Fatal(err)
}
if !signed.Load() {
t.Fatal("archive client lost its dedicated credentials during startup scrub")
}
}
+8 -1
View File
@@ -34,7 +34,8 @@ type archiveBatchItem struct {
Archive int64 `json:"archive_id,omitempty"`
}
func (s *Server) startTaskArchiveWorker() {
// initTaskArchiveStore captures credentials before New scrubs the process environment.
func (s *Server) initTaskArchiveStore() {
if s.m == nil || s.m.pg == nil {
return
}
@@ -44,6 +45,12 @@ func (s *Server) startTaskArchiveWorker() {
} else if s.archiveStore != nil {
log.Printf("[task-archive] new archives use S3 object storage with full SHA-256 readback verification")
}
}
func (s *Server) startTaskArchiveWorker() {
if s.m == nil || s.m.pg == nil {
return
}
if err := s.m.pg.RecoverTaskArchiveJobs(); err != nil {
log.Printf("[task-archive] recover jobs: %v", err)
}