Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
ARTEX_PG_DSN: postgres://artex:testpass@localhost:5432/artextest_race?sslmode=disable

run: >-
go test -race -p 1 ./agent ./cmd/artex ./config ./db ./evidence ./guard
go test -race -p 1 ./agent ./archivestore ./cmd/artex ./config ./db ./evidence ./guard
./intercept ./llmpool ./llmrec ./mcphttp ./notify ./server ./sidequestion
./traffic ./traffic/rewrite

Expand Down
89 changes: 89 additions & 0 deletions archivestore/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package archivestore

import (
"strings"
"testing"
)

func TestArchiveStorageEnvironment(t *testing.T) {
for _, key := range []string{
"ARTEX_ARCHIVE_STORAGE", "ARTEX_ARCHIVE_S3_ENDPOINT", "ARTEX_ARCHIVE_S3_REGION",
"ARTEX_ARCHIVE_S3_BUCKET", "ARTEX_ARCHIVE_S3_PREFIX", "ARTEX_ARCHIVE_S3_ACCESS_KEY_ID",
"ARTEX_ARCHIVE_S3_SECRET_ACCESS_KEY", "ARTEX_ARCHIVE_S3_PATH_STYLE",
"ARTEX_ARCHIVE_S3_SESSION_TOKEN", "ARTEX_ARCHIVE_S3_SSE", "ARTEX_ARCHIVE_S3_KMS_KEY_ID",
} {
t.Setenv(key, "")
}
for _, backend := range []string{"", "local"} {
t.Setenv("ARTEX_ARCHIVE_STORAGE", backend)
if store, err := FromEnv(); err != nil || store != nil {
t.Fatalf("local default: %v %v", store, err)
}
}
for _, backend := range []string{"unknown", "s3"} {
t.Setenv("ARTEX_ARCHIVE_STORAGE", backend)
if _, err := FromEnv(); err == nil {
t.Fatalf("%q silently fell back to local", backend)
}
}
t.Setenv("ARTEX_ARCHIVE_S3_ENDPOINT", "http://127.0.0.1:9000")
t.Setenv("ARTEX_ARCHIVE_S3_BUCKET", "test-bucket")
t.Setenv("ARTEX_ARCHIVE_S3_ACCESS_KEY_ID", "test-key")
t.Setenv("ARTEX_ARCHIVE_S3_SECRET_ACCESS_KEY", "test-secret")
t.Setenv("ARTEX_ARCHIVE_S3_PATH_STYLE", "true")
store, err := FromEnv()
if err != nil {
t.Fatal(err)
}
if uri := store.URI(1, 2, "checksum"); uri != "s3://test-bucket/artex/tasks/task-1-2-checksum.tar.zst" {
t.Fatal("unexpected default object location", uri)
}
t.Setenv("ARTEX_ARCHIVE_S3_SECRET_ACCESS_KEY", "")
if _, err := FromEnv(); err == nil {
t.Fatal("missing credentials accepted")
}
}

func TestArchiveEndpointValidation(t *testing.T) {
config := Config{Bucket: "test", AccessKey: "test-key", SecretKey: "test-secret"}
for _, endpoint := range []string{"https://objects.example.test", "http://127.0.0.1:9000", "http://127.0.0.2:9000", "http://[::1]:9000", "http://localhost:9000"} {
config.Endpoint = endpoint
if _, err := New(config); err != nil {
t.Errorf("rejected %s: %v", endpoint, err)
}
}
for _, endpoint := range []string{"http://objects.example.test", "https://user:test-secret@objects.example.test", "https://objects.example.test/bucket", "https://objects.example.test?token=test-secret", "https://objects.example.test#fragment"} {
config.Endpoint = endpoint
if _, err := New(config); err == nil {
t.Error("accepted endpoint with non-loopback HTTP, credentials, path, query or fragment")
} else if strings.Contains(err.Error(), "test-secret") {
t.Fatal("configuration error disclosed a secret")
}
}
}

func TestArchiveEncryptionConfiguration(t *testing.T) {
c := Config{Endpoint: "https://objects.example.test", Bucket: "test", AccessKey: "test", SecretKey: "test"}
for _, mode := range []string{"none", "invalid"} {
c.SSE = mode
if _, err := New(c); err == nil {
t.Fatal("unencrypted/invalid mode accepted")
}
}
c.SSE = "AES256"
c.KMSKeyID = "alias/archive"
if _, err := New(c); err == nil {
t.Fatal("KMS key accepted with SSE-S3")
}
c.SSE = "aws:kms"
for _, key := range []string{"alias/archive", "bare-key-id", "arn:aws:kms:us-east-1:123456789012:alias/archive"} {
c.KMSKeyID = key
if _, err := New(c); err == nil {
t.Fatalf("accepted noncanonical KMS key %q", key)
}
}
c.KMSKeyID = "arn:aws:kms:us-east-1:123456789012:key/test-key"
if _, err := New(c); err != nil {
t.Fatal(err)
}
}
132 changes: 132 additions & 0 deletions archivestore/credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package archivestore

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)

