mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudformation): keep uniqueness suffix when truncating generated resource names (#1802)
* fix(cloudformation): keep uniqueness suffix when truncating generated resource names
CloudFormation auto-generates a physical name as
`{stackName}-{logicalId}-{random}` for resources declared without an
explicit name. When that string exceeded the resource type's name-length
cap, generatePhysicalName truncated the whole string with
`substring(0, maxLength)`, which dropped the trailing random token. Every
auto-named resource in a long-named stack then collapsed onto the same
truncated name, breaking Ref/GetAtt lookups — most visibly a
`Custom::LogRetention` whose `ServiceToken` (a GetAtt of its provider
Lambda's Arn) resolved to a different, colliding function.
Truncate only the descriptive prefix and always keep the random suffix,
matching how CloudFormation shortens a generated name while preserving its
uniqueness token, so distinct logical resources keep distinct physical names.
* fix(cloudformation): preserve uniqueness suffix in extracted ProvisionContext name generator
The #1825 registry refactor lifted generatePhysicalName into ProvisionContext
verbatim, so migrated resource types (SQS) once again truncated the trailing
uniqueness token when a stack name neared the length limit. Apply the same
suffix-preserving truncation there so extracted provisioners stay consistent
with the monolith.
* fix(cloudformation): harden generated name regression test
This commit is contained in:
1 parent
91e06ebebf
commit
48b2139b4f
3 files changed
+150
-6
No files matched your search
+15
-3
@@ -4422,12 +4422,24 @@ public class CloudFormationResourceProvisioner {
|
||||
*/
|
||||
private String generatePhysicalName(String stackName, String logicalId, int maxLength, boolean lowercase) {
|
||||
String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
|
||||
String name = stackName + "-" + logicalId + "-" + suffix;
|
||||
String base = stackName + "-" + logicalId;
|
||||
if (lowercase) {
|
||||
name = name.toLowerCase();
|
||||
base = base.toLowerCase();
|
||||
}
|
||||
String name = base + "-" + suffix;
|
||||
if (maxLength > 0 && name.length() > maxLength) {
|
||||
name = name.substring(0, maxLength);
|
||||
// Truncate the descriptive prefix but always keep the trailing uniqueness token. When a
|
||||
// stack's name approaches the length limit, distinct logical resources still get distinct
|
||||
// physical names — CloudFormation preserves the random suffix when it shortens a generated
|
||||
// name. Truncating the whole string (suffix included) would collapse every such resource
|
||||
// onto one name and break Ref/GetAtt-based lookup (e.g. a custom resource's ServiceToken
|
||||
// resolving to the wrong Lambda).
|
||||
int keep = Math.max(0, maxLength - suffix.length() - 1);
|
||||
String prefix = base.length() > keep ? base.substring(0, keep) : base;
|
||||
while (prefix.endsWith("-")) {
|
||||
prefix = prefix.substring(0, prefix.length() - 1);
|
||||
}
|
||||
name = prefix.isEmpty() ? suffix : prefix + "-" + suffix;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
+15
-3
@@ -25,12 +25,24 @@ public record ProvisionContext(CloudFormationTemplateEngine engine, String regio
|
||||
/** Generates a CloudFormation-style physical name: {@code <stack>-<logicalId>-<suffix>}. */
|
||||
public String generatePhysicalName(String logicalId, int maxLength, boolean lowercase) {
|
||||
String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
|
||||
String name = stackName + "-" + logicalId + "-" + suffix;
|
||||
String base = stackName + "-" + logicalId;
|
||||
if (lowercase) {
|
||||
name = name.toLowerCase();
|
||||
base = base.toLowerCase();
|
||||
}
|
||||
String name = base + "-" + suffix;
|
||||
if (maxLength > 0 && name.length() > maxLength) {
|
||||
name = name.substring(0, maxLength);
|
||||
// Truncate the descriptive prefix but always keep the trailing uniqueness token. When a
|
||||
// stack's name approaches the length limit, distinct logical resources still get distinct
|
||||
// physical names — CloudFormation preserves the random suffix when it shortens a generated
|
||||
// name. Truncating the whole string (suffix included) would collapse every such resource
|
||||
// onto one name and break Ref/GetAtt-based lookup (e.g. a custom resource's ServiceToken
|
||||
// resolving to the wrong Lambda).
|
||||
int keep = Math.max(0, maxLength - suffix.length() - 1);
|
||||
String prefix = base.length() > keep ? base.substring(0, keep) : base;
|
||||
while (prefix.endsWith("-")) {
|
||||
prefix = prefix.substring(0, prefix.length() - 1);
|
||||
}
|
||||
name = prefix.isEmpty() ? suffix : prefix + "-" + suffix;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Verifies auto-generated physical resource names stay unique when a stack's name approaches the
|
||||
* resource name-length limit.
|
||||
*
|
||||
* <p>Two SQS queues in a stack whose name is longer than the 80-char queue-name cap must still get
|
||||
* distinct generated names: CloudFormation keeps the random uniqueness suffix when it truncates a
|
||||
* generated name. Previously the whole {@code stackName-logicalId-suffix} string was truncated to the
|
||||
* cap, which dropped the suffix, so every auto-named resource in such a stack collapsed onto a single
|
||||
* name. That broke Ref/GetAtt-based lookup — most visibly a {@code Custom::LogRetention} whose
|
||||
* {@code ServiceToken} (GetAtt of its provider Lambda's Arn) resolved to a different, colliding
|
||||
* function, so the invoke ran the wrong handler and no response was ever PUT to the ResponseURL.
|
||||
*
|
||||
* <p>All resources are metadata-only (no container), so the test is Docker-free.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationGeneratedNameUniquenessIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
private void createStack(String stackName, String template) {
|
||||
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);
|
||||
}
|
||||
|
||||
private String describeStacks(String stackName) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when().post("/").then().statusCode(200).extract().asString();
|
||||
}
|
||||
|
||||
private void deleteStack(String stackName) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", stackName)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String outputValue(String xml, String key) {
|
||||
return XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue").get(key);
|
||||
}
|
||||
|
||||
private static String queueName(String queueUrl) {
|
||||
String path = URI.create(queueUrl).getPath();
|
||||
return path.substring(path.lastIndexOf('/') + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedNamesStayUniqueWhenStackNameNearsLengthLimit() {
|
||||
// A stack name longer than the 80-char SQS queue-name cap: after truncation the descriptive
|
||||
// prefix is identical for both queues, so only a preserved suffix can keep them distinct.
|
||||
String uniq = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "gen-name-uniqueness-stack-with-a-deliberately-very-long-name-"
|
||||
+ "padding-padding-padding-padding-" + uniq;
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"QueueOne": { "Type": "AWS::SQS::Queue" },
|
||||
"QueueTwo": { "Type": "AWS::SQS::Queue" }
|
||||
},
|
||||
"Outputs": {
|
||||
"QOne": { "Value": { "Ref": "QueueOne" } },
|
||||
"QTwo": { "Value": { "Ref": "QueueTwo" } }
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
createStack(stackName, template);
|
||||
try {
|
||||
String xml = describeStacks(stackName);
|
||||
assertTrue(xml.contains("<StackStatus>CREATE_COMPLETE</StackStatus>"),
|
||||
"stack should be CREATE_COMPLETE");
|
||||
|
||||
String qOne = outputValue(xml, "QOne");
|
||||
String qTwo = outputValue(xml, "QTwo");
|
||||
assertTrue(qOne != null && !qOne.isBlank(), "QOne ref should be present");
|
||||
assertTrue(qTwo != null && !qTwo.isBlank(), "QTwo ref should be present");
|
||||
String qOneName = queueName(qOne);
|
||||
String qTwoName = queueName(qTwo);
|
||||
assertTrue(qOneName.length() <= 80, "QOne must respect the SQS queue-name limit");
|
||||
assertTrue(qTwoName.length() <= 80, "QTwo must respect the SQS queue-name limit");
|
||||
assertTrue(qOneName.matches(".*-[0-9a-f]{12}$"), "QOne must retain its 12-hex uniqueness suffix");
|
||||
assertTrue(qTwoName.matches(".*-[0-9a-f]{12}$"), "QTwo must retain its 12-hex uniqueness suffix");
|
||||
assertNotEquals(qOne, qTwo,
|
||||
"two auto-named queues in a long-named stack must get distinct names (unique suffix preserved)");
|
||||
} finally {
|
||||
deleteStack(stackName);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user