mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+195
@@ -0,0 +1,195 @@
|
||||
package io.github.hectorvent.floci.services.configservice;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
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 static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class AggregationAuthorizationIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
private static final String TARGET_PREFIX = "StarlingDoveService.";
|
||||
private static final String AUTHORIZED_ACCOUNT = "111122223333";
|
||||
private static final String AUTHORIZED_REGION = "eu-west-1";
|
||||
private static final String MATCHING = "AggregationAuthorizations.findAll { it.AuthorizedAccountId == '"
|
||||
+ AUTHORIZED_ACCOUNT + "' && it.AuthorizedAwsRegion == '" + AUTHORIZED_REGION + "' }";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
private static io.restassured.response.Response call(String action, String body) {
|
||||
return given()
|
||||
.header("X-Amz-Target", TARGET_PREFIX + action)
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
|
||||
private static String authorizationBody() {
|
||||
return """
|
||||
{
|
||||
"AuthorizedAccountId": "%s",
|
||||
"AuthorizedAwsRegion": "%s"
|
||||
}
|
||||
""".formatted(AUTHORIZED_ACCOUNT, AUTHORIZED_REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void putAggregationAuthorization() {
|
||||
call("PutAggregationAuthorization", """
|
||||
{
|
||||
"AuthorizedAccountId": "%s",
|
||||
"AuthorizedAwsRegion": "%s",
|
||||
"Tags": [{"Key": "env", "Value": "prod"}]
|
||||
}
|
||||
""".formatted(AUTHORIZED_ACCOUNT, AUTHORIZED_REGION))
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("AggregationAuthorization.AggregationAuthorizationArn",
|
||||
allOf(startsWith("arn:aws:config:"),
|
||||
endsWith(":aggregation-authorization/" + AUTHORIZED_ACCOUNT + "/" + AUTHORIZED_REGION)))
|
||||
.body("AggregationAuthorization.AuthorizedAccountId", equalTo(AUTHORIZED_ACCOUNT))
|
||||
.body("AggregationAuthorization.AuthorizedAwsRegion", equalTo(AUTHORIZED_REGION))
|
||||
.body("AggregationAuthorization.CreationTime", notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void putAggregationAuthorizationIsIdempotent() {
|
||||
String arn = call("PutAggregationAuthorization", authorizationBody())
|
||||
.then().statusCode(200)
|
||||
.extract().path("AggregationAuthorization.AggregationAuthorizationArn");
|
||||
|
||||
call("DescribeAggregationAuthorizations", "{}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(MATCHING, hasSize(1))
|
||||
.body(MATCHING + ".AggregationAuthorizationArn", contains(arn));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void describeAggregationAuthorizationsListsTheAuthorization() {
|
||||
call("DescribeAggregationAuthorizations", "{}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(MATCHING, hasSize(1))
|
||||
.body(MATCHING + "[0].CreationTime", notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void tagsSuppliedOnPutAreVisibleToListTagsForResource() {
|
||||
String arn = call("DescribeAggregationAuthorizations", "{}")
|
||||
.then().statusCode(200)
|
||||
.extract().<String>path(MATCHING + "[0].AggregationAuthorizationArn");
|
||||
|
||||
call("ListTagsForResource", "{\"ResourceArn\": \"" + arn + "\"}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Tags", hasSize(1))
|
||||
.body("Tags[0].Key", equalTo("env"))
|
||||
.body("Tags[0].Value", equalTo("prod"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void rePutIgnoresItsTagsAndTheTaggingApiStillWorks() {
|
||||
String arn = call("DescribeAggregationAuthorizations", "{}")
|
||||
.then().statusCode(200)
|
||||
.extract().<String>path(MATCHING + "[0].AggregationAuthorizationArn");
|
||||
String listTags = "{\"ResourceArn\": \"" + arn + "\"}";
|
||||
|
||||
call("PutAggregationAuthorization", """
|
||||
{
|
||||
"AuthorizedAccountId": "%s",
|
||||
"AuthorizedAwsRegion": "%s",
|
||||
"Tags": [{"Key": "env", "Value": "dev"}, {"Key": "tier", "Value": "gold"}]
|
||||
}
|
||||
""".formatted(AUTHORIZED_ACCOUNT, AUTHORIZED_REGION))
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
call("ListTagsForResource", listTags)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Tags", hasSize(1))
|
||||
.body("Tags[0].Key", equalTo("env"))
|
||||
.body("Tags[0].Value", equalTo("prod"));
|
||||
|
||||
call("TagResource", """
|
||||
{"ResourceArn": "%s", "Tags": [{"Key": "tier", "Value": "gold"}]}
|
||||
""".formatted(arn))
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
call("ListTagsForResource", listTags)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Tags.Key", containsInAnyOrder("env", "tier"));
|
||||
|
||||
call("UntagResource", """
|
||||
{"ResourceArn": "%s", "TagKeys": ["tier"]}
|
||||
""".formatted(arn))
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
call("ListTagsForResource", listTags)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Tags.Key", contains("env"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void deleteAggregationAuthorization() {
|
||||
call("DeleteAggregationAuthorization", authorizationBody())
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
call("DescribeAggregationAuthorizations", "{}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(MATCHING, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void deleteNonexistentAggregationAuthorizationSucceeds() {
|
||||
call("DeleteAggregationAuthorization", authorizationBody())
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void putRejectsAnAccountIdThatIsNotTwelveDigits() {
|
||||
call("PutAggregationAuthorization", """
|
||||
{"AuthorizedAccountId": "12345", "AuthorizedAwsRegion": "eu-west-1"}
|
||||
""")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValueException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void unrelatedActionsStillReportInvalidAction() {
|
||||
call("PutAggregationAuthorizations", authorizationBody())
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidAction"));
|
||||
}
|
||||
}
|
||||
+339
@@ -0,0 +1,339 @@
|
||||
package io.github.hectorvent.floci.services.configservice;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.configservice.model.AggregationAuthorization;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* AggregationAuthorization is keyed by (AuthorizedAccountId, AuthorizedAwsRegion). Put is an
|
||||
* upsert, and the Config model declares no "not found" error for DeleteAggregationAuthorization,
|
||||
* so deleting one that is absent succeeds.
|
||||
*/
|
||||
class AwsConfigServiceAggregationAuthorizationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String AUTHORIZED_ACCOUNT = "111122223333";
|
||||
|
||||
private AwsConfigService service() {
|
||||
return new AwsConfigService(new RegionResolver(REGION, ACCOUNT), null);
|
||||
}
|
||||
|
||||
private List<AggregationAuthorization> authorizations(AwsConfigService service, String region) {
|
||||
return service.describeAggregationAuthorizations(region, null, null).items();
|
||||
}
|
||||
|
||||
@Test
|
||||
void putReturnsAnArnAndCreationTime() {
|
||||
AwsConfigService service = service();
|
||||
|
||||
AggregationAuthorization created =
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(created.aggregationAuthorizationArn());
|
||||
assertEquals("aws", arn.partition());
|
||||
assertEquals("config", arn.service());
|
||||
assertEquals(REGION, arn.region());
|
||||
assertEquals(ACCOUNT, arn.accountId());
|
||||
assertEquals("aggregation-authorization/" + AUTHORIZED_ACCOUNT + "/eu-west-1", arn.resource());
|
||||
assertEquals(AUTHORIZED_ACCOUNT, created.authorizedAccountId());
|
||||
assertEquals("eu-west-1", created.authorizedAwsRegion());
|
||||
assertNotNull(created.creationTime(), "CreationTime must be populated");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putIsIdempotentForTheSameAccountAndRegion() {
|
||||
AwsConfigService service = service();
|
||||
|
||||
AggregationAuthorization first =
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
AggregationAuthorization second =
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
|
||||
assertEquals(first.aggregationAuthorizationArn(), second.aggregationAuthorizationArn());
|
||||
assertEquals(first.creationTime(), second.creationTime(),
|
||||
"re-authorizing must not restart the creation time");
|
||||
assertEquals(1, authorizations(service, REGION).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeListsEveryAuthorizationSortedByAccountThenRegion() {
|
||||
AwsConfigService service = service();
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "ap-south-1", null);
|
||||
service.putAggregationAuthorization(REGION, "444455556666", "eu-west-1", null);
|
||||
|
||||
List<AggregationAuthorization> listed = authorizations(service, REGION);
|
||||
|
||||
assertEquals(List.of(
|
||||
AUTHORIZED_ACCOUNT + "|ap-south-1",
|
||||
AUTHORIZED_ACCOUNT + "|eu-west-1",
|
||||
"444455556666|eu-west-1"),
|
||||
listed.stream()
|
||||
.map(a -> a.authorizedAccountId() + "|" + a.authorizedAwsRegion())
|
||||
.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeIsScopedToTheRequestRegion() {
|
||||
AwsConfigService service = service();
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
|
||||
assertEquals(1, authorizations(service, REGION).size());
|
||||
assertTrue(authorizations(service, "eu-central-1").isEmpty(),
|
||||
"an authorization must not leak into another region's aggregator");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteRemovesOnlyTheNamedAuthorization() {
|
||||
AwsConfigService service = service();
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "ap-south-1", null);
|
||||
|
||||
service.deleteAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1");
|
||||
|
||||
assertEquals(List.of("ap-south-1"),
|
||||
authorizations(service, REGION).stream()
|
||||
.map(AggregationAuthorization::authorizedAwsRegion).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingAnAbsentAuthorizationSucceeds() {
|
||||
AwsConfigService service = service();
|
||||
|
||||
assertDoesNotThrow(() -> service.deleteAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteClearsTagsEvenWhenTheAuthorizationIsAlreadyGone() {
|
||||
AwsConfigService service = service();
|
||||
String arn = service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null)
|
||||
.aggregationAuthorizationArn();
|
||||
|
||||
// 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")));
|
||||
assertTrue(authorizations(service, REGION).isEmpty(), "precondition: the entry is gone");
|
||||
|
||||
service.deleteAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1");
|
||||
|
||||
assertTrue(service.listTagsForResource(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)
|
||||
.aggregationAuthorizationArn(),
|
||||
"put reuses the same deterministic ARN, which is why the stale tags mattered");
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagsSuppliedOnPutAreReadableThroughListTagsForResource() {
|
||||
AwsConfigService service = service();
|
||||
|
||||
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());
|
||||
assertEquals(List.of(Map.of("Key", "env", "Value", "prod")), tags);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rePutIgnoresItsTagsAndLeavesTheCreationTagsInPlace() {
|
||||
AwsConfigService service = service();
|
||||
String arn = service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1",
|
||||
List.of(Map.of("Key", "env", "Value", "prod"),
|
||||
Map.of("Key", "owner", "Value", "platform")))
|
||||
.aggregationAuthorizationArn();
|
||||
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1",
|
||||
List.of(Map.of("Key", "env", "Value", "dev"),
|
||||
Map.of("Key", "tier", "Value", "gold")));
|
||||
|
||||
assertEquals(Map.of("env", "prod", "owner", "platform"), tagMap(service, arn),
|
||||
"a put that finds the authorization already there must ignore its Tags: "
|
||||
+ "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")));
|
||||
assertEquals(Map.of("env", "dev", "owner", "platform"), tagMap(service, arn),
|
||||
"TagResource must still update an existing tag");
|
||||
|
||||
service.untagResource(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()
|
||||
.collect(Collectors.toMap(t -> t.get("Key"), t -> t.get("Value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteDropsTheAuthorizationTagsSoARecreateStartsClean() {
|
||||
AwsConfigService service = service();
|
||||
AggregationAuthorization created = service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT,
|
||||
"eu-west-1", List.of(Map.of("Key", "env", "Value", "prod")));
|
||||
|
||||
service.deleteAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1");
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
|
||||
assertTrue(service.listTagsForResource(created.aggregationAuthorizationArn()).isEmpty(),
|
||||
"tags of a deleted authorization must not resurface on the reused ARN");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAnAccountIdThatIsNotTwelveDigits() {
|
||||
AwsConfigService service = service();
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> service.putAggregationAuthorization(REGION, "12345", "eu-west-1", null));
|
||||
assertEquals("InvalidParameterValueException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsAMissingAuthorizedRegion() {
|
||||
AwsConfigService service = service();
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, " ", null));
|
||||
assertEquals("InvalidParameterValueException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsALimitAboveTheModeledMaximum() {
|
||||
AwsConfigService service = service();
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> service.describeAggregationAuthorizations(REGION, 101, null));
|
||||
assertEquals("InvalidLimitException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describePaginatesWithNextToken() {
|
||||
AwsConfigService service = service();
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "ap-south-1", null);
|
||||
|
||||
AwsConfigService.Paged<AggregationAuthorization> first =
|
||||
service.describeAggregationAuthorizations(REGION, 1, null);
|
||||
assertEquals(1, first.items().size());
|
||||
assertNotNull(first.nextToken());
|
||||
|
||||
AwsConfigService.Paged<AggregationAuthorization> second =
|
||||
service.describeAggregationAuthorizations(REGION, 1, first.nextToken());
|
||||
assertEquals(1, second.items().size());
|
||||
assertNull(second.nextToken());
|
||||
assertEquals("eu-west-1", second.items().getFirst().authorizedAwsRegion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Idempotency that only usually holds is not idempotency. Put is a get-then-put, so without a
|
||||
* lock two concurrent Puts for one (account, Region) both find nothing and both create, and
|
||||
* the loser's caller is handed an object the store no longer contains.
|
||||
*
|
||||
* <p>Reference identity is the assertion that catches this, and the obvious alternatives do
|
||||
* not: 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 too.
|
||||
*
|
||||
* <p>Many short rounds rather than one wide one: the unlocked window is a few instructions, so
|
||||
* what makes the race land is repeated fresh starts, not more threads on one start.
|
||||
*/
|
||||
@Test
|
||||
void concurrentPutsForOneKeyAllReturnTheStoredAuthorization() throws Exception {
|
||||
int rounds = 300;
|
||||
int threads = 4;
|
||||
ExecutorService pool = Executors.newFixedThreadPool(threads);
|
||||
try {
|
||||
int mismatches = 0;
|
||||
for (int round = 0; round < rounds; round++) {
|
||||
AwsConfigService service = service();
|
||||
CyclicBarrier gate = new CyclicBarrier(threads);
|
||||
List<Future<AggregationAuthorization>> futures = new ArrayList<>();
|
||||
for (int i = 0; i < threads; i++) {
|
||||
futures.add(pool.submit(() -> {
|
||||
gate.await(10, TimeUnit.SECONDS);
|
||||
return service.putAggregationAuthorization(
|
||||
REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
}));
|
||||
}
|
||||
List<AggregationAuthorization> returned = new ArrayList<>();
|
||||
for (Future<AggregationAuthorization> future : futures) {
|
||||
returned.add(future.get(10, TimeUnit.SECONDS));
|
||||
}
|
||||
List<AggregationAuthorization> stored = authorizations(service, REGION);
|
||||
assertEquals(1, stored.size());
|
||||
for (AggregationAuthorization each : returned) {
|
||||
if (each != stored.getFirst()) {
|
||||
mismatches++;
|
||||
}
|
||||
}
|
||||
}
|
||||
assertEquals(0, mismatches,
|
||||
"a concurrent Put returned an authorization the store does not hold");
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
assertTrue(pool.awaitTermination(10, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
/** Delete takes the same lock, so it cannot interleave with the create half of a Put. */
|
||||
@Test
|
||||
void concurrentPutAndDeleteLeaveNoHalfState() throws Exception {
|
||||
AwsConfigService service = service();
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
for (int round = 0; round < 50; round++) {
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
Future<?> put = pool.submit(() -> {
|
||||
start.await();
|
||||
return service.putAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1", null);
|
||||
});
|
||||
Future<?> delete = pool.submit(() -> {
|
||||
start.await();
|
||||
service.deleteAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1");
|
||||
return null;
|
||||
});
|
||||
start.countDown();
|
||||
put.get(10, TimeUnit.SECONDS);
|
||||
delete.get(10, TimeUnit.SECONDS);
|
||||
|
||||
// Either order is legal; what is not legal is an entry whose tags outlived a delete.
|
||||
List<AggregationAuthorization> after = authorizations(service, REGION);
|
||||
assertTrue(after.size() <= 1);
|
||||
if (after.isEmpty()) {
|
||||
assertTrue(service.listTagsForResource(
|
||||
AwsArnUtils.Arn.of("config", REGION, ACCOUNT,
|
||||
"aggregation-authorization/" + AUTHORIZED_ACCOUNT + "/eu-west-1").toString())
|
||||
.isEmpty(), "delete left tags behind for a removed authorization");
|
||||
}
|
||||
service.deleteAggregationAuthorization(REGION, AUTHORIZED_ACCOUNT, "eu-west-1");
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
assertTrue(pool.awaitTermination(10, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user