-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Refactor role descriptor parsing #107430
Refactor role descriptor parsing #107430
Conversation
The method for `RoleDescriptor` parsing is becoming more complex due to its usage being shared between API keys, file-based and native roles. In some cases we allow 2.X format, in others we disallow restrictions. Having a single method with multiple boolean flags that control inclusion/exclusion of fields is becoming hard to extend. This refactoring aims to allow easier introduction of new fields that should be conditionally supported in some cases but not others. One of such cases is introduction of `description` field that should only be supported for file-based and native roles but not for roles embedded in API keys.
return parse(name, parser, allow2xFormat, allowRestriction); | ||
} | ||
} | ||
public static final class Parser { |
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.
The main change here is pulling parse
method to the RoleDescriptor.Parser
class and making allow2xFormat and allowRestriction, properties of the parser class - instead of parameters of the parse method.
Pinging @elastic/es-security (Team:Security) |
…tor-role-parsing # Conflicts: # x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/FileRolesStore.java
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.
LGTM! This is a nice clean up 🧹 One optional suggestion around avoiding instantiating objects on every parse call (see my inline comment).
@@ -70,7 +70,7 @@ public static RoleDescriptorsIntersection fromXContent(XContentParser xContentPa | |||
while ((token = p.nextToken()) != XContentParser.Token.END_OBJECT) { | |||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, p); | |||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, p.nextToken(), p); | |||
roleDescriptors.add(RoleDescriptor.parse(p.currentName(), p, false)); | |||
roleDescriptors.add(RoleDescriptor.parser().allowRestriction(true).parse(p.currentName(), p)); |
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.
Might extract this into a private static field to avoid instantiating every time we parse (here and in other prod code spots).
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.
Totally, thanks for suggesting it!
The method for
RoleDescriptor
parsing is becoming more complex due toits usage being shared between API keys, file-based and native roles.
In some cases we allow 2.X format, in others we disallow
restrictions. Having a single method with multiple boolean flags that
control inclusion/exclusion of fields is becoming hard to extend.
This refactoring aims to allow easier introduction of new fields that
should be conditionally supported in some cases but not others.
One of such cases is introduction of
description
field that should onlybe supported for file-based and native roles but not for roles embedded
in API keys.
Relates to: #107088