mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(cloudformation): support WAFv2 logging configuration (#6194)
* feat(cloudformation): support WAFv2 logging configuration * fix(cloudformation): honor WAFv2 logging replacement and rollback
This commit is contained in:
1 parent
42fd858311
commit
a8e49fbbaa
11 files changed
+607
-51
No files matched your search
+6
@@ -79,6 +79,12 @@ public final class CfnRollback {
|
||||
*/
|
||||
public static final String EVENT_INVOKE_CONFIG_SNAPSHOT_ATTR = "__FlociEventInvokeConfigSnapshot";
|
||||
|
||||
/** The prior WAFv2 logging configuration for an in-place update that may need rollback. */
|
||||
public static final String WAF_LOGGING_UPDATE_SNAPSHOT_ATTR = "__FlociWafLoggingUpdateSnapshot";
|
||||
|
||||
/** Marks an absent WAFv2 logging configuration that an in-place update recreated. */
|
||||
public static final String WAF_LOGGING_UPDATE_ABSENT_ATTR = "__FlociWafLoggingUpdateAbsent";
|
||||
|
||||
/**
|
||||
* Holds what a custom resource had before an update sent its handler an {@code Update}: the
|
||||
* physical id, the old and the attempted properties, the region and the {@code Data} attributes,
|
||||
|
||||
+82
-2
@@ -24,6 +24,7 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
private static final String WEB_ACL = "AWS::WAFv2::WebACL";
|
||||
private static final String WEB_ACL_ASSOCIATION = "AWS::WAFv2::WebACLAssociation";
|
||||
private static final String IP_SET = "AWS::WAFv2::IPSet";
|
||||
private static final String LOGGING_CONFIGURATION = "AWS::WAFv2::LoggingConfiguration";
|
||||
|
||||
private final WafV2Service wafV2Service;
|
||||
|
||||
@@ -34,7 +35,7 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of(WEB_ACL, WEB_ACL_ASSOCIATION, IP_SET);
|
||||
return Set.of(WEB_ACL, WEB_ACL_ASSOCIATION, IP_SET, LOGGING_CONFIGURATION);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,6 +57,10 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
provisionIpSet(resource, props, ctx);
|
||||
return;
|
||||
}
|
||||
if (LOGGING_CONFIGURATION.equals(resource.getResourceType())) {
|
||||
provisionLoggingConfiguration(resource, props, ctx);
|
||||
return;
|
||||
}
|
||||
JsonNode resolved = ctx.engine().resolveNode(props);
|
||||
String scope = required(text(resolved, "Scope"), WEB_ACL, "Scope");
|
||||
required(raw(resolved, "DefaultAction"), WEB_ACL, "DefaultAction");
|
||||
@@ -147,6 +152,54 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
ReplacementCleanup.record(resource, ctx, attributesBefore);
|
||||
}
|
||||
|
||||
private void provisionLoggingConfiguration(StackResource resource, JsonNode props, ProvisionContext ctx) {
|
||||
resource.getAttributes().remove(CfnRollback.WAF_LOGGING_UPDATE_SNAPSHOT_ATTR);
|
||||
resource.getAttributes().remove(CfnRollback.WAF_LOGGING_UPDATE_ABSENT_ATTR);
|
||||
JsonNode resolved = ctx.engine().resolveNode(props);
|
||||
String resourceArn = required(text(resolved, "ResourceArn"), LOGGING_CONFIGURATION, "ResourceArn");
|
||||
JsonNode destinations = resolved.path("LogDestinationConfigs");
|
||||
if (!destinations.isArray() || destinations.isEmpty()) {
|
||||
throw new AwsException("ValidationError", LOGGING_CONFIGURATION + " requires LogDestinationConfigs", 400);
|
||||
}
|
||||
Map<String, String> attributesBefore = new HashMap<>(resource.getAttributes());
|
||||
if (ctx.isUpdate() && resourceArn.equals(ctx.priorPhysicalId())) {
|
||||
try {
|
||||
String previous = wafV2Service.getLoggingConfiguration(resourceArn);
|
||||
if (previous == null) {
|
||||
resource.getAttributes().put(CfnRollback.WAF_LOGGING_UPDATE_ABSENT_ATTR, "true");
|
||||
} else {
|
||||
resource.getAttributes().put(CfnRollback.WAF_LOGGING_UPDATE_SNAPSHOT_ATTR, previous);
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
if (!"WAFNonexistentItemException".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
resource.getAttributes().put(CfnRollback.WAF_LOGGING_UPDATE_ABSENT_ATTR, "true");
|
||||
}
|
||||
} else {
|
||||
rejectExistingLoggingConfiguration(resourceArn);
|
||||
}
|
||||
wafV2Service.putLoggingConfiguration(resourceArn, resolved.toString());
|
||||
resource.setPhysicalId(resourceArn);
|
||||
resource.getAttributes().put("ManagedByFirewallManager", "false");
|
||||
ReplacementCleanup.record(resource, ctx, attributesBefore);
|
||||
}
|
||||
|
||||
private void rejectExistingLoggingConfiguration(String resourceArn) {
|
||||
try {
|
||||
if (wafV2Service.getLoggingConfiguration(resourceArn) == null) {
|
||||
return;
|
||||
}
|
||||
} catch (AwsException e) {
|
||||
if ("WAFNonexistentItemException".equals(e.getErrorCode())) {
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
throw new AwsException("WAFDuplicateItemException",
|
||||
"Logging configuration already exists for " + resourceArn, 400);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
if (WEB_ACL_ASSOCIATION.equals(resourceType)) {
|
||||
@@ -157,6 +210,10 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
deleteIpSet(physicalId);
|
||||
return;
|
||||
}
|
||||
if (LOGGING_CONFIGURATION.equals(resourceType)) {
|
||||
wafV2Service.deleteLoggingConfiguration(physicalId);
|
||||
return;
|
||||
}
|
||||
WebAcl existing = findExisting(physicalId);
|
||||
if (existing != null) {
|
||||
wafV2Service.deleteWebAcl(existing.getScope(), existing.getId(), existing.getName(),
|
||||
@@ -203,11 +260,18 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
@Override
|
||||
public UpdateCleanupResult completeUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.complete(resource, this::delete);
|
||||
UpdateCleanupResult cleanup = ReplacementCleanup.complete(resource, this::delete);
|
||||
if (!cleanup.applicable() && (resource.getAttributes().containsKey(CfnRollback.WAF_LOGGING_UPDATE_SNAPSHOT_ATTR)
|
||||
|| resource.getAttributes().containsKey(CfnRollback.WAF_LOGGING_UPDATE_ABSENT_ATTR))) {
|
||||
return new UpdateCleanupResult(true, true, null, 0, null);
|
||||
}
|
||||
return cleanup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearUpdate(StackResource resource) {
|
||||
resource.getAttributes().remove(CfnRollback.WAF_LOGGING_UPDATE_SNAPSHOT_ATTR);
|
||||
resource.getAttributes().remove(CfnRollback.WAF_LOGGING_UPDATE_ABSENT_ATTR);
|
||||
ReplacementCleanup.clear(resource);
|
||||
}
|
||||
|
||||
@@ -226,6 +290,22 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
if (IP_SET.equals(resource.getResourceType())) {
|
||||
return ReplacementCleanup.rollback(resource, this::delete);
|
||||
}
|
||||
if (LOGGING_CONFIGURATION.equals(resource.getResourceType())) {
|
||||
if (ReplacementCleanup.rollback(resource, this::delete)) {
|
||||
return true;
|
||||
}
|
||||
if (resource.getAttributes().containsKey(CfnRollback.WAF_LOGGING_UPDATE_ABSENT_ATTR)) {
|
||||
wafV2Service.deleteLoggingConfiguration(resource.getPhysicalId());
|
||||
resource.getAttributes().remove(CfnRollback.WAF_LOGGING_UPDATE_ABSENT_ATTR);
|
||||
return true;
|
||||
}
|
||||
String previous = resource.getAttributes().get(CfnRollback.WAF_LOGGING_UPDATE_SNAPSHOT_ATTR);
|
||||
if (previous != null) {
|
||||
wafV2Service.putLoggingConfiguration(resource.getPhysicalId(), previous);
|
||||
resource.getAttributes().remove(CfnRollback.WAF_LOGGING_UPDATE_SNAPSHOT_ATTR);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user