-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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
[HLRC] Add support for get role mappings API #34637
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4ab8e97
[HLRC] Add support for get role mappings API
5567756
Fix precommit errors.
64487a3
Address review comments
84767c0
Merge branch 'master' into hlrc-get-role-mapping-api
75ae8eb
Address review comments
413b688
Merge branch 'master' into hlrc-get-role-mapping-api
79e3b7f
Fix precommit errors
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
...est-high-level/src/main/java/org/elasticsearch/client/security/ExpressionRoleMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* | ||
* 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.security.support.expressiondsl.RoleMapperExpression; | ||
import org.elasticsearch.client.security.support.expressiondsl.parser.RoleMapperExpressionParser; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
|
||
/** | ||
* A representation of a single role-mapping. | ||
* | ||
* @see RoleMapperExpression | ||
* @see RoleMapperExpressionParser | ||
*/ | ||
public final class ExpressionRoleMapping { | ||
|
||
@SuppressWarnings("unchecked") | ||
static final ConstructingObjectParser<ExpressionRoleMapping.Builder, Void> PARSER = new ConstructingObjectParser<>("role-mapping", true, | ||
args -> new ExpressionRoleMapping.Builder((List<String>) args[0], (RoleMapperExpression) args[1], (Map<String, Object>) args[2], | ||
(boolean) args[3])); | ||
|
||
static { | ||
PARSER.declareStringArray(constructorArg(), Fields.ROLES); | ||
PARSER.declareField(constructorArg(), (parser, context) -> RoleMapperExpressionParser.fromXContent(parser), Fields.RULES, | ||
ObjectParser.ValueType.OBJECT); | ||
PARSER.declareField(constructorArg(), XContentParser::map, Fields.METADATA, ObjectParser.ValueType.OBJECT); | ||
PARSER.declareBoolean(constructorArg(), Fields.ENABLED); | ||
} | ||
|
||
private final String name; | ||
private final RoleMapperExpression expression; | ||
private final List<String> roles; | ||
private final Map<String, Object> metadata; | ||
private final boolean enabled; | ||
|
||
/** | ||
* Constructor for role mapping | ||
* | ||
* @param name role mapping name | ||
* @param expr {@link RoleMapperExpression} Expression used for role mapping | ||
* @param roles list of roles to be associated with the user | ||
* @param metadata metadata that helps to identify which roles are assigned | ||
* to the user | ||
* @param enabled a flag when {@code true} signifies the role mapping is active | ||
*/ | ||
public ExpressionRoleMapping(final String name, final RoleMapperExpression expr, final List<String> roles, | ||
final Map<String, Object> metadata, boolean enabled) { | ||
this.name = name; | ||
this.expression = expr; | ||
this.roles = Collections.unmodifiableList(roles); | ||
this.metadata = (metadata == null) ? Collections.emptyMap() : Collections.unmodifiableMap(metadata); | ||
this.enabled = enabled; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public RoleMapperExpression getExpression() { | ||
return expression; | ||
} | ||
|
||
public List<String> getRoles() { | ||
return roles; | ||
} | ||
|
||
public Map<String, Object> getMetadata() { | ||
return metadata; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + (enabled ? 1231 : 1237); | ||
result = prime * result + ((expression == null) ? 0 : expression.hashCode()); | ||
result = prime * result + ((metadata == null) ? 0 : metadata.hashCode()); | ||
result = prime * result + ((name == null) ? 0 : name.hashCode()); | ||
result = prime * result + ((roles == null) ? 0 : roles.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
final ExpressionRoleMapping other = (ExpressionRoleMapping) obj; | ||
if (enabled != other.enabled) | ||
return false; | ||
if (expression == null) { | ||
if (other.expression != null) | ||
return false; | ||
} else if (!expression.equals(other.expression)) | ||
return false; | ||
if (metadata == null) { | ||
if (other.metadata != null) | ||
return false; | ||
} else if (!metadata.equals(other.metadata)) | ||
return false; | ||
if (name == null) { | ||
if (other.name != null) | ||
return false; | ||
} else if (!name.equals(other.name)) | ||
return false; | ||
if (roles == null) { | ||
if (other.roles != null) | ||
return false; | ||
} else if (!roles.equals(other.roles)) | ||
return false; | ||
return true; | ||
} | ||
|
||
/** | ||
* Used to facilitate the use of {@link ObjectParser} (via {@link #PARSER}). | ||
*/ | ||
static class Builder { | ||
private RoleMapperExpression rules; | ||
private List<String> roles; | ||
private Map<String, Object> metadata = Collections.emptyMap(); | ||
private Boolean enabled; | ||
|
||
Builder(List<String> roles, RoleMapperExpression expression, Map<String, Object> metadata, boolean enabled) { | ||
this.rules = expression; | ||
this.roles = Collections.unmodifiableList(roles); | ||
this.metadata = Collections.unmodifiableMap(metadata); | ||
this.enabled = enabled; | ||
} | ||
|
||
ExpressionRoleMapping build(String name) { | ||
if (roles == null) { | ||
throw missingField(name, Fields.ROLES); | ||
} | ||
if (rules == null) { | ||
throw missingField(name, Fields.RULES); | ||
} | ||
if (enabled == null) { | ||
throw missingField(name, Fields.ENABLED); | ||
} | ||
return new ExpressionRoleMapping(name, rules, roles, metadata, enabled); | ||
} | ||
|
||
private IllegalStateException missingField(String id, ParseField field) { | ||
return new IllegalStateException("failed to parse role-mapping [" + id + "]. missing field [" + field + "]"); | ||
} | ||
|
||
} | ||
|
||
public interface Fields { | ||
ParseField ROLES = new ParseField("roles"); | ||
ParseField ENABLED = new ParseField("enabled"); | ||
ParseField RULES = new ParseField("rules"); | ||
ParseField METADATA = new ParseField("metadata"); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...st-high-level/src/main/java/org/elasticsearch/client/security/GetRoleMappingsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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 org.elasticsearch.common.util.set.Sets; | ||
|
||
import java.util.Collections; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* Request object to get role mappings | ||
*/ | ||
public final class GetRoleMappingsRequest implements Validatable { | ||
private final Set<String> roleMappingNames; | ||
|
||
public GetRoleMappingsRequest(final String... roleMappingNames) { | ||
if (roleMappingNames != null) { | ||
this.roleMappingNames = Collections.unmodifiableSet(Sets.newHashSet(roleMappingNames)); | ||
} else { | ||
this.roleMappingNames = Collections.emptySet(); | ||
} | ||
} | ||
|
||
public Set<String> getRoleMappingNames() { | ||
return roleMappingNames; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(roleMappingNames); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
final GetRoleMappingsRequest other = (GetRoleMappingsRequest) obj; | ||
|
||
return Objects.equals(roleMappingNames, other.roleMappingNames); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can get rid of the builder and just return an expressionrolemapping by changing the
Void
to aString
and when you call this use:then the function to build would be:
Obviously that is missing the checks for values, but those should be in the constructor of ExpressionRoleMapping anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, Jay!, this works I missed the context part.
On the missing checks, I think we are not validating in the constructors for the response objects assuming them to be created appropriately. I see this pattern followed in the security rest client response objects, do you think we need to add the checks?