mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(config): add aggregation authorization CRUD (#3627)
* feat(config): add aggregation authorization CRUD PutAggregationAuthorization, DescribeAggregationAuthorizations and DeleteAggregationAuthorization were unrouted, so the JSON handler answered InvalidAction and aws_config_aggregate_authorization could not be created. Authorizations are keyed by (AuthorizedAccountId, AuthorizedAwsRegion) within the request region, persisted through StorageFactory like the other durable Config resources. Put is an upsert that preserves the ARN and creation time of an existing authorization, and accepts Tags, which land in the same tag store that ListTagsForResource reads. Delete of an absent authorization succeeds: the Config model declares no not-found error for that operation. * fix(config): remove aggregation-authorization tags on every delete attempt Tag cleanup ran only when the delete found an entry to remove. A delete that removed the authorization but did not reach the tags left them behind, and the retry that should have cleaned them up skipped the cleanup because the entry was already gone. Put reuses a deterministic ARN, so those tags reappeared on the next authorization for the same account and region. The ARN is now derived from the key instead of read off the removed entry, and its tags are dropped on every attempt. The authorization map is still persisted only when an entry was actually removed. * fix(config): apply PutAggregationAuthorization tags at creation only PutAggregationAuthorization is idempotent, and AWS applies request tags at creation only: a later Put that finds the authorization already there ignores its Tags and leaves the existing ones alone. Tagging the existing ARN on every Put let a local test retag by re-putting, which production would silently refuse. Return the existing authorization untouched and tag only the one just created. Tags still change through TagResource/UntagResource, which is the only way AWS offers. * fix(config): make aggregation authorization put/delete atomic putAggregationAuthorization did a get-then-put on the region map with nothing holding the two together, and deleteAggregationAuthorization removed-then- persisted the same way. Two concurrent Puts for one (AuthorizedAccountId, AuthorizedAwsRegion) could both find nothing and both create: the second overwrote the first's CreationTime and re-applied creation tags over a set TagResource may already have changed, so the documented idempotency held only probabilistically. Both now take the same per-key lock putConfigRule/deleteConfigRule already use for rules. The lock keys are namespaced apart from the rule keys in the shared map rather than relying on rule names never containing '|'. The regression runs many short rounds rather than one wide one, because the unlocked window is a few instructions: repeated fresh starts land the race, more threads on one start do not. It asserts reference identity, which is what distinguishes the two cases -- the store is keyed on the pair so its size is 1 either way, the ARN is derived from the key so it is equal either way, and CreationTime is epoch seconds so a racing pair created inside one second is indistinguishable by timestamp. Verified to fail with the lock removed (17 mismatches) and pass with it, five runs.
This commit is contained in:
1 parent
026abe4878
commit
97cbce205b
6 files changed
+718
-5
No files matched your search
+110
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackedMap;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.configservice.model.AggregationAuthorization;
|
||||
import io.github.hectorvent.floci.services.configservice.model.Compliance;
|
||||
import io.github.hectorvent.floci.services.configservice.model.ComplianceByConfigRule;
|
||||
import io.github.hectorvent.floci.services.configservice.model.ComplianceByResource;
|
||||
@@ -40,6 +41,7 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
@@ -70,6 +72,10 @@ public class AwsConfigService {
|
||||
private static final int MAX_COMPLIANCE_RESOURCE_TYPES = 100;
|
||||
/** {@code DescribeConfigRulesRequest.ConfigRuleNames} is {@code max: 25}. */
|
||||
private static final int MAX_DESCRIBE_CONFIG_RULE_NAMES = 25;
|
||||
/** {@code DescribeAggregationAuthorizationsRequest.Limit} is {@code max: 100}. */
|
||||
private static final int MAX_AGGREGATION_AUTHORIZATIONS_LIMIT = 100;
|
||||
/** {@code AuthorizedAccountId} is modeled with {@code pattern: \d{12}}. */
|
||||
private static final Pattern AUTHORIZED_ACCOUNT_ID_PATTERN = Pattern.compile("\\d{12}");
|
||||
private static final int RULE_CONTRIBUTOR_CAP = 25;
|
||||
private static final int RESOURCE_CONTRIBUTOR_CAP = 100;
|
||||
|
||||
@@ -80,6 +86,8 @@ public class AwsConfigService {
|
||||
private Map<String, Map<String, ConfigRule>> configRules = new ConcurrentHashMap<>();
|
||||
// region -> packName -> pack (nested)
|
||||
private Map<String, Map<String, ConformancePack>> conformancePacks = new ConcurrentHashMap<>();
|
||||
// region -> authorizationKey("accountId|region") -> authorization (nested)
|
||||
private Map<String, Map<String, AggregationAuthorization>> aggregationAuthorizations = new ConcurrentHashMap<>();
|
||||
// region -> ruleName -> resourceKey("Type|Id") -> evaluation (doubly nested)
|
||||
private Map<String, Map<String, Map<String, ConfigEvaluation>>> evaluations = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -120,6 +128,8 @@ public class AwsConfigService {
|
||||
new TypeReference<Map<String, Map<String, ConfigRule>>>() {});
|
||||
this.conformancePacks = storageBacked("config-conformance-packs.json",
|
||||
new TypeReference<Map<String, Map<String, ConformancePack>>>() {});
|
||||
this.aggregationAuthorizations = storageBacked("config-aggregation-authorizations.json",
|
||||
new TypeReference<Map<String, Map<String, AggregationAuthorization>>>() {});
|
||||
this.evaluations = storageBacked("config-evaluations.json",
|
||||
new TypeReference<Map<String, Map<String, Map<String, ConfigEvaluation>>>>() {});
|
||||
this.configurationRecorders = storageBacked("config-recorders.json",
|
||||
@@ -132,6 +142,7 @@ public class AwsConfigService {
|
||||
new TypeReference<Map<String, Map<String, String>>>() {});
|
||||
normalizeRegionMaps(configRules);
|
||||
normalizeRegionMaps(conformancePacks);
|
||||
normalizeRegionMaps(aggregationAuthorizations);
|
||||
normalizeRegionMaps(tags);
|
||||
normalizeEvaluationMaps();
|
||||
}
|
||||
@@ -878,6 +889,88 @@ public class AwsConfigService {
|
||||
return paginate(result, limit, nextToken, 20, 20, "InvalidLimitException");
|
||||
}
|
||||
|
||||
// --- Aggregation Authorizations ---
|
||||
|
||||
/** Put is an upsert, and request tags are applied at creation only. A Put that finds the
|
||||
* authorization already there ignores its Tags, leaving whatever TagResource/UntagResource
|
||||
* last set, so re-putting with a different tag set is not a way to retag.
|
||||
*
|
||||
* <p>The get-then-put runs under the same lock delete takes, because idempotency that is
|
||||
* only probable is not idempotency: two concurrent Puts for one account/Region would
|
||||
* otherwise both find nothing and both create, and the second would overwrite the first's
|
||||
* CreationTime and re-apply creation tags over a set TagResource may already have changed. */
|
||||
public AggregationAuthorization putAggregationAuthorization(String region, String authorizedAccountId,
|
||||
String authorizedAwsRegion, List<Map<String, String>> tagList) {
|
||||
requireAuthorizationKey(authorizedAccountId, authorizedAwsRegion);
|
||||
synchronized (lockFor(authorizationLockKey(region, authorizedAccountId, authorizedAwsRegion))) {
|
||||
Map<String, AggregationAuthorization> store = authorizationsFor(region);
|
||||
String key = authorizationKey(authorizedAccountId, authorizedAwsRegion);
|
||||
AggregationAuthorization existing = store.get(key);
|
||||
if (existing != null) {
|
||||
return existing;
|
||||
}
|
||||
AggregationAuthorization authorization = new AggregationAuthorization(
|
||||
aggregationAuthorizationArn(region, authorizedAccountId, authorizedAwsRegion),
|
||||
authorizedAccountId, authorizedAwsRegion, now());
|
||||
store.put(key, authorization);
|
||||
persistRegion(aggregationAuthorizations, region);
|
||||
if (tagList != null && !tagList.isEmpty()) {
|
||||
tagResource(authorization.aggregationAuthorizationArn(), tagList);
|
||||
}
|
||||
return authorization;
|
||||
}
|
||||
}
|
||||
|
||||
public Paged<AggregationAuthorization> describeAggregationAuthorizations(String region, Integer limit,
|
||||
String nextToken) {
|
||||
Comparator<AggregationAuthorization> byAuthorizedTarget =
|
||||
Comparator.comparing(AggregationAuthorization::authorizedAccountId)
|
||||
.thenComparing(AggregationAuthorization::authorizedAwsRegion);
|
||||
List<AggregationAuthorization> result = new ArrayList<>(authorizationsFor(region).values());
|
||||
result.sort(byAuthorizedTarget);
|
||||
return paginate(result, limit, nextToken, MAX_AGGREGATION_AUTHORIZATIONS_LIMIT,
|
||||
MAX_AGGREGATION_AUTHORIZATIONS_LIMIT, "InvalidLimitException");
|
||||
}
|
||||
|
||||
/** The Config model declares no "not found" error for this operation, so deleting an
|
||||
* authorization that is not there succeeds. Only the parameters are validated.
|
||||
*
|
||||
* <p>The ARN is derived from the key rather than read off the removed entry, and its tags are
|
||||
* dropped whether or not an entry was there. Put reuses that same deterministic ARN, so a
|
||||
* delete that removed the authorization but did not reach the tags would otherwise leave them
|
||||
* to resurface on the next put, and the retry that should clean them up would find the
|
||||
* authorization already gone. */
|
||||
public void deleteAggregationAuthorization(String region, String authorizedAccountId,
|
||||
String authorizedAwsRegion) {
|
||||
requireAuthorizationKey(authorizedAccountId, authorizedAwsRegion);
|
||||
synchronized (lockFor(authorizationLockKey(region, authorizedAccountId, authorizedAwsRegion))) {
|
||||
Map<String, AggregationAuthorization> store = authorizationsFor(region);
|
||||
AggregationAuthorization removed =
|
||||
store.remove(authorizationKey(authorizedAccountId, authorizedAwsRegion));
|
||||
if (removed != null) {
|
||||
persistRegion(aggregationAuthorizations, region);
|
||||
}
|
||||
tags.remove(aggregationAuthorizationArn(region, authorizedAccountId, authorizedAwsRegion));
|
||||
}
|
||||
}
|
||||
|
||||
private String aggregationAuthorizationArn(String region, String authorizedAccountId,
|
||||
String authorizedAwsRegion) {
|
||||
return AwsArnUtils.Arn.of("config", region, regionResolver.getAccountId(),
|
||||
"aggregation-authorization/" + authorizedAccountId + "/" + authorizedAwsRegion).toString();
|
||||
}
|
||||
|
||||
private void requireAuthorizationKey(String authorizedAccountId, String authorizedAwsRegion) {
|
||||
if (isBlank(authorizedAccountId) || !AUTHORIZED_ACCOUNT_ID_PATTERN.matcher(authorizedAccountId).matches()) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"AuthorizedAccountId must be a 12 digit AWS account ID.", 400);
|
||||
}
|
||||
if (isBlank(authorizedAwsRegion)) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"AuthorizedAwsRegion must be specified.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Tagging ---
|
||||
|
||||
public void tagResource(String arn, List<Map<String, String>> tagList) {
|
||||
@@ -945,6 +1038,23 @@ public class AwsConfigService {
|
||||
return conformancePacks.computeIfAbsent(region, r -> new ConcurrentHashMap<>());
|
||||
}
|
||||
|
||||
private Map<String, AggregationAuthorization> authorizationsFor(String region) {
|
||||
return aggregationAuthorizations.computeIfAbsent(region, r -> new ConcurrentHashMap<>());
|
||||
}
|
||||
|
||||
private static String authorizationKey(String authorizedAccountId, String authorizedAwsRegion) {
|
||||
return authorizedAccountId + "|" + authorizedAwsRegion;
|
||||
}
|
||||
|
||||
/** Aggregation authorizations share {@link #ruleLocks} with the config rules, so their keys
|
||||
* are namespaced apart. A config rule name cannot contain '|', but making the two key spaces
|
||||
* disjoint by construction costs nothing and does not depend on that staying true. */
|
||||
private static String authorizationLockKey(String region, String authorizedAccountId,
|
||||
String authorizedAwsRegion) {
|
||||
return "aggregation-authorization|" + region + "|"
|
||||
+ authorizationKey(authorizedAccountId, authorizedAwsRegion);
|
||||
}
|
||||
|
||||
private Map<String, Map<String, ConfigEvaluation>> evaluationsFor(String region) {
|
||||
return evaluations.computeIfAbsent(region, r -> new ConcurrentHashMap<>());
|
||||
}
|
||||
|
||||
+38
-5
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.configservice;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.services.configservice.model.AggregationAuthorization;
|
||||
import io.github.hectorvent.floci.services.configservice.model.ConfigEvaluation;
|
||||
import io.github.hectorvent.floci.services.configservice.model.ConfigRule;
|
||||
import io.github.hectorvent.floci.services.configservice.model.ConfigurationRecorder;
|
||||
@@ -63,6 +64,9 @@ public class ConfigServiceJsonHandler {
|
||||
case "PutRetentionConfiguration" -> putRetentionConfiguration(request, region);
|
||||
case "DescribeRetentionConfigurations" -> describeRetentionConfigurations(request, region);
|
||||
case "DeleteRetentionConfiguration" -> deleteRetentionConfiguration(request, region);
|
||||
case "PutAggregationAuthorization" -> putAggregationAuthorization(request, region);
|
||||
case "DescribeAggregationAuthorizations" -> describeAggregationAuthorizations(request, region);
|
||||
case "DeleteAggregationAuthorization" -> deleteAggregationAuthorization(request, region);
|
||||
case "TagResource" -> tagResource(request);
|
||||
case "UntagResource" -> untagResource(request);
|
||||
case "ListTagsForResource" -> listTagsForResource(request);
|
||||
@@ -299,15 +303,36 @@ public class ConfigServiceJsonHandler {
|
||||
return Response.ok(pagedResponse("ConformancePackStatusDetails", page)).build();
|
||||
}
|
||||
|
||||
// --- Aggregation Authorizations ---
|
||||
|
||||
private Response putAggregationAuthorization(JsonNode req, String region) {
|
||||
AggregationAuthorization authorization = service.putAggregationAuthorization(region,
|
||||
req.path("AuthorizedAccountId").asText(null),
|
||||
req.path("AuthorizedAwsRegion").asText(null),
|
||||
extractTags(req));
|
||||
ObjectNode resp = mapper.createObjectNode();
|
||||
resp.set("AggregationAuthorization", mapper.valueToTree(authorization));
|
||||
return Response.ok(resp).build();
|
||||
}
|
||||
|
||||
private Response describeAggregationAuthorizations(JsonNode req, String region) {
|
||||
AwsConfigService.Paged<AggregationAuthorization> page = service.describeAggregationAuthorizations(region,
|
||||
extractLimit(req, "Limit"), extractNextToken(req));
|
||||
return Response.ok(pagedResponse("AggregationAuthorizations", page)).build();
|
||||
}
|
||||
|
||||
private Response deleteAggregationAuthorization(JsonNode req, String region) {
|
||||
service.deleteAggregationAuthorization(region,
|
||||
req.path("AuthorizedAccountId").asText(null),
|
||||
req.path("AuthorizedAwsRegion").asText(null));
|
||||
return Response.ok(mapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
// --- Tagging ---
|
||||
|
||||
private Response tagResource(JsonNode req) {
|
||||
String arn = req.path("ResourceArn").asText(null);
|
||||
List<Map<String, String>> tagList = new ArrayList<>();
|
||||
req.path("Tags").forEach(t -> tagList.add(Map.of(
|
||||
"Key", t.path("Key").asText(),
|
||||
"Value", t.path("Value").asText())));
|
||||
service.tagResource(arn, tagList);
|
||||
service.tagResource(arn, extractTags(req));
|
||||
return Response.ok(mapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@@ -329,6 +354,14 @@ public class ConfigServiceJsonHandler {
|
||||
|
||||
// --- Helpers ---
|
||||
|
||||
private List<Map<String, String>> extractTags(JsonNode req) {
|
||||
List<Map<String, String>> tagList = new ArrayList<>();
|
||||
req.path("Tags").forEach(t -> tagList.add(Map.of(
|
||||
"Key", t.path("Key").asText(),
|
||||
"Value", t.path("Value").asText())));
|
||||
return tagList;
|
||||
}
|
||||
|
||||
private List<String> extractStringList(JsonNode req, String fieldName) {
|
||||
List<String> result = new ArrayList<>();
|
||||
if (req.has(fieldName)) {
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.configservice.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record AggregationAuthorization(
|
||||
@JsonProperty("AggregationAuthorizationArn") String aggregationAuthorizationArn,
|
||||
@JsonProperty("AuthorizedAccountId") String authorizedAccountId,
|
||||
@JsonProperty("AuthorizedAwsRegion") String authorizedAwsRegion,
|
||||
@JsonProperty("CreationTime") Long creationTime) {
|
||||
}
|
||||
Reference in new issue
Block a user