-
Notifications
You must be signed in to change notification settings - Fork 188
/
CorrelationConfirmationEvaluator.java
402 lines (348 loc) · 18.8 KB
/
CorrelationConfirmationEvaluator.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
/*
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.model.impl.sync;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.evolveum.midpoint.model.impl.expr.ModelExpressionThreadLocalHolder;
import com.evolveum.midpoint.prism.*;
import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.model.common.expression.Expression;
import com.evolveum.midpoint.model.common.expression.ExpressionEvaluationContext;
import com.evolveum.midpoint.model.common.expression.ExpressionFactory;
import com.evolveum.midpoint.model.common.expression.ExpressionUtil;
import com.evolveum.midpoint.model.common.expression.ExpressionVariables;
import com.evolveum.midpoint.model.impl.util.Utils;
import com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.QueryJaxbConvertor;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.constants.ExpressionConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.SchemaDebugUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ConditionalSearchFilterType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectSynchronizationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;
import com.evolveum.prism.xml.ns._public.query_3.PagingType;
import java.util.HashSet;
import java.util.Set;
@Component
public class CorrelationConfirmationEvaluator {
private static transient Trace LOGGER = TraceManager.getTrace(CorrelationConfirmationEvaluator.class);
@Autowired(required = true)
@Qualifier("cacheRepositoryService")
private RepositoryService repositoryService;
@Autowired(required = true)
private PrismContext prismContext;
@Autowired(required = true)
private ExpressionFactory expressionFactory;
@Autowired(required = true)
private MatchingRuleRegistry matchingRuleRegistry;
public <F extends FocusType> List<PrismObject<F>> findFocusesByCorrelationRule(Class<F> focusType, ShadowType currentShadow,
List<ConditionalSearchFilterType> conditionalFilters, ResourceType resourceType, SystemConfigurationType configurationType, Task task, OperationResult result)
throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
if (conditionalFilters == null || conditionalFilters.isEmpty()) {
LOGGER.warn("Correlation rule for resource '{}' doesn't contain query, "
+ "returning empty list of users.", resourceType);
return null;
}
List<PrismObject<F>> users = null;
if (conditionalFilters.size() == 1){
if (satisfyCondition(currentShadow, conditionalFilters.get(0), resourceType, configurationType, "Condition expression", task, result)){
LOGGER.trace("Condition {} in correlation expression evaluated to true", conditionalFilters.get(0).getCondition());
users = findUsersByCorrelationRule(focusType, currentShadow, conditionalFilters.get(0), resourceType, configurationType, task, result);
}
} else {
for (ConditionalSearchFilterType conditionalFilter : conditionalFilters) {
//TODO: better description
if (satisfyCondition(currentShadow, conditionalFilter, resourceType, configurationType, "Condition expression", task, result)) {
LOGGER.trace("Condition {} in correlation expression evaluated to true", conditionalFilter.getCondition());
List<PrismObject<F>> foundUsers = findUsersByCorrelationRule(focusType,
currentShadow, conditionalFilter, resourceType, configurationType, task, result);
if (foundUsers == null && users == null) {
continue;
}
if (foundUsers != null && foundUsers.isEmpty() && users == null) {
users = new ArrayList<PrismObject<F>>();
}
if (users == null && foundUsers != null) {
users = foundUsers;
}
if (users != null && !users.isEmpty() && foundUsers != null && !foundUsers.isEmpty()) {
for (PrismObject<F> foundUser : foundUsers) {
if (!contains(users, foundUser)) {
users.add(foundUser);
}
}
}
}
}
}
if (users != null) {
LOGGER.debug(
"SYNCHRONIZATION: CORRELATION: expression for {} returned {} users: {}",
new Object[] { currentShadow, users.size(),
PrettyPrinter.prettyPrint(users, 3) });
if (users.size() > 1) {
// remove duplicates
Set<PrismObject<F>> usersWithoutDups = new HashSet<>();
usersWithoutDups.addAll(users);
users.clear();
users.addAll(usersWithoutDups);
LOGGER.debug("SYNCHRONIZATION: CORRELATION: found {} users without duplicates", users.size());
}
}
return users;
}
private boolean satisfyCondition(ShadowType currentShadow, ConditionalSearchFilterType conditionalFilter,
ResourceType resourceType, SystemConfigurationType configurationType, String shortDesc, Task task,
OperationResult parentResult) throws SchemaException,
ObjectNotFoundException, ExpressionEvaluationException {
if (conditionalFilter.getCondition() == null){
return true;
}
ExpressionType condition = conditionalFilter.getCondition();
ExpressionVariables variables = Utils.getDefaultExpressionVariables(null,currentShadow, resourceType, configurationType);
ItemDefinition outputDefinition = new PrismPropertyDefinitionImpl(
ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_BOOLEAN,
prismContext);
PrismPropertyValue<Boolean> satisfy = (PrismPropertyValue) ExpressionUtil.evaluateExpression(variables,
outputDefinition, condition, expressionFactory, shortDesc, task, parentResult);
if (satisfy.getValue() == null) {
return false;
}
return satisfy.getValue();
}
private <F extends FocusType> boolean contains(List<PrismObject<F>> users, PrismObject<F> foundUser){
for (PrismObject<F> user : users){
if (user.getOid().equals(foundUser.getOid())){
return true;
}
}
return false;
}
private <F extends FocusType> List<PrismObject<F>> findUsersByCorrelationRule(Class<F> focusType,
ShadowType currentShadow, ConditionalSearchFilterType conditionalFilter, ResourceType resourceType, SystemConfigurationType configurationType,
Task task, OperationResult result)
throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException{
if (!conditionalFilter.containsFilterClause()) {
LOGGER.warn("Correlation rule for resource '{}' doesn't contain filter clause, "
+ "returning empty list of users.", resourceType);
return null;
}
ObjectQuery q;
try {
q = QueryJaxbConvertor.createObjectQuery(focusType, conditionalFilter, prismContext);
q = updateFilterWithAccountValues(currentShadow, resourceType, configurationType, q, "Correlation expression", task, result);
if (q == null) {
// Null is OK here, it means that the value in the filter
// evaluated
// to null and the processing should be skipped
return null;
}
} catch (SchemaException ex) {
LoggingUtils.logException(LOGGER, "Couldn't convert query (simplified)\n{}.", ex,
SchemaDebugUtil.prettyPrint(conditionalFilter));
throw new SchemaException("Couldn't convert query.", ex);
} catch (ObjectNotFoundException ex) {
LoggingUtils.logException(LOGGER, "Couldn't convert query (simplified)\n{}.", ex,
SchemaDebugUtil.prettyPrint(conditionalFilter));
throw new ObjectNotFoundException("Couldn't convert query.", ex);
} catch (ExpressionEvaluationException ex) {
LoggingUtils.logException(LOGGER, "Couldn't convert query (simplified)\n{}.", ex,
SchemaDebugUtil.prettyPrint(conditionalFilter));
throw new ExpressionEvaluationException("Couldn't convert query.", ex);
}
List<PrismObject<F>> users;
try {
LOGGER.trace("SYNCHRONIZATION: CORRELATION: expression for results in filter\n{}", q.debugDumpLazily());
users = repositoryService.searchObjects(focusType, q, null, result);
} catch (RuntimeException ex) {
LoggingUtils.logException(LOGGER,
"Couldn't search users in repository, based on filter (simplified)\n{}.", ex, q.debugDump());
throw new SystemException(
"Couldn't search users in repository, based on filter (See logs).", ex);
}
return users;
}
private <F extends FocusType> boolean matchUserCorrelationRule(Class<F> focusType, PrismObject<ShadowType> currentShadow,
PrismObject<F> userType, ResourceType resourceType, SystemConfigurationType configurationType, ConditionalSearchFilterType conditionalFilter, Task task, OperationResult result) throws SchemaException{
if (conditionalFilter == null) {
LOGGER.warn("Correlation rule for resource '{}' doesn't contain query, "
+ "returning empty list of users.", resourceType);
return false;
}
if (!conditionalFilter.containsFilterClause()) {
LOGGER.warn("Correlation rule for resource '{}' doesn't contain a filter, "
+ "returning empty list of users.", resourceType);
return false;
}
ObjectQuery q;
try {
q = QueryJaxbConvertor.createObjectQuery(focusType, conditionalFilter, prismContext);
q = updateFilterWithAccountValues(currentShadow.asObjectable(), resourceType, configurationType, q, "Correlation expression", task, result);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Start matching user {} with correlation expression {}", userType, q != null ? q.debugDump() : "(null)");
}
if (q == null) {
// Null is OK here, it means that the value in the filter
// evaluated
// to null and the processing should be skipped
return false;
}
} catch (Exception ex) {
LoggingUtils.logException(LOGGER, "Couldn't convert query (simplified)\n{}.", ex,
SchemaDebugUtil.prettyPrint(conditionalFilter));
throw new SystemException("Couldn't convert query.", ex);
}
// List<PrismObject<UserType>> users = null;
// try {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("SYNCHRONIZATION: CORRELATION: expression for {} results in filter\n{}",
new Object[] { currentShadow, q});
}
// PagingType paging = new PagingType();
return ObjectQuery.match(userType, q.getFilter(), matchingRuleRegistry);
// if (users == null) {
// users = new ArrayList<PrismObject<UserType>>();
// }
// } catch (Exception ex) {
// LoggingUtils.logException(LOGGER,
// "Couldn't search users in repository, based on filter (simplified)\n{}.", ex, q.dump());
// throw new SynchronizationException(
// "Couldn't search users in repository, based on filter (See logs).", ex);
// }
}
public <F extends FocusType> boolean matchUserCorrelationRule(Class<F> focusType, PrismObject<ShadowType> currentShadow,
PrismObject<F> userType, ObjectSynchronizationType synchronization, ResourceType resourceType, SystemConfigurationType configurationType, Task task, OperationResult result){
if (synchronization == null){
LOGGER.warn(
"Resource does not support synchronization. Skipping evaluation correlation/confirmation for {} and {}",
userType, currentShadow);
return false;
}
List<ConditionalSearchFilterType> conditionalFilters = synchronization.getCorrelation();
try {
for (ConditionalSearchFilterType conditionalFilter : conditionalFilters){
if (true && matchUserCorrelationRule(focusType, currentShadow, userType, resourceType, configurationType, conditionalFilter, task, result)){
LOGGER.debug("SYNCHRONIZATION: CORRELATION: expression for {} match user: {}", new Object[] {
currentShadow, userType });
return true;
}
}
} catch (SchemaException ex){
throw new SystemException("Failed to match user using correlation rule. " + ex.getMessage(), ex);
}
LOGGER.debug("SYNCHRONIZATION: CORRELATION: expression for {} does not match user: {}", new Object[] {
currentShadow, userType });
return false;
}
public <F extends FocusType> List<PrismObject<F>> findUserByConfirmationRule(Class<F> focusType, List<PrismObject<F>> users,
ShadowType currentShadow, ResourceType resource, SystemConfigurationType configuration, ExpressionType expression, Task task, OperationResult result)
throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException
{
List<PrismObject<F>> list = new ArrayList<PrismObject<F>>();
for (PrismObject<F> user : users) {
try {
F userType = user.asObjectable();
boolean confirmedUser = evaluateConfirmationExpression(focusType, userType,
currentShadow, resource, configuration, expression, task, result);
if (user != null && confirmedUser) {
list.add(user);
}
} catch (RuntimeException ex) {
LoggingUtils.logException(LOGGER, "Couldn't confirm user {}", ex, user.getElementName());
throw new SystemException("Couldn't confirm user " + user.getElementName(), ex);
} catch (ExpressionEvaluationException ex) {
LoggingUtils.logException(LOGGER, "Couldn't confirm user {}", ex, user.getElementName());
throw new ExpressionEvaluationException("Couldn't confirm user " + user.getElementName(), ex);
} catch (ObjectNotFoundException ex) {
LoggingUtils.logException(LOGGER, "Couldn't confirm user {}", ex, user.getElementName());
throw new ObjectNotFoundException("Couldn't confirm user " + user.getElementName(), ex);
} catch (SchemaException ex) {
LoggingUtils.logException(LOGGER, "Couldn't confirm user {}", ex, user.getElementName());
throw new SchemaException("Couldn't confirm user " + user.getElementName(), ex);
}
}
LOGGER.debug("SYNCHRONIZATION: CONFIRMATION: expression for {} matched {} users.", new Object[] {
currentShadow, list.size() });
return list;
}
private ObjectQuery updateFilterWithAccountValues(ShadowType currentShadow, ResourceType resource, SystemConfigurationType configuration,
ObjectQuery origQuery, String shortDesc, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
if (origQuery.getFilter() == null) {
LOGGER.trace("No filter provided, skipping updating filter");
return origQuery;
}
return evaluateQueryExpressions(origQuery, currentShadow, resource, configuration, shortDesc, task, result);
}
private ObjectQuery evaluateQueryExpressions(ObjectQuery query, ShadowType currentShadow, ResourceType resource, SystemConfigurationType configuration,
String shortDesc, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
ExpressionVariables variables = Utils.getDefaultExpressionVariables(null, currentShadow, resource, configuration);
return ExpressionUtil.evaluateQueryExpressions(query, variables, expressionFactory, prismContext, shortDesc, task, result);
}
public <F extends FocusType> boolean evaluateConfirmationExpression(Class<F> focusType, F user, ShadowType shadow, ResourceType resource,
SystemConfigurationType configuration, ExpressionType expressionType, Task task, OperationResult result)
throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
Validate.notNull(user, "User must not be null.");
Validate.notNull(shadow, "Resource object shadow must not be null.");
Validate.notNull(expressionType, "Expression must not be null.");
Validate.notNull(result, "Operation result must not be null.");
ExpressionVariables variables = Utils.getDefaultExpressionVariables(user, shadow, resource, configuration);
String shortDesc = "confirmation expression for "+resource.asPrismObject();
PrismPropertyDefinition<Boolean> outputDefinition = new PrismPropertyDefinitionImpl<>(ExpressionConstants.OUTPUT_ELEMENT_NAME,
DOMUtil.XSD_BOOLEAN, prismContext);
Expression<PrismPropertyValue<Boolean>,PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType,
outputDefinition, shortDesc, task, result);
ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, variables, shortDesc, task, result);
PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = ModelExpressionThreadLocalHolder
.evaluateExpressionInContext(expression, params, task, result);
Collection<PrismPropertyValue<Boolean>> nonNegativeValues = outputTriple.getNonNegativeValues();
if (nonNegativeValues == null || nonNegativeValues.isEmpty()) {
throw new ExpressionEvaluationException("Expression returned no value ("+nonNegativeValues.size()+") in "+shortDesc);
}
if (nonNegativeValues.size() > 1) {
throw new ExpressionEvaluationException("Expression returned more than one value ("+nonNegativeValues.size()+") in "+shortDesc);
}
PrismPropertyValue<Boolean> resultpval = nonNegativeValues.iterator().next();
if (resultpval == null) {
throw new ExpressionEvaluationException("Expression returned no value ("+nonNegativeValues.size()+") in "+shortDesc);
}
Boolean resultVal = resultpval.getValue();
if (resultVal == null) {
throw new ExpressionEvaluationException("Expression returned no value ("+nonNegativeValues.size()+") in "+shortDesc);
}
return resultVal;
}
}