mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(autoscaling): implement DescribeTags (#5750)
* feat(autoscaling): implement DescribeTags DescribeTags returned UnsupportedOperation while CreateOrUpdateTags and DeleteTags already stored group tags. Serve it from those stored tags: filters auto-scaling-group, key, value and propagate-at-launch (values within a filter ORed, filters ANDed, unknown filter names rejected with ValidationError), MaxRecords/NextToken paging reusing the offset token DescribeInstanceRefreshes uses, results ordered by group then key. * fix(autoscaling): reject non-numeric MaxRecords and sparse filter indices in DescribeTags * fix(autoscaling): harden DescribeTags paging, vanished groups and oversized indices - rejectIndexGap compares member indices as BigInteger, so an index past int range is a 400 ValidationError instead of a NumberFormatException 500. - describeTags skips a null group value (deleted between StorageBackedMap's key snapshot and value read). - NextToken is now an opaque cursor on the last (group, key) returned, not an offset, so tags added or removed between pages are neither skipped nor repeated. - Import hamcrest containsString in the integration test. * fix(autoscaling): sign DescribeTags NextToken and route DescribeTags by Version A token that decodes to a (group, key) it was never issued for is now refused with InvalidNextToken, as AWS refuses a token it did not mint: the cursor carries an HMAC-SHA256 over region and cursor under a per-process key. DescribeTags joins the autoscaling action set so a request without a credential scope reaches autoscaling under its 2011-01-01 Version instead of ec2. * fix(autoscaling): bind DescribeTags NextToken to the account; per-instance HMAC key A static SecureRandom key can be initialized at native-image build time and baked into every process; hold it on the ApplicationScoped bean instead. The MAC now covers the caller's account id, so a token issued under one account is InvalidNextToken under another. * fix(autoscaling): draw the DescribeTags cursor key with nextBytes, not generateSeed * fix(autoscaling): AND repeated DescribeTags filters of the same name Each Filters.member entry is now its own filter, so two 'key' filters must both match instead of being merged into one ORed value list, per the Filter shape: multiple filters are joined with AND. Also cover a NextToken whose own cursor tag is deleted between pages. * fix(autoscaling): page DescribeTags through core Pagination, read filters to first gap Per review: the NextToken is now a (group, key) cursor through core.common.Pagination with a namespace and InvalidNextToken, replacing the HMAC, per-instance key and account binding (groups are stored per account, and the per-instance key voided every token on restart). rejectIndexGap is gone; filters are read up to the first missing index like every other member list in the handler. propagate-at-launch values match exactly, as the Filter model says values are case-sensitive.
This commit is contained in:
1 parent
cbd2f61dc4
commit
05153b30be
6 files changed
+389
-1
No files matched your search
@@ -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",
|
||||
|
||||
+43
@@ -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"));
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user