diff --git a/docs/services/config.md b/docs/services/config.md index ec1c088b64..d17a4157c9 100644 --- a/docs/services/config.md +++ b/docs/services/config.md @@ -194,3 +194,8 @@ aws configservice delete-config-rule --config-rule-name s3-bucket-versioning data is aggregated: configuration aggregators are not emulated. `DeleteAggregationAuthorization` on an authorization that does not exist succeeds, because the Config API models no "not found" error for that operation. + - `TagResource`, `UntagResource` and `ListTagsForResource` answer + `ResourceNotFoundException` for a config rule, conformance pack or aggregation + 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. 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 7c7c6a8800..d36e1eaf1a 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 @@ -41,6 +41,8 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BooleanSupplier; +import java.util.function.Supplier; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -929,7 +931,7 @@ public class AwsConfigService { store.put(key, authorization); persistRegion(aggregationAuthorizations, region); if (tagList != null && !tagList.isEmpty()) { - tagResource(authorization.aggregationAuthorizationArn(), tagList); + applyTags(authorization.aggregationAuthorizationArn(), tagList); } return authorization; } @@ -987,7 +989,45 @@ public class AwsConfigService { // --- Tagging --- - public void tagResource(String arn, List> tagList) { + /** TagResource, UntagResource and ListTagsForResource each model ResourceNotFoundException. + * For the kinds Floci stores (config rules, conformance packs, aggregation authorizations) + * the ARN must name a resource that exists in the caller's account and the request Region, + * so a deleted resource, or one in another account, Region or partition, is not found. The + * check and the tag write run under the lock that resource's delete takes, so a delete cannot + * land between them and leave an orphan tag entry for a resource that is gone. ARNs of kinds + * Floci does not model are passed through unchecked, as before. */ + public void tagResource(String region, String arn, List> tagList) { + onTaggableResource(region, arn, () -> { + applyTags(arn, tagList); + return null; + }); + } + + public void untagResource(String region, String arn, List tagKeys) { + onTaggableResource(region, arn, () -> { + Map tagMap = tags.get(arn); + if (tagMap != null) { + tagKeys.forEach(tagMap::remove); + tags.put(arn, tagMap); // write back the in-place inner-map mutation + } + return null; + }); + } + + public List> listTagsForResource(String region, String arn) { + return onTaggableResource(region, arn, () -> storedTags(arn).entrySet().stream() + .map(e -> Map.of("Key", e.getKey(), "Value", e.getValue())) + .collect(Collectors.toList())); + } + + /** Tags as stored, with no existence check. Callers that already hold the resource's lock + * (creation tags on Put) write through {@link #applyTags}; tests read through this to see + * whether a delete reached the tags, which the API itself can no longer observe. */ + Map storedTags(String arn) { + return tags.getOrDefault(arn, Map.of()); + } + + void applyTags(String arn, List> tagList) { Map tagMap = tags.computeIfAbsent(arn, k -> new ConcurrentHashMap<>()); for (Map t : tagList) { tagMap.put(t.get("Key"), t.get("Value")); @@ -995,19 +1035,53 @@ public class AwsConfigService { tags.put(arn, tagMap); // write back the in-place inner-map mutation } - public void untagResource(String arn, List tagKeys) { - Map tagMap = tags.get(arn); - if (tagMap != null) { - tagKeys.forEach(tagMap::remove); - tags.put(arn, tagMap); // write back the in-place inner-map mutation + private T onTaggableResource(String region, String arn, Supplier action) { + String resource = AwsArnUtils.resourceIfArnFor(arn, "config").orElse(""); + String[] parts = resource.split("/", -1); + String lockKey; + BooleanSupplier exists; + switch (parts[0]) { + case "conformance-pack" -> { + String packName = parts.length > 1 ? parts[1] : ""; + lockKey = conformancePackLockKey(region, packName); + exists = () -> { + ConformancePack pack = packsFor(region).get(packName); + return pack != null && arn.equals(pack.conformancePackArn()); + }; + } + case "aggregation-authorization" -> { + String authorizedAccountId = parts.length == 3 ? parts[1] : ""; + String authorizedAwsRegion = parts.length == 3 ? parts[2] : ""; + lockKey = authorizationLockKey(region, authorizedAccountId, authorizedAwsRegion); + exists = () -> { + AggregationAuthorization authorization = authorizationsFor(region) + .get(authorizationKey(authorizedAccountId, authorizedAwsRegion)); + return authorization != null && arn.equals(authorization.aggregationAuthorizationArn()); + }; + } + case "config-rule" -> { + // A rule ARN carries its id, not its name, and the rule lock is keyed by name. + String ruleName = rulesFor(region).values().stream() + .filter(rule -> arn.equals(rule.configRuleArn())) + .map(ConfigRule::configRuleName) + .findFirst().orElse(""); + lockKey = ruleKey(region, ruleName); + exists = () -> { + ConfigRule rule = rulesFor(region).get(ruleName); + return rule != null && arn.equals(rule.configRuleArn()); + }; + } + default -> { + return action.get(); + } + } + synchronized (lockFor(lockKey)) { + if (!exists.getAsBoolean()) { + throw new AwsException("ResourceNotFoundException", + "The resource " + arn + " does not exist.", 400); + } + return action.get(); } - } - - public List> listTagsForResource(String arn) { - Map tagMap = tags.getOrDefault(arn, Map.of()); - return tagMap.entrySet().stream() - .map(e -> Map.of("Key", e.getKey(), "Value", e.getValue())) - .collect(Collectors.toList()); } // --- Pagination --- 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 eb69732e69..b52f6d5a93 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 @@ -72,9 +72,9 @@ public class ConfigServiceJsonHandler { 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); + case "TagResource" -> tagResource(request, region); + case "UntagResource" -> untagResource(request, region); + case "ListTagsForResource" -> listTagsForResource(request, region); default -> throw new io.github.hectorvent.floci.core.common.AwsException( "InvalidAction", "Could not find operation " + action, 400); }; @@ -335,23 +335,23 @@ public class ConfigServiceJsonHandler { // --- Tagging --- - private Response tagResource(JsonNode req) { + private Response tagResource(JsonNode req, String region) { String arn = req.path("ResourceArn").asText(null); - service.tagResource(arn, extractTags(req)); + service.tagResource(region, arn, extractTags(req)); return Response.ok(mapper.createObjectNode()).build(); } - private Response untagResource(JsonNode req) { + private Response untagResource(JsonNode req, String region) { String arn = req.path("ResourceArn").asText(null); List tagKeys = new ArrayList<>(); req.path("TagKeys").forEach(k -> tagKeys.add(k.asText())); - service.untagResource(arn, tagKeys); + service.untagResource(region, arn, tagKeys); return Response.ok(mapper.createObjectNode()).build(); } - private Response listTagsForResource(JsonNode req) { + private Response listTagsForResource(JsonNode req, String region) { String arn = req.path("ResourceArn").asText(null); - List> tagList = service.listTagsForResource(arn); + List> tagList = service.listTagsForResource(region, arn); ObjectNode resp = mapper.createObjectNode(); resp.set("Tags", mapper.valueToTree(tagList)); return Response.ok(resp).build(); diff --git a/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServiceAggregationAuthorizationTest.java b/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServiceAggregationAuthorizationTest.java index be0bfb7c65..9c435cfd27 100644 --- a/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServiceAggregationAuthorizationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServiceAggregationAuthorizationTest.java @@ -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> tags = service.listTagsForResource(created.aggregationAuthorizationArn()); + List> 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 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 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"); diff --git a/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServicePersistenceTest.java index e2872516bc..53e6a69d04 100644 --- a/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServicePersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServicePersistenceTest.java @@ -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 tags = reloaded.listTagsForResource(rule.configRuleArn()).stream() + Map 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"); 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 new file mode 100644 index 0000000000..f476dd71b7 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/configservice/AwsConfigServiceTaggingTest.java @@ -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> 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> tagStore = new ParkingStore<>(); + AwsConfigService service = new AwsConfigService(new RegionResolver(REGION, ACCOUNT), + new StorageFactory(null, null) { + @Override + @SuppressWarnings("unchecked") + public AccountAwareStorageBackend create(String serviceName, String fileName, + TypeReference> typeReference, WriteProfile profile) { + return fileName.equals("config-tags.json") + ? new AccountAwareStorageBackend<>((StorageBackend) (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 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); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/configservice/ConfigTaggingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/configservice/ConfigTaggingIntegrationTest.java index 47cf4ae5b8..1df4dac006 100644 --- a/src/test/java/io/github/hectorvent/floci/services/configservice/ConfigTaggingIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/configservice/ConfigTaggingIntegrationTest.java @@ -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) { diff --git a/src/test/java/io/github/hectorvent/floci/services/configservice/ConformancePackIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/configservice/ConformancePackIntegrationTest.java index 29f27dc320..989fec8ad8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/configservice/ConformancePackIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/configservice/ConformancePackIntegrationTest.java @@ -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