fix(ec2): carry DeliverLogsPermissionArn through CreateFlowLogs and DescribeFlowLogs (#3172)

A flow log with a CloudWatch Logs destination delivers through an IAM
role given as DeliverLogsPermissionArn. The value was dropped on create
and never returned on describe, so Terraform read aws_flow_log's
iam_role_arn back empty and proposed replacing the flow log on every
plan. The Query handler and the AWS::EC2::FlowLog provisioner now pass
it through, DescribeFlowLogs returns it, and the provisioner treats a
change to it as the replacement AWS performs.

Ported from lex00/floci#96.
This commit is contained in:
lex00 authored and GitHub committed 2026-09-07 09:03:44 -04:00
1 parent e7256f5556
commit acc0904be4
7 files changed
+104 -13

No files matched your search

@@ -62,15 +62,17 @@ class Ec2FlowLogCfnProvisionerTest {
@Test
void flowLogSetsPhysicalIdAndIdAttribute() {
when(flowLogs.createFlowLog(eq("us-east-1"), eq("vpc-123"), eq("VPC"), eq("ALL"),
eq("s3"), eq("arn:aws:s3:::flow-bucket"), eq("${srcaddr}"), eq(60)))
eq("cloud-watch-logs"), eq("arn:aws:logs:us-east-1:000000000000:log-group:flows"),
eq("arn:aws:iam::000000000000:role/flow-logs-role"), eq("${srcaddr}"), eq(60)))
.thenReturn(flowLog("fl-0abc"));
StackResource r = resource();
ObjectNode props = mapper.createObjectNode()
.put("ResourceId", "vpc-123")
.put("ResourceType", "VPC")
.put("TrafficType", "ALL")
.put("LogDestinationType", "s3")
.put("LogDestination", "arn:aws:s3:::flow-bucket")
.put("LogDestinationType", "cloud-watch-logs")
.put("LogDestination", "arn:aws:logs:us-east-1:000000000000:log-group:flows")
.put("DeliverLogsPermissionArn", "arn:aws:iam::000000000000:role/flow-logs-role")
.put("LogFormat", "${srcaddr}")
.put("MaxAggregationInterval", 60);
@@ -82,12 +84,12 @@ class Ec2FlowLogCfnProvisionerTest {
@Test
void omittedAggregationIntervalDefaultsToTenMinutes() {
when(flowLogs.createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), anyInt()))
when(flowLogs.createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), any(), anyInt()))
.thenReturn(flowLog("fl-0def"));
provisioner.provision(resource(), mapper.createObjectNode().put("ResourceId", "vpc-9"), ctx());
verify(flowLogs).createFlowLog("us-east-1", "vpc-9", null, null, null, null, null, 600);
verify(flowLogs).createFlowLog("us-east-1", "vpc-9", null, null, null, null, null, null, 600);
}
@Test
@@ -104,7 +106,7 @@ class Ec2FlowLogCfnProvisionerTest {
provisioner.provision(r, mapper.createObjectNode().put("ResourceId", "vpc-123"), ctx());
// A second flow log here outlives the stack: delete only knows the id recorded last.
verify(flowLogs, never()).createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), anyInt());
verify(flowLogs, never()).createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), any(), anyInt());
assertEquals("fl-0abc", r.getPhysicalId());
assertEquals("fl-0abc", r.getAttributes().get("Id"));
}
@@ -123,7 +125,23 @@ class Ec2FlowLogCfnProvisionerTest {
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r,
mapper.createObjectNode().put("ResourceId", "vpc-123").put("TrafficType", "REJECT"), ctx()));
assertEquals("ValidationError", e.getErrorCode());
verify(flowLogs, never()).createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), anyInt());
verify(flowLogs, never()).createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), any(), anyInt());
}
@Test
void changingTheDeliverLogsPermissionArnReportsAnUnsupportedReplacement() {
StackResource r = resource();
r.setPhysicalId("fl-0abc");
FlowLog existing = flowLog("fl-0abc");
existing.setResourceId("vpc-123");
existing.setDeliverLogsPermissionArn("arn:aws:iam::000000000000:role/old-role");
when(flowLogs.describeFlowLogs("us-east-1", List.of("fl-0abc"))).thenReturn(List.of(existing));
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r,
mapper.createObjectNode().put("ResourceId", "vpc-123")
.put("DeliverLogsPermissionArn", "arn:aws:iam::000000000000:role/new-role"), ctx()));
assertEquals("ValidationError", e.getErrorCode());
verify(flowLogs, never()).createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), any(), anyInt());
}
@Test
@@ -141,7 +159,7 @@ class Ec2FlowLogCfnProvisionerTest {
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r,
mapper.createObjectNode().put("ResourceId", "vpc-123"), ctx()));
assertEquals("ValidationError", e.getErrorCode());
verify(flowLogs, never()).createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), anyInt());
verify(flowLogs, never()).createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), any(), anyInt());
}
@Test
@@ -160,7 +178,7 @@ class Ec2FlowLogCfnProvisionerTest {
provisioner.provision(r, mapper.createObjectNode().put("ResourceId", "vpc-123"), ctx());
assertEquals("fl-0abc", r.getPhysicalId());
verify(flowLogs, never()).createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), anyInt());
verify(flowLogs, never()).createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), any(), anyInt());
}
@Test
@@ -183,7 +201,7 @@ class Ec2FlowLogCfnProvisionerTest {
StackResource r = resource();
r.setPhysicalId("fl-gone");
when(flowLogs.describeFlowLogs("us-east-1", List.of("fl-gone"))).thenReturn(List.of());
when(flowLogs.createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), anyInt()))
when(flowLogs.createFlowLog(anyString(), any(), any(), any(), any(), any(), any(), any(), anyInt()))
.thenReturn(flowLog("fl-new"));
provisioner.provision(r, mapper.createObjectNode().put("ResourceId", "vpc-123"), ctx());
@@ -25,6 +25,7 @@ class Ec2FlowLogIntegrationTest {
private static String vpcId;
private static String flowLogId;
private static String cloudWatchFlowLogId;
// =========================================================================
// Fixture: a VPC to attach the flow log to
@@ -119,6 +120,54 @@ class Ec2FlowLogIntegrationTest {
@Test
@Order(13)
void createFlowLogsWithACloudWatchDestinationKeepsTheDeliverLogsPermissionArn() {
// Terraform reads aws_flow_log.iam_role_arn back from DescribeFlowLogs. A dropped ARN
// reads as empty and every later plan proposes replacing the flow log.
cloudWatchFlowLogId = given()
.formParam("Action", "CreateFlowLogs")
.formParam("ResourceType", "VPC")
.formParam("ResourceId.1", vpcId)
.formParam("TrafficType", "ALL")
.formParam("LogDestinationType", "cloud-watch-logs")
.formParam("LogDestination", "arn:aws:logs:us-east-1:000000000000:log-group:/aws/vpc/flow-logs")
.formParam("DeliverLogsPermissionArn", "arn:aws:iam::000000000000:role/flow-logs-role")
.formParam("MaxAggregationInterval", "600")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("CreateFlowLogsResponse.flowLogIdSet.item[0]", startsWith("fl-"))
.extract().path("CreateFlowLogsResponse.flowLogIdSet.item[0]");
given()
.formParam("Action", "DescribeFlowLogs")
.formParam("FlowLogId.1", cloudWatchFlowLogId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("DescribeFlowLogsResponse.flowLogSet.item[0].flowLogId", equalTo(cloudWatchFlowLogId))
.body("DescribeFlowLogsResponse.flowLogSet.item[0].logDestinationType", equalTo("cloud-watch-logs"))
.body("DescribeFlowLogsResponse.flowLogSet.item[0].deliverLogsPermissionArn",
equalTo("arn:aws:iam::000000000000:role/flow-logs-role"));
given()
.formParam("Action", "DeleteFlowLogs")
.formParam("FlowLogId.1", cloudWatchFlowLogId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml");
}
@Test
@Order(14)
void deleteFlowLogs() {
given()
.formParam("Action", "DeleteFlowLogs")
@@ -33,7 +33,7 @@ class FlowLogServiceTest {
@Test
void deleteFlowLogsIsScopedToTheRequestRegion() {
FlowLog fl = flowLogService.createFlowLog("us-east-1", "vpc-123", "VPC", "ALL",
"s3", "arn:aws:s3:::flow-bucket", null, 600);
"s3", "arn:aws:s3:::flow-bucket", null, null, 600);
List<String> otherRegion = flowLogService.deleteFlowLogs("eu-west-1", List.of(fl.getFlowLogId()));
@@ -46,6 +46,17 @@ class FlowLogServiceTest {
assertTrue(flowLogService.describeFlowLogs("us-east-1", List.of()).isEmpty());
}
@Test
void createFlowLogKeepsTheDeliverLogsPermissionArn() {
FlowLog fl = flowLogService.createFlowLog("us-east-1", "vpc-123", "VPC", "ALL",
"cloud-watch-logs", "arn:aws:logs:us-east-1:000000000000:log-group:flows",
"arn:aws:iam::000000000000:role/flow-logs-role", null, 600);
FlowLog described = flowLogService.describeFlowLogs("us-east-1", List.of(fl.getFlowLogId())).get(0);
assertEquals("arn:aws:iam::000000000000:role/flow-logs-role", described.getDeliverLogsPermissionArn());
}
@Test
void deleteFlowLogsIgnoresUnknownIds() {
assertTrue(flowLogService.deleteFlowLogs("us-east-1", List.of("fl-doesnotexist")).isEmpty());