Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(config): store PutConfigRule tags and drop them on DeleteConfigRule (#6080)

Allen Sanborn0ebcf846538f
diff --git a/docs/services/config.md b/docs/services/config.md
index d17a4157c9..6815e9ceb9 100644
--- a/docs/services/config.md
+++ b/docs/services/config.md
@@ -199,3 +199,10 @@ aws configservice delete-config-rule --config-rule-name s3-bucket-versioning
authorization ARN that does not name an existing resource in the caller's account and
region. ARNs of resource kinds Floci does not model (configuration aggregators, stored
queries, organization rules and packs, recorders) are accepted without a check.
+ - `PutConfigRule` applies its `Tags` only when it creates the rule; a later
+ `PutConfigRule` for the same rule leaves its tags unchanged, as AWS documents. Use
+ `TagResource` and `UntagResource` to change them. `DeleteConfigRule` drops the rule's tags.
+ - `PutConfigRule` and `PutAggregationAuthorization` reject more than 50 `Tags`, a tag
+ without a 1-128 character `Key`, or a `Value` over 256 characters with
+ `InvalidParameterValueException`, and store nothing. A tag with no `Value` is stored with
+ an empty value.
diff --git a/src/main/java/io/github/hectorvent/floci/services/configservice/AwsConfigService.java b/src/main/java/io/github/hectorvent/floci/services/configservice/AwsConfigService.java
index d36e1eaf1a..8d1c0ff710 100644
--- a/src/main/java/io/github/hectorvent/floci/services/configservice/AwsConfigService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/configservice/AwsConfigService.java
@@ -187,6 +187,14 @@ public class AwsConfigService {
// --- Config Rules ---
public ConfigRule putConfigRule(String region, ConfigRule requested) {
+ return putConfigRule(region, requested, null);
+ }
+
+ /** Put is an upsert, and request tags are applied at creation only: the PutConfigRule model
+ * (config/2014-11-12) says a later Put with different Tags is treated as idempotent and leaves
+ * the tags alone, which TagResource/UntagResource change instead. The get-then-put runs under
+ * the lock delete takes, so a delete cannot land between them and orphan the creation tags. */
+ public ConfigRule putConfigRule(String region, ConfigRule requested, List<Map<String, String>> tagList) {
if (requested == null || isBlank(requested.configRuleName())) {
throw new AwsException("InvalidParameterValueException",
"ConfigRuleName must be specified.", 400);
@@ -225,6 +233,13 @@ public class AwsConfigService {
}
}
String ruleName = requested.configRuleName();
+ synchronized (lockFor(ruleKey(region, ruleName))) {
+ return storeConfigRule(region, requested, ruleName, tagList);
+ }
+ }
+
+ private ConfigRule storeConfigRule(String region, ConfigRule requested, String ruleName,
+ List<Map<String, String>> tagList) {
Map<String, ConfigRule> store = rulesFor(region);
ConfigRule existing = store.get(ruleName);
String ruleId;
@@ -248,6 +263,12 @@ public class AwsConfigService {
ConfigRule rule = new ConfigRule(ruleName, ruleArn, ruleId, requested.description(),
requested.scope(), requested.source(), requested.inputParameters(),
requested.maximumExecutionFrequency(), state, requested.createdBy(), evaluationModes);
+ // Tags first: a later Put finds the rule and skips them, so a rule stored before its tags
+ // and then a failed tag write could never get them back. This way a failed tag write leaves
+ // no rule and the retry creates both.
+ if (existing == null && tagList != null && !tagList.isEmpty()) {
+ applyTags(ruleArn, tagList);
+ }
store.put(ruleName, rule);
persistRegion(configRules, region);
return rule;
@@ -259,10 +280,12 @@ public class AwsConfigService {
}
synchronized (lockFor(ruleKey(region, ruleName))) {
Map<String, ConfigRule> store = rulesFor(region);
- if (store.remove(ruleName) == null) {
+ ConfigRule removed = store.remove(ruleName);
+ if (removed == null) {
throw new AwsException("NoSuchConfigRuleException", NO_SUCH_CONFIG_RULE_MESSAGE, 400);
}
persistRegion(configRules, region);
+ tags.remove(removed.configRuleArn());
Map<String, Map<String, ConfigEvaluation>> regionEvaluations = evaluations.get(region);
if (regionEvaluations != null && regionEvaluations.remove(ruleName) != null) {
persistRegion(evaluations, region);
diff --git a/src/main/java/io/github/hectorvent/floci/services/configservice/ConfigServiceJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/configservice/ConfigServiceJsonHandler.java
index b52f6d5a93..dd624ccf06 100644
--- a/src/main/java/io/github/hectorvent/floci/services/configservice/ConfigServiceJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/configservice/ConfigServiceJsonHandler.java
@@ -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.core.common.AwsException;
import io.github.hectorvent.floci.services.configservice.model.AggregationAuthorization;
import io.github.hectorvent.floci.services.configservice.model.ComplianceByConfigRule;
import io.github.hectorvent.floci.services.configservice.model.ComplianceByResource;
@@ -85,7 +86,7 @@ public class ConfigServiceJsonHandler {
private Response putConfigRule(JsonNode req, String region) throws Exception {
JsonNode ruleNode = req.path("ConfigRule");
ConfigRule rule = ruleNode.isObject() ? mapper.treeToValue(ruleNode, ConfigRule.class) : null;
- service.putConfigRule(region, rule);
+ service.putConfigRule(region, rule, extractCreationTags(req));
return Response.ok(mapper.createObjectNode()).build();
}
@@ -314,7 +315,7 @@ public class ConfigServiceJsonHandler {
AggregationAuthorization authorization = service.putAggregationAuthorization(region,
req.path("AuthorizedAccountId").asText(null),
req.path("AuthorizedAwsRegion").asText(null),
- extractTags(req));
+ extractCreationTags(req));
ObjectNode resp = mapper.createObjectNode();
resp.set("AggregationAuthorization", mapper.valueToTree(authorization));
return Response.ok(resp).build();
@@ -367,6 +368,36 @@ public class ConfigServiceJsonHandler {
return tagList;
}
+ /** PutConfigRule and PutAggregationAuthorization take a {@code TagsList} (config/2014-11-12:
+ * at most 50 {@code Tag}s, Key 1-128 characters, optional Value of at most 256) and model
+ * {@code InvalidParameterValueException} as their only parameter error. Checked before the
+ * service stores anything, so a rejected request leaves no resource behind. An absent Value
+ * is optional in the model and is stored as the empty string. */
+ private List<Map<String, String>> extractCreationTags(JsonNode req) {
+ JsonNode tags = req.path("Tags");
+ if (tags.isMissingNode() || tags.isNull()) {
+ return List.of();
+ }
+ if (!tags.isArray()) {
+ throw new AwsException("InvalidParameterValueException", "Tags must be a list.", 400);
+ }
+ if (tags.size() > 50) {
+ throw new AwsException("InvalidParameterValueException",
+ "Tags must contain at most 50 tags.", 400);
+ }
+ for (JsonNode tag : tags) {
+ JsonNode key = tag.path("Key");
+ JsonNode value = tag.path("Value");
+ boolean valueOk = value.isMissingNode() || value.isNull()
+ || (value.isTextual() && value.asText().length() <= 256);
+ if (!key.isTextual() || key.asText().isEmpty() || key.asText().length() > 128 || !valueOk) {
+ throw new AwsException("InvalidParameterValueException",
+ "Each tag needs a Key of 1-128 characters and a Value of at most 256 characters.", 400);
+ }
+ }
+ return extractTags(req);
+ }
+
private List<String> extractStringList(JsonNode req, String fieldName) {
List<String> result = new ArrayList<>();
if (req.has(fieldName)) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServiceTaggingTest.java b/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServiceTaggingTest.java
index f476dd71b7..7d266c32fa 100644
--- a/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServiceTaggingTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServiceTaggingTest.java
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
+import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.core.storage.WriteProfile;
@@ -20,6 +21,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import static io.github.hectorvent.floci.testing.ThreadStates.awaitBlockedOrDone;
@@ -116,6 +118,33 @@ class AwsConfigServiceTaggingTest {
assertEquals(ENV_PROD, service.listTagsForResource(REGION, live.configRuleArn()));
}
+ @Test
+ void putConfigRuleAppliesRequestTagsAtCreationOnly() {
+ AwsConfigService service = service();
+ String arn = service.putConfigRule(REGION, rule("tagged"), ENV_PROD).configRuleArn();
+ assertEquals(ENV_PROD, service.listTagsForResource(REGION, arn));
+
+ ConfigRule updated = service.putConfigRule(REGION, rule("tagged"),
+ List.of(Map.of("Key", "env", "Value", "dev"), Map.of("Key", "team", "Value", "sec")));
+
+ assertEquals(arn, updated.configRuleArn());
+ assertEquals(ENV_PROD, service.listTagsForResource(REGION, arn));
+ }
+
+ @Test
+ void deletingAConfigRuleDropsItsTagsAndARecreatedRuleStartsFresh() {
+ AwsConfigService service = service();
+ String oldArn = service.putConfigRule(REGION, rule("tagged"), ENV_PROD).configRuleArn();
+ service.tagResource(REGION, oldArn, List.of(Map.of("Key", "team", "Value", "sec")));
+
+ service.deleteConfigRule(REGION, "tagged");
+
+ assertTrue(service.storedTags(oldArn).isEmpty(), "delete must drop the rule's tags");
+ List<Map<String, String>> fresh = List.of(Map.of("Key", "env", "Value", "dev"));
+ String newArn = service.putConfigRule(REGION, rule("tagged"), fresh).configRuleArn();
+ assertEquals(fresh, service.listTagsForResource(REGION, newArn));
+ }
+
@Test
void aDeletedAggregationAuthorizationIsNotFound() {
AwsConfigService service = service();
@@ -147,19 +176,7 @@ class AwsConfigServiceTaggingTest {
@Test
void aPackDeleteWaitsForATagWriteAlreadyInProgress() throws Exception {
ParkingStore<Map<String, String>> tagStore = new ParkingStore<>();
- AwsConfigService service = new AwsConfigService(new RegionResolver(REGION, ACCOUNT),
- new StorageFactory(null, null) {
- @Override
- @SuppressWarnings("unchecked")
- public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
- TypeReference<Map<String, V>> typeReference, WriteProfile profile) {
- return fileName.equals("config-tags.json")
- ? new AccountAwareStorageBackend<>((StorageBackend<String, V>) (Object) tagStore,
- null, ACCOUNT)
- : AccountAwareStorageBackend.inMemory(ACCOUNT);
- }
- });
- service.initializeStorage();
+ AwsConfigService service = serviceWithTagStore(tagStore);
String arn = service.putConformancePack(REGION, "pack", "s3://bucket/t.yaml", null).conformancePackArn();
tagStore.parkNextWrite();
@@ -188,6 +205,46 @@ class AwsConfigServiceTaggingTest {
assertTrue(service.storedTags(arn).isEmpty(), "a tag entry outlived the pack it was written for");
}
+ /**
+ * Creation tags are skipped once the rule exists, so a tag write that fails after the rule
+ * was stored would leave a retry nothing to restore them with. The retry must end tagged.
+ */
+ @Test
+ void aRetryAfterAFailedCreationTagWriteEndsWithTheTags() {
+ AtomicBoolean failNextWrite = new AtomicBoolean(true);
+ AwsConfigService service = serviceWithTagStore(new InMemoryStorage<String, Map<String, String>>() {
+ @Override
+ public void put(String key, Map<String, String> value) {
+ if (failNextWrite.getAndSet(false)) {
+ throw new IllegalStateException("tag store unavailable");
+ }
+ super.put(key, value);
+ }
+ });
+
+ assertThrows(IllegalStateException.class, () -> service.putConfigRule(REGION, rule("retried"), ENV_PROD));
+ String arn = service.putConfigRule(REGION, rule("retried"), ENV_PROD).configRuleArn();
+
+ assertEquals(ENV_PROD, service.listTagsForResource(REGION, arn));
+ }
+
+ private static AwsConfigService serviceWithTagStore(StorageBackend<String, Map<String, String>> tagStore) {
+ AwsConfigService service = new AwsConfigService(new RegionResolver(REGION, ACCOUNT),
+ new StorageFactory(null, null) {
+ @Override
+ @SuppressWarnings("unchecked")
+ public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
+ TypeReference<Map<String, V>> typeReference, WriteProfile profile) {
+ return fileName.equals("config-tags.json")
+ ? new AccountAwareStorageBackend<>((StorageBackend<String, V>) (Object) tagStore,
+ null, ACCOUNT)
+ : AccountAwareStorageBackend.inMemory(ACCOUNT);
+ }
+ });
+ service.initializeStorage();
+ return service;
+ }
+
private static ConfigRule rule(String name) {
return new ConfigRule(name, null, null, null, null,
new ConfigRuleSource("AWS", "S3_BUCKET_VERSIONING_ENABLED", null, null),
diff --git a/src/test/java/io/github/hectorvent/floci/services/configservice/ConfigRuleIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/configservice/ConfigRuleIntegrationTest.java
index ced86496e3..a544aacb73 100644
--- a/src/test/java/io/github/hectorvent/floci/services/configservice/ConfigRuleIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/configservice/ConfigRuleIntegrationTest.java
@@ -2,12 +2,16 @@ package io.github.hectorvent.floci.services.configservice;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
+import java.util.Collections;
+import java.util.List;
+
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@@ -437,4 +441,117 @@ class ConfigRuleIntegrationTest {
.statusCode(400)
.body("__type", equalTo("InvalidParameterValueException"));
}
+
+ @Test
+ @Order(100)
+ void putConfigRuleTagsAreListedForTheRuleArn() {
+ String body = """
+ {
+ "ConfigRule": {
+ "ConfigRuleName": "rule-tags-test",
+ "Source": {"Owner": "AWS", "SourceIdentifier": "S3_BUCKET_VERSIONING_ENABLED"}
+ },
+ "Tags": [%s]
+ }
+ """;
+ post("PutConfigRule", body.formatted("{\"Key\": \"env\", \"Value\": \"prod\"}"))
+ .then().statusCode(200);
+ String arn = post("DescribeConfigRules", "{\"ConfigRuleNames\": [\"rule-tags-test\"]}")
+ .then().statusCode(200).extract().path("ConfigRules[0].ConfigRuleArn");
+ String listTags = "{\"ResourceArn\": \"" + arn + "\"}";
+
+ post("ListTagsForResource", listTags)
+ .then().statusCode(200)
+ .body("Tags", hasSize(1))
+ .body("Tags[0].Key", equalTo("env"))
+ .body("Tags[0].Value", equalTo("prod"));
+
+ // Tags are applied at creation only; a later Put with other Tags leaves them alone.
+ post("PutConfigRule", body.formatted("{\"Key\": \"env\", \"Value\": \"dev\"}"))
+ .then().statusCode(200);
+ post("ListTagsForResource", listTags)
+ .then().statusCode(200)
+ .body("Tags", hasSize(1))
+ .body("Tags[0].Value", equalTo("prod"));
+
+ post("DeleteConfigRule", "{\"ConfigRuleName\": \"rule-tags-test\"}").then().statusCode(200);
+ }
+
+ /** PutConfigRule's TagsList (config/2014-11-12): at most 50 tags, Key 1-128, Value 0-256,
+ * rejected with the operation's modelled InvalidParameterValueException and nothing stored. */
+ @Test
+ @Order(101)
+ void putConfigRuleRejectsInvalidTagsWithoutCreatingTheRule() {
+ String fiftyOne = String.join(",", Collections.nCopies(51, "{\"Key\": \"k\", \"Value\": \"v\"}"));
+ List<String> invalid = List.of(
+ "{\"Value\": \"v\"}",
+ "{\"Key\": \"\", \"Value\": \"v\"}",
+ "{\"Key\": \"" + "k".repeat(129) + "\", \"Value\": \"v\"}",
+ "{\"Key\": \"k\", \"Value\": \"" + "v".repeat(257) + "\"}",
+ "{\"Key\": 7, \"Value\": \"v\"}",
+ fiftyOne);
+ for (String tags : invalid) {
+ post("PutConfigRule", """
+ {
+ "ConfigRule": {
+ "ConfigRuleName": "rule-bad-tags",
+ "Source": {"Owner": "AWS", "SourceIdentifier": "S3_BUCKET_VERSIONING_ENABLED"}
+ },
+ "Tags": [%s]
+ }
+ """.formatted(tags))
+ .then().statusCode(400)
+ .body("__type", equalTo("InvalidParameterValueException"));
+ }
+ post("DescribeConfigRules", "{\"ConfigRuleNames\": [\"rule-bad-tags\"]}")
+ .then().statusCode(400)
+ .body("__type", equalTo("NoSuchConfigRuleException"));
+ }
+
+ /** Value is optional in the model, so a tag without one is accepted with an empty value. */
+ @Test
+ @Order(102)
+ void putConfigRuleAcceptsATagWithoutAValue() {
+ post("PutConfigRule", """
+ {
+ "ConfigRule": {
+ "ConfigRuleName": "rule-keyonly-tag",
+ "Source": {"Owner": "AWS", "SourceIdentifier": "S3_BUCKET_VERSIONING_ENABLED"}
+ },
+ "Tags": [{"Key": "owner"}]
+ }
+ """).then().statusCode(200);
+ String arn = post("DescribeConfigRules", "{\"ConfigRuleNames\": [\"rule-keyonly-tag\"]}")
+ .then().statusCode(200).extract().path("ConfigRules[0].ConfigRuleArn");
+ post("ListTagsForResource", "{\"ResourceArn\": \"" + arn + "\"}")
+ .then().statusCode(200)
+ .body("Tags", hasSize(1))
+ .body("Tags[0].Key", equalTo("owner"))
+ .body("Tags[0].Value", equalTo(""));
+ post("DeleteConfigRule", "{\"ConfigRuleName\": \"rule-keyonly-tag\"}").then().statusCode(200);
+ }
+
+ /** PutAggregationAuthorization takes the same TagsList and models the same error. */
+ @Test
+ @Order(103)
+ void putAggregationAuthorizationRejectsAnInvalidTag() {
+ post("PutAggregationAuthorization", """
+ {"AuthorizedAccountId": "444455556666", "AuthorizedAwsRegion": "eu-west-1",
+ "Tags": [{"Key": "", "Value": "v"}]}
+ """)
+ .then().statusCode(400)
+ .body("__type", equalTo("InvalidParameterValueException"));
+ post("DescribeAggregationAuthorizations", "{}")
+ .then().statusCode(200)
+ .body("AggregationAuthorizations.AuthorizedAccountId", not(hasItem("444455556666")));
+ }
+
+ private static Response post(String action, String body) {
+ return given()
+ .header("X-Amz-Target", TARGET_PREFIX + action)
+ .contentType(CONTENT_TYPE)
+ .body(body)
+ .when()
+ .post("/");
+ }
}