Feat/cfn auto physical name generation (#163)

* refactor(cfn): add stackName param and generatePhysicalName utility

Add stackName parameter to provision() method signature and pass it
from CloudFormationService.executeTemplate(). Add generatePhysicalName
helper that generates AWS-like names: {stackName}-{logicalId}-{suffix}.

No resource naming behavior changed yet — this commit only introduces
the infrastructure for the next step.

* feat(cfn): use generatePhysicalName for all unnamed resources

Replace cfn-xxx random fallback names with AWS-like naming pattern
{stackName}-{logicalId}-{suffix} for 12 resource types:

- S3 Bucket (lowercase, max 63)
- SQS Queue (max 80)
- SNS Topic (max 256)
- DynamoDB Table/GlobalTable (max 255)
- Lambda Function (max 64)
- IAM Role (max 64), User (max 64), Policy (max 128),
  ManagedPolicy (max 128), InstanceProfile (max 128)
- SSM Parameter (path-based: /cfn/{stackName}/{logicalId})
- SecretsManager Secret (max 512)
- ECR Repository (lowercase, max 256)
This commit is contained in:
yoyo authored and GitHub committed 2026-04-01 16:17:52 -05:00
1 parent 4fc9398df4
commit 675d5a33d0
3 files changed
+555 -41

No files matched your search

@@ -61,34 +61,35 @@ public class CloudFormationResourceProvisioner {
* Returns null and logs a warning for unsupported types.
*/
public StackResource provision(String logicalId, String resourceType, JsonNode properties,
CloudFormationTemplateEngine engine, String region, String accountId) {
CloudFormationTemplateEngine engine, String region, String accountId,
String stackName) {
StackResource resource = new StackResource();
resource.setLogicalId(logicalId);
resource.setResourceType(resourceType);
try {
switch (resourceType) {
case "AWS::S3::Bucket" -> provisionS3Bucket(resource, properties, engine, region, accountId);
case "AWS::SQS::Queue" -> provisionSqsQueue(resource, properties, engine, region, accountId);
case "AWS::SNS::Topic" -> provisionSnsTopic(resource, properties, engine, region, accountId);
case "AWS::S3::Bucket" -> provisionS3Bucket(resource, properties, engine, region, accountId, stackName);
case "AWS::SQS::Queue" -> provisionSqsQueue(resource, properties, engine, region, accountId, stackName);
case "AWS::SNS::Topic" -> provisionSnsTopic(resource, properties, engine, region, accountId, stackName);
case "AWS::DynamoDB::Table", "AWS::DynamoDB::GlobalTable" ->
provisionDynamoTable(resource, properties, engine, region, accountId);
case "AWS::Lambda::Function" -> provisionLambda(resource, properties, engine, region, accountId);
case "AWS::IAM::Role" -> provisionIamRole(resource, properties, engine, accountId);
case "AWS::IAM::User" -> provisionIamUser(resource, properties, engine);
provisionDynamoTable(resource, properties, engine, region, accountId, stackName);
case "AWS::Lambda::Function" -> provisionLambda(resource, properties, engine, region, accountId, stackName);
case "AWS::IAM::Role" -> provisionIamRole(resource, properties, engine, accountId, stackName);
case "AWS::IAM::User" -> provisionIamUser(resource, properties, engine, stackName);
case "AWS::IAM::AccessKey" -> provisionIamAccessKey(resource, properties, engine);
case "AWS::IAM::Policy", "AWS::IAM::ManagedPolicy" ->
provisionIamPolicy(resource, properties, engine, accountId);
case "AWS::IAM::InstanceProfile" -> provisionInstanceProfile(resource, properties, engine, accountId);
case "AWS::SSM::Parameter" -> provisionSsmParameter(resource, properties, engine, region);
provisionIamPolicy(resource, properties, engine, accountId, stackName);
case "AWS::IAM::InstanceProfile" -> provisionInstanceProfile(resource, properties, engine, accountId, stackName);
case "AWS::SSM::Parameter" -> provisionSsmParameter(resource, properties, engine, region, stackName);
case "AWS::KMS::Key" -> provisionKmsKey(resource, properties, engine, region, accountId);
case "AWS::KMS::Alias" -> provisionKmsAlias(resource, properties, engine, region);
case "AWS::SecretsManager::Secret" -> provisionSecret(resource, properties, engine, region, accountId);
case "AWS::SecretsManager::Secret" -> provisionSecret(resource, properties, engine, region, accountId, stackName);
case "AWS::CloudFormation::Stack" -> provisionNestedStack(resource, properties, engine, region);
case "AWS::CDK::Metadata" -> provisionCdkMetadata(resource);
case "AWS::S3::BucketPolicy" -> provisionS3BucketPolicy(resource, properties, engine);
case "AWS::SQS::QueuePolicy" -> provisionSqsQueuePolicy(resource, properties, engine);
case "AWS::ECR::Repository" -> provisionEcrRepository(resource, properties, engine);
case "AWS::ECR::Repository" -> provisionEcrRepository(resource, properties, engine, stackName);
case "AWS::Route53::HostedZone" -> provisionRoute53HostedZone(resource, properties, engine);
case "AWS::Route53::RecordSet" -> provisionRoute53RecordSet(resource, properties, engine);
default -> {
@@ -133,10 +134,10 @@ public class CloudFormationResourceProvisioner {
// ── S3 ────────────────────────────────────────────────────────────────────
private void provisionS3Bucket(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String accountId) {
String region, String accountId, String stackName) {
String bucketName = resolveOptional(props, "BucketName", engine);
if (bucketName == null || bucketName.isBlank()) {
bucketName = "cfn-" + UUID.randomUUID().toString().substring(0, 12).toLowerCase();
bucketName = generatePhysicalName(stackName, r.getLogicalId(), 63, true);
}
s3Service.createBucket(bucketName, region);
r.setPhysicalId(bucketName);
@@ -150,10 +151,10 @@ public class CloudFormationResourceProvisioner {
// ── SQS ───────────────────────────────────────────────────────────────────
private void provisionSqsQueue(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String accountId) {
String region, String accountId, String stackName) {
String queueName = resolveOptional(props, "QueueName", engine);
if (queueName == null || queueName.isBlank()) {
queueName = "cfn-" + UUID.randomUUID().toString().substring(0, 12);
queueName = generatePhysicalName(stackName, r.getLogicalId(), 80, false);
}
Map<String, String> attrs = new HashMap<>();
if (props != null && props.has("VisibilityTimeout")) {
@@ -170,10 +171,10 @@ public class CloudFormationResourceProvisioner {
// ── SNS ───────────────────────────────────────────────────────────────────
private void provisionSnsTopic(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String accountId) {
String region, String accountId, String stackName) {
String topicName = resolveOptional(props, "TopicName", engine);
if (topicName == null || topicName.isBlank()) {
topicName = "cfn-" + UUID.randomUUID().toString().substring(0, 12);
topicName = generatePhysicalName(stackName, r.getLogicalId(), 256, false);
}
var topic = snsService.createTopic(topicName, Map.of(), Map.of(), region);
r.setPhysicalId(topic.getTopicArn());
@@ -184,10 +185,10 @@ public class CloudFormationResourceProvisioner {
// ── DynamoDB ──────────────────────────────────────────────────────────────
private void provisionDynamoTable(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String accountId) {
String region, String accountId, String stackName) {
String tableName = resolveOptional(props, "TableName", engine);
if (tableName == null || tableName.isBlank()) {
tableName = "cfn-" + UUID.randomUUID().toString().substring(0, 12);
tableName = generatePhysicalName(stackName, r.getLogicalId(), 255, false);
}
List<KeySchemaElement> keySchema = new ArrayList<>();
@@ -264,10 +265,10 @@ public class CloudFormationResourceProvisioner {
// ── Lambda ────────────────────────────────────────────────────────────────
private void provisionLambda(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String accountId) {
String region, String accountId, String stackName) {
String funcName = resolveOptional(props, "FunctionName", engine);
if (funcName == null || funcName.isBlank()) {
funcName = "cfn-func-" + UUID.randomUUID().toString().substring(0, 8);
funcName = generatePhysicalName(stackName, r.getLogicalId(), 64, false);
}
Map<String, Object> req = new HashMap<>();
req.put("FunctionName", funcName);
@@ -287,10 +288,10 @@ public class CloudFormationResourceProvisioner {
// ── IAM Role ──────────────────────────────────────────────────────────────
private void provisionIamRole(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String accountId) {
String accountId, String stackName) {
String roleName = resolveOptional(props, "RoleName", engine);
if (roleName == null || roleName.isBlank()) {
roleName = "cfn-role-" + UUID.randomUUID().toString().substring(0, 8);
roleName = generatePhysicalName(stackName, r.getLogicalId(), 64, false);
}
String assumeDoc = props != null && props.has("AssumeRolePolicyDocument")
? props.get("AssumeRolePolicyDocument").toString()
@@ -328,10 +329,10 @@ public class CloudFormationResourceProvisioner {
// ── IAM Policy ────────────────────────────────────────────────────────────
private void provisionIamPolicy(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String accountId) {
String accountId, String stackName) {
String policyName = resolveOptional(props, "PolicyName", engine);
if (policyName == null || policyName.isBlank()) {
policyName = "cfn-policy-" + UUID.randomUUID().toString().substring(0, 8);
policyName = generatePhysicalName(stackName, r.getLogicalId(), 128, false);
}
String document = props != null && props.has("PolicyDocument")
? props.get("PolicyDocument").toString()
@@ -353,17 +354,17 @@ public class CloudFormationResourceProvisioner {
}
private void provisionIamManagedPolicy(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String accountId) {
provisionIamPolicy(r, props, engine, accountId);
String accountId, String stackName) {
provisionIamPolicy(r, props, engine, accountId, stackName);
}
// ── IAM Instance Profile ──────────────────────────────────────────────────
private void provisionInstanceProfile(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String accountId) {
String accountId, String stackName) {
String name = resolveOptional(props, "InstanceProfileName", engine);
if (name == null || name.isBlank()) {
name = "cfn-profile-" + UUID.randomUUID().toString().substring(0, 8);
name = generatePhysicalName(stackName, r.getLogicalId(), 128, false);
}
try {
var profile = iamService.createInstanceProfile(name, "/");
@@ -378,10 +379,10 @@ public class CloudFormationResourceProvisioner {
// ── SSM Parameter ─────────────────────────────────────────────────────────
private void provisionSsmParameter(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region) {
String region, String stackName) {
String name = resolveOptional(props, "Name", engine);
if (name == null || name.isBlank()) {
name = "/cfn/" + UUID.randomUUID().toString().substring(0, 12);
name = generatePhysicalName(stackName, r.getLogicalId(), 2048, false);
}
String value = resolveOptional(props, "Value", engine);
if (value == null) {
@@ -419,10 +420,10 @@ public class CloudFormationResourceProvisioner {
// ── Secrets Manager ───────────────────────────────────────────────────────
private void provisionSecret(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String accountId) {
String region, String accountId, String stackName) {
String name = resolveOptional(props, "Name", engine);
if (name == null || name.isBlank()) {
name = "cfn-secret-" + UUID.randomUUID().toString().substring(0, 8);
name = generatePhysicalName(stackName, r.getLogicalId(), 512, false);
}
String value = resolveOptional(props, "SecretString", engine);
if (value == null) {
@@ -460,10 +461,11 @@ public class CloudFormationResourceProvisioner {
r.setPhysicalId("queue-policy-" + UUID.randomUUID().toString().substring(0, 8));
}
private void provisionIamUser(StackResource r, JsonNode props, CloudFormationTemplateEngine engine) {
private void provisionIamUser(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String stackName) {
String userName = resolveOptional(props, "UserName", engine);
if (userName == null || userName.isBlank()) {
userName = "cfn-user-" + UUID.randomUUID().toString().substring(0, 8);
userName = generatePhysicalName(stackName, r.getLogicalId(), 64, false);
}
var user = iamService.createUser(userName, "/");
r.setPhysicalId(userName);
@@ -479,10 +481,11 @@ public class CloudFormationResourceProvisioner {
}
}
private void provisionEcrRepository(StackResource r, JsonNode props, CloudFormationTemplateEngine engine) {
private void provisionEcrRepository(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String stackName) {
String repoName = resolveOptional(props, "RepositoryName", engine);
if (repoName == null || repoName.isBlank()) {
repoName = "cfn-repo-" + UUID.randomUUID().toString().substring(0, 8);
repoName = generatePhysicalName(stackName, r.getLogicalId(), 256, true);
}
r.setPhysicalId(repoName);
r.getAttributes().put("Arn", "arn:aws:ecr:us-east-1:000000000000:repository/" + repoName);
@@ -527,4 +530,20 @@ public class CloudFormationResourceProvisioner {
LOG.debugv("Could not delete policy {0}: {1}", policyArn, e.getMessage());
}
}
/**
* Generate an AWS-like physical name: {stackName}-{logicalId}-{randomSuffix}.
* Mirrors the naming pattern AWS CloudFormation uses when no explicit name is provided.
*/
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;
if (lowercase) {
name = name.toLowerCase();
}
if (maxLength > 0 && name.length() > maxLength) {
name = name.substring(0, maxLength);
}
return name;
}
}
@@ -240,7 +240,7 @@ public class CloudFormationService {
addEvent(stack, logicalId, null, type, "CREATE_IN_PROGRESS", null);
resource = provisioner.provision(logicalId, type, props.isMissingNode() ? null : props,
engine, region, config.defaultAccountId());
engine, region, config.defaultAccountId(), stack.getStackName());
stack.getResources().put(logicalId, resource);
physicalIds.put(logicalId, resource.getPhysicalId());