Light, fluffy, and always free - The AWS Local Emulator alternative
feat(cloudcontrol): list resources from local services (#1602)
Jason van Zyl4a198fd29bcc
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex b3187c88b7..b2c63642fc 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -485,6 +485,7 @@ public interface EmulatorConfig {BcmDataExportsServiceConfig bcmDataExports();ConfigServiceConfig configservice();CloudTrailServiceConfig cloudtrail();+ CloudControlServiceConfig cloudcontrol();CloudFrontServiceConfig cloudfront();AppSyncServiceConfig appsync();BatchServiceConfig batch();@@ -525,6 +526,11 @@ public interface EmulatorConfig {boolean enabled();}+ interface CloudControlServiceConfig {+ @WithDefault("true")+ boolean enabled();+ }+interface S3VectorsServiceConfig {@WithDefault("true")boolean enabled();diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.javaindex 8312682e62..4095e2ad5c 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java@@ -16,6 +16,7 @@ import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsTimport io.github.hectorvent.floci.services.bcmdataexports.BcmDataExportsJsonHandler;import io.github.hectorvent.floci.services.ce.CostExplorerJsonHandler;import io.github.hectorvent.floci.services.cloudtrail.CloudTrailJsonHandler;+import io.github.hectorvent.floci.services.cloudcontrol.CloudControlJsonHandler;import io.github.hectorvent.floci.services.configservice.ConfigServiceJsonHandler;import io.github.hectorvent.floci.services.cur.CurJsonHandler;import io.github.hectorvent.floci.services.pricing.PricingJsonHandler;@@ -91,6 +92,7 @@ public class AwsJson11Controller {private final BcmDataExportsJsonHandler bcmDataExportsJsonHandler;private final ConfigServiceJsonHandler configServiceJsonHandler;private final CloudTrailJsonHandler cloudTrailJsonHandler;+ private final CloudControlJsonHandler cloudControlJsonHandler;@Injectpublic AwsJson11Controller(ObjectMapper objectMapper, ResolvedServiceCatalog catalog,@@ -122,7 +124,8 @@ public class AwsJson11Controller {CurJsonHandler curJsonHandler,BcmDataExportsJsonHandler bcmDataExportsJsonHandler,ConfigServiceJsonHandler configServiceJsonHandler,- CloudTrailJsonHandler cloudTrailJsonHandler) {+ CloudTrailJsonHandler cloudTrailJsonHandler,+ CloudControlJsonHandler cloudControlJsonHandler) {this.objectMapper = objectMapper;this.catalog = catalog;this.regionResolver = regionResolver;@@ -158,6 +161,7 @@ public class AwsJson11Controller {this.bcmDataExportsJsonHandler = bcmDataExportsJsonHandler;this.configServiceJsonHandler = configServiceJsonHandler;this.cloudTrailJsonHandler = cloudTrailJsonHandler;+ this.cloudControlJsonHandler = cloudControlJsonHandler;}@POST@@ -219,6 +223,7 @@ public class AwsJson11Controller {case "bcm-data-exports" -> bcmDataExportsJsonHandler.handle(action, request, region);case "config" -> configServiceJsonHandler.handle(action, request, region);case "cloudtrail" -> cloudTrailJsonHandler.handle(action, request, region);+ case "cloudcontrol" -> cloudControlJsonHandler.handle(action, request, region);default -> null;};// catalog.matchTarget is protocol-agnostic: a JSON 1.0 targetdiff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.javaindex 42ad95c3ce..c78e9e8152 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.services.cloudcontrol.CloudControlJsonHandler;import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsJsonHandler;import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;import io.github.hectorvent.floci.services.dynamodb.DynamoDbResponses;@@ -39,6 +40,7 @@ public class AwsJsonController {private final SnsJsonHandler snsJsonHandler;private final StepFunctionsJsonHandler sfnJsonHandler;private final CloudWatchMetricsJsonHandler cloudWatchMetricsJsonHandler;+ private final CloudControlJsonHandler cloudControlJsonHandler;@Injectpublic AwsJsonController(ObjectMapper objectMapper, ResolvedServiceCatalog catalog,@@ -47,7 +49,8 @@ public class AwsJsonController {DynamoDbStreamsJsonHandler dynamoDbStreamsJsonHandler,SqsJsonHandler sqsJsonHandler, SnsJsonHandler snsJsonHandler,StepFunctionsJsonHandler sfnJsonHandler,- CloudWatchMetricsJsonHandler cloudWatchMetricsJsonHandler) {+ CloudWatchMetricsJsonHandler cloudWatchMetricsJsonHandler,+ CloudControlJsonHandler cloudControlJsonHandler) {this.objectMapper = objectMapper;this.catalog = catalog;this.regionResolver = regionResolver;@@ -57,6 +60,7 @@ public class AwsJsonController {this.snsJsonHandler = snsJsonHandler;this.sfnJsonHandler = sfnJsonHandler;this.cloudWatchMetricsJsonHandler = cloudWatchMetricsJsonHandler;+ this.cloudControlJsonHandler = cloudControlJsonHandler;}@POST@@ -96,6 +100,7 @@ public class AwsJsonController {case "sns" -> snsJsonHandler.handle(action, request, region);case "states" -> sfnJsonHandler.handle(action, request, region);case "monitoring" -> cloudWatchMetricsJsonHandler.handle(action, request, region);+ case "cloudcontrol" -> cloudControlJsonHandler.handle(action, request, region);default -> null;};// catalog.matchTarget is protocol-agnostic: a JSON 1.1 targetdiff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.javaindex ee3b3d0252..3c61bf1f71 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java@@ -289,6 +289,10 @@ public class ResolvedServiceCatalog {protocols(ServiceProtocol.JSON),Set.of("com.amazonaws.cloudtrail.v20131101.CloudTrail_20131101."),Set.of("cloudtrail"), Set.of(), Set.of()),+ descriptor("cloudcontrol", "cloudcontrol", config.services().cloudcontrol().enabled(), true,+ null, null, 5000L, null, ServiceProtocol.JSON,+ protocols(ServiceProtocol.JSON),+ Set.of("CloudApiService."), Set.of("cloudcontrolapi"), Set.of(), Set.of()),descriptor("autoscaling", "autoscaling", config.services().autoscaling().enabled(), true,"autoscaling", config.storage().mode(), 5000L, AwsNamespaces.AUTOSCALING, ServiceProtocol.QUERY,protocols(ServiceProtocol.QUERY),diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlJsonHandler.javanew file mode 100644index 0000000000..5628f76012--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlJsonHandler.java@@ -0,0 +1,48 @@+package io.github.hectorvent.floci.services.cloudcontrol;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ArrayNode;+import com.fasterxml.jackson.databind.node.ObjectNode;+import io.github.hectorvent.floci.core.common.AwsException;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;+import jakarta.ws.rs.core.Response;++@ApplicationScoped+public class CloudControlJsonHandler {++ private final CloudControlService service;+ private final ObjectMapper mapper;++ @Inject+ public CloudControlJsonHandler(CloudControlService service, ObjectMapper mapper) {+ this.service = service;+ this.mapper = mapper;+ }++ public Response handle(String action, JsonNode request, String region) {+ return switch (action) {+ case "ListResources" -> listResources(request, region);+ default -> throw new AwsException("UnsupportedOperation",+ "Operation " + action + " is not supported.", 400);+ };+ }++ private Response listResources(JsonNode request, String region) {+ String typeName = request.path("TypeName").asText(null);+ if (typeName == null || typeName.isBlank()) {+ throw new AwsException("ValidationException", "TypeName is required.", 400);+ }+ ObjectNode response = mapper.createObjectNode();+ response.put("TypeName", typeName);+ ArrayNode resources = response.putArray("ResourceDescriptions");+ for (CloudControlService.ResourceDescription resource : service.listResources(region, typeName)) {+ ObjectNode node = mapper.createObjectNode();+ node.put("Identifier", resource.identifier());+ node.put("Properties", resource.properties());+ resources.add(node);+ }+ return Response.ok(response).build();+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlService.java b/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlService.javanew file mode 100644index 0000000000..5f7f1e74d0--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlService.java@@ -0,0 +1,134 @@+package io.github.hectorvent.floci.services.cloudcontrol;++import com.fasterxml.jackson.core.JsonProcessingException;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;+import io.github.hectorvent.floci.services.ec2.model.Subnet;+import io.github.hectorvent.floci.services.ec2.model.Vpc;+import io.github.hectorvent.floci.services.iam.IamService;+import io.github.hectorvent.floci.services.iam.model.IamRole;+import io.github.hectorvent.floci.services.iam.model.IamUser;+import io.github.hectorvent.floci.services.s3.S3Service;+import io.github.hectorvent.floci.services.s3.model.Bucket;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;++import java.util.ArrayList;+import java.util.List;+import java.util.Map;++@ApplicationScoped+public class CloudControlService {++ private final S3Service s3Service;+ private final Ec2Service ec2Service;+ private final IamService iamService;+ private final ObjectMapper mapper;++ @Inject+ public CloudControlService(S3Service s3Service, Ec2Service ec2Service,+ IamService iamService, ObjectMapper mapper) {+ this.s3Service = s3Service;+ this.ec2Service = ec2Service;+ this.iamService = iamService;+ this.mapper = mapper;+ }++ public List<ResourceDescription> listResources(String region, String typeName) {+ return switch (typeName) {+ case "AWS::S3::Bucket" -> s3Buckets();+ case "AWS::EC2::VPC" -> vpcs(region);+ case "AWS::EC2::Subnet" -> subnets(region);+ case "AWS::EC2::SecurityGroup" -> securityGroups(region);+ case "AWS::IAM::Role" -> roles();+ case "AWS::IAM::User" -> users();+ default -> List.of();+ };+ }++ private List<ResourceDescription> s3Buckets() {+ List<ResourceDescription> resources = new ArrayList<>();+ for (Bucket bucket : s3Service.listBuckets()) {+ ObjectNode properties = mapper.createObjectNode();+ properties.put("BucketName", bucket.getName());+ resources.add(new ResourceDescription(bucket.getName(), propertiesString(properties)));+ }+ return resources;+ }++ private List<ResourceDescription> vpcs(String region) {+ List<ResourceDescription> resources = new ArrayList<>();+ for (Vpc vpc : ec2Service.describeVpcs(region, List.of(), Map.of())) {+ ObjectNode properties = mapper.createObjectNode();+ properties.put("VpcId", vpc.getVpcId());+ properties.put("CidrBlock", vpc.getCidrBlock());+ properties.put("InstanceTenancy", vpc.getInstanceTenancy());+ resources.add(new ResourceDescription(vpc.getVpcId(), propertiesString(properties)));+ }+ return resources;+ }++ private List<ResourceDescription> subnets(String region) {+ List<ResourceDescription> resources = new ArrayList<>();+ for (Subnet subnet : ec2Service.describeSubnets(region, List.of(), Map.of())) {+ ObjectNode properties = mapper.createObjectNode();+ properties.put("SubnetId", subnet.getSubnetId());+ properties.put("VpcId", subnet.getVpcId());+ properties.put("CidrBlock", subnet.getCidrBlock());+ properties.put("AvailabilityZone", subnet.getAvailabilityZone());+ resources.add(new ResourceDescription(subnet.getSubnetId(), propertiesString(properties)));+ }+ return resources;+ }++ private List<ResourceDescription> securityGroups(String region) {+ List<ResourceDescription> resources = new ArrayList<>();+ for (SecurityGroup group : ec2Service.describeSecurityGroups(region, List.of(), List.of(), Map.of())) {+ ObjectNode properties = mapper.createObjectNode();+ properties.put("GroupId", group.getGroupId());+ properties.put("GroupName", group.getGroupName());+ properties.put("GroupDescription", group.getDescription());+ properties.put("VpcId", group.getVpcId());+ resources.add(new ResourceDescription(group.getGroupId(), propertiesString(properties)));+ }+ return resources;+ }++ private List<ResourceDescription> roles() {+ List<ResourceDescription> resources = new ArrayList<>();+ for (IamRole role : iamService.listRoles("/")) {+ ObjectNode properties = mapper.createObjectNode();+ properties.put("Arn", role.getArn());+ properties.put("RoleName", role.getRoleName());+ properties.put("Path", role.getPath());+ resources.add(new ResourceDescription(role.getRoleName(), propertiesString(properties)));+ }+ return resources;+ }++ private List<ResourceDescription> users() {+ List<ResourceDescription> resources = new ArrayList<>();+ for (IamUser user : iamService.listUsers("/")) {+ ObjectNode properties = mapper.createObjectNode();+ properties.put("Arn", user.getArn());+ properties.put("UserName", user.getUserName());+ properties.put("Path", user.getPath());+ resources.add(new ResourceDescription(user.getUserName(), propertiesString(properties)));+ }+ return resources;+ }++ private String propertiesString(ObjectNode properties) {+ try {+ return mapper.writeValueAsString(properties);+ } catch (JsonProcessingException e) {+ throw new AwsException("InternalFailure",+ "Failed to serialize CloudControl resource properties.", 500);+ }+ }++ public record ResourceDescription(String identifier, String properties) {}+}diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex 32e47239d7..b48bea4de6 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -394,6 +394,8 @@ floci:emit-mode: synchronouscloudtrail:enabled: true+ cloudcontrol:+ enabled: truecloudfront:enabled: trueappsync:diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlIntegrationTest.javanew file mode 100644index 0000000000..f6261ed638--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/cloudcontrol/CloudControlIntegrationTest.java@@ -0,0 +1,110 @@+package io.github.hectorvent.floci.services.cloudcontrol;++import io.quarkus.test.junit.QuarkusTest;+import io.restassured.RestAssured;+import io.restassured.parsing.Parser;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static io.restassured.http.ContentType.TEXT;+import static io.restassured.config.EncoderConfig.encoderConfig;+import static io.restassured.config.RestAssuredConfig.config;+import static org.hamcrest.MatcherAssert.assertThat;+import static org.hamcrest.Matchers.containsString;++@QuarkusTest+class CloudControlIntegrationTest {++ private static final String EC2_AUTH =+ "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 TRUST_POLICY =+ "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","+ + "\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}";++ @BeforeAll+ static void registerAwsJsonParser() {+ RestAssured.registerParser("application/x-amz-json-1.1", Parser.JSON);+ RestAssured.registerParser("application/x-amz-json-1.0", Parser.JSON);+ }++ @Test+ void listResourcesReturnsCreatedS3Ec2AndIamResources() {+ String bucket = "cloudcontrol-test-bucket";+ given().when().put("/" + bucket).then().statusCode(200);++ String vpcId = given()+ .formParam("Action", "CreateVpc")+ .formParam("CidrBlock", "10.42.0.0/16")+ .header("Authorization", EC2_AUTH)+ .when().post("/")+ .then().statusCode(200)+ .extract().path("CreateVpcResponse.vpc.vpcId");++ String subnetId = given()+ .formParam("Action", "CreateSubnet")+ .formParam("VpcId", vpcId)+ .formParam("CidrBlock", "10.42.1.0/24")+ .header("Authorization", EC2_AUTH)+ .when().post("/")+ .then().statusCode(200)+ .extract().path("CreateSubnetResponse.subnet.subnetId");++ String groupId = given()+ .formParam("Action", "CreateSecurityGroup")+ .formParam("GroupName", "cloudcontrol-sg")+ .formParam("GroupDescription", "cloudcontrol sg")+ .formParam("VpcId", vpcId)+ .header("Authorization", EC2_AUTH)+ .when().post("/")+ .then().statusCode(200)+ .extract().path("CreateSecurityGroupResponse.groupId");++ given()+ .formParam("Action", "CreateUser")+ .formParam("UserName", "cloudcontrol-user")+ .header("Authorization", IAM_AUTH)+ .when().post("/")+ .then().statusCode(200);++ given()+ .formParam("Action", "CreateRole")+ .formParam("RoleName", "CloudControlRole")+ .formParam("AssumeRolePolicyDocument", TRUST_POLICY)+ .header("Authorization", IAM_AUTH)+ .when().post("/")+ .then().statusCode(200);++ assertListed("AWS::S3::Bucket", bucket, "BucketName");+ assertListed("AWS::EC2::VPC", vpcId, "CidrBlock");+ assertListed("AWS::EC2::Subnet", subnetId, "VpcId");+ assertListed("AWS::EC2::SecurityGroup", groupId, "GroupName");+ assertListed("AWS::EC2::SecurityGroup", groupId, "GroupName", "application/x-amz-json-1.0");+ assertListed("AWS::IAM::User", "cloudcontrol-user", "UserName");+ assertListed("AWS::IAM::Role", "CloudControlRole", "RoleName");+ }++ private void assertListed(String typeName, String identifier, String propertyName) {+ assertListed(typeName, identifier, propertyName, "application/x-amz-json-1.1");+ }++ private void assertListed(String typeName, String identifier, String propertyName, String contentType) {+ String body = given()+ .config(config().encoderConfig(+ encoderConfig().encodeContentTypeAs(contentType, TEXT)))+ .contentType(contentType)+ .header("X-Amz-Target", "CloudApiService.ListResources")+ .body("{\"TypeName\":\"" + typeName + "\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().asString();++ assertThat(body, containsString("\"TypeName\":\"" + typeName + "\""));+ assertThat(body, containsString("\"Identifier\":\"" + identifier + "\""));+ assertThat(body, containsString(propertyName));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.javaindex 3f190ac5db..0dadeeb649 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java@@ -465,7 +465,7 @@ class IamIntegrationTest {.post("/").then().statusCode(200)- .body("ListUsersResponse.ListUsersResult.Users.member.UserName",+ .body("ListUsersResponse.ListUsersResult.Users.member.find { it.UserName == 'test-user' }.UserName",equalTo("test-user"));}@@ -548,7 +548,7 @@ class IamIntegrationTest {.post("/").then().statusCode(200)- .body("ListRolesResponse.ListRolesResult.Roles.member.RoleName",+ .body("ListRolesResponse.ListRolesResult.Roles.member.find { it.RoleName == 'TestRole' }.RoleName",equalTo("TestRole"));}diff --git a/src/test/resources/application.yml b/src/test/resources/application.ymlindex 3601e217af..1888f6f054 100644--- a/src/test/resources/application.yml+++ b/src/test/resources/application.yml@@ -253,6 +253,8 @@ floci:emit-mode: offcloudtrail:enabled: true+ cloudcontrol:+ enabled: truecloudfront:enabled: trueappsync: