Light, fluffy, and always free - The AWS Local Emulator alternative
feat(resourcegroups): support EC2 capacity reservation pools (#6011)
Andre Kuraitbe2cb677d008
diff --git a/Makefile b/Makefileindex a47fd46b82..30008e70ff 100644--- a/Makefile+++ b/Makefile@@ -215,6 +215,7 @@ aws-data-sync: ## Regenerate src/main/resources/aws/*.json and the IAM managed-p$(PYTHON) tools/aws/regen_region_facts.py$(PYTHON) tools/aws/regen_dynamodb_shapes.py$(PYTHON) tools/aws/regen_redshift_serverless_shapes.py+ $(PYTHON) tools/aws/regen_resource_groups_shapes.py$(PYTHON) tools/aws/regen_apigateway_types.py$(PYTHON) tools/aws/regen_sesv2_shapes.py$(PYTHON) tools/aws/regen_ecs_shapes.py@@ -247,6 +248,10 @@ aws-data-check: ## CI gate: the vendored AWS data must match a fresh generationecho " Run 'make aws-data-sync' and commit the result."; \exit 1; \}+ @$(PYTHON) tools/aws/regen_resource_groups_shapes.py --check || { \+ echo "error: Resource Groups request shapes are out of date; run 'make aws-data-sync'."; \+ exit 1; \+ }@$(PYTHON) tools/aws/regen_apigateway_types.py --check || { \echo "error: API Gateway integration types are out of date; run 'make aws-data-sync'."; \exit 1; \diff --git a/README.md b/README.mdindex 1c3542bb32..afcfa3f111 100644--- a/README.md+++ b/README.md@@ -383,6 +383,7 @@ For operation-level compatibility, see the [Services Overview](https://floci.io/| CloudTrail | In-process | Trails, event selectors (S3 data events with bucket/prefix matching), `StartLogging`/`StopLogging`, scheduled gzipped log file emission to the destination bucket at AWS-shaped key paths, IAM-deny path emits `AccessDenied` records || CloudFront | In-process | Distributions, origins, cache behaviors, invalidations, tagging || WAF v2 | In-process | Web ACLs, IP sets, regex pattern sets, rule groups, logging configs, resource associations, tagging (REGIONAL and CLOUDFRONT scopes) |+| AWS Resource Groups | In-process | Five capacity-pool operations: stored groups, verified EC2 reservation membership, scoped pagination and group deletion; no dynamic queries or capacity allocation || Resource Explorer 2 | In-process | All 32 management APIs: index and view lifecycle, multi-Region setup tasks, the full search query grammar, and tagging; 30 services expose their resources for search, gathered live rather than from an index || Route53 | In-process | Hosted zones, SOA and NS records, resource record sets, change tracking, tagging || Cloud Map | In-process | HTTP and DNS namespaces, services, instance registration, discovery queries, operations, tagging |diff --git a/compatibility-tests/sdk-test-java/pom.xml b/compatibility-tests/sdk-test-java/pom.xmlindex c2b7d5f14e..3985975b84 100644--- a/compatibility-tests/sdk-test-java/pom.xml+++ b/compatibility-tests/sdk-test-java/pom.xml@@ -196,6 +196,10 @@<groupId>software.amazon.awssdk</groupId><artifactId>glue</artifactId></dependency>+ <dependency>+ <groupId>software.amazon.awssdk</groupId>+ <artifactId>resourcegroups</artifactId>+ </dependency><dependency><groupId>software.amazon.awssdk</groupId><artifactId>resourcegroupstaggingapi</artifactId>diff --git a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.javaindex d105073b2c..255b2f30f2 100644--- a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java+++ b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java@@ -110,6 +110,7 @@ import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient;import software.amazon.awssdk.services.redshiftserverless.RedshiftServerlessClient;import software.amazon.awssdk.services.resourceexplorer2.ResourceExplorer2Client;import software.amazon.awssdk.services.resourceexplorer2.ResourceExplorer2ClientBuilder;+import software.amazon.awssdk.services.resourcegroups.ResourceGroupsClient;import software.amazon.awssdk.services.resourcegroupstaggingapi.ResourceGroupsTaggingApiClient;import software.amazon.awssdk.services.route53.Route53Client;import software.amazon.awssdk.services.route53.model.Change;@@ -963,6 +964,14 @@ public final class TestFixtures {.build();}+ public static ResourceGroupsClient resourceGroupsClient() {+ return ResourceGroupsClient.builder()+ .endpointOverride(ENDPOINT)+ .region(REGION)+ .credentialsProvider(CREDENTIALS)+ .build();+ }+public static ResourceGroupsTaggingApiClient resourceGroupsTaggingApiClient() {return ResourceGroupsTaggingApiClient.builder().endpointOverride(ENDPOINT)diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ResourceGroupsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ResourceGroupsTest.javanew file mode 100644index 0000000000..bc98277c3a--- /dev/null+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ResourceGroupsTest.java@@ -0,0 +1,66 @@+package com.floci.test;++import org.junit.jupiter.api.DisplayName;+import org.junit.jupiter.api.Test;+import software.amazon.awssdk.services.ec2.Ec2Client;+import software.amazon.awssdk.services.ec2.model.CapacityReservation;+import software.amazon.awssdk.services.resourcegroups.ResourceGroupsClient;+import software.amazon.awssdk.services.resourcegroups.model.GroupConfigurationItem;+import software.amazon.awssdk.services.resourcegroups.model.GroupConfigurationParameter;+import software.amazon.awssdk.services.resourcegroups.model.GroupResourcesResponse;+import software.amazon.awssdk.services.resourcegroups.model.ListGroupResourcesResponse;+import software.amazon.awssdk.services.resourcegroups.model.NotFoundException;++import java.util.List;+import java.util.Map;++import static org.assertj.core.api.Assertions.assertThat;+import static org.assertj.core.api.Assertions.assertThatThrownBy;++@DisplayName("Resource Groups capacity reservation pools through the AWS SDK")+class ResourceGroupsTest {+ @Test+ @DisplayName("Store verified membership and delete only the group")+ void capacityPoolLifecycle() {+ try (ResourceGroupsClient groups = TestFixtures.resourceGroupsClient();+ Ec2Client ec2 = TestFixtures.ec2Client()) {+ String name = TestFixtures.uniqueName("capacity-pool");+ CapacityReservation reservation = ec2.createCapacityReservation(request -> request+ .instanceType("t3.micro").instancePlatform("Linux/UNIX")+ .availabilityZone(TestFixtures.region().id() + "a").instanceCount(1))+ .capacityReservation();+ try {+ String arn = groups.createGroup(request -> request.name(name).tags(Map.of("purpose", "compat"))+ .configuration(List.of(+ GroupConfigurationItem.builder().type("AWS::EC2::CapacityReservationPool").build(),+ GroupConfigurationItem.builder().type("AWS::ResourceGroups::Generic").parameters(+ GroupConfigurationParameter.builder().name("allowed-resource-types")+ .values("AWS::EC2::CapacityReservation").build()).build())))+ .group().groupArn();+ try {+ assertThat(groups.getGroup(request -> request.groupName(name)).group().groupArn()).isEqualTo(arn);+ GroupResourcesResponse added = groups.groupResources(request -> request+ .group(arn).resourceArns(reservation.capacityReservationArn()));+ assertThat(added.succeeded()).containsExactly(reservation.capacityReservationArn());+ assertThat(added.failed()).isEmpty();+ assertThat(added.pending()).isEmpty();+ ListGroupResourcesResponse listed = groups.listGroupResources(request -> request.group(arn));+ assertThat(listed.resourceIdentifiers()).singleElement().satisfies(identifier -> {+ assertThat(identifier.resourceArn()).isEqualTo(reservation.capacityReservationArn());+ assertThat(identifier.resourceType()).isEqualTo("AWS::EC2::CapacityReservation");+ });+ assertThat(listed.resources()).singleElement().satisfies(resource ->+ assertThat(resource.identifier().resourceArn()).isEqualTo(reservation.capacityReservationArn()));+ } finally {+ groups.deleteGroup(request -> request.groupName(name));+ }+ assertThatThrownBy(() -> groups.getGroup(request -> request.groupName(name)))+ .isInstanceOf(NotFoundException.class);+ assertThat(ec2.describeCapacityReservations(request -> request+ .capacityReservationIds(reservation.capacityReservationId())).capacityReservations()).hasSize(1);+ } finally {+ ec2.cancelCapacityReservation(request -> request.capacityReservationId(reservation.capacityReservationId()));+ }+ }+ }+}diff --git a/docs/services/index.md b/docs/services/index.mdindex f43a5576b6..e341711361 100644--- a/docs/services/index.md+++ b/docs/services/index.md@@ -87,6 +87,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac| [Lightsail](lightsail.md) | `POST /` + `X-Amz-Target: Lightsail_20161128.*` | JSON 1.1 | 79 local responses; 161 recognized actions || [ACM](acm.md) | `POST /` + `X-Amz-Target: CertificateManager.*` | JSON 1.1 | 16 || [ECR](ecr.md) | `POST /` + `X-Amz-Target: AmazonEC2ContainerRegistry_V20150921.*` (control plane) and `/v2/...` (data plane proxied to `registry:2`) | JSON 1.1 + OCI Distribution | 17 |+| [AWS Resource Groups](resource-groups.md) | `/groups`, `/get-group`, `/delete-group`, `/group-resources`, `/list-group-resources` | REST JSON | 5 || [Resource Groups Tagging API](resource-groups-tagging.md) | `POST /` + `X-Amz-Target: ResourceGroupsTaggingAPI_20170126.*` | JSON 1.1 | 5 || [Resource Explorer](resource-explorer.md) | `POST /{OperationName}`, rewritten to `/_re2/*` for the four paths S3 Vectors also claims | REST JSON | 32 || [SES](ses.md) | `POST /` with `Action=` param | Query | 16 |diff --git a/docs/services/resource-groups.md b/docs/services/resource-groups.mdnew file mode 100644index 0000000000..4cdf48d550--- /dev/null+++ b/docs/services/resource-groups.md@@ -0,0 +1,68 @@+# AWS Resource Groups++**Protocol:** REST JSON, signed with `resource-groups`++**Endpoint:** `http://localhost:4566`++Floci supports service-configured EC2 capacity reservation pools. This is a separate service from the Resource Groups Tagging API.++## Supported Actions++<!-- floci:actions:start -->+| Action | Description |+| --- | --- |+| `CreateGroup` | Create a capacity reservation pool with stored configuration and tags |+| `GetGroup` | Read a group by name or ARN |+| `DeleteGroup` | Delete the group and membership, preserving its reservations |+| `GroupResources` | Add verified active reservations |+| `ListGroupResources` | Read stored membership with scoped pagination |+<!-- floci:actions:end -->++The standard REST routes are `POST /groups`, `/get-group`, `/delete-group`, `/group-resources`, and `/list-group-resources`. Get, delete and list also accept the deprecated `GroupName` selector. Supplying both `Group` and `GroupName` is rejected.++With IAM enforcement enabled, signed management operations are checked against their `resource-groups:` actions and the selected group ARN. Signed S3 requests to buckets with the same names retain S3 routing.++## Capacity pools++Create a group with this public AWS service configuration:++```json+[+ {"Type": "AWS::EC2::CapacityReservationPool"},+ {+ "Type": "AWS::ResourceGroups::Generic",+ "Parameters": [+ {"Name": "allowed-resource-types", "Values": ["AWS::EC2::CapacityReservation"]}+ ]+ }+]+```++Groups use the caller's account and region. Their ARNs use the region's AWS partition. Names, descriptions, configuration, creation tags and membership are stored through the normal Floci storage backend.++`GroupResources` accepts one to ten resource ARNs. Each successful member must be an actual active EC2 capacity reservation in the same account, region and partition. RAM-shared reservations are not accepted; reservations must be owned by the group account. Missing, foreign and inactive reservations appear in `Failed`; they are never inserted. Repeated adds preserve one membership. Unexpected EC2 lookup errors remain errors and do not partially change membership.++`ListGroupResources` returns both `Resources.Identifier` and the deprecated `ResourceIdentifiers` representation. It supports the capacity-reservation `resource-type` filter and pages of one to fifty entries. Tokens bind the group generation and filter to an ordered membership snapshot, so later additions do not change an existing traversal. Tokens expire after fifteen minutes without a successful scoped read; the instance retains at most 256 tokens and evicts the least recently used token at capacity. Tokens remain retryable until expiry, eviction, group deletion or emulator restart/reset. Invalid tokens return `BadRequestException`.++Deleting a group removes its membership and tokens. It does not cancel or delete the EC2 reservations.++## Scope++Only the five operations and capacity-pool configuration above are implemented. Dynamic tag/CloudFormation queries, other service configurations, independent tag-update operations and `UngroupResources` are not supported. Unsupported configurations are rejected. Membership does not allocate physical capacity or make instance scheduling consume a reservation. No asynchronous membership transition is simulated: accepted additions complete synchronously and `Pending` is empty.++Group state follows the configured Floci storage mode. Pagination tokens are in memory and are intentionally invalidated by restart/reset.++## Configuration++| Variable | Default | Description |+| --- | --- | --- |+| `FLOCI_SERVICES_RESOURCE_GROUPS_ENABLED` | `true` | Enable the Resource Groups service |+| `FLOCI_STORAGE_SERVICES_RESOURCE_GROUPS_MODE` | global storage mode | Storage mode for groups and membership |+| `FLOCI_STORAGE_SERVICES_RESOURCE_GROUPS_FLUSH_INTERVAL_MS` | `5000` | Storage flush interval |++The public wire contracts and capacity-pool example are documented in the AWS references for+[CreateGroup](https://docs.aws.amazon.com/ARG/latest/APIReference/API_CreateGroup.html),+[GroupResources](https://docs.aws.amazon.com/ARG/latest/APIReference/API_GroupResources.html), and+[ListGroupResources](https://docs.aws.amazon.com/ARG/latest/APIReference/API_ListGroupResources.html).++Requests use botocore's modeled field, tag-pattern and size constraints; unknown fields are ignored. In the supported service-configured capacity-pool mode, valid `Criticality`, `Owner` and `DisplayName` values are rejected as unsupported parameters, matching live AWS.diff --git a/mkdocs.yml b/mkdocs.ymlindex 27eebbe6a4..753919e89d 100644--- a/mkdocs.yml+++ b/mkdocs.yml@@ -170,6 +170,7 @@ nav:- ACM: services/acm.md- ECS: services/ecs.md- ECR: services/ecr.md+ - AWS Resource Groups: services/resource-groups.md- Resource Groups Tagging API: services/resource-groups-tagging.md- Resource Explorer 2: services/resource-explorer.md- EKS: services/eks.mddiff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex e107c35a9c..0c8d5932dd 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -391,6 +391,7 @@ public interface EmulatorConfig {CodeDeployStorageConfig codedeploy();TranscribeStorageConfig transcribe();TaggingStorageConfig tagging();+ ResourceGroupsStorageConfig resourceGroups();ElasticBeanstalkStorageConfig elasticbeanstalk();CloudTrailStorageConfig cloudtrail();RumStorageConfig rum();@@ -624,6 +625,13 @@ public interface EmulatorConfig {long flushIntervalMs();}+ interface ResourceGroupsStorageConfig {+ Optional<String> mode();++ @WithDefault("5000")+ long flushIntervalMs();+ }+interface TaggingStorageConfig {Optional<String> mode();@@ -764,6 +772,7 @@ public interface EmulatorConfig {AppConfigDataServiceConfig appconfigdata();EcrServiceConfig ecr();ResourceGroupsTaggingServiceConfig tagging();+ ResourceGroupsServiceConfig resourceGroups();BedrockServiceConfig bedrock();BedrockRuntimeServiceConfig bedrockRuntime();EksServiceConfig eks();@@ -2330,6 +2339,11 @@ public interface EmulatorConfig {String proxyImage();}+ interface ResourceGroupsServiceConfig {+ @WithDefault("true")+ boolean enabled();+ }+interface ResourceGroupsTaggingServiceConfig {@WithDefault("true")boolean enabled();diff --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 eb760dfd79..cb5c635984 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@@ -58,6 +58,7 @@ import io.github.hectorvent.floci.services.opensearch.OpenSearchController;import io.github.hectorvent.floci.services.pipes.PipesController;import io.github.hectorvent.floci.services.rdsdata.RdsDataController;import io.github.hectorvent.floci.services.resourceexplorer2.ResourceExplorer2Controller;+import io.github.hectorvent.floci.services.resourcegroups.ResourceGroupsController;import io.github.hectorvent.floci.services.route53.Route53Controller;import io.github.hectorvent.floci.services.rum.RumController;import io.github.hectorvent.floci.services.s3.S3Controller;@@ -365,6 +366,11 @@ public class ResolvedServiceCatalog {null, null, 5000L, null, ServiceProtocol.JSON,protocols(ServiceProtocol.JSON),Set.of("AmazonEC2ContainerRegistry_V20150921."), Set.of("ecr"), Set.of(), Set.of()),+ descriptor("resource-groups", "resource-groups", config.services().resourceGroups().enabled(), true,+ "resource-groups", storageMode(config.storage().services().resourceGroups().mode(), config.storage().mode()),+ config.storage().services().resourceGroups().flushIntervalMs(), null, ServiceProtocol.REST_JSON,+ protocols(ServiceProtocol.REST_JSON), Set.of(), Set.of("resource-groups"), Set.of(),+ Set.of(ResourceGroupsController.class)),descriptor("tagging", "tagging", config.services().tagging().enabled(), true,"tagging", storageMode(config.storage().services().tagging().mode(), config.storage().mode()),config.storage().services().tagging().flushIntervalMs(), null, ServiceProtocol.JSON,diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.javaindex d718f04b87..c539e95d7c 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java@@ -269,6 +269,13 @@ public class IamActionRegistry {rule("ses", "POST", "^/v2/email/update-configuration-sets/?$", "ses:UpdateConfigurationSet"),rule("ses", "POST", "^/v2/email/vdm/recommendations/?$", "ses:ListRecommendations"),+ // ── Resource Groups ─────────────────────────────────────────────────────+ rule("resource-groups", "POST", "^/_rg/groups/?$", "resource-groups:CreateGroup"),+ rule("resource-groups", "POST", "^/_rg/get-group/?$", "resource-groups:GetGroup"),+ rule("resource-groups", "POST", "^/_rg/delete-group/?$", "resource-groups:DeleteGroup"),+ rule("resource-groups", "POST", "^/_rg/group-resources/?$", "resource-groups:GroupResources"),+ rule("resource-groups", "POST", "^/_rg/list-group-resources/?$", "resource-groups:ListGroupResources"),+// ── Kinesis ────────────────────────────────────────────────────────────rule("kinesis", "POST", ".*", "kinesis:*"));diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.javaindex 27c799b234..d9764c386e 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java@@ -108,10 +108,27 @@ public class ResourceArnBuilder {case "ssm" -> List.of(buildSsmArn(ctx, region, accountId));case "kms" -> List.of(buildKmsArn(path, region, accountId));case "iam" -> buildIamArns(ctx, region, accountId);+ case "resource-groups" -> List.of(buildResourceGroupArn(ctx, region, accountId));default -> List.of("*");};}+ private String buildResourceGroupArn(ContainerRequestContext ctx, String region, String accountId) {+ JsonNode body = readJsonBody(ctx);+ if (body == null) {+ return "*";+ }+ String path = ctx.getUriInfo().getPath();+ boolean create = path.endsWith("/groups") || path.endsWith("/groups/");+ String group = create ? body.path("Name").asText()+ : body.path("Group").asText(body.path("GroupName").asText());+ if (group.isEmpty()) {+ return "*";+ }+ return group.startsWith("arn:") ? group+ : AwsArnUtils.Arn.of("resource-groups", region, accountId, "group/" + group).toString();+ }+/** Uses the serving operation and the same raw names as the handler. */public List<String> buildLogResources(String action, ContainerRequestContext ctx,String region, String accountId, String partition) {diff --git a/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsController.java b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsController.javanew file mode 100644index 0000000000..66c63ffdde--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsController.java@@ -0,0 +1,92 @@+package io.github.hectorvent.floci.services.resourcegroups;++import com.fasterxml.jackson.core.JsonProcessingException;+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.core.common.RegionResolver;+import io.github.hectorvent.floci.core.common.RequestContext;+import io.github.hectorvent.floci.services.resourcegroups.model.ResourceGroup;+import jakarta.inject.Inject;+import jakarta.ws.rs.Consumes;+import jakarta.ws.rs.POST;+import jakarta.ws.rs.Path;+import jakarta.ws.rs.Produces;+import jakarta.ws.rs.core.Context;+import jakarta.ws.rs.core.HttpHeaders;+import jakarta.ws.rs.core.MediaType;+import jakarta.ws.rs.core.Response;++import java.util.LinkedHashMap;+import java.util.Map;++@Path(ResourceGroupsRouteFilter.INTERNAL_PREFIX)+@Consumes(MediaType.APPLICATION_JSON)+@Produces(MediaType.APPLICATION_JSON)+public class ResourceGroupsController {+ private final ResourceGroupsService service;+ private final RegionResolver regions;+ private final RequestContext context;+ private final ObjectMapper mapper;++ @Inject+ public ResourceGroupsController(ResourceGroupsService service, RegionResolver regions,+ RequestContext context, ObjectMapper mapper) {+ this.service = service;+ this.regions = regions;+ this.context = context;+ this.mapper = mapper;+ }++ @POST+ @Path("/groups")+ public Response createGroup(@Context HttpHeaders headers, String body) {+ ResourceGroup group = service.create(regions.resolveRegion(headers), context.getAccountId(), parse(body));+ return Response.ok(Map.of("Group", groupMetadata(group), "Tags", group.tags(),+ "GroupConfiguration", Map.of("Configuration", group.configuration(), "Status", "UPDATE_COMPLETE"))).build();+ }++ @POST+ @Path("/get-group")+ public Response getGroup(@Context HttpHeaders headers, String body) {+ return Response.ok(Map.of("Group", groupMetadata(+ service.get(regions.resolveRegion(headers), context.getAccountId(), parse(body))))).build();+ }++ @POST+ @Path("/delete-group")+ public Response deleteGroup(@Context HttpHeaders headers, String body) {+ return Response.ok(Map.of("Group", groupMetadata(+ service.delete(regions.resolveRegion(headers), context.getAccountId(), parse(body))))).build();+ }++ @POST+ @Path("/group-resources")+ public Response groupResources(@Context HttpHeaders headers, String body) {+ return Response.ok(service.add(regions.resolveRegion(headers), context.getAccountId(), parse(body))).build();+ }++ @POST+ @Path("/list-group-resources")+ public Response listGroupResources(@Context HttpHeaders headers, String body) {+ return Response.ok(service.list(regions.resolveRegion(headers), context.getAccountId(), parse(body))).build();+ }++ private Map<String, Object> groupMetadata(ResourceGroup group) {+ Map<String, Object> result = new LinkedHashMap<>();+ result.put("GroupArn", group.arn());+ result.put("Name", group.name());+ if (group.description() != null) {+ result.put("Description", group.description());+ }+ return result;+ }++ private JsonNode parse(String body) {+ try {+ return mapper.readTree(body == null ? "{}" : body);+ } catch (JsonProcessingException error) {+ throw new AwsException("BadRequestException", "The request must contain a JSON object.", 400);+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRequestShapes.java b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRequestShapes.javanew file mode 100644index 0000000000..8a70ffb016--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRequestShapes.java@@ -0,0 +1,100 @@+package io.github.hectorvent.floci.services.resourcegroups;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.TextNode;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.VendoredResources;++import java.util.HashMap;+import java.util.Map;+import java.util.regex.Pattern;++final class ResourceGroupsRequestShapes {+ private static final JsonNode MODEL = VendoredResources.read("aws/resource-groups-request-shapes.json",+ "Resource Groups request shapes", input -> new ObjectMapper().readTree(input));+ private static final Map<String, Pattern> PATTERNS = new HashMap<>();++ static {+ for (Map.Entry<String, JsonNode> shape : MODEL.path("shapes").properties()) {+ if (shape.getValue().has("pattern")) {+ PATTERNS.put(shape.getKey(), Pattern.compile(shape.getValue().get("pattern").asText()));+ }+ }+ }++ private ResourceGroupsRequestShapes() {+ }++ static void check(String operation, JsonNode request) {+ checkValue(MODEL.path("operations").path(operation).asText(), request, operation);+ }++ private static void checkValue(String name, JsonNode value, String path) {+ JsonNode shape = MODEL.path("shapes").path(name);+ String type = shape.path("type").asText();+ boolean validType = value != null && switch (type) {+ case "structure", "map" -> value.isObject();+ case "list" -> value.isArray();+ case "string" -> value.isTextual();+ case "integer", "long" -> value.isIntegralNumber() && value.canConvertToLong();+ default -> false;+ };+ if (!validType) {+ throw bad(path + " must be a " + type + ".");+ }+ long size = switch (type) {+ case "string" -> value.textValue().codePointCount(0, value.textValue().length());+ case "integer", "long" -> value.longValue();+ default -> value.size();+ };+ if (shape.has("min") && size < shape.get("min").asLong()+ || shape.has("max") && size > shape.get("max").asLong()) {+ throw bad(path + " is outside the modeled bounds.");+ }+ if (PATTERNS.containsKey(name) && !PATTERNS.get(name).matcher(value.asText()).matches()) {+ throw bad(path + " does not match the modeled pattern.");+ }+ if (shape.has("enum")) {+ boolean known = false;+ for (JsonNode choice : shape.get("enum")) {+ known |= choice.equals(value);+ }+ if (!known) {+ throw bad(path + " is not a modeled value.");+ }+ }+ switch (type) {+ case "structure" -> {+ for (JsonNode required : shape.path("required")) {+ if (!value.hasNonNull(required.asText())) {+ throw bad(path + "." + required.asText() + " is required.");+ }+ }+ for (Map.Entry<String, JsonNode> field : value.properties()) {+ JsonNode member = shape.path("members").get(field.getKey());+ if (member != null && !field.getValue().isNull()) {+ checkValue(member.asText(), field.getValue(), path + "." + field.getKey());+ }+ }+ }+ case "list" -> {+ for (JsonNode item : value) {+ checkValue(shape.get("member").asText(), item, path + "[]");+ }+ }+ case "map" -> {+ for (Map.Entry<String, JsonNode> field : value.properties()) {+ checkValue(shape.get("key").asText(), TextNode.valueOf(field.getKey()), path + ".key");+ checkValue(shape.get("value").asText(), field.getValue(), path + "." + field.getKey());+ }+ }+ default -> {+ }+ }+ }++ private static AwsException bad(String message) {+ return new AwsException("BadRequestException", message, 400);+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRouteFilter.java b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRouteFilter.javanew file mode 100644index 0000000000..73b959bc33--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsRouteFilter.java@@ -0,0 +1,30 @@+package io.github.hectorvent.floci.services.resourcegroups;++import io.github.hectorvent.floci.core.common.ResolvedServiceCatalog;+import io.github.hectorvent.floci.core.common.SignedServiceRoute;+import jakarta.inject.Inject;+import jakarta.ws.rs.container.ContainerRequestContext;+import jakarta.ws.rs.container.ContainerRequestFilter;+import jakarta.ws.rs.container.PreMatching;+import jakarta.ws.rs.ext.Provider;++import java.util.Set;++@Provider+@PreMatching+public class ResourceGroupsRouteFilter implements ContainerRequestFilter {+ static final String INTERNAL_PREFIX = "/_rg";+ private static final Set<String> PUBLIC_PATHS = Set.of(+ "/groups", "/get-group", "/delete-group", "/group-resources", "/list-group-resources");+ private final SignedServiceRoute route;++ @Inject+ public ResourceGroupsRouteFilter(ResolvedServiceCatalog catalog) {+ route = new SignedServiceRoute(catalog, "resource-groups", INTERNAL_PREFIX, PUBLIC_PATHS::contains);+ }++ @Override+ public void filter(ContainerRequestContext ctx) {+ route.apply(ctx);+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsService.java b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsService.javanew file mode 100644index 0000000000..f6e7dda192--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsService.java@@ -0,0 +1,345 @@+package io.github.hectorvent.floci.services.resourcegroups;++import com.fasterxml.jackson.core.type.TypeReference;+import com.fasterxml.jackson.databind.JsonNode;+import io.github.hectorvent.floci.core.common.AwsArnUtils;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.AwsRegions;+import io.github.hectorvent.floci.core.common.Resettable;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.ec2.model.CapacityReservation;+import io.github.hectorvent.floci.services.resourcegroups.model.ResourceGroup;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;++import java.nio.charset.StandardCharsets;+import java.time.Clock;+import java.util.ArrayList;+import java.util.Base64;+import java.util.LinkedHashMap;+import java.util.LinkedHashSet;+import java.util.List;+import java.util.Locale;+import java.util.Map;+import java.util.Set;+import java.util.UUID;++@ApplicationScoped+public class ResourceGroupsService implements Resettable {+ static final String RESOURCE_TYPE = "AWS::EC2::CapacityReservation";+ private static final String POOL_TYPE = "AWS::EC2::CapacityReservationPool";+ private static final String GENERIC_TYPE = "AWS::ResourceGroups::Generic";+ static final int MAX_PAGES = 256;+ static final long PAGE_IDLE_MILLIS = 15 * 60 * 1000L;++ private final AccountAwareStorageBackend<ResourceGroup> groups;+ private final Ec2Service ec2;+ private final Clock clock;+ private final Map<String, Page> pages = new LinkedHashMap<>();++ private record Page(String groupArn, String generation, List<String> filter,+ List<String> remaining, long lastRead) {}++ @Inject+ public ResourceGroupsService(StorageFactory storageFactory, Ec2Service ec2) {+ this(storageFactory.create("resource-groups", "resource-groups.json",+ new TypeReference<Map<String, ResourceGroup>>() {}), ec2, Clock.systemUTC());+ }++ ResourceGroupsService(AccountAwareStorageBackend<ResourceGroup> groups, Ec2Service ec2, Clock clock) {+ this.groups = groups;+ this.ec2 = ec2;+ this.clock = clock;+ }++ public synchronized ResourceGroup create(String region, String account, JsonNode request) {+ ResourceGroupsRequestShapes.check("CreateGroup", request);+ String name = text(request, "Name", true);+ validName(name);+ if (name.toLowerCase(Locale.ROOT).startsWith("aws")) {+ throw bad("Group names beginning with AWS are reserved.");+ }+ if (request.hasNonNull("ResourceQuery")) {+ throw bad("Only service-configured capacity reservation pools are supported.");+ }+ List<Map<String, Object>> configuration = configuration(request.get("Configuration"));+ if (request.hasNonNull("Criticality") || request.hasNonNull("Owner") || request.hasNonNull("DisplayName")) {+ throw bad("Unsupported parameter for this operation");+ }+ String description = text(request, "Description", false);+ Map<String, String> tags = tags(request.get("Tags"));+ String key = key(region, name);+ if (groups.getForAccount(account, key).isPresent()) {+ throw bad("A resource group with this name already exists.");+ }+ ResourceGroup group = new ResourceGroup(name,+ AwsArnUtils.Arn.of("resource-groups", region, account, "group/" + name).toString(),+ description, tags, configuration, List.of(), UUID.randomUUID().toString());+ groups.putForAccount(account, key, group);+ return group;+ }++ public synchronized ResourceGroup get(String region, String account, JsonNode request) {+ ResourceGroupsRequestShapes.check("GetGroup", request);+ return selected(region, account, request);+ }++ public synchronized ResourceGroup delete(String region, String account, JsonNode request) {+ ResourceGroupsRequestShapes.check("DeleteGroup", request);+ ResourceGroup group = selected(region, account, request);+ groups.deleteForAccount(account, key(region, group.name()));+ pages.values().removeIf(page -> page.generation().equals(group.generation()));+ return group;+ }++ public synchronized Map<String, Object> add(String region, String account, JsonNode request) {+ ResourceGroupsRequestShapes.check("GroupResources", request);+ ResourceGroup group = selected(region, account, request);+ JsonNode resources = request.get("ResourceArns");+ if (resources == null || !resources.isArray() || resources.isEmpty() || resources.size() > 10) {+ throw bad("ResourceArns must contain one to ten resource ARNs.");+ }+ Set<String> requested = new LinkedHashSet<>();+ for (JsonNode resource : resources) {+ if (!resource.isTextual() || resource.asText().length() > 2048+ || !AwsArnUtils.isArn(resource.asText())) {+ throw bad("ResourceArns must contain valid resource ARNs.");+ }+ requested.add(resource.asText());+ }+ List<String> succeeded = new ArrayList<>();+ List<Map<String, String>> failed = new ArrayList<>();+ for (String resource : requested) {+ String failure = reservationFailure(region, account, resource);+ if (failure == null) {+ succeeded.add(resource);+ } else {+ failed.add(Map.of("ResourceArn", resource, "ErrorCode", failure,+ "ErrorMessage", "The reservation identity or state does not allow membership."));+ }+ }+ // Validate every lookup before changing the stored group, including unexpected backend failures.+ Set<String> members = new LinkedHashSet<>(group.members());+ members.addAll(succeeded);+ groups.putForAccount(account, key(region, group.name()), group.withMembers(new ArrayList<>(members)));+ return Map.of("Succeeded", succeeded, "Failed", failed, "Pending", List.of());+ }++ private String reservationFailure(String region, String account, String resource) {+ AwsArnUtils.Arn arn = AwsArnUtils.parse(resource);+ if (!arn.partition().equals(AwsRegions.partitionFor(region)) || !arn.service().equals("ec2")+ || !arn.resource().startsWith("capacity-reservation/")) {+ return "ResourceArnValidationException";+ }+ if (!arn.region().equals(region)) {+ return "ValidationException";+ }+ if (!arn.accountId().equals(account)+ || !arn.resource().matches("capacity-reservation/cr-[0-9a-f]{17}")) {+ return "ResourceGroupsValidationException";+ }+ List<CapacityReservation> found;+ try {+ found = ec2.describeCapacityReservations(region,+ List.of(arn.resource().substring("capacity-reservation/".length())), Map.of());+ } catch (AwsException error) {+ if ("InvalidCapacityReservationId.NotFound".equals(error.getErrorCode())) {+ return "ResourceGroupsValidationException";+ }+ throw error;+ }+ if (found.size() != 1 || !resource.equals(found.getFirst().getCapacityReservationArn())+ || !account.equals(found.getFirst().getOwnerId())) {+ throw new AwsException("InternalServerErrorException", "Reservation lookup returned an inconsistent identity.", 500);+ }+ return "active".equals(found.getFirst().getState()) ? null : "ResourceGroupsValidationException";+ }++ public synchronized Map<String, Object> list(String region, String account, JsonNode request) {+ ResourceGroupsRequestShapes.check("ListGroupResources", request);+ ResourceGroup group = selected(region, account, request);+ int maximum = 50;+ if (request.has("MaxResults")) {+ JsonNode value = request.get("MaxResults");+ if (!value.isIntegralNumber() || !value.canConvertToInt() || value.intValue() < 1 || value.intValue() > 50) {+ throw bad("MaxResults must be between 1 and 50.");+ }+ maximum = value.intValue();+ }+ List<String> filter = filter(request.get("Filters"));+ String token = text(request, "NextToken", false);+ if (token != null && (token.length() > 8192 || !token.matches("[a-zA-Z0-9+/]*={0,2}"))) {+ throw bad("NextToken must be a base64 token.");+ }+ long now = clock.millis();+ pages.values().removeIf(page -> now - page.lastRead() >= PAGE_IDLE_MILLIS);+ List<String> selected;+ if (token == null) {+ selected = group.members();+ } else {+ Page page = pages.get(token);+ if (page == null || !page.groupArn().equals(group.arn())+ || !page.generation().equals(group.generation()) || !page.filter().equals(filter)) {+ throw bad("NextToken does not match this group and filter, or has expired.");+ }+ pages.remove(token);+ pages.put(token, new Page(page.groupArn(), page.generation(), page.filter(), page.remaining(), now));+ selected = page.remaining().stream().filter(group.members()::contains).toList();+ }+ List<Map<String, String>> identifiers = selected.stream().limit(maximum)+ .map(arn -> Map.of("ResourceArn", arn, "ResourceType", RESOURCE_TYPE)).toList();+ Map<String, Object> response = new LinkedHashMap<>();+ response.put("ResourceIdentifiers", identifiers);+ response.put("Resources", identifiers.stream().map(identifier -> Map.of("Identifier", identifier)).toList());+ if (selected.size() > maximum) {+ if (pages.size() >= MAX_PAGES) {+ pages.remove(pages.keySet().iterator().next());+ }+ String next = Base64.getEncoder().encodeToString(+ UUID.randomUUID().toString().getBytes(StandardCharsets.US_ASCII));+ pages.put(next, new Page(group.arn(), group.generation(), filter,+ List.copyOf(selected.subList(maximum, selected.size())), now));+ response.put("NextToken", next);+ }+ return response;+ }++ private ResourceGroup selected(String region, String account, JsonNode request) {+ if (request.has("Group") && request.has("GroupName")) {+ throw bad("Specify Group or GroupName, not both.");+ }+ String selected = text(request, request.has("Group") ? "Group" : "GroupName", true);+ String name = selected;+ if (selected.startsWith("arn:")) {+ if (!AwsArnUtils.isArn(selected)) {+ throw bad("Invalid group ARN.");+ }+ AwsArnUtils.Arn arn = AwsArnUtils.parse(selected);+ if (!arn.partition().equals(AwsRegions.partitionFor(region)) || !arn.service().equals("resource-groups")+ || !arn.region().equals(region) || !arn.accountId().equals(account)+ || !arn.resource().startsWith("group/")) {+ throw bad("The group ARN must belong to the calling account and region.");+ }+ name = arn.resource().substring("group/".length());+ }+ validName(name);+ ResourceGroup group = groups.getForAccount(account, key(region, name))+ .orElseThrow(() -> new AwsException("NotFoundException", "The resource group was not found.", 404));+ String expected = AwsArnUtils.Arn.of("resource-groups", region, account, "group/" + name).toString();+ if (!group.arn().equals(expected)) {+ throw new AwsException("InternalServerErrorException", "Stored group identity is inconsistent.", 500);+ }+ return group;+ }++ private static List<Map<String, Object>> configuration(JsonNode configuration) {+ if (configuration == null || !configuration.isArray() || configuration.size() != 2) {+ throw bad("A capacity reservation pool Configuration is required.");+ }+ Set<String> types = new LinkedHashSet<>();+ for (JsonNode item : configuration) {+ requireObject(item);+ String type = text(item, "Type", true);+ if (!types.add(type)) {+ throw bad("Configuration types must be distinct.");+ }+ JsonNode parameters = item.get("Parameters");+ if (POOL_TYPE.equals(type)) {+ if (parameters != null && (!parameters.isArray() || !parameters.isEmpty())) {+ throw bad("CapacityReservationPool does not accept these parameters.");+ }+ } else if (GENERIC_TYPE.equals(type)) {+ if (parameters == null || !parameters.isArray() || parameters.size() != 1) {+ throw bad("The allowed-resource-types parameter is required.");+ }+ JsonNode parameter = parameters.get(0);+ requireObject(parameter);+ JsonNode values = parameter.get("Values");+ if (!"allowed-resource-types".equals(text(parameter, "Name", true))+ || values == null || !values.isArray() || values.size() != 1+ || !values.get(0).isTextual() || !RESOURCE_TYPE.equals(values.get(0).asText())) {+ throw bad("Only CapacityReservation resources are supported.");+ }+ } else {+ throw bad("Only capacity reservation pool configurations are supported.");+ }+ }+ return List.of(Map.of("Type", POOL_TYPE), Map.of("Type", GENERIC_TYPE, "Parameters",+ List.of(Map.of("Name", "allowed-resource-types", "Values", List.of(RESOURCE_TYPE)))));+ }++ private static List<String> filter(JsonNode filters) {+ if (filters == null || filters.isArray() && filters.isEmpty()) {+ return List.of();+ }+ if (!filters.isArray() || filters.size() != 1) {+ throw bad("Only one resource-type filter is supported.");+ }+ JsonNode filter = filters.get(0);+ requireObject(filter);+ JsonNode values = filter.get("Values");+ if (!"resource-type".equals(text(filter, "Name", true)) || values == null+ || !values.isArray() || values.isEmpty() || values.size() > 5) {+ throw bad("Invalid resource-type filter.");+ }+ for (JsonNode value : values) {+ if (!value.isTextual() || !RESOURCE_TYPE.equals(value.asText())) {+ throw bad("The resource type is not allowed by the group configuration.");+ }+ }+ return List.of(RESOURCE_TYPE);+ }++ private static Map<String, String> tags(JsonNode tags) {+ Map<String, String> result = new LinkedHashMap<>();+ if (tags == null || tags.isNull()) {+ return result;+ }+ if (!tags.isObject() || tags.size() > 50) {+ throw bad("Tags must be an object containing at most 50 entries.");+ }+ tags.fields().forEachRemaining(entry -> {+ result.put(entry.getKey(), entry.getValue().asText());+ });+ return result;+ }++ private static void validName(String name) {+ if (name == null || !name.matches("[A-Za-z0-9_.-]{1,300}")) {+ throw bad("Invalid resource group name.");+ }+ }++ private static String key(String region, String name) {+ return region + "::" + name;+ }++ private static String text(JsonNode object, String field, boolean required) {+ JsonNode value = object.get(field);+ if ((value == null || value.isNull()) && !required) {+ return null;+ }+ if (value == null || !value.isTextual() || required && value.asText().isEmpty()) {+ throw bad(field + " must be a string.");+ }+ return value.asText();+ }++ private static void requireObject(JsonNode request) {+ if (request == null || !request.isObject()) {+ throw bad("A JSON object is required.");+ }+ }++ private static AwsException bad(String message) {+ return new AwsException("BadRequestException", message, 400);+ }++ @Override+ public synchronized void clear() {+ groups.clear();+ pages.clear();+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/resourcegroups/model/ResourceGroup.java b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/model/ResourceGroup.javanew file mode 100644index 0000000000..d9d314cc9b--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/resourcegroups/model/ResourceGroup.java@@ -0,0 +1,14 @@+package io.github.hectorvent.floci.services.resourcegroups.model;++import io.quarkus.runtime.annotations.RegisterForReflection;++import java.util.List;+import java.util.Map;++@RegisterForReflection+public record ResourceGroup(String name, String arn, String description, Map<String, String> tags,+ List<Map<String, Object>> configuration, List<String> members, String generation) {+ public ResourceGroup withMembers(List<String> updated) {+ return new ResourceGroup(name, arn, description, tags, configuration, List.copyOf(updated), generation);+ }+}diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex 78b5009a01..296c8addee 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -242,6 +242,9 @@ floci:flush-interval-ms: 5000codedeploy:flush-interval-ms: 5000+ resource-groups:+ mode: ${FLOCI_STORAGE_SERVICES_RESOURCE_GROUPS_MODE:}+ flush-interval-ms: ${FLOCI_STORAGE_SERVICES_RESOURCE_GROUPS_FLUSH_INTERVAL_MS:5000}tagging:flush-interval-ms: 5000elasticbeanstalk:@@ -983,6 +986,8 @@ floci:enabled: truelakeformation:enabled: true+ resource-groups:+ enabled: truetagging:enabled: truepipes:diff --git a/src/main/resources/aws/resource-groups-request-shapes.json b/src/main/resources/aws/resource-groups-request-shapes.jsonnew file mode 100644index 0000000000..e25f004cfd--- /dev/null+++ b/src/main/resources/aws/resource-groups-request-shapes.json@@ -0,0 +1,253 @@+{+ "_source": {+ "generator": "tools/aws/regen_resource_groups_shapes.py",+ "botocore": "botocore 1.43.108 (installed package)"+ },+ "operations": {+ "CreateGroup": "CreateGroupInput",+ "GetGroup": "GetGroupInput",+ "DeleteGroup": "DeleteGroupInput",+ "GroupResources": "GroupResourcesInput",+ "ListGroupResources": "ListGroupResourcesInput"+ },+ "shapes": {+ "CreateGroupInput": {+ "type": "structure",+ "required": [+ "Name"+ ],+ "members": {+ "Name": "CreateGroupName",+ "Description": "Description",+ "ResourceQuery": "ResourceQuery",+ "Tags": "Tags",+ "Configuration": "GroupConfigurationList",+ "Criticality": "Criticality",+ "Owner": "Owner",+ "DisplayName": "DisplayName"+ }+ },+ "CreateGroupName": {+ "type": "string",+ "min": 1,+ "max": 300,+ "pattern": "[a-zA-Z0-9_\\.-]+"+ },+ "Criticality": {+ "type": "integer",+ "min": 1,+ "max": 10+ },+ "DeleteGroupInput": {+ "type": "structure",+ "members": {+ "GroupName": "GroupName",+ "Group": "GroupStringV2"+ }+ },+ "Description": {+ "type": "string",+ "max": 1024,+ "pattern": "[\\sa-zA-Z0-9_\\.-]*"+ },+ "DisplayName": {+ "type": "string",+ "min": 0,+ "max": 300,+ "pattern": "^([\\p{L}\\p{Z}\\p{N}_.:/=+\\-@]*)$"+ },+ "GetGroupInput": {+ "type": "structure",+ "members": {+ "GroupName": "GroupName",+ "Group": "GroupStringV2"+ }+ },+ "GroupConfigurationItem": {+ "type": "structure",+ "required": [+ "Type"+ ],+ "members": {+ "Type": "GroupConfigurationType",+ "Parameters": "GroupParameterList"+ }+ },+ "GroupConfigurationList": {+ "type": "list",+ "max": 2,+ "member": "GroupConfigurationItem"+ },+ "GroupConfigurationParameter": {+ "type": "structure",+ "required": [+ "Name"+ ],+ "members": {+ "Name": "GroupConfigurationParameterName",+ "Values": "GroupConfigurationParameterValueList"+ }+ },+ "GroupConfigurationParameterName": {+ "type": "string",+ "min": 1,+ "max": 80,+ "pattern": "[a-z-]+"+ },+ "GroupConfigurationParameterValue": {+ "type": "string",+ "min": 1,+ "max": 256,+ "pattern": "[a-zA-Z0-9:\\/\\._-]+"+ },+ "GroupConfigurationParameterValueList": {+ "type": "list",+ "member": "GroupConfigurationParameterValue"+ },+ "GroupConfigurationType": {+ "type": "string",+ "max": 40,+ "pattern": "AWS::[a-zA-Z0-9]+::[a-zA-Z0-9]+"+ },+ "GroupName": {+ "type": "string",+ "min": 1,+ "max": 300,+ "pattern": "[a-zA-Z0-9_\\.-]{1,300}|[a-zA-Z0-9_\\.-]{1,150}/[a-z0-9]{26}"+ },+ "GroupParameterList": {+ "type": "list",+ "member": "GroupConfigurationParameter"+ },+ "GroupResourcesInput": {+ "type": "structure",+ "required": [+ "Group",+ "ResourceArns"+ ],+ "members": {+ "Group": "GroupStringV2",+ "ResourceArns": "ResourceArnList"+ }+ },+ "GroupStringV2": {+ "type": "string",+ "min": 1,+ "max": 1600,+ "pattern": "[a-zA-Z0-9_\\.-]{1,300}|[a-zA-Z0-9_\\.-]{1,150}/[a-z0-9]{26}|arn:aws(-[a-z]+)*:resource-groups:[a-z]{2}(-[a-z]+)+-\\d{1}:[0-9]{12}:group/([a-zA-Z0-9_\\.-]{1,300}|[a-zA-Z0-9_\\.-]{1,150}/[a-z0-9]{26})"+ },+ "ListGroupResourcesInput": {+ "type": "structure",+ "members": {+ "GroupName": "GroupName",+ "Group": "GroupStringV2",+ "Filters": "ResourceFilterList",+ "MaxResults": "MaxResults",+ "NextToken": "NextToken"+ }+ },+ "MaxResults": {+ "type": "integer",+ "min": 1,+ "max": 50+ },+ "NextToken": {+ "type": "string",+ "min": 0,+ "max": 8192,+ "pattern": "^[a-zA-Z0-9+/]*={0,2}$"+ },+ "Owner": {+ "type": "string",+ "min": 0,+ "max": 300,+ "pattern": "^([\\p{L}\\p{Z}\\p{N}_.:/=+\\-@]*)$"+ },+ "Query": {+ "type": "string",+ "max": 4096,+ "pattern": "[\\s\\S]*"+ },+ "QueryType": {+ "type": "string",+ "min": 1,+ "max": 128,+ "pattern": "^\\w+$",+ "enum": [+ "TAG_FILTERS_1_0",+ "CLOUDFORMATION_STACK_1_0"+ ]+ },+ "ResourceArn": {+ "type": "string",+ "pattern": "arn:aws(-[a-z]+)*:[a-z0-9\\-]*:([a-z]{2}(-[a-z]+)+-\\d{1})?:([0-9]{12})?:.+"+ },+ "ResourceArnList": {+ "type": "list",+ "min": 1,+ "max": 10,+ "member": "ResourceArn"+ },+ "ResourceFilter": {+ "type": "structure",+ "required": [+ "Name",+ "Values"+ ],+ "members": {+ "Name": "ResourceFilterName",+ "Values": "ResourceFilterValues"+ }+ },+ "ResourceFilterList": {+ "type": "list",+ "member": "ResourceFilter"+ },+ "ResourceFilterName": {+ "type": "string",+ "enum": [+ "resource-type"+ ]+ },+ "ResourceFilterValue": {+ "type": "string",+ "min": 1,+ "max": 128,+ "pattern": "AWS::[a-zA-Z0-9]+::[a-zA-Z0-9]+"+ },+ "ResourceFilterValues": {+ "type": "list",+ "min": 1,+ "max": 5,+ "member": "ResourceFilterValue"+ },+ "ResourceQuery": {+ "type": "structure",+ "required": [+ "Type",+ "Query"+ ],+ "members": {+ "Type": "QueryType",+ "Query": "Query"+ }+ },+ "TagKey": {+ "type": "string",+ "min": 1,+ "max": 128,+ "pattern": "^([\\p{L}\\p{Z}\\p{N}_.:/=+\\-@]*)$"+ },+ "TagValue": {+ "type": "string",+ "min": 0,+ "max": 256,+ "pattern": "^([\\p{L}\\p{Z}\\p{N}_.:/=+\\-@]*)$"+ },+ "Tags": {+ "type": "map",+ "key": "TagKey",+ "value": "TagValue"+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionCrossServiceSmokeIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionCrossServiceSmokeIntegrationTest.javaindex 1af1c5d9c4..4e196e79cd 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/PartitionCrossServiceSmokeIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionCrossServiceSmokeIntegrationTest.java@@ -45,7 +45,7 @@ class PartitionCrossServiceSmokeIntegrationTest {"ecr", "ecs", "efs", "eks", "elasticache", "elasticbeanstalk", "elbv2", "emr", "emrserverless","eventbridge", "firehose", "fis", "glue", "guardduty", "iot", "kinesis", "kinesisanalytics", "lambda","lambdamicrovms", "lambdaweb", "memorydb", "msk", "mwaa", "networkfirewall", "oam", "opensearch", "organizations",- "pipes", "ram", "rds", "redshift", "redshiftserverless", "resourceexplorer2", "route53resolver",+ "pipes", "ram", "rds", "redshift", "redshiftserverless", "resourceexplorer2", "resourcegroups", "route53resolver","s3tables", "s3vectors", "sagemaker", "scheduler", "secretsmanager", "servicequotas", "ses", "sns", "sqs","stepfunctions", "timestreaminfluxdb", "transfer", "verifiedpermissions", "wafv2");@@ -61,6 +61,22 @@ class PartitionCrossServiceSmokeIntegrationTest {RestAssuredJsonUtils.configureAwsContentTypes();}+ @ParameterizedTest+ @MethodSource(PartitionMatrix.NON_COMMERCIAL_CASES)+ void resourceGroupArn(PartitionCase partitionCase) {+ String region = offered(partitionCase, "resource-groups");+ String name = unique("smoke-pool", partitionCase);+ String body = "{\"Name\":\"" + name + "\",\"Configuration\":["+ + "{\"Type\":\"AWS::EC2::CapacityReservationPool\"},"+ + "{\"Type\":\"AWS::ResourceGroups::Generic\",\"Parameters\":["+ + "{\"Name\":\"allowed-resource-types\",\"Values\":[\"AWS::EC2::CapacityReservation\"]}]}]}";+ String arn = signed(region, "resource-groups").contentType(JSON).body(body)+ .when().post("/groups").then().statusCode(200).extract().path("Group.GroupArn");+ cleanup.register(() -> signed(region, "resource-groups").contentType(JSON)+ .body("{\"Group\":\"" + arn + "\"}").when().post("/delete-group"));+ PartitionMatrix.assertArnIn(partitionCase, arn);+ }+@ParameterizedTest@MethodSource(PartitionMatrix.NON_COMMERCIAL_CASES)void snsTopicArn(PartitionCase partitionCase) {diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.javaindex dbcfe5c51a..30d8699619 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java@@ -36,6 +36,19 @@ class IamActionRegistryTest {private final IamActionRegistry registry = new IamActionRegistry();+ @ParameterizedTest+ @CsvSource({"groups,CreateGroup", "get-group,GetGroup", "delete-group,DeleteGroup",+ "group-resources,GroupResources", "list-group-resources,ListGroupResources"})+ void resourceGroupsManagementRoutesResolveTheirOwnActions(String path, String operation) {+ ContainerRequestContext ctx = mockCtx("POST", "/_rg/" + path,+ new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}");+ assertEquals("resource-groups:" + operation, registry.resolveRoute("resource-groups", ctx));+ assertNull(registry.resolveRoute("resource-groups", mockCtx("GET", "/_rg/" + path,+ new MultivaluedHashMap<>(), null, "")));+ assertNull(registry.resolveRoute("resource-groups", mockCtx("POST", "/_rg/" + path + "/extra",+ new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}")));+ }+@Testvoid cloudWatchGetUsesQueryActionAndOperationPrecedence() {MultivaluedMap<String, String> query = new MultivaluedHashMap<>();diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.javaindex e9d30f3c89..ab39b543f2 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java@@ -682,6 +682,23 @@ class ResourceArnBuilderTest {}).when(ctx).setEntityStream(any(InputStream.class));}+ @Test+ void resourceGroupTargetsUseNamesOrArnsInTheRequestPartition() {+ for (String region : List.of("us-east-1", "cn-north-1", "us-gov-west-1")) {+ String arn = "arn:" + AwsRegions.partitionFor(region) + ":resource-groups:"+ + region + ":" + ACCOUNT + ":group/pool";+ for (String selector : List.of("Name", "GroupName", "Group")) {+ when(uriInfo.getPath()).thenReturn(selector.equals("Name") ? "_rg/groups" : "_rg/get-group");+ setJsonBody("{\"" + selector + "\":\"pool\"}");+ assertEquals(List.of(arn), builder.buildResources("resource-groups", ctx, region, ACCOUNT));+ }+ setJsonBody("{\"Group\":\"" + arn + "\"}");+ assertEquals(List.of(arn), builder.buildResources("resource-groups", ctx, region, ACCOUNT));+ }+ setJsonBody("{}");+ assertEquals(List.of("*"), builder.buildResources("resource-groups", ctx, "us-east-1", ACCOUNT));+ }+@Testvoid logStreamTargetsUseTheServingActionAndRequestPartition() {for (String action : List.of("logs:CreateLogStream", "logs:DeleteLogStream")) {diff --git a/src/test/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsIamEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsIamEnforcementIntegrationTest.javanew file mode 100644index 0000000000..41e3d39409--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsIamEnforcementIntegrationTest.java@@ -0,0 +1,146 @@+package io.github.hectorvent.floci.services.resourcegroups;++import io.github.hectorvent.floci.testing.EnforcementFixtures;+import io.github.hectorvent.floci.testing.IamEnforcementProfile;+import io.github.hectorvent.floci.testing.PartitionCleanup;+import io.quarkus.test.common.http.TestHTTPResource;+import io.quarkus.test.junit.QuarkusTest;+import io.quarkus.test.junit.TestProfile;+import io.restassured.response.ValidatableResponse;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.extension.RegisterExtension;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;++import java.net.URI;+import java.util.Map;+import java.util.UUID;++import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;+import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;++@QuarkusTest+@TestProfile(IamEnforcementProfile.class)+class ResourceGroupsIamEnforcementIntegrationTest {+ private static final String ACCOUNT = "111111111111";+ private static final Map<String, String> OPERATIONS = Map.of(+ "/groups", "CreateGroup",+ "/get-group", "GetGroup",+ "/delete-group", "DeleteGroup",+ "/group-resources", "GroupResources",+ "/list-group-resources", "ListGroupResources");++ @TestHTTPResource("/")+ static URI baseUri;++ @RegisterExtension+ final PartitionCleanup cleanup = new PartitionCleanup();++ @Test+ void explicitDenyAppliesToEveryManagementOperation() {+ String user = "rg-deny-" + UUID.randomUUID();+ String key = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth(ACCOUNT, "iam"), user)+ .accessKeyId();+ EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ACCOUNT, "iam"), user, "deny", """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"*","Resource":"*"},+ {"Effect":"Deny","Action":"resource-groups:*","Resource":"*"}]}+ """);+ for (String path : OPERATIONS.keySet()) {+ call(key, path, Map.of()).statusCode(403).body("__type", equalTo("AccessDeniedException"));+ }+ }++ @Test+ void createAuthorizesNameInsteadOfIgnoredGroupSelectors() {+ String user = "rg-create-scope-" + UUID.randomUUID();+ String key = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth(ACCOUNT, "iam"), user)+ .accessKeyId();+ String name = "rg-secret-" + UUID.randomUUID();+ EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ACCOUNT, "iam"), user, "allow", """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"resource-groups:CreateGroup",+ "Resource":"arn:aws:resource-groups:us-east-1:%s:group/allowed"}]}+ """.formatted(ACCOUNT));+ call(key, "/groups", "{\"Name\":\"" + name + "\",\"Group\":\"allowed\",\"GroupName\":\"allowed\","+ + "\"Configuration\":" + ResourceGroupsServiceTest.CONFIGURATION + "}").statusCode(403);+ call(ACCOUNT, "/get-group", Map.of("Group", name)).statusCode(404);+ }++ @Test+ void groupScopedDenyAppliesToEveryManagementOperation() {+ String user = "rg-scoped-deny-" + UUID.randomUUID();+ String key = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth(ACCOUNT, "iam"), user)+ .accessKeyId();+ String name = "rg-scoped-" + UUID.randomUUID();+ String arn = "arn:aws:resource-groups:us-east-1:" + ACCOUNT + ":group/" + name;+ EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ACCOUNT, "iam"), user, "deny", """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"*","Resource":"*"},+ {"Effect":"Deny","Action":"resource-groups:*","Resource":"%s"}]}+ """.formatted(arn));+ for (String path : OPERATIONS.keySet()) {+ Object body = path.equals("/groups")+ ? "{\"Name\":\"" + name + "\",\"Configuration\":" + ResourceGroupsServiceTest.CONFIGURATION + "}"+ : Map.of("Group", name);+ call(key, path, body).statusCode(403).body("__type", equalTo("AccessDeniedException"));+ if (!path.equals("/groups")) {+ call(key, path, Map.of("Group", arn)).statusCode(403)+ .body("__type", equalTo("AccessDeniedException"));+ }+ }+ call(ACCOUNT, "/get-group", Map.of("Group", name)).statusCode(404);+ }++ @ParameterizedTest+ @CsvSource({"Group,false", "Group,true", "GroupName,false"})+ void aGroupScopedGrantAllowsOnlyThatGroup(String selector, boolean useArn) {+ String name = "rg-scoped-allow-" + UUID.randomUUID();+ String arn = "arn:aws:resource-groups:us-east-1:" + ACCOUNT + ":group/" + name;+ String user = "rg-scoped-allow-" + UUID.randomUUID();+ String key = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth(ACCOUNT, "iam"), user)+ .accessKeyId();+ EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ACCOUNT, "iam"), user, "allow", """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"resource-groups:GetGroup","Resource":"%s"}]}+ """.formatted(arn));+ call(ACCOUNT, "/groups", "{\"Name\":\"" + name + "\",\"Configuration\":"+ + ResourceGroupsServiceTest.CONFIGURATION + "}").statusCode(200);+ try {+ call(key, "/get-group", Map.of(selector, useArn ? arn : name)).statusCode(200);+ call(key, "/get-group", Map.of("Group", name + "-other")).statusCode(403);+ call(key, "/delete-group", Map.of("Group", name)).statusCode(403);+ } finally {+ call(ACCOUNT, "/delete-group", Map.of("Group", name)).statusCode(200);+ }+ }++ @Test+ void allowedManagementOperationsStillReachTheirService() {+ String user = "rg-allow-" + UUID.randomUUID();+ String key = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth(ACCOUNT, "iam"), user)+ .accessKeyId();+ EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ACCOUNT, "iam"), user, "allow", """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"resource-groups:*","Resource":"*"}]}+ """);+ String name = "rg-allow-" + UUID.randomUUID();+ call(key, "/groups", "{\"Name\":\"" + name + "\",\"Configuration\":"+ + ResourceGroupsServiceTest.CONFIGURATION + "}").statusCode(200);+ try {+ call(key, "/get-group", Map.of("Group", name)).statusCode(200);+ call(key, "/list-group-resources", Map.of("Group", name)).statusCode(200);+ call(key, "/group-resources", Map.of("Group", name, "ResourceArns",+ new String[]{"arn:aws:ec2:us-east-1:" + ACCOUNT + ":capacity-reservation/cr-00000000000000001"}))+ .statusCode(200).body("Failed[0].ErrorCode", equalTo("ResourceGroupsValidationException"));+ } finally {+ call(key, "/delete-group", Map.of("Group", name)).statusCode(200);+ }+ }++ private static ValidatableResponse call(String key, String path, Object body) {+ return given().header("Authorization", auth(key, "resource-groups"))+ .contentType("application/json").body(body).post(path).then();+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsIntegrationTest.javanew file mode 100644index 0000000000..0ccca34732--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsIntegrationTest.java@@ -0,0 +1,119 @@+package io.github.hectorvent.floci.services.resourcegroups;++import io.github.hectorvent.floci.core.common.ServiceRegistry;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.ValidatableResponse;+import jakarta.inject.Inject;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;++import java.util.Map;+import java.util.UUID;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.*;+import static org.junit.jupiter.api.Assertions.assertTrue;++@QuarkusTest+class ResourceGroupsIntegrationTest {+ private static final String ACCOUNT = "111122223333";+ private static final String REGION = "eu-west-1";++ @Inject+ ServiceRegistry registry;++ @Test+ void standardServiceRegistrationIncludesResourceGroups() {+ assertTrue(registry.getEnabledServices().contains("resource-groups"));+ }++ private static String auth(String service, String account) {+ return "AWS4-HMAC-SHA256 Credential=" + account + "/20261008/" + REGION + "/" + service + "/aws4_request";+ }++ private static ValidatableResponse call(String path, Object body) {+ return given().header("Authorization", auth("resource-groups", ACCOUNT))+ .contentType("application/json").body(body).post(path).then();+ }++ @Test+ void publicCapacityPoolLifecycleUsesRealEc2Records() {+ String name = "pool-" + UUID.randomUUID();+ String reservation = given().header("Authorization", auth("ec2", ACCOUNT))+ .formParam("Action", "CreateCapacityReservation").formParam("InstanceType", "t3.micro")+ .formParam("InstancePlatform", "Linux/UNIX").formParam("AvailabilityZone", REGION + "a")+ .formParam("InstanceCount", 1).post("/").then().statusCode(200)+ .extract().xmlPath().getString("CreateCapacityReservationResponse.capacityReservation.capacityReservationArn");+ String id = reservation.substring(reservation.lastIndexOf('/') + 1);+ try {+ String arn = call("/groups", "{\"Name\":\"" + name + "\",\"Configuration\":"+ + ResourceGroupsServiceTest.CONFIGURATION + "}").statusCode(200)+ .body("GroupConfiguration.Status", equalTo("UPDATE_COMPLETE"))+ .extract().path("Group.GroupArn");+ try {+ call("/get-group", Map.of("GroupName", name)).statusCode(200).body("Group.GroupArn", equalTo(arn));+ call("/group-resources", Map.of("Group", arn, "ResourceArns", new String[]{reservation}))+ .statusCode(200).body("Succeeded", contains(reservation)).body("Failed", empty())+ .body("Pending", empty());+ call("/list-group-resources", Map.of("Group", arn)).statusCode(200)+ .body("ResourceIdentifiers[0].ResourceArn", equalTo(reservation))+ .body("Resources[0].Identifier.ResourceType", equalTo(ResourceGroupsService.RESOURCE_TYPE));+ given().header("Authorization", auth("resource-groups", "444455556666"))+ .contentType("application/json").body(Map.of("GroupName", name))+ .post("/get-group").then().statusCode(404).body("__type", equalTo("NotFoundException"));+ } finally {+ call("/delete-group", Map.of("GroupName", name)).statusCode(200).body("Group.GroupArn", equalTo(arn));+ }+ call("/get-group", Map.of("GroupName", name)).statusCode(404);+ given().header("Authorization", auth("ec2", ACCOUNT))+ .formParam("Action", "DescribeCapacityReservations").formParam("CapacityReservationId.1", id)+ .post("/").then().statusCode(200)+ .body("DescribeCapacityReservationsResponse.capacityReservationSet.item.state", equalTo("active"));+ } finally {+ given().header("Authorization", auth("ec2", ACCOUNT))+ .formParam("Action", "CancelCapacityReservation").formParam("CapacityReservationId", id)+ .post("/").then().statusCode(200);+ }+ }++ @Test+ void standardRestErrorsRefuseUnsupportedAndMalformedRequests() {+ call("/groups", Map.of("Name", "unsupported", "ResourceQuery", Map.of())).statusCode(400)+ .body("__type", equalTo("BadRequestException"));+ call("/groups", "[]").statusCode(400);+ call("/groups", "{").statusCode(400);+ call("/get-group", Map.of("GroupName", "missing-" + UUID.randomUUID())).statusCode(404);+ }++ @ParameterizedTest+ @ValueSource(strings = {"groups", "get-group", "delete-group", "group-resources", "list-group-resources"})+ void s3BucketPostsKeepTheirOwnRoutes(String bucket) {+ String authorization = auth("s3", ACCOUNT);+ given().header("Authorization", authorization).contentType("application/xml")+ .body("<CreateBucketConfiguration><LocationConstraint>" + REGION+ + "</LocationConstraint></CreateBucketConfiguration>")+ .put("/" + bucket).then().statusCode(200);+ try {+ given().header("Authorization", authorization).body("value").put("/" + bucket + "/key")+ .then().statusCode(200);+ given().header("Authorization", authorization).contentType("application/xml")+ .body("<Delete><Object><Key>key</Key></Object></Delete>").post("/" + bucket + "?delete")+ .then().statusCode(200).body("DeleteResult.Deleted.Key", equalTo("key"));+ given().header("Authorization", authorization).get("/" + bucket + "/key")+ .then().statusCode(404);+ } finally {+ given().header("Authorization", authorization).delete("/" + bucket + "/key");+ given().header("Authorization", authorization).delete("/" + bucket).then().statusCode(204);+ }+ }++ @Test+ void querySignedResourceGroupsRequestsReachTheSameController() {+ given().queryParam("X-Amz-Credential", ACCOUNT + "/20261020/" + REGION + "/resource-groups/aws4_request")+ .contentType("application/json").body(Map.of("Group", "missing-" + UUID.randomUUID()))+ .post("/get-group").then().statusCode(404).body("__type", equalTo("NotFoundException"));+ given().contentType("application/json").body("{}").post("/_rg/groups").then().statusCode(400)+ .body(containsString("<Code>InvalidBucketName</Code>"));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsServiceTest.javanew file mode 100644index 0000000000..3b02968480--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsServiceTest.java@@ -0,0 +1,355 @@+package io.github.hectorvent.floci.services.resourcegroups;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;+import io.github.hectorvent.floci.core.common.AwsArnUtils;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.ec2.model.CapacityReservation;+import io.github.hectorvent.floci.services.resourcegroups.model.ResourceGroup;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;+import org.junit.jupiter.params.provider.ValueSource;++import java.time.Clock;+import java.util.ArrayList;+import java.util.Base64;+import java.util.HashMap;+import java.util.List;+import java.util.Map;++import static org.junit.jupiter.api.Assertions.*;+import static org.mockito.ArgumentMatchers.*;+import static org.mockito.Mockito.*;++class ResourceGroupsServiceTest {+ static final String ACCOUNT = "111122223333";+ static final String REGION = "eu-west-1";+ static final String CONFIGURATION = """+ [{"Type":"AWS::EC2::CapacityReservationPool"},+ {"Type":"AWS::ResourceGroups::Generic","Parameters":[+ {"Name":"allowed-resource-types","Values":["AWS::EC2::CapacityReservation"]}]}]+ """;+ private final ObjectMapper mapper = new ObjectMapper();+ private final Map<String, CapacityReservation> reservations = new HashMap<>();+ private ResourceGroupsService service;+ private AccountAwareStorageBackend<ResourceGroup> groups;+ private Ec2Service ec2;+ private Clock clock;++ @BeforeEach+ void setUp() {+ ec2 = mock(Ec2Service.class);+ clock = mock(Clock.class);+ when(clock.millis()).thenReturn(1000L);+ when(ec2.describeCapacityReservations(anyString(), anyList(), anyMap())).thenAnswer(invocation -> {+ List<String> ids = invocation.getArgument(1);+ CapacityReservation reservation = reservations.get(ids.getFirst());+ if (reservation == null) {+ throw new AwsException("InvalidCapacityReservationId.NotFound", "missing", 400);+ }+ return List.of(reservation);+ });+ groups = AccountAwareStorageBackend.inMemory(ACCOUNT);+ service = new ResourceGroupsService(groups, ec2, clock);+ }++ private JsonNode json(Object value) {+ return mapper.valueToTree(value);+ }++ private JsonNode createRequest(String name) throws Exception {+ return mapper.readTree("{\"Name\":\"" + name + "\",\"Configuration\":" + CONFIGURATION+ + ",\"Description\":\"capacity pool\",\"Tags\":{\"purpose\":\"test\"}}");+ }++ private ResourceGroup create(String name) throws Exception {+ return service.create(REGION, ACCOUNT, createRequest(name));+ }++ private String reservation(int number) {+ String id = "cr-" + String.format("%017x", number);+ CapacityReservation reservation = new CapacityReservation();+ reservation.setCapacityReservationId(id);+ reservation.setCapacityReservationArn(+ AwsArnUtils.Arn.of("ec2", REGION, ACCOUNT, "capacity-reservation/" + id).toString());+ reservation.setOwnerId(ACCOUNT);+ reservation.setState("active");+ reservations.put(id, reservation);+ return reservation.getCapacityReservationArn();+ }++ private Map<String, Object> add(String group, String... arns) {+ return service.add(REGION, ACCOUNT, json(Map.of("Group", group, "ResourceArns", List.of(arns))));+ }++ private Map<String, Object> list(String group) {+ return service.list(REGION, ACCOUNT, json(Map.of("Group", group)));+ }++ @Test+ void storesConfigurationTagsAndDeprecatedNameSelector() throws Exception {+ ResourceGroup group = create("pool");+ assertEquals("arn:aws:resource-groups:eu-west-1:111122223333:group/pool", group.arn());+ assertEquals("capacity pool", group.description());+ assertEquals(Map.of("purpose", "test"), group.tags());+ assertEquals(2, group.configuration().size());+ assertEquals(group, service.get(REGION, ACCOUNT, json(Map.of("GroupName", "pool"))));+ assertEquals(group, mapper.readValue(mapper.writeValueAsBytes(group), ResourceGroup.class));+ assertThrows(AwsException.class, () -> create("pool"));+ }++ @Test+ void realMembershipIsIdempotentAndDeleteLeavesReservations() throws Exception {+ ResourceGroup group = create("pool");+ String arn = reservation(1);+ Map<String, Object> result = add(group.arn(), arn, arn);+ assertEquals(List.of(arn), result.get("Succeeded"));+ assertEquals(List.of(), result.get("Failed"));+ assertEquals(List.of(), result.get("Pending"));+ assertEquals(result, add("pool", arn));+ Map<String, String> identifier = Map.of("ResourceArn", arn, "ResourceType", ResourceGroupsService.RESOURCE_TYPE);+ assertEquals(List.of(identifier), list("pool").get("ResourceIdentifiers"));+ assertEquals(List.of(Map.of("Identifier", identifier)), list("pool").get("Resources"));+ service.delete(REGION, ACCOUNT, json(Map.of("GroupName", "pool")));+ assertEquals("NotFoundException", assertThrows(AwsException.class,+ () -> list("pool")).getErrorCode());+ assertEquals(1, reservations.size());+ create("pool");+ assertEquals(List.of(), list("pool").get("ResourceIdentifiers"));+ }++ @ParameterizedTest+ @ValueSource(strings = {"us-gov-west-1", "cn-north-1", "us-iso-east-1", "eusc-de-east-1"})+ void derivesPartitionFromActualRegion(String region) throws Exception {+ ResourceGroup group = service.create(region, ACCOUNT, createRequest("pool"));+ assertEquals(AwsArnUtils.Arn.of("resource-groups", region, ACCOUNT, "group/pool").toString(), group.arn());+ assertEquals(group, service.get(region, ACCOUNT, json(Map.of("Group", "pool"))));+ if (!region.startsWith("eusc-")) {+ assertEquals(group, service.get(region, ACCOUNT, json(Map.of("Group", group.arn()))));+ }+ }++ @Test+ void accountRegionAndFullArnSelectorsAreIsolated() throws Exception {+ ResourceGroup group = create("pool");+ assertThrows(AwsException.class, () -> service.get(REGION, "444455556666", json(Map.of("Group", "pool"))));+ assertThrows(AwsException.class, () -> service.get("us-east-1", ACCOUNT, json(Map.of("Group", "pool"))));+ assertThrows(AwsException.class,+ () -> service.get(REGION, "444455556666", json(Map.of("Group", group.arn()))));+ assertThrows(AwsException.class,+ () -> service.get("us-east-1", ACCOUNT, json(Map.of("Group", group.arn()))));+ ResourceGroup other = service.create(REGION, "444455556666", createRequest("pool"));+ assertNotEquals(group.arn(), other.arn());+ }++ @Test+ void missingForeignAndInactiveReservationsReturnFailedWithoutMembership() throws Exception {+ create("pool");+ String active = reservation(1);+ String cancelled = reservation(2);+ reservations.get("cr-00000000000000002").setState("cancelled");+ String absent = active.replace("00000000000000001", "00000000000000003");+ String foreign = active.replace(ACCOUNT, "444455556666");+ Map<String, Object> result = add("pool", active, cancelled, absent, foreign);+ assertEquals(List.of(active), result.get("Succeeded"));+ List<?> failed = (List<?>) result.get("Failed");+ assertEquals(3, failed.size());+ assertTrue(failed.stream().allMatch(item -> ((Map<?, ?>) item).get("ErrorCode")+ .equals("ResourceGroupsValidationException")));+ assertEquals(1, ((List<?>) list("pool").get("ResourceIdentifiers")).size());+ }++ @ParameterizedTest+ @CsvSource({+ "arn:aws:ec2:us-east-1:111122223333:capacity-reservation/cr-00000000000000001,ValidationException",+ "arn:aws:ec2:eu-west-1:111122223333:instance/i-00000000000000001,ResourceArnValidationException",+ "arn:aws:lambda:eu-west-1:111122223333:function:missing,ResourceArnValidationException",+ "arn:aws:ec2:eu-west-1:111122223333:capacity-reservation/cr-invalid,ResourceGroupsValidationException"+ })+ void unsupportedReservationIdentityUsesTheAwsPerResourceFailure(String arn, String code) throws Exception {+ create("pool");+ Map<String, Object> result = add("pool", arn);+ List<?> failed = (List<?>) result.get("Failed");+ assertEquals(1, failed.size());+ assertEquals(code, ((Map<?, ?>) failed.getFirst()).get("ErrorCode"));+ assertEquals(List.of(), result.get("Succeeded"));+ assertEquals(List.of(), list("pool").get("ResourceIdentifiers"));+ }++ @Test+ void unexpectedLookupOrForeignRecordedOwnerDoesNotCommitEarlierSuccess() throws Exception {+ create("pool");+ String first = reservation(1);+ String second = reservation(2);+ reservations.get("cr-00000000000000002").setOwnerId("444455556666");+ assertThrows(AwsException.class, () -> add("pool", first, second));+ assertEquals(List.of(), list("pool").get("ResourceIdentifiers"));+ when(ec2.describeCapacityReservations(eq(REGION), eq(List.of("cr-00000000000000002")), anyMap()))+ .thenThrow(new AwsException("ServiceUnavailable", "not ready", 503));+ assertEquals(503, assertThrows(AwsException.class, () -> add("pool", first, second)).getHttpStatus());+ assertEquals(List.of(), list("pool").get("ResourceIdentifiers"));+ }++ @ParameterizedTest+ @ValueSource(strings = {+ "{}", "[]", "{\"Name\":\"pool\"}", "{\"Name\":\"pool\",\"Configuration\":[]}",+ "{\"Name\":\"pool\",\"Configuration\":[{\"Type\":\"AWS::EC2::HostManagement\"}]}",+ "{\"Name\":\"pool\",\"ResourceQuery\":{}}"+ })+ void unsupportedOrMissingConfigurationFails(String body) throws Exception {+ assertThrows(AwsException.class, () -> service.create(REGION, ACCOUNT, mapper.readTree(body)));+ }++ @ParameterizedTest+ @ValueSource(strings = {"AWSreserved", "aWsreserved", "bad/name", "bad name", ""})+ void invalidNamesRefuse(String name) throws Exception {+ assertThrows(AwsException.class, () -> create(name));+ }++ @Test+ void publicSdkNameAndDescriptionBoundsArePreserved() throws Exception {+ JsonNode request = mapper.readTree("{\"Name\":\"" + "p".repeat(300) + "\",\"Description\":\""+ + "a".repeat(1024) + "\",\"Configuration\":" + CONFIGURATION + "}");+ assertEquals(300, service.create(REGION, ACCOUNT, request).name().length());+ assertThrows(AwsException.class, () -> create("p".repeat(301)));+ assertThrows(AwsException.class, () -> service.create(REGION, ACCOUNT,+ mapper.readTree(request.toString().replace("a".repeat(1024), "a".repeat(1025)))));+ assertThrows(AwsException.class, () -> service.create(REGION, ACCOUNT,+ mapper.readTree(request.toString().replace("a".repeat(1024), "bad/description"))));+ }++ @Test+ void malformedConfigurationParametersAndMixedQueryRefuse() throws Exception {+ String valid = mapper.writeValueAsString(createRequest("pool"));+ for (String changed : List.of(+ valid.replace(ResourceGroupsService.RESOURCE_TYPE, "AWS::EC2::Instance"),+ valid.replace("allowed-resource-types", "unrecognized"),+ valid.replace("AWS::EC2::CapacityReservationPool", "AWS::ResourceGroups::Generic"),+ valid.replace("\"Name\":\"pool\"", "\"Name\":\"pool\",\"ResourceQuery\":{}"))) {+ assertThrows(AwsException.class, () -> service.create(REGION, ACCOUNT, mapper.readTree(changed)));+ }+ }++ @Test+ void invalidMembershipRequestsFailBeforeAnyChange() throws Exception {+ create("pool");+ String valid = reservation(1);+ assertThrows(AwsException.class, () -> add("pool"));+ assertThrows(AwsException.class, () -> add("pool", valid, "not-an-arn"));+ assertThrows(AwsException.class, () -> add("missing", valid));+ assertThrows(AwsException.class, () -> service.get(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "GroupName", "pool"))));+ assertThrows(AwsException.class, () -> service.list(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "MaxResults", true))));+ assertEquals(List.of(), list("pool").get("ResourceIdentifiers"));+ verifyNoInteractions(ec2);+ }++ @Test+ void ignoresUnknownFieldsAndChecksModeledTagsAndMetadata() throws Exception {+ JsonNode request = createRequest("pool");+ ((ObjectNode) request).put("Group", "ignored");+ ((ObjectNode) request).set("Tags", json(Map.of("équipe /", "a:b@c")));+ ResourceGroup group = service.create(REGION, ACCOUNT, request);+ assertEquals(Map.of("équipe /", "a:b@c"), group.tags());+ assertEquals("pool", group.name());+ for (String field : List.of("Owner", "DisplayName", "Criticality")) {+ JsonNode modeled = createRequest("metadata-" + field);+ ((ObjectNode) modeled).set(field,+ json(field.equals("Criticality") ? 7 : "valid / +"));+ assertEquals("Unsupported parameter for this operation",+ assertThrows(AwsException.class, () -> service.create(REGION, ACCOUNT, modeled)).getMessage());+ }+ for (Map<String, String> tags : List.of(Map.of("bad$", "value"), Map.of("key", "bad$"))) {+ JsonNode invalid = createRequest("bad-tags");+ ((ObjectNode) invalid).set("Tags", json(tags));+ assertThrows(AwsException.class, () -> service.create(REGION, ACCOUNT, invalid));+ }+ for (int value : List.of(0, 11)) {+ JsonNode invalid = createRequest("bad-criticality");+ ((ObjectNode) invalid).put("Criticality", value);+ assertThrows(AwsException.class, () -> service.create(REGION, ACCOUNT, invalid));+ }+ assertEquals("pool", service.get(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "Unknown", "ignored", "DisplayName", Map.of()))).name());+ }++ @Test+ void paginationSurvivesRemovalOfTheLastItemOnThePreviousPage() throws Exception {+ ResourceGroup group = create("pool");+ String first = reservation(1);+ String second = reservation(2);+ String third = reservation(3);+ add("pool", first, second, third);+ String token = (String) service.list(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "MaxResults", 2))).get("NextToken");+ groups.putForAccount(ACCOUNT, REGION + "::pool", group.withMembers(List.of(first, third)));+ Map<String, Object> page = service.list(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "NextToken", token)));+ assertEquals(List.of(Map.of("ResourceArn", third, "ResourceType", ResourceGroupsService.RESOURCE_TYPE)),+ page.get("ResourceIdentifiers"));+ assertFalse(page.containsKey("NextToken"));+ }++ @Test+ void paginationKeepsSnapshotScopeReplayAndGroupGeneration() throws Exception {+ create("pool");+ create("other");+ String first = reservation(1);+ String second = reservation(2);+ add("pool", first, second);+ Map<String, Object> firstPage = service.list(REGION, ACCOUNT, json(Map.of("Group", "pool", "MaxResults", 1)));+ String token = (String) firstPage.get("NextToken");+ assertTrue(token.matches("[a-zA-Z0-9+/]*={0,2}"));+ assertDoesNotThrow(() -> Base64.getDecoder().decode(token));+ JsonNode continued = json(Map.of("Group", "pool", "NextToken", token));+ add("pool", reservation(3));+ Map<String, Object> page = service.list(REGION, ACCOUNT, continued);+ assertEquals(List.of(Map.of("ResourceArn", second, "ResourceType", ResourceGroupsService.RESOURCE_TYPE)),+ page.get("ResourceIdentifiers"));+ assertEquals(page, service.list(REGION, ACCOUNT, continued));+ assertThrows(AwsException.class, () -> service.list(REGION, ACCOUNT,+ json(Map.of("Group", "other", "NextToken", token))));+ assertThrows(AwsException.class, () -> service.list(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "NextToken", token, "Filters",+ List.of(Map.of("Name", "resource-type", "Values", List.of(ResourceGroupsService.RESOURCE_TYPE)))))));+ service.delete(REGION, ACCOUNT, json(Map.of("Group", "pool")));+ create("pool");+ assertThrows(AwsException.class, () -> service.list(REGION, ACCOUNT, continued));+ }++ @Test+ void paginationHasBoundedExpiryAndAllowsFreshTraversal() throws Exception {+ create("pool");+ add("pool", reservation(1), reservation(2));+ JsonNode request = json(Map.of("Group", "pool", "MaxResults", 1));+ List<String> tokens = new ArrayList<>();+ for (int index = 0; index <= ResourceGroupsService.MAX_PAGES; index++) {+ tokens.add((String) service.list(REGION, ACCOUNT, request).get("NextToken"));+ }+ assertThrows(AwsException.class, () -> service.list(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "NextToken", tokens.getFirst()))));+ when(clock.millis()).thenReturn(ResourceGroupsService.PAGE_IDLE_MILLIS + 1001);+ assertThrows(AwsException.class, () -> service.list(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "NextToken", tokens.getLast()))));+ assertNotNull(service.list(REGION, ACCOUNT, request).get("NextToken"));+ }++ @Test+ void clearDropsGroupsAndTokens() throws Exception {+ create("pool");+ add("pool", reservation(1), reservation(2));+ String token = (String) service.list(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "MaxResults", 1))).get("NextToken");+ service.clear();+ assertThrows(AwsException.class, () -> list("pool"));+ create("pool");+ assertThrows(AwsException.class, () -> service.list(REGION, ACCOUNT,+ json(Map.of("Group", "pool", "NextToken", token))));+ }+}diff --git a/src/test/resources/application.yml b/src/test/resources/application.ymlindex 53fb554a35..d471094706 100644--- a/src/test/resources/application.yml+++ b/src/test/resources/application.yml@@ -72,6 +72,9 @@ floci:flush-interval-ms: 60000codedeploy:flush-interval-ms: 60000+ resource-groups:+ mode: ${FLOCI_STORAGE_SERVICES_RESOURCE_GROUPS_MODE:}+ flush-interval-ms: ${FLOCI_STORAGE_SERVICES_RESOURCE_GROUPS_FLUSH_INTERVAL_MS:5000}tagging:flush-interval-ms: 60000elasticbeanstalk:@@ -528,6 +531,8 @@ floci:enabled: truelakeformation:enabled: true+ resource-groups:+ enabled: truetagging:enabled: trueconfigservice:diff --git a/tools/aws/regen_resource_groups_shapes.py b/tools/aws/regen_resource_groups_shapes.pynew file mode 100644index 0000000000..71a3936b85--- /dev/null+++ b/tools/aws/regen_resource_groups_shapes.py@@ -0,0 +1,58 @@+#!/usr/bin/env python3+"""Vendor Resource Groups request constraints from botocore's Apache-2.0 service model."""+from __future__ import annotations++import argparse+import json+from pathlib import Path++from regen_partitions import resolve_botocore_data, strip_source+from request_shapes import REPO_ROOT, load_model++OUTPUT = REPO_ROOT / "src/main/resources/aws/resource-groups-request-shapes.json"+OPERATIONS = ("CreateGroup", "GetGroup", "DeleteGroup", "GroupResources", "ListGroupResources")+++def build(model: dict, provenance: str) -> dict:+ kept = {}++ def keep(name: str) -> None:+ if name in kept:+ return+ shape = model["shapes"][name]+ entry = {k: shape[k] for k in ("type", "required", "min", "max", "pattern", "enum") if k in shape}+ kept[name] = entry+ if shape["type"] == "structure":+ entry["members"] = {k: v["shape"] for k, v in shape.get("members", {}).items()}+ for child in entry["members"].values():+ keep(child)+ elif shape["type"] == "list":+ entry["member"] = shape["member"]["shape"]+ keep(entry["member"])+ elif shape["type"] == "map":+ for side in ("key", "value"):+ entry[side] = shape[side]["shape"]+ keep(entry[side])++ operations = {name: model["operations"][name]["input"]["shape"] for name in OPERATIONS}+ for name in operations.values():+ keep(name)+ return {"_source": {"generator": "tools/aws/regen_resource_groups_shapes.py", "botocore": provenance},+ "operations": operations, "shapes": dict(sorted(kept.items()))}+++def main() -> int:+ parser = argparse.ArgumentParser(description=__doc__)+ parser.add_argument("--check", action="store_true")+ parser.add_argument("--botocore-data", type=Path)+ args = parser.parse_args()+ data, provenance = resolve_botocore_data(args.botocore_data)+ fresh = json.dumps(build(load_model(data, Path("resource-groups/2017-11-27")), provenance), indent=2) + "\n"+ if args.check:+ return int(not OUTPUT.exists() or strip_source(OUTPUT.read_text()) != strip_source(fresh))+ OUTPUT.write_text(fresh)+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/tools/docs/services.yaml b/tools/docs/services.yamlindex 5e7712d247..81ad712add 100644--- a/tools/docs/services.yaml+++ b/tools/docs/services.yaml@@ -283,6 +283,10 @@ services:- { path: src/main/java/io/github/hectorvent/floci/services/securityadmin/SecurityAdminController.java, mode: rest }rename_actions:ListAdmin: ListOrganizationAdminAccounts+ - service: resource-groups+ doc: docs/services/resource-groups.md+ sources:+ - { path: src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsController.java, mode: rest }- service: inspector2doc: docs/services/inspector2.mdsources: