mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(cloudformation): honour the DescribeStackResources filters (#5455)
* fix(cloudformation): honour the DescribeStackResources filters Closes #5451 DescribeStackResources read only StackName, so LogicalResourceId and PhysicalResourceId were accepted and ignored and every call answered with the whole stack. That is quiet rather than loud: a caller that filters and reads the first member gets whichever resource the stack happens to list first, with nothing in the response saying the filter was dropped. Serverless Framework resolves an AppSync API id that way after a deploy, so it ended up asking AppSync for an API whose id was a Lambda function name. Both filters now apply. Naming a LogicalResourceId the stack does not have is a ValidationError rather than an empty list, so the miss cannot be read as "the stack has no resources". A PhysicalResourceId with no StackName resolves the stack that owns the resource, which is the other form AWS accepts, and a call with neither is the ValidationError AWS answers. The response also stamps the resolved stack's own name on each member rather than echoing what the caller passed, which was an ARN when the caller used one and null for the PhysicalResourceId-only form. * fix(cloudformation): correct the DescribeStackResources physical-id form Review on #5455, all against the API reference. A PhysicalResourceId names the stack rather than narrowing it: "the associated resources of the stack that the resource belongs to are returned". It no longer filters the result down to the one resource. Specifying both StackName and PhysicalResourceId is a ValidationError, which the reference states outright and this did not enforce. stackContaining walked listStacks directly, which answers oldest first and keeps deleted stacks for their retention window, so a resource whose stack had been redeployed resolved to the stack that used to own it. Live stacks now come first, newest first within each group. An unmatched LogicalResourceId is an empty result rather than a ValidationError. The reference calls it a filter on the result and documents no error for a miss; DescribeStackResource, the singular action where the resource is required rather than optional, is the one that raises. The error here was my inference, not an observed AWS response. * docs(cloudformation): correct the DescribeStackResources Javadoc and pin the redeploy test Review on #5455. The four-argument describeStackResources still documented an unmatched LogicalResourceId as a ValidationError, which is what the previous commit removed. It now says what the code does: a physical id answers with the whole stack, a logical id is only a filter, and DescribeStackResource is the singular action that raises for a miss. aRedeployedPhysicalIdResolvesToTheLiveStackNotTheDeletedOne created the second stack straight after DeleteStack, which answers before the deletion runs. The old stack could still be DELETE_IN_PROGRESS, sorting it with the live stacks, so the test could pass on creation time alone without ever reaching the deleted branch it is named for. Awaiting the deletion makes it compare against a DELETE_COMPLETE stack every time, and it still fails on a straight walk of listStacks.
This commit is contained in:
1 parent
5092713a6e
commit
c5feab635a
3 files changed
+272
-4
No files matched your search
+5
-2
@@ -396,8 +396,11 @@ public class CloudFormationQueryHandler {
|
||||
private Response describeStackResources(MultivaluedMap<String, String> params, String region) {
|
||||
String stackName = params.getFirst("StackName");
|
||||
try {
|
||||
List<StackResource> resources = cfnService.describeStackResources(stackName, region);
|
||||
return stackResourcesXml(resources, stackName, region);
|
||||
CloudFormationService.StackResourcesView view = cfnService.describeStackResourcesView(stackName,
|
||||
params.getFirst("LogicalResourceId"), params.getFirst("PhysicalResourceId"), region);
|
||||
// The stack's own name, not what the caller passed: it may be an ARN, or absent
|
||||
// entirely when the call named only a PhysicalResourceId.
|
||||
return stackResourcesXml(view.resources(), view.stackName(), region);
|
||||
} catch (AwsException e) {
|
||||
return xmlError(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
|
||||
+77
-2
@@ -1282,8 +1282,83 @@ public class CloudFormationService implements ResourceProvider {
|
||||
// ── DescribeStackResources ────────────────────────────────────────────────
|
||||
|
||||
public List<StackResource> describeStackResources(String stackName, String region) {
|
||||
Stack stack = getStackOrThrow(stackName, region);
|
||||
return new ArrayList<>(stack.resourcesSnapshot().values());
|
||||
return describeStackResources(stackName, null, null, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* The stack's resources, narrowed by the two filters {@code DescribeStackResources} accepts.
|
||||
*
|
||||
* <p>AWS takes either {@code StackName} or {@code PhysicalResourceId}, never both: given a
|
||||
* physical id it answers with the whole stack that resource belongs to, rather than that one
|
||||
* resource. {@code LogicalResourceId} is only a filter on the result, so naming one the stack
|
||||
* does not have is an empty answer; {@code DescribeStackResource}, the singular action where
|
||||
* the resource is required rather than optional, is the one that raises for a miss.
|
||||
*
|
||||
* <p>Both filters being silently ignored is worse than it sounds for a caller that filters and
|
||||
* then reads the first member: it gets whichever resource the stack happens to list first.
|
||||
* Serverless Framework resolves an AppSync API id exactly that way.
|
||||
*/
|
||||
public List<StackResource> describeStackResources(String stackName, String logicalResourceId,
|
||||
String physicalResourceId, String region) {
|
||||
return describeStackResourcesView(stackName, logicalResourceId, physicalResourceId, region).resources();
|
||||
}
|
||||
|
||||
/** The stack a call resolved to and the resources it answered with; the stack's own name is
|
||||
* what AWS stamps on every member, and the caller may not have supplied it. */
|
||||
public record StackResourcesView(String stackName, List<StackResource> resources) {
|
||||
}
|
||||
|
||||
/** @see #describeStackResources(String, String, String, String) */
|
||||
public StackResourcesView describeStackResourcesView(String stackName, String logicalResourceId,
|
||||
String physicalResourceId, String region) {
|
||||
boolean hasStackName = stackName != null && !stackName.isBlank();
|
||||
boolean hasPhysicalId = physicalResourceId != null && !physicalResourceId.isBlank();
|
||||
if (!hasStackName && !hasPhysicalId) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Either StackName or PhysicalResourceId must be specified.", 400);
|
||||
}
|
||||
if (hasStackName && hasPhysicalId) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Cannot specify both StackName and PhysicalResourceId.", 400);
|
||||
}
|
||||
|
||||
// A physical id names the stack, it does not narrow it: AWS returns "the associated
|
||||
// resources of the stack that the resource belongs to", which is the whole stack.
|
||||
Stack stack = hasStackName
|
||||
? getStackOrThrow(stackName, region)
|
||||
: stackContaining(physicalResourceId, region);
|
||||
List<StackResource> resources = new ArrayList<>(stack.resourcesSnapshot().values());
|
||||
|
||||
// Only a filter on the result. The reference documents no error for a logical id the
|
||||
// stack does not have, and DescribeStackResource, the singular action where the resource
|
||||
// is required rather than optional, is the one that raises for a miss.
|
||||
if (logicalResourceId != null && !logicalResourceId.isBlank()) {
|
||||
resources.removeIf(r -> !logicalResourceId.equals(r.getLogicalId()));
|
||||
}
|
||||
return new StackResourcesView(stack.getStackName(), resources);
|
||||
}
|
||||
|
||||
/**
|
||||
* The stack owning a physical id, for the {@code PhysicalResourceId}-only form of the call.
|
||||
*
|
||||
* <p>Live stacks are considered before deleted ones, newest first within each group.
|
||||
* {@code listStacks} answers oldest first and keeps deleted stacks for their retention window,
|
||||
* so walking it directly resolves a redeployed resource to the stack that used to own it.
|
||||
*/
|
||||
private Stack stackContaining(String physicalResourceId, String region) {
|
||||
List<Stack> candidates = new ArrayList<>(listStacks(region));
|
||||
candidates.sort(Comparator
|
||||
.comparing((Stack s) -> "DELETE_COMPLETE".equals(s.getStatus()))
|
||||
.thenComparing(Stack::getCreationTime, Comparator.reverseOrder()));
|
||||
for (Stack stack : candidates) {
|
||||
boolean owns = stack.resourcesSnapshot().values().stream()
|
||||
.anyMatch(r -> physicalResourceId.equals(r.getPhysicalId()));
|
||||
if (owns) {
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
throw new AwsException("ValidationError",
|
||||
"Stack for " + physicalResourceId + " does not exist", 400);
|
||||
}
|
||||
|
||||
// ── ListStacks ────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user