Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(lambda): check RevisionId before applying UpdateFunctionConfiguration changes (#4637)

Sam Fakhreddined4b7265b49cb
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
index 56f216974f..97fd19f0ab 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
@@ -863,6 +863,17 @@ public class LambdaService implements ResourceProvider {
validateFileSystemVpcConfig(requestedFileSystemConfigs, requestedVpcConfig);
}
+ // RevisionId optimistic locking runs after request validation, like AWS, and before any field mutation
+ if (request.containsKey("RevisionId")) {
+ String incomingRevision = (String) request.get("RevisionId");
+ if (incomingRevision != null && !incomingRevision.equals(fn.getRevisionId())) {
+ throw new AwsException("PreconditionFailedException",
+ "The Revision Id provided does not match the latest Revision Id. "
+ + "Call the GetFunction or the GetFunctionConfiguration API to retrieve "
+ + "the latest Revision Id for your resource.", 412);
+ }
+ }
+
if (request.containsKey("Description")) {
fn.setDescription((String) request.get("Description"));
}
@@ -887,17 +898,6 @@ public class LambdaService implements ResourceProvider {
}
}
- // RevisionId optimistic locking
- if (request.containsKey("RevisionId")) {
- String incomingRevision = (String) request.get("RevisionId");
- if (incomingRevision != null && !incomingRevision.equals(fn.getRevisionId())) {
- throw new AwsException("PreconditionFailedException",
- "The Revision Id provided does not match the latest Revision Id. "
- + "Call the GetFunction or the GetFunctionConfiguration API to retrieve "
- + "the latest Revision Id for your resource.", 412);
- }
- }
-
if (architectures != null) {
fn.setArchitectures(new ArrayList<>(architectures));
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java
index b6ed7c6401..21b1bb5b80 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java
@@ -2286,4 +2286,41 @@ class LambdaServiceTest {
assertEquals("ResourceNotFoundException",
assertThrows(AwsException.class, () -> service.getFunction(REGION, "package-type-fn")).getErrorCode());
}
+
+ @Test
+ void updateFunctionConfigurationWithStaleRevisionIdRollsBack() {
+ // Catches: stale RevisionId rejection leaves rejected changes (Timeout) visible afterwards
+ Map<String, Object> request = baseRequest("rollback-test-function");
+ LambdaFunction fn = service.createFunction(REGION, request);
+ String originalRevisionId = fn.getRevisionId();
+
+ Map<String, Object> updateRequest = Map.of(
+ "RevisionId", "stale-id",
+ "Timeout", 99);
+
+ AwsException thrown = assertThrows(AwsException.class, () -> {
+ service.updateFunctionConfiguration(REGION, "rollback-test-function", updateRequest);
+ });
+ assertEquals("PreconditionFailedException", thrown.getErrorCode());
+ assertEquals(412, thrown.getHttpStatus());
+
+ LambdaFunction updatedFn = service.getFunction(REGION, "rollback-test-function");
+ assertEquals(originalRevisionId, updatedFn.getRevisionId());
+ assertEquals(10, updatedFn.getTimeout());
+ }
+
+ @Test
+ void updateFunctionConfigurationValidatesFieldsBeforeCheckingRevisionId() {
+ // Catches: a stale RevisionId answers 412 ahead of the ValidationException an over-long Description earns
+ service.createFunction(REGION, baseRequest("validation-order-function"));
+
+ Map<String, Object> updateRequest = Map.of(
+ "RevisionId", "stale-id",
+ "Description", "d".repeat(300));
+
+ AwsException thrown = assertThrows(AwsException.class,
+ () -> service.updateFunctionConfiguration(REGION, "validation-order-function", updateRequest));
+ assertEquals("ValidationException", thrown.getErrorCode());
+ assertEquals(400, thrown.getHttpStatus());
+ }
}