From 8ececd84b4b587fe25922dd6576ca4a79520d8e4 Mon Sep 17 00:00:00 2001 From: Allen Sanborn Date: Wed, 28 Oct 2026 21:58:24 -0500 Subject: [PATCH] fix(ec2): keep the tags CreateFlowLogs is given (#6890) * fix(ec2): keep the tags CreateFlowLogs is given CreateFlowLogs dropped its vpc-flow-log TagSpecification, and DescribeFlowLogs had no tagSet, so Terraform's aws_flow_log tags never read back and every plan after apply wanted to add them again. The tags are now stored with the flow log and returned. * test(ec2): read a flow log's tags back through the AWS SDK --- docs/services/ec2.md | 2 +- .../floci/services/ec2/Ec2QueryHandler.java | 5 ++ .../ec2/Ec2FlowLogTagsIntegrationTest.java | 83 +++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/ec2/Ec2FlowLogTagsIntegrationTest.java diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 8a6599894b..adf1c6db25 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -1149,7 +1149,7 @@ Validation matches AWS behavior: | Action | Description | |--------|-------------| -| CreateFlowLogs | Creates flow logs for the given resources; returns one flow log id per resource. | +| CreateFlowLogs | Creates flow logs for the given resources; returns one flow log id per resource. A `vpc-flow-log` `TagSpecification` tags each one, and `DescribeFlowLogs` returns the tags. | | DescribeFlowLogs | Lists stored flow logs, optionally filtered by id. | | DeleteFlowLogs | Deletes the named flow logs. | 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.java index c5b10a9e25..92959af7da 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 @@ -1994,9 +1994,13 @@ public class Ec2QueryHandler { .start("CreateFlowLogsResponse", AwsNamespaces.EC2) .elem("requestId", UUID.randomUUID().toString()) .start("flowLogIdSet"); + List flowLogTags = parseTagsForResource(p, "vpc-flow-log"); for (String resourceId : resourceIds) { FlowLog fl = flowLogService.createFlowLog(region, resourceId, resourceType, trafficType, logDestinationType, logDestination, deliverLogsPermissionArn, logFormat, maxAgg); + if (!flowLogTags.isEmpty()) { + service.createTags(region, List.of(fl.getFlowLogId()), flowLogTags); + } xml.elem("item", fl.getFlowLogId()); } xml.end("flowLogIdSet") @@ -2029,6 +2033,7 @@ public class Ec2QueryHandler { .elem("deliverLogsStatus", fl.getDeliverLogsStatus()) .elem("maxAggregationInterval", String.valueOf(fl.getMaxAggregationInterval())) .elem("creationTime", ISO_FMT.format(fl.getCreationTime())) + .raw(tagSetXml(service.resourceTags(region, fl.getFlowLogId()))) .end("item"); } xml.end("flowLogSet").end("DescribeFlowLogsResponse"); diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2FlowLogTagsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2FlowLogTagsIntegrationTest.java new file mode 100644 index 0000000000..b7f5645857 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2FlowLogTagsIntegrationTest.java @@ -0,0 +1,83 @@ +package io.github.hectorvent.floci.services.ec2; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.RestAssured; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.ec2.Ec2Client; +import software.amazon.awssdk.services.ec2.model.FlowLogsResourceType; +import software.amazon.awssdk.services.ec2.model.ResourceType; +import software.amazon.awssdk.services.ec2.model.Tag; +import software.amazon.awssdk.services.ec2.model.TrafficType; + +import java.net.URI; +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.xml.HasXPath.hasXPath; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * CreateFlowLogs dropped its TagSpecification, so Terraform's aws_flow_log tags never read back and + * every plan after apply wanted to add them again. + */ +@QuarkusTest +class Ec2FlowLogTagsIntegrationTest { + + @Test + void aFlowLogKeepsTheTagsItWasCreatedWith() { + String vpcId = given().formParam("Action", "CreateVpc").formParam("CidrBlock", "10.21.0.0/16") + .when().post("/").then().statusCode(200).extract().xmlPath().getString("CreateVpcResponse.vpc.vpcId"); + String flowLogId = given() + .formParam("Action", "CreateFlowLogs") + .formParam("ResourceType", "VPC") + .formParam("ResourceId.1", vpcId) + .formParam("TrafficType", "ALL") + .formParam("LogDestinationType", "s3") + .formParam("LogDestination", "arn:aws:s3:::flow-logs-test-bucket") + .formParam("TagSpecification.1.ResourceType", "vpc-flow-log") + .formParam("TagSpecification.1.Tag.1.Key", "Name") + .formParam("TagSpecification.1.Tag.1.Value", "kthw") + .when().post("/").then().statusCode(200).extract().xmlPath() + .getString("CreateFlowLogsResponse.flowLogIdSet.item"); + + given().formParam("Action", "DescribeFlowLogs").formParam("FlowLogId.1", flowLogId) + .when().post("/").then().statusCode(200) + .body(hasXPath("//*[local-name()='flowLogSet']/*[local-name()='item']/*[local-name()='tagSet']" + + "/*[local-name()='item']/*[local-name()='value']", equalTo("kthw"))); + } + + @Test + void awsSdkReadsBackTheTagsAFlowLogWasCreatedWith() { + try (Ec2Client client = Ec2Client.builder() + .endpointOverride(URI.create("http://localhost:" + RestAssured.port)) + .region(Region.US_EAST_1) + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))) + .httpClientBuilder(UrlConnectionHttpClient.builder()).build()) { + String vpcId = client.createVpc(r -> r.cidrBlock("10.22.0.0/16")).vpc().vpcId(); + String flowLogId = client.createFlowLogs(r -> r + .resourceIds(vpcId) + .resourceType(FlowLogsResourceType.VPC) + .trafficType(TrafficType.ALL) + .logDestinationType("s3") + .logDestination("arn:aws:s3:::flow-logs-test-bucket") + .tagSpecifications(t -> t.resourceType(ResourceType.VPC_FLOW_LOG) + .tags(Tag.builder().key("Name").value("kthw").build(), + Tag.builder().key("env").value("test").build()))) + .flowLogIds().getFirst(); + try { + List tags = client.describeFlowLogs(r -> r.flowLogIds(flowLogId)) + .flowLogs().getFirst().tags(); + assertEquals(List.of(Tag.builder().key("Name").value("kthw").build(), + Tag.builder().key("env").value("test").build()), tags); + } finally { + client.deleteFlowLogs(r -> r.flowLogIds(flowLogId)); + client.deleteVpc(r -> r.vpcId(vpcId)); + } + } + } +}