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