/
PolicyRestrictedMongoSearchAggregation.java
480 lines (407 loc) · 22.1 KB
/
PolicyRestrictedMongoSearchAggregation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.thingsearch.persistence.read.query;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.lookup;
import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Aggregates.project;
import static com.mongodb.client.model.Aggregates.sort;
import static com.mongodb.client.model.Aggregates.unwind;
import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.or;
import static java.util.Objects.requireNonNull;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.CONCAT;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.COUNT_RESULT_NAME;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_ATTRIBUTES;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_ATTRIBUTES_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_DELETED;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_DELETED_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_FEATURES;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_FEATURES_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_GRANTS;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_GRANTS_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_ID;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_INTERNAL;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_INTERNAL_ACL;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_INTERNAL_FEATURE_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_INTERNAL_GLOBAL_READS;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_INTERNAL_KEY_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_INTERNAL_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_NAMESPACE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_REVISION;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIELD_REVISION_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.FIRST_PROJECTION;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.ID_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.IF_NULL_CONDITION;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.NAMESPACE_VARIABLE;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.POLICIES_BASED_SEARCH_INDEX_COLLECTION_NAME;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.POLICY_INDEX_ID;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.PUSH_PROJECTION;
import static org.eclipse.ditto.services.thingsearch.persistence.PersistenceConstants.SUM_GROUPING;
import static org.eclipse.ditto.services.thingsearch.persistence.read.MongoThingsSearchPersistence.filterNotDeleted;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.bson.BsonArray;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonNull;
import org.bson.BsonString;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.eclipse.ditto.model.query.model.criteria.AnyCriteriaImpl;
import org.eclipse.ditto.model.query.model.criteria.Criteria;
import org.eclipse.ditto.model.query.model.criteria.CriteriaFactory;
import org.eclipse.ditto.model.query.model.criteria.CriteriaFactoryImpl;
import org.eclipse.ditto.model.query.model.criteria.Predicate;
import org.eclipse.ditto.model.query.model.expression.FilterFieldExpression;
import org.eclipse.ditto.model.query.model.expression.SimpleFieldExpressionImpl;
import org.eclipse.ditto.model.query.model.expression.SortFieldExpression;
import org.eclipse.ditto.model.query.model.expression.ThingsFieldExpressionFactory;
import org.eclipse.ditto.model.query.model.expression.ThingsFieldExpressionFactoryImpl;
import org.eclipse.ditto.model.query.model.query.QueryConstants;
import org.eclipse.ditto.model.query.model.query.SortDirection;
import org.eclipse.ditto.model.query.model.query.SortOption;
import org.eclipse.ditto.services.thingsearch.persistence.read.AggregationBuilder;
import org.eclipse.ditto.services.thingsearch.persistence.read.PolicyRestrictedSearchAggregation;
import org.eclipse.ditto.services.thingsearch.persistence.read.criteria.visitors.CreateBsonVisitor;
import org.eclipse.ditto.services.thingsearch.persistence.read.criteria.visitors.CreatePolicyRestrictionBsonVisitor;
import org.eclipse.ditto.services.thingsearch.persistence.read.criteria.visitors.CreateUnwoundBsonVisitor;
import org.eclipse.ditto.services.thingsearch.persistence.read.expression.visitors.GetSortBsonVisitor;
import org.eclipse.ditto.services.utils.persistence.mongo.BsonUtil;
import org.reactivestreams.Publisher;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.BsonField;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.model.UnwindOptions;
import com.mongodb.reactivestreams.client.MongoCollection;
import akka.NotUsed;
import akka.stream.javadsl.Source;
/**
* Represents an aggregation used to search on things restricted by policies.
*/
final class PolicyRestrictedMongoSearchAggregation implements PolicyRestrictedSearchAggregation {
private static final CriteriaFactory CRITERIA_FACTORY = new CriteriaFactoryImpl();
private static final ThingsFieldExpressionFactory FIELD_EXPRESSION_FACTORY =
new ThingsFieldExpressionFactoryImpl();
private static final List<SortOption> DEFAULT_SORT_OPTIONS =
Collections.singletonList(new SortOption(FIELD_EXPRESSION_FACTORY.sortByThingId(), SortDirection.ASC));
private static final BsonInt32 BSON_INT_1 = new BsonInt32(1);
private static final Bson GROUP_STAGE = createGroupStage();
private static final Bson GROUPED_ID_PROJECT_STAGE = createGroupedIdProjectStage();
private static final Bson PROJECTION_STAGE_1 = createFirstProjectionStage();
private static final Bson PROJECTION_STAGE_2 = createSecondProjectionStage();
private static final Bson UNWIND_STAGE_1 = unwind(FIELD_INTERNAL_VARIABLE);
private static final Bson UNWIND_STAGE_2 = createSecondUnwindStage();
private static final Bson LOOKUP_STAGE =
lookup(POLICIES_BASED_SEARCH_INDEX_COLLECTION_NAME, POLICY_INDEX_ID, FIELD_ID, FIELD_GRANTS);
private final List<Bson> aggregationPipeline;
private final int skip;
private final int limit;
private final Criteria filterCriteria;
private PolicyRestrictedMongoSearchAggregation(final Builder builder) {
final boolean isSudo = builder.sudo;
if (builder.authorizationSubjects.isEmpty() != isSudo) {
throw new IllegalStateException("AuthorizationSubjects are required for non-sudo searches!");
}
final Predicate authorizationSubjectsPredicate = CRITERIA_FACTORY.in(builder.authorizationSubjects);
final Criteria globalReadsCriteria = createCriteria(isSudo, CRITERIA_FACTORY,
authorizationSubjectsPredicate, FIELD_EXPRESSION_FACTORY.filterByGlobalRead());
final Criteria aclFieldCriteria = createCriteria(isSudo, CRITERIA_FACTORY,
authorizationSubjectsPredicate, FIELD_EXPRESSION_FACTORY.filterByAcl());
final List<Bson> pipeline = new ArrayList<>();
// match1 filters by ACL, global-READ and search criteria.
final Bson match1 = builder.withDeletedThings
? createInitialMatchStageWithDeleted(builder.filterCriteria, aclFieldCriteria, globalReadsCriteria)
: createInitialMatchStageWithNonDeleted(builder.filterCriteria, aclFieldCriteria, globalReadsCriteria);
pipeline.add(match1);
pipeline.add(UNWIND_STAGE_1);
// match2 filters out irrelevant attributes and features.
final Optional<Bson> match2 = createSecondaryMatchStage(builder.filterCriteria);
match2.ifPresent(pipeline::add);
pipeline.add(PROJECTION_STAGE_1);
pipeline.add(LOOKUP_STAGE);
pipeline.add(UNWIND_STAGE_2);
// match3 filters out fields the user is not supposed to see.
final Bson match3 = createTertiaryMatchStage(builder.filterCriteria, authorizationSubjectsPredicate);
pipeline.add(match3);
// groupBy stage
pipeline.add(GROUP_STAGE);
pipeline.add(GROUPED_ID_PROJECT_STAGE);
// filter out mismatched results after computing view
final Bson match4 = match(CreateBsonVisitor.apply(builder.filterCriteria));
pipeline.add(match4);
// sort results after false positives are filtered out
//
// Due to MongoDB issue SERVER-7568:
// Sort stage MUST be after some UNWIND or PROJECT---otherwise MongoDB 3.2/3.4 carries out incorrect
// optimization and iterates through an entire index before applying the filter match1.
addSortingStage(pipeline,
builder.sortOptions.isEmpty() ? DEFAULT_SORT_OPTIONS : builder.sortOptions, builder.count);
// add skip & limit to reduce aggregation result size limit problem
addSkipAndLimit(pipeline, builder.skip, builder.limit, builder.count);
addProjectionStage2IfCount(pipeline, builder.count);
addCountStage(pipeline, builder.count);
aggregationPipeline = pipeline;
skip = builder.skip;
limit = builder.limit;
filterCriteria = builder.filterCriteria;
}
private static Bson createGroupStage() {
return group(new BsonDocument(FIELD_ID, new BsonString(ID_VARIABLE)),
new BsonField(FIELD_NAMESPACE, createProjectionDocument(FIRST_PROJECTION, NAMESPACE_VARIABLE)),
new BsonField(FIELD_ATTRIBUTES, createProjectionDocument(FIRST_PROJECTION, FIELD_ATTRIBUTES_VARIABLE)),
new BsonField(FIELD_FEATURES, createProjectionDocument(FIRST_PROJECTION, FIELD_FEATURES_VARIABLE)),
new BsonField(FIELD_INTERNAL, createProjectionDocument(PUSH_PROJECTION, FIELD_INTERNAL_VARIABLE)),
new BsonField(FIELD_DELETED, createProjectionDocument(FIRST_PROJECTION, FIELD_DELETED_VARIABLE)),
new BsonField(FIELD_REVISION, createProjectionDocument(FIRST_PROJECTION, FIELD_REVISION_VARIABLE)));
}
private static Bson createProjectionDocument(final String projectionKey, final String projectionValue) {
return new BsonDocument(projectionKey, new BsonString(projectionValue));
}
private static Bson createGroupedIdProjectStage() {
return project(new BsonDocument()
.append(FIELD_ID, new BsonString(ID_VARIABLE + "." + FIELD_ID))
.append(FIELD_NAMESPACE, BsonBoolean.TRUE)
.append(FIELD_ATTRIBUTES, BsonBoolean.TRUE)
.append(FIELD_FEATURES, BsonBoolean.TRUE)
.append(FIELD_INTERNAL, BsonBoolean.TRUE)
.append(FIELD_DELETED, BsonBoolean.TRUE)
.append(FIELD_REVISION, BsonBoolean.TRUE));
}
private static Bson createInitialMatchStageWithDeleted(final Criteria filterCriteria,
final Criteria aclCriteria, final Criteria globalPolicyGrantsCriteria) {
final Bson authorization =
or(CreateBsonVisitor.apply(globalPolicyGrantsCriteria), CreateBsonVisitor.apply(aclCriteria));
return match(and(authorization, CreateBsonVisitor.apply(filterCriteria)));
}
private static void addSkipAndLimit(final Collection<Bson> pipeline,
final int skip,
final int limit,
final boolean isCount) {
if (!isCount) {
pipeline.add(Aggregates.skip(skip));
pipeline.add(Aggregates.limit(limit + 1));
}
}
private static void addProjectionStage2IfCount(final Collection<Bson> pipeline, final boolean isCount) {
if (!isCount) {
pipeline.add(PROJECTION_STAGE_2);
}
}
private static void addCountStage(final Collection<Bson> pipeline, final boolean isCount) {
if (isCount) {
pipeline.add(group(new BsonDocument(FIELD_ID, BsonNull.VALUE), new BsonField(COUNT_RESULT_NAME,
new BsonDocument(SUM_GROUPING, BSON_INT_1))));
}
}
private static Criteria createCriteria(final boolean isSudo,
final CriteriaFactory criteriaFactory,
final Predicate authorizationSubjectsPredicate,
final FilterFieldExpression global) {
return (isSudo
? criteriaFactory.any()
: criteriaFactory.fieldCriteria(global, authorizationSubjectsPredicate));
}
private static void addSortingStage(final Collection<Bson> pipeline, final Collection<SortOption> sortOptions,
final boolean isCount) {
if (!isCount && !sortOptions.isEmpty()) {
pipeline.add(sort(getSortOptionsAsBson(sortOptions)));
}
}
private static Bson getSortOptionsAsBson(final Iterable<SortOption> sortOptions) {
final List<Bson> sortings = new ArrayList<>();
for (final SortOption sortOption : sortOptions) {
final SortDirection sortDirection = sortOption.getSortDirection();
final SortFieldExpression sortExpression = sortOption.getSortExpression();
final List<Bson> currentSorts = GetSortBsonVisitor.apply(sortExpression, sortDirection);
sortings.addAll(currentSorts);
}
return Sorts.orderBy(sortings);
}
private static Bson createFirstProjectionStage() {
final BsonDocument projection = new BsonDocument()
.append(POLICY_INDEX_ID, new BsonDocument(CONCAT,
new BsonArray(Arrays.asList(
new BsonString(ID_VARIABLE),
new BsonString(":"),
new BsonDocument(IF_NULL_CONDITION,
new BsonArray(Arrays.asList(
new BsonString(FIELD_INTERNAL_FEATURE_VARIABLE),
new BsonString(""))
)),
new BsonDocument(IF_NULL_CONDITION,
new BsonArray(Arrays.asList(
new BsonString(FIELD_INTERNAL_KEY_VARIABLE),
new BsonString(""))
))
))
))
.append(FIELD_NAMESPACE, BsonBoolean.TRUE)
.append(FIELD_ATTRIBUTES, BsonBoolean.TRUE)
.append(FIELD_FEATURES, BsonBoolean.TRUE)
.append(FIELD_INTERNAL, BsonBoolean.TRUE)
.append(FIELD_DELETED, BsonBoolean.TRUE)
.append(FIELD_REVISION, BsonBoolean.TRUE);
return project(projection);
}
static Optional<Bson> createSecondaryMatchStage(final Criteria filterCriteria) {
// filters relevant attributes/features.
// an attribute/feature is relevant if it contributes to the truth of filterCriteria.
return CreateUnwoundBsonVisitor.apply(filterCriteria).map(Aggregates::match);
}
private static Bson createTertiaryMatchStage(final Criteria filterCriteria,
final Predicate authorizationSubjectsPredicate) {
final Criteria thingV1Criteria = CRITERIA_FACTORY.fieldCriteria(
new SimpleFieldExpressionImpl(FIELD_GRANTS), CRITERIA_FACTORY.eq(null));
// ACL and global-READ are checked at the beginning.
// check policy entries here, let ACL and global-Read fall through.
final Criteria internalGrCriteria = CRITERIA_FACTORY.existsCriteria(
new SimpleFieldExpressionImpl(FIELD_INTERNAL_GLOBAL_READS));
final Criteria internalAclCriteria = CRITERIA_FACTORY.existsCriteria(
new SimpleFieldExpressionImpl(FIELD_INTERNAL_ACL));
return match(or(
CreatePolicyRestrictionBsonVisitor.apply(filterCriteria, authorizationSubjectsPredicate)
.orElse(new BsonDocument()),
CreateBsonVisitor.apply(thingV1Criteria),
CreateBsonVisitor.apply(internalGrCriteria),
CreateBsonVisitor.apply(internalAclCriteria)));
}
private static Bson createInitialMatchStageWithNonDeleted(final Criteria filterCriteria,
final Criteria aclCriteria, final Criteria globalPolicyGrantsCriteria) {
final Bson authorization =
or(CreateBsonVisitor.apply(globalPolicyGrantsCriteria), CreateBsonVisitor.apply(aclCriteria));
return match(and(authorization, filterNotDeleted(), CreateBsonVisitor.apply(filterCriteria)));
}
private static Bson createSecondProjectionStage() {
return project(new BsonDocument().append(FIELD_ID, BsonBoolean.TRUE));
}
private static Bson createSecondUnwindStage() {
return unwind(FIELD_GRANTS_VARIABLE, new UnwindOptions().preserveNullAndEmptyArrays(true));
}
@Override
public List<Bson> getAggregationPipeline() {
return aggregationPipeline;
}
@Override
public int getSkip() {
return skip;
}
@Override
public int getLimit() {
return limit;
}
@Override
public Criteria getCriteria() {
return filterCriteria;
}
@Override
public Source<Document, NotUsed> execute(final MongoCollection<Document> collection, final Duration maxTime) {
checkNotNull(collection, "collection to be aggregated");
final Publisher<Document> publisher = collection.aggregate(aggregationPipeline)
.maxTime(maxTime.getSeconds(), TimeUnit.SECONDS)
.allowDiskUse(true)
// query is faster with cursor disabled
.useCursor(false);
return Source.<Document>fromPublisher(publisher);
}
/**
* Pretty-prints the aggregation pipeline.
*
* @return String representation of the aggregation json.
*/
public String prettyPrintPipeline() {
return "[" + aggregationPipeline.stream()
.map(BsonUtil::toBsonDocument)
.map(BsonDocument::toJson)
.collect(Collectors.joining(",\n")) + "]";
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"aggregationPipeline=" + aggregationPipeline +
", skip=" + skip +
", limit=" + limit +
"]";
}
/**
* Builder to build an aggregation pipeline for searching things.
*/
public static final class Builder implements AggregationBuilder {
private Criteria filterCriteria = AnyCriteriaImpl.getInstance();
private List<Object> authorizationSubjects = Collections.emptyList();
private List<SortOption> sortOptions = Collections.emptyList();
private int limit = QueryConstants.DEFAULT_LIMIT;
private int skip = 0;
private boolean count = false;
private boolean withDeletedThings = false;
private boolean sudo = false;
@Override
public Builder filterCriteria(final Criteria filterCriteria) {
this.filterCriteria = requireNonNull(filterCriteria);
return this;
}
@Override
public Builder authorizationSubjects(final Collection<String> authorizationSubjects) {
this.authorizationSubjects = new ArrayList<>(requireNonNull(authorizationSubjects));
return this;
}
@Override
public Builder sortOptions(final List<SortOption> sortOptions) {
this.sortOptions = requireNonNull(sortOptions);
return this;
}
@Override
public Builder skip(final long skip) {
this.skip = Validator.checkSkip(skip);
return this;
}
@Override
public Builder limit(final long limit) {
this.limit = Validator.checkLimit(limit, QueryConstants.MAX_LIMIT);
return this;
}
/**
* Sets whether this aggregation should be used for counting.
*
* @param count if {@code true} this aggregation is used for counting, if {@code false} it is used for normal
* queries.
* @return this builder.
*/
public Builder count(final boolean count) {
this.count = count;
return this;
}
@Override
public Builder withDeletedThings(final boolean withDeletedThings) {
this.withDeletedThings = withDeletedThings;
return this;
}
@Override
public Builder sudo(final boolean sudo) {
this.sudo = sudo;
return this;
}
@Override
public PolicyRestrictedSearchAggregation build() {
return new PolicyRestrictedMongoSearchAggregation(this);
}
}
}