fix(lambda): check the RevisionId precondition in UpdateFunctionCode (#4641)

* fix(lambda): check the RevisionId precondition in UpdateFunctionCode

Fixes #4639

- M1 PASS: RED run: [ERROR] io.github.hectorvent.floci.services.lambda.LambdaServiceTest.updateFunctionCode_withMismatchedRevisionId_throwsPreconditionFailedException -- Time elapsed: 0.457 s <<< FAILURE!
- M2 PASS: gates green, last summary: [INFO] Tests run: 192, Failures: 0, Errors: 0, Skipped: 0
- M3 PASS: 2 file(s), all in scope
- M4 PASS: no new public members in main code
- M5 PASS: main diff 11 changed lines, limit 25
- M6 PASS: no em-dash in added lines
- A1 PASS: `LambdaService.java:+671-+678` throws `AwsException("PreconditionFailedException", "...", 412)`; `LambdaServiceTest.java:+1780-+1781` asserts exact code, status, and message literals.
- A2 PASS: `LambdaService.java:+671-+678` sits before all `fn.set...` calls; `LambdaServiceTest.java:+1770-+1788` captures `initialRevisionId` before call and asserts `unchangedFn.getRevisionId()` matches it.
- A3 PASS: `LambdaServiceTest.java:+1790-+1794` sends matching RevisionId and asserts update succeeds; Gate 2 output `Tests run: 192, Failures: 0, Errors: 0, Skipped: 0` confirms no regression.
- A4 PASS: `LambdaServiceTest.java:+1779` invokes public `service.updateFunctionCode(...)`; `LambdaServiceTest.java:+1780-+1781` asserts literal error code `"PreconditionFailedException"` and status `412`.
- G1 PASS: `LambdaService.java:+671-+678` directly implements the missing optimistic locking logic identified in the diagnosis, not a test workaround.
- G2 PASS: `LambdaServiceTest.java:+1780-+1781` asserts hardcoded literals for errorCode, httpStatus, and message; `LambdaServiceTest.java:+1788` asserts exact pre-call `initialRevisionId` value.
- G3 PASS: Diff modifies only `LambdaService.java:+671-+678` (a guard clause); Gate 2 shows 0 failures across the full suite, proving other paths are untouched.
- G4 PASS: `LambdaServiceTest.java:+1780-+1781` enforces AWS-correct `PreconditionFailedException` and `412`; `LambdaServiceTest.java:+1788` verifies state is unmutated after rejection.

* refactor(lambda): share the RevisionId precondition check

UpdateFunctionCode and UpdateFunctionConfiguration now call one helper. The stale-RevisionId test also asserts CodeSha256 is unchanged after the rejected call, so it fails if the check moves below code extraction.

* fix(lambda): use the RevisionId mismatch message AWS returns
This commit is contained in:
Sam Fakhreddine authored and GitHub committed 2026-10-07 15:50:23 -04:00
1 parent d0fec13cae
commit b9625bf7f7
2 files changed
+53 -10

No files matched your search

@@ -740,6 +740,8 @@ public class LambdaService implements ResourceProvider {
String s3Bucket = (String) request.get("S3Bucket");
String s3Key = (String) request.get("S3Key");
requireMatchingRevisionId(fn, request);
if (zipFileBase64 != null) {
fn.setS3Bucket(null);
fn.setS3Key(null);
@@ -774,6 +776,22 @@ public class LambdaService implements ResourceProvider {
return fn;
}
/**
* RevisionId optimistic locking: rejects the request with 412 when it carries a RevisionId that
* is not the function's current one. Callers hold the per-function lock and call this before
* mutating {@code fn}.
*/
private static void requireMatchingRevisionId(LambdaFunction fn, Map<String, Object> request) {
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/GetAlias API to retrieve the latest Revision Id", 412);
}
}
}
public LambdaFunction updateFunctionConfiguration(String region, String functionName, Map<String, Object> request) {
// Shape-checked ahead of the lookup, which is where #3051 put these: a malformed member on
// a function that does not exist reports SerializationException, not 404. Splitting the
@@ -863,16 +881,8 @@ 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);
}
}
// Runs after request validation, like AWS, and before any field mutation
requireMatchingRevisionId(fn, request);
if (request.containsKey("Description")) {
fn.setDescription((String) request.get("Description"));
@@ -2323,4 +2323,37 @@ class LambdaServiceTest {
assertEquals("ValidationException", thrown.getErrorCode());
assertEquals(400, thrown.getHttpStatus());
}
@Test
void updateFunctionCode_withMismatchedRevisionId_throwsPreconditionFailedException() throws Exception {
// Catches: UpdateFunctionCode silently accepts a stale RevisionId and overwrites the function's code
// (the CodeSha256 assertion fails if the check ever runs after code extraction)
Map<String, Object> createReq = baseRequest("revision-lock-func");
service.createFunction(REGION, createReq);
LambdaFunction fn = service.getFunction(REGION, "revision-lock-func");
String initialRevisionId = fn.getRevisionId();
String initialCodeSha256 = fn.getCodeSha256();
String mismatchingRevision = "stale-revision-id-abc123";
String zipBase64 = createZipBase64("index.js");
Map<String, Object> updateReq = new HashMap<>();
updateReq.put("ZipFile", zipBase64);
updateReq.put("RevisionId", mismatchingRevision);
AwsException ex = assertThrows(AwsException.class, () -> service.updateFunctionCode(REGION, "revision-lock-func", updateReq));
assertEquals("PreconditionFailedException", ex.getErrorCode());
assertEquals(412, ex.getHttpStatus());
assertEquals("The Revision Id provided does not match the latest Revision Id. Call the GetFunction/GetAlias API to retrieve the latest Revision Id", ex.getMessage());
LambdaFunction unchangedFn = service.getFunction(REGION, "revision-lock-func");
assertEquals(initialRevisionId, unchangedFn.getRevisionId());
assertEquals(initialCodeSha256, unchangedFn.getCodeSha256());
updateReq.put("RevisionId", initialRevisionId);
service.updateFunctionCode(REGION, "revision-lock-func", updateReq);
LambdaFunction updatedFn = service.getFunction(REGION, "revision-lock-func");
assertNotEquals(initialRevisionId, updatedFn.getRevisionId());
}
}