Skip to content

Service Accounts - HLRC #72431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import org.elasticsearch.client.security.ClearRolesCacheRequest;
import org.elasticsearch.client.security.ClearRolesCacheResponse;
import org.elasticsearch.client.security.ClearSecurityCacheResponse;
import org.elasticsearch.client.security.ClearServiceAccountTokenCacheRequest;
import org.elasticsearch.client.security.CreateApiKeyRequest;
import org.elasticsearch.client.security.CreateApiKeyResponse;
import org.elasticsearch.client.security.CreateServiceAccountTokenRequest;
import org.elasticsearch.client.security.CreateServiceAccountTokenResponse;
import org.elasticsearch.client.security.CreateTokenRequest;
import org.elasticsearch.client.security.CreateTokenResponse;
import org.elasticsearch.client.security.DelegatePkiAuthenticationRequest;
Expand All @@ -32,6 +35,8 @@
import org.elasticsearch.client.security.DeleteRoleMappingResponse;
import org.elasticsearch.client.security.DeleteRoleRequest;
import org.elasticsearch.client.security.DeleteRoleResponse;
import org.elasticsearch.client.security.DeleteServiceAccountTokenRequest;
import org.elasticsearch.client.security.DeleteServiceAccountTokenResponse;
import org.elasticsearch.client.security.DeleteUserRequest;
import org.elasticsearch.client.security.DeleteUserResponse;
import org.elasticsearch.client.security.DisableUserRequest;
Expand All @@ -46,6 +51,10 @@
import org.elasticsearch.client.security.GetRoleMappingsResponse;
import org.elasticsearch.client.security.GetRolesRequest;
import org.elasticsearch.client.security.GetRolesResponse;
import org.elasticsearch.client.security.GetServiceAccountCredentialsRequest;
import org.elasticsearch.client.security.GetServiceAccountCredentialsResponse;
import org.elasticsearch.client.security.GetServiceAccountsRequest;
import org.elasticsearch.client.security.GetServiceAccountsResponse;
import org.elasticsearch.client.security.GetSslCertificatesRequest;
import org.elasticsearch.client.security.GetSslCertificatesResponse;
import org.elasticsearch.client.security.GetUserPrivilegesRequest;
Expand Down Expand Up @@ -567,6 +576,38 @@ public Cancellable clearApiKeyCacheAsync(ClearApiKeyCacheRequest request, Reques
ClearSecurityCacheResponse::fromXContent, listener, emptySet());
}

/**
* Clears the service account token cache for the specified namespace, service-name and list of token names.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-service-token-cache.html">
* the docs</a> for more.
*
* @param request the request with namespace, service-name and token names for the service account tokens
* that should be cleared from the cache.
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response from the clear security cache call
* @throws IOException in case there is a problem sending the request or parsing back the response
*/public ClearSecurityCacheResponse clearServiceAccountTokenCache(ClearServiceAccountTokenCacheRequest request,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::clearServiceAccountTokenCache,
options, ClearSecurityCacheResponse::fromXContent, emptySet());
}

/**
* Clears the service account token cache for the specified namespace, service-name and list of token names asynchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-service-token-cache.html">
* the docs</a> for more.
*
* @param request the request with namespace, service-name and token names for the service account tokens
* that should be cleared from the cache.
* @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
* @return cancellable that may be used to cancel the request
*/public Cancellable clearServiceAccountTokenCacheAsync(ClearServiceAccountTokenCacheRequest request, RequestOptions options,
ActionListener<ClearSecurityCacheResponse> listener) {
return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::clearServiceAccountTokenCache,
options, ClearSecurityCacheResponse::fromXContent, listener, emptySet());
}

/**
* Synchronously retrieve the X.509 certificates that are used to encrypt communications in an Elasticsearch cluster.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-ssl.html">
Expand Down Expand Up @@ -1096,6 +1137,131 @@ public Cancellable grantApiKeyAsync(final GrantApiKeyRequest request, final Requ
CreateApiKeyResponse::fromXContent, listener, emptySet());
}

/**
* Get a service account, or list of service accounts synchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-service-accounts.html">
* the docs</a> for more information.
* @param request the request with namespace and service-name
* @param options the request options (e.g., headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response from the get service accounts call
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public GetServiceAccountsResponse getServiceAccounts(GetServiceAccountsRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getServiceAccounts, options,
GetServiceAccountsResponse::fromXContent, emptySet());
}

/**
* Get a service account, or list of service accounts asynchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-service-accounts.html">
* the docs</a> for more information.
* @param request the request with namespace and service-name
* @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
* @return cancellable that may be used to cancel the request
*/
public Cancellable getServiceAccountsAsync(GetServiceAccountsRequest request, RequestOptions options,
ActionListener<GetServiceAccountsResponse> listener) {
return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getServiceAccounts, options,
GetServiceAccountsResponse::fromXContent, listener, emptySet());
}

