|
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; |
@@ -518,6 +520,80 @@ public void testGetApiKeysForApiKeyName() throws InterruptedException, Execution |
518 | 520 | verifyGetResponse(1, responses, response, Collections.singleton(responses.get(0).getId()), null); |
519 | 521 | } |
520 | 522 |
|
| 523 | + public void testDerivedKeys() throws ExecutionException, InterruptedException { |
| 524 | + final Client client = client().filterWithHeader(Collections.singletonMap("Authorization", |
| 525 | + UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, |
| 526 | + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING))); |
| 527 | + |
| 528 | + final CreateApiKeyResponse response = new SecurityClient(client) |
| 529 | + .prepareCreateApiKey() |
| 530 | + .setName("key-1") |
| 531 | + .setRoleDescriptors(Collections.singletonList( |
| 532 | + new RoleDescriptor("role", new String[] { "manage_api_key" }, null, null))) |
| 533 | + .get(); |
| 534 | + |
| 535 | + assertEquals("key-1", response.getName()); |
| 536 | + assertNotNull(response.getId()); |
| 537 | + assertNotNull(response.getKey()); |
| 538 | + |
| 539 | + // use the first ApiKey for authorized action |
| 540 | + final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( |
| 541 | + (response.getId() + ":" + response.getKey().toString()).getBytes(StandardCharsets.UTF_8)); |
| 542 | + final SecurityClient clientKey1 = new SecurityClient( |
| 543 | + client().filterWithHeader(Collections.singletonMap("Authorization", "ApiKey " + base64ApiKeyKeyValue))); |
| 544 | + |
| 545 | + final String expectedMessage = "creating derived api keys requires an explicit role descriptor that is empty"; |
| 546 | + |
| 547 | + final IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, |
| 548 | + () -> clientKey1.prepareCreateApiKey().setName("key-2").get()); |
| 549 | + assertThat(e1.getMessage(), containsString(expectedMessage)); |
| 550 | + |
| 551 | + final IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, |
| 552 | + () -> clientKey1.prepareCreateApiKey().setName("key-3") |
| 553 | + .setRoleDescriptors(Collections.emptyList()).get()); |
| 554 | + assertThat(e2.getMessage(), containsString(expectedMessage)); |
| 555 | + |
| 556 | + final IllegalArgumentException e3 = expectThrows(IllegalArgumentException.class, |
| 557 | + () -> clientKey1.prepareCreateApiKey().setName("key-4") |
| 558 | + .setRoleDescriptors(Collections.singletonList( |
| 559 | + new RoleDescriptor("role", new String[] {"manage_own_api_key"}, null, null) |
| 560 | + )).get()); |
| 561 | + assertThat(e3.getMessage(), containsString(expectedMessage)); |
| 562 | + |
| 563 | + final List<RoleDescriptor> roleDescriptors = randomList(2, 10, |
| 564 | + () -> new RoleDescriptor("role", null, null, null)); |
| 565 | + roleDescriptors.set(randomInt(roleDescriptors.size() - 1), |
| 566 | + new RoleDescriptor("role", new String[] {"manage_own_api_key"}, null, null)); |
| 567 | + |
| 568 | + final IllegalArgumentException e4 = expectThrows(IllegalArgumentException.class, |
| 569 | + () -> clientKey1.prepareCreateApiKey().setName("key-5") |
| 570 | + .setRoleDescriptors(roleDescriptors).get()); |
| 571 | + assertThat(e4.getMessage(), containsString(expectedMessage)); |
| 572 | + |
| 573 | + final CreateApiKeyResponse key100Response = clientKey1.prepareCreateApiKey().setName("key-100") |
| 574 | + .setRoleDescriptors(Collections.singletonList( |
| 575 | + new RoleDescriptor("role", null, null, null) |
| 576 | + )).get(); |
| 577 | + assertEquals("key-100", key100Response.getName()); |
| 578 | + assertNotNull(key100Response.getId()); |
| 579 | + assertNotNull(key100Response.getKey()); |
| 580 | + |
| 581 | + // Check at the end to allow sometime for the operation to happen. Since an erroneous creation is |
| 582 | + // asynchronous so that the document is not available immediately. |
| 583 | + assertApiKeyNotCreated(client,"key-2"); |
| 584 | + assertApiKeyNotCreated(client,"key-3"); |
| 585 | + assertApiKeyNotCreated(client,"key-4"); |
| 586 | + assertApiKeyNotCreated(client,"key-5"); |
| 587 | + } |
| 588 | + |
| 589 | + private void assertApiKeyNotCreated(Client client, String keyName) throws ExecutionException, InterruptedException { |
| 590 | + new RefreshRequestBuilder(client, RefreshAction.INSTANCE).setIndices(SECURITY_MAIN_ALIAS).execute().get(); |
| 591 | + PlainActionFuture<GetApiKeyResponse> getApiKeyResponseListener = new PlainActionFuture<>(); |
| 592 | + new SecurityClient(client).getApiKey( |
| 593 | + GetApiKeyRequest.usingApiKeyName(keyName, false), getApiKeyResponseListener); |
| 594 | + assertEquals(0, getApiKeyResponseListener.get().getApiKeyInfos().length); |
| 595 | + } |
| 596 | + |
521 | 597 | private void verifyGetResponse(int noOfApiKeys, List<CreateApiKeyResponse> responses, GetApiKeyResponse response, |
522 | 598 | Set<String> validApiKeyIds, |
523 | 599 | List<String> invalidatedApiKeyIds) { |
|
0 commit comments