From 167a697fceaf9b1c01fdcfb2690f60346d0898ed Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Wed, 30 Aug 2023 18:19:30 +0200 Subject: [PATCH] [KOGITO-9650] Adding identity to SWF workflow --- .../src/main/resources/expression.sw.json | 9 ++++ .../quarkus/workflows/ExpressionRestIT.java | 1 + .../QuarkusKogitoProcessContextResolver.java | 48 +++++++++++++++++++ ...tils.KogitoProcessContextResolverExtension | 1 + 4 files changed, 59 insertions(+) create mode 100644 quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow/src/main/java/org/kie/kogito/serverless/workflow/QuarkusKogitoProcessContextResolver.java create mode 100644 quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow/src/main/resources/META-INF/services/org.kie.kogito.serverless.workflow.utils.KogitoProcessContextResolverExtension diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/expression.sw.json b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/expression.sw.json index ee4ec2d1f94..b738e603ea6 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/expression.sw.json +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/expression.sw.json @@ -36,6 +36,11 @@ "type": "expression", "operation": ".message |=.+\" and in my native language dog is translated to \"+$CONST.dog.castellano" }, + { + "name": "userMessage", + "type": "expression", + "operation": ".user |= $WORKFLOW.identity" + }, { "name": "contextMessage", "type": "expression", @@ -96,6 +101,10 @@ "name": "costantMessageAction", "functionRef" : "constantMessage" }, + { + "name": "userMessageAction", + "functionRef" : "userMessage" + }, { "name": "contextMessageAction", "functionRef" : "contextMessage" diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ExpressionRestIT.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ExpressionRestIT.java index f05c7a4d7b0..d80b0ae6a82 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ExpressionRestIT.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ExpressionRestIT.java @@ -42,6 +42,7 @@ void testExpressionRest() { .body("workflowdata.result", is(4)) .body("workflowdata.number", nullValue()) .body("workflowdata.message", is("my name is javierito and in my native language dog is translated to perro and the header pepe is pepa")) + .body("workflowdata.user", is("anonymous")) .body("workflowdata.discardedResult", nullValue()); } diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow/src/main/java/org/kie/kogito/serverless/workflow/QuarkusKogitoProcessContextResolver.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow/src/main/java/org/kie/kogito/serverless/workflow/QuarkusKogitoProcessContextResolver.java new file mode 100644 index 00000000000..8e38f722be2 --- /dev/null +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow/src/main/java/org/kie/kogito/serverless/workflow/QuarkusKogitoProcessContextResolver.java @@ -0,0 +1,48 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.kie.kogito.serverless.workflow; + +import java.util.Map; +import java.util.function.Function; + +import org.kie.kogito.internal.process.runtime.KogitoProcessContext; +import org.kie.kogito.serverless.workflow.utils.KogitoProcessContextResolverExtension; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.quarkus.arc.Arc; +import io.quarkus.security.identity.SecurityIdentity; + +public class QuarkusKogitoProcessContextResolver implements KogitoProcessContextResolverExtension { + + private static final Logger logger = LoggerFactory.getLogger(QuarkusKogitoProcessContextResolver.class); + + @Override + public Map> getKogitoProcessContextResolver() { + return Map.of("identity", this::resolveInitiator); + } + + private String resolveInitiator(KogitoProcessContext context) { + try { + SecurityIdentity identity = Arc.container().select(SecurityIdentity.class).get(); + return identity.isAnonymous() ? "anonymous" : identity.getPrincipal().getName(); + } catch (RuntimeException ex) { + logger.warn("Unable to resolve quarkus user identity", ex); + return null; + } + } + +} diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow/src/main/resources/META-INF/services/org.kie.kogito.serverless.workflow.utils.KogitoProcessContextResolverExtension b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow/src/main/resources/META-INF/services/org.kie.kogito.serverless.workflow.utils.KogitoProcessContextResolverExtension new file mode 100644 index 00000000000..d00e17ab303 --- /dev/null +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow/src/main/resources/META-INF/services/org.kie.kogito.serverless.workflow.utils.KogitoProcessContextResolverExtension @@ -0,0 +1 @@ +org.kie.kogito.serverless.workflow.QuarkusKogitoProcessContextResolver \ No newline at end of file