/**
* Create a service account token.<br>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-service-token.html">
* the docs</a> for more.
*
* @param request the request to create a service account token
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response from the create service account token call
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public CreateServiceAccountTokenResponse createServiceAccountToken(final CreateServiceAccountTokenRequest request,
final RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::createServiceAccountToken, options,
CreateServiceAccountTokenResponse::fromXContent, emptySet());
}

/**
* Asynchronously creates a service account token.<br>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-service-token.html">
* the docs</a> for more.
*
* @param request the request to create a service account token
* @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
* @return cancellable that may be used to cancel the request
*/
public Cancellable createServiceAccountTokenAsync(final CreateServiceAccountTokenRequest request,
final RequestOptions options,
final ActionListener<CreateServiceAccountTokenResponse> listener) {
return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::createServiceAccountToken, options,
CreateServiceAccountTokenResponse::fromXContent, listener, emptySet());
}

/**
* Delete a service account token.<br>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-delete-service-token.html">
* the docs</a> for more.
*
* @param request the request to delete a service account token
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response from the create service account token call
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public DeleteServiceAccountTokenResponse deleteServiceAccountToken(final DeleteServiceAccountTokenRequest request,
final RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deleteServiceAccountToken, options,
DeleteServiceAccountTokenResponse::fromXContent, emptySet());
}

/**
* Asynchronously deletes a service account token.<br>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-delete-service-token.html">
* the docs</a> for more.
*
* @param request the request to delete a service account token
* @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
* @return cancellable that may be used to cancel the request
*/
public Cancellable deleteServiceAccountTokenAsync(final DeleteServiceAccountTokenRequest request,
final RequestOptions options,
final ActionListener<DeleteServiceAccountTokenResponse> listener) {
return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::deleteServiceAccountToken, options,
DeleteServiceAccountTokenResponse::fromXContent, listener, emptySet());
}

/**
* Get credentials for a service account synchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-service-credentails.html">
* the docs</a> for more information.
* @param request the request with namespace and service-name
* @param options the request options (e.g., headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response from the get service accounts call
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public GetServiceAccountCredentialsResponse getServiceAccountCredentials(GetServiceAccountCredentialsRequest request,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getServiceAccountCredentials,
options, GetServiceAccountCredentialsResponse::fromXContent, emptySet());
}

/**
* Get credentials for a service account asynchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-service-credentails.html">
* the docs</a> for more information.
* @param request the request with namespace and service-name
* @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
* @return cancellable that may be used to cancel the request
*/
public Cancellable getServiceAccountCredentialsAsync(GetServiceAccountCredentialsRequest request, RequestOptions options,
ActionListener<GetServiceAccountCredentialsResponse> listener) {
return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getServiceAccountCredentials,
options, GetServiceAccountCredentialsResponse::fromXContent, listener, emptySet());
}