func isolateAWSCredentials(t *testing.T) {
t.Helper()
for _, key := range []string{
"AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_PROFILE", "AWS_DEFAULT_PROFILE",
"AWS_ROLE_ARN", "AWS_ROLE_SESSION_NAME", "AWS_WEB_IDENTITY_TOKEN_FILE",
"AWS_CONTAINER_CREDENTIALS_RELATIVE_URI", "AWS_CONTAINER_CREDENTIALS_FULL_URI",
"AWS_CONTAINER_AUTHORIZATION_TOKEN", "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE",
"AWS_EC2_METADATA_SERVICE_ENDPOINT", "AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE",
} {
t.Setenv(key, "")
}
dir := t.TempDir()
t.Setenv("AWS_SHARED_CREDENTIALS_FILE", filepath.Join(dir, "credentials"))
t.Setenv("AWS_CONFIG_FILE", filepath.Join(dir, "config"))
t.Setenv("AWS_EC2_METADATA_DISABLED", "true")
}

func TestArchiveCredentialChain(t *testing.T) {
config := Config{Endpoint: "https://objects.example.test", Bucket: "test"}
for _, mode := range []string{"explicit-session", "environment", "shared-profile", "ecs", "imds"} {
t.Run(mode, func(t *testing.T) {
isolateAWSCredentials(t)
c := config
switch mode {
case "explicit-session":
t.Setenv("AWS_ACCESS_KEY_ID", "other-key")
t.Setenv("AWS_SECRET_ACCESS_KEY", "other-secret")
c.AccessKey, c.SecretKey, c.SessionToken = "fixture-key", "fixture-secret", "fixture-session"
case "environment":
t.Setenv("AWS_ACCESS_KEY_ID", "fixture-key")
t.Setenv("AWS_SECRET_ACCESS_KEY", "fixture-secret")
t.Setenv("AWS_SESSION_TOKEN", "fixture-session")
case "shared-profile":
t.Setenv("AWS_PROFILE", "archive")
data := `[archive]
aws_access_key_id=fixture-key
aws_secret_access_key=fixture-secret
aws_session_token=fixture-session
`
if err := os.WriteFile(os.Getenv("AWS_SHARED_CREDENTIALS_FILE"), []byte(data), 0600); err != nil {
t.Fatal(err)
}
case "ecs", "imds":
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if mode == "imds" {
switch r.URL.Path {
case "/latest/api/token":
if r.Method != "PUT" {
t.Error("IMDS did not use token request")
}
w.Header().Set("X-aws-ec2-metadata-token-ttl-seconds", "21600")
fmt.Fprint(w, "fixture-imds-token")
return
case "/latest/meta-data/iam/security-credentials/":
if r.Header.Get("X-aws-ec2-metadata-token") != "fixture-imds-token" {
t.Error("missing IMDSv2 token")
}
fmt.Fprint(w, "archive-role")
return
case "/latest/meta-data/iam/security-credentials/archive-role":
if r.Header.Get("X-aws-ec2-metadata-token") != "fixture-imds-token" {
t.Error("missing IMDSv2 token")
}
default:
t.Errorf("unexpected IMDS request %s", r.URL.Path)
w.WriteHeader(404)
return
}
} else if r.Header.Get("Authorization") != "fixture-container-token" {
t.Error("missing ECS authorization token")
}
_ = json.NewEncoder(w).Encode(map[string]any{
"Code": "Success", "AccessKeyId": "fixture-key", "SecretAccessKey": "fixture-secret",
"Token": "fixture-session", "Expiration": time.Now().Add(time.Hour).UTC().Format(time.RFC3339),
})
}))
defer srv.Close()
if mode == "imds" {
t.Setenv("AWS_EC2_METADATA_DISABLED", "false")
t.Setenv("AWS_EC2_METADATA_V1_DISABLED", "true")
t.Setenv("AWS_EC2_METADATA_SERVICE_ENDPOINT", srv.URL)
} else {
t.Setenv("AWS_CONTAINER_CREDENTIALS_FULL_URI", srv.URL+"/credentials")
t.Setenv("AWS_CONTAINER_AUTHORIZATION_TOKEN", "fixture-container-token")
}
}
store, err := New(c)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
credentials, err := store.client.Options().Credentials.Retrieve(ctx)
if err != nil {
t.Fatal(err)
}
if credentials.AccessKeyID != "fixture-key" || credentials.SecretAccessKey != "fixture-secret" || credentials.SessionToken != "fixture-session" {
t.Fatal("wrong credential provider or session token")
}
if (mode == "imds" || mode == "ecs") && !credentials.CanExpire {
t.Fatal("role credentials must remain refreshable")
}
})
}
}

func TestArchiveIncompleteCredentialOverride(t *testing.T) {
isolateAWSCredentials(t)
t.Setenv("AWS_ACCESS_KEY_ID", "valid-ambient-key")
t.Setenv("AWS_SECRET_ACCESS_KEY", "valid-ambient-secret")
for _, c := range []Config{{AccessKey: "private-value"}, {SecretKey: "private-value"}, {SessionToken: "private-value"}} {
c.Endpoint, c.Bucket = "https://objects.example.test", "test"
if _, err := New(c); err == nil || strings.Contains(err.Error(), "private-value") {
t.Fatalf("partial override must fail without disclosing credentials: %v", err)
}
}
}
Loading
Loading