mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -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 ────────────────────────────────────────────────────────────
|
||||
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* The two filters {@code DescribeStackResources} accepts.
|
||||
*
|
||||
* <p>Ignoring them is quiet rather than loud: a caller that filters and reads the first member
|
||||
* gets whichever resource the stack happens to list first, and nothing in the response says the
|
||||
* filter was dropped. Serverless Framework resolves an AppSync API id exactly that way, which is
|
||||
* how this surfaced (issue #5451).
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationDescribeStackResourcesFilterIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
|
||||
|
||||
private String createStack(String suffix) {
|
||||
return createStack("cfn-dsr-" + suffix, suffix);
|
||||
}
|
||||
|
||||
/** Stack name and resource names chosen separately, so two stacks can own the same physical id. */
|
||||
private String createStack(String stackName, String suffix) {
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"MyQueue": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": "cfn-dsr-q-%s"}},
|
||||
"MyTopic": {"Type": "AWS::SNS::Topic", "Properties": {"TopicName": "cfn-dsr-t-%s"}}
|
||||
}
|
||||
}
|
||||
""".formatted(suffix, suffix);
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
return stackName;
|
||||
}
|
||||
|
||||
private RequestSpecification describe() {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackResources");
|
||||
}
|
||||
|
||||
@Test
|
||||
void logicalResourceIdNarrowsToThatOneResource() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = createStack(suffix);
|
||||
|
||||
describe()
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("LogicalResourceId", "MyTopic")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<LogicalResourceId>MyTopic</LogicalResourceId>"))
|
||||
.body(not(containsString("<LogicalResourceId>MyQueue</LogicalResourceId>")))
|
||||
// AWS stamps the stack's own name on every member.
|
||||
.body(containsString("<StackName>" + stackName + "</StackName>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLogicalResourceIdTheStackDoesNotHaveMatchesNothing() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = createStack(suffix);
|
||||
|
||||
// A filter that matches nothing, not an error: DescribeStackResource, the singular action
|
||||
// where the resource is required rather than optional, is the one that raises for a miss.
|
||||
describe()
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("LogicalResourceId", "NoSuchResource")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString("<LogicalResourceId>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void physicalResourceIdNamesTheStackAndReturnsAllOfIt() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = createStack(suffix);
|
||||
String topicName = "cfn-dsr-t-" + suffix;
|
||||
|
||||
// "the associated resources of the stack that the resource belongs to are returned": the
|
||||
// physical id selects the stack, it does not narrow it to the one resource.
|
||||
describe()
|
||||
.formParam("PhysicalResourceId", "arn:aws:sns:us-east-1:000000000000:" + topicName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<LogicalResourceId>MyTopic</LogicalResourceId>"))
|
||||
.body(containsString("<LogicalResourceId>MyQueue</LogicalResourceId>"))
|
||||
.body(containsString("<StackName>" + stackName + "</StackName>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bothStackNameAndPhysicalResourceIdIsAValidationError() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = createStack(suffix);
|
||||
|
||||
describe()
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("PhysicalResourceId", "arn:aws:sns:us-east-1:000000000000:cfn-dsr-t-" + suffix)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("ValidationError"))
|
||||
.body(containsString("Cannot specify both StackName and PhysicalResourceId."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRedeployedPhysicalIdResolvesToTheLiveStackNotTheDeletedOne() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String topicArn = "arn:aws:sns:us-east-1:000000000000:cfn-dsr-t-" + suffix;
|
||||
String dead = createStack("cfn-dsr-old-" + suffix, suffix);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", dead)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// DeleteStack answers before the deletion runs. Without this the old stack can still be
|
||||
// DELETE_IN_PROGRESS when the new one is created, which sorts it with the live stacks and
|
||||
// lets the test pass on creation time alone without ever exercising the deleted branch.
|
||||
CfnStackWaits.awaitStackDeleted(dead);
|
||||
|
||||
// The same resource names under a second, live stack. A deleted stack stays listed for its
|
||||
// retention window and listStacks answers oldest first, so the dead one would win on a
|
||||
// straight walk. The two stacks are named differently so the answer is tellable apart.
|
||||
String live = createStack("cfn-dsr-new-" + suffix, suffix);
|
||||
|
||||
describe()
|
||||
.formParam("PhysicalResourceId", topicArn)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackName>" + live + "</StackName>"))
|
||||
.body(not(containsString("<StackName>" + dead + "</StackName>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void neitherStackNameNorPhysicalResourceIdIsAValidationError() {
|
||||
describe()
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("ValidationError"))
|
||||
.body(containsString("Either StackName or PhysicalResourceId must be specified."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutAFilterEveryResourceStillComesBack() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = createStack(suffix);
|
||||
|
||||
describe()
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<LogicalResourceId>MyQueue</LogicalResourceId>"))
|
||||
.body(containsString("<LogicalResourceId>MyTopic</LogicalResourceId>"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user