|
10 | 10 | import org.elasticsearch.ElasticsearchSecurityException; |
11 | 11 | import org.elasticsearch.action.DocWriteResponse; |
12 | 12 | import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; |
| 13 | +import org.elasticsearch.action.admin.indices.refresh.RefreshAction; |
| 14 | +import org.elasticsearch.action.admin.indices.refresh.RefreshRequestBuilder; |
13 | 15 | import org.elasticsearch.action.admin.indices.refresh.RefreshResponse; |
14 | 16 | import org.elasticsearch.action.support.PlainActionFuture; |
15 | 17 | import org.elasticsearch.action.support.WriteRequest; |
@@ -771,6 +773,80 @@ public void testApiKeyWithManageOwnPrivilegeIsAbleToInvalidateItselfButNotAnyOth |
771 | 773 | assertThat(invalidateResponse.getErrors().size(), equalTo(0)); |
772 | 774 | } |
773 | 775 |
|
| 776 | + public void testDerivedKeys() throws ExecutionException, InterruptedException { |
| 777 | + final Client client = client().filterWithHeader(Collections.singletonMap("Authorization", |
| 778 | + UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, |
| 779 | + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING))); |
| 780 | + |
| 781 | + final CreateApiKeyResponse response = new SecurityClient(client) |
| 782 | + .prepareCreateApiKey() |
| 783 | + .setName("key-1") |
| 784 | + .setRoleDescriptors(Collections.singletonList( |
| 785 | + new RoleDescriptor("role", new String[] { "manage_api_key" }, null, null))) |
| 786 | + .get(); |
| 787 | + |
| 788 | + assertEquals("key-1", response.getName()); |
| 789 | + assertNotNull(response.getId()); |
| 790 | + assertNotNull(response.getKey()); |
| 791 | + |
| 792 | + // use the first ApiKey for authorized action |
| 793 | + final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( |
| 794 | + (response.getId() + ":" + response.getKey().toString()).getBytes(StandardCharsets.UTF_8)); |
| 795 | + final SecurityClient clientKey1 = new SecurityClient( |
| 796 | + client().filterWithHeader(Collections.singletonMap("Authorization", "ApiKey " + base64ApiKeyKeyValue))); |
| 797 | + |
| 798 | + final String expectedMessage = "creating derived api keys requires an explicit role descriptor that is empty"; |
| 799 | + |
| 800 | + final IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, |
| 801 | + () -> clientKey1.prepareCreateApiKey().setName("key-2").get()); |
| 802 | + assertThat(e1.getMessage(), containsString(expectedMessage)); |
| 803 | + |
| 804 | + final IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, |
| 805 | + () -> clientKey1.prepareCreateApiKey().setName("key-3") |
| 806 | + .setRoleDescriptors(Collections.emptyList()).get()); |
| 807 | + assertThat(e2.getMessage(), containsString(expectedMessage)); |
| 808 | + |
| 809 | + final IllegalArgumentException e3 = expectThrows(IllegalArgumentException.class, |
| 810 | + () -> clientKey1.prepareCreateApiKey().setName("key-4") |
| 811 | + .setRoleDescriptors(Collections.singletonList( |
| 812 | + new RoleDescriptor("role", new String[] {"manage_own_api_key"}, null, null) |
| 813 | + )).get()); |
| 814 | + assertThat(e3.getMessage(), containsString(expectedMessage)); |
| 815 | + |
| 816 | + final List<RoleDescriptor> roleDescriptors = randomList(2, 10, |
| 817 | + () -> new RoleDescriptor("role", null, null, null)); |
| 818 | + roleDescriptors.set(randomInt(roleDescriptors.size() - 1), |
| 819 | + new RoleDescriptor("role", new String[] {"manage_own_api_key"}, null, null)); |
| 820 | + |
| 821 | + final IllegalArgumentException e4 = expectThrows(IllegalArgumentException.class, |
| 822 | + () -> clientKey1.prepareCreateApiKey().setName("key-5") |
| 823 | + .setRoleDescriptors(roleDescriptors).get()); |
| 824 | + assertThat(e4.getMessage(), containsString(expectedMessage)); |
| 825 | + |
| 826 | + final CreateApiKeyResponse key100Response = clientKey1.prepareCreateApiKey().setName("key-100") |
| 827 | + .setRoleDescriptors(Collections.singletonList( |
| 828 | + new RoleDescriptor("role", null, null, null) |
| 829 | + )).get(); |
| 830 | + assertEquals("key-100", key100Response.getName()); |
| 831 | + assertNotNull(key100Response.getId()); |
| 832 | + assertNotNull(key100Response.getKey()); |
| 833 | + |
| 834 | + // Check at the end to allow sometime for the operation to happen. Since an erroneous creation is |
| 835 | + // asynchronous so that the document is not available immediately. |
| 836 | + assertApiKeyNotCreated(client,"key-2"); |
| 837 | + assertApiKeyNotCreated(client,"key-3"); |
| 838 | + assertApiKeyNotCreated(client,"key-4"); |
| 839 | + assertApiKeyNotCreated(client,"key-5"); |
| 840 | + } |
| 841 | + |
| 842 | + private void assertApiKeyNotCreated(Client client, String keyName) throws ExecutionException, InterruptedException { |
| 843 | + new RefreshRequestBuilder(client, RefreshAction.INSTANCE).setIndices(SECURITY_MAIN_ALIAS).execute().get(); |
| 844 | + PlainActionFuture<GetApiKeyResponse> getApiKeyResponseListener = new PlainActionFuture<>(); |
| 845 | + new SecurityClient(client).getApiKey( |
| 846 | + GetApiKeyRequest.usingApiKeyName(keyName, false), getApiKeyResponseListener); |
| 847 | + assertEquals(0, getApiKeyResponseListener.get().getApiKeyInfos().length); |
| 848 | + } |
| 849 | + |
774 | 850 | private void verifyGetResponse(int expectedNumberOfApiKeys, List<CreateApiKeyResponse> responses, |
775 | 851 | GetApiKeyResponse response, Set<String> validApiKeyIds, List<String> invalidatedApiKeyIds) { |
776 | 852 | verifyGetResponse(SecuritySettingsSource.TEST_SUPERUSER, expectedNumberOfApiKeys, responses, response, validApiKeyIds, |
|
0 commit comments