fix(cloudcontrol): scope resources by caller account (#3244)

* fix(cloudcontrol): scope resources by caller account

* fix(cloudcontrol): register persisted models for reflection

* fix(cloudcontrol): make persisted recovery idempotent

* fix(cloudcontrol): preserve request order across restart
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-08 15:29:49 -04:00
1 parent 56ab219132
commit b6080ea4ff
7 files changed
+408 -59

No files matched your search

@@ -27,6 +27,10 @@ class CloudControlIntegrationTest {
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
private static final String IAM_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request";
private static final String ACCOUNT_A_AUTH =
"AWS4-HMAC-SHA256 Credential=111111111111/20260227/us-east-1/cloudcontrol/aws4_request";
private static final String ACCOUNT_B_AUTH =
"AWS4-HMAC-SHA256 Credential=222222222222/20260227/us-east-1/cloudcontrol/aws4_request";
private static final String TRUST_POLICY =
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}";
@@ -141,6 +145,32 @@ class CloudControlIntegrationTest {
assertListed("AWS::EC2::VPC", identifier, "VpcId", ct);
}
@Test
void cloudControlResourcesAndTokensAreIsolatedByAccount() throws InterruptedException {
String ct = "application/x-amz-json-1.0";
String tokenA = createVpcThroughCloudControl(ct, ACCOUNT_A_AUTH, "10.81.0.0/16");
String tokenB = createVpcThroughCloudControl(ct, ACCOUNT_B_AUTH, "10.82.0.0/16");
String vpcA = awaitIdentifier(tokenA, ct, ACCOUNT_A_AUTH);
String vpcB = awaitIdentifier(tokenB, ct, ACCOUNT_B_AUTH);
assertListedWithAuth("AWS::EC2::VPC", vpcA, ACCOUNT_A_AUTH, ct);
assertListedWithAuth("AWS::EC2::VPC", vpcB, ACCOUNT_B_AUTH, ct);
assertNotFoundWithAuth("AWS::EC2::VPC", vpcA, ACCOUNT_B_AUTH, ct);
assertNotFoundWithAuth("AWS::EC2::VPC", vpcB, ACCOUNT_A_AUTH, ct);
given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(ct, TEXT)))
.contentType(ct).header("Authorization", ACCOUNT_B_AUTH)
.header("X-Amz-Target", "CloudApiService.GetResourceRequestStatus")
.body("{\"RequestToken\":\"" + tokenA + "\"}")
.when().post("/").then().statusCode(404)
.body("__type", containsString("RequestTokenNotFoundException"));
deleteThroughCloudControl(ct, ACCOUNT_B_AUTH, vpcA, "FAILED");
assertListedWithAuth("AWS::EC2::VPC", vpcA, ACCOUNT_A_AUTH, ct);
deleteThroughCloudControl(ct, ACCOUNT_A_AUTH, vpcA, "SUCCESS");
deleteThroughCloudControl(ct, ACCOUNT_B_AUTH, vpcB, "SUCCESS");
}
@Test
void malformedRequestsReportInvalidRequestException() {
// InvalidRequestException is the code Cloud Control declares, so an SDK can map it onto a
@@ -257,16 +287,35 @@ class CloudControlIntegrationTest {
.body("ProgressEvent.OperationStatus", org.hamcrest.Matchers.equalTo("FAILED"));
}
private String createVpcThroughCloudControl(String ct, String auth, String cidr) {
return given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(ct, TEXT)))
.contentType(ct).header("Authorization", auth)
.header("X-Amz-Target", "CloudApiService.CreateResource")
.body("{\"TypeName\":\"AWS::EC2::VPC\",\"DesiredState\":\"{\\\"CidrBlock\\\":\\\"" + cidr + "\\\"}\"}")
.when().post("/").then().statusCode(200)
.extract().path("ProgressEvent.RequestToken");
}
private void deleteThroughCloudControl(String ct, String auth, String identifier, String status) {
given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(ct, TEXT)))
.contentType(ct).header("Authorization", auth)
.header("X-Amz-Target", "CloudApiService.DeleteResource")
.body("{\"TypeName\":\"AWS::EC2::VPC\",\"Identifier\":\"" + identifier + "\"}")
.when().post("/").then().statusCode(200)
.body("ProgressEvent.OperationStatus", org.hamcrest.Matchers.equalTo(status));
}
private String awaitIdentifier(String token, String ct) throws InterruptedException {
return awaitIdentifier(token, ct, null);
}
private String awaitIdentifier(String token, String ct, String auth) throws InterruptedException {
for (int i = 0; i < 20; i++) {
var pe = given()
.config(config().encoderConfig(encoderConfig().encodeContentTypeAs(ct, TEXT)))
.contentType(ct)
.header("X-Amz-Target", "CloudApiService.GetResourceRequestStatus")
.body("{\"RequestToken\":\"" + token + "\"}")
.when().post("/")
.then().statusCode(200)
.extract();
var request = given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(ct, TEXT)))
.contentType(ct).header("X-Amz-Target", "CloudApiService.GetResourceRequestStatus");
if (auth != null) request.header("Authorization", auth);
var pe = request.body("{\"RequestToken\":\"" + token + "\"}")
.when().post("/").then().statusCode(200).extract();
if ("SUCCESS".equals(pe.path("ProgressEvent.OperationStatus"))) {
return pe.path("ProgressEvent.Identifier");
}
@@ -365,6 +414,24 @@ class CloudControlIntegrationTest {
&& tag.path("Value").isTextual()));
}
private void assertListedWithAuth(String typeName, String identifier, String auth, String contentType) {
String body = given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(contentType, TEXT)))
.contentType(contentType).header("Authorization", auth)
.header("X-Amz-Target", "CloudApiService.ListResources")
.body("{\"TypeName\":\"" + typeName + "\"}")
.when().post("/").then().statusCode(200).extract().asString();
assertThat(body, containsString("\"Identifier\":\"" + identifier + "\""));
}
private void assertNotFoundWithAuth(String typeName, String identifier, String auth, String contentType) {
given().config(config().encoderConfig(encoderConfig().encodeContentTypeAs(contentType, TEXT)))
.contentType(contentType).header("Authorization", auth)
.header("X-Amz-Target", "CloudApiService.GetResource")
.body("{\"TypeName\":\"" + typeName + "\",\"Identifier\":\"" + identifier + "\"}")
.when().post("/").then().statusCode(404)
.body("__type", containsString("ResourceNotFoundException"));
}
private String listResources(String typeName, String contentType) {
return given()
.config(config().encoderConfig(
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.CloudFormationResourceProvisioner;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
@@ -20,9 +22,86 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;
class CloudControlServiceTest {
@Test
void accountScopesCreateStatusLookupAndDelete() throws Exception {
CloudFormationResourceProvisioner provisioner = mock(CloudFormationResourceProvisioner.class);
StackResource resource = new StackResource();
resource.setPhysicalId("vpc-account-a");
resource.setAttributes(Map.of("VpcId", "vpc-account-a"));
when(provisioner.provisionStandalone(org.mockito.ArgumentMatchers.eq("AWS::EC2::VPC"),
org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.eq("us-east-1"),
org.mockito.ArgumentMatchers.eq("111111111111"))).thenReturn(resource);
CloudControlService service = new CloudControlService(
mock(S3Service.class), mock(Ec2Service.class), mock(IamService.class), provisioner,
new ObjectMapper());
CloudControlService.ProgressEvent pending = service.createResource(
"us-east-1", "111111111111", "AWS::EC2::VPC", "{\"CidrBlock\":\"10.0.0.0/16\"}");
CloudControlService.ProgressEvent completed = pending;
for (int i = 0; i < 20 && !"SUCCESS".equals(completed.operationStatus()); i++) {
Thread.sleep(10);
completed = service.requestStatus("111111111111", pending.requestToken());
}
assertEquals("SUCCESS", completed.operationStatus());
assertEquals("vpc-account-a", service.getResource("us-east-1", "111111111111",
"AWS::EC2::VPC", "vpc-account-a").identifier());
assertThrows(AwsException.class, () -> service.requestStatus("222222222222", pending.requestToken()));
assertThrows(AwsException.class, () -> service.getResource("us-east-1", "222222222222",
"AWS::EC2::VPC", "vpc-account-a"));
CloudControlService.ProgressEvent deniedDelete = service.deleteResource(
"us-east-1", "222222222222", "AWS::EC2::VPC", "vpc-account-a");
assertEquals("FAILED", deniedDelete.operationStatus());
verify(provisioner, never()).deleteStandalone(org.mockito.ArgumentMatchers.anyString(),
org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.anyString(),
org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.anyMap());
CloudControlService.ProgressEvent deleted = service.deleteResource(
"us-east-1", "111111111111", "AWS::EC2::VPC", "vpc-account-a");
assertEquals("SUCCESS", deleted.operationStatus());
verify(provisioner).deleteStandalone("AWS::EC2::VPC", "vpc-account-a", "us-east-1",
"111111111111", Map.of("VpcId", "vpc-account-a"));
}
@Test
void restoresAccountOwnersAndRequestStateFromMetadataStores() throws Exception {
CloudFormationResourceProvisioner provisioner = mock(CloudFormationResourceProvisioner.class);
StackResource resource = new StackResource();
resource.setPhysicalId("igw-persisted");
when(provisioner.provisionStandalone(org.mockito.ArgumentMatchers.eq("AWS::EC2::InternetGateway"),
org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.eq("us-east-1"),
org.mockito.ArgumentMatchers.eq("111111111111"))).thenReturn(resource);
AccountAwareStorageBackend<CloudControlService.PersistedRequest> requests =
AccountAwareStorageBackend.inMemory("000000000000");
AccountAwareStorageBackend<CloudControlService.PersistedCreatedResource> created =
AccountAwareStorageBackend.inMemory("000000000000");
CloudControlService first = new CloudControlService(
mock(S3Service.class), mock(Ec2Service.class), mock(IamService.class), provisioner,
new ObjectMapper(), requests, created);
CloudControlService.ProgressEvent pending = first.createResource("us-east-1", "111111111111",
"AWS::EC2::InternetGateway", "{}");
CloudControlService.ProgressEvent completed = first.requestStatus("111111111111", pending.requestToken());
for (int i = 0; i < 20 && !"SUCCESS".equals(completed.operationStatus()); i++) {
Thread.sleep(10);
completed = first.requestStatus("111111111111", pending.requestToken());
}
CloudControlService restarted = new CloudControlService(
mock(S3Service.class), mock(Ec2Service.class), mock(IamService.class), provisioner,
new ObjectMapper(), requests, created);
assertEquals("SUCCESS", restarted.requestStatus("111111111111", pending.requestToken()).operationStatus());
assertEquals("igw-persisted", restarted.getResource("us-east-1", "111111111111",
"AWS::EC2::InternetGateway", "igw-persisted").identifier());
assertThrows(AwsException.class, () -> restarted.requestStatus("222222222222", pending.requestToken()));
assertThrows(AwsException.class, () -> restarted.getResource("us-east-1", "222222222222",
"AWS::EC2::InternetGateway", "igw-persisted"));
}
@Test
void emitsOnlyAwsShapedTagsForMalformedPersistedData() throws Exception {
Ec2Service ec2Service = mock(Ec2Service.class);