test: capture SDK logs and require every scanned secret surface
Task 7 fix round 1 (Astra R1 P2-1, P2-2). - P2-1: the adapter secret test now sends the SDK's own logger (stderr by default) to a capture through new test-only constructors (NewComputeLogged, NewStorageLogged) and asserts the adapters' load path resolves no request, response or signing log mode (LoadedLogMode). Both the Go and SDK logs are searched for the plaintext sentinels, the base64 user data and its exact wire form. A detector control with body logging forced on must see the wire user data and a minted secret in the same capture. - P2-2: the console secret test requires both boxes ready, both ready objects read and decoded, the show reply valid (200, decoded name, managed, boxes ready, credential state) and the database file read; only the SQLite -wal/-shm sidecars may be absent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+151
−9
internal/awsmanaged/export_test.go+30 −0internal/awsmanaged/integration_test.go+97 −6internal/console/managed_e2e_test.go+24 −3
diff --git a/internal/awsmanaged/export_test.go b/internal/awsmanaged/export_test.go| index 20fc72485..3b0a280e4 100644 |
| --- a/internal/awsmanaged/export_test.go |
| +++ b/internal/awsmanaged/export_test.go |
| @@ -4,9 +4,11 @@ import ( |
| "context" |
| "net/http" |
| + "github.com/aws/aws-sdk-go-v2/aws" |
| awsconfig "github.com/aws/aws-sdk-go-v2/config" |
| "github.com/aws/aws-sdk-go-v2/credentials" |
| "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" |
| + "github.com/aws/smithy-go/logging" |
| ) |
| // HTTPClient is the transport a test puts under the real SDK clients. |
| @@ -28,6 +30,34 @@ func NewStorageWith(ctx context.Context, cfg StorageConfig, transport HTTPClient |
| return loadStorage(ctx, cfg, testLoadOptions(transport)...) |
| } |
| +// NewComputeLogged and NewStorageLogged are the With constructors with the |
| +// SDK's own logger (stderr by default) sent to logger, so a test sees every |
| +// line the loaded clients could write. The adapter's client log mode is kept |
| +// unless extra is nonzero; extra exists only for a detector control that |
| +// proves the capture would see a logged request body. |
| +func NewComputeLogged(ctx context.Context, cfg ComputeConfig, transport HTTPClient, logger logging.Logger, extra aws.ClientLogMode) (*Compute, error) { |
| + return loadCompute(ctx, cfg, testLogOptions(transport, logger, extra)...) |
| +} |
| + |
| +func NewStorageLogged(ctx context.Context, cfg StorageConfig, transport HTTPClient, logger logging.Logger, extra aws.ClientLogMode) (*Storage, error) { |
| + return loadStorage(ctx, cfg, testLogOptions(transport, logger, extra)...) |
| +} |
| + |
| +// LoadedLogMode is the client log mode the adapters' load path resolves for |
| +// region, through loadRegional as every adapter client is loaded. |
| +func LoadedLogMode(ctx context.Context, region string, transport HTTPClient) (aws.ClientLogMode, error) { |
| + cfg, err := loadRegional(ctx, region, testLoadOptions(transport)...) |
| + return cfg.ClientLogMode, err |
| +} |
| + |
| +func testLogOptions(transport HTTPClient, logger logging.Logger, extra aws.ClientLogMode) []func(*awsconfig.LoadOptions) error { |
| + opts := append(testLoadOptions(transport), awsconfig.WithLogger(logger)) |
| + if extra != 0 { |
| + opts = append(opts, awsconfig.WithClientLogMode(extra)) |
| + } |
| + return opts |
| +} |
| + |
| func testLoadOptions(transport HTTPClient) []func(*awsconfig.LoadOptions) error { |
| return []func(*awsconfig.LoadOptions) error{ |
| awsconfig.WithHTTPClient(transport), |
diff --git a/internal/awsmanaged/integration_test.go b/internal/awsmanaged/integration_test.go| index 4c330a56f..38e5ddb01 100644 |
| --- a/internal/awsmanaged/integration_test.go |
| +++ b/internal/awsmanaged/integration_test.go |
| @@ -30,6 +30,8 @@ import ( |
| "sync" |
| "testing" |
| + "github.com/aws/aws-sdk-go-v2/aws" |
| + "github.com/aws/smithy-go/logging" |
| "github.com/carlosframework/platform/internal/awsmanaged" |
| "github.com/carlosframework/platform/internal/compute" |
| "github.com/carlosframework/platform/internal/fleetstore" |
| @@ -822,9 +824,53 @@ func sortedPair(a, b string) []string { |
| return out |
| } |
| +// itLog captures what the SDK's own logger (stderr by default) would write. |
| +type itLog struct { |
| + mu sync.Mutex |
| + b strings.Builder |
| +} |
| + |
| +func (l *itLog) logger() logging.Logger { |
| + return logging.LoggerFunc(func(_ logging.Classification, format string, v ...any) { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + fmt.Fprintf(&l.b, format+"\n", v...) |
| + }) |
| +} |
| + |
| +func (l *itLog) String() string { |
| + l.mu.Lock() |
| + defer l.mu.Unlock() |
| + return l.b.String() |
| +} |
| + |
| +// itBodyLogModes are the client log modes that write requests, responses or |
| +// signing material, any of which can carry user data or a minted secret. |
| +const itBodyLogModes = aws.LogRequest | aws.LogRequestWithBody | aws.LogResponse | aws.LogResponseWithBody | aws.LogSigning |
| + |
| +// itWireUserData is UserData exactly as it went on the wire in the first |
| +// recorded RunInstances request. |
| +func itWireUserData(t *testing.T, w *awsWorld) string { |
| + t.Helper() |
| + for i, req := range w.requests { |
| + if w.actions[i] != "RunInstances" { |
| + continue |
| + } |
| + _, body, _ := strings.Cut(req, "\n\n") |
| + for _, field := range strings.Split(body, "&") { |
| + if v, ok := strings.CutPrefix(field, "UserData="); ok && v != "" { |
| + return v |
| + } |
| + } |
| + } |
| + t.Fatal("no RunInstances user data on the wire") |
| + return "" |
| +} |
| + |
| // TestAWSManagedSecretSurfaces: the enrol token and private key in user |
| // data, the minted secret, and every AWS error message stay out of adapter |
| -// errors, diagnostics, logs and every request other than their own. |
| +// errors, diagnostics, both the Go and the SDK logs, and every request other |
| +// than their own. |
| func TestAWSManagedSecretSurfaces(t *testing.T) { |
| itEnv(t) |
| var logs bytes.Buffer |
| @@ -833,8 +879,25 @@ func TestAWSManagedSecretSurfaces(t *testing.T) { |
| defer log.SetOutput(old) |
| w := newAWSWorld(t) |
| ctx := context.Background() |
| - p := newITCompute(t, w) |
| - s := newITStorage(t, w) |
| + // The adapters' load path resolves no request, response or signing |
| + // logging for any client it makes. |
| + for _, region := range []string{itRegion, itStorageConfig().IAMRegion} { |
| + mode, err := awsmanaged.LoadedLogMode(ctx, region, w) |
| + if err != nil || mode&itBodyLogModes != 0 { |
| + t.Fatalf("%s clients log mode %b: %v", region, mode, err) |
| + } |
| + } |
| + sdkLog := &itLog{} |
| + pc, err := awsmanaged.NewComputeLogged(ctx, itComputeConfig(), w, sdkLog.logger(), 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + sc, err := awsmanaged.NewStorageLogged(ctx, itStorageConfig(), w, sdkLog.logger(), 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var p compute.Provisioner = pc |
| + var s fleetstore.Provisioner = sc |
| var errs []error |
| inst, err := p.Launch(ctx, itSpec()) |
| @@ -913,8 +976,10 @@ func TestAWSManagedSecretSurfaces(t *testing.T) { |
| t.Fatalf("FleetCredentials returned %d secret bytes: %v", len(named.Secret), err) |
| } |
| - secrets := []string{itToken, itPrivateKey, "PRIVKEY-SENTINEL", itAWSMessage, string(cred.Secret), itSecretMark} |
| - surfaces := map[string]string{"diagnostics": diag, "logs": logs.String()} |
| + userData := base64.StdEncoding.EncodeToString([]byte(itSpec().UserData)) |
| + wireUserData := itWireUserData(t, w) |
| + secrets := []string{itToken, itPrivateKey, "PRIVKEY-SENTINEL", itAWSMessage, string(cred.Secret), itSecretMark, userData, wireUserData} |
| + surfaces := map[string]string{"diagnostics": diag, "logs": logs.String(), "sdk logs": sdkLog.String()} |
| for i, err := range errs { |
| surfaces[fmt.Sprintf("error %d", i)] = err.Error() |
| if strings.Contains(err.Error(), "AKIAFLEETOLDDELIVERED") { |
| @@ -930,7 +995,6 @@ func TestAWSManagedSecretSurfaces(t *testing.T) { |
| } |
| // On the wire: the token and key travel only inside RunInstances user |
| // data (base64), and no request ever carries a minted secret. |
| - userData := base64.StdEncoding.EncodeToString([]byte(itSpec().UserData)) |
| for i, req := range w.requests { |
| for _, secret := range []string{itToken, itPrivateKey, "PRIVKEY-SENTINEL", itSecretMark, string(cred.Secret)} { |
| if strings.Contains(req, secret) { |
| @@ -948,4 +1012,31 @@ func TestAWSManagedSecretSurfaces(t *testing.T) { |
| } |
| } |
| } |
| + |
| + // Detector control: with body logging forced on, the same capture sees |
| + // the wire user data and a minted secret, so the clean result above is |
| + // the adapter's, not a blind capture. |
| + control := &itLog{} |
| + cw := newAWSWorld(t) |
| + cp, err := awsmanaged.NewComputeLogged(ctx, itComputeConfig(), cw, control.logger(), aws.LogRequestWithBody|aws.LogResponseWithBody) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err = cp.Launch(ctx, itSpec()); err != nil { |
| + t.Fatal(err) |
| + } |
| + cs, err := awsmanaged.NewStorageLogged(ctx, itStorageConfig(), cw, control.logger(), aws.LogRequestWithBody|aws.LogResponseWithBody) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err = cs.EnsureFleetStorage(ctx, itFleet, itRegion); err != nil { |
| + t.Fatal(err) |
| + } |
| + minted, err := cs.RotateFleetCredentials(ctx, itFleet, "", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := control.String(); !strings.Contains(got, itWireUserData(t, cw)) || !strings.Contains(got, string(minted.Secret)) { |
| + t.Fatalf("control capture missed a logged body (%d bytes)", len(got)) |
| + } |
| } |
diff --git a/internal/console/managed_e2e_test.go b/internal/console/managed_e2e_test.go| index 2a291f14a..e0a8793a9 100644 |
| --- a/internal/console/managed_e2e_test.go |
| +++ b/internal/console/managed_e2e_test.go |
| @@ -778,6 +778,12 @@ func TestManaged2B1SecretSurfaces(t *testing.T) { |
| secrets = append(secrets, []byte(token), []byte(reply.TunnelToken), reply.Credentials.Secret, []byte(base64.StdEncoding.EncodeToString(reply.Credentials.Secret))) |
| } |
| e2eReady(t, s, time.Now().UTC().Add(time.Minute)) |
| + // Both boxes published: every surface the scan names must exist. |
| + for _, b := range e2eBoxes(t, s) { |
| + if b.State != "ready" || b.ReadyAt.IsZero() { |
| + t.Fatalf("box not ready before the scan: %+v", b) |
| + } |
| + } |
| // Another delivered key is still live: an add refuses for an operator, |
| // naming no key. |
| extra, err := storage.Fake.RotateFleetCredentials(ctx, "production", keep, nil) |
| @@ -796,6 +802,16 @@ func TestManaged2B1SecretSurfaces(t *testing.T) { |
| t.Fatalf("needs-operator refusal: %d %s", refused.Code, refused.Body) |
| } |
| show := managedAPICall(s, "show", "abc", "production", "") |
| + shown := managedAPIReply(t, show) |
| + if show.Code != 200 || shown.Name != "production" || !shown.Managed || len(shown.Boxes) != 2 || shown.Operation == nil || |
| + shown.CredentialState != managedNeedsOperator || shown.CredentialReason != managedReasonDeliveredKeyLive { |
| + t.Fatalf("show before the scan: %d %s", show.Code, show.Body) |
| + } |
| + for _, b := range shown.Boxes { |
| + if b.State != "ready" { |
| + t.Fatalf("show box before the scan: %+v", b) |
| + } |
| + } |
| if strings.Contains(show.Body.String(), extra.ID) || strings.Contains(show.Body.String(), keep) { |
| t.Fatalf("show names a key: %s", show.Body) |
| } |
| @@ -807,7 +823,9 @@ func TestManaged2B1SecretSurfaces(t *testing.T) { |
| } |
| for _, f := range []string{path, path + "-wal", path + "-shm"} { |
| raw, err := os.ReadFile(f) |
| - if err != nil && !os.IsNotExist(err) { |
| + // Only the SQLite sidecars may be absent; the database itself must |
| + // be read. |
| + if err != nil && (f == path || !os.IsNotExist(err)) { |
| t.Fatal(err) |
| } |
| surfaces[f] = raw |
| @@ -815,9 +833,12 @@ func TestManaged2B1SecretSurfaces(t *testing.T) { |
| for _, b := range e2eBoxes(t, s) { |
| raw, _ := json.Marshal(b) |
| surfaces["box "+b.Label] = raw |
| - if obj, _, err := s.cfg.Releases.Get(ctx, managedReadyKey(b)); err == nil { |
| - surfaces["ready "+b.Label] = obj |
| + obj, _, err := s.cfg.Releases.Get(ctx, managedReadyKey(b)) |
| + var ready managedReadyObject |
| + if err != nil || json.Unmarshal(obj, &ready) != nil || ready.Fleet != b.Fleet || ready.Label != b.Label || ready.ReadyAt.IsZero() { |
| + t.Fatalf("ready object for %s: %q %v", b.Label, obj, err) |
| } |
| + surfaces["ready "+b.Label] = obj |
| } |
| surfaces["logs"] = logs.Bytes() |
| secrets = append(secrets, []byte("SENTINEL")) |