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

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

PutConfigRule accepted a top-level Tags list but never stored it, so
ListTagsForResource on the new rule's ARN returned an empty list and a
tagged aws_config_config_rule drifted on every plan. DeleteConfigRule
also left any tags behind under the removed rule's ARN.

Tags are now applied when PutConfigRule creates the rule, and ignored
when it updates an existing one, as the PutConfigRule model documents
("Tags are added at creation and cannot be updated with this
operation"). The get-then-put now runs under the lock DeleteConfigRule
takes, and DeleteConfigRule removes the rule's tags inside that lock,
matching conformance packs and aggregation authorizations.

* fix(config): validate creation tags and write them before the rule

PutConfigRule and PutAggregationAuthorization now check their TagsList
against the config/2014-11-12 model (at most 50 tags, Key 1-128
characters, optional Value of at most 256) and reject with the modelled
InvalidParameterValueException before anything is stored. A tag with no
Value stays accepted, as the model makes Value optional.

PutConfigRule writes the creation tags before the rule. Tags are applied
only when the Put creates the rule, so a rule stored ahead of a failed
tag write could never get its tags back from a retry; now the failed
write leaves no rule and the retry creates both.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-20 11:43:26 -04:00
1 parent a899318862
commit 0ebcf84653
5 files changed
+251 -16

No files matched your search

@@ -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),
@@ -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("/");
}
}