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

Refactor role descriptor parsing #107430

Merged

Conversation

slobodanadamovic
Copy link
Contributor

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.

Relates to: #107088

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.
@slobodanadamovic slobodanadamovic added >non-issue :Security/Authorization Roles, Privileges, DLS/FLS, RBAC/ABAC Team:Security Meta label for security team labels Apr 12, 2024
@slobodanadamovic slobodanadamovic self-assigned this Apr 12, 2024
return parse(name, parser, allow2xFormat, allowRestriction);
}
}
public static final class Parser {
Copy link
Contributor Author

@slobodanadamovic slobodanadamovic Apr 12, 2024

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.

@slobodanadamovic slobodanadamovic marked this pull request as ready for review April 12, 2024 17:05
@elasticsearchmachine
Copy link
Collaborator

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
Copy link
Contributor

@n1v0lg n1v0lg left a 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));
Copy link
Contributor

@n1v0lg n1v0lg Apr 15, 2024

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).

Copy link
Contributor Author

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!

@slobodanadamovic slobodanadamovic merged commit a09ae3f into elastic:main Apr 16, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>non-issue :Security/Authorization Roles, Privileges, DLS/FLS, RBAC/ABAC Team:Security Meta label for security team v8.14.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants