Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ec2): keep the tags CreateFlowLogs is given (#6890)

Allen Sanborn8ececd84b4b5
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<Tag> 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<Tag> 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));
+ }
+ }
+ }
+}