|
11 | 11 | import org.elasticsearch.ElasticsearchSecurityException;
|
12 | 12 | import org.elasticsearch.action.DocWriteResponse;
|
13 | 13 | import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
|
| 14 | +import org.elasticsearch.action.admin.indices.refresh.RefreshAction; |
| 15 | +import org.elasticsearch.action.admin.indices.refresh.RefreshRequestBuilder; |
14 | 16 | import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
|
15 | 17 | import org.elasticsearch.action.support.PlainActionFuture;
|
16 | 18 | import org.elasticsearch.action.support.WriteRequest;
|
|
52 | 54 | import java.util.concurrent.TimeUnit;
|
53 | 55 | import java.util.stream.Collectors;
|
54 | 56 |
|
| 57 | +import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_INDEX_NAME; |
55 | 58 | import static org.hamcrest.Matchers.containsInAnyOrder;
|
56 | 59 | import static org.hamcrest.Matchers.containsString;
|
57 | 60 | import static org.hamcrest.Matchers.equalTo;
|
@@ -518,6 +521,82 @@ public void testGetApiKeysForApiKeyName() throws InterruptedException, Execution
|
518 | 521 | verifyGetResponse(1, responses, response, Collections.singleton(responses.get(0).getId()), null);
|
519 | 522 | }
|
520 | 523 |
|
| 524 | + public void testDerivedKeys() throws ExecutionException, InterruptedException { |
| 525 | + final Client client = client().filterWithHeader(Collections.singletonMap("Authorization", |
| 526 | + UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, |
| 527 | + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING))); |
| 528 | + |
| 529 | + final CreateApiKeyResponse response = new SecurityClient(client) |
| 530 | + .prepareCreateApiKey() |
| 531 | + .setName("key-1") |
| 532 | + .setRoleDescriptors(Collections.singletonList( |
| 533 | + new RoleDescriptor("role", new String[] { "manage_security" }, null, null))) |
| 534 | + .get(); |
| 535 | + |
| 536 | + assertEquals("key-1", response.getName()); |
| 537 | + assertNotNull(response.getId()); |
| 538 | + assertNotNull(response.getKey()); |
| 539 | + |
| 540 | + // use the first ApiKey for authorized action |
| 541 | + final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( |
| 542 | + (response.getId() + ":" + response.getKey().toString()).getBytes(StandardCharsets.UTF_8)); |
| 543 | + final SecurityClient clientKey1 = new SecurityClient( |
| 544 | + client().filterWithHeader(Collections.singletonMap("Authorization", "ApiKey " + base64ApiKeyKeyValue))); |
| 545 | + |
| 546 | + final String expectedMessage = "creating derived api keys requires an explicit role descriptor that is empty"; |
| 547 | + |
| 548 | + final IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, |
| 549 | + () -> clientKey1.prepareCreateApiKey().setName("key-2").get()); |
| 550 | + assertThat(e1.getMessage(), containsString(expectedMessage)); |
| 551 | + |
| 552 | + final IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, |
| 553 | + () -> clientKey1.prepareCreateApiKey().setName("key-3") |
| 554 | + .setRoleDescriptors(Collections.emptyList()).get()); |
| 555 | + assertThat(e2.getMessage(), containsString(expectedMessage)); |
| 556 | + |
| 557 | + final IllegalArgumentException e3 = expectThrows(IllegalArgumentException.class, |
| 558 | + () -> clientKey1.prepareCreateApiKey().setName("key-4") |
| 559 | + .setRoleDescriptors(Collections.singletonList( |
| 560 | + new RoleDescriptor("role", new String[] {"manage_security"}, null, null) |
| 561 | + )).get()); |
| 562 | + assertThat(e3.getMessage(), containsString(expectedMessage)); |
| 563 | + |
| 564 | + final List<RoleDescriptor> roleDescriptors = new ArrayList<>(); |
| 565 | + for (int i = 0; i < randomIntBetween(2, 10); i++) { |
| 566 | + roleDescriptors.add(new RoleDescriptor("role", null, null, null)); |
| 567 | + } |
| 568 | + roleDescriptors.set(randomInt(roleDescriptors.size() - 1), |
| 569 | + new RoleDescriptor("role", new String[] {"manage_security"}, null, null)); |
| 570 | + |
| 571 | + final IllegalArgumentException e4 = expectThrows(IllegalArgumentException.class, |
| 572 | + () -> clientKey1.prepareCreateApiKey().setName("key-5") |
| 573 | + .setRoleDescriptors(roleDescriptors).get()); |
| 574 | + assertThat(e4.getMessage(), containsString(expectedMessage)); |
| 575 | + |
| 576 | + final CreateApiKeyResponse key100Response = clientKey1.prepareCreateApiKey().setName("key-100") |
| 577 | + .setRoleDescriptors(Collections.singletonList( |
| 578 | + new RoleDescriptor("role", null, null, null) |
| 579 | + )).get(); |
| 580 | + assertEquals("key-100", key100Response.getName()); |
| 581 | + assertNotNull(key100Response.getId()); |
| 582 | + assertNotNull(key100Response.getKey()); |
| 583 | + |
| 584 | + // Check at the end to allow sometime for the operation to happen. Since an erroneous creation is |
| 585 | + // asynchronous so that the document is not available immediately. |
| 586 | + assertApiKeyNotCreated(client,"key-2"); |
| 587 | + assertApiKeyNotCreated(client,"key-3"); |
| 588 | + assertApiKeyNotCreated(client,"key-4"); |
| 589 | + assertApiKeyNotCreated(client,"key-5"); |
| 590 | + } |
| 591 | + |
| 592 | + private void assertApiKeyNotCreated(Client client, String keyName) throws ExecutionException, InterruptedException { |
| 593 | + new RefreshRequestBuilder(client, RefreshAction.INSTANCE).setIndices(SECURITY_INDEX_NAME).execute().get(); |
| 594 | + PlainActionFuture<GetApiKeyResponse> getApiKeyResponseListener = new PlainActionFuture<>(); |
| 595 | + new SecurityClient(client).getApiKey( |
| 596 | + GetApiKeyRequest.usingApiKeyName(keyName), getApiKeyResponseListener); |
| 597 | + assertEquals(0, getApiKeyResponseListener.get().getApiKeyInfos().length); |
| 598 | + } |
| 599 | + |
521 | 600 | private void verifyGetResponse(int noOfApiKeys, List<CreateApiKeyResponse> responses, GetApiKeyResponse response,
|
522 | 601 | Set<String> validApiKeyIds,
|
523 | 602 | List<String> invalidatedApiKeyIds) {
|
|
0 commit comments