|
@@ -84,6 +84,8 @@ import org.elasticsearch.client.security.PutRoleRequest;
|
|
|
import org.elasticsearch.client.security.PutRoleResponse;
|
|
|
import org.elasticsearch.client.security.PutUserRequest;
|
|
|
import org.elasticsearch.client.security.PutUserResponse;
|
|
|
+import org.elasticsearch.client.security.QueryApiKeyRequest;
|
|
|
+import org.elasticsearch.client.security.QueryApiKeyResponse;
|
|
|
import org.elasticsearch.client.security.RefreshPolicy;
|
|
|
import org.elasticsearch.client.security.TemplateRoleName;
|
|
|
import org.elasticsearch.client.security.support.ApiKey;
|
|
@@ -109,6 +111,10 @@ import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.core.TimeValue;
|
|
|
import org.elasticsearch.common.util.concurrent.ThreadContext;
|
|
|
import org.elasticsearch.common.util.set.Sets;
|
|
|
+import org.elasticsearch.index.query.QueryBuilders;
|
|
|
+import org.elasticsearch.search.searchafter.SearchAfterBuilder;
|
|
|
+import org.elasticsearch.search.sort.FieldSortBuilder;
|
|
|
+import org.elasticsearch.search.sort.SortOrder;
|
|
|
import org.hamcrest.Matchers;
|
|
|
|
|
|
import javax.crypto.SecretKeyFactory;
|
|
@@ -132,7 +138,9 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.TimeoutException;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static org.hamcrest.Matchers.contains;
|
|
@@ -2557,6 +2565,131 @@ public class SecurityDocumentationIT extends ESRestHighLevelClientTestCase {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public void testQueryApiKey() throws IOException, ExecutionException, InterruptedException, TimeoutException {
|
|
|
+ RestHighLevelClient client = highLevelClient();
|
|
|
+ final CreateApiKeyRequest createApiKeyRequest1 = new CreateApiKeyRequest("key-10000", List.of(),
|
|
|
+ randomBoolean() ? TimeValue.timeValueHours(24) : null,
|
|
|
+ RefreshPolicy.WAIT_UNTIL, Map.of("environment", "east-production"));
|
|
|
+ final CreateApiKeyResponse createApiKeyResponse1 = client.security().createApiKey(createApiKeyRequest1, RequestOptions.DEFAULT);
|
|
|
+ final CreateApiKeyRequest createApiKeyRequest2 = new CreateApiKeyRequest("key-20000", List.of(),
|
|
|
+ randomBoolean() ? TimeValue.timeValueHours(24) : null,
|
|
|
+ RefreshPolicy.WAIT_UNTIL, Map.of("environment", "east-staging"));
|
|
|
+ final CreateApiKeyResponse createApiKeyResponse2 = client.security().createApiKey(createApiKeyRequest2, RequestOptions.DEFAULT);
|
|
|
+
|
|
|
+ {
|
|
|
+ // tag::query-api-key-default-request
|
|
|
+ QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest();
|
|
|
+ // end::query-api-key-default-request
|
|
|
+
|
|
|
+ // tag::query-api-key-execute
|
|
|
+ QueryApiKeyResponse queryApiKeyResponse = client.security().queryApiKey(queryApiKeyRequest, RequestOptions.DEFAULT);
|
|
|
+ // end::query-api-key-execute
|
|
|
+
|
|
|
+ assertThat(queryApiKeyResponse.getTotal(), equalTo(2L));
|
|
|
+ assertThat(queryApiKeyResponse.getCount(), equalTo(2));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getName).collect(Collectors.toUnmodifiableSet()),
|
|
|
+ equalTo(Set.of("key-10000", "key-20000")));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getId).collect(Collectors.toUnmodifiableSet()),
|
|
|
+ equalTo(Set.of(createApiKeyResponse1.getId(), createApiKeyResponse2.getId())));
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ // tag::query-api-key-query-request
|
|
|
+ QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest().queryBuilder(
|
|
|
+ QueryBuilders.boolQuery()
|
|
|
+ .must(QueryBuilders.prefixQuery("metadata.environment", "east-"))
|
|
|
+ .mustNot(QueryBuilders.termQuery("name", "key-20000")));
|
|
|
+ // end::query-api-key-query-request
|
|
|
+
|
|
|
+ QueryApiKeyResponse queryApiKeyResponse = client.security().queryApiKey(queryApiKeyRequest, RequestOptions.DEFAULT);
|
|
|
+ assertThat(queryApiKeyResponse.getTotal(), equalTo(1L));
|
|
|
+ assertThat(queryApiKeyResponse.getCount(), equalTo(1));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys().get(0).getName(), equalTo(createApiKeyResponse1.getName()));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys().get(0).getId(), equalTo(createApiKeyResponse1.getId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ // tag::query-api-key-from-size-sort-request
|
|
|
+ QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest()
|
|
|
+ .from(1)
|
|
|
+ .size(100)
|
|
|
+ .fieldSortBuilders(List.of(new FieldSortBuilder("name").order(SortOrder.DESC)));
|
|
|
+ // end::query-api-key-from-size-sort-request
|
|
|
+
|
|
|
+ QueryApiKeyResponse queryApiKeyResponse = client.security().queryApiKey(queryApiKeyRequest, RequestOptions.DEFAULT);
|
|
|
+
|
|
|
+ // tag::query-api-key-from-size-sort-response
|
|
|
+ final long total = queryApiKeyResponse.getTotal(); // <1>
|
|
|
+ final int count = queryApiKeyResponse.getCount(); // <2>
|
|
|
+ final List<ApiKey> apiKeys = queryApiKeyResponse.getApiKeys(); // <3>
|
|
|
+ final Object[] sortValues = apiKeys.get(apiKeys.size()-1).getSortValues(); // <4>
|
|
|
+ // end::query-api-key-from-size-sort-response
|
|
|
+
|
|
|
+ assertThat(total, equalTo(2L));
|
|
|
+ assertThat(count, equalTo(1));
|
|
|
+ assertThat(apiKeys.get(0).getName(), equalTo(createApiKeyResponse1.getName()));
|
|
|
+ assertThat(apiKeys.get(0).getId(), equalTo(createApiKeyResponse1.getId()));
|
|
|
+ assertThat(sortValues.length, equalTo(1));
|
|
|
+ assertThat(sortValues[0], equalTo(createApiKeyResponse1.getName()));
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ // tag::query-api-key-search-after-request
|
|
|
+ QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest()
|
|
|
+ .fieldSortBuilders(List.of(new FieldSortBuilder("name")))
|
|
|
+ .searchAfterBuilder(new SearchAfterBuilder().setSortValues(new String[] {"key-10000"}));
|
|
|
+ // end::query-api-key-search-after-request
|
|
|
+
|
|
|
+ QueryApiKeyResponse queryApiKeyResponse = client.security().queryApiKey(queryApiKeyRequest, RequestOptions.DEFAULT);
|
|
|
+ assertThat(queryApiKeyResponse.getTotal(), equalTo(2L));
|
|
|
+ assertThat(queryApiKeyResponse.getCount(), equalTo(1));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys().get(0).getName(), equalTo(createApiKeyResponse2.getName()));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys().get(0).getId(), equalTo(createApiKeyResponse2.getId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest();
|
|
|
+
|
|
|
+ ActionListener<QueryApiKeyResponse> listener;
|
|
|
+ // tag::query-api-key-execute-listener
|
|
|
+ listener = new ActionListener<QueryApiKeyResponse>() {
|
|
|
+ @Override
|
|
|
+ public void onResponse(QueryApiKeyResponse queryApiKeyResponse) {
|
|
|
+ // <1>
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(Exception e) {
|
|
|
+ // <2>
|
|
|
+ }
|
|
|
+ };
|
|
|
+ // end::query-api-key-execute-listener
|
|
|
+
|
|
|
+ // Avoid unused variable warning
|
|
|
+ assertNotNull(listener);
|
|
|
+
|
|
|
+ // Replace the empty listener by a blocking listener in test
|
|
|
+ final PlainActionFuture<QueryApiKeyResponse> future = new PlainActionFuture<>();
|
|
|
+ listener = future;
|
|
|
+
|
|
|
+ // tag::query-api-key-execute-async
|
|
|
+ client.security().queryApiKeyAsync(queryApiKeyRequest, RequestOptions.DEFAULT, listener); // <1>
|
|
|
+ // end::query-api-key-execute-async
|
|
|
+
|
|
|
+ final QueryApiKeyResponse queryApiKeyResponse = future.get(30, TimeUnit.SECONDS);
|
|
|
+ assertNotNull(queryApiKeyResponse);
|
|
|
+
|
|
|
+ assertThat(queryApiKeyResponse.getTotal(), equalTo(2L));
|
|
|
+ assertThat(queryApiKeyResponse.getCount(), equalTo(2));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys(), is(notNullValue()));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys().size(), is(2));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getName).collect(Collectors.toUnmodifiableSet()),
|
|
|
+ equalTo(Set.of("key-10000", "key-20000")));
|
|
|
+ assertThat(queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getId).collect(Collectors.toUnmodifiableSet()),
|
|
|
+ equalTo(Set.of(createApiKeyResponse1.getId(), createApiKeyResponse2.getId())));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void testGetServiceAccounts() throws IOException {
|
|
|
RestHighLevelClient client = highLevelClient();
|
|
|
{
|