Skip to content

Commit

Permalink
Remove all "user" related methods from HLRC (#84011)
Browse files Browse the repository at this point in the history
Removes the following methods from the SecurityClient component
of the High Level Rest Client

- putUser
- deleteUser
- changePassword
- authenticate

As part of this change, I renamed the SecurityClientTestHelper class
to TestSecurityClient and made it a real object rather than a set of
utility methods.

This was needed because different tests need different RequestOptions
objects, but passing it into every method made it cumbersome.
The code is clearer if we use a field in the test client itself. 

Relates: #83423
  • Loading branch information
tvernum committed Feb 16, 2022
1 parent 476240e commit f9f6ec9
Show file tree
Hide file tree
Showing 22 changed files with 391 additions and 762 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

package org.elasticsearch.client;

import org.elasticsearch.client.security.AuthenticateRequest;
import org.elasticsearch.client.security.AuthenticateResponse;
import org.elasticsearch.client.security.ChangePasswordRequest;
import org.elasticsearch.client.security.ClearRealmCacheRequest;
import org.elasticsearch.client.security.ClearRealmCacheResponse;
import org.elasticsearch.client.security.CreateTokenRequest;
Expand All @@ -21,8 +18,6 @@
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.GetApiKeyRequest;
import org.elasticsearch.client.security.GetApiKeyResponse;
import org.elasticsearch.client.security.GetRolesRequest;
Expand All @@ -37,8 +32,6 @@
import org.elasticsearch.client.security.PutRoleMappingResponse;
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 java.io.IOException;

Expand All @@ -64,45 +57,6 @@ public final class SecurityClient {
this.restHighLevelClient = restHighLevelClient;
}

/**
* Create/update a user in the native realm synchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-users.html">
* the docs</a> for more.
*
* @param request the request with the user's information
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response from the put user call
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public PutUserResponse putUser(PutUserRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(
request,
SecurityRequestConverters::putUser,
options,
PutUserResponse::fromXContent,
emptySet()
);
}

/**
* Removes user from the native realm synchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-delete-user.html">
* the docs</a> 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)
);
}

/**
* Create/Update a role mapping.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-put-role-mapping.html">
Expand All @@ -122,24 +76,6 @@ public PutRoleMappingResponse putRoleMapping(final PutRoleMappingRequest request
);
}

/**
* Authenticate the current user and return all the information about the authenticated user.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-authenticate.html">
* the docs</a> for more.
*
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the responsee from the authenticate user call
*/
public AuthenticateResponse authenticate(RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(
AuthenticateRequest.INSTANCE,
AuthenticateRequest::getRequest,
options,
AuthenticateResponse::fromXContent,
emptySet()
);
}

/**
* Clears the cache in one or more realms.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-cache.html">
Expand All @@ -160,26 +96,6 @@ public ClearRealmCacheResponse clearRealmCache(ClearRealmCacheRequest request, R
);
}

/**
* Change the password of a user of a native realm or built-in user synchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-change-password.html">
* the docs</a> for more.
*
* @param request the request with the user's new password
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return {@code true} if the request succeeded (the new password was set)
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public boolean changePassword(ChangePasswordRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequest(
request,
SecurityRequestConverters::changePassword,
options,
RestHighLevelClient::convertExistsResponse,
emptySet()
);
}

/**
* Delete a role mapping.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-delete-role-mapping.html">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.security.ChangePasswordRequest;
import org.elasticsearch.client.security.ClearRealmCacheRequest;
import org.elasticsearch.client.security.CreateTokenRequest;
import org.elasticsearch.client.security.DelegatePkiAuthenticationRequest;
import org.elasticsearch.client.security.DeleteRoleMappingRequest;
import org.elasticsearch.client.security.DeleteRoleRequest;
import org.elasticsearch.client.security.DeleteUserRequest;
import org.elasticsearch.client.security.GetApiKeyRequest;
import org.elasticsearch.client.security.GetRolesRequest;
import org.elasticsearch.client.security.InvalidateApiKeyRequest;
import org.elasticsearch.client.security.InvalidateTokenRequest;
import org.elasticsearch.client.security.PutPrivilegesRequest;
import org.elasticsearch.client.security.PutRoleMappingRequest;
import org.elasticsearch.client.security.PutRoleRequest;
import org.elasticsearch.client.security.PutUserRequest;
import org.elasticsearch.common.Strings;

import java.io.IOException;
Expand All @@ -38,42 +35,6 @@ final class SecurityRequestConverters {

private SecurityRequestConverters() {}

static Request changePassword(ChangePasswordRequest changePasswordRequest) throws IOException {
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/user")
.addPathPart(changePasswordRequest.getUsername())
.addPathPartAsIs("_password")
.build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
request.setEntity(createEntity(changePasswordRequest, REQUEST_BODY_CONTENT_TYPE));
RequestConverters.Params params = new RequestConverters.Params();
params.withRefreshPolicy(changePasswordRequest.getRefreshPolicy());
request.addParameters(params.asMap());
return request;
}

static Request putUser(PutUserRequest putUserRequest) throws IOException {
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/user")
.addPathPart(putUserRequest.getUser().getUsername())
.build();
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
request.setEntity(createEntity(putUserRequest, REQUEST_BODY_CONTENT_TYPE));
RequestConverters.Params params = new RequestConverters.Params();
params.withRefreshPolicy(putUserRequest.getRefreshPolicy());
request.addParameters(params.asMap());
return request;
}

static Request deleteUser(DeleteUserRequest deleteUserRequest) {
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security", "user")
.addPathPart(deleteUserRequest.getName())
.build();
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
RequestConverters.Params params = new RequestConverters.Params();
params.withRefreshPolicy(deleteUserRequest.getRefreshPolicy());
request.addParameters(params.asMap());
return request;
}

static Request putRoleMapping(final PutRoleMappingRequest putRoleMappingRequest) throws IOException {
final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/role_mapping")
.addPathPart(putRoleMappingRequest.getName())
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit f9f6ec9

Please sign in to comment.