Skip to content

Commit c9e66aa

Browse files
authored
[8.3] Ensure CreateApiKey always creates a new document (#88413) (#88415)
* Ensure CreateApiKey always creates a new document (#88413) 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. * fix import
1 parent 927ee8f commit c9e66aa

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

docs/changelog/88413.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 88413
2+
summary: Ensure `CreateApiKey` always creates a new document
3+
area: Security
4+
type: bug
5+
issues: []

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.Version;
1616
import org.elasticsearch.action.ActionListener;
1717
import org.elasticsearch.action.ActionRunnable;
18+
import org.elasticsearch.action.DocWriteRequest;
1819
import org.elasticsearch.action.DocWriteResponse;
1920
import org.elasticsearch.action.bulk.BulkAction;
2021
import org.elasticsearch.action.bulk.BulkItemResponse;
@@ -304,6 +305,7 @@ private void createApiKeyAndIndexIt(
304305
final IndexRequest indexRequest = client.prepareIndex(SECURITY_MAIN_ALIAS)
305306
.setSource(builder)
306307
.setId(request.getId())
308+
.setOpType(DocWriteRequest.OpType.CREATE)
307309
.setRefreshPolicy(request.getRefreshPolicy())
308310
.request();
309311
final BulkRequest bulkRequest = toSingleItemBulkRequest(indexRequest);
@@ -317,6 +319,7 @@ private void createApiKeyAndIndexIt(
317319
bulkRequest,
318320
TransportSingleItemBulkWriteAction.<IndexResponse>wrapBulkResponse(ActionListener.wrap(indexResponse -> {
319321
assert request.getId().equals(indexResponse.getId());
322+
assert indexResponse.getResult() == DocWriteResponse.Result.CREATED;
320323
final ListenableFuture<CachedApiKeyHashResult> listenableFuture = new ListenableFuture<>();
321324
listenableFuture.onResponse(new CachedApiKeyHashResult(true, apiKey));
322325
apiKeyAuthCache.put(request.getId(), listenableFuture);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ public void testCreateApiKeyUsesBulkIndexAction() throws Exception {
206206
assertThat(bulkRequest.requests().get(0), instanceOf(IndexRequest.class));
207207
IndexRequest indexRequest = (IndexRequest) bulkRequest.requests().get(0);
208208
assertThat(indexRequest.id(), is(createApiKeyRequest.getId()));
209+
// The index request has opType create so that it will *not* override any existing document
210+
assertThat(indexRequest.opType(), is(DocWriteRequest.OpType.CREATE));
209211
bulkActionInvoked.set(true);
210212
return null;
211213
}).when(client).execute(eq(BulkAction.INSTANCE), any(BulkRequest.class), any());

0 commit comments

Comments
 (0)