Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,60 @@
/*
* 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;

import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;

/**
* Base class for responses that are node responses. These responses always contain the cluster
* name and the {@link NodesResponseHeader}.
*/
public abstract class NodesResponse {

private final NodesResponseHeader header;
private final String clusterName;

protected NodesResponse(NodesResponseHeader header, String clusterName) {
this.header = header;
this.clusterName = clusterName;
}

/**
* Get the cluster name associated with all of the nodes.
*
* @return Never {@code null}.
*/
public String getClusterName() {
return clusterName;
}

/**
* Gets information about the number of total, successful and failed nodes the request was run on.
* Also includes exceptions if relevant.
*/
public NodesResponseHeader getHeader() {
return header;
}

public static <T extends NodesResponse> void declareCommonNodesResponseParsing(ConstructingObjectParser<T, Void> parser) {
parser.declareObject(ConstructingObjectParser.constructorArg(), NodesResponseHeader::fromXContent, new ParseField("_nodes"));
parser.declareString(ConstructingObjectParser.constructorArg(), new ParseField("cluster_name"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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.ClearRolesCacheRequest;
import org.elasticsearch.client.security.ClearRolesCacheResponse;
import org.elasticsearch.client.security.CreateTokenRequest;
Expand Down Expand Up @@ -241,13 +243,43 @@ public void authenticateAsync(RequestOptions options, ActionListener<Authenticat
}

/**
* Clears the native roles cache for a set of roles.
* 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">
* the docs</a> for more.
*
* @param request the request with the realm names and usernames to clear the cache for
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response from the clear realm cache call
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public ClearRealmCacheResponse clearRealmCache(ClearRealmCacheRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::clearRealmCache, options,
ClearRealmCacheResponse::fromXContent, emptySet());
}

/**
* Clears the cache in one or more realms asynchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-cache.html">
* the docs</a> for more.
*
* @param request the request with the realm names and usernames to clear the cache for
* @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 clearRealmCacheAsync(ClearRealmCacheRequest request, RequestOptions options,
ActionListener<ClearRealmCacheResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::clearRealmCache, options,
ClearRealmCacheResponse::fromXContent, listener, emptySet());
}

/**
* Clears the roles cache for a set of roles.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-role-cache.html">
* the docs</a> for more.
*
* @param request the request with the roles for which the cache should be cleared.
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response from the enable user call
* @return the response from the clear roles cache call
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public ClearRolesCacheResponse clearRolesCache(ClearRolesCacheRequest request, RequestOptions options) throws IOException {
Expand All @@ -256,7 +288,7 @@ public ClearRolesCacheResponse clearRolesCache(ClearRolesCacheRequest request, R
}

/**
* Clears the native roles cache for a set of roles asynchronously.
* Clears the roles cache for a set of roles asynchronously.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-clear-role-cache.html">
* the docs</a> for more.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.security.ClearRealmCacheRequest;
import org.elasticsearch.client.security.ClearRolesCacheRequest;
import org.elasticsearch.client.security.CreateTokenRequest;
import org.elasticsearch.client.security.DeleteRoleMappingRequest;
Expand Down Expand Up @@ -112,6 +113,23 @@ private static Request setUserEnabled(SetUserEnabledRequest setUserEnabledReques
return request;
}

static Request clearRealmCache(ClearRealmCacheRequest clearRealmCacheRequest) {
RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack/security/realm");
if (clearRealmCacheRequest.getRealms().isEmpty() == false) {
builder.addCommaSeparatedPathParts(clearRealmCacheRequest.getRealms().toArray(Strings.EMPTY_ARRAY));
} else {
builder.addPathPart("_all");
}
final String endpoint = builder.addPathPartAsIs("_clear_cache").build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
if (clearRealmCacheRequest.getUsernames().isEmpty() == false) {
RequestConverters.Params params = new RequestConverters.Params(request);
params.putParam("usernames", Strings.collectionToCommaDelimitedString(clearRealmCacheRequest.getUsernames()));
}
return request;
}

static Request clearRolesCache(ClearRolesCacheRequest disableCacheRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack/security/role")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.Collections;
import java.util.List;
import java.util.Objects;

/**
* Request for clearing the cache of one or more realms
*/
public final class ClearRealmCacheRequest implements Validatable {

private final List<String> realms;
private final List<String> usernames;

/**
* Create a new request to clear cache of realms
* @param realms the realms to clear the cache of. Must not be {@code null}. An empty list
* indicates that all realms should have their caches cleared.
* @param usernames the usernames to clear the cache of. Must not be {@code null}. An empty
* list indicates that every user in the listed realms should have their cache
* cleared.
*/
public ClearRealmCacheRequest(List<String> realms, List<String> usernames) {
this.realms = Collections.unmodifiableList(Objects.requireNonNull(realms, "the realms list must not be null"));
this.usernames = Collections.unmodifiableList(Objects.requireNonNull(usernames, "usernames list must no be null"));
}

public List<String> getRealms() {
return realms;
}

public List<String> getUsernames() {
return usernames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.NodesResponseHeader;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.List;

/**
* Response for a clear realm cache request. The response includes a header that contains the
* number of successful and failed nodes.
*/
public final class ClearRealmCacheResponse extends SecurityNodesResponse {

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<ClearRealmCacheResponse, Void> PARSER =
new ConstructingObjectParser<>("clear_realm_cache_response_parser",
args -> new ClearRealmCacheResponse((List<Node>) args[0], (NodesResponseHeader) args[1], (String) args[2]));

static {
SecurityNodesResponse.declareCommonNodesResponseParsing(PARSER);
}

public ClearRealmCacheResponse(List<Node> nodes, NodesResponseHeader header, String clusterName) {
super(nodes, header, clusterName);
}

public static ClearRealmCacheResponse fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,87 +20,28 @@
package org.elasticsearch.client.security;

import org.elasticsearch.client.NodesResponseHeader;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
* The response object that will be returned when clearing the cache of native roles
* The response object that will be returned when clearing the roles cache
*/
public final class ClearRolesCacheResponse {
public final class ClearRolesCacheResponse extends SecurityNodesResponse {

@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<ClearRolesCacheResponse, Void> PARSER =
new ConstructingObjectParser<>("clear_roles_cache_response", false,
args -> new ClearRolesCacheResponse((List<Node>)args[0], (NodesResponseHeader) args[1], (String) args[2]));

static {
PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> Node.PARSER.apply(p, n),
new ParseField("nodes"));
PARSER.declareObject(ConstructingObjectParser.constructorArg(), NodesResponseHeader::fromXContent, new ParseField("_nodes"));
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("cluster_name"));
SecurityNodesResponse.declareCommonNodesResponseParsing(PARSER);
}

private final List<Node> nodes;
private final NodesResponseHeader header;
private final String clusterName;

public ClearRolesCacheResponse(List<Node> nodes, NodesResponseHeader header, String clusterName) {
this.nodes = nodes;
this.header = header;
this.clusterName = Objects.requireNonNull(clusterName, "cluster name must be provided");
}

/** returns a list of nodes in which the cache was cleared */
public List<Node> getNodes() {
return nodes;
}

/**
* Get the cluster name associated with all of the nodes.
*
* @return Never {@code null}.
*/
public String getClusterName() {
return clusterName;
}

/**
* Gets information about the number of total, successful and failed nodes the request was run on.
* Also includes exceptions if relevant.
*/
public NodesResponseHeader getHeader() {
return header;
}

public static class Node {

private static final ConstructingObjectParser<Node, String> PARSER =
new ConstructingObjectParser<>("clear_roles_cache_response_node", false, (args, id) -> new Node(id, (String) args[0]));

static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("name"));
}

private final String id;
private final String name;

public Node(String id, String name) {
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public String getName() {
return name;
}
super(nodes, header, clusterName);
}

public static ClearRolesCacheResponse fromXContent(XContentParser parser) throws IOException {
Expand Down
Loading