mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): report a flow log's custom LogFormat (#4386)
* fix(ec2): report a flow log's custom LogFormat CreateFlowLogs parsed LogFormat and stored it on the flow log, and DescribeFlowLogs never emitted it. A client comparing its declared format against the live flow log saw the field unset every time and proposed the same change on every plan. XmlBuilder.elem skips a null, so a flow log created without a custom format reports no element rather than an empty one. LogGroupName is also in the model's FlowLog shape and is not parsed, stored or emitted here. That is a missing input rather than a dropped round-trip, and it needs a decision about how it sits alongside LogDestination, so it is left out of this change. * test(ec2): address the flow log format assertions by id DescribeFlowLogs serves no filters, so the resource-id filter these tests passed was ignored and both described every flow log in the region. The negative test then found the other test's custom format whenever it ran second. Capture the id CreateFlowLogs returns and describe that one log. * test(ec2): validate the flow log format with the EC2 SDK client AGENTS.md asks for management APIs to be exercised through an SDK client rather than handcrafted requests alone. Ec2Client.describeFlowLogs decodes logFormat as the field the create call set, and reports null when the flow log was created without one. * test(ec2): tolerate a null id when cleaning up the flow log test List.of rejects a null, so a failed CreateFlowLogs would have thrown from the finally block and left the VPC behind, masking the real failure. * fix(ec2): report the default log format when none was set CreateFlowLogs builds the flow log with the default format when LogFormat is omitted, so the flow log carries one from that point and DescribeFlowLogs reports it. Omitting the element left the same gap this branch set out to close, one step further in. DEFAULT_LOG_FORMAT is the version 2 fields in the order AWS documents. It is separate from DEFAULT_HEADER, which is the alphabetised superset written as the header line of a generated log file. * docs(ec2): restore the DEFAULT_HEADER javadoc the new constant displaced The new constant landed between DEFAULT_HEADER and its comment, leaving that comment attached to nothing. It also called the value a version 5 field order, which it is not. It is every available field in alphabetical order.
This commit is contained in:
1 parent
c08e594f49
commit
fd3696a9fc
4 files changed
+162
-1
No files matched your search
+64
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()))
|
||||
|
||||
@@ -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 "
|
||||
|
||||
+82
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user