Skip to content

Commit

Permalink
Fix connector schema parsing race condition
Browse files Browse the repository at this point in the history
The DOM objects used in midPoint are not thread safe, even for reading.
The problem manifests itself when a connector is initialized in
multiple threads at once.

This commit provides an immediate fix by coordinating access to
that particular place. (Note that the issue emerged also due to
- mistaken - schema cloning elimination in
59bee63b1b8eb933db39e8a9b61a4023b25ec4c0, but we are not ready
to re-introduce schema cloning now because of the expected
performance penalty.)

This resolves MID-8860.

(cherry picked from commit e254736)
  • Loading branch information
mederly committed Jun 1, 2023
1 parent 40cf0ac commit a0527c5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public static PrismSchema parseConnectorSchema(ConnectorType connectorType, Pris
if (connectorSchemaElement == null) {
return null;
}
PrismSchema connectorSchema = PrismSchemaImpl.parse(connectorSchemaElement, true, "schema for " + connectorType, prismContext);
PrismSchema connectorSchema =
PrismSchemaImpl.parse(
connectorSchemaElement, true, "schema for " + connectorType, prismContext);
// Make sure that the config container definition has a correct compile-time class name
QName configContainerQName = new QName(connectorType.getNamespace(), ResourceType.F_CONNECTOR_CONFIGURATION.getLocalPart());
PrismContainerDefinition<ConnectorConfigurationType> configurationContainerDefinition =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public abstract class AbstractProvisioningIntegrationTest extends AbstractIntegr

@Override
public void initSystem(Task initTask, OperationResult initResult) throws Exception {
// We need to switch off the encryption checks. Some values cannot be encrypted as we do not have a definition.
InternalsConfig.encryptionChecks = false;
repositoryService.postInit(initResult); // initialize caches here
provisioningService.postInit(initResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ protected PrismObject<ResourceType> getResource() {

@Override
public void initSystem(Task initTask, OperationResult initResult) throws Exception {
// We need to switch off the encryption checks. Some values cannot be encrypted as we do
// not have a definition here
InternalsConfig.encryptionChecks = false;
provisioningService.postInit(initResult);
super.initSystem(initTask, initResult);
resource = addResourceFromFile(getResourceDummyFile(), getDummyConnectorType(), initResult);
resourceType = resource.asObjectable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,45 @@
*/
package com.evolveum.midpoint.provisioning.impl.dummy;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.AssertJUnit.*;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import java.util.List;
import java.util.concurrent.*;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.test.IntegrationTestTools;

import org.jetbrains.annotations.NotNull;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.w3c.dom.Element;

import com.evolveum.icf.dummy.resource.DummyAccount;
import com.evolveum.icf.dummy.resource.DummyObjectClass;
import com.evolveum.midpoint.common.refinery.RefinedAssociationDefinition;
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.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.crypto.EncryptionException;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.processor.ResourceSchema;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ConnectorTypeUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.IntegrationTestTools;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationProvisioningScriptsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowKindType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.CredentialsCapabilityType;
import com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.PasswordCapabilityType;

Expand Down Expand Up @@ -254,6 +259,57 @@ public void test499DeleteAccountElizabeth() throws Exception {
assertSteadyResource();
}

/** MID-8860. Here we try to reproduce the bug by repeated parsing of a connector schema. */
@Test
public void test990ParseConnectorSchemaMultithreaded() throws CommonException, InterruptedException, ExecutionException, TimeoutException {
Task task = getTestTask();
OperationResult result = task.getResult();

final int THREADS = 10;

given("caching the connector search result");
getCsvConnector(result);

when("retrieving and parsing the schema repeatedly");
CountDownLatch latch = new CountDownLatch(1);
ExecutorService executorService = Executors.newFixedThreadPool(THREADS);
List<Future<Boolean>> futures = new ArrayList<>();
for (int i = 0; i < THREADS; i++) {
futures.add(
executorService.submit(() -> {
try {
latch.await();
long time = System.nanoTime();
ConnectorType connector = getCsvConnector(result);
var hasSchema = ConnectorTypeUtil.parseConnectorSchema(connector, PrismContext.get()) != null;
Element elem = ConnectorTypeUtil.getConnectorXsdSchema(connector);
System.out.println(time + ": " + Thread.currentThread().getId() + ": " + System.identityHashCode(elem));
return hasSchema;
} catch (SchemaException e) {
throw new AssertionError(e);
}
}));
}
latch.countDown();

then("everything is OK");
for (Future<Boolean> future : futures) {
assertThat(future.get(30, TimeUnit.SECONDS)).isTrue();
}
}

private @NotNull ConnectorType getCsvConnector(OperationResult result) throws SchemaException {
var connectors = repositoryService.searchObjects(
ConnectorType.class,
PrismContext.get().queryFor(ConnectorType.class)
.item(ConnectorType.F_CONNECTOR_TYPE)
.eq("com.evolveum.polygon.connector.csv.CsvConnector")
.build(),
null,
result);
return MiscUtil.extractSingleton(connectors).asObjectable();
}

@Override
protected void checkAccountWill(PrismObject<ShadowType> shadow, OperationResult result, XMLGregorianCalendar startTs, XMLGregorianCalendar endTs) throws SchemaException, EncryptionException {
super.checkAccountWill(shadow, result, startTs, endTs);
Expand Down

0 comments on commit a0527c5

Please sign in to comment.