Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/88413.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 88413
summary: Ensure `CreateApiKey` always creates a new document
area: Security
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRunnable;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkAction;
import org.elasticsearch.action.bulk.BulkItemResponse;
Expand Down Expand Up @@ -304,6 +305,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);
Expand All @@ -317,6 +319,7 @@ private void createApiKeyAndIndexIt(
bulkRequest,
TransportSingleItemBulkWriteAction.<IndexResponse>wrapBulkResponse(ActionListener.wrap(indexResponse -> {
assert request.getId().equals(indexResponse.getId());
assert indexResponse.getResult() == DocWriteResponse.Result.CREATED;
final ListenableFuture<CachedApiKeyHashResult> listenableFuture = new ListenableFuture<>();
listenableFuture.onResponse(new CachedApiKeyHashResult(true, apiKey));
apiKeyAuthCache.put(request.getId(), listenableFuture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down