Skip to content

Commit d84886e

Browse files
committed
code review feedback + test
1 parent 402b118 commit d84886e

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/apikey/TransportCreateApiKeyAction.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ protected void doExecute(Task task, CreateApiKeyRequest request, ActionListener<
6363
);
6464
return;
6565
}
66-
if (authentication.isCloudApiKey()) {
67-
listener.onFailure(new IllegalArgumentException("creating elasticsearch api keys using cloud api keys is not supported"));
68-
return;
69-
}
7066
resolver.resolveUserRoleDescriptors(
7167
authentication,
7268
ActionListener.wrap(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ public void createApiKey(
368368
ensureEnabled();
369369
if (authentication == null) {
370370
listener.onFailure(new IllegalArgumentException("authentication must be provided"));
371+
} else if (authentication.isCloudApiKey()) {
372+
listener.onFailure(new IllegalArgumentException("creating elasticsearch api keys using cloud api keys is not supported"));
371373
} else {
372374
final TransportVersion transportVersion = getMinTransportVersion();
373375
if (validateRoleDescriptorsForMixedCluster(listener, request.getRoleDescriptors(), transportVersion) == false) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
import org.elasticsearch.xpack.security.test.SecurityMocks;
126126
import org.junit.After;
127127
import org.junit.Before;
128+
import org.junit.Test;
128129
import org.mockito.ArgumentMatcher;
129130
import org.mockito.Mockito;
130131

@@ -2557,6 +2558,25 @@ public void testCreationWillFailIfHashingThreadPoolIsSaturated() {
25572558
assertThat(e, is(rejectedExecutionException));
25582559
}
25592560

2561+
@Test
2562+
public void testCreationFailsIfAuthenticationIsCloudApiKey() throws InterruptedException {
2563+
final Authentication authentication = AuthenticationTestHelper.randomCloudApiKeyAuthentication();
2564+
final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest(randomAlphaOfLengthBetween(3, 8), null, null);
2565+
ApiKeyService service = createApiKeyService(Settings.EMPTY);
2566+
final PlainActionFuture<CreateApiKeyResponse> future = new PlainActionFuture<>();
2567+
service.createApiKey(authentication, createApiKeyRequest, Set.of(), future);
2568+
assertEquals(true, future.isDone());
2569+
assertThrows(ExecutionException.class, future::get);
2570+
try {
2571+
future.get();
2572+
} catch (ExecutionException ex) {
2573+
assertEquals(
2574+
"java.lang.IllegalArgumentException: creating elasticsearch api keys using cloud api keys is not supported",
2575+
ex.getMessage()
2576+
);
2577+
}
2578+
}
2579+
25602580
public void testCachedApiKeyValidationWillNotBeBlockedByUnCachedApiKey() throws IOException, ExecutionException, InterruptedException {
25612581
final String apiKeyId1 = randomAlphaOfLength(12);
25622582
final String apiKey1 = randomAlphaOfLength(16);

0 commit comments

Comments
 (0)