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

Test unknown named application privileges #104863

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public Set<String> getResourcePatterns(ApplicationPrivilege privilege) {
.collect(Collectors.toSet());
}

public Set<String> getResourcePatternsByName(ApplicationPrivilege privilege) {
return permissions.stream()
.filter(e -> e.privilege.getApplication().equals(privilege.getApplication()) && e.privilege.name().equals(privilege.name()))
.map(e -> e.resourceNames)
.flatMap(Set::stream)
.collect(Collectors.toSet());
}

private static class PermissionEntry {
private final ApplicationPrivilege privilege;
private final Predicate<String> application;
Expand All @@ -191,18 +199,18 @@ private boolean grants(ApplicationPrivilege other, Automaton resource) {
}

private boolean matchesPrivilege(ApplicationPrivilege other) {
if (this.privilege.equals(other)) {
return true;
}
if (this.application.test(other.getApplication()) == false) {
assert other.name().size() <= 1 : "can only compare to singletons but got [" + other + "]";
if (application.test(other.getApplication()) == false) {
return false;
}
if (Operations.isTotal(privilege.getAutomaton())) {
return true;
}
return Operations.isEmpty(privilege.getAutomaton()) == false
&& Operations.isEmpty(other.getAutomaton()) == false
&& Operations.subsetOf(other.getAutomaton(), privilege.getAutomaton());
// If the privilege we are checking is not stored (i.e., maps to no action patterns) check by name instead
if (Operations.isEmpty(other.getAutomaton())) {
return privilege.name().containsAll(other.name());
}
return Operations.subsetOf(other.getAutomaton(), privilege.getAutomaton());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ public void testUserWithWildcardPrivileges() throws Exception {
}
}

public void testNamed() throws IOException {
createApplicationPrivilege("app", "write", new String[] { "action:write/*" });
createRole("correct", "app", new String[] { "read" }, new String[] { "*" });
createRole("wrong", "app", new String[] { "read", randomFrom("create", "write") }, new String[] { "*" });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only difference between the roles: "single" unknown privilege or unknown privilege + plus another (known or unknown, doesn't matter).


var password = new SecureString("password-123");
createUser("correct", password, Set.of("correct"));
createUser("wrong", password, Set.of("wrong"));

{
var reqOptions = RequestOptions.DEFAULT.toBuilder()
.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue("correct", password))
.build();
var actual = hasPrivilege(reqOptions, "app", new String[] { "read" }, new String[] { "resource" });
assertSinglePrivilege(actual, "resource", "read", true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This works and is expected behavior based on: #33928 (comment)

}

{
var reqOptions = RequestOptions.DEFAULT.toBuilder()
.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue("wrong", password))
.build();
var actual = hasPrivilege(reqOptions, "app", new String[] { "read" }, new String[] { "resource" });
assertSinglePrivilege(actual, "resource", "read", true);
Copy link
Contributor Author

@n1v0lg n1v0lg Jan 29, 2024

Choose a reason for hiding this comment

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

This fails but shouldn't.

Copy link
Contributor Author

@n1v0lg n1v0lg Jan 29, 2024

Choose a reason for hiding this comment

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

This is the problem:

We are checking for equality, but when there's more than one privilege on the Role, we fail this check since name is Set.of("create", "read") for the privilege on the role, and it does not equal Set.of("read") -- the privilege from the request (i.e., other)

}
}

private void assertSinglePrivilege(
List<ResourcePrivileges> hasPrivilegesResult,
String expectedResource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ static GetUserPrivilegesResponse buildUserPrivilegesResponseObject(Role userRole
final Set<RoleDescriptor.ApplicationResourcePrivileges> application = new LinkedHashSet<>();
for (String applicationName : userRole.application().getApplicationNames()) {
for (ApplicationPrivilege privilege : userRole.application().getPrivileges(applicationName)) {
final Set<String> resources = userRole.application().getResourcePatterns(privilege);
final Set<String> resources = userRole.application().getResourcePatternsByName(privilege);
if (resources.isEmpty()) {
logger.trace("No resources defined in application privilege {}", privilege);
} else {
Expand Down