fix(cloudformation): resolve path-style AWS S3 TemplateURL against local S3 (#627) (#641)

CDK passes TemplateURL as https://s3.{region}.amazonaws.com/{bucket}/{key}.
The condition host.endsWith(".amazonaws.com") incorrectly matched this as
virtual-hosted style, extracting "s3" as the bucket name instead of the real
bucket. Fixed by only treating URLs with a bucket label before ".s3." as
virtual-hosted. Also changed the failure path from silently returning "{}"
(causing CREATE_COMPLETE with zero resources) to rethrowing so the stack
correctly reaches CREATE_FAILED.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-29 00:02:33 -05:00
1 parent 3960b6e20e
commit 78b0d4d3e2
2 files changed
+156 -26

No files matched your search

@@ -427,36 +427,44 @@ public class CloudFormationService {
}
private String fetchTemplateFromS3(String url) {
// Parse S3 URL — three forms:
// Virtual-hosted AWS: https://bucket.s3[.region].amazonaws.com/key
// Virtual-hosted local: http://bucket.localhost:4566/key (or configured hostname)
// Path-style (both): https://s3[.region].amazonaws.com/bucket/key
// http://host:port/bucket/key
//
// The old condition matched host.endsWith(".amazonaws.com") for virtual-hosted, which
// incorrectly caught path-style AWS URLs like s3.us-east-1.amazonaws.com and extracted
// "s3" as the bucket name. Virtual-hosted URLs always have a bucket label before ".s3.".
String bucket;
String key;
URI uri = URI.create(url);
String host = uri.getHost();
String path = uri.getRawPath();
boolean isVirtualHosted = host != null && (
host.contains(".s3.")
|| (config.hostname().isPresent() && host.endsWith("." + config.hostname().get()))
|| host.endsWith(".localhost"));
if (isVirtualHosted) {
bucket = host.split("\\.")[0];
key = path.startsWith("/") ? path.substring(1) : path;
} else {
// Path-style: /bucket/key
String rawPath = path.startsWith("/") ? path.substring(1) : path;
int slash = rawPath.indexOf('/');
bucket = slash > 0 ? rawPath.substring(0, slash) : rawPath;
key = slash > 0 ? rawPath.substring(slash + 1) : "";
}
try {
// Parse S3 URL:
// Virtual-hosted local: http://bucket.localhost:4566/key
// Virtual-hosted AWS: https://bucket.s3.region.amazonaws.com/key
// Path-style: http://host:port/bucket/key
String bucket;
String key;
URI uri = URI.create(url);
String host = uri.getHost();
String path = uri.getRawPath();
if (host != null && (host.contains(".s3.") || host.endsWith(".amazonaws.com") ||
(config.hostname().isPresent() && host.endsWith("." + config.hostname().get())) ||
(host.endsWith(".localhost")))) {
// Virtual-hosted style
bucket = host.split("\\.")[0];
key = path.startsWith("/") ? path.substring(1) : path;
} else {
// Path-style: /bucket/key
String rawPath = path.startsWith("/") ? path.substring(1) : path;
int slash = rawPath.indexOf('/');
bucket = slash > 0 ? rawPath.substring(0, slash) : rawPath;
key = slash > 0 ? rawPath.substring(slash + 1) : "";
}
var obj = s3Service.getObject(bucket, key);
return new String(obj.getData());
} catch (Exception e) {
LOG.warnv("Failed to fetch template from {0}: {1}", url, e.getMessage());
return "{}";
LOG.errorv("Failed to fetch CloudFormation template from {0}: {1}", url, e.getMessage());
throw new RuntimeException("Failed to fetch CloudFormation template from " + url + ": " + e.getMessage(), e);
}
}