Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ec2): report a flow log's custom LogFormat (#4386)
lex00fd3696a9fc09
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2FlowLogFormatTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2FlowLogFormatTest.javanew file mode 100644index 0000000000..39da8df7fc--- /dev/null+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2FlowLogFormatTest.java@@ -0,0 +1,64 @@+package com.floci.test;++import org.junit.jupiter.api.DisplayName;+import org.junit.jupiter.api.Test;+import software.amazon.awssdk.services.ec2.Ec2Client;+import software.amazon.awssdk.services.ec2.model.FlowLog;+import software.amazon.awssdk.services.ec2.model.FlowLogsResourceType;+import software.amazon.awssdk.services.ec2.model.TrafficType;++import java.util.List;++import static org.assertj.core.api.Assertions.assertThat;++class Ec2FlowLogFormatTest {+ private static final String FORMAT =+ "${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${action}";+ /** What AWS creates a flow log with when LogFormat is omitted, the version 2 fields. */+ private static final String DEFAULT_FORMAT =+ "${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${srcport} "+ + "${dstport} ${protocol} ${packets} ${bytes} ${start} ${end} ${action} ${log-status}";++ private static String createFlowLog(Ec2Client ec2, String vpcId, String logFormat) {+ return ec2.createFlowLogs(r -> {+ r.resourceIds(vpcId)+ .resourceType(FlowLogsResourceType.VPC)+ .trafficType(TrafficType.ALL)+ .logDestinationType("s3")+ .logDestination("arn:aws:s3:::flow-logs-bucket");+ if (logFormat != null) {+ r.logFormat(logFormat);+ }+ }).flowLogIds().get(0);+ }++ private static FlowLog describe(Ec2Client ec2, String flowLogId) {+ List<FlowLog> logs = ec2.describeFlowLogs(r -> r.flowLogIds(flowLogId)).flowLogs();+ assertThat(logs).hasSize(1);+ return logs.get(0);+ }++ @Test+ @DisplayName("DescribeFlowLogs returns the custom LogFormat, or the default when none was set")+ void describesTheCustomLogFormat() {+ try (Ec2Client ec2 = TestFixtures.ec2Client()) {+ String vpcId = ec2.createVpc(r -> r.cidrBlock("10.72.0.0/16")).vpc().vpcId();+ String withFormat = null;+ String withoutFormat = null;+ try {+ withFormat = createFlowLog(ec2, vpcId, FORMAT);+ withoutFormat = createFlowLog(ec2, vpcId, null);++ assertThat(describe(ec2, withFormat).logFormat()).isEqualTo(FORMAT);+ assertThat(describe(ec2, withoutFormat).logFormat()).isEqualTo(DEFAULT_FORMAT);+ } finally {+ for (String id : new String[] {withFormat, withoutFormat}) {+ if (id != null) {+ ec2.deleteFlowLogs(r -> r.flowLogIds(id));+ }+ }+ ec2.deleteVpc(r -> r.vpcId(vpcId));+ }+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.javaindex eed5bbc983..073de3247f 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java@@ -1697,6 +1697,8 @@ public class Ec2QueryHandler {.elem("logDestination", fl.getLogDestination()).elem("deliverLogsPermissionArn", fl.getDeliverLogsPermissionArn()).elem("flowLogStatus", fl.getFlowLogStatus())+ .elem("logFormat", fl.getLogFormat() != null+ ? fl.getLogFormat() : FlowLogService.DEFAULT_LOG_FORMAT).elem("deliverLogsStatus", fl.getDeliverLogsStatus()).elem("maxAggregationInterval", String.valueOf(fl.getMaxAggregationInterval())).elem("creationTime", ISO_FMT.format(fl.getCreationTime()))diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/FlowLogService.java b/src/main/java/io/github/hectorvent/floci/services/ec2/FlowLogService.javaindex 6df73a455f..e5c81f0976 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/FlowLogService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/FlowLogService.java@@ -56,7 +56,20 @@ public class FlowLogService {private static final Logger LOG = Logger.getLogger(FlowLogService.class);- /** AWS VPC Flow Logs default (version 5) field order. */+ /**+ * The format a flow log is created with when CreateFlowLogs omits LogFormat, which+ * DescribeFlowLogs reports for it thereafter. Version 2 fields in the order AWS documents.+ *+ * <p>Distinct from {@link #DEFAULT_HEADER}, which is the header line of a generated log file.+ */+ public static final String DEFAULT_LOG_FORMAT =+ "${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${srcport} "+ + "${dstport} ${protocol} ${packets} ${bytes} ${start} ${end} ${action} ${log-status}";++ /**+ * Every available flow log field, alphabetised, written as the header line of a generated log+ * file. Not a version ordering, and not what {@link #DEFAULT_LOG_FORMAT} describes.+ */static final String DEFAULT_HEADER ="account-id action az-id bytes dstaddr dstport end flow-direction instance-id "+ "interface-id log-status packets pkt-dst-aws-service pkt-dstaddr pkt-src-aws-service "diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2FlowLogFormatIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2FlowLogFormatIntegrationTest.javanew file mode 100644index 0000000000..20bedcec26--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2FlowLogFormatIntegrationTest.java@@ -0,0 +1,82 @@+package io.github.hectorvent.floci.services.ec2;++import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.ValidatableResponse;+import io.restassured.specification.RequestSpecification;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;++/**+ * DescribeFlowLogs reports the custom LogFormat the flow log was created with.+ *+ * <p>CreateFlowLogs parsed LogFormat and stored it, and the describe never emitted it. A client+ * comparing its declared format against the live flow log therefore saw the field as unset every+ * time and proposed the same change on every plan.+ */+@QuarkusTest+class Ec2FlowLogFormatIntegrationTest {++ private static final String AUTH =+ "AWS4-HMAC-SHA256 Credential=test/20260908/us-east-1/ec2/aws4_request";+ private static final String FORMAT =+ "${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${action}";++ private static ValidatableResponse ec2(String action, String... formParams) {+ RequestSpecification request = given().header("Authorization", AUTH)+ .formParam("Action", action);+ for (int i = 0; i < formParams.length; i += 2) {+ request = request.formParam(formParams[i], formParams[i + 1]);+ }+ return request.when().post("/").then();+ }++ private static String vpc() {+ return ec2("CreateVpc", "CidrBlock", "10.71.0.0/16")+ .statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId");+ }++ /** DescribeFlowLogs serves no filters, so every assertion here names the flow log by id. */+ private static String createFlowLog(String vpcId, String... extraParams) {+ String[] params = {+ "ResourceId.1", vpcId,+ "ResourceType", "VPC",+ "TrafficType", "ALL",+ "LogDestinationType", "s3",+ "LogDestination", "arn:aws:s3:::flow-logs-bucket"+ };+ String[] all = new String[params.length + extraParams.length];+ System.arraycopy(params, 0, all, 0, params.length);+ System.arraycopy(extraParams, 0, all, params.length, extraParams.length);+ return ec2("CreateFlowLogs", all)+ .statusCode(200).extract().path("CreateFlowLogsResponse.flowLogIdSet.item");+ }++ @Test+ void aCustomLogFormatSurvivesTheRoundTrip() {+ String flowLogId = createFlowLog(vpc(), "LogFormat", FORMAT);++ ec2("DescribeFlowLogs", "FlowLogId.1", flowLogId)+ .statusCode(200)+ .body("DescribeFlowLogsResponse.flowLogSet.item.flowLogId", equalTo(flowLogId))+ .body("DescribeFlowLogsResponse.flowLogSet.item.logFormat", equalTo(FORMAT));+ }++ /**+ * A flow log created without a LogFormat reports the default one.+ *+ * <p>The model says CreateFlowLogs builds it with the default format when the parameter is+ * omitted, so the flow log has a format from that point on and DescribeFlowLogs reports it.+ */+ @Test+ void noCustomFormatReportsTheDefault() {+ String flowLogId = createFlowLog(vpc());++ ec2("DescribeFlowLogs", "FlowLogId.1", flowLogId)+ .statusCode(200)+ .body("DescribeFlowLogsResponse.flowLogSet.item.flowLogId", equalTo(flowLogId))+ .body("DescribeFlowLogsResponse.flowLogSet.item.logFormat",+ equalTo(FlowLogService.DEFAULT_LOG_FORMAT));+ }+}