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

Validate query field when creating roles #46275

Merged
merged 16 commits into from
Sep 25, 2019
Merged

Conversation

bizybot
Copy link
Contributor

@bizybot bizybot commented Sep 3, 2019

In the current implementation, the validation of the role query
occurs at runtime when the query is being executed.

This commit adds validation for the role query when creating a role
but not for the template query as we do not have the runtime
information required for evaluating the template query (eg. authenticated user's
information). This is similar to the scripts that we
store but do not evaluate or parse if they are valid queries or not.

For validation, the query is evaluated (if not a template), parsed to build the
QueryBuilder and verify if the query type is allowed.

Closes #34252

@bizybot bizybot added >bug :Security/Authorization Roles, Privileges, DLS/FLS, RBAC/ABAC v8.0.0 v7.5.0 labels Sep 3, 2019
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-security

@bizybot bizybot force-pushed the 34252-fix branch 10 times, most recently from 406f3a1 to 6efafea Compare September 5, 2019 03:15
@bizybot
Copy link
Contributor Author

bizybot commented Sep 5, 2019

unrelated failure
@elasticmachine run elasticsearch-ci/2

As of now the validation occurs at runtime when the query is being
executed. I think the reason was due to the use of template queries
which need runtime information as they need to be evaluated like
user information.

This commit adds validation for the role query but **not for the
template query** as we do not have the runtime information required
for evaluating the template query.
This also corrects some tests and roles.yml files where the `query` field was not
populated correctly.
For validation, the query is evaluated (if not a template), parsed to build the
`QueryBuilder` and verify if the query type is allowed.

Closes elastic#34252
@bizybot
Copy link
Contributor Author

bizybot commented Sep 5, 2019

@elasticmachine run elasticsearch-ci/2

@bizybot bizybot marked this pull request as ready for review September 5, 2019 09:47
Copy link
Member

@jkakavas jkakavas left a comment

Choose a reason for hiding this comment

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

