mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(cloudformation): validate WAFv2 WebACL and Config rule required properties (#3576)
Fail fast on missing required properties, read WebACL tags via ListTagsForResource, delete Config rules by name through CfnDeletes and keep absent ComplianceResourceTypes unset. * Fix #2711 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
569f9f55e4
commit
36c8396f25
5 files changed
+284
-27
No files matched your search
+13
-10
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
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;
|
||||
@@ -38,6 +39,9 @@ public class ConfigCfnProvisioner implements CfnResourceProvisioner {
|
||||
@Override
|
||||
public void provision(StackResource resource, JsonNode props, ProvisionContext ctx) {
|
||||
JsonNode resolved = ctx.engine().resolveNode(props);
|
||||
if (!resolved.path("Source").isObject()) {
|
||||
throw new AwsException("ValidationError", CONFIG_RULE + " requires Source", 400);
|
||||
}
|
||||
String previousName = resource.getPhysicalId();
|
||||
String name = text(resolved, "ConfigRuleName");
|
||||
if (name == null || name.isBlank()) {
|
||||
@@ -55,7 +59,7 @@ public class ConfigCfnProvisioner implements CfnResourceProvisioner {
|
||||
resource.getAttributes().put("ConfigRuleId", rule.configRuleId());
|
||||
if (previousName != null && !previousName.equals(name)) {
|
||||
try {
|
||||
deleteIfPresent(ctx.region(), previousName);
|
||||
deleteRule(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}",
|
||||
@@ -66,17 +70,15 @@ public class ConfigCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
deleteIfPresent(region, physicalId);
|
||||
deleteRule(region, physicalId);
|
||||
}
|
||||
|
||||
private void deleteIfPresent(String region, String name) {
|
||||
private void deleteRule(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);
|
||||
}
|
||||
CfnDeletes.safeDelete("Config rule", name, () -> configService.deleteConfigRule(region, name),
|
||||
"NoSuchConfigRuleException");
|
||||
}
|
||||
|
||||
private Scope scope(JsonNode node) {
|
||||
@@ -116,10 +118,11 @@ public class ConfigCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
private List<String> strings(JsonNode nodes) {
|
||||
List<String> values = new ArrayList<>();
|
||||
if (nodes.isArray()) {
|
||||
nodes.forEach(node -> values.add(node.asText()));
|
||||
if (!nodes.isArray()) {
|
||||
return null;
|
||||
}
|
||||
List<String> values = new ArrayList<>();
|
||||
nodes.forEach(node -> values.add(node.asText()));
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
+12
-2
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
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;
|
||||
@@ -35,6 +36,9 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
@Override
|
||||
public void provision(StackResource resource, JsonNode props, ProvisionContext ctx) {
|
||||
JsonNode resolved = ctx.engine().resolveNode(props);
|
||||
String scope = required(text(resolved, "Scope"), "Scope");
|
||||
required(raw(resolved, "DefaultAction"), "DefaultAction");
|
||||
required(raw(resolved, "VisibilityConfig"), "VisibilityConfig");
|
||||
String name = text(resolved, "Name");
|
||||
WebAcl existing = findExisting(resource.getPhysicalId());
|
||||
if (name == null || name.isBlank()) {
|
||||
@@ -42,7 +46,6 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
? 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)) {
|
||||
@@ -107,8 +110,15 @@ public class WafV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
return acl;
|
||||
}
|
||||
|
||||
private static String required(String value, String property) {
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new AwsException("ValidationError", WEB_ACL + " requires " + property, 400);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private void reconcileTags(WebAcl existing, Map<String, String> desired) {
|
||||
List<String> removed = existing.getTags().keySet().stream()
|
||||
List<String> removed = wafV2Service.listTagsForResource(existing.getArn()).keySet().stream()
|
||||
.filter(key -> !desired.containsKey(key)).toList();
|
||||
if (!removed.isEmpty()) {
|
||||
wafV2Service.untagResource(existing.getArn(), removed);
|
||||
|
||||
Reference in new issue
Block a user