|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.core.security.authz.support; |
| 7 | + |
| 8 | +import org.elasticsearch.ElasticsearchParseException; |
| 9 | +import org.elasticsearch.common.Nullable; |
| 10 | +import org.elasticsearch.common.ParsingException; |
| 11 | +import org.elasticsearch.common.Strings; |
| 12 | +import org.elasticsearch.common.bytes.BytesReference; |
| 13 | +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; |
| 14 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 15 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 16 | +import org.elasticsearch.common.xcontent.XContentParseException; |
| 17 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 18 | +import org.elasticsearch.common.xcontent.XContentType; |
| 19 | +import org.elasticsearch.index.query.AbstractQueryBuilder; |
| 20 | +import org.elasticsearch.index.query.BoolQueryBuilder; |
| 21 | +import org.elasticsearch.index.query.BoostingQueryBuilder; |
| 22 | +import org.elasticsearch.index.query.ConstantScoreQueryBuilder; |
| 23 | +import org.elasticsearch.index.query.GeoShapeQueryBuilder; |
| 24 | +import org.elasticsearch.index.query.QueryBuilder; |
| 25 | +import org.elasticsearch.index.query.TermsQueryBuilder; |
| 26 | +import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; |
| 27 | +import org.elasticsearch.script.ScriptService; |
| 28 | +import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; |
| 29 | +import org.elasticsearch.xpack.core.security.user.User; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +/** |
| 36 | + * This class helps in evaluating the query field if it is template, |
| 37 | + * validating the query and checking if the query type is allowed to be used in DLS role query. |
| 38 | + */ |
| 39 | +public final class DLSRoleQueryValidator { |
| 40 | + |
| 41 | + private DLSRoleQueryValidator() { |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Validates the query field in the {@link RoleDescriptor.IndicesPrivileges} only if it is not a template query.<br> |
| 46 | + * It parses the query and builds the {@link QueryBuilder}, also checks if the query type is supported in DLS role query. |
| 47 | + * |
| 48 | + * @param indicesPrivileges {@link RoleDescriptor.IndicesPrivileges} |
| 49 | + * @param xContentRegistry {@link NamedXContentRegistry} for finding named queries |
| 50 | + */ |
| 51 | + public static void validateQueryField(RoleDescriptor.IndicesPrivileges[] indicesPrivileges, |
| 52 | + NamedXContentRegistry xContentRegistry) { |
| 53 | + if (indicesPrivileges != null) { |
| 54 | + for (int i = 0; i < indicesPrivileges.length; i++) { |
| 55 | + BytesReference query = indicesPrivileges[i].getQuery(); |
| 56 | + try { |
| 57 | + if (query != null) { |
| 58 | + if (isTemplateQuery(query, xContentRegistry)) { |
| 59 | + // skip template query, this requires runtime information like 'User' information. |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + evaluateAndVerifyRoleQuery(query.utf8ToString(), xContentRegistry); |
| 64 | + } |
| 65 | + } catch (ParsingException | IllegalArgumentException | IOException e) { |
| 66 | + throw new ElasticsearchParseException("failed to parse field 'query' for indices [" + |
| 67 | + Strings.arrayToCommaDelimitedString(indicesPrivileges[i].getIndices()) + |
| 68 | + "] at index privilege [" + i + "] of role descriptor", e); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static boolean isTemplateQuery(BytesReference query, NamedXContentRegistry xContentRegistry) throws IOException { |
| 75 | + try (XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, |
| 76 | + LoggingDeprecationHandler.INSTANCE, query.utf8ToString())) { |
| 77 | + XContentParser.Token token = parser.nextToken(); |
| 78 | + if (token != XContentParser.Token.START_OBJECT) { |
| 79 | + throw new XContentParseException(parser.getTokenLocation(), "expected [" + XContentParser.Token.START_OBJECT + "] but " + |
| 80 | + "found [" + token + "] instead"); |
| 81 | + } |
| 82 | + token = parser.nextToken(); |
| 83 | + if (token != XContentParser.Token.FIELD_NAME) { |
| 84 | + throw new XContentParseException(parser.getTokenLocation(), "expected [" + XContentParser.Token.FIELD_NAME + "] with " + |
| 85 | + "value a query name or 'template' but found [" + token + "] instead"); |
| 86 | + } |
| 87 | + String fieldName = parser.currentName(); |
| 88 | + if ("template".equals(fieldName)) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Evaluates the query if it is a template and then validates the query by parsing |
| 98 | + * and building the {@link QueryBuilder}. It also checks if the query type is |
| 99 | + * supported in DLS role query. |
| 100 | + * |
| 101 | + * @param query {@link BytesReference} query field from the role |
| 102 | + * @param scriptService {@link ScriptService} used for evaluation of a template query |
| 103 | + * @param xContentRegistry {@link NamedXContentRegistry} for finding named queries |
| 104 | + * @param user {@link User} used when evaluation a template query |
| 105 | + * @return {@link QueryBuilder} if the query is valid and allowed, in case {@link RoleDescriptor.IndicesPrivileges} |
| 106 | + * * does not have a query field then it returns {@code null}. |
| 107 | + */ |
| 108 | + @Nullable |
| 109 | + public static QueryBuilder evaluateAndVerifyRoleQuery(BytesReference query, ScriptService scriptService, |
| 110 | + NamedXContentRegistry xContentRegistry, User user) { |
| 111 | + if (query != null) { |
| 112 | + String templateResult = SecurityQueryTemplateEvaluator.evaluateTemplate(query.utf8ToString(), scriptService, |
| 113 | + user); |
| 114 | + try { |
| 115 | + return evaluateAndVerifyRoleQuery(templateResult, xContentRegistry); |
| 116 | + } catch (ElasticsearchParseException | ParsingException | XContentParseException | IOException e) { |
| 117 | + throw new ElasticsearchParseException("failed to parse field 'query' from the role descriptor", e); |
| 118 | + } |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + @Nullable |
| 124 | + private static QueryBuilder evaluateAndVerifyRoleQuery(String query, NamedXContentRegistry xContentRegistry) throws IOException { |
| 125 | + if (query != null) { |
| 126 | + try (XContentParser parser = XContentFactory.xContent(query).createParser(xContentRegistry, |
| 127 | + LoggingDeprecationHandler.INSTANCE, query)) { |
| 128 | + QueryBuilder queryBuilder = AbstractQueryBuilder.parseInnerQueryBuilder(parser); |
| 129 | + verifyRoleQuery(queryBuilder); |
| 130 | + return queryBuilder; |
| 131 | + } |
| 132 | + } |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Checks whether the role query contains queries we know can't be used as DLS role query. |
| 138 | + * |
| 139 | + * @param queryBuilder {@link QueryBuilder} for given query |
| 140 | + */ |
| 141 | + // pkg protected for testing |
| 142 | + static void verifyRoleQuery(QueryBuilder queryBuilder) { |
| 143 | + if (queryBuilder instanceof TermsQueryBuilder) { |
| 144 | + TermsQueryBuilder termsQueryBuilder = (TermsQueryBuilder) queryBuilder; |
| 145 | + if (termsQueryBuilder.termsLookup() != null) { |
| 146 | + throw new IllegalArgumentException("terms query with terms lookup isn't supported as part of a role query"); |
| 147 | + } |
| 148 | + } else if (queryBuilder instanceof GeoShapeQueryBuilder) { |
| 149 | + GeoShapeQueryBuilder geoShapeQueryBuilder = (GeoShapeQueryBuilder) queryBuilder; |
| 150 | + if (geoShapeQueryBuilder.shape() == null) { |
| 151 | + throw new IllegalArgumentException("geoshape query referring to indexed shapes isn't supported as part of a role query"); |
| 152 | + } |
| 153 | + } else if (queryBuilder.getName().equals("percolate")) { |
| 154 | + // actually only if percolate query is referring to an existing document then this is problematic, |
| 155 | + // a normal percolate query does work. However we can't check that here as this query builder is inside |
| 156 | + // another module. So we don't allow the entire percolate query. I don't think users would ever use |
| 157 | + // a percolate query as role query, so this restriction shouldn't prohibit anyone from using dls. |
| 158 | + throw new IllegalArgumentException("percolate query isn't supported as part of a role query"); |
| 159 | + } else if (queryBuilder.getName().equals("has_child")) { |
| 160 | + throw new IllegalArgumentException("has_child query isn't supported as part of a role query"); |
| 161 | + } else if (queryBuilder.getName().equals("has_parent")) { |
| 162 | + throw new IllegalArgumentException("has_parent query isn't supported as part of a role query"); |
| 163 | + } else if (queryBuilder instanceof BoolQueryBuilder) { |
| 164 | + BoolQueryBuilder boolQueryBuilder = (BoolQueryBuilder) queryBuilder; |
| 165 | + List<QueryBuilder> clauses = new ArrayList<>(); |
| 166 | + clauses.addAll(boolQueryBuilder.filter()); |
| 167 | + clauses.addAll(boolQueryBuilder.must()); |
| 168 | + clauses.addAll(boolQueryBuilder.mustNot()); |
| 169 | + clauses.addAll(boolQueryBuilder.should()); |
| 170 | + for (QueryBuilder clause : clauses) { |
| 171 | + verifyRoleQuery(clause); |
| 172 | + } |
| 173 | + } else if (queryBuilder instanceof ConstantScoreQueryBuilder) { |
| 174 | + verifyRoleQuery(((ConstantScoreQueryBuilder) queryBuilder).innerQuery()); |
| 175 | + } else if (queryBuilder instanceof FunctionScoreQueryBuilder) { |
| 176 | + verifyRoleQuery(((FunctionScoreQueryBuilder) queryBuilder).query()); |
| 177 | + } else if (queryBuilder instanceof BoostingQueryBuilder) { |
| 178 | + verifyRoleQuery(((BoostingQueryBuilder) queryBuilder).negativeQuery()); |
| 179 | + verifyRoleQuery(((BoostingQueryBuilder) queryBuilder).positiveQuery()); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments