From a8d08d2e23fbae155cf3105136fef508ba4e8600 Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Mon, 11 Jul 2022 13:26:15 +1000 Subject: [PATCH 1/2] Ensure CreateApiKey always creates a new document The OpType of the indexRequest used for creating new API keys does not have its OpType configured. This means it defaults to OpType.INDEX which allows it to replace an existing document. This PR fixes it by explicity set OpType to CREATE so that it always create a new document (or throw error if ID conflict does happen). Since API key ID is time-based random base64 UUID, it is unlikely for this to happen in practice and we are not aware of any related bug report. --- .../org/elasticsearch/xpack/security/authc/ApiKeyService.java | 2 ++ .../elasticsearch/xpack/security/authc/ApiKeyServiceTests.java | 2 ++ 2 files changed, 4 insertions(+) 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 ce771df7dfc25..fe30462e9b8d6 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 @@ -325,6 +325,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); @@ -338,6 +339,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 1d16d28d99aa3..181dcf8211283 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 @@ -209,6 +209,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()); From 8cb8d439dc0af143e479f43c8a36ed253ff0e715 Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Mon, 11 Jul 2022 13:32:48 +1000 Subject: [PATCH 2/2] Update docs/changelog/88413.yaml --- docs/changelog/88413.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/changelog/88413.yaml 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: []