Looks good @bizybot , left some comments

}
}
} catch (XContentParseException | IOException e) {
throw new ElasticsearchParseException("failed to parse field 'query' from the role descriptor", e);
Copy link
Member

Choose a reason for hiding this comment

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

consider changing the error message here, we wouldn't trying parse the query. WDYT ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed the message here, Thank you.

import java.util.List;

/**
* This class helps in evaluating the query field if it is template
Copy link
Member

Choose a reason for hiding this comment

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

it evaluates non template queries also, right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So there are two validation methods one that validates query (validateQueryField) and another one that evaluates a query if it is a template and then performs validation(validateAndVerifyRoleQuery).
validateQueryField cannot evaluate the template query so skips the validation for it as the runtime information is not available.
I have corrected the documentation for validateQueryField which was incorrect. Thank you.

Yogesh Gaikwad added 2 commits September 12, 2019 13:15
- add index privileg index to the error message to denote query of which
index privilege failed to validate.
- add more information to the error message where we know the expected values
@@ -190,7 +190,7 @@ private static Role randomRole(String roleName) {
.name(roleName)
.clusterPrivileges(randomSubsetOf(randomInt(3), Role.ClusterPrivilegeName.ALL_ARRAY))
.indicesPrivileges(
randomArray(3, IndicesPrivileges[]::new, () -> IndicesPrivilegesTests.createNewRandom(randomAlphaOfLength(3))))
randomArray(3, IndicesPrivileges[]::new, () -> IndicesPrivilegesTests.createNewRandom("{\"match_all\": {}}")))
Copy link
Contributor

Choose a reason for hiding this comment

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

If we really want this to be a random role then may this would be better as

Suggested change
randomArray(3, IndicesPrivileges[]::new, () -> IndicesPrivilegesTests.createNewRandom("{\"match_all\": {}}")))
randomArray(3, IndicesPrivileges[]::new, () -> IndicesPrivilegesTests.createNewRandom(
"{\"term\":{ \"" + randomAlphaOfLength(5) + "\" : \"" + randomAlphaOfLength(3) + "\"}}")))

But maybe that's overkill, I didn't really look at where we use this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this was an IT testing put role, we were failing due to validation, earlier the query was invalid.
I don't think we need to create a random query here, the createNewRandom is being used in unit tests mostly. But if you feel that we should have a random query string generated then we can do something here. Thank you.

@legrego
Copy link
Member

legrego commented Sep 12, 2019

I think adding a new API only for validation is something we might not want to do as it is very specific to the presentation layer and we have avoided doing that till now.

Yep, I assumed that was the case, but there's no harm in asking 😄

Few things UI could do more:
validate if the query JSON is a valid JSON. For example, if the query is { malformed }

Yeah, this is a quick win for us, good suggestion.

another suggestion would be to use the query completion which is present on the dev tools? I think this could be useful, though the query completion may allow query names that are not supported for DLS but you will get an error from the backend.

I think this would be ideal, but that would be quite a bit of work for us, and not something I could take on in the near future.

@bizybot bizybot merged commit 777f4b3 into elastic:master Sep 25, 2019
bizybot added a commit to bizybot/elasticsearch that referenced this pull request Sep 25, 2019
In the current implementation, the validation of the role query
occurs at runtime when the query is being executed.

This commit adds validation for the role query when creating a role
but not for the template query as we do not have the runtime
information required for evaluating the template query (eg. authenticated user's
information). This is similar to the scripts that we
store but do not evaluate or parse if they are valid queries or not.

For validation, the query is evaluated (if not a template), parsed to build the
QueryBuilder and verify if the query type is allowed.

Closes elastic#34252
bizybot added a commit that referenced this pull request Sep 26, 2019
In the current implementation, the validation of the role query
occurs at runtime when the query is being executed.

This commit adds validation for the role query when creating a role
but not for the template query as we do not have the runtime
information required for evaluating the template query (eg. authenticated user's
information). This is similar to the scripts that we
store but do not evaluate or parse if they are valid queries or not.

For validation, the query is evaluated (if not a template), parsed to build the
QueryBuilder and verify if the query type is allowed.

Closes #34252
tvernum added a commit that referenced this pull request Jan 13, 2020
When creating a role, we do not check if the exceptions for
the field permissions are a subset of granted fields. If such
a role is assigned to a user then that user's authentication fails
for this reason.

We added a check to validate role query in #46275 and on the same lines,
this commit adds check if the exceptions for the field
permissions is a subset of granted fields when parsing the
index privileges from the role descriptor.

Co-authored-by: Yogesh Gaikwad <bizybot@users.noreply.github.com>
tvernum added a commit to tvernum/elasticsearch that referenced this pull request Jan 13, 2020
When creating a role, we do not check if the exceptions for
the field permissions are a subset of granted fields. If such
a role is assigned to a user then that user's authentication fails
for this reason.

We added a check to validate role query in elastic#46275 and on the same lines,
this commit adds check if the exceptions for the field
permissions is a subset of granted fields when parsing the
index privileges from the role descriptor.

Co-authored-by: Yogesh Gaikwad <bizybot@users.noreply.github.com>

Backport of: elastic#50212
tvernum added a commit that referenced this pull request Jan 14, 2020
When creating a role, we do not check if the exceptions for
the field permissions are a subset of granted fields. If such
a role is assigned to a user then that user's authentication fails
for this reason.

We added a check to validate role query in #46275 and on the same lines,
this commit adds check if the exceptions for the field
permissions is a subset of granted fields when parsing the
index privileges from the role descriptor.

Backport of: #50212

Co-authored-by: Yogesh Gaikwad <bizybot@users.noreply.github.com>
SivagurunathanV pushed a commit to SivagurunathanV/elasticsearch that referenced this pull request Jan 23, 2020
When creating a role, we do not check if the exceptions for
the field permissions are a subset of granted fields. If such
a role is assigned to a user then that user's authentication fails
for this reason.

We added a check to validate role query in elastic#46275 and on the same lines,
this commit adds check if the exceptions for the field
permissions is a subset of granted fields when parsing the
index privileges from the role descriptor.

Co-authored-by: Yogesh Gaikwad <bizybot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create Security Role API allows malformed/invalid query JSON
6 participants