{"record":{"id":"f18d625c86d428b3","repo":"floci-io/floci","slug":"publickeyalreadyexists","errorCode":"PublicKeyAlreadyExists","errorMessage":"A public key with this caller reference already exists.","messagePattern":"A public key with this caller reference already exists\\.","errorType":"exception","errorClass":"AwsException","httpStatus":409,"severity":"error","filePath":"src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java","lineNumber":930,"sourceCode":"        copy.setConfig(primary.getConfig());\n        if (copy.getConfig() != null) {\n            copy.getConfig().setCallerReference(callerReference);\n            copy.getConfig().setStaging(true);\n        }\n        return createDistribution(copy, tags);\n    }\n\n    // ── Public Keys ───────────────────────────────────────────────────────────\n\n    public synchronized PublicKey createPublicKey(PublicKey key) {\n        validatePublicKey(key);\n        boolean duplicateCallerReference =\n                publicKeyStore.scan(existing -> true).stream()\n                        .anyMatch(existing -> Objects.equals(\n                                existing.getCallerReference(),\n                                key.getCallerReference()));\n        if (duplicateCallerReference) {\n            throw new AwsException(\n                    \"PublicKeyAlreadyExists\",\n                    \"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);","sourceCodeStart":912,"sourceCodeEnd":948,"githubUrl":"https://github.com/floci-io/floci/blob/62ff490619e7bd3554597c28c704081b4c15add5/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java#L912-L948","documentation":"CloudFrontService.createPublicKey throws PublicKeyAlreadyExists (HTTP 409) when any stored public key already carries the same CallerReference. CallerReference is CloudFront's idempotency token for key creation, and this emulator treats a reused token as a conflict rather than returning the original resource.","triggerScenarios":"CreatePublicKey twice with the same CallerReference string — e.g. a fixed token like \"key-1\" hardcoded in config, or a retry after a timeout that resends the same token after the first create actually succeeded.","commonSituations":"Hardcoded CallerReferences in templates or test fixtures; retries of flaky requests where the first attempt succeeded; copy-pasted JSON payloads between environments pointed at the same emulator instance.","solutions":["Generate a fresh unique CallerReference (UUID) for every CreatePublicKey call.","If this is a retry after an uncertain failure, first check ListPublicKeys/GetPublicKey for the earlier creation before re-submitting.","Parameterize fixtures so each test run mints its own token instead of reusing a constant."],"exampleFix":"// before\nkey.setCallerReference(\"fixed-token\");\ncreatePublicKey(key); // ok\ncreatePublicKey(key); // 409 PublicKeyAlreadyExists\n\n// after\nkey.setCallerReference(UUID.randomUUID().toString());\ncreatePublicKey(key);","handlingStrategy":"validation","validationCode":"boolean callerReferenceInUse(CloudFrontClient client, String ref) {\n    return client.listPublicKeys(r -> r.build()).publicKeyList().items().stream()\n            .anyMatch(k -> ref.equals(k.callerReference()));\n}\n\n// before create:\nif (callerReferenceInUse(client, ref)) throw new IllegalStateException(\"caller reference already used\");","typeGuard":null,"tryCatchPattern":"try {\n    return client.createPublicKey(r -> r.publicKeyConfig(cfg));\n} catch (PublicKeyAlreadyExists e) {\n    // this caller reference already created a key; look it up and reuse it\n    return client.listPublicKeys(r -> r.build()).publicKeyList().items().stream()\n            .filter(k -> ref.equals(k.callerReference())).findFirst()\n            .orElseThrow(() -> e);\n}","preventionTips":["Mint a fresh UUID CallerReference for every create call.","On retry after a network error, check whether the first attempt already succeeded.","Never hardcode caller references in shared fixtures."],"tags":["cloudfront","public-key","idempotency","conflict","aws-emulator"],"backgroundTag":null,"analyzedSha":"62ff490619e7bd3554597c28c704081b4c15add5","analyzedAt":"2026-08-14T14:25:23.764Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}