Skip to content
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 7 commits into from
Oct 28, 2018

Conversation

bizybot
Copy link
Contributor

@bizybot bizybot commented Oct 19, 2018

This commit adds support for get role mappings API in HLRC.

This commit adds support for get role mappings API
in HLRC.
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-security

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-role-mapping.html">
* the docs</a> for more.
*
* @param request request {@link GetRoleMappingsRequest} with role mapping name(s).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/request request/request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, Thank you.

*/
public final class ExpressionRoleMapping {

static final ObjectParser<Builder, String> PARSER = new ObjectParser<>("role-mapping", Builder::new);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using a ConstructingObjectParser? You get the arguments and then can just return an ExpressionRoleMapping instead of the builder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have modified this to use ConstructingObjectParser but could not make it work to create ExpressionRoleMapping instead of the builder. The structure that I was trying to parse is as follows:

{
    "kerberosmapping" : { ... },
    "ldapmapping" : { ... }
}

I tried using the named object parser but it expected a field wrapping the list of objects or else it will throw an error. May be I am missing something here, if you feel that there is a way we can do please suggest. Thank you.

* Request object to get role mappings
*/
public final class GetRoleMappingsRequest implements Validatable, ToXContentObject {
private Set<String> roleMappingNames = new HashSet<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it final, set the value in the constructor, and wrap in an unmodifiable set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Thank you.

}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this? I do not think everything needs to implement ToXContentObject since it is ok to have an empty body IIUC

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need it, removed it, Thanks.

*/
public final class GetRoleMappingsResponse {

private List<ExpressionRoleMapping> mappings;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this variable final

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Thank you.

public static GetRoleMappingsResponse fromXContent(XContentParser parser) throws IOException {
List<ExpressionRoleMapping> roleMappings = new ArrayList<>();

parser.nextToken();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use XContentParserUtils#ensureExpectedToken in this parsing code. There are also other useful methods in that class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Thank you.

@@ -101,6 +104,25 @@ public void testPutRoleMapping() throws IOException {
assertToXContentBody(putRoleMappingRequest, request.getEntity());
}

public void testGetRoleMappings() throws IOException {
int noOfRoleMappingNames = randomIntBetween(0, 2);
final String[] roleMappingNames = randomArray(noOfRoleMappingNames, noOfRoleMappingNames, String[]::new, () -> randomAlphaOfLength(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might read better if you wrap after the = so the right hand side is all on one line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Thank you.

if (noOfRoleMappingNames == 0) {
assertEquals("/_xpack/security/role_mapping", request.getEndpoint());
} else {
assertEquals("/_xpack/security/role_mapping/" + Strings.collectionToCommaDelimitedString(getRoleMappingsRequest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like splitting a statement across multiple lines like this. I'd prefer to have the whole Strings.collection... call on one line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Thank you.

public final class ExpressionRoleMapping {

@SuppressWarnings("unchecked")
static final ConstructingObjectParser<ExpressionRoleMapping.Builder, Void> PARSER = new ConstructingObjectParser<>("role-mapping", true,
Copy link
Member

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 a String and when you call this use:

            String name = parser.currentName();
            ExpressionRoleMapping mapping = ExpressionRoleMapping.PARSER.parse(parser, name);

then the function to build would be:

(args, name) -> new ExpressionRoleMapping(name, (List<String>) args[0], (RoleMapperExpression) args[1], (Map<String, Object>) args[2], (boolean) args[3]);

Obviously that is missing the checks for values, but those should be in the constructor of ExpressionRoleMapping anyway

Copy link
Contributor Author

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?

Copy link
Member

@jaymode jaymode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@colings86 colings86 added v6.6.0 and removed v6.5.0 labels Oct 25, 2018
@bizybot bizybot merged commit a5ee134 into elastic:master Oct 28, 2018
bizybot added a commit that referenced this pull request Oct 28, 2018
This commit adds support for get role mappings API
in HLRC.
@bizybot bizybot deleted the hlrc-get-role-mapping-api branch October 28, 2018 23:25
kcm pushed a commit that referenced this pull request Oct 30, 2018
This commit adds support for get role mappings API
in HLRC.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants