mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(cloudformation): provision WAFv2 web ACLs and AWS Config rules (#2701)
* feat: provision WAFv2 web ACLs
* feat: provision AWS Config rules
* fix: adapt provisioners to current 3-arg CfnResourceProvisioner.delete()
* fix: preserve new-resource tracking when replacement cleanup fails, and stop dropping inline-object InputParameters
Local code review (review-local.ts) flagged two ordering bugs on the replacement
path in both provisioners: the new resource's physicalId/attributes were only
recorded after the old resource's delete call succeeded, so a delete failure
(e.g. WAFAssociatedItemException, or a Config rule still referenced elsewhere)
left the stack tracking the old resource while the newly created one leaked
untracked. Both now record the new resource first and treat old-resource
cleanup failure as non-fatal, matching the existing precedent in
Ec2LaunchTemplateCfnProvisioner.
Also fixed AWS::Config::ConfigRule.InputParameters being silently dropped
when a template supplies it as an inline JSON object (the common case) rather
than a pre-stringified JSON string: JsonNode.asText() returns empty string
for object nodes.
* fix(cloudformation): preserve intrinsic-resolved JSON fields on WAFv2 WebACL, log tolerated replacement-cleanup failures, add GetAtt coverage
raw() in WafV2CfnProvisioner used JsonNode#isEmpty(), which is true for every
scalar node, so any field CloudFormationTemplateEngine#resolveNode collapsed
into a TextNode (e.g. an Fn::Join-built DefaultAction) silently became null
instead of the literal JSON text. Mirror ConfigCfnProvisioner's existing
isTextual()-aware handling.
Also log (previously fully swallowed) exceptions when replacement cleanup of
the superseded WAFv2 WebACL or Config rule fails, so a leaked old resource is
diagnosable, and add an integration test asserting the exact Fn::GetAtt keys
for both provisioners resolve through CloudFormation to the real resource.
Config's PutConfigRule call being unconditional on every provision is correct
AWS upsert semantics ("Adds or updates" per the Config API), not a missing
create/update branch.
This commit is contained in:
1 parent
a1383fdd3d
commit
8a705d0aae
6 files changed
+724
No files matched your search
+137
@@ -0,0 +1,137 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.configservice.AwsConfigService;
|
||||
import io.github.hectorvent.floci.services.configservice.model.ConfigRule;
|
||||
import io.github.hectorvent.floci.services.configservice.model.ConfigRuleSource;
|
||||
import io.github.hectorvent.floci.services.configservice.model.CustomPolicyDetails;
|
||||
import io.github.hectorvent.floci.services.configservice.model.EvaluationModeConfiguration;
|
||||
import io.github.hectorvent.floci.services.configservice.model.Scope;
|
||||
import io.github.hectorvent.floci.services.configservice.model.SourceDetail;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ConfigCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ConfigCfnProvisioner.class);
|
||||
private static final String CONFIG_RULE = "AWS::Config::ConfigRule";
|
||||
|
||||
private final AwsConfigService configService;
|
||||
|
||||
@Inject
|
||||
public ConfigCfnProvisioner(AwsConfigService configService) {
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of(CONFIG_RULE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource resource, JsonNode props, ProvisionContext ctx) {
|
||||
JsonNode resolved = ctx.engine().resolveNode(props);
|
||||
String previousName = resource.getPhysicalId();
|
||||
String name = text(resolved, "ConfigRuleName");
|
||||
if (name == null || name.isBlank()) {
|
||||
name = previousName == null
|
||||
? ctx.generatePhysicalName(resource.getLogicalId(), 128, false)
|
||||
: previousName;
|
||||
}
|
||||
ConfigRule rule = configService.putConfigRule(ctx.region(), new ConfigRule(
|
||||
name, null, null, text(resolved, "Description"), scope(resolved.path("Scope")),
|
||||
source(resolved.path("Source")), inputParameters(resolved.path("InputParameters")),
|
||||
text(resolved, "MaximumExecutionFrequency"), null, null,
|
||||
evaluationModes(resolved.path("EvaluationModes"))));
|
||||
resource.setPhysicalId(rule.configRuleName());
|
||||
resource.getAttributes().put("Arn", rule.configRuleArn());
|
||||
resource.getAttributes().put("ConfigRuleId", rule.configRuleId());
|
||||
if (previousName != null && !previousName.equals(name)) {
|
||||
try {
|
||||
deleteIfPresent(ctx.region(), previousName);
|
||||
} catch (RuntimeException e) {
|
||||
// old rule may still be referenced elsewhere; new rule is already tracked
|
||||
LOG.warnv("Config CFN replacement cleanup of rule {0} tolerated: {1}",
|
||||
previousName, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
deleteIfPresent(region, physicalId);
|
||||
}
|
||||
|
||||
private void deleteIfPresent(String region, String name) {
|
||||
if (name == null) {
|
||||
return;
|
||||
}
|
||||
if (configService.describeConfigRules(region, List.of()).stream()
|
||||
.anyMatch(rule -> name.equals(rule.configRuleName()))) {
|
||||
configService.deleteConfigRule(region, name);
|
||||
}
|
||||
}
|
||||
|
||||
private Scope scope(JsonNode node) {
|
||||
if (!node.isObject()) {
|
||||
return null;
|
||||
}
|
||||
return new Scope(strings(node.path("ComplianceResourceTypes")), text(node, "TagKey"),
|
||||
text(node, "TagValue"), text(node, "ComplianceResourceId"));
|
||||
}
|
||||
|
||||
private ConfigRuleSource source(JsonNode node) {
|
||||
List<SourceDetail> details = new ArrayList<>();
|
||||
JsonNode detailNodes = node.path("SourceDetails");
|
||||
if (detailNodes.isArray()) {
|
||||
for (JsonNode detail : detailNodes) {
|
||||
details.add(new SourceDetail(text(detail, "EventSource"), text(detail, "MessageType"),
|
||||
text(detail, "MaximumExecutionFrequency")));
|
||||
}
|
||||
}
|
||||
JsonNode policy = node.path("CustomPolicyDetails");
|
||||
CustomPolicyDetails customPolicy = policy.isObject()
|
||||
? new CustomPolicyDetails(text(policy, "PolicyRuntime"), text(policy, "PolicyText"),
|
||||
policy.has("EnableDebugLogDelivery")
|
||||
? policy.get("EnableDebugLogDelivery").asBoolean() : null)
|
||||
: null;
|
||||
return new ConfigRuleSource(text(node, "Owner"), text(node, "SourceIdentifier"),
|
||||
details.isEmpty() ? null : details, customPolicy);
|
||||
}
|
||||
|
||||
private List<EvaluationModeConfiguration> evaluationModes(JsonNode nodes) {
|
||||
if (!nodes.isArray()) {
|
||||
return null;
|
||||
}
|
||||
List<EvaluationModeConfiguration> modes = new ArrayList<>();
|
||||
nodes.forEach(node -> modes.add(new EvaluationModeConfiguration(text(node, "Mode"))));
|
||||
return modes;
|
||||
}
|
||||
|
||||
private List<String> strings(JsonNode nodes) {
|
||||
List<String> values = new ArrayList<>();
|
||||
if (nodes.isArray()) {
|
||||
nodes.forEach(node -> values.add(node.asText()));
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private String text(JsonNode node, String field) {
|
||||
JsonNode value = node == null ? null : node.get(field);
|
||||
return value == null || value.isNull() ? null : value.asText();
|
||||
}
|
||||
|
||||
private String inputParameters(JsonNode node) {
|
||||
if (node == null || node.isMissingNode() || node.isNull()) {
|
||||
return null;
|
||||
}
|
||||
return node.isTextual() ? node.asText() : node.toString();
|
||||
}
|
||||
}
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.wafv2.WafV2Service;
|
||||
import io.github.hectorvent.floci.services.wafv2.model.WebAcl;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationScoped
|
||||
public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(WafV2CfnProvisioner.class);
|
||||
private static final String WEB_ACL = "AWS::WAFv2::WebACL";
|
||||
|
||||
private final WafV2Service wafV2Service;
|
||||
|
||||
@Inject
|
||||
public WafV2CfnProvisioner(WafV2Service wafV2Service) {
|
||||
this.wafV2Service = wafV2Service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of(WEB_ACL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource resource, JsonNode props, ProvisionContext ctx) {
|
||||
JsonNode resolved = ctx.engine().resolveNode(props);
|
||||
String name = text(resolved, "Name");
|
||||
WebAcl existing = findExisting(resource.getPhysicalId());
|
||||
if (name == null || name.isBlank()) {
|
||||
name = existing == null
|
||||
? ctx.generatePhysicalName(resource.getLogicalId(), 128, false)
|
||||
: existing.getName();
|
||||
}
|
||||
String scope = text(resolved, "Scope");
|
||||
WebAcl desired = fromProperties(resolved);
|
||||
WebAcl acl;
|
||||
if (existing != null && existing.getName().equals(name) && existing.getScope().equals(scope)) {
|
||||
wafV2Service.updateWebAcl(desired, scope, existing.getId(), existing.getLockToken());
|
||||
acl = wafV2Service.getWebAcl(scope, existing.getId());
|
||||
reconcileTags(acl, desired.getTags());
|
||||
} else {
|
||||
acl = wafV2Service.createWebAcl(desired, scope, name, ctx.region());
|
||||
setReferences(resource, acl);
|
||||
if (existing != null) {
|
||||
try {
|
||||
wafV2Service.deleteWebAcl(existing.getScope(), existing.getId(), existing.getLockToken());
|
||||
} catch (RuntimeException e) {
|
||||
// old ACL may still be associated with a resource; new ACL is already tracked
|
||||
LOG.warnv("WAFv2 CFN replacement cleanup of {0}|{1}|{2} tolerated: {3}",
|
||||
existing.getName(), existing.getId(), existing.getScope(), e.getMessage());
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
setReferences(resource, acl);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
WebAcl existing = findExisting(physicalId);
|
||||
if (existing != null) {
|
||||
wafV2Service.deleteWebAcl(existing.getScope(), existing.getId(), existing.getLockToken());
|
||||
}
|
||||
}
|
||||
|
||||
private WebAcl fromProperties(JsonNode props) {
|
||||
WebAcl acl = new WebAcl();
|
||||
acl.setDescription(text(props, "Description"));
|
||||
acl.setDefaultAction(raw(props, "DefaultAction"));
|
||||
acl.setRules(raw(props, "Rules"));
|
||||
acl.setVisibilityConfig(raw(props, "VisibilityConfig"));
|
||||
acl.setCustomResponseBodies(raw(props, "CustomResponseBodies"));
|
||||
acl.setCaptchaConfig(raw(props, "CaptchaConfig"));
|
||||
acl.setChallengeConfig(raw(props, "ChallengeConfig"));
|
||||
acl.setAssociationConfig(raw(props, "AssociationConfig"));
|
||||
acl.setDataProtectionConfig(raw(props, "DataProtectionConfig"));
|
||||
List<String> tokenDomains = new ArrayList<>();
|
||||
JsonNode tokens = props.path("TokenDomains");
|
||||
if (tokens.isArray()) {
|
||||
tokens.forEach(token -> tokenDomains.add(token.asText()));
|
||||
}
|
||||
acl.setTokenDomains(tokenDomains);
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
JsonNode tagNodes = props.path("Tags");
|
||||
if (tagNodes.isArray()) {
|
||||
for (JsonNode tag : tagNodes) {
|
||||
String key = text(tag, "Key");
|
||||
if (key != null) {
|
||||
tags.put(key, text(tag, "Value"));
|
||||
}
|
||||
}
|
||||
}
|
||||
acl.setTags(tags);
|
||||
return acl;
|
||||
}
|
||||
|
||||
private void reconcileTags(WebAcl existing, Map<String, String> desired) {
|
||||
List<String> removed = existing.getTags().keySet().stream()
|
||||
.filter(key -> !desired.containsKey(key)).toList();
|
||||
if (!removed.isEmpty()) {
|
||||
wafV2Service.untagResource(existing.getArn(), removed);
|
||||
}
|
||||
if (!desired.isEmpty()) {
|
||||
wafV2Service.tagResource(existing.getArn(), desired);
|
||||
}
|
||||
}
|
||||
|
||||
private WebAcl findExisting(String physicalId) {
|
||||
String[] parts = physicalId == null ? new String[0] : physicalId.split("\\|", -1);
|
||||
if (parts.length != 3) {
|
||||
return null;
|
||||
}
|
||||
return wafV2Service.listWebAcls(parts[2]).stream()
|
||||
.filter(acl -> parts[1].equals(acl.getId()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private void setReferences(StackResource resource, WebAcl acl) {
|
||||
resource.setPhysicalId(acl.getName() + "|" + acl.getId() + "|" + acl.getScope());
|
||||
resource.getAttributes().put("Arn", acl.getArn());
|
||||
resource.getAttributes().put("Capacity", Long.toString(acl.getCapacity()));
|
||||
resource.getAttributes().put("Id", acl.getId());
|
||||
resource.getAttributes().put("LabelNamespace", acl.getLabelNamespace());
|
||||
}
|
||||
|
||||
private String text(JsonNode node, String field) {
|
||||
JsonNode value = node == null ? null : node.get(field);
|
||||
return value == null || value.isNull() ? null : value.asText();
|
||||
}
|
||||
|
||||
private String raw(JsonNode node, String field) {
|
||||
JsonNode value = node == null ? null : node.get(field);
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (value.isTextual()) {
|
||||
return value.asText();
|
||||
}
|
||||
return value.isEmpty() ? null : value.toString();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user