/
ProvisioningUtil.java
756 lines (679 loc) · 38.1 KB
/
ProvisioningUtil.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/*
* Copyright (c) 2010-2019 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.provisioning.util;
import static java.util.Collections.emptyList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.common.ResourceObjectPattern;
import com.evolveum.midpoint.common.StaticExpressionUtil;
import com.evolveum.midpoint.common.refinery.RefinedAssociationDefinition;
import com.evolveum.midpoint.common.refinery.RefinedAttributeDefinition;
import com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition;
import com.evolveum.midpoint.common.refinery.RefinedResourceSchema;
import com.evolveum.midpoint.common.refinery.RefinedResourceSchemaImpl;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.delta.PropertyDelta;
import com.evolveum.midpoint.prism.delta.builder.S_ItemEntry;
import com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy;
import com.evolveum.midpoint.prism.match.MatchingRule;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.util.CloneUtil;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.provisioning.api.ProvisioningOperationOptions;
import com.evolveum.midpoint.provisioning.api.ResourceOperationDescription;
import com.evolveum.midpoint.provisioning.impl.ProvisioningContext;
import com.evolveum.midpoint.provisioning.impl.ProvisioningOperationState;
import com.evolveum.midpoint.provisioning.ucf.api.AttributesToReturn;
import com.evolveum.midpoint.provisioning.ucf.api.ExecuteProvisioningScriptOperation;
import com.evolveum.midpoint.provisioning.ucf.api.ExecuteScriptArgument;
import com.evolveum.midpoint.schema.*;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.processor.ResourceAttribute;
import com.evolveum.midpoint.schema.processor.ResourceAttributeContainer;
import com.evolveum.midpoint.schema.processor.ResourceAttributeContainerDefinition;
import com.evolveum.midpoint.schema.processor.ResourceAttributeDefinition;
import com.evolveum.midpoint.schema.result.AsynchronousOperationReturnValue;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.schema.util.ResourceTypeUtil;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
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.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.ActivationCapabilityType;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.CredentialsCapabilityType;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.ReadCapabilityType;
import com.evolveum.prism.xml.ns._public.types_3.ChangeTypeType;
public class ProvisioningUtil {
private static final QName FAKE_SCRIPT_ARGUMENT_NAME = new QName(SchemaConstants.NS_C, "arg");
public static final Duration DEFAULT_OPERATION_RETRY_PERIOD_DURATION = XmlTypeConverter.createDuration("PT30M");
public static final int DEFAULT_OPERATION_RETRY_MAX_ATTEMPTS = 3;
private static final Duration DEFAULT_PENDING_OPERATION_RETENTION_PERIOD_DURATION = XmlTypeConverter.createDuration("P1D");
public static final Duration DEFAULT_DEAD_SHADOW_RETENTION_PERIOD_DURATION = XmlTypeConverter.createDuration("P7D");
private static final Trace LOGGER = TraceManager.getTrace(ProvisioningUtil.class);
public static PrismObjectDefinition<ShadowType> getResourceObjectShadowDefinition(
PrismContext prismContext) {
return prismContext.getSchemaRegistry().findObjectDefinitionByCompileTimeClass(ShadowType.class);
}
public static ExecuteProvisioningScriptOperation convertToScriptOperation(
ProvisioningScriptType scriptType, String desc, PrismContext prismContext) throws SchemaException {
ExecuteProvisioningScriptOperation scriptOperation = new ExecuteProvisioningScriptOperation();
MutablePrismPropertyDefinition scriptArgumentDefinition = prismContext.definitionFactory().createPropertyDefinition(
FAKE_SCRIPT_ARGUMENT_NAME, DOMUtil.XSD_STRING);
scriptArgumentDefinition.setMinOccurs(0);
scriptArgumentDefinition.setMaxOccurs(-1);
for (ProvisioningScriptArgumentType argument : scriptType.getArgument()) {
ExecuteScriptArgument arg = new ExecuteScriptArgument(argument.getName(),
StaticExpressionUtil.getStaticOutput(argument, scriptArgumentDefinition, desc,
ExpressionReturnMultiplicityType.SINGLE, prismContext));
scriptOperation.getArgument().add(arg);
}
scriptOperation.setLanguage(scriptType.getLanguage());
scriptOperation.setTextCode(scriptType.getCode());
if (scriptType.getHost() != null && scriptType.getHost().equals(ProvisioningScriptHostType.CONNECTOR)) {
scriptOperation.setConnectorHost(true);
scriptOperation.setResourceHost(false);
}
if (scriptType.getHost() == null || scriptType.getHost().equals(ProvisioningScriptHostType.RESOURCE)) {
scriptOperation.setConnectorHost(false);
scriptOperation.setResourceHost(true);
}
scriptOperation.setCriticality(scriptType.getCriticality());
return scriptOperation;
}
public static AttributesToReturn createAttributesToReturn(ProvisioningContext ctx) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
RefinedObjectClassDefinition objectClassDefinition = ctx.getObjectClassDefinition();
ResourceType resource = ctx.getResource();
boolean apply = false;
AttributesToReturn attributesToReturn = new AttributesToReturn();
// Attributes
boolean hasMinimal = false;
for (RefinedAttributeDefinition attributeDefinition : objectClassDefinition.getAttributeDefinitions()) {
if (attributeDefinition.getFetchStrategy() == AttributeFetchStrategyType.MINIMAL) {
hasMinimal = true;
break;
}
}
attributesToReturn.setReturnDefaultAttributes(!hasMinimal);
Collection<ResourceAttributeDefinition> explicit = new ArrayList<>();
for (RefinedAttributeDefinition attributeDefinition : objectClassDefinition.getAttributeDefinitions()) {
AttributeFetchStrategyType fetchStrategy = attributeDefinition.getFetchStrategy();
if (fetchStrategy == AttributeFetchStrategyType.EXPLICIT) {
explicit.add(attributeDefinition);
} else if (hasMinimal && (fetchStrategy != AttributeFetchStrategyType.MINIMAL ||
(SelectorOptions.hasToLoadPath(ctx.getPrismContext().path(ShadowType.F_ATTRIBUTES, attributeDefinition.getItemName()), ctx.getGetOperationOptions(), false) ||
// Following clause is legacy (MID-5838)
SelectorOptions.hasToLoadPath(ctx.getPrismContext().toUniformPath(attributeDefinition.getItemName()), ctx.getGetOperationOptions(), false)))) {
explicit.add(attributeDefinition);
}
}
if (!explicit.isEmpty()) {
attributesToReturn.setAttributesToReturn(explicit);
apply = true;
}
// Password
CredentialsCapabilityType credentialsCapabilityType = ResourceTypeUtil.getEffectiveCapability(
resource, CredentialsCapabilityType.class);
if (credentialsCapabilityType != null) {
if (SelectorOptions.hasToLoadPath(ctx.getPrismContext().toUniformPath(SchemaConstants.PATH_PASSWORD_VALUE), ctx.getGetOperationOptions())) {
attributesToReturn.setReturnPasswordExplicit(true);
apply = true;
} else {
if (!CapabilityUtil.isPasswordReturnedByDefault(credentialsCapabilityType)) {
// There resource is capable of returning password but it does not
// do it by default
AttributeFetchStrategyType passwordFetchStrategy = objectClassDefinition
.getPasswordFetchStrategy();
if (passwordFetchStrategy == AttributeFetchStrategyType.EXPLICIT) {
attributesToReturn.setReturnPasswordExplicit(true);
apply = true;
}
}
}
}
// Activation
ActivationCapabilityType activationCapabilityType = ResourceTypeUtil.getEffectiveCapability(resource,
ActivationCapabilityType.class);
if (activationCapabilityType != null) {
if (CapabilityUtil.isCapabilityEnabled(activationCapabilityType.getStatus())) {
if (!CapabilityUtil.isActivationStatusReturnedByDefault(activationCapabilityType)) {
// There resource is capable of returning enable flag but it does
// not do it by default
if (SelectorOptions.hasToLoadPath(ctx.path(SchemaConstants.PATH_ACTIVATION_ADMINISTRATIVE_STATUS), ctx.getGetOperationOptions())) {
attributesToReturn.setReturnAdministrativeStatusExplicit(true);
apply = true;
} else {
AttributeFetchStrategyType administrativeStatusFetchStrategy = objectClassDefinition
.getActivationFetchStrategy(ActivationType.F_ADMINISTRATIVE_STATUS);
if (administrativeStatusFetchStrategy == AttributeFetchStrategyType.EXPLICIT) {
attributesToReturn.setReturnAdministrativeStatusExplicit(true);
apply = true;
}
}
}
}
if (CapabilityUtil.isCapabilityEnabled(activationCapabilityType.getValidFrom())) {
if (!CapabilityUtil.isActivationValidFromReturnedByDefault(activationCapabilityType)) {
if (SelectorOptions.hasToLoadPath(ctx.path(SchemaConstants.PATH_ACTIVATION_VALID_FROM), ctx.getGetOperationOptions())) {
attributesToReturn.setReturnValidFromExplicit(true);
apply = true;
} else {
AttributeFetchStrategyType administrativeStatusFetchStrategy = objectClassDefinition
.getActivationFetchStrategy(ActivationType.F_VALID_FROM);
if (administrativeStatusFetchStrategy == AttributeFetchStrategyType.EXPLICIT) {
attributesToReturn.setReturnValidFromExplicit(true);
apply = true;
}
}
}
}
if (CapabilityUtil.isCapabilityEnabled(activationCapabilityType.getValidTo())) {
if (!CapabilityUtil.isActivationValidToReturnedByDefault(activationCapabilityType)) {
if (SelectorOptions.hasToLoadPath(ctx.path(SchemaConstants.PATH_ACTIVATION_VALID_TO), ctx.getGetOperationOptions())) {
attributesToReturn.setReturnValidToExplicit(true);
apply = true;
} else {
AttributeFetchStrategyType administrativeStatusFetchStrategy = objectClassDefinition
.getActivationFetchStrategy(ActivationType.F_VALID_TO);
if (administrativeStatusFetchStrategy == AttributeFetchStrategyType.EXPLICIT) {
attributesToReturn.setReturnValidToExplicit(true);
apply = true;
}
}
}
}
if (CapabilityUtil.isCapabilityEnabled(activationCapabilityType.getLockoutStatus())) {
if (!CapabilityUtil.isActivationLockoutStatusReturnedByDefault(activationCapabilityType)) {
// There resource is capable of returning lockout flag but it does
// not do it by default
if (SelectorOptions.hasToLoadPath(ctx.path(SchemaConstants.PATH_ACTIVATION_LOCKOUT_STATUS), ctx.getGetOperationOptions())) {
attributesToReturn.setReturnAdministrativeStatusExplicit(true);
apply = true;
} else {
AttributeFetchStrategyType statusFetchStrategy = objectClassDefinition
.getActivationFetchStrategy(ActivationType.F_LOCKOUT_STATUS);
if (statusFetchStrategy == AttributeFetchStrategyType.EXPLICIT) {
attributesToReturn.setReturnLockoutStatusExplicit(true);
apply = true;
}
}
}
}
}
if (apply) {
return attributesToReturn;
} else {
return null;
}
}
public static <T> PropertyDelta<T> narrowPropertyDelta(PropertyDelta<T> propertyDelta,
PrismObject<ShadowType> currentShadow, QName overridingMatchingRuleQName, MatchingRuleRegistry matchingRuleRegistry) throws SchemaException {
ItemDefinition propertyDef = propertyDelta.getDefinition();
QName matchingRuleQName;
if (overridingMatchingRuleQName != null) {
matchingRuleQName = overridingMatchingRuleQName;
} else if (propertyDef instanceof RefinedAttributeDefinition) {
matchingRuleQName = ((RefinedAttributeDefinition<?>)propertyDef).getMatchingRuleQName();
} else {
matchingRuleQName = null;
}
MatchingRule<T> matchingRule;
if (matchingRuleQName != null && propertyDef != null) {
matchingRule = matchingRuleRegistry.getMatchingRule(matchingRuleQName, propertyDef.getTypeName());
} else {
matchingRule = null;
}
LOGGER.trace("Narrowing attr def={}, matchingRule={} ({})", propertyDef, matchingRule, matchingRuleQName);
Comparator<PrismPropertyValue<T>> comparator = (o1, o2) -> {
if (o1.equals(o2, EquivalenceStrategy.REAL_VALUE, matchingRule)) {
return 0;
} else {
return 1;
}
};
// We can safely narrow delta using real values, because we are not interested in value metadata here.
// Because we are dealing with properties, container IDs are also out of questions, and operational items
// as well.
PropertyDelta<T> filteredDelta = propertyDelta.narrow(currentShadow, comparator, comparator, true); // MID-5280
if (LOGGER.isTraceEnabled() && (filteredDelta == null || !filteredDelta.equals(propertyDelta))) {
LOGGER.trace("Narrowed delta: {}", filteredDelta==null?null:filteredDelta.debugDump());
}
return filteredDelta;
}
public static RefinedResourceSchema getRefinedSchema(ResourceType resourceType) throws SchemaException, ConfigurationException {
RefinedResourceSchema refinedSchema = RefinedResourceSchemaImpl.getRefinedSchema(resourceType);
if (refinedSchema == null) {
throw new ConfigurationException("No schema for "+resourceType);
}
return refinedSchema;
}
public static boolean isProtectedShadow(RefinedObjectClassDefinition objectClassDefinition, PrismObject<ShadowType> shadow,
MatchingRuleRegistry matchingRuleRegistry, RelationRegistry relationRegistry) throws SchemaException {
boolean isProtected;
if (objectClassDefinition == null) {
isProtected = false;
} else {
Collection<ResourceObjectPattern> protectedAccountPatterns = objectClassDefinition.getProtectedObjectPatterns();
if (protectedAccountPatterns == null) {
isProtected = false;
} else {
isProtected = ResourceObjectPattern.matches(shadow, protectedAccountPatterns, matchingRuleRegistry, relationRegistry);
}
}
LOGGER.trace("isProtectedShadow: {}: {} = {}", objectClassDefinition, shadow, isProtected);
return isProtected;
}
public static void setProtectedFlag(ProvisioningContext ctx, PrismObject<ShadowType> resourceObject,
MatchingRuleRegistry matchingRuleRegistry, RelationRegistry relationRegistry) throws SchemaException,
ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
if (isProtectedShadow(ctx.getObjectClassDefinition(), resourceObject, matchingRuleRegistry, relationRegistry)) {
resourceObject.asObjectable().setProtectedObject(true);
}
}
public static RefinedResourceSchema getRefinedSchema(PrismObject<ResourceType> resource) throws SchemaException, ConfigurationException {
RefinedResourceSchema refinedSchema = RefinedResourceSchemaImpl.getRefinedSchema(resource);
if (refinedSchema == null) {
throw new ConfigurationException("No schema for "+resource);
}
return refinedSchema;
}
public static void recordFatalError(Trace logger, OperationResult opResult, String explicitMessage, Throwable ex) {
String message = explicitMessage != null ? explicitMessage : ex.getMessage();
logger.error(message, ex);
opResult.recordFatalError(message, ex);
opResult.cleanupResult(ex);
}
public static void logWarning(Trace logger, OperationResult opResult, String message, Exception ex) {
logger.error(message, ex);
opResult.recordWarning(message, ex);
}
public static boolean shouldStoreAttributeInShadow(RefinedObjectClassDefinition objectClassDefinition, QName attributeName,
CachingStategyType cachingStrategy) throws ConfigurationException {
if (cachingStrategy == null || cachingStrategy == CachingStategyType.NONE) {
if (objectClassDefinition.isPrimaryIdentifier(attributeName) || objectClassDefinition.isSecondaryIdentifier(attributeName)) {
return true;
}
for (RefinedAssociationDefinition associationDef: objectClassDefinition.getAssociationDefinitions()) {
if (associationDef.getResourceObjectAssociationType().getDirection() == ResourceObjectAssociationDirectionType.OBJECT_TO_SUBJECT) {
QName valueAttributeName = associationDef.getResourceObjectAssociationType().getValueAttribute();
if (QNameUtil.match(attributeName, valueAttributeName)) {
return true;
}
}
}
return false;
} else if (cachingStrategy == CachingStategyType.PASSIVE) {
RefinedAttributeDefinition<Object> attrDef = objectClassDefinition.findAttributeDefinition(attributeName);
return attrDef != null;
} else {
throw new ConfigurationException("Unknown caching strategy "+cachingStrategy);
}
}
public static boolean shouldStoreActivationItemInShadow(QName elementName, CachingStategyType cachingStrategy) { // MID-2585
if (cachingStrategy == CachingStategyType.PASSIVE) {
return true;
} else {
return QNameUtil.match(elementName, ActivationType.F_ARCHIVE_TIMESTAMP) ||
QNameUtil.match(elementName, ActivationType.F_DISABLE_TIMESTAMP) ||
QNameUtil.match(elementName, ActivationType.F_ENABLE_TIMESTAMP) ||
QNameUtil.match(elementName, ActivationType.F_DISABLE_REASON);
}
}
public static void cleanupShadowActivation(ShadowType repoShadowType) {
// cleanup activation - we don't want to store these data in repo shadow (MID-2585)
if (repoShadowType.getActivation() != null) {
cleanupShadowActivation(repoShadowType.getActivation());
}
}
// mirrors createShadowActivationCleanupDeltas
public static void cleanupShadowActivation(ActivationType a) {
a.setAdministrativeStatus(null);
a.setEffectiveStatus(null);
a.setValidFrom(null);
a.setValidTo(null);
a.setValidityStatus(null);
a.setLockoutStatus(null);
a.setLockoutExpirationTimestamp(null);
a.setValidityChangeTimestamp(null);
}
// mirrors cleanupShadowActivation
public static List<ItemDelta<?, ?>> createShadowActivationCleanupDeltas(
ShadowType repo, PrismContext prismContext) throws SchemaException {
ActivationType activation = repo.getActivation();
if (activation == null) {
return emptyList();
}
S_ItemEntry i = prismContext.deltaFor(ShadowType.class);
if (activation.getAdministrativeStatus() != null) {
i = i.item(ShadowType.F_ACTIVATION, ActivationType.F_ADMINISTRATIVE_STATUS).replace();
}
if (activation.getEffectiveStatus() != null) {
i = i.item(ShadowType.F_ACTIVATION, ActivationType.F_EFFECTIVE_STATUS).replace();
}
if (activation.getValidFrom() != null) {
i = i.item(ShadowType.F_ACTIVATION, ActivationType.F_VALID_FROM).replace();
}
if (activation.getValidTo() != null) {
i = i.item(ShadowType.F_ACTIVATION, ActivationType.F_VALID_TO).replace();
}
if (activation.getValidityStatus() != null) {
i = i.item(ShadowType.F_ACTIVATION, ActivationType.F_VALIDITY_STATUS).replace();
}
if (activation.getLockoutStatus() != null) {
i = i.item(ShadowType.F_ACTIVATION, ActivationType.F_LOCKOUT_STATUS).replace();
}
if (activation.getLockoutExpirationTimestamp() != null) {
i = i.item(ShadowType.F_ACTIVATION, ActivationType.F_LOCKOUT_EXPIRATION_TIMESTAMP).replace();
}
if (activation.getValidityChangeTimestamp() != null) {
i = i.item(ShadowType.F_ACTIVATION, ActivationType.F_VALIDITY_CHANGE_TIMESTAMP).replace();
}
return i.asItemDeltas();
}
/**
* Identifiers are set.
* Extra non-identifying attributes are removed.
*/
public static Collection<? extends ItemDelta<?, ?>> createShadowAttributesReconciliationDeltas(
PrismObject<ShadowType> sourceShadow,
PrismObject<ShadowType> repoShadowBefore,
PrismContext prismContext) throws SchemaException {
List<ItemDelta<?, ?>> rv = new ArrayList<>();
ResourceAttributeContainer attributes = ShadowUtil.getAttributesContainer(sourceShadow);
ResourceAttributeContainerDefinition attributesDefinition = attributes.getDefinition();
if (attributesDefinition == null) {
throw new IllegalStateException("No definition for " + attributes);
}
List<QName> identifiers = attributesDefinition.getAllIdentifiers().stream().map(ItemDefinition::getItemName).collect(Collectors.toList());
List<QName> outstandingInRepo;
PrismContainer<?> repoAttributes = repoShadowBefore.findContainer(ShadowType.F_ATTRIBUTES);
if (repoAttributes != null) {
outstandingInRepo = repoAttributes.getValue().getItems().stream()
.map(Item::getElementName)
.collect(Collectors.toCollection(ArrayList::new));
} else {
outstandingInRepo = new ArrayList<>();
}
for (ResourceAttribute<?> attribute : attributes.getAttributes()) {
QName attributeName = attribute.getElementName();
boolean isIdentifier = QNameUtil.matchAny(attributeName, identifiers);
List<? extends PrismPropertyValue<?>> valuesToReplace;
if (isIdentifier) {
valuesToReplace = CloneUtil.cloneCollectionMembers(attribute.getValues());
LOGGER.trace("- updating identifier {} value of {}", attributeName, attribute.getValues());
rv.add(prismContext.deltaFor(ShadowType.class)
.item(ItemPath.create(ShadowType.F_ATTRIBUTES, attributeName), attribute.getDefinition()).replace(valuesToReplace)
.asItemDelta());
QNameUtil.remove(outstandingInRepo, attributeName);
}
}
for (QName outstanding : outstandingInRepo) {
boolean isIdentifier = QNameUtil.matchAny(outstanding, identifiers);
if (!isIdentifier) {
ResourceAttributeDefinition<?> outstandingDefinition = attributesDefinition.findAttributeDefinition(ItemName.fromQName(outstanding));
if (outstandingDefinition == null) {
continue; // cannot do anything with this
}
LOGGER.trace("- removing non-identifier {} value", outstanding);
rv.add(prismContext.deltaFor(ShadowType.class)
.item(ItemPath.create(ShadowType.F_ATTRIBUTES, outstanding), outstandingDefinition).replace()
.asItemDelta());
}
}
return rv;
}
public static void cleanupShadowPassword(PasswordType p) {
p.setValue(null);
}
public static void addPasswordMetadata(PasswordType p, XMLGregorianCalendar now, PrismObject<? extends FocusType> owner) {
MetadataType metadata = p.getMetadata();
if (metadata != null) {
return;
}
// Supply some metadata if they are not present. However the
// normal thing is that those metadata are provided by model
metadata = new MetadataType();
metadata.setCreateTimestamp(now);
if (owner != null) {
metadata.creatorRef(owner.getOid(), null);
}
p.setMetadata(metadata);
}
public static void checkShadowActivationConsistency(PrismObject<ShadowType> shadow) {
if (shadow == null) { // just for sure
return;
}
ActivationType activation = shadow.asObjectable().getActivation();
if (activation == null) {
return;
}
if (activation.getAdministrativeStatus() != null ||
activation.getEffectiveStatus() != null ||
activation.getValidFrom() != null ||
activation.getValidTo() != null ||
activation.getValidityStatus() != null ||
activation.getLockoutStatus() != null ||
activation.getLockoutExpirationTimestamp() != null ||
activation.getValidityChangeTimestamp() != null) {
String m = "Unexpected content in shadow.activation for " + ObjectTypeUtil.toShortString(shadow) + ": " + activation;
LOGGER.warn("{}", m);
//throw new IllegalStateException(m); // use only for testing
}
}
public static CachingStategyType getCachingStrategy(ProvisioningContext ctx)
throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
ResourceType resource = ctx.getResource();
CachingPolicyType caching = resource.getCaching();
if (caching == null || caching.getCachingStategy() == null) {
ReadCapabilityType readCapabilityType = ResourceTypeUtil.getEffectiveCapability(resource, ReadCapabilityType.class);
if (readCapabilityType == null) {
return CachingStategyType.NONE;
}
Boolean cachingOnly = readCapabilityType.isCachingOnly();
if (cachingOnly == Boolean.TRUE) {
return CachingStategyType.PASSIVE;
}
return CachingStategyType.NONE;
}
return caching.getCachingStategy();
}
public static boolean shouldDoRepoSearch(GetOperationOptions rootOptions) {
return GetOperationOptions.isNoFetch(rootOptions) || GetOperationOptions.isMaxStaleness(rootOptions);
}
public static boolean isResourceModification(ItemDelta modification) {
QName firstPathName = modification.getPath().firstName();
return isAttributeModification(firstPathName) || isNonAttributeResourceModification(firstPathName);
}
public static boolean isAttributeModification(ItemDelta modification) {
QName firstPathName = modification.getPath().firstName();
return isAttributeModification(firstPathName);
}
public static boolean isAttributeModification(QName firstPathName) {
return QNameUtil.match(firstPathName, ShadowType.F_ATTRIBUTES);
}
public static boolean isNonAttributeResourceModification(ItemDelta modification) {
QName firstPathName = modification.getPath().firstName();
return isNonAttributeResourceModification(firstPathName);
}
public static boolean isNonAttributeResourceModification(QName firstPathName) {
return QNameUtil.match(firstPathName, ShadowType.F_ACTIVATION) || QNameUtil.match(firstPathName, ShadowType.F_CREDENTIALS) ||
QNameUtil.match(firstPathName, ShadowType.F_ASSOCIATION) || QNameUtil.match(firstPathName, ShadowType.F_AUXILIARY_OBJECT_CLASS);
}
public static boolean resourceReadIsCachingOnly(ResourceType resource) {
ReadCapabilityType readCapabilityType = ResourceTypeUtil.getEffectiveCapability(resource, ReadCapabilityType.class);
if (readCapabilityType == null) {
return false; // TODO reconsider this
}
return Boolean.TRUE.equals(readCapabilityType.isCachingOnly());
}
public static Duration getGracePeriod(ProvisioningContext ctx) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
Duration gracePeriod = null;
ResourceConsistencyType consistency = ctx.getResource().getConsistency();
if (consistency != null) {
gracePeriod = consistency.getPendingOperationGracePeriod();
}
return gracePeriod;
}
public static Duration getPendingOperationRetentionPeriod(ProvisioningContext ctx) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
Duration period = null;
ResourceConsistencyType consistency = ctx.getResource().getConsistency();
if (consistency != null) {
period = consistency.getPendingOperationRetentionPeriod();
}
if (period == null) {
period = DEFAULT_PENDING_OPERATION_RETENTION_PERIOD_DURATION;
}
return period;
}
public static boolean isOverPeriod(XMLGregorianCalendar now, Duration period, PendingOperationType pendingOperation) {
if (!isCompleted(pendingOperation.getResultStatus())) {
return false;
}
XMLGregorianCalendar completionTimestamp = pendingOperation.getCompletionTimestamp();
if (completionTimestamp == null) {
return false;
}
return isOverPeriod(now, period, completionTimestamp);
}
public static boolean isOverPeriod(XMLGregorianCalendar now, Duration period, XMLGregorianCalendar lastActivityTimestamp) {
if (period == null) {
return true;
}
XMLGregorianCalendar expirationTimestamp = XmlTypeConverter.addDuration(lastActivityTimestamp, period);
return XmlTypeConverter.compare(now, expirationTimestamp) == DatatypeConstants.GREATER;
}
public static Duration getRetryPeriod(ProvisioningContext ctx) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
Duration period = null;
ResourceConsistencyType consistency = ctx.getResource().getConsistency();
if (consistency != null) {
period = consistency.getOperationRetryPeriod();
}
if (period == null) {
period = DEFAULT_OPERATION_RETRY_PERIOD_DURATION;
}
return period;
}
public static Duration getDeadShadowRetentionPeriod(ProvisioningContext ctx) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
Duration period = null;
ResourceConsistencyType consistency = ctx.getResource().getConsistency();
if (consistency != null) {
period = consistency.getDeadShadowRetentionPeriod();
}
if (period == null) {
period = DEFAULT_DEAD_SHADOW_RETENTION_PERIOD_DURATION;
}
return period;
}
public static int getMaxRetryAttempts(ProvisioningContext ctx) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
ResourceConsistencyType consistency = ctx.getResource().getConsistency();
if (consistency == null) {
return DEFAULT_OPERATION_RETRY_MAX_ATTEMPTS;
}
Integer operationRetryMaxAttempts = consistency.getOperationRetryMaxAttempts();
if (operationRetryMaxAttempts == null) {
return DEFAULT_OPERATION_RETRY_MAX_ATTEMPTS;
}
return operationRetryMaxAttempts;
}
public static boolean isCompleted(OperationResultStatusType statusType) {
return statusType != null && statusType != OperationResultStatusType.IN_PROGRESS && statusType != OperationResultStatusType.UNKNOWN;
}
public static boolean hasPendingAddOperation(PrismObject<ShadowType> shadow) {
for (PendingOperationType pendingOperation: shadow.asObjectable().getPendingOperation()) {
if (isPendingAddOperation(pendingOperation)) {
return true;
}
}
return false;
}
private static boolean isPendingAddOperation(PendingOperationType pendingOperation) {
return ChangeTypeType.ADD.equals(pendingOperation.getDelta().getChangeType()) &&
!PendingOperationExecutionStatusType.COMPLETED.equals(pendingOperation.getExecutionStatus());
}
/**
* Explicitly check the capability of the resource (primary connector), not capabilities of additional connectors
*/
public static boolean isPrimaryCachingOnly(ResourceType resource) {
ReadCapabilityType readCapabilityType = CapabilityUtil.getEffectiveCapability(resource.getCapabilities(), ReadCapabilityType.class);
if (readCapabilityType == null) {
return false;
}
if (!CapabilityUtil.isCapabilityEnabled(readCapabilityType)) {
return false;
}
return Boolean.TRUE.equals(readCapabilityType.isCachingOnly());
}
public static boolean isFuturePointInTime(Collection<SelectorOptions<GetOperationOptions>> options) {
PointInTimeType pit = GetOperationOptions.getPointInTimeType(SelectorOptions.findRootOptions(options));
return PointInTimeType.FUTURE.equals(pit);
}
public static ResourceOperationDescription createResourceFailureDescription(
PrismObject<ShadowType> conflictedShadow, ResourceType resource, ObjectDelta<ShadowType> delta, OperationResult parentResult) {
ResourceOperationDescription failureDesc = new ResourceOperationDescription();
failureDesc.setCurrentShadow(conflictedShadow);
failureDesc.setObjectDelta(delta);
failureDesc.setResource(resource.asPrismObject());
failureDesc.setResult(parentResult);
failureDesc.setSourceChannel(QNameUtil.qNameToUri(SchemaConstants.CHANNEL_DISCOVERY));
return failureDesc;
}
public static boolean isDoDiscovery(ResourceType resource, GetOperationOptions rootOptions) {
return !GetOperationOptions.isDoNotDiscovery(rootOptions) && isDoDiscovery(resource);
}
public static boolean isDoDiscovery(ResourceType resource, ProvisioningOperationOptions options) {
return !ProvisioningOperationOptions.isDoNotDiscovery(options) && isDoDiscovery(resource);
}
public static boolean isDoDiscovery (ResourceType resource) {
if (resource == null) {
return true;
}
if (resource.getConsistency() == null) {
return true;
}
if (resource.getConsistency().isDiscovery() == null) {
return true;
}
return resource.getConsistency().isDiscovery();
}
public static OperationResultStatus postponeModify(ProvisioningContext ctx,
PrismObject<ShadowType> repoShadow,
Collection<? extends ItemDelta> modifications,
ProvisioningOperationState<AsynchronousOperationReturnValue<Collection<PropertyDelta<PrismPropertyValue>>>> opState,
OperationResult failedOperationResult,
OperationResult result) {
LOGGER.trace("Postponing MODIFY operation for {}", repoShadow);
opState.setExecutionStatus(PendingOperationExecutionStatusType.EXECUTING);
AsynchronousOperationReturnValue<Collection<PropertyDelta<PrismPropertyValue>>> asyncResult = new AsynchronousOperationReturnValue<>();
asyncResult.setOperationResult(failedOperationResult);
asyncResult.setOperationType(PendingOperationTypeType.RETRY);
opState.setAsyncResult(asyncResult);
if (opState.getAttemptNumber() == null) {
opState.setAttemptNumber(1);
}
result.recordInProgress();
return OperationResultStatus.IN_PROGRESS;
}
}