Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Hector Ventura78b0d4d3e23b
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
index cd3b987f06..e48dbfee09 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java
@@ -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);
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
index 7250f1057f..09988de44d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java
@@ -2268,4 +2268,126 @@ class CloudFormationIntegrationTest {
.then()
.statusCode(404);
}
+
+ // ── TemplateURL (path-style AWS S3) ──────────────────────────────────────
+
+ @Test
+ void createStack_templateUrlPathStyle_resolvesLocalS3() {
+ String bucket = "cfn-template-url-bucket";
+ String key = "template.json";
+ String template = """
+ {
+ "Resources": {
+ "MyQueue": {
+ "Type": "AWS::SQS::Queue",
+ "Properties": {
+ "QueueName": "cfn-template-url-queue"
+ }
+ }
+ }
+ }
+ """;
+
+ // Create S3 bucket and upload template
+ given().when().put("/" + bucket).then().statusCode(200);
+ given()
+ .contentType("application/json")
+ .body(template)
+ .when()
+ .put("/" + bucket + "/" + key)
+ .then()
+ .statusCode(200);
+
+ // CreateStack using a CDK-style path-style AWS S3 TemplateURL
+ String templateUrl = "https://s3.us-east-1.amazonaws.com/" + bucket + "/" + key;
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", "cfn-template-url-stack")
+ .formParam("TemplateURL", templateUrl)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackId>"));
+
+ // Verify stack and its resource were provisioned from the S3 template
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", "cfn-template-url-stack")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("CREATE_COMPLETE"));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "GetQueueUrl")
+ .formParam("QueueName", "cfn-template-url-queue")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ @Test
+ void createStack_templateUrlVirtualHosted_resolvesLocalS3() {
+ String bucket = "cfn-vhost-template-bucket";
+ String key = "template.json";
+ String template = """
+ {
+ "Resources": {
+ "MyQueue": {
+ "Type": "AWS::SQS::Queue",
+ "Properties": {
+ "QueueName": "cfn-vhost-template-queue"
+ }
+ }
+ }
+ }
+ """;
+
+ given().when().put("/" + bucket).then().statusCode(200);
+ given()
+ .contentType("application/json")
+ .body(template)
+ .when()
+ .put("/" + bucket + "/" + key)
+ .then()
+ .statusCode(200);
+
+ // Virtual-hosted style: bucket.s3.region.amazonaws.com/key
+ String templateUrl = "https://" + bucket + ".s3.us-east-1.amazonaws.com/" + key;
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", "cfn-vhost-template-stack")
+ .formParam("TemplateURL", templateUrl)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("<StackId>"));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", "cfn-vhost-template-stack")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(containsString("CREATE_COMPLETE"));
+
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .formParam("Action", "GetQueueUrl")
+ .formParam("QueueName", "cfn-vhost-template-queue")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
}