From bc3eeefa5115f790d86ca8fcf5e6a27669381e91 Mon Sep 17 00:00:00 2001 From: melloware Date: Thu, 26 Sep 2024 13:38:28 -0400 Subject: [PATCH] Fix #107: Handle environment variable connection prop --- .../ROOT/pages/includes/quarkus-temporal.adoc | 4 +- .../TemporalConfigRelocateInterceptor.java | 101 ++++++++++++------ .../temporal/WorkflowClientRecorder.java | 4 +- 3 files changed, 71 insertions(+), 38 deletions(-) diff --git a/docs/modules/ROOT/pages/includes/quarkus-temporal.adoc b/docs/modules/ROOT/pages/includes/quarkus-temporal.adoc index 091e157..b0f7307 100644 --- a/docs/modules/ROOT/pages/includes/quarkus-temporal.adoc +++ b/docs/modules/ROOT/pages/includes/quarkus-temporal.adoc @@ -899,7 +899,7 @@ endif::add-copy-button-to-env-var[] ifndef::add-copy-button-to-env-var[] Environment variable: `+++QUARKUS_TEMPORAL_CONNECTION_MTLS_CLIENT_CERT_PATH+++` endif::add-copy-button-to-env-var[] ---|string +--|path | @@ -916,7 +916,7 @@ endif::add-copy-button-to-env-var[] ifndef::add-copy-button-to-env-var[] Environment variable: `+++QUARKUS_TEMPORAL_CONNECTION_MTLS_CLIENT_KEY_PATH+++` endif::add-copy-button-to-env-var[] ---|string +--|path | diff --git a/extension/runtime/src/main/java/io/quarkiverse/temporal/TemporalConfigRelocateInterceptor.java b/extension/runtime/src/main/java/io/quarkiverse/temporal/TemporalConfigRelocateInterceptor.java index 4b81d09..7807a15 100644 --- a/extension/runtime/src/main/java/io/quarkiverse/temporal/TemporalConfigRelocateInterceptor.java +++ b/extension/runtime/src/main/java/io/quarkiverse/temporal/TemporalConfigRelocateInterceptor.java @@ -13,46 +13,46 @@ public class TemporalConfigRelocateInterceptor implements ConfigSourceIntercepto @Override public ConfigValue getValue(ConfigSourceInterceptorContext context, String name) { - if (name.equals("quarkus.grpc.clients.temporal-client.host")) { - - ConfigValue host = context.proceed("quarkus.grpc.clients.temporal-client.host"); - ConfigValue target = context.proceed("quarkus.temporal.connection.target"); - if (host == null && target != null) { - String[] split = target.getValue().split(":"); - return target.from() - .withName("quarkus.grpc.clients.temporal-client.host") - .withValue(split[0]) - .build(); - } - return host; + ConfigValue target = context.proceed("quarkus.temporal.connection.target"); + if (target == null) { + return context.proceed(name); } + String[] connection = processConnectString(target.getValue()); - if (name.equals("quarkus.grpc.clients.temporal-client.port")) { + switch (name) { + case "quarkus.grpc.clients.temporal-client.host": { - ConfigValue port = context.proceed("quarkus.grpc.clients.temporal-client.port"); - ConfigValue target = context.proceed("quarkus.temporal.connection.target"); - if (port == null && target != null) { - String[] split = target.getValue().split(":"); - return target.from() - .withName("quarkus.grpc.clients.temporal-client.port") - .withValue(split[1]) - .build(); + ConfigValue host = context.proceed("quarkus.grpc.clients.temporal-client.host"); + if (host == null && connection[0] != null) { + return target.from() + .withName("quarkus.grpc.clients.temporal-client.host") + .withValue(connection[0]) + .build(); + } + return host; } - return port; - } + case "quarkus.grpc.clients.temporal-client.port": { - if (name.equals("quarkus.grpc.clients.temporal-client.test-port")) { + ConfigValue port = context.proceed("quarkus.grpc.clients.temporal-client.port"); + if (port == null && connection[1] != null) { + return target.from() + .withName("quarkus.grpc.clients.temporal-client.port") + .withValue(connection[1]) + .build(); + } + return port; + } + case "quarkus.grpc.clients.temporal-client.test-port": { - ConfigValue port = context.proceed("quarkus.grpc.clients.temporal-client.test-port"); - ConfigValue target = context.proceed("quarkus.temporal.connection.target"); - if (port == null && target != null) { - String[] split = target.getValue().split(":"); - return target.from() - .withName("quarkus.grpc.clients.temporal-client.test-port") - .withValue(split[1]) - .build(); + ConfigValue port = context.proceed("quarkus.grpc.clients.temporal-client.test-port"); + if (port == null && connection[1] != null) { + return target.from() + .withName("quarkus.grpc.clients.temporal-client.test-port") + .withValue(connection[1]) + .build(); + } + return port; } - return port; } return context.proceed(name); @@ -71,4 +71,37 @@ public Iterator iterateNames(ConfigSourceInterceptorContext context) { return names.iterator(); } -} + /** + * Processes a connection string and extracts the host and port. + * The input can either be in the format "${PLACEHOLDER:host:port}" or "host:port". + * If the input contains a placeholder in the form of "${...}", the method removes it + * and returns the host and port. If the port is not provided, the port will be an empty string. + * + * @param input the connection string which may include a placeholder in the form "${...}" or just "host:port". + * @return a String array where the first element is the host and the second element is the port. If the port is + * not provided, the second element will be an empty string. + * @throws IllegalArgumentException if the input is null or empty. + */ + private static String[] processConnectString(String input) { + if (input == null || input.isEmpty()) { + throw new IllegalArgumentException("'quarkus.temporal.connection.target' cannot be null or empty"); + } + + // Check if the string starts with "${" and contains a "}" + input = input.replaceAll("^\\$\\{", ""); + input = input.replaceAll("}", ""); + + // Split by colon + String[] parts = input.split(":"); + + // Validate that there are at least two parts (host and port) + if (parts.length >= 2) { + String host = parts[parts.length - 2]; + String port = parts[parts.length - 1]; + // Return the host and port as a string array + return new String[] { host, port }; + } else { + throw new IllegalArgumentException("'quarkus.temporal.connection.target' is not in host:port format."); + } + } +} \ No newline at end of file diff --git a/extension/runtime/src/main/java/io/quarkiverse/temporal/WorkflowClientRecorder.java b/extension/runtime/src/main/java/io/quarkiverse/temporal/WorkflowClientRecorder.java index cda2b65..d0c3345 100644 --- a/extension/runtime/src/main/java/io/quarkiverse/temporal/WorkflowClientRecorder.java +++ b/extension/runtime/src/main/java/io/quarkiverse/temporal/WorkflowClientRecorder.java @@ -51,7 +51,7 @@ public WorkflowClientRecorder(TemporalRuntimeConfig runtimeConfig, TemporalBuild /** * Creates an instance of {@link WorkflowClientOptions} based on the provided propagators and telemetry settings. * - * @param context + * @param context the workflow Synthetic Creation Context * @return A configured {@link WorkflowClientOptions} instance. */ public WorkflowClientOptions createWorkflowClientOptions( @@ -99,4 +99,4 @@ public Function, WorkflowClient> crea createWorkflowClientOptions(context)); } -} +} \ No newline at end of file