Skip to content

Commit 8daa854

Browse files
authored
[HLRC] Add support for get roles API (#35787)
This commits adds support for the Get Roles API to the HLRC Relates: #29827
1 parent dfd93de commit 8daa854

File tree

12 files changed

+677
-54
lines changed

12 files changed

+677
-54
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java

+31
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import org.elasticsearch.client.security.GetPrivilegesResponse;
4343
import org.elasticsearch.client.security.GetRoleMappingsRequest;
4444
import org.elasticsearch.client.security.GetRoleMappingsResponse;
45+
import org.elasticsearch.client.security.GetRolesRequest;
46+
import org.elasticsearch.client.security.GetRolesResponse;
4547
import org.elasticsearch.client.security.GetSslCertificatesRequest;
4648
import org.elasticsearch.client.security.GetSslCertificatesResponse;
4749
import org.elasticsearch.client.security.HasPrivilegesRequest;
@@ -407,6 +409,35 @@ public DeleteRoleMappingResponse deleteRoleMapping(DeleteRoleMappingRequest requ
407409
DeleteRoleMappingResponse::fromXContent, emptySet());
408410
}
409411

412+
/**
413+
* Asynchronously retrieves roles from the native roles store.
414+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-role.html">
415+
* the docs</a> for more.
416+
*
417+
* @param request the request with the roles to get
418+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
419+
* @param listener the listener to be notified upon request completion
420+
*/
421+
public void getRolesAsync(GetRolesRequest request, RequestOptions options, ActionListener<GetRolesResponse> listener) {
422+
restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getRoles, options,
423+
GetRolesResponse::fromXContent, listener, emptySet());
424+
}
425+
426+
/**
427+
* Retrieves roles from the native roles store.
428+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-role.html">
429+
* the docs</a> for more.
430+
*
431+
* @param request the request with the roles to get
432+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
433+
* @return the response from the delete role call
434+
* @throws IOException in case there is a problem sending the request or parsing back the response
435+
*/
436+
public GetRolesResponse getRoles(final GetRolesRequest request, final RequestOptions options) throws IOException {
437+
return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getRoles, options,
438+
GetRolesResponse::fromXContent, emptySet());
439+
}
440+
410441
/**
411442
* Asynchronously delete a role mapping.
412443
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-delete-role-mapping.html">

client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.client.security.DeleteRoleMappingRequest;
3333
import org.elasticsearch.client.security.DeleteRoleRequest;
3434
import org.elasticsearch.client.security.InvalidateTokenRequest;
35+
import org.elasticsearch.client.security.GetRolesRequest;
3536
import org.elasticsearch.client.security.PutRoleMappingRequest;
3637
import org.elasticsearch.client.security.HasPrivilegesRequest;
3738
import org.elasticsearch.client.security.DisableUserRequest;
@@ -170,6 +171,15 @@ static Request deleteRole(DeleteRoleRequest deleteRoleRequest) {
170171
return request;
171172
}
172173

174+
static Request getRoles(GetRolesRequest getRolesRequest) {
175+
RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder();
176+
builder.addPathPartAsIs("_xpack/security/role");
177+
if (getRolesRequest.getRoleNames().size() > 0) {
178+
builder.addPathPart(Strings.collectionToCommaDelimitedString(getRolesRequest.getRoleNames()));
179+
}
180+
return new Request(HttpGet.METHOD_NAME, builder.build());
181+
}
182+
173183
static Request createToken(CreateTokenRequest createTokenRequest) throws IOException {
174184
Request request = new Request(HttpPost.METHOD_NAME, "/_xpack/security/oauth2/token");
175185
request.setEntity(createEntity(createTokenRequest, REQUEST_BODY_CONTENT_TYPE));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.security;
21+
22+
import org.elasticsearch.client.Validatable;
23+
import org.elasticsearch.common.util.set.Sets;
24+
25+
import java.util.Collections;
26+
import java.util.Objects;
27+
import java.util.Set;
28+
29+
/**
30+
* Request object to retrieve roles from the native roles store
31+
*/
32+
public final class GetRolesRequest implements Validatable {
33+
34+
private final Set<String> roleNames;
35+
36+
public GetRolesRequest(final String... roleNames) {
37+
if (roleNames != null) {
38+
this.roleNames = Collections.unmodifiableSet(Sets.newHashSet(roleNames));
39+
} else {
40+
this.roleNames = Collections.emptySet();
41+
}
42+
}
43+
44+
public Set<String> getRoleNames() {
45+
return roleNames;
46+
}
47+
48+
@Override
49+
public boolean equals(Object o) {
50+
if (this == o) {
51+
return true;
52+
}
53+
if (o == null || getClass() != o.getClass()) {
54+
return false;
55+
}
56+
final GetRolesRequest that = (GetRolesRequest) o;
57+
return Objects.equals(roleNames, that.roleNames);
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hash(roleNames);
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.security;
21+
22+
import org.elasticsearch.client.security.user.privileges.Role;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
import org.elasticsearch.common.xcontent.XContentParserUtils;
25+
26+
import java.io.IOException;
27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.List;
30+
import java.util.Objects;
31+
32+
/**
33+
* Response when requesting one or more roles.
34+
* Returns a List of {@link Role} objects
35+
*/
36+
public final class GetRolesResponse {
37+
38+
private final List<Role> roles;
39+
40+
public GetRolesResponse(List<Role> roles) {
41+
this.roles = Collections.unmodifiableList(roles);
42+
}
43+
44+
public List<Role> getRoles() {
45+
return roles;
46+
}
47+
48+
public static GetRolesResponse fromXContent(XContentParser parser) throws IOException {
49+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
50+
final List<Role> roles = new ArrayList<>();
51+
XContentParser.Token token;
52+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
53+
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation);
54+
roles.add(Role.PARSER.parse(parser, parser.currentName()));
55+
}
56+
return new GetRolesResponse(roles);
57+
}
58+
59+
@Override
60+
public boolean equals(Object o) {
61+
if (this == o) return true;
62+
if (o == null || getClass() != o.getClass()) return false;
63+
GetRolesResponse response = (GetRolesResponse) o;
64+
return Objects.equals(roles, response.roles);
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return Objects.hash(roles);
70+
}
71+
}

client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/IndicesPrivileges.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public static final class Builder {
249249
private @Nullable Collection<String> deniedFields = null;
250250
private @Nullable String query = null;
251251

252-
private Builder() {
252+
public Builder() {
253253
}
254254

255255
public Builder indices(String... indices) {

0 commit comments

Comments
 (0)