mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): derive flow log az-id the way subnets do (#5238)
Flow log records derived az-id with a mapping of their own (use1-az1, and cnnorth1-az1 for China), while subnets and DescribeAvailabilityZones derive zone ids with Ec2Service.zoneIdForZoneName (us-east-1-az1), so a record's az-id never matched the AvailabilityZoneId of the subnet it came from. Flow logs now use the same derivation in every region.
This commit is contained in:
1 parent
bcfb9c786c
commit
7f5a332620
2 files changed
+34
-26
No files matched your search
@@ -593,27 +593,13 @@ public class FlowLogService {
|
||||
return ports[ThreadLocalRandom.current().nextInt(ports.length)];
|
||||
}
|
||||
|
||||
/** Map a region + az letter to a plausible AWS az-id (e.g. us-east-1a -> use1-az1). */
|
||||
/**
|
||||
* The zone id of the ENI's zone, derived exactly as subnets and DescribeAvailabilityZones
|
||||
* derive it ({@link Ec2Service#zoneIdForZoneName}), so a record's {@code az-id} matches the
|
||||
* {@code AvailabilityZoneId} of the subnet it came from, in every region and partition.
|
||||
*/
|
||||
private static String azId(String region, String az) {
|
||||
String code = switch (region == null ? "" : region) {
|
||||
case "us-east-1" -> "use1";
|
||||
case "us-east-2" -> "use2";
|
||||
case "us-west-1" -> "usw1";
|
||||
case "us-west-2" -> "usw2";
|
||||
case "eu-west-1" -> "euw1";
|
||||
case "eu-central-1" -> "euc1";
|
||||
case "ap-southeast-1" -> "apse1";
|
||||
case "ap-southeast-2" -> "apse2";
|
||||
default -> region == null ? "use1" : region.replaceAll("[^a-z0-9]", "");
|
||||
};
|
||||
int n = 1;
|
||||
if (az != null && !az.isEmpty()) {
|
||||
char last = az.charAt(az.length() - 1);
|
||||
if (last >= 'a' && last <= 'f') {
|
||||
n = (last - 'a') + 1;
|
||||
}
|
||||
}
|
||||
return code + "-az" + n;
|
||||
return region == null ? null : Ec2Service.zoneIdForZoneName(region, az);
|
||||
}
|
||||
|
||||
private static String nz(String s) {
|
||||
|
||||
@@ -181,6 +181,23 @@ class FlowLogServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A record's az-id is the zone id subnets and DescribeAvailabilityZones publish for the same
|
||||
* zone. FlowLogService once derived its own ({@code use1-az1}, and {@code cnnorth1-az1} for
|
||||
* China), so the two never agreed in any region.
|
||||
*/
|
||||
@Test
|
||||
void azIdMatchesTheZoneIdEc2PublishesForTheZone() throws Exception {
|
||||
for (String region : List.of("us-east-1", "cn-north-1", "us-gov-west-1")) {
|
||||
List<String> lines = deliver(region, "${az-id}");
|
||||
|
||||
assertEquals("az-id", lines.get(0));
|
||||
for (String row : records(lines)) {
|
||||
assertEquals(Ec2Service.zoneIdForZoneName(region, region + "a"), row, "in " + region);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The record lines of a delivered file, which must not be empty. */
|
||||
private static List<String> records(List<String> lines) {
|
||||
assertFalse(lines.size() < 2, "delivery carried a header and no records");
|
||||
@@ -189,16 +206,21 @@ class FlowLogServiceTest {
|
||||
|
||||
/** Deliver one file for a flow log with this LogFormat and return its lines. */
|
||||
private List<String> deliver(String logFormat) throws Exception {
|
||||
return deliver("us-east-1", logFormat);
|
||||
}
|
||||
|
||||
/** Deliver one file for a flow log in {@code region} with this LogFormat and return its lines. */
|
||||
private List<String> deliver(String region, String logFormat) throws Exception {
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
when(ec2Service.callerAccountId()).thenReturn("000000000000");
|
||||
when(ec2Service.describeInstances(any(), any(), any()))
|
||||
.thenReturn(List.of(reservation()));
|
||||
.thenReturn(List.of(reservation(region)));
|
||||
when(ec2Service.endpointNetworkInterfaces(any())).thenReturn(List.of());
|
||||
S3Service s3Service = mock(S3Service.class);
|
||||
FlowLogService service =
|
||||
new FlowLogService(ec2Service, s3Service, new InMemoryStorage<>());
|
||||
|
||||
FlowLog fl = service.createFlowLog("us-east-1", "vpc-123", "VPC", "ALL", "s3",
|
||||
FlowLog fl = service.createFlowLog(region, "vpc-123", "VPC", "ALL", "s3",
|
||||
"arn:aws:s3:::flow-bucket", null, logFormat, 600);
|
||||
service.generateAndDeliver(fl);
|
||||
|
||||
@@ -209,20 +231,20 @@ class FlowLogServiceTest {
|
||||
return List.of(gunzip(body.getValue()).split("\n"));
|
||||
}
|
||||
|
||||
private static Reservation reservation() {
|
||||
private static Reservation reservation(String region) {
|
||||
Reservation reservation = new Reservation();
|
||||
reservation.setInstances(List.of(instance("i-aaa", "10.0.0.4"), instance("i-bbb", "10.0.0.5")));
|
||||
reservation.setInstances(List.of(instance("i-aaa", "10.0.0.4", region), instance("i-bbb", "10.0.0.5", region)));
|
||||
return reservation;
|
||||
}
|
||||
|
||||
private static Instance instance(String instanceId, String privateIp) {
|
||||
private static Instance instance(String instanceId, String privateIp, String region) {
|
||||
Instance instance = new Instance();
|
||||
instance.setInstanceId(instanceId);
|
||||
instance.setPrivateIpAddress(privateIp);
|
||||
instance.setVpcId("vpc-123");
|
||||
instance.setSubnetId("subnet-123");
|
||||
Placement placement = new Placement();
|
||||
placement.setAvailabilityZone("us-east-1a");
|
||||
placement.setAvailabilityZone(region + "a");
|
||||
instance.setPlacement(placement);
|
||||
return instance;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user