From 396e65c0d767342675ee83aaa10e82f332c58967 Mon Sep 17 00:00:00 2001 From: iverase Date: Tue, 6 Nov 2018 12:57:37 +0100 Subject: [PATCH 01/17] HLRC: Add delete user action It adds delete user action to the high level rest client. Relates #29827 --- .../elasticsearch/client/SecurityClient.java | 29 +++++++ .../client/SecurityRequestConverters.java | 12 +++ .../client/security/DeleteUserRequest.java | 71 +++++++++++++++++ .../client/security/DeleteUserResponse.java | 55 ++++++++++++++ .../org/elasticsearch/client/SecurityIT.java | 12 ++- .../SecurityRequestConvertersTests.java | 13 ++++ .../SecurityDocumentationIT.java | 62 +++++++++++++++ .../security/DeleteUserRequestTests.java | 76 +++++++++++++++++++ .../security/DeleteUserResponseTests.java | 59 ++++++++++++++ .../high-level/security/delete-user.asciidoc | 56 ++++++++++++++ .../high-level/supported-apis.asciidoc | 2 + 11 files changed, 444 insertions(+), 3 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java create mode 100644 docs/java-rest/high-level/security/delete-user.asciidoc diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java index aee6eb5efccd5..5337885edb866 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java @@ -31,6 +31,8 @@ import org.elasticsearch.client.security.DeleteRoleMappingResponse; import org.elasticsearch.client.security.DeleteRoleRequest; import org.elasticsearch.client.security.DeleteRoleResponse; +import org.elasticsearch.client.security.DeleteUserRequest; +import org.elasticsearch.client.security.DeleteUserResponse; import org.elasticsearch.client.security.DisableUserRequest; import org.elasticsearch.client.security.EmptyResponse; import org.elasticsearch.client.security.EnableUserRequest; @@ -92,6 +94,33 @@ public void putUserAsync(PutUserRequest request, RequestOptions options, ActionL PutUserResponse::fromXContent, listener, emptySet()); } + /** + * Removes user from the native realm synchronously. + * See + * the docs for more. + * @param request the request with the user to delete + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response from the delete user call + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public DeleteUserResponse deleteUser(DeleteUserRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deleteUser, options, + DeleteUserResponse::fromXContent, singleton(404)); + } + + /** + * Asynchronously deletes a user in the native realm. + * See + * the docs for more. + * @param request the request with the user to delete + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + */ + public void deleteUserAsync(DeleteUserRequest request, RequestOptions options, ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::deleteUser, options, + DeleteUserResponse::fromXContent, listener, singleton(404)); + } + /** * Create/Update a role mapping. * See diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java index c8e3fe2b04dfb..eeb3ccdbe7168 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java @@ -27,6 +27,7 @@ import org.elasticsearch.client.security.CreateTokenRequest; import org.elasticsearch.client.security.DeleteRoleMappingRequest; import org.elasticsearch.client.security.DeleteRoleRequest; +import org.elasticsearch.client.security.DeleteUserRequest; import org.elasticsearch.client.security.InvalidateTokenRequest; import org.elasticsearch.client.security.PutRoleMappingRequest; import org.elasticsearch.client.security.DisableUserRequest; @@ -71,6 +72,17 @@ static Request putUser(PutUserRequest putUserRequest) throws IOException { return request; } + static Request deleteUser(DeleteUserRequest deleteUserRequest) { + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_xpack/security/user") + .addPathPart(deleteUserRequest.getName()) + .build(); + Request request = new Request(HttpDelete.METHOD_NAME, endpoint); + RequestConverters.Params params = new RequestConverters.Params(request); + params.withRefreshPolicy(deleteUserRequest.getRefreshPolicy()); + return request; + } + static Request putRoleMapping(final PutRoleMappingRequest putRoleMappingRequest) throws IOException { final String endpoint = new RequestConverters.EndpointBuilder() .addPathPartAsIs("_xpack/security/role_mapping") diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java new file mode 100644 index 0000000000000..f5c943b8c41d9 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java @@ -0,0 +1,71 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client.security; + +import org.elasticsearch.client.Validatable; + +import java.util.Objects; + +/** + * A request delete a role from the security index + */ +public final class DeleteUserRequest implements Validatable { + + private final String name; + private final RefreshPolicy refreshPolicy; + + public DeleteUserRequest(String name) { + this(name, RefreshPolicy.IMMEDIATE); + } + + public DeleteUserRequest(String name, RefreshPolicy refreshPolicy) { + this.name = Objects.requireNonNull(name, "user name is required"); + this.refreshPolicy = Objects.requireNonNull(refreshPolicy, "refresh policy is required"); + } + + public String getName() { + return name; + } + + public RefreshPolicy getRefreshPolicy() { + return refreshPolicy; + } + + @Override + public int hashCode() { + return Objects.hash(name, refreshPolicy); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final DeleteUserRequest other = (DeleteUserRequest) obj; + + return (refreshPolicy == other.refreshPolicy) && Objects.equals(name, other.name); + } +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java new file mode 100644 index 0000000000000..78258c395262e --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -0,0 +1,55 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client.security; + +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; + +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; + +/** + * Response for a role being deleted from the native realm + */ +public final class DeleteUserResponse { + + private final boolean found; + + public DeleteUserResponse(boolean found) { + this.found = found; + } + + public boolean isFound() { + return this.found; + } + + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("delete_user_response", + true, args -> new DeleteUserResponse((boolean) args[0])); + + static { + PARSER.declareBoolean(constructorArg(), new ParseField("found")); + } + + public static DeleteUserResponse fromXContent(XContentParser parser) throws IOException { + return PARSER.parse(parser, null); + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java index 74a4d58e2bf77..f20872b54d937 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java @@ -19,9 +19,10 @@ package org.elasticsearch.client; -import org.apache.http.client.methods.HttpDelete; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.client.security.AuthenticateResponse; +import org.elasticsearch.client.security.DeleteUserRequest; +import org.elasticsearch.client.security.DeleteUserResponse; import org.elasticsearch.client.security.PutUserRequest; import org.elasticsearch.client.security.PutUserResponse; import org.elasticsearch.client.security.RefreshPolicy; @@ -64,13 +65,18 @@ public void testAuthenticate() throws Exception { assertThat(authenticateResponse.enabled(), is(true)); // delete user - final Request deleteUserRequest = new Request(HttpDelete.METHOD_NAME, "/_xpack/security/user/" + putUserRequest.getUsername()); - highLevelClient().getLowLevelClient().performRequest(deleteUserRequest); + final DeleteUserRequest deleteUserRequest = new DeleteUserRequest(putUserRequest.getUsername(), putUserRequest.getRefreshPolicy()); + final DeleteUserResponse deleteUserResponse = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); + assertThat(deleteUserResponse.isFound(), is(true)); // authentication no longer works ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> execute(securityClient::authenticate, securityClient::authenticateAsync, authorizationRequestOptions(basicAuthHeader))); assertThat(e.getMessage(), containsString("unable to authenticate user [" + putUserRequest.getUsername() + "]")); + + // delete non-existing user + final DeleteUserResponse deleteUserResponse2 = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); + assertThat(deleteUserResponse2.isFound(), is(false)); } private static PutUserRequest randomPutUserRequest(boolean enabled) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityRequestConvertersTests.java index e0499c621f7ba..34a0bf7867dfa 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityRequestConvertersTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.client.security.CreateTokenRequest; import org.elasticsearch.client.security.DeleteRoleMappingRequest; import org.elasticsearch.client.security.DeleteRoleRequest; +import org.elasticsearch.client.security.DeleteUserRequest; import org.elasticsearch.client.security.DisableUserRequest; import org.elasticsearch.client.security.EnableUserRequest; import org.elasticsearch.client.security.GetRoleMappingsRequest; @@ -78,6 +79,18 @@ public void testPutUser() throws IOException { assertToXContentBody(putUserRequest, request.getEntity()); } + public void testDeleteUser() { + final String name = randomAlphaOfLengthBetween(4, 12); + final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); + final Map expectedParams = getExpectedParamsFromRefreshPolicy(refreshPolicy); + DeleteUserRequest deleteUserRequest = new DeleteUserRequest(name, refreshPolicy); + Request request = SecurityRequestConverters.deleteUser(deleteUserRequest); + assertEquals(HttpDelete.METHOD_NAME, request.getMethod()); + assertEquals("/_xpack/security/user/" + name, request.getEndpoint()); + assertEquals(expectedParams, request.getParameters()); + assertNull(request.getEntity()); + } + public void testPutRoleMapping() throws IOException { final String username = randomAlphaOfLengthBetween(4, 7); final String rolename = randomAlphaOfLengthBetween(4, 7); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java index 3fc787fa8585d..f52f9c33eabe4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java @@ -39,6 +39,8 @@ import org.elasticsearch.client.security.DeleteRoleMappingResponse; import org.elasticsearch.client.security.DeleteRoleRequest; import org.elasticsearch.client.security.DeleteRoleResponse; +import org.elasticsearch.client.security.DeleteUserRequest; +import org.elasticsearch.client.security.DeleteUserResponse; import org.elasticsearch.client.security.DisableUserRequest; import org.elasticsearch.client.security.EmptyResponse; import org.elasticsearch.client.security.EnableUserRequest; @@ -130,6 +132,66 @@ public void onFailure(Exception e) { } } + public void testDeleteUser() throws Exception { + RestHighLevelClient client = highLevelClient(); + addUser(client, "testUser", "testPassword"); + + { + // tag::delete-user-request + DeleteUserRequest deleteUserRequest = new DeleteUserRequest( + "testUser"); // <1> + // end::delete-user-request + + // tag::delete-user-execute + DeleteUserResponse deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); + // end::delete-user-execute + + // tag::delete-user-response + boolean found = deleteUserResponse.isFound(); // <1> + // end::delete-user-response + assertTrue(found); + + // check if deleting the already deleted user again will give us a different response + deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); + assertFalse(deleteUserResponse.isFound()); + } + + { + DeleteUserRequest deleteUserRequest = new DeleteUserRequest("testUser", RefreshPolicy.IMMEDIATE); + + ActionListener listener; + //tag::delete-user-execute-listener + listener = new ActionListener() { + @Override + public void onResponse(DeleteUserResponse deleteUserResponse) { + // <1> + } + + @Override + public void onFailure(Exception e) { + // <2> + } + }; + //end::delete-user-execute-listener + + // Replace the empty listener by a blocking listener in test + final CountDownLatch latch = new CountDownLatch(1); + listener = new LatchedActionListener<>(listener, latch); + + //tag::delete-user-execute-async + client.security().deleteUserAsync(deleteUserRequest, RequestOptions.DEFAULT, listener); // <1> + //end::delete-user-execute-async + + assertTrue(latch.await(30L, TimeUnit.SECONDS)); + } + } + + private void addUser(RestHighLevelClient client, String userName, String password) throws IOException { + PutUserRequest request = new PutUserRequest(userName, password.toCharArray(), Collections.singletonList("superuser"), null, null, true, null, RefreshPolicy.IMMEDIATE); + PutUserResponse response = client.security().putUser(request, RequestOptions.DEFAULT); + assertTrue(response.isCreated()); + } + public void testPutRoleMapping() throws Exception { final RestHighLevelClient client = highLevelClient(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java new file mode 100644 index 0000000000000..fd917ca4eb778 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java @@ -0,0 +1,76 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client.security; + +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static org.hamcrest.Matchers.equalTo; + +public class DeleteUserRequestTests extends ESTestCase { + + public void testDeleteUserRequest() { + final String name = randomAlphaOfLength(10); + final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); + final DeleteUserRequest deleteUserRequest = new DeleteUserRequest(name, refreshPolicy); + assertThat(deleteUserRequest.getName(), equalTo(name)); + assertThat(deleteUserRequest.getRefreshPolicy(), equalTo(refreshPolicy)); + } + + public void testDeleteUserRequestThrowsExceptionForNullName() { + final NullPointerException ile = expectThrows(NullPointerException.class, () -> new DeleteUserRequest(null, randomFrom(RefreshPolicy.values()))); + assertThat(ile.getMessage(), equalTo("user name is required")); + } + + public void testDeleteUserRequestThrowsExceptionForNullRefreshPolicy() { + final NullPointerException ile = expectThrows(NullPointerException.class, () -> new DeleteUserRequest(randomAlphaOfLength(10), null)); + assertThat(ile.getMessage(), equalTo("refresh policy is required")); + } + + public void testEqualsHashCode() { + final String name = randomAlphaOfLength(10); + final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); + final DeleteUserRequest deleteUserRequest = new DeleteUserRequest(name, refreshPolicy); + assertNotNull(deleteUserRequest); + + EqualsHashCodeTestUtils.checkEqualsAndHashCode(deleteUserRequest, (original) -> { + return new DeleteUserRequest(original.getName(), original.getRefreshPolicy()); + }); + + EqualsHashCodeTestUtils.checkEqualsAndHashCode(deleteUserRequest, (original) -> { + return new DeleteUserRequest(original.getName(), original.getRefreshPolicy()); + }, DeleteUserRequestTests::mutateTestItem); + + } + + private static DeleteUserRequest mutateTestItem(DeleteUserRequest original) { + if (randomBoolean()) { + return new DeleteUserRequest(randomAlphaOfLength(10), original.getRefreshPolicy()); + } else { + List values = Arrays.stream(RefreshPolicy.values()).filter(rp -> rp != original.getRefreshPolicy()).collect( + Collectors.toList()); + return new DeleteUserRequest(original.getName(), randomFrom(values)); + } + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java new file mode 100644 index 0000000000000..58dbd5e659d30 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java @@ -0,0 +1,59 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.client.security; + +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; + +public class DeleteUserResponseTests extends ESTestCase { + + public void testBasicParsing() throws IOException { + XContentType contentType = randomFrom(XContentType.values()); + final boolean found = randomBoolean(); + XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() + .field("found", found).endObject(); + BytesReference bytes = BytesReference.bytes(builder); + + DeleteUserResponse response = parse(builder.contentType(), bytes); + assertEquals(found, response.isFound()); + } + + public void testParsingWithMissingField() throws IOException { + XContentType contentType = randomFrom(XContentType.values()); + XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().endObject(); + BytesReference bytes = BytesReference.bytes(builder); + + expectThrows(IllegalArgumentException.class, () -> parse(builder.contentType(), bytes)); + } + + private DeleteUserResponse parse(XContentType contentType, BytesReference bytes) throws IOException { + XContentParser parser = XContentFactory.xContent(contentType) + .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); + parser.nextToken(); + return DeleteUserResponse.fromXContent(parser); + } + +} diff --git a/docs/java-rest/high-level/security/delete-user.asciidoc b/docs/java-rest/high-level/security/delete-user.asciidoc new file mode 100644 index 0000000000000..283ef37e2bffb --- /dev/null +++ b/docs/java-rest/high-level/security/delete-user.asciidoc @@ -0,0 +1,56 @@ +-- +:api: delete-user +:request: DeleteUserRequest +:response: DeleteUserResponse +-- + +[id="{upid}-{api}"] +=== Delete User API + +[id="{upid}-{api}-request"] +==== Delete User Request + +A user can be deleted as follows: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-request] +-------------------------------------------------- + +[id="{upid}-{api}-response"] +==== Delete Response + +The returned +{response}+ allows to retrieve information about the executed + operation as follows: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-response] +-------------------------------------------------- +<1> whether the given user was found + +==== Asynchronous Execution + +This request can be executed asynchronously: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-execute-async] +-------------------------------------------------- +<1> The +{request}+ to execute and the `ActionListener` to use when +the execution completes + +The asynchronous method does not block and returns immediately. Once it is +completed the `ActionListener` is called back using the `onResponse` method +if the execution successfully completed or using the `onFailure` method if +it failed. + +A typical listener for +{response}+ looks like: + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests-file}[{api}-execute-listener] +-------------------------------------------------- +<1> Called when the execution is successfully completed. The response is +provided as an argument +<2> Called in case of failure. The raised exception is provided as an argument \ No newline at end of file diff --git a/docs/java-rest/high-level/supported-apis.asciidoc b/docs/java-rest/high-level/supported-apis.asciidoc index 7a2a86a8390ea..0c7d2ce8f0f49 100644 --- a/docs/java-rest/high-level/supported-apis.asciidoc +++ b/docs/java-rest/high-level/supported-apis.asciidoc @@ -326,6 +326,7 @@ include::rollup/get_rollup_caps.asciidoc[] The Java High Level REST Client supports the following Security APIs: * <> +* <<{upid}-delete-user>> * <> * <> * <> @@ -340,6 +341,7 @@ The Java High Level REST Client supports the following Security APIs: * <<{upid}-invalidate-token>> include::security/put-user.asciidoc[] +include::security/delete-user.asciidoc[] include::security/enable-user.asciidoc[] include::security/disable-user.asciidoc[] include::security/change-password.asciidoc[] From 876f4a07ac8e6caa6b2644ef75cc26ddf81f2972 Mon Sep 17 00:00:00 2001 From: iverase Date: Tue, 6 Nov 2018 13:42:10 +0100 Subject: [PATCH 02/17] Fix precommit --- .../client/documentation/SecurityDocumentationIT.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java index f52f9c33eabe4..5e16210cdd738 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java @@ -187,7 +187,8 @@ public void onFailure(Exception e) { } private void addUser(RestHighLevelClient client, String userName, String password) throws IOException { - PutUserRequest request = new PutUserRequest(userName, password.toCharArray(), Collections.singletonList("superuser"), null, null, true, null, RefreshPolicy.IMMEDIATE); + PutUserRequest request = new PutUserRequest(userName, password.toCharArray(), + Collections.singletonList("superuser"), null, null, true, null, RefreshPolicy.IMMEDIATE); PutUserResponse response = client.security().putUser(request, RequestOptions.DEFAULT); assertTrue(response.isCreated()); } From ae9059f087c2030494632a91c6a5262da324aa5c Mon Sep 17 00:00:00 2001 From: iverase Date: Tue, 6 Nov 2018 14:26:24 +0100 Subject: [PATCH 03/17] Fix precommit --- .../client/security/DeleteUserResponse.java | 4 ++-- .../test/java/org/elasticsearch/client/SecurityIT.java | 9 ++++++--- .../client/security/DeleteUserRequestTests.java | 6 ++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java index 78258c395262e..a067536608714 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -42,8 +42,8 @@ public boolean isFound() { return this.found; } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("delete_user_response", - true, args -> new DeleteUserResponse((boolean) args[0])); + private static final ConstructingObjectParser PARSER = + new ConstructingObjectParser<>("delete_user_response", true, args -> new DeleteUserResponse((boolean) args[0])); static { PARSER.declareBoolean(constructorArg(), new ParseField("found")); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java index f20872b54d937..984e1306262a1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java @@ -65,8 +65,10 @@ public void testAuthenticate() throws Exception { assertThat(authenticateResponse.enabled(), is(true)); // delete user - final DeleteUserRequest deleteUserRequest = new DeleteUserRequest(putUserRequest.getUsername(), putUserRequest.getRefreshPolicy()); - final DeleteUserResponse deleteUserResponse = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); + final DeleteUserRequest deleteUserRequest = + new DeleteUserRequest(putUserRequest.getUsername(), putUserRequest.getRefreshPolicy()); + final DeleteUserResponse deleteUserResponse = + execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); assertThat(deleteUserResponse.isFound(), is(true)); // authentication no longer works @@ -75,7 +77,8 @@ public void testAuthenticate() throws Exception { assertThat(e.getMessage(), containsString("unable to authenticate user [" + putUserRequest.getUsername() + "]")); // delete non-existing user - final DeleteUserResponse deleteUserResponse2 = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); + final DeleteUserResponse deleteUserResponse2 = + execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); assertThat(deleteUserResponse2.isFound(), is(false)); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java index fd917ca4eb778..a317d41f21b69 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java @@ -39,12 +39,14 @@ public void testDeleteUserRequest() { } public void testDeleteUserRequestThrowsExceptionForNullName() { - final NullPointerException ile = expectThrows(NullPointerException.class, () -> new DeleteUserRequest(null, randomFrom(RefreshPolicy.values()))); + final NullPointerException ile = + expectThrows(NullPointerException.class, () -> new DeleteUserRequest(null, randomFrom(RefreshPolicy.values()))); assertThat(ile.getMessage(), equalTo("user name is required")); } public void testDeleteUserRequestThrowsExceptionForNullRefreshPolicy() { - final NullPointerException ile = expectThrows(NullPointerException.class, () -> new DeleteUserRequest(randomAlphaOfLength(10), null)); + final NullPointerException ile = + expectThrows(NullPointerException.class, () -> new DeleteUserRequest(randomAlphaOfLength(10), null)); assertThat(ile.getMessage(), equalTo("refresh policy is required")); } From 321935a90fc747ce9200a845656f9b26d8fc215b Mon Sep 17 00:00:00 2001 From: iverase Date: Wed, 7 Nov 2018 09:44:35 +0100 Subject: [PATCH 04/17] address some of the review comments --- .../client/SecurityRequestConverters.java | 2 +- .../client/security/DeleteUserResponse.java | 35 +++++++++++++++++-- .../security/DeleteUserResponseTests.java | 34 +++++++++--------- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java index eeb3ccdbe7168..0a36a02f8be3c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java @@ -74,7 +74,7 @@ static Request putUser(PutUserRequest putUserRequest) throws IOException { static Request deleteUser(DeleteUserRequest deleteUserRequest) { String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_xpack/security/user") + .addPathPartAsIs("_xpack","security", "user") .addPathPart(deleteUserRequest.getName()) .build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java index a067536608714..5157889420a75 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -21,18 +21,22 @@ import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; +import java.util.Objects; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; /** * Response for a role being deleted from the native realm */ -public final class DeleteUserResponse { +public final class DeleteUserResponse implements ToXContent { private final boolean found; + private static final String PARSE_FIELD = "found"; public DeleteUserResponse(boolean found) { this.found = found; @@ -46,10 +50,37 @@ public boolean isFound() { new ConstructingObjectParser<>("delete_user_response", true, args -> new DeleteUserResponse((boolean) args[0])); static { - PARSER.declareBoolean(constructorArg(), new ParseField("found")); + PARSER.declareBoolean(constructorArg(), new ParseField(PARSE_FIELD)); } public static DeleteUserResponse fromXContent(XContentParser parser) throws IOException { return PARSER.parse(parser, null); } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + { + builder.field(PARSE_FIELD, isFound()); + } + builder.endObject(); + return builder; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + final DeleteUserResponse that = (DeleteUserResponse) o; + return isFound() == that.isFound(); + } + + @Override + public int hashCode() { + return Objects.hash(found); + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java index 58dbd5e659d30..f2d4686997de2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java @@ -24,36 +24,34 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; -public class DeleteUserResponseTests extends ESTestCase { - - public void testBasicParsing() throws IOException { - XContentType contentType = randomFrom(XContentType.values()); - final boolean found = randomBoolean(); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() - .field("found", found).endObject(); - BytesReference bytes = BytesReference.bytes(builder); - - DeleteUserResponse response = parse(builder.contentType(), bytes); - assertEquals(found, response.isFound()); - } +public class DeleteUserResponseTests extends AbstractXContentTestCase { public void testParsingWithMissingField() throws IOException { XContentType contentType = randomFrom(XContentType.values()); XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().endObject(); BytesReference bytes = BytesReference.bytes(builder); - - expectThrows(IllegalArgumentException.class, () -> parse(builder.contentType(), bytes)); - } - - private DeleteUserResponse parse(XContentType contentType, BytesReference bytes) throws IOException { XContentParser parser = XContentFactory.xContent(contentType) .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); parser.nextToken(); + expectThrows(IllegalArgumentException.class, () -> DeleteUserResponse.fromXContent(parser)); + } + + @Override + protected DeleteUserResponse createTestInstance() { + return new DeleteUserResponse(randomBoolean()); + } + + @Override + protected DeleteUserResponse doParseInstance(XContentParser parser) throws IOException { return DeleteUserResponse.fromXContent(parser); } + @Override + protected boolean supportsUnknownFields() { + return false; + } } From e4a0904ff9fbf00babf6b8d6545de4410f96155e Mon Sep 17 00:00:00 2001 From: iverase Date: Wed, 7 Nov 2018 15:02:56 +0100 Subject: [PATCH 05/17] fist bug serializing response --- .../elasticsearch/client/security/DeleteUserResponse.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java index 5157889420a75..0bed5935a6e23 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -59,12 +59,7 @@ public static DeleteUserResponse fromXContent(XContentParser parser) throws IOEx @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - { - builder.field(PARSE_FIELD, isFound()); - } - builder.endObject(); - return builder; + return builder.field(PARSE_FIELD, isFound()); } @Override From 058f6c414b71e99e74e07df885badc3b54d40ee7 Mon Sep 17 00:00:00 2001 From: iverase Date: Thu, 8 Nov 2018 13:11:08 +0100 Subject: [PATCH 06/17] Implement DeleteUserResponse by extending AcknowledgedResponse --- .../client/security/DeleteUserResponse.java | 51 ++++--------------- .../org/elasticsearch/client/SecurityIT.java | 4 +- .../SecurityDocumentationIT.java | 4 +- 3 files changed, 14 insertions(+), 45 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java index 0bed5935a6e23..5776d22ddda06 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -19,63 +19,32 @@ package org.elasticsearch.client.security; -import org.elasticsearch.common.ParseField; +import org.elasticsearch.client.rollup.AcknowledgedResponse; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; -import java.util.Objects; - -import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; /** * Response for a role being deleted from the native realm */ -public final class DeleteUserResponse implements ToXContent { - - private final boolean found; - private static final String PARSE_FIELD = "found"; - - public DeleteUserResponse(boolean found) { - this.found = found; - } +public final class DeleteUserResponse extends AcknowledgedResponse { - public boolean isFound() { - return this.found; - } + private static final String PARSE_FIELD_NAME = "found"; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("delete_user_response", true, args -> new DeleteUserResponse((boolean) args[0])); + private static final ConstructingObjectParser PARSER = AcknowledgedResponse + .generateParser("delete_rollup_job_response", DeleteUserResponse::new, PARSE_FIELD_NAME); - static { - PARSER.declareBoolean(constructorArg(), new ParseField(PARSE_FIELD)); + public DeleteUserResponse(boolean acknowledged) { + super(acknowledged); } - public static DeleteUserResponse fromXContent(XContentParser parser) throws IOException { + public static DeleteUserResponse fromXContent(final XContentParser parser) throws IOException { return PARSER.parse(parser, null); } @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.field(PARSE_FIELD, isFound()); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - final DeleteUserResponse that = (DeleteUserResponse) o; - return isFound() == that.isFound(); - } - - @Override - public int hashCode() { - return Objects.hash(found); + protected String getFieldName() { + return PARSE_FIELD_NAME; } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java index 984e1306262a1..3a6c3aad249e9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java @@ -69,7 +69,7 @@ public void testAuthenticate() throws Exception { new DeleteUserRequest(putUserRequest.getUsername(), putUserRequest.getRefreshPolicy()); final DeleteUserResponse deleteUserResponse = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); - assertThat(deleteUserResponse.isFound(), is(true)); + assertThat(deleteUserResponse.isAcknowledged(), is(true)); // authentication no longer works ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> execute(securityClient::authenticate, @@ -79,7 +79,7 @@ public void testAuthenticate() throws Exception { // delete non-existing user final DeleteUserResponse deleteUserResponse2 = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); - assertThat(deleteUserResponse2.isFound(), is(false)); + assertThat(deleteUserResponse2.isAcknowledged(), is(false)); } private static PutUserRequest randomPutUserRequest(boolean enabled) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java index 5e16210cdd738..ef2f7e8f524fe 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java @@ -147,13 +147,13 @@ public void testDeleteUser() throws Exception { // end::delete-user-execute // tag::delete-user-response - boolean found = deleteUserResponse.isFound(); // <1> + boolean found = deleteUserResponse.isAcknowledged(); // <1> // end::delete-user-response assertTrue(found); // check if deleting the already deleted user again will give us a different response deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); - assertFalse(deleteUserResponse.isFound()); + assertFalse(deleteUserResponse.isAcknowledged()); } { From ff6f055d5ca1e4a45f072540d76f1053f5c5ec57 Mon Sep 17 00:00:00 2001 From: iverase Date: Thu, 8 Nov 2018 14:26:21 +0100 Subject: [PATCH 07/17] Acknowledge response is in core now --- .../org/elasticsearch/client/security/DeleteUserResponse.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java index 5776d22ddda06..8af4422114618 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -19,7 +19,7 @@ package org.elasticsearch.client.security; -import org.elasticsearch.client.rollup.AcknowledgedResponse; +import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.XContentParser; @@ -33,7 +33,7 @@ public final class DeleteUserResponse extends AcknowledgedResponse { private static final String PARSE_FIELD_NAME = "found"; private static final ConstructingObjectParser PARSER = AcknowledgedResponse - .generateParser("delete_rollup_job_response", DeleteUserResponse::new, PARSE_FIELD_NAME); + .generateParser("delete_user_response", DeleteUserResponse::new, PARSE_FIELD_NAME); public DeleteUserResponse(boolean acknowledged) { super(acknowledged); From 041aeb65298208a53c02e22e43c1885406381c3f Mon Sep 17 00:00:00 2001 From: iverase Date: Thu, 8 Nov 2018 14:37:43 +0100 Subject: [PATCH 08/17] javadocs --- .../org/elasticsearch/client/security/DeleteUserRequest.java | 2 +- .../org/elasticsearch/client/security/DeleteUserResponse.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java index f5c943b8c41d9..6995cfdfbca5e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java @@ -24,7 +24,7 @@ import java.util.Objects; /** - * A request delete a role from the security index + * A request to delete a user from the native realm. */ public final class DeleteUserRequest implements Validatable { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java index 8af4422114618..31306dd7478be 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -26,7 +26,7 @@ import java.io.IOException; /** - * Response for a role being deleted from the native realm + * Response for a user being deleted from the native realm */ public final class DeleteUserResponse extends AcknowledgedResponse { From ee6d4c6b9f6d2b5ffe3c6d7b743619f10944db7f Mon Sep 17 00:00:00 2001 From: iverase Date: Fri, 9 Nov 2018 13:31:48 +0100 Subject: [PATCH 09/17] simplify docs --- .../high-level/security/delete-user.asciidoc | 26 +------------------ 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/docs/java-rest/high-level/security/delete-user.asciidoc b/docs/java-rest/high-level/security/delete-user.asciidoc index 283ef37e2bffb..52573bb29c74e 100644 --- a/docs/java-rest/high-level/security/delete-user.asciidoc +++ b/docs/java-rest/high-level/security/delete-user.asciidoc @@ -29,28 +29,4 @@ include-tagged::{doc-tests-file}[{api}-response] -------------------------------------------------- <1> whether the given user was found -==== Asynchronous Execution - -This request can be executed asynchronously: - -["source","java",subs="attributes,callouts,macros"] --------------------------------------------------- -include-tagged::{doc-tests-file}[{api}-execute-async] --------------------------------------------------- -<1> The +{request}+ to execute and the `ActionListener` to use when -the execution completes - -The asynchronous method does not block and returns immediately. Once it is -completed the `ActionListener` is called back using the `onResponse` method -if the execution successfully completed or using the `onFailure` method if -it failed. - -A typical listener for +{response}+ looks like: - -["source","java",subs="attributes,callouts,macros"] --------------------------------------------------- -include-tagged::{doc-tests-file}[{api}-execute-listener] --------------------------------------------------- -<1> Called when the execution is successfully completed. The response is -provided as an argument -<2> Called in case of failure. The raised exception is provided as an argument \ No newline at end of file +include::../execution.asciidoc[] \ No newline at end of file From 74654621026d767a8c861ce7bc71f3ab2b7dd612 Mon Sep 17 00:00:00 2001 From: iverase Date: Mon, 12 Nov 2018 20:32:31 +0100 Subject: [PATCH 10/17] fix checkStyle --- .../src/test/java/org/elasticsearch/client/SecurityIT.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java index bd7b25bfec63f..5326a50e449ae 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java @@ -80,7 +80,8 @@ public void testAuthenticate() throws Exception { final DeleteUserRequest deleteUserRequest = new DeleteUserRequest(putUserRequest.getUser().getUsername(), putUserRequest.getRefreshPolicy()); - final Optional deleteUserResponse = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); + final Optional deleteUserResponse = + execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); assertThat(deleteUserResponse.get().isAcknowledged(), is(true)); // authentication no longer works @@ -89,7 +90,8 @@ public void testAuthenticate() throws Exception { assertThat(e.getMessage(), containsString("unable to authenticate user [" + putUserRequest.getUser().getUsername() + "]")); // delete non-existing user - final Optional deleteUserResponse2 = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); + final Optional deleteUserResponse2 = + execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); assertThat(deleteUserResponse2.isPresent(), is(false)); } From b83312d540b6fdc8e831421d0a7209461a7351df Mon Sep 17 00:00:00 2001 From: iverase Date: Mon, 19 Nov 2018 07:22:04 +0100 Subject: [PATCH 11/17] remove otioonal and use AcknowledgeResponse --- .../elasticsearch/client/SecurityClient.java | 15 +++-- .../client/security/DeleteUserResponse.java | 50 ---------------- .../org/elasticsearch/client/SecurityIT.java | 11 ++-- .../SecurityDocumentationIT.java | 15 +++-- .../security/DeleteUserResponseTests.java | 57 ------------------- 5 files changed, 19 insertions(+), 129 deletions(-) delete mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java delete mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java index c3e53da48d9e7..6285388f33c82 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java @@ -20,6 +20,7 @@ package org.elasticsearch.client; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.client.security.AuthenticateRequest; import org.elasticsearch.client.security.AuthenticateResponse; import org.elasticsearch.client.security.ChangePasswordRequest; @@ -34,7 +35,6 @@ import org.elasticsearch.client.security.DeleteRoleRequest; import org.elasticsearch.client.security.DeleteRoleResponse; import org.elasticsearch.client.security.DeleteUserRequest; -import org.elasticsearch.client.security.DeleteUserResponse; import org.elasticsearch.client.security.DisableUserRequest; import org.elasticsearch.client.security.EmptyResponse; import org.elasticsearch.client.security.EnableUserRequest; @@ -50,7 +50,6 @@ import org.elasticsearch.client.security.PutUserResponse; import java.io.IOException; -import java.util.Optional; import static java.util.Collections.emptySet; import static java.util.Collections.singleton; @@ -106,9 +105,9 @@ public void putUserAsync(PutUserRequest request, RequestOptions options, ActionL * @return the response from the delete user call * @throws IOException in case there is a problem sending the request or parsing back the response */ - public Optional deleteUser(DeleteUserRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseOptionalEntity(request, SecurityRequestConverters::deleteUser, options, - DeleteUserResponse::fromXContent); + public AcknowledgedResponse deleteUser(DeleteUserRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deleteUser, options, + AcknowledgedResponse::fromXContent, singleton(404)); } /** @@ -119,9 +118,9 @@ public Optional deleteUser(DeleteUserRequest request, Reques * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion */ - public void deleteUserAsync(DeleteUserRequest request, RequestOptions options, ActionListener> listener) { - restHighLevelClient.performRequestAsyncAndParseOptionalEntity(request, SecurityRequestConverters::deleteUser, options, - DeleteUserResponse::fromXContent, listener); + public void deleteUserAsync(DeleteUserRequest request, RequestOptions options, ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::deleteUser, options, + AcknowledgedResponse::fromXContent, listener, singleton(404)); } /** diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java deleted file mode 100644 index 31306dd7478be..0000000000000 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.client.security; - -import org.elasticsearch.client.core.AcknowledgedResponse; -import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.XContentParser; - -import java.io.IOException; - -/** - * Response for a user being deleted from the native realm - */ -public final class DeleteUserResponse extends AcknowledgedResponse { - - private static final String PARSE_FIELD_NAME = "found"; - - private static final ConstructingObjectParser PARSER = AcknowledgedResponse - .generateParser("delete_user_response", DeleteUserResponse::new, PARSE_FIELD_NAME); - - public DeleteUserResponse(boolean acknowledged) { - super(acknowledged); - } - - public static DeleteUserResponse fromXContent(final XContentParser parser) throws IOException { - return PARSER.parse(parser, null); - } - - @Override - protected String getFieldName() { - return PARSE_FIELD_NAME; - } -} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java index 5326a50e449ae..04a1af419010a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java @@ -21,9 +21,9 @@ import org.apache.http.client.methods.HttpDelete; import org.elasticsearch.ElasticsearchStatusException; +import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.client.security.AuthenticateResponse; import org.elasticsearch.client.security.DeleteUserRequest; -import org.elasticsearch.client.security.DeleteUserResponse; import org.elasticsearch.client.security.PutUserRequest; import org.elasticsearch.client.security.PutUserResponse; import org.elasticsearch.client.security.RefreshPolicy; @@ -35,7 +35,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Optional; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.containsString; @@ -80,9 +79,9 @@ public void testAuthenticate() throws Exception { final DeleteUserRequest deleteUserRequest = new DeleteUserRequest(putUserRequest.getUser().getUsername(), putUserRequest.getRefreshPolicy()); - final Optional deleteUserResponse = + final AcknowledgedResponse deleteUserResponse = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); - assertThat(deleteUserResponse.get().isAcknowledged(), is(true)); + assertThat(deleteUserResponse.isAcknowledged(), is(true)); // authentication no longer works ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> execute(securityClient::authenticate, @@ -90,9 +89,9 @@ public void testAuthenticate() throws Exception { assertThat(e.getMessage(), containsString("unable to authenticate user [" + putUserRequest.getUser().getUsername() + "]")); // delete non-existing user - final Optional deleteUserResponse2 = + final AcknowledgedResponse deleteUserResponse2 = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); - assertThat(deleteUserResponse2.isPresent(), is(false)); + assertThat(deleteUserResponse2.isAcknowledged(), is(false)); } private static User randomUser() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java index 2c602bab59264..1789d693f24df 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java @@ -29,6 +29,7 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.client.security.AuthenticateResponse; import org.elasticsearch.client.security.ChangePasswordRequest; import org.elasticsearch.client.security.ClearRealmCacheRequest; @@ -42,7 +43,6 @@ import org.elasticsearch.client.security.DeleteRoleRequest; import org.elasticsearch.client.security.DeleteRoleResponse; import org.elasticsearch.client.security.DeleteUserRequest; -import org.elasticsearch.client.security.DeleteUserResponse; import org.elasticsearch.client.security.DisableUserRequest; import org.elasticsearch.client.security.EmptyResponse; import org.elasticsearch.client.security.EnableUserRequest; @@ -72,7 +72,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -146,27 +145,27 @@ public void testDeleteUser() throws Exception { // end::delete-user-request // tag::delete-user-execute - Optional deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); + AcknowledgedResponse deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); // end::delete-user-execute // tag::delete-user-response - boolean found = deleteUserResponse.get().isAcknowledged(); // <1> + boolean found = deleteUserResponse.isAcknowledged(); // <1> // end::delete-user-response assertTrue(found); // check if deleting the already deleted user again will give us a different response deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); - assertFalse(deleteUserResponse.isPresent()); + assertFalse(deleteUserResponse.isAcknowledged()); } { DeleteUserRequest deleteUserRequest = new DeleteUserRequest("testUser", RefreshPolicy.IMMEDIATE); - ActionListener> listener; + ActionListener listener; //tag::delete-user-execute-listener - listener = new ActionListener>() { + listener = new ActionListener() { @Override - public void onResponse(Optional deleteUserResponse) { + public void onResponse(AcknowledgedResponse deleteUserResponse) { // <1> } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java deleted file mode 100644 index f2d4686997de2..0000000000000 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.client.security; - -import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.NamedXContentRegistry; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.test.AbstractXContentTestCase; - -import java.io.IOException; - -public class DeleteUserResponseTests extends AbstractXContentTestCase { - - public void testParsingWithMissingField() throws IOException { - XContentType contentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().endObject(); - BytesReference bytes = BytesReference.bytes(builder); - XContentParser parser = XContentFactory.xContent(contentType) - .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); - parser.nextToken(); - expectThrows(IllegalArgumentException.class, () -> DeleteUserResponse.fromXContent(parser)); - } - - @Override - protected DeleteUserResponse createTestInstance() { - return new DeleteUserResponse(randomBoolean()); - } - - @Override - protected DeleteUserResponse doParseInstance(XContentParser parser) throws IOException { - return DeleteUserResponse.fromXContent(parser); - } - - @Override - protected boolean supportsUnknownFields() { - return false; - } -} From 7e90b833686c2fabe825d753be8d965026f673a7 Mon Sep 17 00:00:00 2001 From: iverase Date: Mon, 19 Nov 2018 07:52:41 +0100 Subject: [PATCH 12/17] Implement delete response --- .../elasticsearch/client/SecurityClient.java | 9 +++--- .../client/security/DeleteUserResponse.java | 31 +++++++++++++++++++ .../org/elasticsearch/client/SecurityIT.java | 5 +-- .../SecurityDocumentationIT.java | 9 +++--- .../security/DeleteUserResponseTests.java | 24 ++++++++++++++ 5 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java index 384ea6904bb8b..5e7c83cb39fe5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java @@ -37,6 +37,7 @@ import org.elasticsearch.client.security.DeleteRoleRequest; import org.elasticsearch.client.security.DeleteRoleResponse; import org.elasticsearch.client.security.DeleteUserRequest; +import org.elasticsearch.client.security.DeleteUserResponse; import org.elasticsearch.client.security.DisableUserRequest; import org.elasticsearch.client.security.EmptyResponse; import org.elasticsearch.client.security.EnableUserRequest; @@ -109,9 +110,9 @@ public void putUserAsync(PutUserRequest request, RequestOptions options, ActionL * @return the response from the delete user call * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse deleteUser(DeleteUserRequest request, RequestOptions options) throws IOException { + public DeleteUserResponse deleteUser(DeleteUserRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deleteUser, options, - AcknowledgedResponse::fromXContent, singleton(404)); + DeleteUserResponse::fromXContent, singleton(404)); } /** @@ -122,9 +123,9 @@ public AcknowledgedResponse deleteUser(DeleteUserRequest request, RequestOptions * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion */ - public void deleteUserAsync(DeleteUserRequest request, RequestOptions options, ActionListener listener) { + public void deleteUserAsync(DeleteUserRequest request, RequestOptions options, ActionListener listener) { restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::deleteUser, options, - AcknowledgedResponse::fromXContent, listener, singleton(404)); + DeleteUserResponse::fromXContent, listener, singleton(404)); } /** diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java new file mode 100644 index 0000000000000..02c599c79ee31 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -0,0 +1,31 @@ +package org.elasticsearch.client.security; + +import org.elasticsearch.client.core.AcknowledgedResponse; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; + +/** + * Response for a user being deleted from the native realm + */ +public final class DeleteUserResponse extends AcknowledgedResponse { + + private static final String PARSE_FIELD_NAME = "found"; + + private static final ConstructingObjectParser PARSER = AcknowledgedResponse + .generateParser("delete_user_response", DeleteUserResponse::new, PARSE_FIELD_NAME); + + public DeleteUserResponse(boolean acknowledged) { + super(acknowledged); + } + + public static DeleteUserResponse fromXContent(final XContentParser parser) throws IOException { + return PARSER.parse(parser, null); + } + + @Override + protected String getFieldName() { + return PARSE_FIELD_NAME; + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java index 04a1af419010a..9af459f4d2845 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java @@ -24,6 +24,7 @@ import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.client.security.AuthenticateResponse; import org.elasticsearch.client.security.DeleteUserRequest; +import org.elasticsearch.client.security.DeleteUserResponse; import org.elasticsearch.client.security.PutUserRequest; import org.elasticsearch.client.security.PutUserResponse; import org.elasticsearch.client.security.RefreshPolicy; @@ -79,7 +80,7 @@ public void testAuthenticate() throws Exception { final DeleteUserRequest deleteUserRequest = new DeleteUserRequest(putUserRequest.getUser().getUsername(), putUserRequest.getRefreshPolicy()); - final AcknowledgedResponse deleteUserResponse = + final DeleteUserResponse deleteUserResponse = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); assertThat(deleteUserResponse.isAcknowledged(), is(true)); @@ -89,7 +90,7 @@ public void testAuthenticate() throws Exception { assertThat(e.getMessage(), containsString("unable to authenticate user [" + putUserRequest.getUser().getUsername() + "]")); // delete non-existing user - final AcknowledgedResponse deleteUserResponse2 = + final DeleteUserResponse deleteUserResponse2 = execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); assertThat(deleteUserResponse2.isAcknowledged(), is(false)); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java index baa815534e81f..ab25b8b0c7cd8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java @@ -46,6 +46,7 @@ import org.elasticsearch.client.security.DeleteRoleRequest; import org.elasticsearch.client.security.DeleteRoleResponse; import org.elasticsearch.client.security.DeleteUserRequest; +import org.elasticsearch.client.security.DeleteUserResponse; import org.elasticsearch.client.security.DisableUserRequest; import org.elasticsearch.client.security.EmptyResponse; import org.elasticsearch.client.security.EnableUserRequest; @@ -154,7 +155,7 @@ public void testDeleteUser() throws Exception { // end::delete-user-request // tag::delete-user-execute - AcknowledgedResponse deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); + DeleteUserResponse deleteUserResponse = client.security().deleteUser(deleteUserRequest, RequestOptions.DEFAULT); // end::delete-user-execute // tag::delete-user-response @@ -170,11 +171,11 @@ public void testDeleteUser() throws Exception { { DeleteUserRequest deleteUserRequest = new DeleteUserRequest("testUser", RefreshPolicy.IMMEDIATE); - ActionListener listener; + ActionListener listener; //tag::delete-user-execute-listener - listener = new ActionListener() { + listener = new ActionListener() { @Override - public void onResponse(AcknowledgedResponse deleteUserResponse) { + public void onResponse(DeleteUserResponse deleteUserResponse) { // <1> } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java new file mode 100644 index 0000000000000..3d07b2b2a74d6 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java @@ -0,0 +1,24 @@ +package org.elasticsearch.client.security; + +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; + +public class DeleteUserResponseTests extends ESTestCase { + + public void testParsingWithMissingField() throws IOException { + XContentType contentType = randomFrom(XContentType.values()); + XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().endObject(); + BytesReference bytes = BytesReference.bytes(builder); + XContentParser parser = XContentFactory.xContent(contentType) + .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); + parser.nextToken(); + expectThrows(IllegalArgumentException.class, () -> DeleteUserResponse.fromXContent(parser)); + } +} From f3e76a55e9ef336d4d9e784aef2224b30db41859 Mon Sep 17 00:00:00 2001 From: iverase Date: Mon, 19 Nov 2018 07:55:49 +0100 Subject: [PATCH 13/17] remove unused imports --- .../src/main/java/org/elasticsearch/client/SecurityClient.java | 1 - .../src/test/java/org/elasticsearch/client/SecurityIT.java | 1 - .../client/documentation/SecurityDocumentationIT.java | 1 - 3 files changed, 3 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java index 5e7c83cb39fe5..f044b26dc8744 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java @@ -20,7 +20,6 @@ package org.elasticsearch.client; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.client.security.AuthenticateRequest; import org.elasticsearch.client.security.AuthenticateResponse; import org.elasticsearch.client.security.ChangePasswordRequest; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java index 9af459f4d2845..27b1d31e6d7d5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java @@ -21,7 +21,6 @@ import org.apache.http.client.methods.HttpDelete; import org.elasticsearch.ElasticsearchStatusException; -import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.client.security.AuthenticateResponse; import org.elasticsearch.client.security.DeleteUserRequest; import org.elasticsearch.client.security.DeleteUserResponse; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java index ab25b8b0c7cd8..b694cde762be2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java @@ -30,7 +30,6 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestHighLevelClient; -import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.client.security.AuthenticateResponse; import org.elasticsearch.client.security.ChangePasswordRequest; import org.elasticsearch.client.security.ClearRealmCacheRequest; From 0351a26e4be6c08f0097e4a14490c1fd0695cdef Mon Sep 17 00:00:00 2001 From: iverase Date: Mon, 19 Nov 2018 08:12:44 +0100 Subject: [PATCH 14/17] add license --- .../client/security/DeleteUserResponse.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java index 02c599c79ee31..31306dd7478be 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -1,3 +1,22 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.elasticsearch.client.security; import org.elasticsearch.client.core.AcknowledgedResponse; From 5f105203f7c5ff6f2d503ec3ee24f55e695e2d2d Mon Sep 17 00:00:00 2001 From: iverase Date: Mon, 19 Nov 2018 08:14:13 +0100 Subject: [PATCH 15/17] formatting --- .../elasticsearch/client/security/DeleteRoleResponseTests.java | 1 + 1 file changed, 1 insertion(+) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java index a2c08fcf8814e..ea16c9fad330a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.elasticsearch.client.security; import org.elasticsearch.common.bytes.BytesReference; From 1f7fb314dbccd8a80d5b3b90d1ed9085bdf42863 Mon Sep 17 00:00:00 2001 From: iverase Date: Mon, 19 Nov 2018 08:20:12 +0100 Subject: [PATCH 16/17] add license --- .../security/DeleteUserResponseTests.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java index 3d07b2b2a74d6..e27ebad6d01a4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java @@ -1,3 +1,22 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.elasticsearch.client.security; import org.elasticsearch.common.bytes.BytesReference; From c697fc043d00b2d1b4a1d611d16fab3f0ce4d22f Mon Sep 17 00:00:00 2001 From: iverase Date: Mon, 19 Nov 2018 09:09:06 +0100 Subject: [PATCH 17/17] fix checkStyle --- .../org/elasticsearch/client/SecurityRequestConverters.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java index 641c80028cf06..6004a5991e326 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java @@ -29,7 +29,7 @@ import org.elasticsearch.client.security.CreateTokenRequest; import org.elasticsearch.client.security.DeletePrivilegesRequest; import org.elasticsearch.client.security.DeleteRoleMappingRequest; -import org.elasticsearch.client.security.DeleteRoleRequest;; +import org.elasticsearch.client.security.DeleteRoleRequest; import org.elasticsearch.client.security.DeleteUserRequest; import org.elasticsearch.client.security.InvalidateTokenRequest; import org.elasticsearch.client.security.PutRoleMappingRequest;