Skip to content

Commit

Permalink
Use 'should' clause instead of 'filter' when querying native privileg…
Browse files Browse the repository at this point in the history
…es (#47019) (#47271)

When we added support for wildcard application names, we started to build
the prefix query along with the term query but we used 'filter' clause
instead of 'should', so this would not fetch the correct application
privilege descriptor thereby failing the _has_privilege checks.
This commit changes the clause to use should and with minimum_should_match
as 1.
  • Loading branch information
bizybot authored Sep 30, 2019
1 parent cec2ff5 commit 2be351c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ private QueryBuilder getApplicationNameQuery(Collection<String> applications) {
}
final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if (termsQuery != null) {
boolQuery.filter(termsQuery);
boolQuery.should(termsQuery);
}
for (String wildcard : wildcardNames) {
final String prefix = wildcard.substring(0, wildcard.length() - 1);
boolQuery.filter(QueryBuilders.prefixQuery(APPLICATION.getPreferredName(), prefix));
boolQuery.should(QueryBuilders.prefixQuery(APPLICATION.getPreferredName(), prefix));
}
boolQuery.minimumShouldMatch(1);
return boolQuery;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void testGetPrivilegesByWildcardApplicationName() throws Exception {
assertThat(request.indices(), arrayContaining(RestrictedIndicesNames.SECURITY_MAIN_ALIAS));

final String query = Strings.toString(request.source().query());
assertThat(query, containsString("{\"bool\":{\"filter\":[{\"terms\":{\"application\":[\"yourapp\"]"));
assertThat(query, containsString("{\"bool\":{\"should\":[{\"terms\":{\"application\":[\"yourapp\"]"));
assertThat(query, containsString("{\"prefix\":{\"application\":{\"value\":\"myapp-\""));
assertThat(query, containsString("{\"term\":{\"type\":{\"value\":\"application-privilege\""));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ setup:
]
}
- do:
security.put_role:
name: "role_containing_wildcard_app_name_and_plain_app_name"
body: >
{
"cluster": [],
"indices": [],
"applications": [
{
"application": "myapp",
"privileges": ["user"],
"resources": ["*"]
},
{
"application": "yourapp-*",
"privileges": ["read"],
"resources": ["*"]
}
]
}
# And a user for each role
- do:
security.put_user:
Expand All @@ -134,6 +155,14 @@ setup:
"password": "p@ssw0rd",
"roles" : [ "yourapp_read_config" ]
}
- do:
security.put_user:
username: "myapp_yourapp_wildard_role_user"
body: >
{
"password": "p@ssw0rd",
"roles" : [ "role_containing_wildcard_app_name_and_plain_app_name" ]
}
---
teardown:
Expand Down Expand Up @@ -168,6 +197,11 @@ teardown:
username: "your_read"
ignore: 404

- do:
security.delete_user:
username: "myapp_yourapp_wildard_role_user"
ignore: 404

- do:
security.delete_role:
name: "myapp_engineering_read"
Expand All @@ -182,6 +216,12 @@ teardown:
security.delete_role:
name: "yourapp_read_config"
ignore: 404

- do:
security.delete_role:
name: "role_containing_wildcard_app_name_and_plain_app_name"
ignore: 404

---
"Test has_privileges with application-privileges":
- do:
Expand Down Expand Up @@ -291,3 +331,48 @@ teardown:
}
}
} }

- do:
headers: { Authorization: "Basic bXlhcHBfeW91cmFwcF93aWxkYXJkX3JvbGVfdXNlcjpwQHNzdzByZA==" } # myapp_yourapp_wildard_role_user
security.has_privileges:
user: null
body: >
{
"application": [
{
"application" : "myapp",
"resources" : [ "*" ],
"privileges" : [ "action:login" ]
},
{
"application" : "yourapp-v1",
"resources" : [ "*" ],
"privileges" : [ "read" ]
},
{
"application" : "yourapp-v2",
"resources" : [ "*" ],
"privileges" : [ "read" ]
}
]
}
- match: { "username" : "myapp_yourapp_wildard_role_user" }
- match: { "has_all_requested" : true }
- match: { "application" : {
"myapp" : {
"*" : {
"action:login" : true
}
},
"yourapp-v1": {
"*": {
"read": true
}
},
"yourapp-v2": {
"*": {
"read": true
}
}
} }

0 comments on commit 2be351c

Please sign in to comment.