/**
* Get an Elasticsearch access token from an {@code X509Certificate} chain. The certificate chain is that of the client from a mutually
* authenticated TLS session, and it is validated by the PKI realms with {@code delegation.enabled} toggled to {@code true}.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@
import org.elasticsearch.client.security.ClearPrivilegesCacheRequest;
import org.elasticsearch.client.security.ClearRealmCacheRequest;
import org.elasticsearch.client.security.ClearRolesCacheRequest;
import org.elasticsearch.client.security.ClearServiceAccountTokenCacheRequest;
import org.elasticsearch.client.security.CreateApiKeyRequest;
import org.elasticsearch.client.security.CreateServiceAccountTokenRequest;
import org.elasticsearch.client.security.CreateTokenRequest;
import org.elasticsearch.client.security.DelegatePkiAuthenticationRequest;
import org.elasticsearch.client.security.DeletePrivilegesRequest;
import org.elasticsearch.client.security.DeleteRoleMappingRequest;
import org.elasticsearch.client.security.DeleteRoleRequest;
import org.elasticsearch.client.security.DeleteServiceAccountTokenRequest;
import org.elasticsearch.client.security.DeleteUserRequest;
import org.elasticsearch.client.security.DisableUserRequest;
import org.elasticsearch.client.security.EnableUserRequest;
import org.elasticsearch.client.security.GetApiKeyRequest;
import org.elasticsearch.client.security.GetPrivilegesRequest;
import org.elasticsearch.client.security.GetRoleMappingsRequest;
import org.elasticsearch.client.security.GetRolesRequest;
import org.elasticsearch.client.security.GetServiceAccountCredentialsRequest;
import org.elasticsearch.client.security.GetServiceAccountsRequest;
import org.elasticsearch.client.security.GetUsersRequest;
import org.elasticsearch.client.security.GrantApiKeyRequest;
import org.elasticsearch.client.security.HasPrivilegesRequest;
Expand Down Expand Up @@ -193,6 +198,17 @@ static Request clearApiKeyCache(ClearApiKeyCacheRequest clearApiKeyCacheRequest)
return new Request(HttpPost.METHOD_NAME, endpoint);
}

static Request clearServiceAccountTokenCache(ClearServiceAccountTokenCacheRequest clearServiceAccountTokenCacheRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_security/service")
.addPathPart(clearServiceAccountTokenCacheRequest.getNamespace(), clearServiceAccountTokenCacheRequest.getServiceName())
.addPathPartAsIs("credential/token")
.addCommaSeparatedPathParts(clearServiceAccountTokenCacheRequest.getTokenNames())
.addPathPart("_clear_cache")
.build();
return new Request(HttpPost.METHOD_NAME, endpoint);
}

static Request deleteRoleMapping(DeleteRoleMappingRequest deleteRoleMappingRequest) {
final String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_security/role_mapping")
Expand Down Expand Up @@ -329,4 +345,58 @@ static Request invalidateApiKey(final InvalidateApiKeyRequest invalidateApiKeyRe
request.setEntity(createEntity(invalidateApiKeyRequest, REQUEST_BODY_CONTENT_TYPE));
return request;
}

static Request getServiceAccounts(final GetServiceAccountsRequest getServiceAccountsRequest) {
final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_security/service");
if (getServiceAccountsRequest.getNamespace() != null) {
endpointBuilder.addPathPart(getServiceAccountsRequest.getNamespace());
if (getServiceAccountsRequest.getServiceName() != null) {
endpointBuilder.addPathPart(getServiceAccountsRequest.getServiceName());
}
}
return new Request(HttpGet.METHOD_NAME, endpointBuilder.build());
}

static Request createServiceAccountToken(final CreateServiceAccountTokenRequest createServiceAccountTokenRequest) throws IOException {
final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_security/service")
.addPathPart(createServiceAccountTokenRequest.getNamespace(), createServiceAccountTokenRequest.getServiceName())
.addPathPartAsIs("credential/token");
if (createServiceAccountTokenRequest.getTokenName() != null) {
endpointBuilder.addPathPart(createServiceAccountTokenRequest.getTokenName());
}
final Request request = new Request(HttpPost.METHOD_NAME, endpointBuilder.build());
final RequestConverters.Params params = new RequestConverters.Params();
if (createServiceAccountTokenRequest.getRefreshPolicy() != null) {
params.withRefreshPolicy(createServiceAccountTokenRequest.getRefreshPolicy());
}
request.addParameters(params.asMap());
return request;
}

static Request deleteServiceAccountToken(final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest) {
final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_security/service")
.addPathPart(deleteServiceAccountTokenRequest.getNamespace(), deleteServiceAccountTokenRequest.getServiceName())
.addPathPartAsIs("credential/token")
.addPathPart(deleteServiceAccountTokenRequest.getTokenName());

final Request request = new Request(HttpDelete.METHOD_NAME, endpointBuilder.build());
final RequestConverters.Params params = new RequestConverters.Params();
if (deleteServiceAccountTokenRequest.getRefreshPolicy() != null) {
params.withRefreshPolicy(deleteServiceAccountTokenRequest.getRefreshPolicy());
}
request.addParameters(params.asMap());
return request;
}

static Request getServiceAccountCredentials(final GetServiceAccountCredentialsRequest getServiceAccountCredentialsRequest) {
final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_security/service")
.addPathPart(getServiceAccountCredentialsRequest.getNamespace(), getServiceAccountCredentialsRequest.getServiceName())
.addPathPartAsIs("credential");

return new Request(HttpGet.METHOD_NAME, endpointBuilder.build());
}
}
Loading