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] Support for role mapper expression dsl #33745

Merged
merged 12 commits into from Sep 27, 2018

Conversation

bizybot
Copy link
Contributor

@bizybot bizybot commented Sep 17, 2018

This commit adds support for role mapping expression dsl.
Functionally it is similar to what we have on the server side
except for the rule evaluation which is not required on the client.

The role mapper expression can either be field expression or
composite expression of one or more expressions. Role mapper
expression parser is used to parse JSON DSL to list of expressions.

This forms the base for role mapping APIs (get, post/put and delete)

This commit adds support for role mapping expression dsl.
Functionally it is similar to what we have on the server side
except the rule evaluation which is not required on the client.

The role mapper expression can either be field expression or
composite expression of one or more expressions. Role mapper
expression parser is used to parse json dsl to list of expressions.

This forms the base for role mapping APIs (get, post/put and delete)
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-security

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra


public T build() {
try {
return ctor.newInstance(new Object[] { elements.toArray(new RoleMapperExpression[0]) });
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should be using reflection for this.
There's only 2 classes to handle, we can do it more simply and safely with a Function<RoleMapperExpression[], T>

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 refactored the code to simplify this. Thank you.

* the provided values. A <em>field</em> expression may have more than one provided value, in which
* case the expression is true if <em>any</em> of the values are matched.
*/
public abstract class FieldExpressionBase implements RoleMapperExpression {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the purpose of having different types for the different fields? The JSON isn't any different based on the field type...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only difference was with Metadata field as the key was dynamic. I have addressed this with refactoring now with a FieldType enum and its usage to distinguish within the builder. Thanks for your feedback.

public FieldExpressionBuilder<T> withKey(String key) {
assert Strings.hasLength(key) : "metadata key cannot be null or empty";
assert MetadataFieldExpression.class.isAssignableFrom(
clazz) : "metadat key can only be provided when building MetadataFieldExpression";
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: s/metadat/metadata/

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, changed this.

ParseField ANY = new ParseField("any");
ParseField ALL = new ParseField("all");
ParseField EXCEPT = new ParseField("except");
ParseField FIELD = new ParseField("field");
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't have both these fields and also the NAME fields on the various expression types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. I initially did not add support for expression parser. I will address this. Thank you.

Yogesh Gaikwad added 3 commits September 18, 2018 13:18
Removed individual expression classes, simiplified
the usage using builders.
Now there is FieldRoleMapperExpression for Field types
(USERNAME, GROUPS, METADATA, DN)
and CompositeRoleMapperExpression for Composite types
(ALL, ANY, EXCEPT)
@bizybot
Copy link
Contributor Author

bizybot commented Sep 21, 2018

Hi @tvernum, I have addressed your review comments, please review when you get some time. Thank you for your review.

Copy link
Contributor

@tvernum tvernum left a comment

Choose a reason for hiding this comment

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

LGTM, with 1 suggestion/preference.

}

public static FieldRoleMapperExpression ofMetadata(String key, Object... values) {
String fieldName = key.startsWith("metadata.") ? key : "metadata." + key;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer we require one or the other. That is always add the "metadata." prefix or throw an exception if the prefix is missing.
Adding it if it doesn't exist feels convenient, but is just a form of leniency that tends to come back and bite us.

Copy link
Member

@jkakavas jkakavas left a comment

Choose a reason for hiding this comment

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

LGTM with two minor comments, sorry for getting to this too late @bizybot

* </pre>
*/
public class FieldRoleMapperExpression implements RoleMapperExpression {
private final String NAME = "field";
Copy link
Member

Choose a reason for hiding this comment

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

nit: s/NAME/name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed this, Thanks.

return parsedFieldName;
}

private List<RoleMapperExpression> parseExpressionArray(ParseField field, XContentParser parser, boolean allowExcept)
Copy link
Member

Choose a reason for hiding this comment

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

parameter allowExcept is not used

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.

@bizybot
Copy link
Contributor Author

bizybot commented Sep 27, 2018

@elasticmachine test this, please

@bizybot bizybot merged commit 53d10bb into elastic:master Sep 27, 2018
bizybot added a commit that referenced this pull request Sep 27, 2018
This commit adds support for role mapping expression dsl.
Functionally it is similar to what we have on the server side
except for the rule evaluation which is not required on the client.

The role mapper expression can either be field expression or
composite expression of one or more expressions. Role mapper
expression parser is used to parse JSON DSL to list of expressions.

This forms the base for role mapping APIs (get, post/put and delete)
jasontedor added a commit to jasontedor/elasticsearch that referenced this pull request Sep 27, 2018
* master: (25 commits)
  [DOCS] Synchronize location of Breaking Changes (elastic#33588)
  [DOCS] Synchronizes captialization in top-level titles (elastic#33605)
  [SQL] Clean up LogicalPlanBuilder#doJoin (elastic#34048)
  Fix remote cluster seeds fallback (elastic#34090)
  [ML][HLRC] Replace REST-based ML test cleanup with the ML client (elastic#34109)
  Handle MatchNoDocsQuery in span query wrappers (elastic#34106)
  Update MovAvgIT AwaitsFix bug url
  Bad regex in CORS settings should throw a nicer error (elastic#34035)
  [HLRC] Support for role mapper expression dsl (elastic#33745)
  Watcher: Reduce script cache churn by checking for mustache tags (elastic#33978)
  Fold EngineSearcher into Engine.Searcher (elastic#34082)
  Mute SpanMultiTermQueryBuilderTests#testToQuery
  TESTS: Enable DEBUG Logging in Flaky Test (elastic#34091)
  TEST: Add engine is closed as expected failure msg
  Adjust bwc version for max_seq_no_of_updates
  Build DocStats from SegmentInfos in ReadOnlyEngine (elastic#34079)
  When creating wildcard queries, use MatchNoDocsQuery when the field type doesn't exist. (elastic#34093)
  [DOCS] Moves graph to docs folder (elastic#33472)
  Mute MovAvgIT#testHoltWintersNotEnoughData
  Security: use default scroll keepalive (elastic#33639)
  ...
atript8 pushed a commit to atript8/elasticsearch that referenced this pull request Sep 28, 2018
This commit adds support for role mapping expression dsl.
Functionally it is similar to what we have on the server side
except for the rule evaluation which is not required on the client.

The role mapper expression can either be field expression or
composite expression of one or more expressions. Role mapper
expression parser is used to parse JSON DSL to list of expressions.

This forms the base for role mapping APIs (get, post/put and delete)
}

public String getName() {
return this.getName();
Copy link
Member

Choose a reason for hiding this comment

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

s/return this.getName();/return this.name;/
Noticed this when reviewing #34171 , maybe you can correct this there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I will address in the other review for the create role mapping API. Thank you.

throw new IllegalArgumentException("null or empty field name (" + field + ")");
}
if (values == null || values.length == 0) {
throw new IllegalArgumentException("null or empty values (" + values + ")");
Copy link
Member

Choose a reason for hiding this comment

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

In case of an empty array, this will invoke toString() on the array which will offer no useful information. Maybe change this to

throw new IllegalArgumentException("null or empty values for field (" + field + ")");

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, will address this in another review.

bizybot added a commit to bizybot/elasticsearch that referenced this pull request Oct 16, 2018
We added support for role mapper expression DSL in elastic#33745,
that allows us to build the role mapper expression used in the
role mapping (as rules for determining user roles based on what
the boolean expression resolves to).

This change now adds support for create/update role mapping
API to the high-level rest client.
bizybot added a commit to bizybot/elasticsearch that referenced this pull request Oct 16, 2018
We added support for role mapper expression DSL in elastic#33745,
that allows us to build the role mapper expression used in the
role mapping (as rules for determining user roles based on what
the boolean expression resolves to).

This change now adds support for create/update role mapping
API to the high-level rest client.
bizybot added a commit that referenced this pull request Oct 16, 2018
We added support for role mapper expression DSL in #33745,
that allows us to build the role mapper expression used in the
role mapping (as rules for determining user roles based on what
the boolean expression resolves to).

This change now adds support for create/update role mapping
API to the high-level rest client.
@colings86 colings86 added >enhancement and removed :Security/Authorization Roles, Privileges, DLS/FLS, RBAC/ABAC labels Oct 25, 2018
kcm pushed a commit that referenced this pull request Oct 30, 2018
This commit adds support for role mapping expression dsl.
Functionally it is similar to what we have on the server side
except for the rule evaluation which is not required on the client.

The role mapper expression can either be field expression or
composite expression of one or more expressions. Role mapper
expression parser is used to parse JSON DSL to list of expressions.

This forms the base for role mapping APIs (get, post/put and delete)
kcm pushed a commit that referenced this pull request Oct 30, 2018
We added support for role mapper expression DSL in #33745,
that allows us to build the role mapper expression used in the
role mapping (as rules for determining user roles based on what
the boolean expression resolves to).

This change now adds support for create/update role mapping
API to the high-level rest client.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants