|
| 1 | +package com.evolveum.midpoint.smart.impl.conndev; |
| 2 | + |
| 3 | +import com.evolveum.midpoint.model.api.util.ResourceUtils; |
| 4 | +import com.evolveum.midpoint.prism.PrismContext; |
| 5 | +import com.evolveum.midpoint.prism.path.ItemPath; |
| 6 | +import com.evolveum.midpoint.schema.constants.SchemaConstants; |
| 7 | +import com.evolveum.midpoint.schema.result.OperationResult; |
| 8 | +import com.evolveum.midpoint.smart.api.conndev.ScimRestConfigurationProperties; |
| 9 | +import com.evolveum.midpoint.util.logging.Trace; |
| 10 | +import com.evolveum.midpoint.util.logging.TraceManager; |
| 11 | +import com.evolveum.midpoint.smart.impl.conndev.activity.ConnDevBeans; |
| 12 | +import com.evolveum.midpoint.task.api.Task; |
| 13 | +import com.evolveum.midpoint.util.exception.*; |
| 14 | +import com.evolveum.midpoint.xml.ns._public.common.common_3.ConnDevDocumentationSourceType; |
| 15 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 16 | +import com.evolveum.midpoint.xml.ns._public.common.common_3.ConnectorDevelopmentType; |
| 17 | +import com.evolveum.midpoint.xml.ns._public.common.common_3.ProcessedDocumentationType; |
| 18 | +import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType; |
| 19 | +import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType; |
| 20 | + |
| 21 | +import javax.xml.namespace.QName; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +public class ScimBackend extends RestBackend { |
| 29 | + |
| 30 | + private static final Trace LOGGER = TraceManager.getTrace(ScimBackend.class); |
| 31 | + |
| 32 | + private static final List<String> SCIM_OBJECT_CLASSES = List.of("conndev_ScimSchema", "conndev_ScimResource"); |
| 33 | + |
| 34 | + public ScimBackend(ConnDevBeans beans, ConnectorDevelopmentType connDev, Task task, OperationResult result) { |
| 35 | + super(beans, connDev, task, result); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public List<ConnDevDocumentationSourceType> discoverDocumentation() { |
| 40 | + return super.discoverDocumentation(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void ensureDocumentationIsProcessed() throws SchemaException, ExpressionEvaluationException, CommunicationException, SecurityViolationException, ConfigurationException, ObjectNotFoundException, PolicyViolationException, ObjectAlreadyExistsException { |
| 45 | + super.ensureDocumentationIsProcessed(); |
| 46 | + |
| 47 | + refreshScimDocumentation(); |
| 48 | + } |
| 49 | + |
| 50 | + private void refreshScimDocumentation() throws SchemaException, ExpressionEvaluationException, CommunicationException, SecurityViolationException, ConfigurationException, ObjectNotFoundException, PolicyViolationException, ObjectAlreadyExistsException { |
| 51 | + var testingResourceOid = getTestingResourceOid(); |
| 52 | + |
| 53 | + if (!isScimDiscoveryConfigured(testingResourceOid)) { |
| 54 | + LOGGER.info("Testing resource {} is not configured for SCIM discovery (missing scimBaseUrl or developmentMode), skipping", testingResourceOid); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + LOGGER.info("Starting SCIM schema discovery from shadows on testing resource {}", testingResourceOid); |
| 59 | + ResourceUtils.deleteSchema(testingResourceOid, beans.modelService, task, result); |
| 60 | + beans.provisioningService.testResource(testingResourceOid, task, result); |
| 61 | + |
| 62 | + var newScimDocs = SCIM_OBJECT_CLASSES |
| 63 | + .stream() |
| 64 | + .flatMap(objectClass -> loadShadowsAsDocumentation(testingResourceOid, objectClass).stream()) |
| 65 | + .map(ProcessedDocumentation::toBean) |
| 66 | + .toList(); |
| 67 | + |
| 68 | + var mergedDocs = new ArrayList<>( |
| 69 | + developmentObject() |
| 70 | + .getProcessedDocumentation() |
| 71 | + .stream() |
| 72 | + .filter(d -> SCIM_OBJECT_CLASSES.stream().noneMatch(c -> d.getUri().startsWith(c))) |
| 73 | + .map(ProcessedDocumentationType::clone) |
| 74 | + .toList() |
| 75 | + ); |
| 76 | + mergedDocs.addAll(newScimDocs); |
| 77 | + |
| 78 | + var delta = PrismContext.get().deltaFor(ConnectorDevelopmentType.class) |
| 79 | + .item(ConnectorDevelopmentType.F_PROCESSED_DOCUMENTATION) |
| 80 | + .replaceRealValues(mergedDocs) |
| 81 | + .<ConnectorDevelopmentType>asObjectDelta(developmentObject().getOid()); |
| 82 | + beans.modelService.executeChanges(List.of(delta), null, task, result); |
| 83 | + reload(); |
| 84 | + ensureDocumentationIsUploaded(client().synchronizationClient()); |
| 85 | + } |
| 86 | + |
| 87 | + private List<ProcessedDocumentation> loadShadowsAsDocumentation(String resourceOid, String objectClassLocalName) { |
| 88 | + try { |
| 89 | + var objectClass = new QName(SchemaConstants.NS_RI, objectClassLocalName); |
| 90 | + var query = PrismContext.get().queryFor(ShadowType.class) |
| 91 | + .item(ShadowType.F_RESOURCE_REF).ref(resourceOid) |
| 92 | + .and().item(ShadowType.F_OBJECT_CLASS).eq(objectClass) |
| 93 | + .build(); |
| 94 | + |
| 95 | + var shadows = beans.provisioningService.searchObjects(ShadowType.class, query, null, task, result); |
| 96 | + if (shadows.isEmpty()) { |
| 97 | + LOGGER.warn("No shadows found for object class {} on resource {}", objectClassLocalName, resourceOid); |
| 98 | + return List.of(); |
| 99 | + } |
| 100 | + |
| 101 | + var serializer = PrismContext.get().jsonSerializer(); |
| 102 | + var mapper = new ObjectMapper(); |
| 103 | + var docs = new ArrayList<ProcessedDocumentation>(); |
| 104 | + for (var shadow : shadows) { |
| 105 | + var attrs = shadow.findContainer(ShadowType.F_ATTRIBUTES); |
| 106 | + if (attrs != null && !attrs.isEmpty()) { |
| 107 | + var json = serializer.serialize(attrs.getValue()); |
| 108 | + var parsedAttrs = mapper.readTree(json).get("attributes"); |
| 109 | + var idNode = parsedAttrs.get("id"); |
| 110 | + var name = idNode != null |
| 111 | + ? idNode.asText() |
| 112 | + : shadow.getOid(); |
| 113 | + var doc = new ProcessedDocumentation(UUID.randomUUID().toString(), objectClassLocalName + "_" + name); |
| 114 | + doc.write(parsedAttrs.toString()); |
| 115 | + docs.add(doc); |
| 116 | + } |
| 117 | + } |
| 118 | + return docs; |
| 119 | + } catch (Exception e) { |
| 120 | + throw new SystemException("Could not load shadow documentation for " + objectClassLocalName, e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private String getTestingResourceOid() { |
| 125 | + var testing = developmentObject().getTesting(); |
| 126 | + if (testing == null || testing.getTestingResource() == null) { |
| 127 | + throw new SystemException("No testing resource configured"); |
| 128 | + } |
| 129 | + return testing.getTestingResource().getOid(); |
| 130 | + } |
| 131 | + |
| 132 | + private boolean isScimDiscoveryConfigured(String testingResourceOid) { |
| 133 | + try { |
| 134 | + var resource = beans.modelService.getObject(ResourceType.class, testingResourceOid, null, task, result).asObjectable(); |
| 135 | + var connectorConfig = ItemPath.create(ResourceType.F_CONNECTOR_CONFIGURATION, SchemaConstants.ICF_CONFIGURATION_PROPERTIES_LOCAL_NAME); |
| 136 | + |
| 137 | + var scimBaseUrl = resource.asPrismObject().findProperty(ItemPath.create(connectorConfig, ScimRestConfigurationProperties.SCIM_BASE_URL)); |
| 138 | + var developmentMode = resource.asPrismObject().findProperty(ItemPath.create(connectorConfig, ScimRestConfigurationProperties.DEVELOPMENT_MODE)); |
| 139 | + |
| 140 | + var hasScimBaseUrl = scimBaseUrl != null && scimBaseUrl.getRealValue() != null |
| 141 | + && !((String) scimBaseUrl.getRealValue()).isBlank(); |
| 142 | + var hasDevelopmentMode = developmentMode != null && Boolean.TRUE.equals(developmentMode.getRealValue()); |
| 143 | + |
| 144 | + return hasScimBaseUrl && hasDevelopmentMode; |
| 145 | + } catch (Exception e) { |
| 146 | + LOGGER.warn("Could not check configuration on testing resource {}: {}", testingResourceOid, e.getMessage()); |
| 147 | + return false; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments