use of org.apache.syncope.core.persistence.api.entity.resource.Provision in project syncope by apache.
the class JPAExternalResourceDAO method delete.
@Override
public void delete(final String name) {
ExternalResource resource = find(name);
if (resource == null) {
return;
}
taskDAO().deleteAll(resource, TaskType.PROPAGATION);
taskDAO().deleteAll(resource, TaskType.PULL);
taskDAO().deleteAll(resource, TaskType.PUSH);
realmDAO().findByResource(resource).forEach(realm -> realm.getResources().remove(resource));
anyObjectDAO().findByResource(resource).forEach(anyObject -> anyObject.getResources().remove(resource));
userDAO().findByResource(resource).forEach(user -> user.getResources().remove(resource));
groupDAO().findByResource(resource).forEach(group -> group.getResources().remove(resource));
policyDAO().findByResource(resource).forEach(policy -> policy.getResources().remove(resource));
resource.getProvisions().stream().filter(provision -> provision.getMapping() != null).peek(provision -> provision.getMapping().getItems().forEach(item -> item.setMapping(null))).peek(provision -> {
provision.getMapping().getItems().clear();
provision.setMapping(null);
provision.setResource(null);
}).forEachOrdered(provision -> virSchemaDAO().findByProvision(provision).forEach(schema -> virSchemaDAO().delete(schema.getKey())));
externalResourceHistoryConfDAO().deleteByEntity(resource);
if (resource.getConnector() != null && resource.getConnector().getResources() != null && !resource.getConnector().getResources().isEmpty()) {
resource.getConnector().getResources().remove(resource);
}
resource.setConnector(null);
entityManager().remove(resource);
}
use of org.apache.syncope.core.persistence.api.entity.resource.Provision in project syncope by apache.
the class PropagationManagerImpl method createTasks.
/**
* 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 deleteOnResource whether any must be deleted anyway from external resource or not
* @param propByRes operation to be performed per resource
* @param vAttrs virtual attributes to be set
* @return list of propagation tasks created
*/
protected List<PropagationTaskTO> createTasks(final Any<?> any, final String password, final boolean changePwd, final Boolean enable, final boolean deleteOnResource, final PropagationByResource propByRes, final Collection<AttrTO> 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(dao(any.getType().getKind()).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).getAllowedSchemas(any, VirSchema.class).contains(schema) && virtualResources.contains(schema.getProvision().getResource().getKey())) {
Set<Attribute> values = vAttrMap.get(schema.getProvision().getResource().getKey());
if (values == null) {
values = new HashSet<>();
vAttrMap.put(schema.getProvision().getResource().getKey(), values);
}
values.add(AttributeBuilder.build(schema.getExtAttrName(), vAttr.getValues()));
propByRes.add(ResourceOperation.UPDATE, schema.getProvision().getResource().getKey());
} else {
LOG.warn("{} not owned by or {} not allowed for {}", schema.getProvision().getResource(), schema, any);
}
});
}
LOG.debug("With virtual attributes {}:\n{}\n{}", any, propByRes, vAttrMap);
List<PropagationTaskTO> tasks = new ArrayList<>();
propByRes.asMap().forEach((resourceKey, operation) -> {
ExternalResource resource = resourceDAO.find(resourceKey);
Provision provision = resource == null ? null : resource.getProvision(any.getType()).orElse(null);
List<? extends Item> mappingItems = provision == null ? Collections.<Item>emptyList() : MappingUtils.getPropagationItems(provision.getMapping().getItems());
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 (mappingItems.isEmpty()) {
LOG.warn("Requesting propagation for {} but no propagation mapping provided for {}", any.getType(), resource);
} else {
PropagationTaskTO task = new PropagationTaskTO();
task.setResource(resource.getKey());
task.setObjectClassName(provision.getObjectClass().getObjectClassValue());
task.setAnyTypeKind(any.getType().getKind());
task.setAnyType(any.getType().getKey());
if (!deleteOnResource) {
task.setEntityKey(any.getKey());
}
task.setOperation(operation);
task.setOldConnObjectKey(propByRes.getOldConnObjectKey(resource.getKey()));
Pair<String, Set<Attribute>> preparedAttrs = mappingManager.prepareAttrs(any, password, changePwd, enable, provision);
task.setConnObjectKey(preparedAttrs.getKey());
// 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.stream().filter(item -> (!item.isConnObjectKey() && JexlUtils.evaluateMandatoryCondition(item.getMandatoryCondition(), any))).forEachOrdered(item -> {
Attribute attr = AttributeUtil.find(item.getExtAttrName(), preparedAttrs.getValue());
if (attr == null) {
mandatoryMissing.add(item.getExtAttrName());
} else if (attr.getValue() == null || attr.getValue().isEmpty()) {
mandatoryNullOrEmpty.add(item.getExtAttrName());
}
});
if (!mandatoryMissing.isEmpty()) {
preparedAttrs.getValue().add(AttributeBuilder.build(PropagationTaskExecutor.MANDATORY_MISSING_ATTR_NAME, mandatoryMissing));
}
if (!mandatoryNullOrEmpty.isEmpty()) {
preparedAttrs.getValue().add(AttributeBuilder.build(PropagationTaskExecutor.MANDATORY_NULL_OR_EMPTY_ATTR_NAME, mandatoryNullOrEmpty));
}
if (vAttrMap.containsKey(resource.getKey())) {
preparedAttrs.getValue().addAll(vAttrMap.get(resource.getKey()));
}
task.setAttributes(POJOHelper.serialize(preparedAttrs.getValue()));
tasks.add(task);
LOG.debug("PropagationTask created: {}", task);
}
});
return tasks;
}
use of org.apache.syncope.core.persistence.api.entity.resource.Provision in project syncope by apache.
the class AbstractProvisioningJobDelegate method doExecute.
@Override
protected String doExecute(final boolean dryRun) throws JobExecutionException {
try {
Class<T> clazz = getTaskClassReference();
if (!clazz.isAssignableFrom(task.getClass())) {
throw new JobExecutionException("Task " + task.getKey() + " isn't a ProvisioningTask");
}
T provisioningTask = clazz.cast(task);
Connector connector;
try {
connector = connFactory.getConnector(provisioningTask.getResource());
} catch (Exception e) {
String msg = String.format("Connector instance bean for resource %s and connInstance %s not found", provisioningTask.getResource(), provisioningTask.getResource().getConnector());
throw new JobExecutionException(msg, e);
}
boolean noMapping = true;
for (Provision provision : provisioningTask.getResource().getProvisions()) {
Mapping mapping = provision.getMapping();
if (mapping != null) {
noMapping = false;
if (mapping.getConnObjectKeyItem() == null) {
throw new JobExecutionException("Invalid ConnObjectKey mapping for provision " + provision);
}
}
}
if (noMapping) {
noMapping = provisioningTask.getResource().getOrgUnit() == null;
}
if (noMapping) {
return "No provisions nor orgUnit available: aborting...";
}
return doExecuteProvisioning(provisioningTask, connector, dryRun);
} catch (Throwable t) {
LOG.error("While executing provisioning job {}", getClass().getName(), t);
throw t;
}
}
use of org.apache.syncope.core.persistence.api.entity.resource.Provision in project syncope by apache.
the class ReconciliationReportlet method doExtract.
private void doExtract(final ContentHandler handler, final List<? extends Any<?>> anys) throws SAXException, ReportException {
final Set<Missing> missing = new HashSet<>();
final Set<Misaligned> misaligned = new HashSet<>();
for (Any<?> any : anys) {
missing.clear();
misaligned.clear();
AnyUtils anyUtils = anyUtilsFactory.getInstance(any);
anyUtils.getAllResources(any).forEach(resource -> {
Provision provision = resource.getProvision(any.getType()).orElse(null);
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(provision);
final String connObjectKeyValue = connObjectKeyItem.isPresent() ? mappingManager.getConnObjectKeyValue(any, provision).get() : StringUtils.EMPTY;
if (provision != null && connObjectKeyItem.isPresent() && StringUtils.isNotBlank(connObjectKeyValue)) {
// 1. read from the underlying connector
Connector connector = connFactory.getConnector(resource);
ConnectorObject connectorObject = connector.getObject(provision.getObjectClass(), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue), MappingUtils.buildOperationOptions(provision.getMapping().getItems().iterator()));
if (connectorObject == null) {
// 2. not found on resource?
LOG.error("Object {} with class {} not found on resource {}", connObjectKeyValue, provision.getObjectClass(), resource);
missing.add(new Missing(resource.getKey(), connObjectKeyValue));
} else {
// 3. found but misaligned?
Pair<String, Set<Attribute>> preparedAttrs = mappingManager.prepareAttrs(any, null, false, null, provision);
preparedAttrs.getRight().add(AttributeBuilder.build(Uid.NAME, preparedAttrs.getLeft()));
preparedAttrs.getRight().add(AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), preparedAttrs.getLeft()));
final Map<String, Set<Object>> syncopeAttrs = new HashMap<>();
preparedAttrs.getRight().forEach(attr -> {
syncopeAttrs.put(attr.getName(), getValues(attr));
});
final Map<String, Set<Object>> resourceAttrs = new HashMap<>();
connectorObject.getAttributes().stream().filter(attr -> (!OperationalAttributes.PASSWORD_NAME.equals(attr.getName()) && !OperationalAttributes.ENABLE_NAME.equals(attr.getName()))).forEachOrdered(attr -> {
resourceAttrs.put(attr.getName(), getValues(attr));
});
syncopeAttrs.keySet().stream().filter(syncopeAttr -> !resourceAttrs.containsKey(syncopeAttr)).forEach(name -> {
misaligned.add(new Misaligned(resource.getKey(), connObjectKeyValue, name, syncopeAttrs.get(name), Collections.emptySet()));
});
resourceAttrs.forEach((key, values) -> {
if (syncopeAttrs.containsKey(key)) {
if (!Objects.equals(syncopeAttrs.get(key), values)) {
misaligned.add(new Misaligned(resource.getKey(), connObjectKeyValue, key, syncopeAttrs.get(key), values));
}
} else {
misaligned.add(new Misaligned(resource.getKey(), connObjectKeyValue, key, Collections.emptySet(), values));
}
});
}
}
});
if (!missing.isEmpty() || !misaligned.isEmpty()) {
doExtract(handler, any, missing, misaligned);
}
}
}
use of org.apache.syncope.core.persistence.api.entity.resource.Provision 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;
}
Aggregations