Skip to content

Commit 97bce72

Browse files
committed
Address review feedback: nil check, ambiguity detection, test fixes
- Add nil check for c.EC2 in resolveCurrentVPCID to prevent panic - Detect ambiguous VPC resolution when private IP matches multiple VPCs - Replace os.Unsetenv with t.Setenv for test isolation - Replace flaky resolveCurrentVPCID test with nil EC2 client test https://claude.ai/code/session_019cXyAQaLbKyaHWnZupcNUC
1 parent f056d96 commit 97bce72

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

pkg/aws/ec2.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,10 @@ func (c *EC2Client) tryVPCWideConfiguration(ctx context.Context, hopLimit int32,
12151215

12161216
// resolveCurrentVPCID determines the VPC ID of the current instance by looking up its private IP
12171217
func (c *EC2Client) resolveCurrentVPCID(ctx context.Context) (string, error) {
1218+
if c.EC2 == nil {
1219+
return "", fmt.Errorf("EC2 client is not initialized")
1220+
}
1221+
12181222
privateIP, err := c.getPrivateIPFromNetworkInterface()
12191223
if err != nil {
12201224
return "", fmt.Errorf("failed to get private IP for VPC resolution: %v", err)
@@ -1244,15 +1248,30 @@ func (c *EC2Client) resolveCurrentVPCID(ctx context.Context) (string, error) {
12441248
return "", fmt.Errorf("failed to describe instances for VPC resolution: %v", err)
12451249
}
12461250

1251+
// Collect unique VPC IDs to detect ambiguity from overlapping CIDRs
1252+
vpcIDs := make(map[string]struct{})
12471253
for _, reservation := range result.Reservations {
12481254
for _, instance := range reservation.Instances {
12491255
if instance.VpcId != nil {
1250-
c.Logger.V(1).Info("Resolved VPC ID", "vpcID", *instance.VpcId, "privateIP", privateIP)
1251-
return *instance.VpcId, nil
1256+
vpcIDs[*instance.VpcId] = struct{}{}
12521257
}
12531258
}
12541259
}
12551260

1261+
if len(vpcIDs) == 0 {
1262+
return "", fmt.Errorf("no VPC ID found for instance with private IP %s", privateIP)
1263+
}
1264+
1265+
if len(vpcIDs) > 1 {
1266+
return "", fmt.Errorf("ambiguous VPC resolution: private IP %s matched instances in %d different VPCs", privateIP, len(vpcIDs))
1267+
}
1268+
1269+
// Exactly one VPC matched
1270+
for vpcID := range vpcIDs {
1271+
c.Logger.V(1).Info("Resolved VPC ID", "vpcID", vpcID, "privateIP", privateIP)
1272+
return vpcID, nil
1273+
}
1274+
12561275
return "", fmt.Errorf("no VPC ID found for instance with private IP %s", privateIP)
12571276
}
12581277

pkg/aws/imds_vpc_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package aws
22

33
import (
44
"context"
5-
"os"
65
"testing"
76

87
"github.com/go-logr/logr/testr"
@@ -29,8 +28,8 @@ func TestTryVPCWideConfiguration_EmptyVPCID(t *testing.T) {
2928
// TestTryVPCWideConfiguration_AggressiveDisabled verifies that tryVPCWideConfiguration
3029
// returns an error when aggressive configuration is disabled.
3130
func TestTryVPCWideConfiguration_AggressiveDisabled(t *testing.T) {
32-
// Ensure aggressive configuration is disabled
33-
os.Unsetenv("IMDS_AGGRESSIVE_CONFIGURATION")
31+
// t.Setenv automatically restores the original value after the test
32+
t.Setenv("IMDS_AGGRESSIVE_CONFIGURATION", "false")
3433

3534
client := &EC2Client{
3635
Logger: testr.New(t),
@@ -47,21 +46,20 @@ func TestTryVPCWideConfiguration_AggressiveDisabled(t *testing.T) {
4746
}
4847
}
4948

50-
// TestResolveCurrentVPCID_NoPrivateIP verifies that resolveCurrentVPCID
51-
// returns an error when no private IP can be determined.
52-
func TestResolveCurrentVPCID_NoPrivateIP(t *testing.T) {
53-
// Ensure PRIVATE_IP env var is not set so the lookup relies on network interfaces
54-
os.Unsetenv("PRIVATE_IP")
55-
49+
// TestResolveCurrentVPCID_NilEC2Client verifies that resolveCurrentVPCID
50+
// returns a clear error when the EC2 client is not initialized.
51+
func TestResolveCurrentVPCID_NilEC2Client(t *testing.T) {
5652
client := &EC2Client{
5753
Logger: testr.New(t),
5854
}
5955

60-
// This will fail because there's no EC2 client and the network interface
61-
// lookup will either return a local IP or fail - either way, without a
62-
// real EC2 client the DescribeInstances call would fail.
6356
_, err := client.resolveCurrentVPCID(context.Background())
6457
if err == nil {
65-
t.Fatal("expected error when resolving VPC ID without AWS credentials, got nil")
58+
t.Fatal("expected error when EC2 client is nil, got nil")
59+
}
60+
61+
expected := "EC2 client is not initialized"
62+
if err.Error() != expected {
63+
t.Errorf("expected error %q, got %q", expected, err.Error())
6664
}
6765
}

0 commit comments

Comments
 (0)