/
DefaultPropagationManager.java
798 lines (693 loc) · 35.8 KB
/
DefaultPropagationManager.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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.syncope.core.provisioning.java.propagation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.syncope.common.lib.Attr;
import org.apache.syncope.common.lib.request.AbstractPatchItem;
import org.apache.syncope.common.lib.request.AnyUR;
import org.apache.syncope.common.lib.request.PasswordPatch;
import org.apache.syncope.common.lib.request.UserUR;
import org.apache.syncope.common.lib.to.Item;
import org.apache.syncope.common.lib.to.OrgUnit;
import org.apache.syncope.common.lib.to.Provision;
import org.apache.syncope.common.lib.types.AnyTypeKind;
import org.apache.syncope.common.lib.types.ResourceOperation;
import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
import org.apache.syncope.core.persistence.api.dao.VirSchemaDAO;
import org.apache.syncope.core.persistence.api.entity.Any;
import org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory;
import org.apache.syncope.core.persistence.api.entity.EntityFactory;
import org.apache.syncope.core.persistence.api.entity.ExternalResource;
import org.apache.syncope.core.persistence.api.entity.Realm;
import org.apache.syncope.core.persistence.api.entity.VirSchema;
import org.apache.syncope.core.persistence.api.entity.task.PropagationData;
import org.apache.syncope.core.persistence.api.entity.user.LinkedAccount;
import org.apache.syncope.core.persistence.api.entity.user.User;
import org.apache.syncope.core.provisioning.api.DerAttrHandler;
import org.apache.syncope.core.provisioning.api.MappingManager;
import org.apache.syncope.core.provisioning.api.PropagationByResource;
import org.apache.syncope.core.provisioning.api.UserWorkflowResult;
import org.apache.syncope.core.provisioning.api.jexl.JexlUtils;
import org.apache.syncope.core.provisioning.api.propagation.PropagationManager;
import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskInfo;
import org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils;
import org.apache.syncope.core.provisioning.java.utils.MappingUtils;
import org.identityconnectors.framework.common.objects.Attribute;
import org.identityconnectors.framework.common.objects.AttributeBuilder;
import org.identityconnectors.framework.common.objects.AttributeDelta;
import org.identityconnectors.framework.common.objects.AttributeDeltaBuilder;
import org.identityconnectors.framework.common.objects.AttributeUtil;
import org.identityconnectors.framework.common.objects.Name;
import org.identityconnectors.framework.common.objects.ObjectClass;
import org.identityconnectors.framework.common.objects.OperationalAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
/**
* Manage the data propagation to external resources.
*/
@Transactional(rollbackFor = { Throwable.class })
public class DefaultPropagationManager implements PropagationManager {
protected static final Logger LOG = LoggerFactory.getLogger(PropagationManager.class);
protected final VirSchemaDAO virSchemaDAO;
protected final ExternalResourceDAO resourceDAO;
protected final EntityFactory entityFactory;
protected final ConnObjectUtils connObjectUtils;
protected final MappingManager mappingManager;
protected final DerAttrHandler derAttrHandler;
protected final AnyUtilsFactory anyUtilsFactory;
public DefaultPropagationManager(
final VirSchemaDAO virSchemaDAO,
final ExternalResourceDAO resourceDAO,
final EntityFactory entityFactory,
final ConnObjectUtils connObjectUtils,
final MappingManager mappingManager,
final DerAttrHandler derAttrHandler,
final AnyUtilsFactory anyUtilsFactory) {
this.virSchemaDAO = virSchemaDAO;
this.resourceDAO = resourceDAO;
this.entityFactory = entityFactory;
this.connObjectUtils = connObjectUtils;
this.mappingManager = mappingManager;
this.derAttrHandler = derAttrHandler;
this.anyUtilsFactory = anyUtilsFactory;
}
@Override
public List<PropagationTaskInfo> getCreateTasks(
final AnyTypeKind kind,
final String key,
final Boolean enable,
final PropagationByResource<String> propByRes,
final Collection<Attr> vAttrs,
final Collection<String> excludedResources) {
return getCreateTasks(
anyUtilsFactory.getInstance(kind).dao().authFind(key),
null,
enable,
propByRes,
null,
vAttrs,
excludedResources);
}
@Override
public List<PropagationTaskInfo> getUserCreateTasks(
final String key,
final String password,
final Boolean enable,
final PropagationByResource<String> propByRes,
final PropagationByResource<Pair<String, String>> propByLinkedAccount,
final Collection<Attr> vAttrs,
final Collection<String> excludedResources) {
return getCreateTasks(
anyUtilsFactory.getInstance(AnyTypeKind.USER).dao().authFind(key),
password,
enable,
propByRes,
propByLinkedAccount,
vAttrs,
excludedResources);
}
protected List<PropagationTaskInfo> getCreateTasks(
final Any<?> any,
final String password,
final Boolean enable,
final PropagationByResource<String> propByRes,
final PropagationByResource<Pair<String, String>> propByLinkedAccount,
final Collection<Attr> vAttrs,
final Collection<String> excludedResources) {
if ((propByRes == null || propByRes.isEmpty())
&& (propByLinkedAccount == null || propByLinkedAccount.isEmpty())) {
return List.of();
}
if (excludedResources != null) {
if (propByRes != null) {
propByRes.get(ResourceOperation.CREATE).removeAll(excludedResources);
}
if (propByLinkedAccount != null) {
propByLinkedAccount.get(ResourceOperation.CREATE).
removeIf(account -> excludedResources.contains(account.getLeft()));
}
}
return createTasks(any, password, true, enable, propByRes, propByLinkedAccount, vAttrs);
}
@Override
public List<PropagationTaskInfo> getUpdateTasks(
final AnyTypeKind kind,
final String key,
final boolean changePwd,
final Boolean enable,
final PropagationByResource<String> propByRes,
final PropagationByResource<Pair<String, String>> propByLinkedAccount,
final Collection<Attr> vAttrs,
final Collection<String> excludedResources) {
return getUpdateTasks(
anyUtilsFactory.getInstance(kind).dao().authFind(key),
null,
changePwd,
enable,
propByRes,
propByLinkedAccount,
vAttrs,
excludedResources);
}
@Override
public List<PropagationTaskInfo> getUserUpdateTasks(
final UserWorkflowResult<Pair<UserUR, Boolean>> wfResult,
final boolean changePwd,
final Collection<String> excludedResources) {
return getUpdateTasks(
anyUtilsFactory.getInstance(AnyTypeKind.USER).dao().authFind(wfResult.getResult().getLeft().getKey()),
Optional.ofNullable(wfResult.getResult().getLeft().getPassword()).
map(PasswordPatch::getValue).orElse(null),
changePwd,
wfResult.getResult().getRight(),
wfResult.getPropByRes(),
wfResult.getPropByLinkedAccount(),
wfResult.getResult().getLeft().getVirAttrs(),
excludedResources);
}
@Override
public List<PropagationTaskInfo> getUserUpdateTasks(final UserWorkflowResult<Pair<UserUR, Boolean>> wfResult) {
UserUR userUR = wfResult.getResult().getLeft();
// Propagate password update only to requested resources
List<PropagationTaskInfo> tasks;
if (userUR.getPassword() == null) {
// a. no specific password propagation request: generate propagation tasks for any resource associated
tasks = getUserUpdateTasks(wfResult, false, null);
} else {
tasks = new ArrayList<>();
// b. generate the propagation task list in two phases: first the ones containing password,
// then the rest (with no password)
UserWorkflowResult<Pair<UserUR, Boolean>> pwdWFResult = new UserWorkflowResult<>(
wfResult.getResult(),
new PropagationByResource<>(),
wfResult.getPropByLinkedAccount(),
wfResult.getPerformedTasks());
Set<String> pwdResourceNames = new HashSet<>(userUR.getPassword().getResources());
Collection<String> allResourceNames = anyUtilsFactory.getInstance(AnyTypeKind.USER).
dao().findAllResourceKeys(userUR.getKey());
pwdResourceNames.retainAll(allResourceNames);
if (wfResult.getPropByRes() == null || wfResult.getPropByRes().isEmpty()) {
pwdWFResult.getPropByRes().addAll(ResourceOperation.UPDATE, pwdResourceNames);
} else {
Map<String, ResourceOperation> wfPropByResMap = wfResult.getPropByRes().asMap();
pwdResourceNames.forEach(r -> pwdWFResult.getPropByRes().
add(wfPropByResMap.getOrDefault(r, ResourceOperation.UPDATE), r));
}
if (!pwdWFResult.getPropByRes().isEmpty()) {
Set<String> toBeExcluded = new HashSet<>(allResourceNames);
toBeExcluded.addAll(userUR.getResources().stream().
map(AbstractPatchItem::getValue).collect(Collectors.toList()));
toBeExcluded.removeAll(pwdResourceNames);
tasks.addAll(getUserUpdateTasks(pwdWFResult, true, toBeExcluded));
}
UserWorkflowResult<Pair<UserUR, Boolean>> noPwdWFResult = new UserWorkflowResult<>(
wfResult.getResult(),
new PropagationByResource<>(),
new PropagationByResource<>(),
wfResult.getPerformedTasks());
noPwdWFResult.getPropByRes().merge(wfResult.getPropByRes());
noPwdWFResult.getPropByRes().removeAll(pwdResourceNames);
noPwdWFResult.getPropByRes().purge();
if (!noPwdWFResult.getPropByRes().isEmpty()) {
tasks.addAll(getUserUpdateTasks(noPwdWFResult, false, pwdResourceNames));
}
tasks = tasks.stream().distinct().collect(Collectors.toList());
}
return tasks;
}
protected List<PropagationTaskInfo> getUpdateTasks(
final Any<?> any,
final String password,
final boolean changePwd,
final Boolean enable,
final PropagationByResource<String> propByRes,
final PropagationByResource<Pair<String, String>> propByLinkedAccount,
final Collection<Attr> vAttrs,
final Collection<String> excludedResources) {
if (excludedResources != null) {
if (propByRes != null) {
propByRes.removeAll(excludedResources);
}
if (propByLinkedAccount != null) {
propByLinkedAccount.get(ResourceOperation.CREATE).
removeIf(account -> excludedResources.contains(account.getLeft()));
propByLinkedAccount.get(ResourceOperation.UPDATE).
removeIf(account -> excludedResources.contains(account.getLeft()));
propByLinkedAccount.get(ResourceOperation.DELETE).
removeIf(account -> excludedResources.contains(account.getLeft()));
}
}
return createTasks(
any,
password,
changePwd,
enable,
Optional.ofNullable(propByRes).orElseGet(PropagationByResource::new),
propByLinkedAccount,
vAttrs);
}
@Override
public List<PropagationTaskInfo> getDeleteTasks(
final AnyTypeKind kind,
final String key,
final PropagationByResource<String> propByRes,
final PropagationByResource<Pair<String, String>> propByLinkedAccount,
final Collection<String> excludedResources) {
return getDeleteTasks(
anyUtilsFactory.getInstance(kind).dao().authFind(key),
propByRes, propByLinkedAccount, excludedResources);
}
protected List<PropagationTaskInfo> getDeleteTasks(
final Any<?> any,
final PropagationByResource<String> propByRes,
final PropagationByResource<Pair<String, String>> propByLinkedAccount,
final Collection<String> excludedResources) {
PropagationByResource<String> localPropByRes = new PropagationByResource<>();
if (propByRes == null || propByRes.isEmpty()) {
localPropByRes.addAll(
ResourceOperation.DELETE,
anyUtilsFactory.getInstance(any).dao().findAllResourceKeys(any.getKey()));
} else {
localPropByRes.merge(propByRes);
}
if (excludedResources != null) {
localPropByRes.removeAll(excludedResources);
if (propByLinkedAccount != null) {
propByLinkedAccount.get(ResourceOperation.CREATE).
removeIf(account -> excludedResources.contains(account.getLeft()));
propByLinkedAccount.get(ResourceOperation.UPDATE).
removeIf(account -> excludedResources.contains(account.getLeft()));
propByLinkedAccount.get(ResourceOperation.DELETE).
removeIf(account -> excludedResources.contains(account.getLeft()));
}
}
return createTasks(any, null, false, false, localPropByRes, propByLinkedAccount, null);
}
@Override
public PropagationTaskInfo newTask(
final DerAttrHandler derAttrHandler,
final Any<?> any,
final ExternalResource resource,
final ResourceOperation operation,
final Provision provision,
final Stream<Item> mappingItems,
final Pair<String, Set<Attribute>> preparedAttrs) {
// Check if any of mandatory attributes (in the mapping) is missing or not received any value:
// if so, add special attributes that will be evaluated by PropagationTaskExecutor
List<String> mandatoryMissing = new ArrayList<>();
List<String> mandatoryNullOrEmpty = new ArrayList<>();
mappingItems.filter(item -> !item.isConnObjectKey()
&& JexlUtils.evaluateMandatoryCondition(item.getMandatoryCondition(), any, derAttrHandler)).
forEach(item -> {
Attribute attr = AttributeUtil.find(item.getExtAttrName(), preparedAttrs.getRight());
if (attr == null) {
mandatoryMissing.add(item.getExtAttrName());
} else if (CollectionUtils.isEmpty(attr.getValue())) {
mandatoryNullOrEmpty.add(item.getExtAttrName());
}
});
if (!mandatoryMissing.isEmpty()) {
preparedAttrs.getRight().add(AttributeBuilder.build(
MANDATORY_MISSING_ATTR_NAME, mandatoryMissing));
}
if (!mandatoryNullOrEmpty.isEmpty()) {
preparedAttrs.getRight().add(AttributeBuilder.build(
MANDATORY_NULL_OR_EMPTY_ATTR_NAME, mandatoryNullOrEmpty));
}
return new PropagationTaskInfo(
resource,
operation,
new ObjectClass(provision.getObjectClass()),
any.getType().getKind(),
any.getType().getKey(),
any.getKey(),
preparedAttrs.getLeft(),
new PropagationData(preparedAttrs.getRight()));
}
/**
* Create propagation tasks.
*
* @param any to be provisioned
* @param password clear text password to be provisioned
* @param changePwd whether password should be included for propagation attributes or not
* @param enable whether user must be enabled or not
* @param propByRes operation to be performed per resource
* @param propByLinkedAccount operation to be performed on linked accounts
* @param vAttrs virtual attributes to be set
* @return list of propagation tasks created
*/
protected List<PropagationTaskInfo> createTasks(
final Any<?> any,
final String password,
final boolean changePwd,
final Boolean enable,
final PropagationByResource<String> propByRes,
final PropagationByResource<Pair<String, String>> propByLinkedAccount,
final Collection<Attr> vAttrs) {
LOG.debug("Provisioning {}:\n{}", any, propByRes);
// Avoid duplicates - see javadoc
propByRes.purge();
LOG.debug("After purge {}:\n{}", any, propByRes);
// Virtual attributes
Set<String> virtualResources = new HashSet<>();
virtualResources.addAll(propByRes.get(ResourceOperation.CREATE));
virtualResources.addAll(propByRes.get(ResourceOperation.UPDATE));
virtualResources.addAll(anyUtilsFactory.getInstance(any).dao().findAllResourceKeys(any.getKey()));
Map<String, Set<Attribute>> vAttrMap = new HashMap<>();
if (vAttrs != null) {
vAttrs.forEach(vAttr -> {
VirSchema schema = virSchemaDAO.find(vAttr.getSchema());
if (schema == null) {
LOG.warn("Ignoring invalid {} {}", VirSchema.class.getSimpleName(), vAttr.getSchema());
} else if (schema.isReadonly()) {
LOG.warn("Ignoring read-only {} {}", VirSchema.class.getSimpleName(), vAttr.getSchema());
} else if (anyUtilsFactory.getInstance(any).dao().
findAllowedSchemas(any, VirSchema.class).contains(schema)
&& virtualResources.contains(schema.getResource().getKey())) {
Set<Attribute> values = vAttrMap.get(schema.getResource().getKey());
if (values == null) {
values = new HashSet<>();
vAttrMap.put(schema.getResource().getKey(), values);
}
values.add(AttributeBuilder.build(schema.getExtAttrName(), vAttr.getValues()));
if (!propByRes.contains(ResourceOperation.CREATE, schema.getResource().getKey())) {
propByRes.add(ResourceOperation.UPDATE, schema.getResource().getKey());
}
} else {
LOG.warn("{} not owned by or {} not allowed for {}", schema.getResource(), schema, any);
}
});
}
LOG.debug("With virtual attributes {}:\n{}\n{}", any, propByRes, vAttrMap);
List<PropagationTaskInfo> tasks = new ArrayList<>();
propByRes.asMap().forEach((resourceKey, operation) -> {
ExternalResource resource = resourceDAO.find(resourceKey);
Provision provision = Optional.ofNullable(resource).
flatMap(r -> r.getProvisionByAnyType(any.getType().getKey())).orElse(null);
Stream<Item> mappingItems = provision == null
? Stream.empty()
: MappingUtils.getPropagationItems(provision.getMapping().getItems().stream());
if (resource == null) {
LOG.error("Invalid resource name specified: {}, ignoring...", resourceKey);
} else if (provision == null) {
LOG.error("No provision specified on resource {} for type {}, ignoring...",
resource, any.getType());
} else if (provision.getMapping() == null || provision.getMapping().getItems().isEmpty()) {
LOG.warn("Requesting propagation for {} but no propagation mapping provided for {}",
any.getType(), resource);
} else {
Pair<String, Set<Attribute>> preparedAttrs =
mappingManager.prepareAttrsFromAny(any, password, changePwd, enable, resource, provision);
if (vAttrMap.containsKey(resourceKey)) {
preparedAttrs.getRight().addAll(vAttrMap.get(resourceKey));
}
PropagationTaskInfo task = newTask(
derAttrHandler,
any,
resource,
operation,
provision,
mappingItems,
preparedAttrs);
task.setOldConnObjectKey(propByRes.getOldConnObjectKey(resourceKey));
tasks.add(task);
LOG.debug("PropagationTask created: {}", task);
}
});
if (any instanceof User && propByLinkedAccount != null) {
User user = (User) any;
propByLinkedAccount.asMap().forEach((accountInfo, operation) -> {
LinkedAccount account = user.getLinkedAccount(accountInfo.getLeft(), accountInfo.getRight()).
orElse(null);
if (account == null && operation == ResourceOperation.DELETE) {
account = new DeletingLinkedAccount(
user, resourceDAO.find(accountInfo.getLeft()), accountInfo.getRight());
}
Provision provision = account == null || account.getResource() == null
? null
: account.getResource().getProvisionByAnyType(AnyTypeKind.USER.name()).orElse(null);
Stream<Item> mappingItems = provision == null
? Stream.empty()
: MappingUtils.getPropagationItems(provision.getMapping().getItems().stream());
if (account == null) {
LOG.error("Invalid operation {} on deleted account {} on resource {}, ignoring...",
operation, accountInfo.getRight(), accountInfo.getLeft());
} else if (account.getResource() == null) {
LOG.error("Invalid resource name specified: {}, ignoring...", accountInfo.getLeft());
} else if (provision == null) {
LOG.error("No provision specified on resource {} for type {}, ignoring...",
account.getResource(), AnyTypeKind.USER.name());
} else if (provision.getMapping() == null || provision.getMapping().getItems().isEmpty()) {
LOG.warn("Requesting propagation for {} but no propagation mapping provided for {}",
AnyTypeKind.USER.name(), account.getResource());
} else {
PropagationTaskInfo accountTask = newTask(
derAttrHandler,
user,
account.getResource(),
operation,
provision,
mappingItems,
Pair.of(account.getConnObjectKeyValue(),
mappingManager.prepareAttrsFromLinkedAccount(
user, account, password, true, provision)));
tasks.add(accountTask);
LOG.debug("PropagationTask created for Linked Account {}: {}",
account.getConnObjectKeyValue(), accountTask);
}
});
}
return tasks;
}
@Override
public List<PropagationTaskInfo> createTasks(
final Realm realm,
final PropagationByResource<String> propByRes,
final Collection<String> excludedResources) {
if (excludedResources != null) {
propByRes.removeAll(excludedResources);
}
LOG.debug("Provisioning {}:\n{}", realm, propByRes);
// Avoid duplicates - see javadoc
propByRes.purge();
LOG.debug("After purge {}:\n{}", realm, propByRes);
List<PropagationTaskInfo> tasks = new ArrayList<>();
propByRes.asMap().forEach((resourceKey, operation) -> {
ExternalResource resource = resourceDAO.find(resourceKey);
OrgUnit orgUnit = Optional.ofNullable(resource).map(ExternalResource::getOrgUnit).orElse(null);
if (resource == null) {
LOG.error("Invalid resource name specified: {}, ignoring...", resourceKey);
} else if (orgUnit == null) {
LOG.error("No orgUnit specified on resource {}, ignoring...", resource);
} else if (StringUtils.isBlank(orgUnit.getConnObjectLink())) {
LOG.warn("Requesting propagation for {} but no ConnObjectLink provided for {}",
realm.getFullPath(), resource);
} else {
Pair<String, Set<Attribute>> preparedAttrs = mappingManager.prepareAttrsFromRealm(realm, orgUnit);
PropagationTaskInfo task = new PropagationTaskInfo(
resource,
operation,
new ObjectClass(orgUnit.getObjectClass()),
null,
null,
realm.getKey(),
preparedAttrs.getLeft(),
new PropagationData(preparedAttrs.getRight()));
task.setOldConnObjectKey(propByRes.getOldConnObjectKey(resource.getKey()));
tasks.add(task);
LOG.debug("PropagationTask created: {}", task);
}
});
return tasks;
}
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
@Override
public Map<Pair<String, String>, Set<Attribute>> prepareAttrs(
final AnyTypeKind kind,
final String key,
final String password,
final boolean changePwd,
final Boolean enable,
final Collection<String> excludedResources) {
Map<Pair<String, String>, Set<Attribute>> attrs = new HashMap<>();
Any<?> any = anyUtilsFactory.getInstance(kind).dao().authFind(key);
anyUtilsFactory.getInstance(kind).dao().findAllResourceKeys(key).stream().
map(resourceDAO::find).
filter(resource -> !excludedResources.contains(resource.getKey())
&& resource.getProvisionByAnyType(any.getType().getKey()).isPresent()
&& resource.getPropagationPolicy() != null && resource.getPropagationPolicy().isUpdateDelta()).
forEach(resource -> {
Pair<String, Set<Attribute>> preparedAttrs = mappingManager.prepareAttrsFromAny(
any,
password,
changePwd,
enable,
resource,
resource.getProvisionByAnyType(any.getType().getKey()).get());
attrs.put(
Pair.of(resource.getKey(), preparedAttrs.getLeft()),
preparedAttrs.getRight());
});
if (any instanceof User) {
((User) any).getLinkedAccounts().stream().
filter(account -> !excludedResources.contains(account.getResource().getKey())
&& account.getResource().getProvisionByAnyType(any.getType().getKey()).isPresent()
&& account.getResource().getPropagationPolicy() != null
&& account.getResource().getPropagationPolicy().isUpdateDelta()).
forEach(account -> {
Set<Attribute> preparedAttrs = mappingManager.prepareAttrsFromLinkedAccount(
(User) any,
account,
password,
true,
account.getResource().getProvisionByAnyType(any.getType().getKey()).get());
attrs.put(
Pair.of(account.getResource().getKey(), account.getConnObjectKeyValue()),
preparedAttrs);
});
}
LOG.debug("Prepared attrs for {} {}: {}", kind, key, attrs);
return attrs;
}
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
@Override
public Map<Pair<String, String>, Set<Attribute>> prepareAttrs(final Realm realm) {
Map<Pair<String, String>, Set<Attribute>> attrs = new HashMap<>();
realm.getResources().stream().
filter(resource -> resource.getOrgUnit() != null
&& resource.getPropagationPolicy() != null && resource.getPropagationPolicy().isUpdateDelta()).
forEach(resource -> {
Pair<String, Set<Attribute>> preparedAttrs = mappingManager.prepareAttrsFromRealm(
realm,
resource.getOrgUnit());
attrs.put(
Pair.of(resource.getKey(), preparedAttrs.getLeft()),
preparedAttrs.getRight());
});
return attrs;
}
/**
* Checks whether the given attribute shall be treated as an ordinary attribute or not, for purpose of building
* AttributeDelta instances.
*
* @param attr ConnId attibute
* @return whether the condition is matched or not
*/
protected boolean isOrdinaryForAttrForDelta(final Attribute attr) {
return !attr.is(Name.NAME) && !OperationalAttributes.isOperationalAttribute(attr);
}
@Override
public List<PropagationTaskInfo> setAttributeDeltas(
final List<PropagationTaskInfo> tasks,
final Map<Pair<String, String>, Set<Attribute>> beforeAttrs,
final AnyUR updateRequest) {
if (beforeAttrs.isEmpty()) {
return tasks;
}
for (PropagationTaskInfo task : tasks) {
// rename is not supported by updateDelta
if (task.getOldConnObjectKey() != null && !task.getConnObjectKey().equals(task.getOldConnObjectKey())) {
continue;
}
Pair<String, String> key = Pair.of(task.getResource().getKey(), task.getConnObjectKey());
if (!beforeAttrs.containsKey(key)) {
continue;
}
Set<Attribute> attrs = new HashSet<>(beforeAttrs.get(key));
// purge unwanted attributes, even though prepared
attrs.removeIf(
attr -> MANDATORY_MISSING_ATTR_NAME.equals(attr.getName())
|| MANDATORY_NULL_OR_EMPTY_ATTR_NAME.equals(attr.getName()));
PropagationData propagationData = task.getPropagationData();
Set<AttributeDelta> attributeDeltas = new HashSet<>();
// build delta for updated attributes
propagationData.getAttributes().forEach(next -> {
Set<Object> valuesToAdd = new HashSet<>();
Set<Object> valuesToRemove = new HashSet<>();
Optional.ofNullable(AttributeUtil.find(next.getName(), attrs)).ifPresent(prev -> {
// password is unchanged from beforeAttrs but needs to be taken into account anyway
if (next.is(OperationalAttributes.PASSWORD_NAME)) {
valuesToAdd.addAll(next.getValue());
} else {
if (next.getValue() == null && prev.getValue() != null) {
valuesToRemove.addAll(prev.getValue());
} else if (next.getValue() != null && prev.getValue() == null) {
valuesToAdd.addAll(next.getValue());
} else if (next.getValue() != null && prev.getValue() != null) {
next.getValue().stream().
filter(value -> !prev.getValue().contains(value)).
forEach(valuesToAdd::add);
prev.getValue().stream().
filter(value -> !next.getValue().contains(value)).
forEach(valuesToRemove::add);
}
}
});
// Following org.identityconnectors.framework.impl.api.local.operations.UpdateDeltaImpl#updateDelta
// we create AttributeDelta instances with (valuesToAdd, valuesToRemove) or (valuesToReplace)
// depending on the attribute name
if (isOrdinaryForAttrForDelta(next)) {
if (!valuesToAdd.isEmpty() || !valuesToRemove.isEmpty()) {
attributeDeltas.add(AttributeDeltaBuilder.build(next.getName(), valuesToAdd, valuesToRemove));
}
} else {
if (!valuesToAdd.isEmpty()) {
attributeDeltas.add(AttributeDeltaBuilder.build(next.getName(), valuesToAdd));
}
}
});
// build delta for new or removed attributes
Set<String> nextNames = propagationData.getAttributes().stream().
filter(this::isOrdinaryForAttrForDelta).
map(Attribute::getName).
collect(Collectors.toSet());
Set<String> prevNames = attrs.stream().
filter(this::isOrdinaryForAttrForDelta).
map(Attribute::getName).
collect(Collectors.toSet());
nextNames.stream().filter(name -> !prevNames.contains(name)).
forEach(toAdd -> Optional.ofNullable(AttributeUtil.find(toAdd, propagationData.getAttributes())).
ifPresent(attr -> attributeDeltas.add(
AttributeDeltaBuilder.build(attr.getName(), attr.getValue(), Set.of()))));
prevNames.stream().filter(name -> !nextNames.contains(name)).
forEach(toRemove -> Optional.ofNullable(AttributeUtil.find(toRemove, attrs)).
ifPresent(attr -> attributeDeltas.add(
AttributeDeltaBuilder.build(attr.getName(), Set.of(), attr.getValue()))));
if (!attributeDeltas.isEmpty()) {
propagationData.setAttributeDeltas(attributeDeltas);
task.setUpdateRequest(updateRequest);
}
}
return tasks;
}
}