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:
lex00 authored and GitHub committed 2026-09-25 16:40:36 -04:00
1 parent c08e594f49
commit fd3696a9fc
4 files changed
+162 -1

No files matched your search

@@ -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 "