From 6b4c73294e09fdc8c6ef7b387ad175cdd782bfe5 Mon Sep 17 00:00:00 2001 From: Cristiano Nicolai <570894+cristianonicolai@users.noreply.github.com> Date: Fri, 21 Jul 2023 00:51:07 -0300 Subject: [PATCH] KOGITO-9611 - Track user identity in Kogito event --- .../test/java/org/kie/kogito/index/TestUtils.java | 4 ++-- .../index/addon/DataIndexEventPublisherTest.java | 2 +- .../kogito/jobs/service/events/JobDataEvent.java | 13 ++++++++++--- .../embedded/stream/EventPublisherJobStreams.java | 15 +++++++++++---- .../stream/EventPublisherJobStreamsTest.java | 7 ++++++- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/data-index/data-index-common/src/test/java/org/kie/kogito/index/TestUtils.java b/data-index/data-index-common/src/test/java/org/kie/kogito/index/TestUtils.java index 103b0568fa..b98819762e 100644 --- a/data-index/data-index-common/src/test/java/org/kie/kogito/index/TestUtils.java +++ b/data-index/data-index-common/src/test/java/org/kie/kogito/index/TestUtils.java @@ -132,7 +132,7 @@ public static ProcessInstanceDataEvent getProcessCloudEvent(String processId, St .build() : null) .build(); - return new ProcessInstanceDataEvent(URI.create("http://localhost:8080/" + processId).toString(), "jobs-management,prometheus-monitoring,process-management", body.metaData(), body); + return new ProcessInstanceDataEvent(URI.create("http://localhost:8080/" + processId).toString(), "jobs-management,prometheus-monitoring,process-management", null, body.metaData(), body); } public static ProcessInstance getProcessInstance(String processId, String processInstanceId, Integer status, String rootProcessInstanceId, String rootProcessId) { @@ -229,7 +229,7 @@ public static UserTaskInstanceDataEvent getUserTaskCloudEvent(String taskId, Str .outputs(emptyMap()) .build(); - return new UserTaskInstanceDataEvent(URI.create("http://localhost:8080/" + processId).toString(), null, body.metaData(), body); + return new UserTaskInstanceDataEvent(URI.create("http://localhost:8080/" + processId).toString(), null, null, body.metaData(), body); } public static AttachmentEventBody getTaskAttachment(String id, String user, String name, String content) { diff --git a/data-index/kogito-addons-quarkus-data-index/kogito-addons-quarkus-data-index-common/runtime/src/test/java/org/kie/kogito/index/addon/DataIndexEventPublisherTest.java b/data-index/kogito-addons-quarkus-data-index/kogito-addons-quarkus-data-index-common/runtime/src/test/java/org/kie/kogito/index/addon/DataIndexEventPublisherTest.java index 7afb99e1f7..c5061a1db9 100644 --- a/data-index/kogito-addons-quarkus-data-index/kogito-addons-quarkus-data-index-common/runtime/src/test/java/org/kie/kogito/index/addon/DataIndexEventPublisherTest.java +++ b/data-index/kogito-addons-quarkus-data-index/kogito-addons-quarkus-data-index-common/runtime/src/test/java/org/kie/kogito/index/addon/DataIndexEventPublisherTest.java @@ -130,7 +130,7 @@ public TestingDataEvent(String type, String kogitoProcessId, String kogitoRootProcessId) { super(type, source, data, kogitoProcessInstanceId, kogitoRootProcessInstanceId, kogitoProcessId, - kogitoRootProcessId, null); + kogitoRootProcessId, null, null); } } diff --git a/jobs-service/jobs-service-common/src/main/java/org/kie/kogito/jobs/service/events/JobDataEvent.java b/jobs-service/jobs-service-common/src/main/java/org/kie/kogito/jobs/service/events/JobDataEvent.java index 3cf1fcbdcb..5000e29f21 100644 --- a/jobs-service/jobs-service-common/src/main/java/org/kie/kogito/jobs/service/events/JobDataEvent.java +++ b/jobs-service/jobs-service-common/src/main/java/org/kie/kogito/jobs/service/events/JobDataEvent.java @@ -28,7 +28,7 @@ public class JobDataEvent extends AbstractDataEvent { public static final String JOB_EVENT_TYPE = "JobEvent"; - public JobDataEvent(String source, ScheduledJob data) { + public JobDataEvent(String source, String identity, ScheduledJob data) { super(JOB_EVENT_TYPE, source, data, @@ -36,7 +36,8 @@ public JobDataEvent(String source, ScheduledJob data) { data.getRootProcessInstanceId(), data.getProcessId(), data.getRootProcessId(), - null); + null, + identity); } @JsonIgnore @@ -49,19 +50,25 @@ public static class JobDataEventBuilder { private String source; private ScheduledJob data; + private String identity; public JobDataEventBuilder source(String source) { this.source = source; return this; } + public JobDataEventBuilder identity(String identity) { + this.identity = identity; + return this; + } + public JobDataEventBuilder data(ScheduledJob data) { this.data = data; return this; } public JobDataEvent build() { - return new JobDataEvent(source, data); + return new JobDataEvent(source, identity, data); } } } diff --git a/jobs-service/kogito-addons-jobs-service/kogito-addons-quarkus-jobs-service-embedded/runtime/src/main/java/org/kie/kogito/addons/quarkus/jobs/service/embedded/stream/EventPublisherJobStreams.java b/jobs-service/kogito-addons-jobs-service/kogito-addons-quarkus-jobs-service-embedded/runtime/src/main/java/org/kie/kogito/addons/quarkus/jobs/service/embedded/stream/EventPublisherJobStreams.java index dc848c0fd6..14a9201c1e 100644 --- a/jobs-service/kogito-addons-jobs-service/kogito-addons-quarkus-jobs-service-embedded/runtime/src/main/java/org/kie/kogito/addons/quarkus/jobs/service/embedded/stream/EventPublisherJobStreams.java +++ b/jobs-service/kogito-addons-jobs-service/kogito-addons-quarkus-jobs-service-embedded/runtime/src/main/java/org/kie/kogito/addons/quarkus/jobs/service/embedded/stream/EventPublisherJobStreams.java @@ -36,6 +36,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; +import io.quarkus.security.identity.SecurityIdentity; import io.smallrye.reactive.messaging.annotations.Blocking; import static org.kie.kogito.jobs.service.events.JobDataEvent.JOB_EVENT_TYPE; @@ -57,16 +58,20 @@ public class EventPublisherJobStreams { private final ObjectMapper objectMapper; + private final SecurityIdentity identity; + @Inject public EventPublisherJobStreams(@ConfigProperty(name = "kogito.service.url", defaultValue = "http://localhost:8080") String url, Instance eventPublishers, - ObjectMapper objectMapper) { + ObjectMapper objectMapper, + SecurityIdentity identity) { this.url = url; eventPublisher = eventPublishers.stream() .filter(publisher -> publisher.getClass().getName().startsWith(DATA_INDEX_EVENT_PUBLISHER)) .findFirst() .orElse(null); this.objectMapper = objectMapper; + this.identity = identity; } @Incoming(AvailableStreams.JOB_STATUS_CHANGE_EVENTS) @@ -87,7 +92,8 @@ public void onJobStatusChange(JobDetails jobDetails) { scheduledJob.getProcessInstanceId(), scheduledJob.getRootProcessInstanceId(), scheduledJob.getProcessId(), - scheduledJob.getRootProcessId()); + scheduledJob.getRootProcessId(), + identity.isAnonymous() ? null : identity.getPrincipal().getName()); try { eventPublisher.publish(event); } catch (Exception e) { @@ -103,9 +109,10 @@ public EventPublisherJobDataEvent(String type, String kogitoProcessInstanceId, String kogitoRootProcessInstanceId, String kogitoProcessId, - String kogitoRootProcessId) { + String kogitoRootProcessId, + String kogitoIdentity) { super(type, source, data, kogitoProcessInstanceId, kogitoRootProcessInstanceId, kogitoProcessId, - kogitoRootProcessId, null); + kogitoRootProcessId, null, kogitoIdentity); } } } diff --git a/jobs-service/kogito-addons-jobs-service/kogito-addons-quarkus-jobs-service-embedded/runtime/src/test/java/org/kie/kogito/addons/quarkus/jobs/service/embedded/stream/EventPublisherJobStreamsTest.java b/jobs-service/kogito-addons-jobs-service/kogito-addons-quarkus-jobs-service-embedded/runtime/src/test/java/org/kie/kogito/addons/quarkus/jobs/service/embedded/stream/EventPublisherJobStreamsTest.java index 361aa9932e..d172f4f8f3 100644 --- a/jobs-service/kogito-addons-jobs-service/kogito-addons-quarkus-jobs-service-embedded/runtime/src/test/java/org/kie/kogito/addons/quarkus/jobs/service/embedded/stream/EventPublisherJobStreamsTest.java +++ b/jobs-service/kogito-addons-jobs-service/kogito-addons-quarkus-jobs-service-embedded/runtime/src/test/java/org/kie/kogito/addons/quarkus/jobs/service/embedded/stream/EventPublisherJobStreamsTest.java @@ -40,6 +40,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import io.quarkus.security.identity.SecurityIdentity; + import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.anyCollection; import static org.mockito.Mockito.doReturn; @@ -47,6 +49,7 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; class EventPublisherJobStreamsTest { @@ -94,7 +97,9 @@ void onJobStatusChange() throws Exception { .registerModule(new JavaTimeModule()) .disable(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - EventPublisherJobStreams eventPublisherJobStreams = new EventPublisherJobStreams(URL, eventPublisherInstance, objectMapper); + SecurityIdentity identity = mock(SecurityIdentity.class); + when(identity.isAnonymous()).thenReturn(true); + EventPublisherJobStreams eventPublisherJobStreams = new EventPublisherJobStreams(URL, eventPublisherInstance, objectMapper, identity); JobDetails jobDetails = buildJobDetails(); eventPublisherJobStreams.onJobStatusChange(jobDetails);