Light, fluffy, and always free - The AWS Local Emulator alternative
feat(autoscaling): implement DescribeTags (#5750)
Allen Sanborn05153b30be80
diff --git a/docs/services/autoscaling.md b/docs/services/autoscaling.mdindex c2c0fd3956..1711ecd916 100644--- a/docs/services/autoscaling.md+++ b/docs/services/autoscaling.md@@ -28,6 +28,7 @@ Floci implements the EC2 Auto Scaling API — stored-state management for launch| `DescribeAutoScalingGroups` | Filtered by name list; returns all if no filter; includes current instance list with lifecycle state and mixed instances policy shape when configured || `UpdateAutoScalingGroup` | Updates capacity bounds, cooldown, launch source, AZs || `DeleteAutoScalingGroup` | `ForceDelete=true` terminates all instances before deletion |+| `DescribeTags` | Group tags; filters `auto-scaling-group`, `key`, `value`, `propagate-at-launch` (values ORed, filters ANDed); `MaxRecords`/`NextToken` paging || `EnableMetricsCollection` | Records the named group metrics, or all 20 when none are named, as `EnabledMetrics` on `DescribeAutoScalingGroups`. `Granularity` is required and must be `1Minute`; an unknown metric name raises `ValidationError`. No metrics are published to CloudWatch || `DisableMetricsCollection` | Removes the named metrics from `EnabledMetrics`, or all of them when none are named |diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.javaindex b77cc68471..3458ddbbd2 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java@@ -152,7 +152,7 @@ public class AwsQueryController {"CreateAutoScalingGroup", "UpdateAutoScalingGroup", "DeleteAutoScalingGroup","DescribeAutoScalingGroups", "SetDesiredCapacity","StartInstanceRefresh", "DescribeInstanceRefreshes",- "CreateOrUpdateTags", "DeleteTags",+ "CreateOrUpdateTags", "DeleteTags", "DescribeTags","DescribeAutoScalingInstances", "AttachInstances", "DetachInstances","TerminateInstanceInAutoScalingGroup","AttachLoadBalancerTargetGroups", "DetachLoadBalancerTargetGroups",diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.javaindex 542a565067..31c41d98d1 100644--- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingQueryHandler.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.autoscaling;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.AwsNamespaces;import io.github.hectorvent.floci.core.common.AwsQueryResponse;+import io.github.hectorvent.floci.core.common.PaginatedResult;import io.github.hectorvent.floci.core.common.XmlBuilder;import io.github.hectorvent.floci.services.autoscaling.model.*;import jakarta.enterprise.context.ApplicationScoped;@@ -55,6 +56,7 @@ public class AutoScalingQueryHandler {case "DescribeInstanceRefreshes" -> handleDescribeInstanceRefreshes(p, region);case "CreateOrUpdateTags" -> handleCreateOrUpdateTags(p, region);case "DeleteTags" -> handleDeleteTags(p, region);+ case "DescribeTags" -> handleDescribeTags(p, region);case "EnableMetricsCollection" -> handleEnableMetricsCollection(p, region);case "DisableMetricsCollection" -> handleDisableMetricsCollection(p, region);// Instances@@ -491,6 +493,47 @@ public class AutoScalingQueryHandler {.end("CreateOrUpdateTagsResponse").build());}+ private Response handleDescribeTags(MultivaluedMap<String, String> p, String region) {+ List<Map.Entry<String, List<String>>> filters = new ArrayList<>();+ for (int i = 1; ; i++) {+ String name = p.getFirst("Filters.member." + i + ".Name");+ if (name == null) { break; }+ filters.add(Map.entry(name, memberList(p, "Filters.member." + i + ".Values")));+ }+ String maxRecords = p.getFirst("MaxRecords");+ Integer limit = null;+ if (maxRecords != null && !maxRecords.isBlank()) {+ try {+ limit = Integer.parseInt(maxRecords);+ } catch (NumberFormatException e) {+ throw new AwsException("ValidationError", "MaxRecords '" + maxRecords + "' is not a valid integer.", 400);+ }+ }+ PaginatedResult<AutoScalingService.TagDescription> page = service.describeTags(region, filters,+ limit, p.getFirst("NextToken"));+ XmlBuilder xml = new XmlBuilder()+ .start("DescribeTagsResponse", NS)+ .start("DescribeTagsResult")+ .start("Tags");+ for (AutoScalingService.TagDescription tag : page.items()) {+ xml.start("member")+ .elem("ResourceId", tag.resourceId())+ .elem("ResourceType", "auto-scaling-group")+ .elem("Key", tag.key())+ .elem("Value", tag.value())+ .elem("PropagateAtLaunch", String.valueOf(tag.propagateAtLaunch()))+ .end("member");+ }+ xml.end("Tags");+ if (page.nextToken() != null) {+ xml.elem("NextToken", page.nextToken());+ }+ xml.end("DescribeTagsResult")+ .raw(AwsQueryResponse.responseMetadata())+ .end("DescribeTagsResponse");+ return ok(xml.build());+ }+private Response handleEnableMetricsCollection(MultivaluedMap<String, String> p, String region) {service.enableMetricsCollection(region, p.getFirst("AutoScalingGroupName"),memberList(p, "Metrics"), p.getFirst("Granularity"));diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.javaindex cf3957164e..72d8c8cccb 100644--- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java+++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingService.java@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.autoscaling;import com.fasterxml.jackson.core.type.TypeReference;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.PaginatedResult;+import io.github.hectorvent.floci.core.common.Pagination;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.core.storage.StorageBackedMap;@@ -558,6 +560,44 @@ public class AutoScalingService {groups.put(asgKey(region, asg.getAutoScalingGroupName()), asg);}+ // AWS: values inside one filter are ORed, separate filters are ANDed; only these four+ // filter names exist. Ordered by (group, key); NextToken is the last pair returned, so tags+ // added or removed between pages neither skip nor repeat an entry. Values match exactly+ // (the Filter model says they are case-sensitive).+ public PaginatedResult<TagDescription> describeTags(String region, List<Map.Entry<String, List<String>>> filters,+ Integer maxRecords, String nextToken) {+ for (Map.Entry<String, List<String>> filter : filters) {+ if (!Set.of("auto-scaling-group", "key", "value", "propagate-at-launch").contains(filter.getKey())) {+ throw new AwsException("ValidationError", "Filter name '" + filter.getKey() + "' is not valid.", 400);+ }+ }+ int limit = maxRecords != null ? Math.min(Math.max(maxRecords, 1), 100) : 50;+ List<TagDescription> matches = groups.values().stream()+ .filter(Objects::nonNull) // group deleted between key snapshot and value read+ .filter(g -> region.equals(g.getRegion()))+ .flatMap(g -> g.getTags().entrySet().stream()+ .map(t -> new TagDescription(g.getAutoScalingGroupName(), t.getKey(), t.getValue(),+ g.getTagPropagateAtLaunch().getOrDefault(t.getKey(), false))))+ .filter(t -> matchesFilter(filters, "auto-scaling-group", t.resourceId())+ && matchesFilter(filters, "key", t.key())+ && matchesFilter(filters, "value", t.value())+ && matchesFilter(filters, "propagate-at-launch", String.valueOf(t.propagateAtLaunch())))+ .collect(Collectors.toList());+ // "\0" sorts below every other character, so the cursor orders as the (group, key) pair.+ return Pagination.paginate(matches,+ t -> t.resourceId() + "\0" + t.key(), limit, nextToken, "autoscaling-tags",+ token -> new AwsException("InvalidNextToken", "The NextToken value is not valid.", 400));+ }++ // Every filter of this name must match (a repeated name is a second filter, ANDed).+ private static boolean matchesFilter(List<Map.Entry<String, List<String>>> filters, String name, String actual) {+ return filters.stream()+ .filter(f -> f.getKey().equals(name) && !f.getValue().isEmpty())+ .allMatch(f -> f.getValue().contains(actual));+ }++ public record TagDescription(String resourceId, String key, String value, boolean propagateAtLaunch) {}+private AutoScalingGroup requireTaggableGroup(String region, String resourceId, String resourceType) {if (!"auto-scaling-group".equals(resourceType)) {throw new AwsException("ValidationError",diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingDescribeTagsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingDescribeTagsIntegrationTest.javanew file mode 100644index 0000000000..be41a89b00--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingDescribeTagsIntegrationTest.java@@ -0,0 +1,301 @@+package io.github.hectorvent.floci.services.autoscaling;++import io.quarkus.test.junit.QuarkusTest;+import io.restassured.path.xml.XmlPath;+import io.restassured.specification.RequestSpecification;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.nio.charset.StandardCharsets;+import java.util.Base64;+import java.util.List;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNotNull;++/**+ * DescribeTags over the tags CreateAutoScalingGroup and CreateOrUpdateTags store. Tags are read+ * back through DescribeTags itself, never trusted from the mutating call's response.+ */+@QuarkusTest+class AutoScalingDescribeTagsIntegrationTest {++ private static final String AUTH =+ "AWS4-HMAC-SHA256 Credential=test/20260501/us-east-1/autoscaling/aws4_request";+ private static final String ROOT = "DescribeTagsResponse.DescribeTagsResult.Tags.member";++ private static final String GROUP_A = "describe-tags-a";+ private static final String GROUP_B = "describe-tags-b";+ private static final String LC = "describe-tags-lc";++ private static boolean created;++ // Not @BeforeAll: a static hook runs before the Quarkus test server is listening.+ @BeforeEach+ void createGroups() {+ if (created) { return; }+ created = true;+ given()+ .formParam("Action", "CreateLaunchConfiguration")+ .formParam("LaunchConfigurationName", LC)+ .formParam("ImageId", "ami-12345678")+ .formParam("InstanceType", "t3.micro")+ .header("Authorization", AUTH)+ .when().post("/")+ .then().statusCode(200);+ createGroup(GROUP_A, "AsgId", "1", true);+ createGroup(GROUP_B, "AsgId", "2", false);+ given()+ .formParam("Action", "CreateOrUpdateTags")+ .formParam("Tags.member.1.ResourceId", GROUP_A)+ .formParam("Tags.member.1.ResourceType", "auto-scaling-group")+ .formParam("Tags.member.1.Key", "team")+ .formParam("Tags.member.1.Value", "blue")+ .formParam("Tags.member.1.PropagateAtLaunch", "false")+ .header("Authorization", AUTH)+ .when().post("/")+ .then().statusCode(200);+ }++ // The corpus call: terraform-aws-asg's get-desired-capacity.py finds its group by+ // describe_tags(Filters=[{Name: key, Values: [AsgId]}, {Name: value, Values: [<id>]}]).+ @Test+ void keyAndValueFiltersFindTheOneGroup() {+ XmlPath xml = describe()+ .formParam("Filters.member.1.Name", "key")+ .formParam("Filters.member.1.Values.member.1", "AsgId")+ .formParam("Filters.member.2.Name", "value")+ .formParam("Filters.member.2.Values.member.1", "1")+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();++ assertEquals(List.of(GROUP_A), xml.getList(ROOT + ".ResourceId"));+ assertEquals("auto-scaling-group", xml.getString(ROOT + ".ResourceType"));+ assertEquals("AsgId", xml.getString(ROOT + ".Key"));+ assertEquals("1", xml.getString(ROOT + ".Value"));+ assertEquals("true", xml.getString(ROOT + ".PropagateAtLaunch"));+ }++ @Test+ void autoScalingGroupFilterReturnsEveryTagOfThatGroupSortedByKey() {+ XmlPath xml = describe()+ .formParam("Filters.member.1.Name", "auto-scaling-group")+ .formParam("Filters.member.1.Values.member.1", GROUP_A)+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();++ assertEquals(List.of("AsgId", "team"), xml.getList(ROOT + ".Key"));+ assertEquals(List.of("true", "false"), xml.getList(ROOT + ".PropagateAtLaunch"));+ }++ // Values inside one filter are ORed; separate filters are ANDed.+ @Test+ void valuesWithinAFilterAreOredAndPropagateAtLaunchFilters() {+ XmlPath xml = describe()+ .formParam("Filters.member.1.Name", "auto-scaling-group")+ .formParam("Filters.member.1.Values.member.1", GROUP_A)+ .formParam("Filters.member.1.Values.member.2", GROUP_B)+ .formParam("Filters.member.2.Name", "propagate-at-launch")+ .formParam("Filters.member.2.Values.member.1", "false")+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();++ assertEquals(List.of(GROUP_A, GROUP_B), xml.getList(ROOT + ".ResourceId"));+ assertEquals(List.of("team", "AsgId"), xml.getList(ROOT + ".Key"));+ }++ // Two filters with the same name are still two filters: ANDed, not merged into one ORed list.+ @Test+ void repeatedFilterNamesAreAndedNotMerged() {+ XmlPath xml = describe()+ .formParam("Filters.member.1.Name", "key")+ .formParam("Filters.member.1.Values.member.1", "AsgId")+ .formParam("Filters.member.1.Values.member.2", "team")+ .formParam("Filters.member.2.Name", "key")+ .formParam("Filters.member.2.Values.member.1", "team")+ .formParam("Filters.member.3.Name", "auto-scaling-group")+ .formParam("Filters.member.3.Values.member.1", GROUP_A)+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();++ assertEquals(List.of("team"), xml.getList(ROOT + ".Key"));+ }++ @Test+ void maxRecordsPagesWithNextToken() {+ XmlPath first = describe()+ .formParam("Filters.member.1.Name", "auto-scaling-group")+ .formParam("Filters.member.1.Values.member.1", GROUP_A)+ .formParam("Filters.member.1.Values.member.2", GROUP_B)+ .formParam("MaxRecords", "2")+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();+ assertEquals(2, first.getList(ROOT + ".Key").size());+ String token = first.getString("DescribeTagsResponse.DescribeTagsResult.NextToken");+ assertNotNull(token);++ XmlPath second = describe()+ .formParam("Filters.member.1.Name", "auto-scaling-group")+ .formParam("Filters.member.1.Values.member.1", GROUP_A)+ .formParam("Filters.member.1.Values.member.2", GROUP_B)+ .formParam("MaxRecords", "2")+ .formParam("NextToken", token)+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();+ assertEquals(List.of(GROUP_B), second.getList(ROOT + ".ResourceId"));+ assertEquals("", second.getString("DescribeTagsResponse.DescribeTagsResult.NextToken"));+ }++ @Test+ void unknownFilterNameIsAValidationError() {+ describe()+ .formParam("Filters.member.1.Name", "tag-key")+ .formParam("Filters.member.1.Values.member.1", "AsgId")+ .when().post("/")+ .then().statusCode(400)+ .body(containsString("ValidationError"));+ }++ @Test+ void nonNumericMaxRecordsIsAValidationError() {+ describe()+ .formParam("MaxRecords", "abc")+ .when().post("/")+ .then().statusCode(400)+ .body(containsString("ValidationError"));+ }++ // Like every other member list in this handler, filters are read up to the first missing index.+ @Test+ void filtersAreReadUpToTheFirstMissingIndex() {+ XmlPath xml = describe()+ .formParam("Filters.member.1.Name", "auto-scaling-group")+ .formParam("Filters.member.1.Values.member.1", GROUP_B)+ .formParam("Filters.member.3.Name", "key")+ .formParam("Filters.member.3.Values.member.1", "no-such-key")+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();++ assertEquals(List.of("AsgId"), xml.getList(ROOT + ".Key"));+ }++ // Filter values are case-sensitive (AWS Filter model), propagate-at-launch included.+ @Test+ void propagateAtLaunchFilterIsCaseSensitive() {+ XmlPath xml = describe()+ .formParam("Filters.member.1.Name", "auto-scaling-group")+ .formParam("Filters.member.1.Values.member.1", GROUP_A)+ .formParam("Filters.member.2.Name", "propagate-at-launch")+ .formParam("Filters.member.2.Values.member.1", "True")+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();++ assertEquals(List.of(), xml.getList(ROOT + ".Key"));+ }++ // A tag deleted from an already-returned page must not shift the next page past an unread tag.+ @Test+ void nextTokenSurvivesATagDeletedBeforeTheCursor() {+ assertSecondPageAfterDeleting("describe-tags-cursor", "k1");+ }++ // The exact tag the NextToken encodes is deleted: the next page resumes at the first greater+ // key, rather than failing or restarting the listing.+ @Test+ void nextTokenSurvivesDeletingTheCursorTagItself() {+ assertSecondPageAfterDeleting("describe-tags-cursor-gone", "k2");+ }++ // Group has tags k1, k2, k3; the first page (MaxRecords=2) returns k1, k2, so the token encodes k2.+ private static void assertSecondPageAfterDeleting(String group, String deletedKey) {+ createGroup(group, "k1", "v", false);+ given()+ .formParam("Action", "CreateOrUpdateTags")+ .formParam("Tags.member.1.ResourceId", group)+ .formParam("Tags.member.1.ResourceType", "auto-scaling-group")+ .formParam("Tags.member.1.Key", "k2")+ .formParam("Tags.member.1.Value", "v")+ .formParam("Tags.member.2.ResourceId", group)+ .formParam("Tags.member.2.ResourceType", "auto-scaling-group")+ .formParam("Tags.member.2.Key", "k3")+ .formParam("Tags.member.2.Value", "v")+ .header("Authorization", AUTH)+ .when().post("/")+ .then().statusCode(200);++ XmlPath first = describe()+ .formParam("Filters.member.1.Name", "auto-scaling-group")+ .formParam("Filters.member.1.Values.member.1", group)+ .formParam("MaxRecords", "2")+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();+ assertEquals(List.of("k1", "k2"), first.getList(ROOT + ".Key"));+ String token = first.getString("DescribeTagsResponse.DescribeTagsResult.NextToken");++ given()+ .formParam("Action", "DeleteTags")+ .formParam("Tags.member.1.ResourceId", group)+ .formParam("Tags.member.1.ResourceType", "auto-scaling-group")+ .formParam("Tags.member.1.Key", deletedKey)+ .header("Authorization", AUTH)+ .when().post("/")+ .then().statusCode(200);++ XmlPath second = describe()+ .formParam("Filters.member.1.Name", "auto-scaling-group")+ .formParam("Filters.member.1.Values.member.1", group)+ .formParam("MaxRecords", "2")+ .formParam("NextToken", token)+ .when().post("/")+ .then().statusCode(200).extract().xmlPath();+ assertEquals(List.of("k3"), second.getList(ROOT + ".Key"));+ }++ @Test+ void garbageNextTokenIsInvalidNextToken() {+ describe()+ .formParam("NextToken", "!!not-a-token!!")+ .when().post("/")+ .then().statusCode(400)+ .body(containsString("InvalidNextToken"));+ }++ // A token that decodes to a plausible (group, key) but lacks this list's namespace was not+ // minted here.+ @Test+ void tokenWithoutTheNamespaceIsInvalidNextToken() {+ assertInvalidNextToken(Base64.getUrlEncoder().encodeToString(+ (GROUP_A + "\0AsgId").getBytes(StandardCharsets.UTF_8)));+ }++ private static void assertInvalidNextToken(String token) {+ describe()+ .formParam("NextToken", token)+ .when().post("/")+ .then().statusCode(400)+ .body(containsString("InvalidNextToken"));+ }++ private static RequestSpecification describe() {+ return given().formParam("Action", "DescribeTags").header("Authorization", AUTH);+ }++ private static void createGroup(String name, String key, String value, boolean propagate) {+ given()+ .formParam("Action", "CreateAutoScalingGroup")+ .formParam("AutoScalingGroupName", name)+ .formParam("LaunchConfigurationName", LC)+ .formParam("MinSize", "0")+ .formParam("MaxSize", "1")+ .formParam("DesiredCapacity", "0")+ .formParam("AvailabilityZones.member.1", "us-east-1a")+ .formParam("Tags.member.1.Key", key)+ .formParam("Tags.member.1.Value", value)+ .formParam("Tags.member.1.PropagateAtLaunch", String.valueOf(propagate))+ .header("Authorization", AUTH)+ .when().post("/")+ .then().statusCode(200);+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.javaindex 9db721db53..32626b90cb 100644--- a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingServiceTest.java@@ -929,11 +929,14 @@ class AutoScalingServiceTest {groups.putAll(service.groups);service.groups = groups;service.attachInstances(REGION, "test-asg", List.of("i-live"));+ service.createOrUpdateTags(REGION, "test-asg", "auto-scaling-group", Map.of("k", "v"), Map.of());assertFalse(service.describeAutoScalingGroups(null, null).contains(null));assertEquals(List.of("test-asg"), service.describeAutoScalingGroups(REGION, null).stream().map(AutoScalingGroup::getAutoScalingGroupName).toList());assertEquals(1, service.describeAutoScalingInstances(REGION, null).size());+ assertEquals(List.of("k"), service.describeTags(REGION, List.of(), null, null).items().stream()+ .map(AutoScalingService.TagDescription::key).toList());service.setInstanceHealth(REGION, "i-live", "Unhealthy", false);service.terminateInstanceInAutoScalingGroup(REGION, "i-live", false);