mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): keep stack sets with their instances and operations in their Region (#8987)
This commit is contained in:
1 parent
c2721ca938
commit
573cb9e9b7
12 files changed
+657
-50
No files matched your search
@@ -3,6 +3,7 @@ package io.floci.aws.core.storage;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.testing.StorageFactories;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
@@ -136,11 +137,69 @@ class RegionScopedStorageTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A scan of every Region gives each of the request's account's values with the Region whose view holds it and its
|
||||
* key there, a regionless one the default Region's, whatever the request's Region, and no other account's.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"memory", "persistent", "hybrid", "wal"})
|
||||
void aScanOfEveryRegionGivesEachValueTheRegionHoldingIt(String mode) {
|
||||
StorageFactory factory = StorageFactories.open(directory, mode, "000000000000");
|
||||
try {
|
||||
AccountAwareStorageBackend<String> legacy = factory.create(
|
||||
"appconfig", "records.json", new TypeReference<>() {});
|
||||
legacy.put("older", "regionless");
|
||||
legacy.putForAccount("111111111111", "us-west-2/app", "another-account");
|
||||
store(factory, "us-west-2").put("app", "west");
|
||||
store(factory, "eu-west-1").put("app", "eu");
|
||||
store(factory, "eu-west-1").put("my-cd-1/plan", "eu-plan");
|
||||
|
||||
assertEquals(Set.of(
|
||||
new RegionScopedStorage.RegionEntry<>("us-east-1", "older", "regionless"),
|
||||
new RegionScopedStorage.RegionEntry<>("us-west-2", "app", "west"),
|
||||
new RegionScopedStorage.RegionEntry<>("eu-west-1", "app", "eu"),
|
||||
new RegionScopedStorage.RegionEntry<>("eu-west-1", "my-cd-1/plan", "eu-plan")),
|
||||
Set.copyOf(store(factory, "us-west-2").scanEveryRegion()));
|
||||
} finally {
|
||||
factory.shutdownAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A read in an account and Region, as IAM reads the tags of the resource an ARN names, whatever the request's: it
|
||||
* finds what that Region's view holds in that account, a regionless key only for the default Region and a key
|
||||
* stored before accounts only for the default account, and moves nothing.
|
||||
*/
|
||||
@Test
|
||||
void aReadInAnAccountAndRegionFindsItsKeyInPlace() {
|
||||
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
|
||||
RegionScopedStorage<String> east = new RegionScopedStorage<>(
|
||||
new AccountAwareStorageBackend<>(raw, null, "000000000000"), regions("us-east-1"));
|
||||
raw.put("111111111111/us-west-2/app", "west");
|
||||
raw.put("111111111111/older", "regionless");
|
||||
raw.put("oldest", "before-accounts");
|
||||
|
||||
assertEquals(Optional.of("west"), east.readInAccount("111111111111", "us-west-2", "app"));
|
||||
assertEquals(Optional.empty(), east.readInAccount("111111111111", "us-east-1", "app"));
|
||||
assertEquals(Optional.empty(), east.readInAccount("222222222222", "us-west-2", "app"));
|
||||
assertEquals(Optional.of("regionless"), east.readInAccount("111111111111", "us-east-1", "older"));
|
||||
assertEquals(Optional.empty(), east.readInAccount("111111111111", "us-west-2", "older"));
|
||||
assertEquals(Optional.of("before-accounts"), east.readInAccount("000000000000", "us-east-1", "oldest"));
|
||||
assertEquals(Optional.empty(), east.readInAccount("111111111111", "us-east-1", "oldest"));
|
||||
assertEquals(Set.of("111111111111/us-west-2/app", "111111111111/older", "oldest"), raw.keys());
|
||||
}
|
||||
|
||||
private static RegionScopedStorage<String> store(StorageFactory factory, String region) {
|
||||
return new RegionScopedStorage<>(factory, regions(region), "appconfig", "records.json",
|
||||
new TypeReference<>() {});
|
||||
}
|
||||
|
||||
/** A resolver whose request is in {@code region}, {@code us-east-1} being the default Region. */
|
||||
private static RegionResolver regions(String region) {
|
||||
RegionResolver regions = mock(RegionResolver.class);
|
||||
when(regions.getDefaultRegion()).thenReturn("us-east-1");
|
||||
when(regions.getRegion()).thenReturn(region);
|
||||
return new RegionScopedStorage<>(factory, regions, "appconfig", "records.json", new TypeReference<>() {});
|
||||
return regions;
|
||||
}
|
||||
|
||||
}
|
||||
+5
-10
@@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.floci.aws.testing.StackSetErrors.assertNotFound;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
@@ -540,14 +541,11 @@ class CloudFormationStackSetsIntegrationTest {
|
||||
String queue = "errq-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
// Describe a non-existent stack set → StackSetNotFoundException.
|
||||
given()
|
||||
assertNotFound(given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackSet")
|
||||
.formParam("StackSetName", "does-not-exist-" + setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(404)
|
||||
.body(containsString("StackSetNotFoundException"));
|
||||
.header("Authorization", auth(ADMIN, "cloudformation")), "does-not-exist-" + setName);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
@@ -919,14 +917,11 @@ class CloudFormationStackSetsIntegrationTest {
|
||||
.body(containsString("<Code>ValidationError</Code>"))
|
||||
.body(containsString("<Message>Parameters: [Suffix] must have values</Message>"));
|
||||
|
||||
given()
|
||||
assertNotFound(given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(404)
|
||||
.body(containsString("StackSetNotFoundException"));
|
||||
.header("Authorization", auth(ADMIN, "cloudformation")), setName);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package io.floci.aws.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.services.cloudformation.model.StackInstance;
|
||||
import io.floci.aws.services.cloudformation.model.StackSet;
|
||||
import io.floci.aws.services.cloudformation.model.StackSetAutoDeploymentTarget;
|
||||
import io.floci.aws.services.cloudformation.model.StackSetOperation;
|
||||
import io.floci.aws.services.organizations.OrganizationsService;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.floci.aws.testing.StackSetErrors.assertNotFound;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* A stack set stored before stack sets were kept per Region, with its instance, operation and automatic deployment
|
||||
* target, recorded no Region: it is the default Region's, listed and reached there and not in another Region, where
|
||||
* its name is free. The stores are account stores, as in production.
|
||||
*/
|
||||
class StackSetLegacyRecordsTest {
|
||||
|
||||
private static final String ACCOUNT = "123456789012";
|
||||
private static final String DEFAULT_REGION = "us-east-1";
|
||||
private static final String OTHER_REGION = "eu-west-1";
|
||||
private static final String SET = "stored-before";
|
||||
private static final String ID = SET + ":5f0c3a52-8d4e-4c41-9b7a-2f1e6d0a9c11";
|
||||
private static final String TARGET_ACCOUNT = "210987654321";
|
||||
private static final String OU = "ou-abcd-abcdefgh";
|
||||
|
||||
private final RequestRegion regions = new RequestRegion();
|
||||
private StackSetService service;
|
||||
|
||||
@BeforeEach
|
||||
void storeRecordsWithoutARegion() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory(ACCOUNT);
|
||||
StackSet stackSet = new StackSet();
|
||||
stackSet.setStackSetName(SET);
|
||||
stackSet.setStackSetId(ID);
|
||||
stackSet.setTemplateBody("{\"Resources\":{}}");
|
||||
stackSet.setPermissionModel("SERVICE_MANAGED");
|
||||
stackSet.setTags(Map.of("team", "red"));
|
||||
storage.create("cloudformation", "cloudformation-stacksets.json",
|
||||
new TypeReference<Map<String, StackSet>>() {}).put(SET, stackSet);
|
||||
StackInstance instance = new StackInstance();
|
||||
instance.setStackSetId(ID);
|
||||
instance.setStackSetName(SET);
|
||||
instance.setAccount(TARGET_ACCOUNT);
|
||||
instance.setRegion("eu-central-1");
|
||||
storage.create("cloudformation", "cloudformation-stackset-instances.json",
|
||||
new TypeReference<Map<String, StackInstance>>() {})
|
||||
.put(SET + ":" + TARGET_ACCOUNT + ":eu-central-1", instance);
|
||||
storage.create("cloudformation", "cloudformation-stackset-operations.json",
|
||||
new TypeReference<Map<String, StackSetOperation>>() {})
|
||||
.put(SET + ":op-1", new StackSetOperation("op-1", SET, "CREATE"));
|
||||
storage.create("cloudformation", "cloudformation-stackset-auto-targets.json",
|
||||
new TypeReference<Map<String, StackSetAutoDeploymentTarget>>() {})
|
||||
.put(SET + "::" + OU, new StackSetAutoDeploymentTarget(SET, OU, List.of("eu-central-1")));
|
||||
service = new StackSetService(mock(CloudFormationService.class), storage, mock(OrganizationsService.class),
|
||||
regions);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStackSetStoredBeforeIsListedAndReachedInTheDefaultRegionOnly() {
|
||||
assertEquals(ID, service.describeStackSet(SET).getStackSetId());
|
||||
assertEquals(List.of(SET), service.listStackSets().stream().map(StackSet::getStackSetName).toList());
|
||||
assertEquals(List.of(TARGET_ACCOUNT + "/eu-central-1"), service.listStackInstances(SET, null, null).stream()
|
||||
.map(instance -> instance.getAccount() + "/" + instance.getRegion()).toList());
|
||||
assertEquals("CREATE", service.describeStackSetOperation(SET, "op-1").getAction());
|
||||
assertEquals(List.of(OU), service.organizationalUnitIds(SET));
|
||||
assertEquals(Map.of("team", "red"), service.storedTags(arn(DEFAULT_REGION)));
|
||||
|
||||
regions.region = OTHER_REGION;
|
||||
|
||||
assertNotFound(() -> service.describeStackSet(SET), SET);
|
||||
assertEquals(List.of(), service.listStackSets());
|
||||
assertNotFound(() -> service.listStackInstances(SET, null, null), SET);
|
||||
assertNotFound(() -> service.describeStackSetOperation(SET, "op-1"), SET);
|
||||
assertEquals(Map.of(), service.storedTags(arn(OTHER_REGION)));
|
||||
|
||||
StackSet created = service.createStackSet(SET, "{\"Resources\":{}}", null, null, null, null);
|
||||
assertNotEquals(ID, created.getStackSetId());
|
||||
regions.region = DEFAULT_REGION;
|
||||
assertEquals(ID, service.describeStackSet(SET).getStackSetId());
|
||||
}
|
||||
|
||||
private static String arn(String region) {
|
||||
return "arn:aws:cloudformation:" + region + ":" + ACCOUNT + ":stackset/" + ID;
|
||||
}
|
||||
|
||||
/** A resolver whose request is in the Region a test sets, {@code us-east-1} being the default Region. */
|
||||
private static final class RequestRegion extends RegionResolver {
|
||||
private volatile String region = DEFAULT_REGION;
|
||||
|
||||
RequestRegion() {
|
||||
super(DEFAULT_REGION, ACCOUNT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRegion() {
|
||||
return region;
|
||||
}
|
||||
}
|
||||
}
|
||||
+318
@@ -0,0 +1,318 @@
|
||||
package io.floci.aws.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.cloudformation.model.StackSet;
|
||||
import io.floci.aws.services.cloudformation.model.StackSetAutoDeploymentTarget;
|
||||
import io.floci.aws.testing.CfnRequests;
|
||||
import io.floci.aws.testing.OrganizationsFixture;
|
||||
import io.floci.aws.testing.PartitionCleanup;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.floci.aws.testing.SidecarContainersProfile;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static io.floci.aws.testing.EnforcementFixtures.auth;
|
||||
import static io.floci.aws.testing.StackSetErrors.assertNotFound;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
/**
|
||||
* A stack set is Regional, as on AWS: it is listed, described, changed and deleted only in the Region it was created
|
||||
* in, its administrator Region, and so are its instances and operations, while its instances deploy stacks in the
|
||||
* Regions they name. Its name can be used again in another Region, and automatic deployment runs in its Region,
|
||||
* whatever Region the Organizations call was sent to, the default Region for one stored before, without a Region, and
|
||||
* the stack set's own for one of a Region no partition publishes, served with {@code allow-unknown-regions} (from
|
||||
* {@link SidecarContainersProfile}). Each test works in an account of its own.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(SidecarContainersProfile.class)
|
||||
class StackSetRegionScopingIntegrationTest {
|
||||
|
||||
private static final AtomicLong ACCOUNTS = new AtomicLong(732200000000L);
|
||||
private static final String HOME = "eu-west-1";
|
||||
private static final String OTHER = "eu-central-1";
|
||||
/** The deployment's default Region, where the Organizations calls are sent unless a test sends one elsewhere. */
|
||||
private static final String DEFAULT_REGION = "us-east-1";
|
||||
/** A Region no partition publishes, served with {@code allow-unknown-regions}. */
|
||||
private static final String UNKNOWN = "polygondwanaland-west-1";
|
||||
private static final String OU_TARGET = "DeploymentTargets.OrganizationalUnitIds.member.1";
|
||||
private static final String INSTANCES = "ListStackInstancesResponse.ListStackInstancesResult.Summaries.member";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Inject
|
||||
StorageFactory storageFactory;
|
||||
|
||||
private final OrganizationsFixture organizations = new OrganizationsFixture();
|
||||
private String account;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void accountOfItsOwn() {
|
||||
account = Long.toString(ACCOUNTS.incrementAndGet());
|
||||
}
|
||||
|
||||
/**
|
||||
* A stack set of {@code eu-west-1} with an instance in {@code eu-central-1} is neither listed nor reached from
|
||||
* {@code eu-central-1}, where its instance's stack is: not described, changed, deployed or deleted there, nor are
|
||||
* its instances and operations.
|
||||
*/
|
||||
@Test
|
||||
void aStackSetIsListedAndReachedOnlyInTheRegionItWasCreatedIn() {
|
||||
String stackSet = "regional";
|
||||
createStackSet(HOME, stackSet, "regional-q").statusCode(200);
|
||||
String operation = cfn(HOME, "CreateStackInstances")
|
||||
.formParam("StackSetName", stackSet)
|
||||
.formParam("Accounts.member.1", account)
|
||||
.formParam("Regions.member.1", OTHER)
|
||||
.post("/").then().statusCode(200)
|
||||
.extract().path("CreateStackInstancesResponse.CreateStackInstancesResult.OperationId");
|
||||
cleanup.register(() -> deleteWithInstances(HOME, stackSet, "Accounts.member.1", account));
|
||||
|
||||
assertEquals(List.of(), stackSetNames(OTHER));
|
||||
assertNotFound(cfn(OTHER, "DescribeStackSet").formParam("StackSetName", stackSet), stackSet);
|
||||
assertNotFound(cfn(OTHER, "ListStackInstances").formParam("StackSetName", stackSet), stackSet);
|
||||
assertNotFound(cfn(OTHER, "DescribeStackInstance").formParam("StackSetName", stackSet)
|
||||
.formParam("StackInstanceAccount", account).formParam("StackInstanceRegion", OTHER), stackSet);
|
||||
assertNotFound(cfn(OTHER, "ListStackSetOperations").formParam("StackSetName", stackSet), stackSet);
|
||||
assertNotFound(cfn(OTHER, "DescribeStackSetOperation").formParam("StackSetName", stackSet)
|
||||
.formParam("OperationId", operation), stackSet);
|
||||
assertNotFound(cfn(OTHER, "UpdateStackSet").formParam("StackSetName", stackSet)
|
||||
.formParam("UsePreviousTemplate", "true"), stackSet);
|
||||
assertNotFound(cfn(OTHER, "CreateStackInstances").formParam("StackSetName", stackSet)
|
||||
.formParam("Accounts.member.1", account).formParam("Regions.member.1", HOME), stackSet);
|
||||
assertNotFound(cfn(OTHER, "DeleteStackInstances").formParam("StackSetName", stackSet)
|
||||
.formParam("Accounts.member.1", account).formParam("Regions.member.1", OTHER)
|
||||
.formParam("RetainStacks", "false"), stackSet);
|
||||
assertNotFound(cfn(OTHER, "DeleteStackSet").formParam("StackSetName", stackSet), stackSet);
|
||||
|
||||
assertEquals(List.of(stackSet), stackSetNames(HOME));
|
||||
cfn(HOME, "DescribeStackSet").formParam("StackSetName", stackSet).post("/").then().statusCode(200)
|
||||
.body("DescribeStackSetResponse.DescribeStackSetResult.StackSet.StackSetARN",
|
||||
startsWith("arn:aws:cloudformation:" + HOME + ":" + account + ":stackset/" + stackSet + ":"));
|
||||
cfn(HOME, "DescribeStackSetOperation").formParam("StackSetName", stackSet).formParam("OperationId", operation)
|
||||
.post("/").then().statusCode(200)
|
||||
.body("DescribeStackSetOperationResponse.DescribeStackSetOperationResult.StackSetOperation.Status",
|
||||
equalTo("SUCCEEDED"));
|
||||
cfn(HOME, "ListStackInstances").formParam("StackSetName", stackSet).post("/").then().statusCode(200)
|
||||
.body("ListStackInstancesResponse.ListStackInstancesResult.Summaries.member.Region", equalTo(OTHER))
|
||||
.body("ListStackInstancesResponse.ListStackInstancesResult.Summaries.member.StackId",
|
||||
startsWith("arn:aws:cloudformation:" + OTHER + ":" + account + ":stack/StackSet-" + stackSet));
|
||||
queueUrl(account, OTHER, "regional-q").statusCode(200)
|
||||
.body("GetQueueUrlResponse.GetQueueUrlResult.QueueUrl", endsWith("/" + account + "/regional-q"));
|
||||
}
|
||||
|
||||
/** A name is unique in its Region: each Region holds a stack set of its own under it, and deletes only its own. */
|
||||
@Test
|
||||
void aStackSetNameCanBeUsedAgainInAnotherRegion() {
|
||||
String stackSet = "shared";
|
||||
String home = createStackSet(HOME, stackSet, "shared-home-q").statusCode(200)
|
||||
.extract().path("CreateStackSetResponse.CreateStackSetResult.StackSetId");
|
||||
cleanup.register(() -> cfn(HOME, "DeleteStackSet").formParam("StackSetName", stackSet).post("/"));
|
||||
String other = createStackSet(OTHER, stackSet, "shared-other-q").statusCode(200)
|
||||
.extract().path("CreateStackSetResponse.CreateStackSetResult.StackSetId");
|
||||
cleanup.register(() -> cfn(OTHER, "DeleteStackSet").formParam("StackSetName", stackSet).post("/"));
|
||||
assertNotEquals(home, other);
|
||||
|
||||
createStackSet(HOME, stackSet, "shared-again-q").statusCode(409)
|
||||
.body("ErrorResponse.Error.Code", equalTo("NameAlreadyExistsException"));
|
||||
cfn(OTHER, "DeleteStackSet").formParam("StackSetName", stackSet).post("/").then().statusCode(200);
|
||||
|
||||
assertNotFound(cfn(OTHER, "DescribeStackSet").formParam("StackSetName", stackSet), stackSet);
|
||||
cfn(HOME, "DescribeStackSet").formParam("StackSetName", stackSet).post("/").then().statusCode(200)
|
||||
.body("DescribeStackSetResponse.DescribeStackSetResult.StackSet.StackSetId", equalTo(home));
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatic deployment runs in each stack set's Region: an account moved, by an Organizations call sent to
|
||||
* {@code us-east-1}, under the target of two service-managed stack sets of one name, one of {@code eu-west-1}
|
||||
* and one of {@code eu-central-1}, gets each one's instance, each automatic operation is its stack set's, in its
|
||||
* Region, and {@code us-east-1} holds no such stack set.
|
||||
*/
|
||||
@Test
|
||||
void automaticDeploymentRunsInEachStackSetsRegion() {
|
||||
cleanup.register(organizations::cleanup);
|
||||
String root = organizations.createWithTrustedAccess(account);
|
||||
String target = organizations.createOu(account, root, "RegionalTarget");
|
||||
String stackSet = "regional-auto";
|
||||
for (String region : List.of(HOME, OTHER)) {
|
||||
cfn(region, "CreateStackSet")
|
||||
.formParam("StackSetName", stackSet)
|
||||
.formParam("TemplateBody", queueTemplate(region + "-auto-q"))
|
||||
.formParam("PermissionModel", "SERVICE_MANAGED")
|
||||
.formParam("AutoDeployment.Enabled", "true")
|
||||
.formParam("AutoDeployment.RetainStacksOnAccountRemoval", "false")
|
||||
.post("/").then().statusCode(200);
|
||||
cfn(region, "CreateStackInstances")
|
||||
.formParam("StackSetName", stackSet)
|
||||
.formParam(OU_TARGET, target)
|
||||
.formParam("Regions.member.1", OTHER)
|
||||
.post("/").then().statusCode(200);
|
||||
cleanup.register(() -> deleteWithInstances(region, stackSet, OU_TARGET, target));
|
||||
}
|
||||
|
||||
String member = organizations.createAccount(account, "regional-auto-" + account + "@example.com",
|
||||
"RegionalAuto");
|
||||
organizations.moveAccount(account, member, root, target);
|
||||
|
||||
for (String region : List.of(HOME, OTHER)) {
|
||||
awaitInstance(region, stackSet, member);
|
||||
queueUrl(member, OTHER, region + "-auto-q").statusCode(200);
|
||||
assertEquals(List.of("CREATE", "CREATE"), operationActions(region, stackSet));
|
||||
}
|
||||
assertEquals(List.of(), stackSetNames(DEFAULT_REGION));
|
||||
assertNotFound(cfn(DEFAULT_REGION, "ListStackSetOperations").formParam("StackSetName", stackSet), stackSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* An automatically deploying stack set stored before, without a Region, is the default Region's: an account moved
|
||||
* under its target by an Organizations call sent to {@code eu-west-1} gets its instance, its automatic operation is
|
||||
* recorded in {@code us-east-1}, and {@code eu-west-1} holds no such stack set.
|
||||
*/
|
||||
@Test
|
||||
void aStackSetStoredBeforeDeploysAutomaticallyInTheDefaultRegion() {
|
||||
cleanup.register(organizations::cleanup);
|
||||
String root = organizations.createWithTrustedAccess(account);
|
||||
String target = organizations.createOu(account, root, "StoredBeforeTarget");
|
||||
String stackSet = "stored-before";
|
||||
StackSet stored = new StackSet();
|
||||
stored.setStackSetName(stackSet);
|
||||
stored.setStackSetId(stackSet + ":" + UUID.randomUUID());
|
||||
stored.setTemplateBody(queueTemplate("stored-before-q"));
|
||||
stored.setPermissionModel("SERVICE_MANAGED");
|
||||
stored.setAutoDeploymentEnabled(true);
|
||||
storageFactory.create("cloudformation", "cloudformation-stacksets.json",
|
||||
new TypeReference<Map<String, StackSet>>() {}).putForAccount(account, stackSet, stored);
|
||||
storageFactory.create("cloudformation", "cloudformation-stackset-auto-targets.json",
|
||||
new TypeReference<Map<String, StackSetAutoDeploymentTarget>>() {})
|
||||
.putForAccount(account, stackSet + "::" + target,
|
||||
new StackSetAutoDeploymentTarget(stackSet, target, List.of(OTHER)));
|
||||
cleanup.register(() -> deleteWithInstances(DEFAULT_REGION, stackSet, OU_TARGET, target));
|
||||
|
||||
String member = organizations.createAccount(account, "stored-before-" + account + "@example.com",
|
||||
"StoredBefore");
|
||||
organizations.moveAccount(account, HOME, member, root, target);
|
||||
|
||||
awaitInstance(DEFAULT_REGION, stackSet, member);
|
||||
queueUrl(member, OTHER, "stored-before-q").statusCode(200);
|
||||
assertEquals(List.of("CREATE"), operationActions(DEFAULT_REGION, stackSet));
|
||||
assertEquals(List.of(), stackSetNames(HOME));
|
||||
}
|
||||
|
||||
/**
|
||||
* A stack set of a Region no partition publishes deploys automatically in that Region: an account moved under its
|
||||
* target, by an Organizations call sent to {@code us-east-1}, gets its instance, and its automatic operation is
|
||||
* recorded there.
|
||||
*/
|
||||
@Test
|
||||
void aStackSetOfAnUnknownRegionDeploysAutomaticallyThere() {
|
||||
cleanup.register(organizations::cleanup);
|
||||
String root = organizations.createWithTrustedAccess(account);
|
||||
String target = organizations.createOu(account, root, "UnknownRegionTarget");
|
||||
String stackSet = "unknown-region-auto";
|
||||
cfn(UNKNOWN, "CreateStackSet")
|
||||
.formParam("StackSetName", stackSet)
|
||||
.formParam("TemplateBody", queueTemplate("unknown-region-auto-q"))
|
||||
.formParam("PermissionModel", "SERVICE_MANAGED")
|
||||
.formParam("AutoDeployment.Enabled", "true")
|
||||
.formParam("AutoDeployment.RetainStacksOnAccountRemoval", "false")
|
||||
.post("/").then().statusCode(200);
|
||||
cfn(UNKNOWN, "CreateStackInstances")
|
||||
.formParam("StackSetName", stackSet)
|
||||
.formParam(OU_TARGET, target)
|
||||
.formParam("Regions.member.1", OTHER)
|
||||
.post("/").then().statusCode(200);
|
||||
cleanup.register(() -> deleteWithInstances(UNKNOWN, stackSet, OU_TARGET, target));
|
||||
|
||||
String member = organizations.createAccount(account, "unknown-region-" + account + "@example.com",
|
||||
"UnknownRegion");
|
||||
organizations.moveAccount(account, member, root, target);
|
||||
|
||||
awaitInstance(UNKNOWN, stackSet, member);
|
||||
queueUrl(member, OTHER, "unknown-region-auto-q").statusCode(200);
|
||||
assertEquals(List.of("CREATE", "CREATE"), operationActions(UNKNOWN, stackSet));
|
||||
}
|
||||
|
||||
private ValidatableResponse createStackSet(String region, String stackSet, String queue) {
|
||||
return cfn(region, "CreateStackSet")
|
||||
.formParam("StackSetName", stackSet)
|
||||
.formParam("TemplateBody", queueTemplate(queue))
|
||||
.post("/").then();
|
||||
}
|
||||
|
||||
/** The names {@code ListStackSets} lists in {@code region}. */
|
||||
private List<String> stackSetNames(String region) {
|
||||
return cfn(region, "ListStackSets").post("/").then().statusCode(200).extract().xmlPath()
|
||||
.getList("ListStackSetsResponse.ListStackSetsResult.Summaries.member.StackSetName", String.class);
|
||||
}
|
||||
|
||||
/** The actions of the operations {@code ListStackSetOperations} lists for the {@code region} stack set. */
|
||||
private List<String> operationActions(String region, String stackSet) {
|
||||
return cfn(region, "ListStackSetOperations").formParam("StackSetName", stackSet)
|
||||
.post("/").then().statusCode(200).extract().xmlPath().getList(
|
||||
"ListStackSetOperationsResponse.ListStackSetOperationsResult.Summaries.member.Action",
|
||||
String.class);
|
||||
}
|
||||
|
||||
/** Waits until the {@code region} stack set has a successful instance in {@code member}'s {@code eu-central-1}. */
|
||||
private void awaitInstance(String region, String stackSet, String member) {
|
||||
await().atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(100)).untilAsserted(() ->
|
||||
cfn(region, "ListStackInstances").formParam("StackSetName", stackSet)
|
||||
.formParam("StackInstanceAccount", member)
|
||||
.post("/").then().statusCode(200)
|
||||
.body(INSTANCES + ".Region", equalTo(OTHER))
|
||||
.body(INSTANCES + ".StackInstanceStatus.DetailedStatus", equalTo("SUCCEEDED")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the {@code region} stack set's instances in {@code eu-central-1} of the account or the root or OU
|
||||
* {@code target} names, as the request member {@code targetMember}, with their stacks, then the stack set.
|
||||
*/
|
||||
private void deleteWithInstances(String region, String stackSet, String targetMember, String target) {
|
||||
cfn(region, "DeleteStackInstances").formParam("StackSetName", stackSet)
|
||||
.formParam(targetMember, target).formParam("Regions.member.1", OTHER)
|
||||
.formParam("RetainStacks", "false").post("/");
|
||||
cfn(region, "DeleteStackSet").formParam("StackSetName", stackSet).post("/");
|
||||
}
|
||||
|
||||
private RequestSpecification cfn(String region, String action) {
|
||||
return CfnRequests.cfn(action, null).header("Authorization", auth(account, region, "cloudformation"));
|
||||
}
|
||||
|
||||
private static ValidatableResponse queueUrl(String owner, String region, String queue) {
|
||||
return given().header("Authorization", auth(owner, region, "sqs"))
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "GetQueueUrl")
|
||||
.formParam("QueueName", queue)
|
||||
.post("/").then();
|
||||
}
|
||||
|
||||
private static String queueTemplate(String queue) {
|
||||
return "{\"Resources\":{\"Q\":{\"Type\":\"AWS::SQS::Queue\",\"Properties\":{\"QueueName\":\""
|
||||
+ queue + "\"}}}}";
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -17,6 +17,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static io.floci.aws.testing.StackSetErrors.assertNotFound;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -120,8 +121,7 @@ class StackSetServiceDeleteFailureTest {
|
||||
service.deleteStackSetWithInstances(SET);
|
||||
service.deleteStackSetWithInstances(SET);
|
||||
|
||||
AwsException missing = assertThrows(AwsException.class, () -> service.describeStackSet(SET));
|
||||
assertEquals("StackSetNotFoundException", missing.getErrorCode());
|
||||
assertNotFound(() -> service.describeStackSet(SET), SET);
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
|
||||
@@ -51,8 +51,13 @@ public final class OrganizationsFixture {
|
||||
}
|
||||
|
||||
public void moveAccount(String owner, String account, String source, String destination) {
|
||||
request(owner, "MoveAccount", "{\"AccountId\":\"" + account + "\",\"SourceParentId\":\"" + source
|
||||
+ "\",\"DestinationParentId\":\"" + destination + "\"}")
|
||||
moveAccount(owner, "us-east-1", account, source, destination);
|
||||
}
|
||||
|
||||
/** As above; the call signed for {@code region}. */
|
||||
public void moveAccount(String owner, String region, String account, String source, String destination) {
|
||||
OrganizationsRequests.request(owner, region, "MoveAccount", "{\"AccountId\":\"" + account
|
||||
+ "\",\"SourceParentId\":\"" + source + "\",\"DestinationParentId\":\"" + destination + "\"}")
|
||||
.post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,8 +18,13 @@ public final class OrganizationsRequests {
|
||||
|
||||
/** One call, named by its action, carrying {@code body} as its payload. */
|
||||
public static RequestSpecification request(String accountId, String action, String body) {
|
||||
return request(accountId, "us-east-1", action, body);
|
||||
}
|
||||
|
||||
/** As above; signed for {@code region}, as a client configured for that Region signs it. */
|
||||
public static RequestSpecification request(String accountId, String region, String action, String body) {
|
||||
return given()
|
||||
.header("Authorization", EnforcementFixtures.auth(accountId, "organizations"))
|
||||
.header("Authorization", EnforcementFixtures.auth(accountId, region, "organizations"))
|
||||
.header("X-Amz-Target", TARGET_PREFIX + action)
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body(body);
|
||||
|
||||
@@ -74,8 +74,8 @@ import java.util.Map;
|
||||
* outside the subset Floci evaluates. {@code IotTopicRuleSqlStrictIntegrationTest} relies on
|
||||
* it.</li>
|
||||
* <li>{@code floci.partitions.allow-unknown-regions=true} gives a region label no partition
|
||||
* publishes a namespace of its own. {@code PartitionUnknownRegionAllowedIntegrationTest} and
|
||||
* {@code ColonRegionRejectionIntegrationTest} rely on it.</li>
|
||||
* publishes a namespace of its own. {@code PartitionUnknownRegionAllowedIntegrationTest},
|
||||
* {@code ColonRegionRejectionIntegrationTest} and {@code StackSetRegionScopingIntegrationTest} rely on it.</li>
|
||||
* <li>{@code floci.services.ec2instanceconnect.enabled=false} turns EC2 Instance Connect off.
|
||||
* {@code InstanceConnectEnablementIntegrationTest} relies on it.</li>
|
||||
* <li>{@code floci.storage.services.rekognition.mode=persistent} keeps Rekognition collections on
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.floci.aws.testing;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* What CloudFormation answers for a stack set that isn't there: {@code StackSetNotFoundException}, "StackSet NAME not
|
||||
* found", HTTP 404, a {@code Sender} error (measured, eu-west-1 and eu-central-1, 2026-11-17).
|
||||
*/
|
||||
public final class StackSetErrors {
|
||||
|
||||
private StackSetErrors() {
|
||||
}
|
||||
|
||||
/** Asserts that {@code call} throws that answer for {@code stackSet}. */
|
||||
public static void assertNotFound(Executable call, String stackSet) {
|
||||
AwsException missing = assertThrows(AwsException.class, call);
|
||||
assertEquals("StackSetNotFoundException", missing.getErrorCode());
|
||||
assertEquals(message(stackSet), missing.getMessage());
|
||||
assertEquals(404, missing.getHttpStatus());
|
||||
}
|
||||
|
||||
/** Sends {@code request}, a CloudFormation Query request, and asserts it gets that answer for {@code stackSet}. */
|
||||
public static void assertNotFound(RequestSpecification request, String stackSet) {
|
||||
request.post("/").then().statusCode(404)
|
||||
.body("ErrorResponse.Error.Type", equalTo("Sender"))
|
||||
.body("ErrorResponse.Error.Code", equalTo("StackSetNotFoundException"))
|
||||
.body("ErrorResponse.Error.Message", equalTo(message(stackSet)));
|
||||
}
|
||||
|
||||
private static String message(String stackSet) {
|
||||
return "StackSet " + stackSet + " not found";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user