mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ec2): keep a flow log's CloudWatch log group and default its destination type (#7197)
* fix(ec2): keep a flow log's CloudWatch log group and default its destination type CreateFlowLogs dropped LogGroupName, so DescribeFlowLogs never returned it and the Terraform provider saw a perpetual diff. Store it (deriving it from a log-group ARN LogDestination when only that is given), return it in DescribeFlowLogs, and default LogDestinationType to cloud-watch-logs when a log group is supplied without a type. * fix(ec2): only a CloudWatch Logs ARN implies a flow log's log group A LogDestination was treated as CloudWatch whenever it contained ':log-group:', so an S3 prefix such as arn:aws:s3:::bkt/archive:log-group:x/ with LogDestinationType omitted became cloud-watch-logs and was never delivered. Parse the ARN and require the logs service. The CloudFormation update check defaulted an omitted LogDestinationType to s3 while create now derives cloud-watch-logs from a log-group ARN, so an unchanged stack update was rejected as a replacement. Both now go through one effectiveLogDestinationType. * fix(ec2): a logs ARN with no group name is not a CloudWatch flow log destination logGroupFromArn returned an empty name for arn:aws:logs:...:log-group: (and log-group::*), so the destination type became cloud-watch-logs with a blank group. Treat a blank name as no group, so the type falls back to s3. * test(ec2): use the shared InMemoryStorageFactory in the flow log CFN test Replaces the hand-rolled StorageFactory mock with the existing test factory, which hands out the same account-scoped in-memory store.
This commit is contained in:
1 parent
7bae152c69
commit
3e0d8d640b
7 files changed
+177
-4
No files matched your search
+2
-2
@@ -25,7 +25,6 @@ public class Ec2FlowLogCfnProvisioner implements CfnResourceProvisioner {
|
||||
/** The defaults createFlowLog applies when the template omits these, kept in step with it. */
|
||||
private static final String DEFAULT_RESOURCE_TYPE = "VPC";
|
||||
private static final String DEFAULT_TRAFFIC_TYPE = "ALL";
|
||||
private static final String DEFAULT_LOG_DESTINATION_TYPE = "s3";
|
||||
|
||||
private final FlowLogService flowLogService;
|
||||
|
||||
@@ -90,7 +89,8 @@ public class Ec2FlowLogCfnProvisioner implements CfnResourceProvisioner {
|
||||
rejectIfChanged("TrafficType", existing.getTrafficType(),
|
||||
orDefault(ctx.resolveOptional(props, "TrafficType"), DEFAULT_TRAFFIC_TYPE));
|
||||
rejectIfChanged("LogDestinationType", existing.getLogDestinationType(),
|
||||
orDefault(ctx.resolveOptional(props, "LogDestinationType"), DEFAULT_LOG_DESTINATION_TYPE));
|
||||
FlowLogService.effectiveLogDestinationType(ctx.resolveOptional(props, "LogDestinationType"),
|
||||
ctx.resolveOptional(props, "LogDestination"), null));
|
||||
rejectIfChanged("LogDestination", existing.getLogDestination(),
|
||||
ctx.resolveOptional(props, "LogDestination"));
|
||||
rejectIfChanged("DeliverLogsPermissionArn", existing.getDeliverLogsPermissionArn(),
|
||||
|
||||
@@ -2015,6 +2015,7 @@ public class Ec2QueryHandler {
|
||||
if (logDestination == null) {
|
||||
logDestination = p.getFirst("LogDestinationArn");
|
||||
}
|
||||
String logGroupName = p.getFirst("LogGroupName");
|
||||
String deliverLogsPermissionArn = p.getFirst("DeliverLogsPermissionArn");
|
||||
String logFormat = p.getFirst("LogFormat");
|
||||
int maxAgg = parseIntParam(p, "MaxAggregationInterval", 600);
|
||||
@@ -2034,7 +2035,7 @@ public class Ec2QueryHandler {
|
||||
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);
|
||||
logDestinationType, logDestination, logGroupName, deliverLogsPermissionArn, logFormat, maxAgg);
|
||||
if (!flowLogTags.isEmpty()) {
|
||||
service.createTags(region, List.of(fl.getFlowLogId()), flowLogTags);
|
||||
}
|
||||
@@ -2063,6 +2064,7 @@ public class Ec2QueryHandler {
|
||||
.elem("trafficType", fl.getTrafficType())
|
||||
.elem("logDestinationType", fl.getLogDestinationType())
|
||||
.elem("logDestination", fl.getLogDestination())
|
||||
.elem("logGroupName", fl.getLogGroupName())
|
||||
.elem("deliverLogsPermissionArn", fl.getDeliverLogsPermissionArn())
|
||||
.elem("flowLogStatus", fl.getFlowLogStatus())
|
||||
.elem("logFormat", fl.getLogFormat() != null
|
||||
|
||||
@@ -156,12 +156,23 @@ public class FlowLogService {
|
||||
String trafficType, String logDestinationType,
|
||||
String logDestination, String deliverLogsPermissionArn,
|
||||
String logFormat, int maxAggregationInterval) {
|
||||
return createFlowLog(region, resourceId, resourceType, trafficType, logDestinationType,
|
||||
logDestination, null, deliverLogsPermissionArn, logFormat, maxAggregationInterval);
|
||||
}
|
||||
|
||||
public FlowLog createFlowLog(String region, String resourceId, String resourceType,
|
||||
String trafficType, String logDestinationType,
|
||||
String logDestination, String logGroupName,
|
||||
String deliverLogsPermissionArn,
|
||||
String logFormat, int maxAggregationInterval) {
|
||||
String groupName = logGroupName != null ? logGroupName : logGroupFromArn(logDestination);
|
||||
FlowLog fl = new FlowLog();
|
||||
fl.setFlowLogId("fl-" + randomHex(17));
|
||||
fl.setResourceId(resourceId);
|
||||
fl.setResourceType(resourceType != null ? resourceType : "VPC");
|
||||
fl.setTrafficType(trafficType != null ? trafficType : "ALL");
|
||||
fl.setLogDestinationType(logDestinationType != null ? logDestinationType : "s3");
|
||||
fl.setLogDestinationType(effectiveLogDestinationType(logDestinationType, logDestination, logGroupName));
|
||||
fl.setLogGroupName(groupName);
|
||||
fl.setLogDestination(logDestination);
|
||||
fl.setDeliverLogsPermissionArn(deliverLogsPermissionArn);
|
||||
fl.setBucketName(bucketFromArn(logDestination));
|
||||
@@ -569,6 +580,34 @@ public class FlowLogService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The LogDestinationType a flow log gets: the requested one, else cloud-watch-logs when a log
|
||||
* group is named directly or by a CloudWatch Logs ARN, else s3. CloudFormation compares an
|
||||
* update against this, so create and compare cannot drift apart.
|
||||
*/
|
||||
public static String effectiveLogDestinationType(String logDestinationType, String logDestination,
|
||||
String logGroupName) {
|
||||
if (logDestinationType != null && !logDestinationType.isBlank()) {
|
||||
return logDestinationType;
|
||||
}
|
||||
return logGroupName != null || logGroupFromArn(logDestination) != null ? "cloud-watch-logs" : "s3";
|
||||
}
|
||||
|
||||
/**
|
||||
* The group named by a CloudWatch Logs ARN ({@code arn:aws:logs:r:a:log-group:name[:*]}), or
|
||||
* null for anything else. Only a {@code logs} ARN counts: an S3 prefix may itself contain
|
||||
* {@code :log-group:}.
|
||||
*/
|
||||
static String logGroupFromArn(String arn) {
|
||||
String resource = AwsArnUtils.resourceIfArnFor(arn, "logs").orElse("");
|
||||
if (!resource.startsWith("log-group:")) {
|
||||
return null;
|
||||
}
|
||||
String name = resource.substring("log-group:".length());
|
||||
name = name.endsWith(":*") ? name.substring(0, name.length() - 2) : name;
|
||||
return name.isBlank() ? null : name;
|
||||
}
|
||||
|
||||
static String bucketFromArn(String arn) {
|
||||
if (arn == null) {
|
||||
return null;
|
||||
|
||||
@@ -25,6 +25,7 @@ public class FlowLog {
|
||||
private String trafficType; // ALL | ACCEPT | REJECT
|
||||
private String logDestinationType; // s3 | cloud-watch-logs
|
||||
private String logDestination; // S3 bucket ARN, e.g. arn:aws:s3:::flow-logs-bucket
|
||||
private String logGroupName; // CloudWatch Logs group, for cloud-watch-logs flow logs
|
||||
private String deliverLogsPermissionArn; // IAM role ARN used for cloud-watch-logs delivery
|
||||
private String bucketName; // resolved bucket name from the ARN
|
||||
private String logFormat; // optional custom format string (default format used if null)
|
||||
@@ -53,6 +54,9 @@ public class FlowLog {
|
||||
public String getLogDestination() { return logDestination; }
|
||||
public void setLogDestination(String logDestination) { this.logDestination = logDestination; }
|
||||
|
||||
public String getLogGroupName() { return logGroupName; }
|
||||
public void setLogGroupName(String logGroupName) { this.logGroupName = logGroupName; }
|
||||
|
||||
public String getDeliverLogsPermissionArn() { return deliverLogsPermissionArn; }
|
||||
public void setDeliverLogsPermissionArn(String deliverLogsPermissionArn) {
|
||||
this.deliverLogsPermissionArn = deliverLogsPermissionArn;
|
||||
|
||||
Reference in new issue
Block a user