use of org.apache.syncope.common.lib.collections.IteratorChain in project syncope by apache.
the class PullJobDelegate method doExecuteProvisioning.
@Override
protected String doExecuteProvisioning(final PullTask pullTask, final Connector connector, final boolean dryRun) throws JobExecutionException {
LOG.debug("Executing pull on {}", pullTask.getResource());
List<PullActions> actions = new ArrayList<>();
pullTask.getActions().forEach(impl -> {
try {
actions.add(ImplementationManager.build(impl));
} catch (Exception e) {
LOG.warn("While building {}", impl, e);
}
});
profile = new ProvisioningProfile<>(connector, pullTask);
profile.getActions().addAll(actions);
profile.setDryRun(dryRun);
profile.setResAct(pullTask.getResource().getPullPolicy() == null ? ConflictResolutionAction.IGNORE : pullTask.getResource().getPullPolicy().getConflictResolutionAction());
latestSyncTokens.clear();
if (!profile.isDryRun()) {
for (PullActions action : actions) {
action.beforeAll(profile);
}
}
status.set("Initialization completed");
// First realms...
if (pullTask.getResource().getOrgUnit() != null) {
status.set("Pulling " + pullTask.getResource().getOrgUnit().getObjectClass().getObjectClassValue());
OrgUnit orgUnit = pullTask.getResource().getOrgUnit();
OperationOptions options = MappingUtils.buildOperationOptions(MappingUtils.getPullItems(orgUnit.getItems()).iterator());
rhandler = buildRealmHandler();
try {
switch(pullTask.getPullMode()) {
case INCREMENTAL:
if (!dryRun) {
latestSyncTokens.put(orgUnit.getObjectClass(), orgUnit.getSyncToken());
}
connector.sync(orgUnit.getObjectClass(), orgUnit.getSyncToken(), rhandler, options);
if (!dryRun) {
orgUnit.setSyncToken(latestSyncTokens.get(orgUnit.getObjectClass()));
resourceDAO.save(orgUnit.getResource());
}
break;
case FILTERED_RECONCILIATION:
ReconFilterBuilder filterBuilder = ImplementationManager.build(pullTask.getReconFilterBuilder());
connector.filteredReconciliation(orgUnit.getObjectClass(), filterBuilder, rhandler, options);
break;
case FULL_RECONCILIATION:
default:
connector.fullReconciliation(orgUnit.getObjectClass(), rhandler, options);
break;
}
} catch (Throwable t) {
throw new JobExecutionException("While pulling from connector", t);
}
}
// ...then provisions for any types
ahandler = buildAnyObjectHandler();
uhandler = buildUserHandler();
ghandler = buildGroupHandler();
for (Provision provision : pullTask.getResource().getProvisions()) {
if (provision.getMapping() != null) {
status.set("Pulling " + provision.getObjectClass().getObjectClassValue());
SyncopePullResultHandler handler;
switch(provision.getAnyType().getKind()) {
case USER:
handler = uhandler;
break;
case GROUP:
handler = ghandler;
break;
case ANY_OBJECT:
default:
handler = ahandler;
}
try {
Set<MappingItem> linkingMappingItems = virSchemaDAO.findByProvision(provision).stream().map(schema -> schema.asLinkingMappingItem()).collect(Collectors.toSet());
Iterator<MappingItem> mapItems = new IteratorChain<>(provision.getMapping().getItems().iterator(), linkingMappingItems.iterator());
OperationOptions options = MappingUtils.buildOperationOptions(mapItems);
switch(pullTask.getPullMode()) {
case INCREMENTAL:
if (!dryRun) {
latestSyncTokens.put(provision.getObjectClass(), provision.getSyncToken());
}
connector.sync(provision.getObjectClass(), provision.getSyncToken(), handler, options);
if (!dryRun) {
provision.setSyncToken(latestSyncTokens.get(provision.getObjectClass()));
resourceDAO.save(provision.getResource());
}
break;
case FILTERED_RECONCILIATION:
ReconFilterBuilder filterBuilder = ImplementationManager.build(pullTask.getReconFilterBuilder());
connector.filteredReconciliation(provision.getObjectClass(), filterBuilder, handler, options);
break;
case FULL_RECONCILIATION:
default:
connector.fullReconciliation(provision.getObjectClass(), handler, options);
break;
}
} catch (Throwable t) {
throw new JobExecutionException("While pulling from connector", t);
}
}
}
try {
setGroupOwners(ghandler);
} catch (Exception e) {
LOG.error("While setting group owners", e);
}
if (!profile.isDryRun()) {
for (PullActions action : actions) {
action.afterAll(profile);
}
}
status.set("Pull done");
String result = createReport(profile.getResults(), pullTask.getResource(), dryRun);
LOG.debug("Pull result: {}", result);
return result;
}
use of org.apache.syncope.common.lib.collections.IteratorChain in project syncope by apache.
the class ResourceLogic method listConnObjects.
@PreAuthorize("hasRole('" + StandardEntitlement.RESOURCE_LIST_CONNOBJECT + "')")
@Transactional(readOnly = true)
public Pair<SearchResult, List<ConnObjectTO>> listConnObjects(final String key, final String anyTypeKey, final int size, final String pagedResultsCookie, final List<OrderByClause> orderBy) {
ExternalResource resource;
ObjectClass objectClass;
OperationOptions options;
if (SyncopeConstants.REALM_ANYTYPE.equals(anyTypeKey)) {
resource = resourceDAO.authFind(key);
if (resource == null) {
throw new NotFoundException("Resource '" + key + "'");
}
if (resource.getOrgUnit() == null) {
throw new NotFoundException("Realm provisioning for resource '" + key + "'");
}
objectClass = resource.getOrgUnit().getObjectClass();
options = MappingUtils.buildOperationOptions(MappingUtils.getPropagationItems(resource.getOrgUnit().getItems()).iterator());
} else {
Triple<ExternalResource, AnyType, Provision> init = connObjectInit(key, anyTypeKey);
resource = init.getLeft();
objectClass = init.getRight().getObjectClass();
init.getRight().getMapping().getItems();
Set<MappingItem> linkinMappingItems = virSchemaDAO.findByProvision(init.getRight()).stream().map(virSchema -> virSchema.asLinkingMappingItem()).collect(Collectors.toSet());
Iterator<MappingItem> mapItems = new IteratorChain<>(init.getRight().getMapping().getItems().iterator(), linkinMappingItems.iterator());
options = MappingUtils.buildOperationOptions(mapItems);
}
final List<ConnObjectTO> connObjects = new ArrayList<>();
SearchResult searchResult = connFactory.getConnector(resource).search(objectClass, null, new ResultsHandler() {
private int count;
@Override
public boolean handle(final ConnectorObject connectorObject) {
connObjects.add(ConnObjectUtils.getConnObjectTO(connectorObject));
// safety protection against uncontrolled result size
count++;
return count < size;
}
}, size, pagedResultsCookie, orderBy, options);
return ImmutablePair.of(searchResult, connObjects);
}
use of org.apache.syncope.common.lib.collections.IteratorChain in project syncope by apache.
the class ResourceLogic method readConnObject.
@PreAuthorize("hasRole('" + StandardEntitlement.RESOURCE_GET_CONNOBJECT + "')")
@Transactional(readOnly = true)
public ConnObjectTO readConnObject(final String key, final String anyTypeKey, final String anyKey) {
Triple<ExternalResource, AnyType, Provision> init = connObjectInit(key, anyTypeKey);
// 1. find any
Any<?> any = init.getMiddle().getKind() == AnyTypeKind.USER ? userDAO.find(anyKey) : init.getMiddle().getKind() == AnyTypeKind.ANY_OBJECT ? anyObjectDAO.find(anyKey) : groupDAO.find(anyKey);
if (any == null) {
throw new NotFoundException(init.getMiddle() + " " + anyKey);
}
// 2. build connObjectKeyItem
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(init.getRight());
if (!connObjectKeyItem.isPresent()) {
throw new NotFoundException("ConnObjectKey mapping for " + init.getMiddle() + " " + anyKey + " on resource '" + key + "'");
}
Optional<String> connObjectKeyValue = mappingManager.getConnObjectKeyValue(any, init.getRight());
// 3. determine attributes to query
Set<MappingItem> linkinMappingItems = virSchemaDAO.findByProvision(init.getRight()).stream().map(virSchema -> virSchema.asLinkingMappingItem()).collect(Collectors.toSet());
Iterator<MappingItem> mapItems = new IteratorChain<>(init.getRight().getMapping().getItems().iterator(), linkinMappingItems.iterator());
// 4. read from the underlying connector
Connector connector = connFactory.getConnector(init.getLeft());
ConnectorObject connectorObject = connector.getObject(init.getRight().getObjectClass(), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue.get()), MappingUtils.buildOperationOptions(mapItems));
if (connectorObject == null) {
throw new NotFoundException("Object " + connObjectKeyValue.get() + " with class " + init.getRight().getObjectClass() + " not found on resource " + key);
}
// 5. build result
Set<Attribute> attributes = connectorObject.getAttributes();
if (AttributeUtil.find(Uid.NAME, attributes) == null) {
attributes.add(connectorObject.getUid());
}
if (AttributeUtil.find(Name.NAME, attributes) == null) {
attributes.add(connectorObject.getName());
}
return ConnObjectUtils.getConnObjectTO(connectorObject);
}
use of org.apache.syncope.common.lib.collections.IteratorChain in project syncope by apache.
the class ResourceDataBinderImpl method update.
@Override
public ExternalResource update(final ExternalResource resource, final ResourceTO resourceTO) {
if (resource.getKey() != null) {
ResourceTO current = getResourceTO(resource);
if (!current.equals(resourceTO)) {
// 1. save the current configuration, before update
ExternalResourceHistoryConf resourceHistoryConf = entityFactory.newEntity(ExternalResourceHistoryConf.class);
resourceHistoryConf.setCreator(AuthContextUtils.getUsername());
resourceHistoryConf.setCreation(new Date());
resourceHistoryConf.setEntity(resource);
resourceHistoryConf.setConf(current);
resourceHistoryConfDAO.save(resourceHistoryConf);
// 2. ensure the maximum history size is not exceeded
List<ExternalResourceHistoryConf> history = resourceHistoryConfDAO.findByEntity(resource);
long maxHistorySize = confDAO.find("resource.conf.history.size", 10L);
if (maxHistorySize < history.size()) {
// always remove the last item since history was obtained by a query with ORDER BY creation DESC
for (int i = 0; i < history.size() - maxHistorySize; i++) {
resourceHistoryConfDAO.delete(history.get(history.size() - 1).getKey());
}
}
}
}
resource.setKey(resourceTO.getKey());
if (resourceTO.getConnector() != null) {
ConnInstance connector = connInstanceDAO.find(resourceTO.getConnector());
resource.setConnector(connector);
if (!connector.getResources().contains(resource)) {
connector.add(resource);
}
}
resource.setEnforceMandatoryCondition(resourceTO.isEnforceMandatoryCondition());
resource.setPropagationPriority(resourceTO.getPropagationPriority());
resource.setRandomPwdIfNotProvided(resourceTO.isRandomPwdIfNotProvided());
// 1. add or update all (valid) provisions from TO
resourceTO.getProvisions().forEach(provisionTO -> {
AnyType anyType = anyTypeDAO.find(provisionTO.getAnyType());
if (anyType == null) {
LOG.debug("Invalid {} specified {}, ignoring...", AnyType.class.getSimpleName(), provisionTO.getAnyType());
} else {
Provision provision = resource.getProvision(anyType).orElse(null);
if (provision == null) {
provision = entityFactory.newEntity(Provision.class);
provision.setResource(resource);
resource.add(provision);
provision.setAnyType(anyType);
}
if (provisionTO.getObjectClass() == null) {
SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidProvision);
sce.getElements().add("Null " + ObjectClass.class.getSimpleName());
throw sce;
}
provision.setObjectClass(new ObjectClass(provisionTO.getObjectClass()));
// add all classes contained in the TO
for (String name : provisionTO.getAuxClasses()) {
AnyTypeClass anyTypeClass = anyTypeClassDAO.find(name);
if (anyTypeClass == null) {
LOG.warn("Ignoring invalid {}: {}", AnyTypeClass.class.getSimpleName(), name);
} else {
provision.add(anyTypeClass);
}
}
// remove all classes not contained in the TO
provision.getAuxClasses().removeIf(anyTypeClass -> !provisionTO.getAuxClasses().contains(anyTypeClass.getKey()));
if (provisionTO.getMapping() == null) {
provision.setMapping(null);
} else {
Mapping mapping = provision.getMapping();
if (mapping == null) {
mapping = entityFactory.newEntity(Mapping.class);
mapping.setProvision(provision);
provision.setMapping(mapping);
} else {
mapping.getItems().clear();
}
AnyTypeClassTO allowedSchemas = new AnyTypeClassTO();
for (Iterator<AnyTypeClass> itor = new IteratorChain<>(provision.getAnyType().getClasses().iterator(), provision.getAuxClasses().iterator()); itor.hasNext(); ) {
AnyTypeClass anyTypeClass = itor.next();
allowedSchemas.getPlainSchemas().addAll(anyTypeClass.getPlainSchemas().stream().map(s -> s.getKey()).collect(Collectors.toList()));
allowedSchemas.getDerSchemas().addAll(anyTypeClass.getDerSchemas().stream().map(s -> s.getKey()).collect(Collectors.toList()));
allowedSchemas.getVirSchemas().addAll(anyTypeClass.getVirSchemas().stream().map(s -> s.getKey()).collect(Collectors.toList()));
}
populateMapping(provisionTO.getMapping(), mapping, allowedSchemas);
}
if (provisionTO.getVirSchemas().isEmpty()) {
for (VirSchema schema : virSchemaDAO.findByProvision(provision)) {
virSchemaDAO.delete(schema.getKey());
}
} else {
for (String schemaName : provisionTO.getVirSchemas()) {
VirSchema schema = virSchemaDAO.find(schemaName);
if (schema == null) {
LOG.debug("Invalid {} specified: {}, ignoring...", VirSchema.class.getSimpleName(), schemaName);
} else {
schema.setProvision(provision);
}
}
}
}
});
// 2. remove all provisions not contained in the TO
for (Iterator<? extends Provision> itor = resource.getProvisions().iterator(); itor.hasNext(); ) {
Provision provision = itor.next();
if (resourceTO.getProvision(provision.getAnyType().getKey()) == null) {
virSchemaDAO.findByProvision(provision).forEach(schema -> {
virSchemaDAO.delete(schema.getKey());
});
itor.remove();
}
}
// 3. orgUnit
if (resourceTO.getOrgUnit() == null && resource.getOrgUnit() != null) {
resource.getOrgUnit().setResource(null);
resource.setOrgUnit(null);
} else if (resourceTO.getOrgUnit() != null) {
OrgUnitTO orgUnitTO = resourceTO.getOrgUnit();
OrgUnit orgUnit = resource.getOrgUnit();
if (orgUnit == null) {
orgUnit = entityFactory.newEntity(OrgUnit.class);
orgUnit.setResource(resource);
resource.setOrgUnit(orgUnit);
}
if (orgUnitTO.getObjectClass() == null) {
SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidOrgUnit);
sce.getElements().add("Null " + ObjectClass.class.getSimpleName());
throw sce;
}
orgUnit.setObjectClass(new ObjectClass(orgUnitTO.getObjectClass()));
if (orgUnitTO.getConnObjectLink() == null) {
SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidOrgUnit);
sce.getElements().add("Null connObjectLink");
throw sce;
}
orgUnit.setConnObjectLink(orgUnitTO.getConnObjectLink());
SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
SyncopeClientException invalidMapping = SyncopeClientException.build(ClientExceptionType.InvalidMapping);
SyncopeClientException requiredValuesMissing = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
orgUnit.getItems().clear();
for (ItemTO itemTO : orgUnitTO.getItems()) {
if (itemTO == null) {
LOG.error("Null {}", ItemTO.class.getSimpleName());
invalidMapping.getElements().add("Null " + ItemTO.class.getSimpleName());
} else if (itemTO.getIntAttrName() == null) {
requiredValuesMissing.getElements().add("intAttrName");
scce.addException(requiredValuesMissing);
} else {
if (!"name".equals(itemTO.getIntAttrName()) && !"fullpath".equals(itemTO.getIntAttrName())) {
LOG.error("Only 'name' and 'fullpath' are supported for Realms");
invalidMapping.getElements().add("Only 'name' and 'fullpath' are supported for Realms");
} else {
// no mandatory condition implies mandatory condition false
if (!JexlUtils.isExpressionValid(itemTO.getMandatoryCondition() == null ? "false" : itemTO.getMandatoryCondition())) {
SyncopeClientException invalidMandatoryCondition = SyncopeClientException.build(ClientExceptionType.InvalidValues);
invalidMandatoryCondition.getElements().add(itemTO.getMandatoryCondition());
scce.addException(invalidMandatoryCondition);
}
OrgUnitItem item = entityFactory.newEntity(OrgUnitItem.class);
BeanUtils.copyProperties(itemTO, item, ITEM_IGNORE_PROPERTIES);
item.setOrgUnit(orgUnit);
if (item.isConnObjectKey()) {
orgUnit.setConnObjectKeyItem(item);
} else {
orgUnit.add(item);
}
itemTO.getTransformers().forEach(transformerKey -> {
Implementation transformer = implementationDAO.find(transformerKey);
if (transformer == null) {
LOG.debug("Invalid " + Implementation.class.getSimpleName() + " {}, ignoring...", transformerKey);
} else {
item.add(transformer);
}
});
// remove all implementations not contained in the TO
item.getTransformers().removeIf(implementation -> !itemTO.getTransformers().contains(implementation.getKey()));
}
}
}
if (!invalidMapping.getElements().isEmpty()) {
scce.addException(invalidMapping);
}
if (scce.hasExceptions()) {
throw scce;
}
}
resource.setCreateTraceLevel(resourceTO.getCreateTraceLevel());
resource.setUpdateTraceLevel(resourceTO.getUpdateTraceLevel());
resource.setDeleteTraceLevel(resourceTO.getDeleteTraceLevel());
resource.setProvisioningTraceLevel(resourceTO.getProvisioningTraceLevel());
resource.setPasswordPolicy(resourceTO.getPasswordPolicy() == null ? null : (PasswordPolicy) policyDAO.find(resourceTO.getPasswordPolicy()));
resource.setAccountPolicy(resourceTO.getAccountPolicy() == null ? null : (AccountPolicy) policyDAO.find(resourceTO.getAccountPolicy()));
resource.setPullPolicy(resourceTO.getPullPolicy() == null ? null : (PullPolicy) policyDAO.find(resourceTO.getPullPolicy()));
resource.setConfOverride(new HashSet<>(resourceTO.getConfOverride()));
resource.setOverrideCapabilities(resourceTO.isOverrideCapabilities());
resource.getCapabilitiesOverride().clear();
resource.getCapabilitiesOverride().addAll(resourceTO.getCapabilitiesOverride());
resourceTO.getPropagationActions().forEach(propagationActionKey -> {
Implementation propagationAction = implementationDAO.find(propagationActionKey);
if (propagationAction == null) {
LOG.debug("Invalid " + Implementation.class.getSimpleName() + " {}, ignoring...", propagationActionKey);
} else {
resource.add(propagationAction);
}
});
// remove all implementations not contained in the TO
resource.getPropagationActions().removeIf(implementation -> !resourceTO.getPropagationActions().contains(implementation.getKey()));
return resource;
}
use of org.apache.syncope.common.lib.collections.IteratorChain in project syncope by apache.
the class AbstractPropagationTaskExecutor method getRemoteObject.
/**
* Get remote object for given task.
*
* @param connector connector facade proxy.
* @param task current propagation task.
* @param provision provision
* @param latest 'FALSE' to retrieve object using old connObjectKey if not null.
* @return remote connector object.
*/
protected ConnectorObject getRemoteObject(final PropagationTask task, final Connector connector, final Provision provision, final boolean latest) {
String connObjectKey = latest || task.getOldConnObjectKey() == null ? task.getConnObjectKey() : task.getOldConnObjectKey();
Set<MappingItem> linkingMappingItems = virSchemaDAO.findByProvision(provision).stream().map(schema -> schema.asLinkingMappingItem()).collect(Collectors.toSet());
ConnectorObject obj = null;
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(provision);
if (connObjectKeyItem.isPresent()) {
try {
obj = connector.getObject(new ObjectClass(task.getObjectClassName()), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKey), MappingUtils.buildOperationOptions(new IteratorChain<>(MappingUtils.getPropagationItems(provision.getMapping().getItems()).iterator(), linkingMappingItems.iterator())));
for (MappingItem item : linkingMappingItems) {
Attribute attr = obj.getAttributeByName(item.getExtAttrName());
if (attr == null) {
virAttrCache.expire(task.getAnyType(), task.getEntityKey(), item.getIntAttrName());
} else {
VirAttrCacheValue cacheValue = new VirAttrCacheValue();
cacheValue.setValues(attr.getValue());
virAttrCache.put(task.getAnyType(), task.getEntityKey(), item.getIntAttrName(), cacheValue);
}
}
} catch (TimeoutException toe) {
LOG.debug("Request timeout", toe);
throw toe;
} catch (RuntimeException ignore) {
LOG.debug("While resolving {}", connObjectKey, ignore);
}
}
return obj;
}
Aggregations