{"record":{"id":"3e243a0e2acda2db","repo":"floci-io/floci","slug":"preconditionfailed","errorCode":"PreconditionFailed","errorMessage":"The precondition in one or more request-header fields evaluated to false.","messagePattern":"The precondition in one or more request-header fields evaluated to false\\.","errorType":"exception","errorClass":"AwsException","httpStatus":412,"severity":"error","filePath":"src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java","lineNumber":950,"sourceCode":"                    \"A public key with this caller reference already exists.\",\n                    409);\n        }\n        key.setId(UUID.randomUUID().toString());\n        key.setCreatedTime(Instant.now());\n        key.setEtag(UUID.randomUUID().toString());\n        publicKeyStore.put(key.getId(), key);\n        return key;\n    }\n\n    public PublicKey getPublicKey(String id) {\n        return publicKeyStore.get(id).orElseThrow(() ->\n                new AwsException(\"NoSuchPublicKey\", \"The specified public key does not exist.\", 404));\n    }\n\n    public synchronized PublicKey updatePublicKey(String id, String ifMatch, PublicKey updated) {\n        PublicKey existing = getPublicKey(id);\n        if (!existing.getEtag().equals(ifMatch)) {\n            throw new AwsException(\n                    \"PreconditionFailed\",\n                    \"The precondition in one or more request-header fields evaluated to false.\",\n                    412);\n        }\n        validatePublicKey(updated);\n        if (!Objects.equals(\n                    existing.getCallerReference(),\n                    updated.getCallerReference())\n                || !Objects.equals(existing.getName(), updated.getName())\n                || !Objects.equals(\n                    existing.getEncodedKey(), updated.getEncodedKey())) {\n            throw new AwsException(\n                    \"CannotChangeImmutablePublicKeyFields\",\n                    \"The caller reference, name, and encoded public key cannot be changed.\",\n                    400);\n        }\n        updated.setId(id);\n        updated.setCreatedTime(existing.getCreatedTime());","sourceCodeStart":932,"sourceCodeEnd":968,"githubUrl":"https://github.com/floci-io/floci/blob/62ff490619e7bd3554597c28c704081b4c15add5/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java#L932-L968","documentation":"CloudFrontService.updatePublicKey throws PreconditionFailed (HTTP 412) when the If-Match header does not equal the key's current ETag. Public keys use ETag-based optimistic concurrency identical to the rest of CloudFront; each successful write rotates the UUID ETag and the next write must present the newest one.","triggerScenarios":"UpdatePublicKey with an ETag captured at creation when a prior update already rotated it; omitting If-Match; concurrent updates to the same key id where the loser's ETag is stale.","commonSituations":"Scripts that update a key's comment twice while caching the create-time ETag; stateful CLI sessions that hold old descriptions after background automation touched the key.","solutions":["GetPublicKey immediately before updating and pass its ETag in If-Match.","Carry the ETag returned by each UpdatePublicKey forward to the next mutation.","Catch PreconditionFailed, refetch the ETag, and retry the update once."],"exampleFix":"// before\nString etag = createResp.eTag();\nupdateKey(id, etag, v1);\nupdateKey(id, etag, v2); // 412\n\n// after\nvar r1 = updateKey(id, createResp.eTag(), v1);\nvar r2 = updateKey(id, r1.eTag(), v2);","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for (int i = 0; i < 3; i++) {\n    String etag = client.getPublicKey(r -> r.id(id)).eTag();\n    try {\n        return client.updatePublicKey(r -> r.id(id).ifMatch(etag).publicKeyConfig(cfg));\n    } catch (PreconditionFailed e) {\n        // etag rotated; refetch and retry\n    }\n}","preventionTips":["Use the ETag from the most recent Get/Update response.","Update only the Comment field to minimize conflict windows.","Keep a single writer per key id."],"tags":["cloudfront","public-key","etag","optimistic-concurrency","aws-emulator"],"backgroundTag":null,"analyzedSha":"62ff490619e7bd3554597c28c704081b4c15add5","analyzedAt":"2026-08-14T14:25:23.764Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}