mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(config): answer ResourceNotFoundException for tags on a missing resource (#6047)
TagResource, UntagResource and ListTagsForResource were plain map operations keyed by ARN. Tagging a deleted conformance pack's ARN returned 200 and re-created an orphan tag entry, and listing it returned an empty list. The Config model lists ResourceNotFoundException for all three operations. For the kinds Floci stores (config rules, conformance packs, aggregation authorizations) the ARN must now match an existing resource in the caller's account and request Region, so a deleted resource, or an ARN from another account, Region or partition, is not found. The check and the write run under the lock the resource's delete takes, so a delete cannot land between them and leave tags for a resource that is gone. ARNs of kinds Floci does not model are still accepted unchecked.
This commit is contained in:
1 parent
0da0c80824
commit
88b0522168
8 files changed
+334
-51
No files matched your search
+12
-12
@@ -145,12 +145,12 @@ class AwsConfigServiceAggregationAuthorizationTest {
|
||||
// An interrupted delete leaves the entry removed but its tags behind. Reproduce that
|
||||
// state, then retry the delete: the retry must still reach the tags.
|
||||
service.deleteAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1");
|
||||
service.tagResource(arn, List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
service.applyTags(arn, List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
assertTrue(authorizations(service, REGION).isEmpty(), "precondition: the entry is gone");
|
||||
|
||||
service.deleteAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1");
|
||||
|
||||
assertTrue(service.listTagsForResource(arn).isEmpty(),
|
||||
assertTrue(service.storedTags(arn).isEmpty(),
|
||||
"a retried delete must clear tags an interrupted attempt left on the ARN");
|
||||
assertEquals(arn,
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null)
|
||||
@@ -165,7 +165,7 @@ class AwsConfigServiceAggregationAuthorizationTest {
|
||||
AggregationAuthorization created = service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT,
|
||||
"eu-west-1", List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
|
||||
List<Map<String, String>> tags = service.listTagsForResource(created.aggregationAuthorizationArn());
|
||||
List<Map<String, String>> tags = service.listTagsForResource(REGION, created.aggregationAuthorizationArn());
|
||||
assertEquals(List.of(Map.of("Key", "env", "Value", "prod")), tags);
|
||||
}
|
||||
|
||||
@@ -186,17 +186,17 @@ class AwsConfigServiceAggregationAuthorizationTest {
|
||||
+ "no overwrite of env, and no tier added");
|
||||
|
||||
// Tags do change through the tagging API, which is the only way AWS offers.
|
||||
service.tagResource(arn, List.of(Map.of("Key", "env", "Value", "dev")));
|
||||
service.tagResource(REGION, arn, List.of(Map.of("Key", "env", "Value", "dev")));
|
||||
assertEquals(Map.of("env", "dev", "owner", "platform"), tagMap(service, arn),
|
||||
"TagResource must still update an existing tag");
|
||||
|
||||
service.untagResource(arn, List.of("owner"));
|
||||
service.untagResource(REGION, arn, List.of("owner"));
|
||||
assertEquals(Map.of("env", "dev"), tagMap(service, arn),
|
||||
"UntagResource must still remove a tag");
|
||||
}
|
||||
|
||||
private Map<String, String> tagMap(AwsConfigService service, String arn) {
|
||||
return service.listTagsForResource(arn).stream()
|
||||
return service.listTagsForResource(REGION, arn).stream()
|
||||
.collect(Collectors.toMap(t -> t.get("Key"), t -> t.get("Value")));
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ class AwsConfigServiceAggregationAuthorizationTest {
|
||||
service.deleteAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1");
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
|
||||
assertTrue(service.listTagsForResource(created.aggregationAuthorizationArn()).isEmpty(),
|
||||
assertTrue(service.listTagsForResource(REGION, created.aggregationAuthorizationArn()).isEmpty(),
|
||||
"tags of a deleted authorization must not resurface on the reused ARN");
|
||||
}
|
||||
|
||||
@@ -218,11 +218,11 @@ class AwsConfigServiceAggregationAuthorizationTest {
|
||||
AwsConfigService service = service();
|
||||
String arn = service.putConformancePack(REGION, "pack", "s3://bucket/template.yaml", null)
|
||||
.conformancePackArn();
|
||||
service.tagResource(arn, List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
service.tagResource(REGION, arn, List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
|
||||
service.deleteConformancePack(REGION, "pack");
|
||||
|
||||
assertTrue(service.listTagsForResource(arn).isEmpty(),
|
||||
assertTrue(service.storedTags(arn).isEmpty(),
|
||||
"tags of a deleted conformance pack must not outlive it");
|
||||
}
|
||||
|
||||
@@ -251,7 +251,7 @@ class AwsConfigServiceAggregationAuthorizationTest {
|
||||
service.initializeStorage();
|
||||
String arn = service.putConformancePack(REGION, "pack", "s3://bucket/v1.yaml", null)
|
||||
.conformancePackArn();
|
||||
service.tagResource(arn, List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
service.tagResource(REGION, arn, List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
|
||||
packs.parkNextWrite();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
@@ -275,7 +275,7 @@ class AwsConfigServiceAggregationAuthorizationTest {
|
||||
|
||||
assertTrue(service.describeConformancePacks(REGION, null).isEmpty(),
|
||||
"a Delete that succeeded left the pack it deleted in the store");
|
||||
assertTrue(service.listTagsForResource(arn).isEmpty(), "the deleted pack's tags survived");
|
||||
assertTrue(service.storedTags(arn).isEmpty(), "the deleted pack's tags survived");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -399,7 +399,7 @@ class AwsConfigServiceAggregationAuthorizationTest {
|
||||
List<AggregationAuthorization> after = authorizations(service, REGION);
|
||||
assertTrue(after.size() <= 1);
|
||||
if (after.isEmpty()) {
|
||||
assertTrue(service.listTagsForResource(
|
||||
assertTrue(service.storedTags(
|
||||
AwsArnUtils.Arn.of("config", REGION, ACCOUNT,
|
||||
"aggregation-authorization/" + AUTHORIZED_ACCOUNT + "/eu-west-1").toString())
|
||||
.isEmpty(), "delete left tags behind for a removed authorization");
|
||||
|
||||
+5
-5
@@ -41,7 +41,7 @@ class AwsConfigServicePersistenceTest {
|
||||
"arn:aws:iam::000000000000:role/config", new RecordingGroup(true, false, null)));
|
||||
first.putDeliveryChannel(REGION,
|
||||
new DeliveryChannel("default", "config-bucket", null, null, null, null));
|
||||
first.tagResource(rule.configRuleArn(), List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
first.tagResource(REGION, rule.configRuleArn(), List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
|
||||
AwsConfigService reloaded = serviceWithStorage(storage);
|
||||
|
||||
@@ -54,7 +54,7 @@ class AwsConfigServicePersistenceTest {
|
||||
reloaded.describeConfigurationRecorders(REGION, null).getFirst().name());
|
||||
assertEquals("config-bucket",
|
||||
reloaded.describeDeliveryChannels(REGION, null).getFirst().s3BucketName());
|
||||
assertEquals("prod", reloaded.listTagsForResource(rule.configRuleArn()).getFirst().get("Value"));
|
||||
assertEquals("prod", reloaded.listTagsForResource(REGION, rule.configRuleArn()).getFirst().get("Value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -65,15 +65,15 @@ class AwsConfigServicePersistenceTest {
|
||||
ConfigRule rule = first.putConfigRule(REGION, rule("keep", "AWS", "REQUIRED_TAGS"));
|
||||
first.putConfigRule(REGION, rule("drop", "AWS", "REQUIRED_TAGS"));
|
||||
first.deleteConfigRule(REGION, "drop");
|
||||
first.tagResource(rule.configRuleArn(),
|
||||
first.tagResource(REGION, rule.configRuleArn(),
|
||||
List.of(Map.of("Key", "env", "Value", "prod"), Map.of("Key", "team", "Value", "sec")));
|
||||
first.untagResource(rule.configRuleArn(), List.of("team"));
|
||||
first.untagResource(REGION, rule.configRuleArn(), List.of("team"));
|
||||
|
||||
AwsConfigService reloaded = serviceWithStorage(storage);
|
||||
|
||||
assertEquals(List.of("keep"),
|
||||
reloaded.describeConfigRules(REGION, null).stream().map(ConfigRule::configRuleName).toList());
|
||||
Map<String, String> tags = reloaded.listTagsForResource(rule.configRuleArn()).stream()
|
||||
Map<String, String> tags = reloaded.listTagsForResource(REGION, rule.configRuleArn()).stream()
|
||||
.collect(java.util.stream.Collectors.toMap(t -> t.get("Key"), t -> t.get("Value")));
|
||||
assertEquals("prod", tags.get("env"));
|
||||
assertTrue(!tags.containsKey("team"), "untagged key must not reappear after restart");
|
||||
|
||||
+196
@@ -0,0 +1,196 @@
|
||||
package io.github.hectorvent.floci.services.configservice;
|
||||
|
||||
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.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.core.storage.WriteProfile;
|
||||
import io.github.hectorvent.floci.services.configservice.model.ConfigRule;
|
||||
import io.github.hectorvent.floci.services.configservice.model.ConfigRuleSource;
|
||||
import io.github.hectorvent.floci.testing.ParkingStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
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.AtomicReference;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.ThreadStates.awaitBlockedOrDone;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* TagResource, UntagResource and ListTagsForResource all model ResourceNotFoundException
|
||||
* (config/2014-11-12). For the kinds Floci stores, an ARN that names no live resource in the
|
||||
* caller's account and Region must get it, rather than a 200 that re-creates an orphan tag entry.
|
||||
*/
|
||||
class AwsConfigServiceTaggingTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final List<Map<String, String>> ENV_PROD = List.of(Map.of("Key", "env", "Value", "prod"));
|
||||
|
||||
private AwsConfigService service() {
|
||||
return new AwsConfigService(new RegionResolver(REGION, ACCOUNT), null);
|
||||
}
|
||||
|
||||
private static void assertNotFound(Executable call) {
|
||||
AwsException ex = assertThrows(AwsException.class, call);
|
||||
assertEquals("ResourceNotFoundException", ex.getErrorCode());
|
||||
assertEquals(400, ex.getHttpStatus());
|
||||
}
|
||||
|
||||
private static void assertAllThreeNotFound(AwsConfigService service, String region, String arn) {
|
||||
assertNotFound(() -> service.tagResource(region, arn, ENV_PROD));
|
||||
assertNotFound(() -> service.untagResource(region, arn, List.of("env")));
|
||||
assertNotFound(() -> service.listTagsForResource(region, arn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeletedConformancePackIsNotFoundAndTaggingItLeavesNoOrphan() {
|
||||
AwsConfigService service = service();
|
||||
String arn = service.putConformancePack(REGION, "pack", "s3://bucket/t.yaml", null).conformancePackArn();
|
||||
service.deleteConformancePack(REGION, "pack");
|
||||
|
||||
assertAllThreeNotFound(service, REGION, arn);
|
||||
assertTrue(service.storedTags(arn).isEmpty(), "a rejected TagResource must not store tags");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aConformancePackThatNeverExistedIsNotFound() {
|
||||
assertAllThreeNotFound(service(), REGION,
|
||||
"arn:aws:config:us-east-1:000000000000:conformance-pack/ghost/conformance-pack-abc123");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRecreatedPackDoesNotAnswerForItsPredecessorsArn() {
|
||||
AwsConfigService service = service();
|
||||
String oldArn = service.putConformancePack(REGION, "pack", "s3://bucket/t.yaml", null).conformancePackArn();
|
||||
service.deleteConformancePack(REGION, "pack");
|
||||
service.putConformancePack(REGION, "pack", "s3://bucket/t.yaml", null);
|
||||
|
||||
assertAllThreeNotFound(service, REGION, oldArn);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLiveConformancePackIsStillTaggable() {
|
||||
AwsConfigService service = service();
|
||||
String arn = service.putConformancePack(REGION, "pack", "s3://bucket/t.yaml", null).conformancePackArn();
|
||||
|
||||
service.tagResource(REGION, arn, List.of(Map.of("Key", "env", "Value", "prod"),
|
||||
Map.of("Key", "team", "Value", "sec")));
|
||||
service.untagResource(REGION, arn, List.of("team"));
|
||||
|
||||
assertEquals(ENV_PROD, service.listTagsForResource(REGION, arn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPackArnFromAnotherRegionAccountOrPartitionIsNotFound() {
|
||||
AwsConfigService service = service();
|
||||
String arn = service.putConformancePack(REGION, "pack", "s3://bucket/t.yaml", null).conformancePackArn();
|
||||
service.putConformancePack("eu-west-1", "pack", "s3://bucket/t.yaml", null);
|
||||
|
||||
assertAllThreeNotFound(service, "eu-west-1", arn);
|
||||
assertAllThreeNotFound(service, REGION, arn.replace(":" + ACCOUNT + ":", ":111122223333:"));
|
||||
assertAllThreeNotFound(service, REGION, arn.replace("arn:aws:", "arn:aws-cn:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeletedConfigRuleIsNotFoundAndALiveOneIsTaggable() {
|
||||
AwsConfigService service = service();
|
||||
ConfigRule live = service.putConfigRule(REGION, rule("live"));
|
||||
String goneArn = service.putConfigRule(REGION, rule("gone")).configRuleArn();
|
||||
service.deleteConfigRule(REGION, "gone");
|
||||
|
||||
assertAllThreeNotFound(service, REGION, goneArn);
|
||||
service.tagResource(REGION, live.configRuleArn(), ENV_PROD);
|
||||
assertEquals(ENV_PROD, service.listTagsForResource(REGION, live.configRuleArn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeletedAggregationAuthorizationIsNotFound() {
|
||||
AwsConfigService service = service();
|
||||
String arn = service.putAggregationAuthorization(REGION, "111122223333", "eu-west-1", ENV_PROD)
|
||||
.aggregationAuthorizationArn();
|
||||
assertEquals(ENV_PROD, service.listTagsForResource(REGION, arn));
|
||||
|
||||
service.deleteAggregationAuthorization(REGION, "111122223333", "eu-west-1");
|
||||
|
||||
assertAllThreeNotFound(service, REGION, arn);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anArnOfAKindFlociDoesNotModelIsPassedThrough() {
|
||||
AwsConfigService service = service();
|
||||
String arn = "arn:aws:config:us-east-1:000000000000:config-aggregator/config-aggregator-abc123";
|
||||
|
||||
service.tagResource(REGION, arn, ENV_PROD);
|
||||
|
||||
assertEquals(ENV_PROD, service.listTagsForResource(REGION, arn));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check-then-tag must not race a delete. Unlocked, a delete landing between TagResource's
|
||||
* existence check and its write removes the pack and its tags, then the write re-creates
|
||||
* the tag entry for a pack that is gone. The TagResource is parked in its write, inside the
|
||||
* section the pack lock guards, and the delete must be seen waiting on it.
|
||||
*/
|
||||
@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();
|
||||
String arn = service.putConformancePack(REGION, "pack", "s3://bucket/t.yaml", null).conformancePackArn();
|
||||
|
||||
tagStore.parkNextWrite();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<?> tag = pool.submit(() -> service.tagResource(REGION, arn, ENV_PROD));
|
||||
assertTrue(tagStore.awaitParked(), "the tag write must reach its persist");
|
||||
AtomicReference<Thread> deleteThread = new AtomicReference<>();
|
||||
Future<?> delete = pool.submit(() -> {
|
||||
deleteThread.set(Thread.currentThread());
|
||||
service.deleteConformancePack(REGION, "pack");
|
||||
});
|
||||
awaitBlockedOrDone(deleteThread, delete);
|
||||
assertFalse(delete.isDone(), "the delete ran while a tag write on the same pack was mid-flight");
|
||||
|
||||
tagStore.release();
|
||||
tag.get(2, TimeUnit.SECONDS);
|
||||
delete.get(2, TimeUnit.SECONDS);
|
||||
} catch (ExecutionException e) {
|
||||
throw new AssertionError(e.getCause());
|
||||
} finally {
|
||||
tagStore.release();
|
||||
pool.shutdownNow();
|
||||
}
|
||||
|
||||
assertTrue(service.storedTags(arn).isEmpty(), "a tag entry outlived the pack it was written for");
|
||||
}
|
||||
|
||||
private static ConfigRule rule(String name) {
|
||||
return new ConfigRule(name, null, null, null, null,
|
||||
new ConfigRuleSource("AWS", "S3_BUCKET_VERSIONING_ENABLED", null, null),
|
||||
null, null, null, null, null);
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -132,7 +132,7 @@ class ConfigTaggingIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void listTagsForResourceNoTags() {
|
||||
void listTagsForResourceOfAMissingRuleIsNotFound() {
|
||||
given()
|
||||
.header("X-Amz-Target", TARGET_PREFIX + "ListTagsForResource")
|
||||
.contentType(CONTENT_TYPE)
|
||||
@@ -142,8 +142,8 @@ class ConfigTaggingIntegrationTest {
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Tags", hasSize(0));
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
private String getConfigRuleArn(String ruleName) {
|
||||
|
||||
+16
-8
@@ -96,7 +96,7 @@ class ConformancePackIntegrationTest {
|
||||
@Test
|
||||
@Order(4)
|
||||
void deleteConformancePack() {
|
||||
listPackTags().body("Tags", hasSize(1));
|
||||
packTagCall("ListTagsForResource", "").statusCode(200).body("Tags", hasSize(1));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", TARGET_PREFIX + "DeleteConformancePack")
|
||||
@@ -109,19 +109,27 @@ class ConformancePackIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// The pack's tags go with it.
|
||||
listPackTags().body("Tags", empty());
|
||||
// The deleted pack's ARN is not found by any of the tagging operations, and the
|
||||
// rejected TagResource does not bring its tags back.
|
||||
packTagCall("TagResource", ", \"Tags\": [{\"Key\": \"env\", \"Value\": \"prod\"}]")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
packTagCall("UntagResource", ", \"TagKeys\": [\"env\"]")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
packTagCall("ListTagsForResource", "")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
private ValidatableResponse listPackTags() {
|
||||
private ValidatableResponse packTagCall(String action, String extraMembers) {
|
||||
return given()
|
||||
.header("X-Amz-Target", TARGET_PREFIX + "ListTagsForResource")
|
||||
.header("X-Amz-Target", TARGET_PREFIX + action)
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{\"ResourceArn\": \"" + packArn + "\"}")
|
||||
.body("{\"ResourceArn\": \"" + packArn + "\"" + extraMembers + "}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
.then();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user