diff --git a/docs/changelog/88413.yaml b/docs/changelog/88413.yaml new file mode 100644 index 0000000000000..5b2a172b1ba6e --- /dev/null +++ b/docs/changelog/88413.yaml @@ -0,0 +1,5 @@ +pr: 88413 +summary: Ensure `CreateApiKey` always creates a new document +area: Security +type: bug +issues: [] diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java index 447ff9191ab6f..96b7ad03dc042 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java @@ -304,6 +304,7 @@ private void createApiKeyAndIndexIt( final IndexRequest indexRequest = client.prepareIndex(SECURITY_MAIN_ALIAS) .setSource(builder) .setId(request.getId()) + .setOpType(DocWriteRequest.OpType.CREATE) .setRefreshPolicy(request.getRefreshPolicy()) .request(); final BulkRequest bulkRequest = toSingleItemBulkRequest(indexRequest); @@ -317,6 +318,7 @@ private void createApiKeyAndIndexIt( bulkRequest, TransportSingleItemBulkWriteAction.wrapBulkResponse(ActionListener.wrap(indexResponse -> { assert request.getId().equals(indexResponse.getId()); + assert indexResponse.getResult() == DocWriteResponse.Result.CREATED; final ListenableFuture listenableFuture = new ListenableFuture<>(); listenableFuture.onResponse(new CachedApiKeyHashResult(true, apiKey)); apiKeyAuthCache.put(request.getId(), listenableFuture); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java index 338dea27b9936..c67ce159250bc 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java @@ -206,6 +206,8 @@ public void testCreateApiKeyUsesBulkIndexAction() throws Exception { assertThat(bulkRequest.requests().get(0), instanceOf(IndexRequest.class)); IndexRequest indexRequest = (IndexRequest) bulkRequest.requests().get(0); assertThat(indexRequest.id(), is(createApiKeyRequest.getId())); + // The index request has opType create so that it will *not* override any existing document + assertThat(indexRequest.opType(), is(DocWriteRequest.OpType.CREATE)); bulkActionInvoked.set(true); return null; }).when(client).execute(eq(BulkAction.INSTANCE), any(BulkRequest.class), any());