Skip to content

Commit bdaa34a

Browse files
fix issue with tenant detection in tenant specific channels
1 parent 2d19e4d commit bdaa34a

3 files changed

Lines changed: 200 additions & 2 deletions

File tree

‎modules/flowable-engine/src/test/java/org/flowable/engine/test/eventregistry/MultiTenantBpmnEventRegistryConsumerTest.java‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121
import java.util.Set;
2222

23+
import org.flowable.common.engine.impl.DefaultTenantProvider;
2324
import org.flowable.common.engine.impl.interceptor.EngineConfigurationConstants;
2425
import org.flowable.engine.ProcessEngineConfiguration;
2526
import org.flowable.engine.repository.DeploymentBuilder;
@@ -278,6 +279,101 @@ public void testStartProcessInstanceWithSameEventKeyDeployedInDifferentTenants()
278279
.containsExactly("task tenant A", "task tenant B", "task tenant B");
279280
}
280281

282+
@Test
283+
public void testChannelOfATenantOnlyStartsProcessInstancesInItsTenant() {
284+
deployProcessModel("startProcessInstanceSameKeyTenantA.bpmn20.xml", TENANT_A);
285+
deployProcessModel("startProcessInstanceSameKeyTenantB.bpmn20.xml", TENANT_B);
286+
287+
// The channel of tenant A detects the tenant from the event, which can name another tenant, the default tenant or no tenant
288+
getEventRegistryEngineConfiguration().getExpressionManager().getBeans()
289+
.put("tenantAJsonPointerChannelAdapter", new TestInboundChannelAdapter(processEngineConfiguration.getObjectMapper()));
290+
getEventRepositoryService().createInboundChannelModelBuilder()
291+
.key("tenantAJsonPointerChannel")
292+
.resourceName("tenantAJsonPointerChannel.channel")
293+
.deploymentTenantId(TENANT_A)
294+
.channelAdapter("${tenantAJsonPointerChannelAdapter}")
295+
.jsonDeserializer()
296+
.fixedEventKey("sameKey")
297+
.detectEventTenantUsingJsonPointerExpression("/tenantId")
298+
.jsonFieldsMapDirectlyToPayload()
299+
.deploy();
300+
TestInboundChannelAdapter channelAdapter = (TestInboundChannelAdapter) ((InboundChannelModel) getEventRepositoryService()
301+
.getChannelModelByKey("tenantAJsonPointerChannel", TENANT_A)).getInboundEventChannelAdapter();
302+
303+
channelAdapter.triggerEventForTenantId("customerA", TENANT_B);
304+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isEqualTo(1);
305+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isZero();
306+
307+
channelAdapter.triggerEventForTenantId("customerA", ProcessEngineConfiguration.NO_TENANT_ID);
308+
channelAdapter.triggerEventForTenantId("customerA", null);
309+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isEqualTo(3);
310+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isZero();
311+
assertThat(runtimeService.createProcessInstanceQuery().count()).isEqualTo(3);
312+
}
313+
314+
@Test
315+
public void testChannelOfATenantWithoutTenantDetectionOnlyStartsProcessInstancesInItsTenant() {
316+
deployProcessModel("startProcessInstanceSameKeyTenantA.bpmn20.xml", TENANT_A);
317+
deployProcessModel("startProcessInstanceSameKeyTenantB.bpmn20.xml", TENANT_B);
318+
319+
// Without the tenant of the channel the event would have no tenant, which correlates it with the event subscriptions of all tenants
320+
getEventRegistryEngineConfiguration().getExpressionManager().getBeans()
321+
.put("tenantANoTenantDetectionChannelAdapter", new TestInboundChannelAdapter(processEngineConfiguration.getObjectMapper()));
322+
getEventRepositoryService().createInboundChannelModelBuilder()
323+
.key("tenantANoTenantDetectionChannel")
324+
.resourceName("tenantANoTenantDetectionChannel.channel")
325+
.deploymentTenantId(TENANT_A)
326+
.channelAdapter("${tenantANoTenantDetectionChannelAdapter}")
327+
.jsonDeserializer()
328+
.fixedEventKey("sameKey")
329+
.jsonFieldsMapDirectlyToPayload()
330+
.deploy();
331+
TestInboundChannelAdapter channelAdapter = (TestInboundChannelAdapter) ((InboundChannelModel) getEventRepositoryService()
332+
.getChannelModelByKey("tenantANoTenantDetectionChannel", TENANT_A)).getInboundEventChannelAdapter();
333+
334+
channelAdapter.triggerEventForTenantId("customerA", TENANT_B);
335+
channelAdapter.triggerEventWithoutTenantId("customerA");
336+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isEqualTo(2);
337+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isZero();
338+
}
339+
340+
@Test
341+
public void testChannelOfTheDefaultTenantStartsProcessInstancesInTheDetectedTenant() {
342+
deployProcessModel("startProcessInstanceSameKeyTenantA.bpmn20.xml", TENANT_A);
343+
deployProcessModel("startProcessInstanceSameKeyTenantB.bpmn20.xml", TENANT_B);
344+
345+
// A channel in the default tenant is shared with the other tenants, so it gets events for the tenant it detects
346+
DefaultTenantProvider originalDefaultTenantProvider = getEventRegistryEngineConfiguration().getDefaultTenantProvider();
347+
getEventRegistryEngineConfiguration().setDefaultTenantValue("defaultTenant");
348+
try {
349+
getEventRegistryEngineConfiguration().getExpressionManager().getBeans()
350+
.put("defaultTenantChannelAdapter", new TestInboundChannelAdapter(processEngineConfiguration.getObjectMapper()));
351+
getEventRepositoryService().createInboundChannelModelBuilder()
352+
.key("defaultTenantChannel")
353+
.resourceName("defaultTenantChannel.channel")
354+
.deploymentTenantId("defaultTenant")
355+
.channelAdapter("${defaultTenantChannelAdapter}")
356+
.jsonDeserializer()
357+
.fixedEventKey("sameKey")
358+
.detectEventTenantUsingJsonPointerExpression("/tenantId")
359+
.jsonFieldsMapDirectlyToPayload()
360+
.deploy();
361+
TestInboundChannelAdapter channelAdapter = (TestInboundChannelAdapter) ((InboundChannelModel) getEventRepositoryService()
362+
.getChannelModelByKey("defaultTenantChannel", "defaultTenant")).getInboundEventChannelAdapter();
363+
364+
channelAdapter.triggerEventForTenantId("customerA", TENANT_B);
365+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isZero();
366+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isEqualTo(1);
367+
368+
channelAdapter.triggerEventForTenantId("customerA", TENANT_A);
369+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isEqualTo(1);
370+
assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isEqualTo(1);
371+
372+
} finally {
373+
getEventRegistryEngineConfiguration().setDefaultTenantProvider(originalDefaultTenantProvider);
374+
}
375+
}
376+
281377
@Test
282378
public void testUniqueStartProcessInstanceWithSameEventKeyDeployedInDifferentTenants() {
283379
deployProcessModel("startUniqueProcessInstanceSameKeyTenantA.bpmn20.xml", TENANT_A);

‎modules/flowable-event-registry/src/main/java/org/flowable/eventregistry/impl/pipeline/InboundChannelModelProcessor.java‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.apache.commons.lang3.StringUtils;
2020
import org.flowable.common.engine.api.FlowableException;
21+
import org.flowable.common.engine.api.scope.ScopeTypes;
2122
import org.flowable.common.engine.impl.el.VariableContainerWrapper;
2223
import org.flowable.eventregistry.api.ChannelModelProcessor;
2324
import org.flowable.eventregistry.api.EventRegistry;
@@ -39,6 +40,7 @@
3940
import org.flowable.eventregistry.impl.payload.XmlElementsToMapPayloadExtractor;
4041
import org.flowable.eventregistry.impl.serialization.StringToJsonDeserializer;
4142
import org.flowable.eventregistry.impl.serialization.StringToXmlDocumentDeserializer;
43+
import org.flowable.eventregistry.impl.tenantdetector.ChannelTenantInboundEventTenantDetector;
4244
import org.flowable.eventregistry.impl.tenantdetector.InboundEventStaticTenantDetector;
4345
import org.flowable.eventregistry.impl.tenantdetector.JsonPointerBasedInboundEventTenantDetector;
4446
import org.flowable.eventregistry.impl.tenantdetector.XpathBasedInboundEventTenantDetector;
@@ -81,8 +83,43 @@ public void registerChannelModel(ChannelModel channelModel, String tenantId, Eve
8183
EventRepositoryService eventRepositoryService,
8284
boolean fallbackToDefaultTenant) {
8385

84-
if (channelModel instanceof InboundChannelModel) {
85-
registerChannelModel((InboundChannelModel) channelModel, eventRepositoryService, fallbackToDefaultTenant);
86+
if (channelModel instanceof InboundChannelModel inboundChannelModel) {
87+
registerChannelModel(inboundChannelModel, eventRepositoryService, fallbackToDefaultTenant);
88+
89+
// A custom pipeline is responsible for the tenant of its events itself
90+
if (StringUtils.isEmpty(inboundChannelModel.getPipelineDelegateExpression())
91+
&& inboundChannelModel.getInboundEventProcessingPipeline() instanceof DefaultInboundEventProcessingPipeline<?> eventProcessingPipeline
92+
&& isTenantSpecificChannel(inboundChannelModel, tenantId, fallbackToDefaultTenant)) {
93+
restrictToChannelTenant(eventProcessingPipeline, inboundChannelModel, tenantId);
94+
}
95+
}
96+
}
97+
98+
/**
99+
* Whether the channel is deployed in a specific tenant, which only gets events for its own tenant. A channel without a tenant
100+
* or in the default tenant, which is shared with the other tenants, gets events for the tenant detected by its tenant detection.
101+
*/
102+
protected boolean isTenantSpecificChannel(InboundChannelModel inboundChannelModel, String tenantId, boolean fallbackToDefaultTenant) {
103+
if (StringUtils.isEmpty(tenantId)) {
104+
return false;
105+
}
106+
107+
if (fallbackToDefaultTenant) {
108+
String defaultTenantId = engineConfiguration.getDefaultTenantProvider()
109+
.getDefaultTenant(tenantId, ScopeTypes.EVENT_REGISTRY, inboundChannelModel.getKey());
110+
return !tenantId.equals(defaultTenantId);
111+
}
112+
113+
return true;
114+
}
115+
116+
protected <T> void restrictToChannelTenant(DefaultInboundEventProcessingPipeline<T> eventProcessingPipeline, InboundChannelModel inboundChannelModel,
117+
String tenantId) {
118+
119+
// The pipeline is created once for a channel model, which can be registered again
120+
if (!(eventProcessingPipeline.getInboundEventTenantDetector() instanceof ChannelTenantInboundEventTenantDetector)) {
121+
eventProcessingPipeline.setInboundEventTenantDetector(new ChannelTenantInboundEventTenantDetector<>(inboundChannelModel.getKey(), tenantId,
122+
eventProcessingPipeline.getInboundEventTenantDetector()));
86123
}
87124
}
88125

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.eventregistry.impl.tenantdetector;
14+
15+
import org.flowable.eventregistry.api.InboundEventTenantDetector;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
19+
/**
20+
* The tenant detector of an inbound channel that is deployed in a specific tenant. Such a channel only produces events for its own tenant:
21+
* the tenant detected by the tenant detection of the channel is replaced by the tenant of the channel. Without this, an event without
22+
* a tenant would be correlated with the event subscriptions of all tenants, and an event of another tenant would reach that tenant.
23+
*/
24+
public class ChannelTenantInboundEventTenantDetector<T> implements InboundEventTenantDetector<T> {
25+
26+
private static final Logger LOGGER = LoggerFactory.getLogger(ChannelTenantInboundEventTenantDetector.class);
27+
28+
protected String channelKey;
29+
protected String channelTenantId;
30+
protected InboundEventTenantDetector<T> channelTenantDetector;
31+
32+
/**
33+
* @param channelTenantDetector the tenant detector of the channel model, which can be {@code null} when the channel has no tenant detection
34+
*/
35+
public ChannelTenantInboundEventTenantDetector(String channelKey, String channelTenantId, InboundEventTenantDetector<T> channelTenantDetector) {
36+
this.channelKey = channelKey;
37+
this.channelTenantId = channelTenantId;
38+
this.channelTenantDetector = channelTenantDetector;
39+
}
40+
41+
@Override
42+
public String detectTenantId(T payload) {
43+
if (channelTenantDetector != null) {
44+
String detectedTenantId = channelTenantDetector.detectTenantId(payload);
45+
if (!channelTenantId.equals(detectedTenantId)) {
46+
LOGGER.warn("Tenant {} detected for an event on channel {} is not the tenant of the channel. The event is processed for tenant {}",
47+
detectedTenantId, channelKey, channelTenantId);
48+
}
49+
}
50+
51+
return channelTenantId;
52+
}
53+
54+
public String getChannelKey() {
55+
return channelKey;
56+
}
57+
58+
public String getChannelTenantId() {
59+
return channelTenantId;
60+
}
61+
62+
public InboundEventTenantDetector<T> getChannelTenantDetector() {
63+
return channelTenantDetector;
64+
}
65+
}

0 commit comments

Comments
 (0)