fix: validate every EC2 reservation; fetch latest console output
Task 3 review round 1. - describeAll checks each reservation's owner and that it carries at least one instance before reading its instances, so an empty or ownerless reservation makes Find, ListDeployment and Lookup ambiguous instead of being skipped. No reservations at all is still empty. - ConsoleOutput asks for the latest output (Latest=true) so breadcrumbs written after boot are visible. - The test-only NewComputeWith turns the metadata client off in process, as rootaws.LoadStaticConfig does; the transport witness now runs with metadata enabled in the environment.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+66
−5
internal/awsmanaged/compute.go+6 −1internal/awsmanaged/compute_test.go+49 −1internal/awsmanaged/diagnostics_test.go+1 −1internal/awsmanaged/export_test.go+10 −2
diff --git a/internal/awsmanaged/compute.go b/internal/awsmanaged/compute.go| index 67a0d6420..7db12a96c 100644 |
| --- a/internal/awsmanaged/compute.go |
| +++ b/internal/awsmanaged/compute.go |
| @@ -414,6 +414,11 @@ func (c *Compute) describeAll(ctx context.Context, api ec2API, region string, qu |
| return nil, uncertain(ctx, "describe instances answer") |
| } |
| for _, r := range page.Reservations { |
| + // Each reservation is checked on its own, so one with no |
| + // instance cannot slip past the owner check. |
| + if aws.ToString(r.OwnerId) != c.account || len(r.Instances) == 0 { |
| + return nil, uncertain(ctx, "describe instances answer") |
| + } |
| for _, inst := range r.Instances { |
| o, err := c.observe(region, r.OwnerId, inst) |
| if err != nil || ids[o.inst.CloudID] { |
| @@ -593,7 +598,7 @@ func (c *Compute) ConsoleOutput(ctx context.Context, region, cloudID string) (st |
| if o.dup || id.Deployment != c.deployment || id.Fleet == "" || id.Label == "" || id.Operation == "" { |
| return "", mismatch("console output") |
| } |
| - out, err := api.GetConsoleOutput(ctx, &ec2.GetConsoleOutputInput{InstanceId: aws.String(cloudID)}, inRegion(region)) |
| + out, err := api.GetConsoleOutput(ctx, &ec2.GetConsoleOutputInput{InstanceId: aws.String(cloudID), Latest: aws.Bool(true)}, inRegion(region)) |
| if err != nil { |
| return "", uncertain(ctx, "console output") |
| } |
diff --git a/internal/awsmanaged/compute_test.go b/internal/awsmanaged/compute_test.go| index 602b99210..10c26cd59 100644 |
| --- a/internal/awsmanaged/compute_test.go |
| +++ b/internal/awsmanaged/compute_test.go |
| @@ -876,6 +876,52 @@ func TestAWSComputeDescribeComplete(t *testing.T) { |
| ambiguous(t, err) |
| }) |
| } |
| + // A reservation is validated even when it carries no instance: an empty |
| + // or ownerless one is a malformed answer, never a quiet skip. |
| + valid := page(testAccount, managedView(idA, testIdentity(), types.InstanceStateNameRunning)) |
| + for badName, bad := range map[string]types.Reservation{"empty reservation": {}, "owned but empty reservation": {OwnerId: aws.String(testAccount)}} { |
| + for name, pages := range map[string][]*ec2.DescribeInstancesOutput{ |
| + "alone": {{Reservations: []types.Reservation{bad}}}, |
| + "beside valid": {{Reservations: []types.Reservation{valid, bad}}}, |
| + "later page": {{Reservations: []types.Reservation{valid}, NextToken: aws.String("t2")}, {Reservations: []types.Reservation{bad}}}, |
| + } { |
| + t.Run(badName+" "+name, func(t *testing.T) { |
| + f := newFakeEC2(t, testRegion) |
| + f.describeHook = func(in *ec2.DescribeInstancesInput, _ int) (*ec2.DescribeInstancesOutput, error) { |
| + if in.NextToken == nil { |
| + return pages[0], nil |
| + } |
| + return pages[1], nil |
| + } |
| + c := newTestCompute(t, f) |
| + found, err := c.Find(ctx, testRegion, testIdentity()) |
| + if found != nil { |
| + t.Fatalf("usable prefix %v", found) |
| + } |
| + ambiguous(t, err) |
| + list, err := c.ListDeployment(ctx, testRegion, testDeployment) |
| + if list != nil { |
| + t.Fatalf("usable prefix %v", list) |
| + } |
| + ambiguous(t, err) |
| + _, err = c.Lookup(ctx, testRegion, idA) |
| + ambiguous(t, err) |
| + }) |
| + } |
| + } |
| + t.Run("no reservations is an empty result", func(t *testing.T) { |
| + f := newFakeEC2(t, testRegion) |
| + f.describeHook = func(*ec2.DescribeInstancesInput, int) (*ec2.DescribeInstancesOutput, error) { |
| + return &ec2.DescribeInstancesOutput{Reservations: []types.Reservation{}}, nil |
| + } |
| + c := newTestCompute(t, f) |
| + if found, err := c.Find(ctx, testRegion, testIdentity()); err != nil || len(found) != 0 { |
| + t.Fatalf("%v %v", found, err) |
| + } |
| + if list, err := c.ListDeployment(ctx, testRegion, testDeployment); err != nil || len(list) != 0 { |
| + t.Fatalf("%v %v", list, err) |
| + } |
| + }) |
| } |
| func TestAWSComputeAbsence(t *testing.T) { |
| @@ -1181,7 +1227,9 @@ const xmlInstance = `<item><instanceId>i-0123456789abcdef0</instanceId><instance |
| // loaded through rootaws, with every byte through a mock transport. |
| func TestAWSComputeTransport(t *testing.T) { |
| none := filepath.Join(t.TempDir(), "none") |
| - for k, v := range map[string]string{"AWS_EC2_METADATA_DISABLED": "true", "AWS_PROFILE": "", "AWS_DEFAULT_PROFILE": "", "AWS_ACCESS_KEY_ID": "", "AWS_SECRET_ACCESS_KEY": "", |
| + // Metadata is left enabled in the environment: NewComputeWith must turn it |
| + // off in process. |
| + for k, v := range map[string]string{"AWS_EC2_METADATA_DISABLED": "false", "AWS_PROFILE": "", "AWS_DEFAULT_PROFILE": "", "AWS_ACCESS_KEY_ID": "", "AWS_SECRET_ACCESS_KEY": "", |
| "AWS_SESSION_TOKEN": "", "AWS_CONFIG_FILE": none, "AWS_SHARED_CREDENTIALS_FILE": none, "AWS_REGION": otherRegion, "AWS_DEFAULT_REGION": otherRegion} { |
| t.Setenv(k, v) |
| } |
diff --git a/internal/awsmanaged/diagnostics_test.go b/internal/awsmanaged/diagnostics_test.go| index e072066f4..b8ca4f0dd 100644 |
| --- a/internal/awsmanaged/diagnostics_test.go |
| +++ b/internal/awsmanaged/diagnostics_test.go |
| @@ -99,7 +99,7 @@ func TestAWSComputeConsoleOutput(t *testing.T) { |
| if err != nil || got != "joining" { |
| t.Fatalf("%q %v", got, err) |
| } |
| - if f.callCount("DescribeInstances") != 1 || f.callCount("GetConsoleOutput") != 1 || aws.ToString(f.consoleInputs[0].InstanceId) != inst.id { |
| + if f.callCount("DescribeInstances") != 1 || f.callCount("GetConsoleOutput") != 1 || aws.ToString(f.consoleInputs[0].InstanceId) != inst.id || !aws.ToBool(f.consoleInputs[0].Latest) { |
| t.Fatalf("calls %v", f.calls) |
| } |
| if in := f.describeInputs[0]; in.Filters != nil || len(in.InstanceIds) != 1 || in.InstanceIds[0] != inst.id { |
diff --git a/internal/awsmanaged/export_test.go b/internal/awsmanaged/export_test.go| index 250c8ff66..35b81d87e 100644 |
| --- a/internal/awsmanaged/export_test.go |
| +++ b/internal/awsmanaged/export_test.go |
| @@ -6,6 +6,7 @@ import ( |
| 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" |
| ) |
| // HTTPClient is the transport a test puts under the real SDK clients. |
| @@ -14,13 +15,20 @@ type HTTPClient interface { |
| } |
| // NewComputeWith is NewCompute with every request sent through transport, |
| -// static dummy credentials and no shared files, so no test can reach AWS |
| -// or the metadata service. Tests also set AWS_EC2_METADATA_DISABLED. |
| +// static dummy credentials, no shared files and the metadata client off in |
| +// process (as rootaws.LoadStaticConfig does), so no test can reach AWS or |
| +// the metadata service whatever its environment. |
| func NewComputeWith(ctx context.Context, cfg ComputeConfig, transport HTTPClient) (*Compute, error) { |
| return loadCompute(ctx, cfg, |
| awsconfig.WithHTTPClient(transport), |
| awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("AKIDTEST", "test-secret", "")), |
| awsconfig.WithSharedConfigFiles([]string{}), awsconfig.WithSharedCredentialsFiles([]string{}), |
| awsconfig.WithSharedConfigProfile(""), |
| + func(o *awsconfig.LoadOptions) error { |
| + o.UseEC2IMDSRegion = nil |
| + o.EC2IMDSClientEnableState = imds.ClientDisabled |
| + o.DefaultsModeOptions.IMDSClient = imds.New(imds.Options{ClientEnableState: imds.ClientDisabled}) |
| + return nil |
| + }, |
| ) |
| } |