diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 0000000000..7fd4eb5cf2 --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,154 @@ +# Copilot Instructions for Pull Request Review + +Review pull requests in the Floci repository with AWS compatibility as the primary concern. + +Floci is a Java-based local AWS emulator built on Quarkus. Its goal is to match AWS SDK and AWS CLI behavior through real AWS wire protocols, not convenience APIs or custom abstractions. + +## Review Priorities + +Evaluate changes in this order: + +1. Preserve AWS protocol compatibility +2. Match AWS SDK and AWS CLI behavior +3. Reuse existing Floci patterns +4. Prefer correctness over convenience +5. Keep changes focused and testable + +## What to Flag + +Raise concerns when a PR introduces any of the following without strong justification: + +- Non-AWS endpoint shapes +- Request or response format changes made for convenience +- Broad refactors unrelated to the PR goal +- New service patterns where an existing Floci pattern should be reused +- Direct storage implementation usage instead of `StorageFactory` + +## Architecture Expectations + +Floci follows a layered design: + +- Controllers / handlers parse AWS protocol input and produce AWS-compatible responses +- Services contain business logic and should throw `AwsException` +- Models hold domain data + +Core infrastructure commonly relevant in reviews: + +- `EmulatorConfig` +- `ServiceRegistry` +- `StorageFactory` +- `AwsQueryController` +- `AwsJson11Controller` +- `AwsException` +- `AwsExceptionMapper` +- `EmulatorLifecycle` + +Check that controllers stay thin, business logic remains in services, and new changes fit existing repository patterns. + +## Protocol Review Rules + +Floci implements real AWS wire protocols. Review protocol-affecting changes carefully. + +- Query services should keep form-encoded POST requests with `Action` and XML responses +- JSON 1.1 services should keep `X-Amz-Target` requests and AWS-style JSON responses +- REST JSON and REST XML services should stay aligned with AWS path and payload conventions +- TCP-based services should not drift into HTTP-style abstractions + +Pay extra attention to these cases: + +- CloudWatch Metrics supports both Query and JSON 1.1 and both paths must stay aligned +- SQS and SNS may have multiple compatibility paths that must not drift +- Cognito well-known endpoints are OIDC REST JSON endpoints, not AWS management APIs +- Management APIs should ideally be validated with AWS SDK clients, not only handcrafted HTTP + +## XML and JSON Rules + +Flag PRs that: + +- Ignore `AwsNamespaces` constants +- Return JSON errors that do not follow AWS error structures +- Change controller return types in ways that may break reflection or native-image compatibility + +## Config and Storage Review + +When a PR changes configuration or persistence behavior, verify the change is wired consistently. + +Check for updates to: + +- `EmulatorConfig` +- main `application.yml` +- test `application.yml` +- `StorageFactory` +- lifecycle hooks when relevant + +Supported storage modes include: + +- `memory` +- `persistent` +- `hybrid` +- `wal` + +Treat repository YAML as the source of truth for runtime behavior unless the PR explicitly changes configuration semantics. + +## Testing Expectations + +Expect automated coverage for changes that affect: + +- request parsing +- response shape +- error handling +- persistence semantics +- URL generation +- service enablement + +Prefer: + +- AWS SDK-based validation over raw HTTP-only testing +- integration tests for compatibility-sensitive behavior +- existing naming conventions such as `*ServiceTest.java` and `*IntegrationTest.java` + +If behavior changes without automated coverage, call that out explicitly. + +## Review Checklist + +When analyzing a PR, check: + +- Is the change focused? +- Does it preserve AWS-compatible wire behavior? +- Does it reuse an existing Floci pattern? +- Are controllers thin and services responsible for domain logic? +- Are `AwsException` and existing error-mapping patterns used correctly? +- Are config and YAML updates complete? +- Are storage changes wired through `StorageFactory`? +- Are tests added or updated where compatibility is affected? +- Are docs updated when user-facing behavior changes? + +## How to Write Feedback + +Write review comments that are: + +- specific +- repository-aware +- grounded in AWS compatibility risk + +Use severity when helpful: + +- `high`: likely breaks AWS SDK / CLI compatibility or protocol behavior +- `medium`: inconsistent with Floci architecture, wiring, or testing expectations +- `low`: maintainability, clarity, or minor convention issue + +Prefer comments that explain: + +- what is risky +- why it matters in Floci +- which existing pattern should be followed instead + +## If Behavior Is Unclear + +Use this fallback order: + +1. Prefer AWS behavior +2. Then existing Floci behavior +3. Then compatibility test expectations + +If correctness would require a broader architectural change, call out the tradeoff instead of suggesting blind refactoring. diff --git a/.gitignore b/.gitignore index 98017711ce..2fe912b9cf 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ data/ .classpath .vscode/ .DS_Store +.zed/settings.json CLAUDE.md GEMINI.md diff --git a/docs/configuration/application-yml.md b/docs/configuration/application-yml.md index 0cd9f58453..4b85b81267 100644 --- a/docs/configuration/application-yml.md +++ b/docs/configuration/application-yml.md @@ -9,6 +9,7 @@ floci: base-url: "http://localhost:4566" # Used to build callback URLs (e.g. SNS subscription endpoints) default-region: us-east-1 default-account-id: "000000000000" + max-request-size: 512 # Max HTTP request body size in MB (default 512 MB) auth: validate-signatures: false # Set to true to enforce AWS request signing @@ -125,6 +126,7 @@ floci: | `FLOCI_SERVICES_SQS_DEFAULT_VISIBILITY_TIMEOUT` | `30` | Visibility timeout (seconds) | | `FLOCI_SERVICES_SQS_MAX_MESSAGE_SIZE` | `262144` | Max message size (bytes) | | `FLOCI_SERVICES_SQS_MAX_RECEIVE_COUNT` | `10` | Max receive count | +| `FLOCI_MAX_REQUEST_SIZE` | `512` | Max HTTP request body size in MB | | `FLOCI_SERVICES_S3_MAX_MULTIPART_PARTS` | `10000` | Max parts per upload | | `FLOCI_SERVICES_S3_DEFAULT_PRESIGN_EXPIRY_SECONDS` | `3600` | Pre-signed URL expiry | | `FLOCI_SERVICES_DYNAMODB_MAX_ITEM_SIZE` | `400000` | Max item size (bytes) | diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 9dad2e424e..036df0dc1e 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -38,6 +38,9 @@ public interface EmulatorConfig { @WithDefault("000000000000") String defaultAccountId(); + @WithDefault("512") + int maxRequestSize(); + StorageConfig storage(); AuthConfig auth(); diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java index 54b4538c43..62588c1e2b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java @@ -887,11 +887,41 @@ public class DynamoDbService { } // Resolve the value - if (valuePart.startsWith(":") && exprAttrValues != null) { + if (valuePart.startsWith("if_not_exists(")) { + // if_not_exists(attrRef, fallbackExpr) evaluates to: + // attrRef's current value — when attrRef exists in the item + // fallbackExpr — otherwise + // The result is always assigned to attrName. + String[] args = extractFunctionArgs(valuePart); + if (args.length == 2) { + String checkAttr = resolveAttributeName(args[0].trim(), exprAttrNames); + String fallbackExpr = args[1].trim(); + JsonNode resolved; + if (item.has(checkAttr)) { + // attrRef exists — evaluate to its current value + resolved = item.get(checkAttr); + } else if (fallbackExpr.startsWith(":") && exprAttrValues != null) { + resolved = exprAttrValues.get(fallbackExpr); + } else { + // fallback is itself an attribute reference + resolved = item.get(resolveAttributeName(fallbackExpr, exprAttrNames)); + } + if (resolved != null) { + item.set(attrName, resolved); + } + } + } else if (valuePart.startsWith(":") && exprAttrValues != null) { JsonNode value = exprAttrValues.get(valuePart); if (value != null) { item.set(attrName, value); } + } else if (!valuePart.isEmpty()) { + // Plain attribute reference: SET a = b or SET a = #alias + String refAttr = resolveAttributeName(valuePart, exprAttrNames); + JsonNode refValue = item.get(refAttr); + if (refValue != null) { + item.set(attrName, refValue); + } } clause = rest; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index dc4c78b6c7..c1d72ca3c3 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -3,7 +3,7 @@ quarkus: port: 4566 host: 0.0.0.0 limits: - max-body-size: 256M + max-body-size: ${floci.max-request-size}M native: additional-build-args: >- --initialize-at-run-time=org.apache.hc.client5.http.impl.auth.NTLMEngineImpl, @@ -16,6 +16,7 @@ quarkus: --initialize-at-run-time=io.github.hectorvent.floci.services.acm.CertificateGenerator floci: + max-request-size: 512 base-url: "http://localhost:4566" # hostname: "" # When set, overrides the hostname in response URLs (e.g. SQS QueueUrl). # Needed for multi-container Docker setups. Example: FLOCI_HOSTNAME=floci diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java index 1661208623..7f8e7da422 100644 --- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbServiceTest.java @@ -246,4 +246,160 @@ class DynamoDbServiceTest { assertThrows(AwsException.class, () -> service.query("NoTable", null, null, null, null, null)); assertThrows(AwsException.class, () -> service.scan("NoTable", null, null, null, null, null)); } + + @Test + void updateItemSetIfNotExistsOnNonExistentItemCreatesAttribute() { + createOrdersTable(); + + ObjectNode key = item("customerId", "1", "orderId", "sort1"); + + ObjectNode exprValues = mapper.createObjectNode(); + ObjectNode priceVal = mapper.createObjectNode(); + priceVal.put("N", "100"); + exprValues.set(":val", priceVal); + + service.updateItem("Orders", key, null, + "SET price = if_not_exists(price, :val)", + null, exprValues, null); + + JsonNode stored = service.getItem("Orders", key); + assertNotNull(stored, "item should have been created"); + assertTrue(stored.has("price"), "price attribute must be present on a newly created item"); + assertEquals("100", stored.get("price").get("N").asText()); + } + + @Test + void updateItemSetIfNotExistsPreservesExistingValue() { + createOrdersTable(); + + // Put an item that already has price = 200 + ObjectNode existing = mapper.createObjectNode(); + ObjectNode pkVal = mapper.createObjectNode(); pkVal.put("S", "1"); + ObjectNode skVal = mapper.createObjectNode(); skVal.put("S", "sort1"); + ObjectNode priceExisting = mapper.createObjectNode(); priceExisting.put("N", "200"); + existing.set("customerId", pkVal); + existing.set("orderId", skVal); + existing.set("price", priceExisting); + service.putItem("Orders", existing); + + ObjectNode key = item("customerId", "1", "orderId", "sort1"); + + ObjectNode exprValues = mapper.createObjectNode(); + ObjectNode fallback = mapper.createObjectNode(); fallback.put("N", "100"); + exprValues.set(":val", fallback); + + service.updateItem("Orders", key, null, + "SET price = if_not_exists(price, :val)", + null, exprValues, null); + + JsonNode stored = service.getItem("Orders", key); + assertNotNull(stored); + // Existing value must NOT be overwritten + assertEquals("200", stored.get("price").get("N").asText(), + "if_not_exists should preserve the existing value"); + } + + @Test + void updateItemSetIfNotExistsSetsAttributeWhenMissingFromExistingItem() { + createOrdersTable(); + + // Put an item that does NOT have a price attribute + service.putItem("Orders", item("customerId", "1", "orderId", "sort1")); + + ObjectNode key = item("customerId", "1", "orderId", "sort1"); + + ObjectNode exprValues = mapper.createObjectNode(); + ObjectNode fallback = mapper.createObjectNode(); fallback.put("N", "99"); + exprValues.set(":val", fallback); + + service.updateItem("Orders", key, null, + "SET price = if_not_exists(price, :val)", + null, exprValues, null); + + JsonNode stored = service.getItem("Orders", key); + assertNotNull(stored); + assertTrue(stored.has("price"), + "price should be set when it was absent from an existing item"); + assertEquals("99", stored.get("price").get("N").asText()); + } + + @Test + void updateItemSetIfNotExistsMultipleAttributesOnNewItem() { + createUsersTable(); + + ObjectNode key = item("userId", "u-new"); + + ObjectNode exprValues = mapper.createObjectNode(); + ObjectNode nameVal = mapper.createObjectNode(); nameVal.put("S", "DefaultName"); + ObjectNode scoreVal = mapper.createObjectNode(); scoreVal.put("N", "0"); + exprValues.set(":name", nameVal); + exprValues.set(":score", scoreVal); + + service.updateItem("Users", key, null, + "SET name = if_not_exists(name, :name), score = if_not_exists(score, :score)", + null, exprValues, null); + + JsonNode stored = service.getItem("Users", key); + assertNotNull(stored, "item should have been created"); + assertTrue(stored.has("name"), "name attribute must be present"); + assertEquals("DefaultName", stored.get("name").get("S").asText()); + assertTrue(stored.has("score"), "score attribute must be present"); + assertEquals("0", stored.get("score").get("N").asText()); + } + + @Test + void updateItemSetIfNotExistsCopiesSourceAttributeWhenAttrNameDiffersFromCheckAttr() { + // SET a = if_not_exists(b, :v) where b exists → a must be set to b's current value + createUsersTable(); + + // Put an item that has "source" but not "target" + ObjectNode existing = mapper.createObjectNode(); + ObjectNode userIdVal = mapper.createObjectNode(); userIdVal.put("S", "u-copy"); + ObjectNode sourceVal = mapper.createObjectNode(); sourceVal.put("S", "copied-value"); + existing.set("userId", userIdVal); + existing.set("source", sourceVal); + service.putItem("Users", existing); + + ObjectNode key = item("userId", "u-copy"); + + ObjectNode exprValues = mapper.createObjectNode(); + ObjectNode fallbackVal = mapper.createObjectNode(); fallbackVal.put("S", "fallback"); + exprValues.set(":v", fallbackVal); + + // target = if_not_exists(source, :v) — source exists, so target should receive source's value + service.updateItem("Users", key, null, + "SET target = if_not_exists(source, :v)", + null, exprValues, null); + + JsonNode stored = service.getItem("Users", key); + assertNotNull(stored); + assertTrue(stored.has("target"), "target attribute must be present"); + assertEquals("copied-value", stored.get("target").get("S").asText(), + "target should receive source's value when source exists"); + } + + @Test + void updateItemSetIfNotExistsUsesFallbackWhenCheckAttrAbsentAndAttrNameDiffers() { + // SET a = if_not_exists(b, :v) where b is absent → a must be set to :v + createUsersTable(); + + // Item has no "source" attribute + service.putItem("Users", item("userId", "u-fallback")); + + ObjectNode key = item("userId", "u-fallback"); + + ObjectNode exprValues = mapper.createObjectNode(); + ObjectNode fallbackVal = mapper.createObjectNode(); fallbackVal.put("S", "fallback"); + exprValues.set(":v", fallbackVal); + + service.updateItem("Users", key, null, + "SET target = if_not_exists(source, :v)", + null, exprValues, null); + + JsonNode stored = service.getItem("Users", key); + assertNotNull(stored); + assertTrue(stored.has("target"), "target attribute must be present"); + assertEquals("fallback", stored.get("target").get("S").asText(), + "target should receive the fallback value when source is absent"); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java index 82a1c246b2..ec27cc05b2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java @@ -6,6 +6,8 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import java.util.Arrays; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.*; @@ -434,7 +436,7 @@ class S3IntegrationTest { } @Test - @Order(15) + @Order(16) void getObjectAttributesRejectsUnknownSelector() { given() .header("x-amz-object-attributes", "ETag,UnknownThing") @@ -446,6 +448,7 @@ class S3IntegrationTest { } @Test + @Order(17) void getNonExistentBucket() { given() .when() @@ -534,4 +537,37 @@ class S3IntegrationTest { .statusCode(400) .body(containsString("InvalidLocationConstraint")); } + + @Test + @Order(20) + void putLargeObject() { + // 22 MB – exceeds the old Jackson 20 MB maxStringLength default + byte[] largeBody = new byte[22 * 1024 * 1024]; + Arrays.fill(largeBody, (byte) 'A'); + + given() + .when() + .put("/large-object-bucket") + .then() + .statusCode(200); + + given() + .contentType("application/octet-stream") + .body(largeBody) + .when() + .put("/large-object-bucket/large-file.bin") + .then() + .statusCode(200) + .header("ETag", notNullValue()); + + given() + .when() + .get("/large-object-bucket/large-file.bin") + .then() + .statusCode(200) + .header("Content-Length", String.valueOf(largeBody.length)); + + given().delete("/large-object-bucket/large-file.bin"); + given().delete("/large-object-bucket"); + } } diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 59467c3d04..b8f3bf49b2 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -2,6 +2,8 @@ quarkus: http: port: 0 test-port: 0 + limits: + max-body-size: ${floci.max-request-size}M floci: base-url: "http://localhost:4566"