Skip to content

Conversation

@n1v0lg
Copy link
Contributor

@n1v0lg n1v0lg commented Oct 25, 2022

This PR refactors functionality for building a role from a single role
descriptor. This used to be handled by a specialized constructor of the
Role.Builder class that accessed internal fields. This PR consolidates
this logic into a static method that uses the builder's canonical
methods instead. The method is used to construct the roles of internal
users, from static role descriptors, as well as in test code.

Relates: #90614 (comment)

@n1v0lg n1v0lg added >refactoring :Security/Security Security issues without another label labels Oct 25, 2022
@n1v0lg n1v0lg self-assigned this Oct 25, 2022
@n1v0lg n1v0lg changed the title Refactor role from role descriptor Refactor building roles from role descriptors Oct 25, 2022
// TODO handle this when we introduce remote index privileges for built-in users and roles. That's the only production code
// using this builder
assert false == roleDescriptor.hasRemoteIndicesPrivileges();
Objects.requireNonNull(fieldPermissionsCache);
Copy link
Contributor Author

@n1v0lg n1v0lg Oct 25, 2022

Choose a reason for hiding this comment

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

fieldPermissionsCache was nulleable before, however it was only ever null in test code. Opting to make it not nulleable since test code should not impact production code. Passing an instantiated FieldPermissionsCache in the relevant tests does not incur a notable overhead.

@n1v0lg n1v0lg changed the title Refactor building roles from role descriptors Refactor building role from single role descriptor Oct 25, 2022
@n1v0lg n1v0lg marked this pull request as ready for review October 25, 2022 09:06
@n1v0lg n1v0lg requested a review from ywangd October 25, 2022 09:06
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-security (Team:Security)

@elasticsearchmachine elasticsearchmachine added the Team:Security Meta label for security team label Oct 25, 2022
@n1v0lg n1v0lg added the v8.6.0 label Oct 25, 2022
Copy link
Member

@ywangd ywangd left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +346 to +355
for (RoleDescriptor.ApplicationResourcePrivileges applicationPrivilege : roleDescriptor.getApplicationPrivileges()) {
builder.addApplicationPrivilege(
new ApplicationPrivilege(
applicationPrivilege.getApplication(),
Sets.newHashSet(applicationPrivilege.getPrivileges()),
applicationPrivilege.getPrivileges()
),
Sets.newHashSet(applicationPrivilege.getResources())
);
}
Copy link
Member

Choose a reason for hiding this comment

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

It's not part of the current code. But I think it is worthwhile to assert that all privilege names are patterns (e.g. *) instead of concrete names (this is already the case for all production code and likely tests as well). The code here does not really work if there are concrete names because we need to look up what actions the concrete name include from the security index, e.g. kibana's space_all includes login:, saved_object:tag/get and a bunch of other actions.

Concretely, what I am suggesting is something like

assert Stream.of(applicationPrivilege.getPrivileges()).noneMatch(ApplicationPrivilege::isValidPrivilegeName)

similar to the check in NativePrivilegeStore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Added the assertion 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's quite a few tests that fail this assertion, e.g., testElasticFleetServerPrivileges because we build a role for fleet account which includes concrete app privilge names. I'll merge without the assertion and follow up with a PR that addresses this, to better separate pure refactoring from bigger "functional" changes.

Copy link
Member

Choose a reason for hiding this comment

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

I was wondering whether it would catch something like this. Yeah, separate PR is fine. Should just be a test issue, but it might need some work to get around it. Thanks!

@n1v0lg n1v0lg merged commit 3baaab5 into elastic:main Oct 26, 2022
@n1v0lg n1v0lg deleted the refactor-role-from-role-descriptor branch October 26, 2022 09:07
n1v0lg added a commit that referenced this pull request Oct 28, 2022
Building a role from a single role descriptor previously only worked
correctly for application privileges with wildcard patterns. This PR
adds support for concrete name look up and ports the relevant tests.

Relates: #91107 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>refactoring :Security/Security Security issues without another label Team:Security Meta label for security team v8.6.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants