From f616724929c721f17c8a3c24a34458a87e857abb Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Tue, 10 Nov 2026 17:42:23 -0800 Subject: [PATCH 1/6] Use native Nexus serialization context for async sample results --- .../nexusserializationcontext/README.md | 35 +++++++------ .../codec/AesGcmCodec.java | 8 +-- .../CompressedEncryptedHandlerWorker.java | 15 +----- .../handler/EncryptedHandlerWorker.java | 15 +----- .../NexusEndpointContextPropagator.java | 51 ------------------- .../propagation/NexusEndpointInterceptor.java | 36 ------------- .../codec/NexusCodecTest.java | 50 ------------------ 7 files changed, 23 insertions(+), 187 deletions(-) delete mode 100644 core/src/main/java/io/temporal/samples/nexusserializationcontext/propagation/NexusEndpointContextPropagator.java delete mode 100644 core/src/main/java/io/temporal/samples/nexusserializationcontext/propagation/NexusEndpointInterceptor.java diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md index 32fc41ba..4c4f88fd 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md @@ -36,32 +36,31 @@ The caller workflow's input and final result use `encryption-key-id: key-c`; the have endpoint metadata. The starter uses the same converter as the caller worker, so it can decode the final result before printing it. -`NexusSerializationContext` works end to end for synchronous Nexus operations. -For an asynchronous operation, the handler's final result is serialized as a workflow -result and does not receive `NexusSerializationContext`. We plan to add -`NexusSerializationContext` support for asynchronous operation results in the Java SDK. -Until then, the sample's `NexusEndpointInterceptor` captures the endpoint when the -handler starts the backing `EchoWorkflow`. `NexusEndpointContextPropagator` saves it in -the workflow headers and restores it on the workflow thread. Each handler's codec uses -its fixed key to encrypt the workflow result and includes the propagated endpoint name -in the outer payload metadata. This propagation is needed for the endpoint metadata; -the handler already knows which key to use. When the result reaches the caller, the SDK -supplies `NexusSerializationContext` to decode it. - -This endpoint propagation covers asynchronous operations backed by workflows. If the -endpoint is not propagated, the result remains encrypted with the handler's fixed key -but lacks the endpoint name in its metadata. +For synchronous operations, the SDK supplies `NexusSerializationContext` when decoding +inputs and encoding results. For asynchronous operations backed by `EchoWorkflow`, the +handler's SDK sends the context when starting the workflow, and the server stores it. +The workflow worker receives that context and uses it to encode the workflow's result +for the Nexus caller. Other workflow payloads keep their normal workflow context. + +The sample's `AesGcmCodec.withContext` reads the endpoint directly from this context +and includes it in the encrypted result's metadata. No custom interceptor or workflow +header propagation is needed. The SDK-side codec chooses the key; the server carries +the context without access to encryption keys. The hard-coded keys are only for this local example. For production encryption, use a secure key store, as in the [AWS Encryption SDK sample](../keymanagementencryption/awsencryptionsdk/README.md). -Requires Java SDK 1.40.0 or later and Temporal Server 1.30.0 or later with Nexus enabled -so the handler can read the endpoint name. +## Prerequisites + +Use a Temporal Server that supports Nexus serialization-context propagation for +workflow-backed asynchronous operations. For local development, download +[Temporal CLI `v1.9.3-nexus-serialization-context`](https://github.com/temporalio/cli/releases/tag/v1.9.3-nexus-serialization-context) +and add its `temporal` binary to your `PATH`. Its dev server includes this support. ## Run locally -Start a Temporal dev server: +Start the dev server: ```bash temporal server start-dev diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/codec/AesGcmCodec.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/codec/AesGcmCodec.java index 0208849a..f13899b5 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/codec/AesGcmCodec.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/codec/AesGcmCodec.java @@ -9,7 +9,6 @@ import io.temporal.payload.context.NexusSerializationContext; import io.temporal.payload.context.SerializationContext; import io.temporal.samples.nexusserializationcontext.SampleConfig; -import io.temporal.samples.nexusserializationcontext.propagation.NexusEndpointContextPropagator; import java.nio.ByteBuffer; import java.security.GeneralSecurityException; import java.security.SecureRandom; @@ -19,7 +18,6 @@ import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.GCMParameterSpec; -import org.apache.commons.lang.StringUtils; /** Encrypts payloads with a configured key and includes endpoint metadata when available. */ public final class AesGcmCodec implements PayloadCodec { @@ -58,8 +56,6 @@ public PayloadCodec withContext(@Nonnull SerializationContext context) { @Override @Nonnull public List encode(@Nonnull List payloads) { - String endpointName = - StringUtils.defaultIfBlank(endpoint, NexusEndpointContextPropagator.currentEndpoint()); List encoded = new ArrayList<>(payloads.size()); for (Payload payload : payloads) { Payload.Builder encrypted = @@ -69,9 +65,9 @@ public List encode(@Nonnull List payloads) { ByteString.copyFromUtf8(NexusEncoding.AES_GCM.encodingName())) .putMetadata(KEY_ID_METADATA_KEY, ByteString.copyFromUtf8(keyId)) .setData(ByteString.copyFrom(encrypt(payload.toByteArray()))); - if (StringUtils.isNotBlank(endpointName)) { + if (endpoint != null && !endpoint.isEmpty()) { encrypted.putMetadata( - SampleConfig.ENDPOINT_METADATA_KEY, ByteString.copyFromUtf8(endpointName)); + SampleConfig.ENDPOINT_METADATA_KEY, ByteString.copyFromUtf8(endpoint)); } encoded.add(encrypted.build()); } diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java index 9640b0f2..818f3be5 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java @@ -10,11 +10,8 @@ import io.temporal.samples.nexusserializationcontext.SampleConfig; import io.temporal.samples.nexusserializationcontext.codec.AesGcmCodec; import io.temporal.samples.nexusserializationcontext.codec.ZlibCodec; -import io.temporal.samples.nexusserializationcontext.propagation.NexusEndpointContextPropagator; -import io.temporal.samples.nexusserializationcontext.propagation.NexusEndpointInterceptor; import io.temporal.worker.Worker; import io.temporal.worker.WorkerFactory; -import io.temporal.worker.WorkerFactoryOptions; import java.nio.charset.StandardCharsets; import java.util.List; import javax.crypto.SecretKey; @@ -26,16 +23,8 @@ public class CompressedEncryptedHandlerWorker { public static void main(String[] args) { WorkflowClient client = ClientOptions.getWorkflowClient( - args, - WorkflowClientOptions.newBuilder() - .setDataConverter(dataConverter()) - .setContextPropagators(List.of(new NexusEndpointContextPropagator()))); - WorkerFactory factory = - WorkerFactory.newInstance( - client, - WorkerFactoryOptions.newBuilder() - .setWorkerInterceptors(new NexusEndpointInterceptor()) - .build()); + args, WorkflowClientOptions.newBuilder().setDataConverter(dataConverter())); + WorkerFactory factory = WorkerFactory.newInstance(client); Worker worker = factory.newWorker(TASK_QUEUE); worker.registerWorkflowImplementationTypes(EchoWorkflowImpl.class); worker.registerNexusServiceImplementation(new SyncEchoServiceImpl()); diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java index 85525aac..ea954ded 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java @@ -8,11 +8,8 @@ import io.temporal.samples.nexus.options.ClientOptions; import io.temporal.samples.nexusserializationcontext.SampleConfig; import io.temporal.samples.nexusserializationcontext.codec.AesGcmCodec; -import io.temporal.samples.nexusserializationcontext.propagation.NexusEndpointContextPropagator; -import io.temporal.samples.nexusserializationcontext.propagation.NexusEndpointInterceptor; import io.temporal.worker.Worker; import io.temporal.worker.WorkerFactory; -import io.temporal.worker.WorkerFactoryOptions; import java.nio.charset.StandardCharsets; import java.util.List; import javax.crypto.SecretKey; @@ -24,16 +21,8 @@ public class EncryptedHandlerWorker { public static void main(String[] args) { WorkflowClient client = ClientOptions.getWorkflowClient( - args, - WorkflowClientOptions.newBuilder() - .setDataConverter(dataConverter()) - .setContextPropagators(List.of(new NexusEndpointContextPropagator()))); - WorkerFactory factory = - WorkerFactory.newInstance( - client, - WorkerFactoryOptions.newBuilder() - .setWorkerInterceptors(new NexusEndpointInterceptor()) - .build()); + args, WorkflowClientOptions.newBuilder().setDataConverter(dataConverter())); + WorkerFactory factory = WorkerFactory.newInstance(client); Worker worker = factory.newWorker(TASK_QUEUE); worker.registerWorkflowImplementationTypes(EchoWorkflowImpl.class); worker.registerNexusServiceImplementation(new SyncEchoServiceImpl()); diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/propagation/NexusEndpointContextPropagator.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/propagation/NexusEndpointContextPropagator.java deleted file mode 100644 index 394275f1..00000000 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/propagation/NexusEndpointContextPropagator.java +++ /dev/null @@ -1,51 +0,0 @@ -package io.temporal.samples.nexusserializationcontext.propagation; - -import io.temporal.api.common.v1.Payload; -import io.temporal.common.context.ContextPropagator; -import io.temporal.common.converter.DataConverter; -import io.temporal.samples.nexusserializationcontext.SampleConfig; -import java.util.Map; -import org.slf4j.MDC; - -/** Carries the Nexus endpoint from a handler into the workflow it starts. */ -public final class NexusEndpointContextPropagator implements ContextPropagator { - private static final DataConverter CONVERTER = DataConverter.getDefaultInstance(); - - public static String currentEndpoint() { - return MDC.get(SampleConfig.ENDPOINT_METADATA_KEY); - } - - @Override - public String getName() { - return NexusEndpointContextPropagator.class.getName(); - } - - @Override - public Object getCurrentContext() { - return currentEndpoint(); - } - - @Override - public void setCurrentContext(Object context) { - if (context == null) { - MDC.remove(SampleConfig.ENDPOINT_METADATA_KEY); - } else { - MDC.put(SampleConfig.ENDPOINT_METADATA_KEY, (String) context); - } - } - - @Override - public Map serializeContext(Object context) { - if (context == null) { - return Map.of(); - } - return Map.of( - SampleConfig.ENDPOINT_METADATA_KEY, CONVERTER.toPayload((String) context).orElseThrow()); - } - - @Override - public Object deserializeContext(Map header) { - Payload payload = header.get(SampleConfig.ENDPOINT_METADATA_KEY); - return payload == null ? null : CONVERTER.fromPayload(payload, String.class, String.class); - } -} diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/propagation/NexusEndpointInterceptor.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/propagation/NexusEndpointInterceptor.java deleted file mode 100644 index acf05000..00000000 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/propagation/NexusEndpointInterceptor.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.temporal.samples.nexusserializationcontext.propagation; - -import io.nexusrpc.OperationException; -import io.nexusrpc.handler.OperationContext; -import io.temporal.common.interceptors.NexusOperationInboundCallsInterceptor; -import io.temporal.common.interceptors.NexusOperationInboundCallsInterceptorBase; -import io.temporal.common.interceptors.WorkerInterceptorBase; -import io.temporal.nexus.Nexus; -import io.temporal.samples.nexusserializationcontext.SampleConfig; -import org.slf4j.MDC; - -/** Makes the endpoint available while a Nexus handler starts its backing workflow. */ -public final class NexusEndpointInterceptor extends WorkerInterceptorBase { - @Override - public NexusOperationInboundCallsInterceptor interceptNexusOperation( - OperationContext context, NexusOperationInboundCallsInterceptor next) { - return new NexusOperationInboundCallsInterceptorBase(next) { - @Override - public StartOperationOutput startOperation(StartOperationInput input) - throws OperationException { - String previousEndpoint = NexusEndpointContextPropagator.currentEndpoint(); - String endpoint = Nexus.getOperationContext().getInfo().getEndpoint(); - try { - MDC.put(SampleConfig.ENDPOINT_METADATA_KEY, endpoint); - return super.startOperation(input); - } finally { - if (previousEndpoint == null) { - MDC.remove(SampleConfig.ENDPOINT_METADATA_KEY); - } else { - MDC.put(SampleConfig.ENDPOINT_METADATA_KEY, previousEndpoint); - } - } - } - }; - } -} diff --git a/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java b/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java index b5329952..82f271c0 100644 --- a/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java +++ b/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java @@ -17,7 +17,6 @@ import io.temporal.samples.nexusserializationcontext.caller.EndpointResults; import io.temporal.samples.nexusserializationcontext.handler.CompressedEncryptedHandlerWorker; import io.temporal.samples.nexusserializationcontext.handler.EncryptedHandlerWorker; -import io.temporal.samples.nexusserializationcontext.propagation.NexusEndpointContextPropagator; import io.temporal.samples.nexusserializationcontext.service.AsyncEchoService; import io.temporal.samples.nexusserializationcontext.service.SyncEchoService; import org.junit.jupiter.api.Test; @@ -182,55 +181,6 @@ void handlersDecodeAsyncRequestsWithTheirEndpointKey() { () -> handlerA.fromPayload(requestB, String.class, String.class)); } - @Test - void propagatedEndpointAppearsOnEncryptedAsyncWorkflowResult() { - NexusEndpointContextPropagator propagator = new NexusEndpointContextPropagator(); - try { - propagator.setCurrentContext(null); - DataConverter handlerA = - CompressedEncryptedHandlerWorker.dataConverter() - .withContext(new WorkflowSerializationContext("handler-a", "echo-workflow")); - DataConverter handlerB = - EncryptedHandlerWorker.dataConverter() - .withContext(new WorkflowSerializationContext("handler-b", "echo-workflow")); - - Payload resultA = - asyncResultFor( - propagator, - handlerA, - SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, - SampleConfig.KEY_A_ID); - Payload resultB = - asyncResultFor( - propagator, handlerB, SampleConfig.ENCRYPTED_ENDPOINT, SampleConfig.KEY_B_ID); - assertTrue(resultA.getData().size() < resultB.getData().size()); - } finally { - propagator.setCurrentContext(null); - } - } - - private static Payload asyncResultFor( - NexusEndpointContextPropagator propagator, - DataConverter handler, - String endpoint, - String keyId) { - propagator.setCurrentContext(endpoint); - var header = propagator.serializeContext(propagator.getCurrentContext()); - propagator.setCurrentContext(null); - propagator.setCurrentContext(propagator.deserializeContext(header)); - - String message = "reply ".repeat(100); - Payload result = handler.toPayload(message).orElseThrow(); - assertEquals( - endpoint, result.getMetadataOrThrow(SampleConfig.ENDPOINT_METADATA_KEY).toStringUtf8()); - assertEquals(keyId, result.getMetadataOrThrow(AesGcmCodec.KEY_ID_METADATA_KEY).toStringUtf8()); - assertEquals( - message, converterFor(endpoint, true).fromPayload(result, String.class, String.class)); - assertEquals(message, handler.fromPayload(result, String.class, String.class)); - propagator.setCurrentContext(null); - return result; - } - private static DataConverter converterFor(String endpoint) { return CallerWorker.dataConverter().withContext(contextFor(endpoint)); } From 215be9a8d4f5c589f816cb3ffed653e651c63800 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Tue, 10 Nov 2026 17:52:14 -0800 Subject: [PATCH 2/6] Test native Nexus context propagation in the encryption sample --- .../nexusserializationcontext/README.md | 13 ++ ...usSerializationContextIntegrationTest.java | 175 ++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 core/src/test/java/io/temporal/samples/nexusserializationcontext/NexusSerializationContextIntegrationTest.java diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md index 4c4f88fd..9294e01f 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md @@ -112,3 +112,16 @@ Encrypted endpoint sync result: Hello from Nexus Compressed and encrypted endpoint async result: Hello from Nexus Encrypted endpoint async result: Hello from Nexus ``` + +## Test + +Run the codec tests and integration test: + +```bash +./gradlew :core:test --tests 'io.temporal.samples.nexusserializationcontext.*' +``` + +The integration test downloads and starts the dev-server version listed above, +runs synchronous and workflow-backed asynchronous calls through both endpoints, +and checks the recorded Nexus context and encrypted async-result metadata in +workflow history. It stops the server and workers when finished. diff --git a/core/src/test/java/io/temporal/samples/nexusserializationcontext/NexusSerializationContextIntegrationTest.java b/core/src/test/java/io/temporal/samples/nexusserializationcontext/NexusSerializationContextIntegrationTest.java new file mode 100644 index 00000000..0447d313 --- /dev/null +++ b/core/src/test/java/io/temporal/samples/nexusserializationcontext/NexusSerializationContextIntegrationTest.java @@ -0,0 +1,175 @@ +package io.temporal.samples.nexusserializationcontext; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import io.temporal.api.common.v1.Payload; +import io.temporal.api.common.v1.WorkflowExecution; +import io.temporal.api.history.v1.WorkflowExecutionStartedEventAttributes; +import io.temporal.client.WorkflowClient; +import io.temporal.client.WorkflowClientOptions; +import io.temporal.client.WorkflowOptions; +import io.temporal.common.WorkflowExecutionHistory; +import io.temporal.common.converter.DataConverter; +import io.temporal.common.converter.EncodingKeys; +import io.temporal.samples.nexusserializationcontext.caller.CallerWorker; +import io.temporal.samples.nexusserializationcontext.caller.CallerWorkflow; +import io.temporal.samples.nexusserializationcontext.caller.CallerWorkflowImpl; +import io.temporal.samples.nexusserializationcontext.caller.EndpointResults; +import io.temporal.samples.nexusserializationcontext.handler.AsyncEchoServiceImpl; +import io.temporal.samples.nexusserializationcontext.handler.CompressedEncryptedHandlerWorker; +import io.temporal.samples.nexusserializationcontext.handler.EchoWorkflowImpl; +import io.temporal.samples.nexusserializationcontext.handler.EncryptedHandlerWorker; +import io.temporal.samples.nexusserializationcontext.handler.SyncEchoServiceImpl; +import io.temporal.samples.nexusserializationcontext.service.AsyncEchoService; +import io.temporal.testing.TemporalDevServerOptions; +import io.temporal.testing.TestEnvironmentOptions; +import io.temporal.testing.TestWorkflowEnvironment; +import io.temporal.worker.Worker; +import io.temporal.worker.WorkerFactory; +import java.time.Duration; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +class NexusSerializationContextIntegrationTest { + private static final String DEV_SERVER_VERSION = "v1.9.3-nexus-serialization-context"; + + @Test + @Timeout(120) + void asyncResultsUseTheEndpointContextRecordedByTheServer() throws InterruptedException { + try (TestWorkflowEnvironment environment = + TestWorkflowEnvironment.startLocal( + TestEnvironmentOptions.newBuilder() + .setWorkflowClientOptions( + WorkflowClientOptions.newBuilder() + .setDataConverter(CallerWorker.dataConverter()) + .build()) + .build(), + TemporalDevServerOptions.newBuilder().setDownloadVersion(DEV_SERVER_VERSION).build())) { + String callerQueue = "serialization-caller"; + String handlerAQueue = "serialization-handler-a"; + String handlerBQueue = "serialization-handler-b"; + WorkerFactory handlerA = + handlerFactory( + environment, handlerAQueue, CompressedEncryptedHandlerWorker.dataConverter()); + WorkerFactory handlerB = + handlerFactory(environment, handlerBQueue, EncryptedHandlerWorker.dataConverter()); + try { + environment.createNexusEndpoint(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, handlerAQueue); + environment.createNexusEndpoint(SampleConfig.ENCRYPTED_ENDPOINT, handlerBQueue); + environment + .newWorker(callerQueue) + .registerWorkflowImplementationTypes(CallerWorkflowImpl.class); + handlerA.start(); + handlerB.start(); + environment.start(); + + WorkflowClient client = environment.getWorkflowClient(); + CallerWorkflow caller = + client.newWorkflowStub( + CallerWorkflow.class, + WorkflowOptions.newBuilder() + .setTaskQueue(callerQueue) + .setWorkflowExecutionTimeout(Duration.ofSeconds(60)) + .build()); + String message = "Hello from Nexus"; + assertEquals( + new EndpointResults(message, message, message, message), + caller.echoThroughEndpoints(message)); + + Map expectedKeys = + Map.of( + SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, SampleConfig.KEY_A_ID, + SampleConfig.ENCRYPTED_ENDPOINT, SampleConfig.KEY_B_ID); + Set observedEndpoints = new HashSet<>(); + for (WorkflowExecution execution : backingWorkflows(environment)) { + WorkflowExecutionHistory history = + client.fetchHistory(execution.getWorkflowId(), execution.getRunId()); + WorkflowExecutionStartedEventAttributes started = + history.getEvents().get(0).getWorkflowExecutionStartedEventAttributes(); + assertTrue(started.hasPropagatedNexusSerializationContext()); + String endpoint = started.getPropagatedNexusSerializationContext().getEndpoint(); + assertTrue(expectedKeys.containsKey(endpoint), "Unexpected endpoint: " + endpoint); + assertTrue(observedEndpoints.add(endpoint), "Duplicate endpoint: " + endpoint); + assertEquals( + AsyncEchoService.SERVICE_NAME, + started.getPropagatedNexusSerializationContext().getService()); + assertEquals( + AsyncEchoService.ECHO_ASYNC_OPERATION_NAME, + started.getPropagatedNexusSerializationContext().getOperation()); + + // Native propagation uses the start-event field, not the sample's old custom header. + assertFalse(started.getHeader().containsFields(SampleConfig.ENDPOINT_METADATA_KEY)); + assertFalse( + started + .getInput() + .getPayloads(0) + .containsMetadata(SampleConfig.ENDPOINT_METADATA_KEY)); + assertTrue(history.getLastEvent().hasWorkflowExecutionCompletedEventAttributes()); + Payload result = + history + .getLastEvent() + .getWorkflowExecutionCompletedEventAttributes() + .getResult() + .getPayloads(0); + assertEquals( + "binary/nexus-aes-gcm", + result.getMetadataOrThrow(EncodingKeys.METADATA_ENCODING_KEY).toStringUtf8()); + assertEquals( + endpoint, + result.getMetadataOrThrow(SampleConfig.ENDPOINT_METADATA_KEY).toStringUtf8()); + assertEquals( + expectedKeys.get(endpoint), + result.getMetadataOrThrow("encryption-key-id").toStringUtf8()); + } + assertEquals(expectedKeys.keySet(), observedEndpoints); + } finally { + handlerA.shutdownNow(); + handlerB.shutdownNow(); + handlerA.awaitTermination(10, TimeUnit.SECONDS); + handlerB.awaitTermination(10, TimeUnit.SECONDS); + } + } + } + + private static WorkerFactory handlerFactory( + TestWorkflowEnvironment environment, String taskQueue, DataConverter converter) { + WorkflowClient client = + WorkflowClient.newInstance( + environment.getWorkflowServiceStubs(), + WorkflowClientOptions.newBuilder() + .setNamespace(environment.getNamespace()) + .setDataConverter(converter) + .build()); + WorkerFactory factory = WorkerFactory.newInstance(client); + Worker worker = factory.newWorker(taskQueue); + worker.registerWorkflowImplementationTypes(EchoWorkflowImpl.class); + worker.registerNexusServiceImplementation( + new SyncEchoServiceImpl(), new AsyncEchoServiceImpl()); + return factory; + } + + private static List backingWorkflows(TestWorkflowEnvironment environment) { + // Visibility is eventually consistent; wait for both backing workflows to be indexed. + long deadline = System.nanoTime() + Duration.ofSeconds(10).toNanos(); + do { + List executions; + try (var stream = + environment.getWorkflowClient().listExecutions("WorkflowType = 'EchoWorkflow'")) { + executions = stream.map(metadata -> metadata.getExecution()).collect(Collectors.toList()); + } + if (executions.size() == 2) { + return executions; + } + environment.sleep(Duration.ofMillis(100)); + } while (System.nanoTime() < deadline); + throw new AssertionError("Expected two backing workflows to appear in visibility"); + } +} From fbf43c28908cd8b1db4958320191f285c29677d1 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Fri, 13 Nov 2026 13:34:44 -0800 Subject: [PATCH 3/6] Demonstrate per-endpoint key selection in Nexus handlers Serve two endpoints per handler with distinct keys in one namespace. Keep sync and async calls explicit, simplify the sample codec tests, and remove the dev-server integration test. --- .../nexusserializationcontext/README.md | 159 ++++++++------- .../SampleConfig.java | 23 ++- .../caller/CallerStarter.java | 21 +- .../caller/CallerWorker.java | 46 +++-- .../caller/CallerWorkflowImpl.java | 63 ++++-- .../caller/EndpointResults.java | 12 +- .../codec/NexusCodec.java | 8 +- .../CompressedEncryptedHandlerWorker.java | 27 ++- .../handler/EncryptedHandlerWorker.java | 21 +- ...usSerializationContextIntegrationTest.java | 175 ---------------- .../codec/NexusCodecTest.java | 192 +++++------------- 11 files changed, 284 insertions(+), 463 deletions(-) delete mode 100644 core/src/test/java/io/temporal/samples/nexusserializationcontext/NexusSerializationContextIntegrationTest.java diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md index 9294e01f..b2edca44 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md @@ -1,51 +1,62 @@ # Nexus serialization context -This sample calls a synchronous and an asynchronous Nexus operation through each of -two endpoints. Each endpoint routes to its own handler worker, which registers both -`SyncEchoService` and `AsyncEchoService`. The caller's `NexusCodec` uses -`NexusSerializationContext` to select the `PayloadCodec` registered for each endpoint. -The sample uses these keys: - -- Key A: compresses with zlib, then encrypts synchronous and asynchronous payloads for `nexus-serialization-compressed-encrypted`. -- Key B: encrypts synchronous and asynchronous payloads for `nexus-serialization-encrypted`. -- Key C: encrypts the caller workflow's input and final result. - -The caller configures a codec for each endpoint and a separate codec for its own -workflow input and result. Each handler worker uses the fixed key for its endpoint. - -The caller schedules all four operations before waiting for their results, so each -result must be decoded using the context of its own endpoint. The caller's -`NexusCodec` uses Key C for its own workflow payloads, which have no Nexus endpoint. - -For both Nexus endpoints, the outer encrypted payload stores the endpoint name in -`nexus-endpoint-name` metadata alongside `binary/nexus-aes-gcm` and a sample key ID -(`key-a` or `key-b`). A Codec Server can use the endpoint name to select the matching -key and decompression chain without SDK context. - -For the compressed endpoint, the outer payload's decoded metadata looks like: +This sample uses `NexusSerializationContext` to select an encryption key for each Nexus +endpoint. The caller invokes synchronous and workflow-backed asynchronous operations +through four endpoints. Handler A (`CompressedEncryptedHandlerWorker`) compresses and +then encrypts Nexus payloads. Handler B (`EncryptedHandlerWorker`) encrypts without +compression. Each handler selects between its own key 1 and key 2 using +the endpoint in the Nexus context. + +| Endpoint | Encryption key | Nexus payload encoding | +|---|---|---| +| `handler-a-1` | `HANDLER_A_KEY_1` | zlib, then AES-GCM | +| `handler-b-1` | `HANDLER_B_KEY_1` | AES-GCM | +| `handler-a-2` | `HANDLER_A_KEY_2` | zlib, then AES-GCM | +| `handler-b-2` | `HANDLER_B_KEY_2` | AES-GCM | + +These are four distinct 128-bit AES keys. Compression runs before encryption; +decoding decrypts before decompressing. AES-GCM authenticates the ciphertext. +The AES-GCM and zlib codecs are implemented in this sample; Nexus supplies the endpoint +context used to select them. + +Each worker configures `NexusCodec` with an endpoint-to-codec map and a default codec +for ordinary workflow payloads. The sample reuses key 1 for workflow payloads +to keep the example to four keys: + +| Ordinary workflow payload | Encryption key | +|---|---| +| Caller workflow input and final result | `HANDLER_A_KEY_1` | +| Handler A backing workflow inputs | `HANDLER_A_KEY_1` | +| Handler B backing workflow inputs | `HANDLER_B_KEY_1` | + +These workflow payloads use AES-GCM without compression or endpoint metadata. +An async Nexus result uses the key and compression chain selected by its endpoint, +even when that differs from the backing workflow's input encryption. + +For synchronous operations, the SDK supplies Nexus context when decoding inputs and +encoding results. When an asynchronous handler starts `EchoWorkflow`, the SDK includes +the Nexus endpoint, service, and operation in the workflow-start request. The server +stores these fields in the `WorkflowExecutionStarted` history event. The worker SDK +restores the Nexus context from that event when encoding the workflow's result, so +`NexusCodec` can select the endpoint's codec and key from the worker's local configuration. +The workflow's input still uses ordinary workflow context; this sample's default codec +encrypts it with that handler's key 1. + +The caller starts all eight operations before waiting for their results. Each operation +keeps its own endpoint context, even when operations for different keys are in flight. +No custom interceptor or workflow-header propagation is required. + +The outer encrypted payload includes `encoding`, `encryption-key-id`, and, for Nexus +payloads, `nexus-endpoint-name` metadata. For example: ```text encoding: binary/nexus-aes-gcm -encryption-key-id: key-a -nexus-endpoint-name: nexus-serialization-compressed-encrypted +encryption-key-id: handler-a-1 +nexus-endpoint-name: handler-a-1 ``` -The asynchronous results have `encryption-key-id: key-a` or `key-b` and their respective -endpoint names in the outer payload metadata. -The caller workflow's input and final result use `encryption-key-id: key-c`; they do not -have endpoint metadata. The starter uses the same converter as the caller worker, so it -can decode the final result before printing it. - -For synchronous operations, the SDK supplies `NexusSerializationContext` when decoding -inputs and encoding results. For asynchronous operations backed by `EchoWorkflow`, the -handler's SDK sends the context when starting the workflow, and the server stores it. -The workflow worker receives that context and uses it to encode the workflow's result -for the Nexus caller. Other workflow payloads keep their normal workflow context. - -The sample's `AesGcmCodec.withContext` reads the endpoint directly from this context -and includes it in the encrypted result's metadata. No custom interceptor or workflow -header propagation is needed. The SDK-side codec chooses the key; the server carries -the context without access to encryption keys. +A Codec Server can use that metadata to select the key and decompression chain without +SDK context. Ordinary workflow payloads have a key ID but no endpoint metadata or compression. The hard-coded keys are only for this local example. For production encryption, use a secure key store, as in the @@ -55,73 +66,71 @@ use a secure key store, as in the Use a Temporal Server that supports Nexus serialization-context propagation for workflow-backed asynchronous operations. For local development, download -[Temporal CLI `v1.9.3-nexus-serialization-context`](https://github.com/temporalio/cli/releases/tag/v1.9.3-nexus-serialization-context) -and add its `temporal` binary to your `PATH`. Its dev server includes this support. +[Temporal CLI `v1.9.3-nexus-serialization-context`](https://github.com/temporalio/cli/releases/tag/v1.9.3-nexus-serialization-context). +Keep this prerelease binary separate from your stable CLI. The commands below use +`./temporal` to run the downloaded binary from the repository root. ## Run locally +The caller and both handlers share the `nexus-serialization` namespace. Each worker +polls its own task queue; both endpoints for a handler target that handler's queue. + Start the dev server: ```bash -temporal server start-dev +./temporal server start-dev --namespace nexus-serialization ``` -In another terminal, create the namespaces and endpoints: +Create two endpoints for each handler worker: ```bash -temporal operator namespace create --namespace nexus-serialization-caller -temporal operator namespace create --namespace nexus-serialization-key-a-handler -temporal operator namespace create --namespace nexus-serialization-key-b-handler -temporal operator nexus endpoint create \ - --name nexus-serialization-compressed-encrypted \ - --target-namespace nexus-serialization-key-a-handler \ - --target-task-queue nexus-serialization-key-a-handler -temporal operator nexus endpoint create \ - --name nexus-serialization-encrypted \ - --target-namespace nexus-serialization-key-b-handler \ - --target-task-queue nexus-serialization-key-b-handler +./temporal operator nexus endpoint create \ + --name handler-a-1 \ + --target-namespace nexus-serialization \ + --target-task-queue nexus-serialization-handler-a +./temporal operator nexus endpoint create \ + --name handler-a-2 \ + --target-namespace nexus-serialization \ + --target-task-queue nexus-serialization-handler-a +./temporal operator nexus endpoint create \ + --name handler-b-1 \ + --target-namespace nexus-serialization \ + --target-task-queue nexus-serialization-handler-b +./temporal operator nexus endpoint create \ + --name handler-b-2 \ + --target-namespace nexus-serialization \ + --target-task-queue nexus-serialization-handler-b ``` Run each of the following in its own terminal from the repository root: ```bash ./gradlew -q :core:execute -PmainClass=io.temporal.samples.nexusserializationcontext.handler.CompressedEncryptedHandlerWorker \ - --args="-namespace nexus-serialization-key-a-handler" + --args="-namespace nexus-serialization" ``` ```bash ./gradlew -q :core:execute -PmainClass=io.temporal.samples.nexusserializationcontext.handler.EncryptedHandlerWorker \ - --args="-namespace nexus-serialization-key-b-handler" + --args="-namespace nexus-serialization" ``` ```bash ./gradlew -q :core:execute -PmainClass=io.temporal.samples.nexusserializationcontext.caller.CallerWorker \ - --args="-namespace nexus-serialization-caller" + --args="-namespace nexus-serialization" ``` ```bash ./gradlew -q :core:execute -PmainClass=io.temporal.samples.nexusserializationcontext.caller.CallerStarter \ - --args="-namespace nexus-serialization-caller" + --args="-namespace nexus-serialization" ``` -The starter will print: - -```text -Compressed and encrypted endpoint sync result: Hello from Nexus -Encrypted endpoint sync result: Hello from Nexus -Compressed and encrypted endpoint async result: Hello from Nexus -Encrypted endpoint async result: Hello from Nexus -``` +The starter prints a sync result and an async result for each endpoint. All eight +results should be `Hello from Nexus`. ## Test -Run the codec tests and integration test: +Run the codec tests: ```bash -./gradlew :core:test --tests 'io.temporal.samples.nexusserializationcontext.*' +./gradlew :core:test --tests 'io.temporal.samples.nexusserializationcontext.codec.NexusCodecTest' ``` - -The integration test downloads and starts the dev-server version listed above, -runs synchronous and workflow-backed asynchronous calls through both endpoints, -and checks the recorded Nexus context and encrypted async-result metadata in -workflow history. It stops the server and workers when finished. diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/SampleConfig.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/SampleConfig.java index 1ba965c4..8dfb19a9 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/SampleConfig.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/SampleConfig.java @@ -1,18 +1,21 @@ package io.temporal.samples.nexusserializationcontext; public final class SampleConfig { - public static final String COMPRESSED_ENCRYPTED_ENDPOINT = - "nexus-serialization-compressed-encrypted"; - public static final String ENCRYPTED_ENDPOINT = "nexus-serialization-encrypted"; + public static final String HANDLER_A_1_ENDPOINT = "handler-a-1"; + public static final String HANDLER_B_1_ENDPOINT = "handler-b-1"; + public static final String HANDLER_A_2_ENDPOINT = "handler-a-2"; + public static final String HANDLER_B_2_ENDPOINT = "handler-b-2"; public static final String ENDPOINT_METADATA_KEY = "nexus-endpoint-name"; - public static final String KEY_A_ID = "key-a"; - public static final String KEY_B_ID = "key-b"; - public static final String KEY_C_ID = "key-c"; + public static final String HANDLER_A_KEY_1_ID = "handler-a-1"; + public static final String HANDLER_B_KEY_1_ID = "handler-b-1"; + public static final String HANDLER_A_KEY_2_ID = "handler-a-2"; + public static final String HANDLER_B_KEY_2_ID = "handler-b-2"; - // Hard-coded keys are only for this local sample. - public static final String KEY_A_VALUE = "sample-key-A-123"; - public static final String KEY_B_VALUE = "sample-key-B-123"; - public static final String KEY_C_VALUE = "sample-key-C-123"; + // Four distinct, hard-coded keys for this local sample only. + public static final String HANDLER_A_KEY_1 = "handler-a-key-01"; + public static final String HANDLER_B_KEY_1 = "handler-b-key-01"; + public static final String HANDLER_A_KEY_2 = "handler-a-key-02"; + public static final String HANDLER_B_KEY_2 = "handler-b-key-02"; private SampleConfig() {} } diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerStarter.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerStarter.java index 7defd522..b5b11b4c 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerStarter.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerStarter.java @@ -4,6 +4,7 @@ import io.temporal.client.WorkflowClientOptions; import io.temporal.client.WorkflowOptions; import io.temporal.samples.nexus.options.ClientOptions; +import io.temporal.samples.nexusserializationcontext.SampleConfig; public class CallerStarter { public static void main(String[] args) { @@ -18,12 +19,20 @@ public static void main(String[] args) { EndpointResults results = workflow.echoThroughEndpoints("Hello from Nexus"); System.out.println( - "Compressed and encrypted endpoint sync result: " - + results.compressedEncryptedSyncResult()); - System.out.println("Encrypted endpoint sync result: " + results.encryptedSyncResult()); + SampleConfig.HANDLER_A_1_ENDPOINT + " sync result: " + results.handlerA1SyncResult()); System.out.println( - "Compressed and encrypted endpoint async result: " - + results.compressedEncryptedAsyncResult()); - System.out.println("Encrypted endpoint async result: " + results.encryptedAsyncResult()); + SampleConfig.HANDLER_A_1_ENDPOINT + " async result: " + results.handlerA1AsyncResult()); + System.out.println( + SampleConfig.HANDLER_B_1_ENDPOINT + " sync result: " + results.handlerB1SyncResult()); + System.out.println( + SampleConfig.HANDLER_B_1_ENDPOINT + " async result: " + results.handlerB1AsyncResult()); + System.out.println( + SampleConfig.HANDLER_A_2_ENDPOINT + " sync result: " + results.handlerA2SyncResult()); + System.out.println( + SampleConfig.HANDLER_A_2_ENDPOINT + " async result: " + results.handlerA2AsyncResult()); + System.out.println( + SampleConfig.HANDLER_B_2_ENDPOINT + " sync result: " + results.handlerB2SyncResult()); + System.out.println( + SampleConfig.HANDLER_B_2_ENDPOINT + " async result: " + results.handlerB2AsyncResult()); } } diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorker.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorker.java index 3c993809..a78c5ac3 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorker.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorker.java @@ -34,25 +34,37 @@ public static void main(String[] args) { } public static DataConverter dataConverter() { - SecretKey keyA = - new SecretKeySpec(SampleConfig.KEY_A_VALUE.getBytes(StandardCharsets.UTF_8), "AES"); - SecretKey keyB = - new SecretKeySpec(SampleConfig.KEY_B_VALUE.getBytes(StandardCharsets.UTF_8), "AES"); - SecretKey keyC = - new SecretKeySpec(SampleConfig.KEY_C_VALUE.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey handlerAKey1 = + new SecretKeySpec(SampleConfig.HANDLER_A_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey handlerBKey1 = + new SecretKeySpec(SampleConfig.HANDLER_B_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey handlerAKey2 = + new SecretKeySpec(SampleConfig.HANDLER_A_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey handlerBKey2 = + new SecretKeySpec(SampleConfig.HANDLER_B_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); + PayloadCodec handlerAKey1Encryption = + new AesGcmCodec(SampleConfig.HANDLER_A_KEY_1_ID, handlerAKey1); + PayloadCodec handlerBKey1Encryption = + new AesGcmCodec(SampleConfig.HANDLER_B_KEY_1_ID, handlerBKey1); + PayloadCodec handlerAKey2Encryption = + new AesGcmCodec(SampleConfig.HANDLER_A_KEY_2_ID, handlerAKey2); + PayloadCodec handlerBKey2Encryption = + new AesGcmCodec(SampleConfig.HANDLER_B_KEY_2_ID, handlerBKey2); // ChainCodec encodes last to first: compress, then encrypt. - PayloadCodec compressedEncrypted = - new ChainCodec(List.of(new AesGcmCodec(SampleConfig.KEY_A_ID, keyA), new ZlibCodec())); - PayloadCodec encrypted = new AesGcmCodec(SampleConfig.KEY_B_ID, keyB); + Map nexusEndpointCodecs = + Map.of( + SampleConfig.HANDLER_A_1_ENDPOINT, + new ChainCodec(List.of(handlerAKey1Encryption, new ZlibCodec())), + SampleConfig.HANDLER_B_1_ENDPOINT, + handlerBKey1Encryption, + SampleConfig.HANDLER_A_2_ENDPOINT, + new ChainCodec(List.of(handlerAKey2Encryption, new ZlibCodec())), + SampleConfig.HANDLER_B_2_ENDPOINT, + handlerBKey2Encryption); + // Reuse handler A's key 1 for the caller's workflow payloads. + PayloadCodec callerWorkflowEncryption = handlerAKey1Encryption; return new CodecDataConverter( DefaultDataConverter.newDefaultInstance(), - List.of( - new NexusCodec( - Map.of( - SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, - compressedEncrypted, - SampleConfig.ENCRYPTED_ENDPOINT, - encrypted), - new AesGcmCodec(SampleConfig.KEY_C_ID, keyC)))); + List.of(new NexusCodec(nexusEndpointCodecs, callerWorkflowEncryption))); } } diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorkflowImpl.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorkflowImpl.java index 9e0505b0..1944545c 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorkflowImpl.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorkflowImpl.java @@ -12,29 +12,50 @@ public class CallerWorkflowImpl implements CallerWorkflow { @Override public EndpointResults echoThroughEndpoints(String message) { - SyncEchoService compressedEncryptedService = - serviceFor(SyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT); - SyncEchoService encryptedService = - serviceFor(SyncEchoService.class, SampleConfig.ENCRYPTED_ENDPOINT); - AsyncEchoService compressedEncryptedAsyncService = - serviceFor(AsyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT); - AsyncEchoService encryptedAsyncService = - serviceFor(AsyncEchoService.class, SampleConfig.ENCRYPTED_ENDPOINT); + SyncEchoService handlerA1SyncService = + serviceFor(SyncEchoService.class, SampleConfig.HANDLER_A_1_ENDPOINT); + SyncEchoService handlerB1SyncService = + serviceFor(SyncEchoService.class, SampleConfig.HANDLER_B_1_ENDPOINT); + SyncEchoService handlerA2SyncService = + serviceFor(SyncEchoService.class, SampleConfig.HANDLER_A_2_ENDPOINT); + SyncEchoService handlerB2SyncService = + serviceFor(SyncEchoService.class, SampleConfig.HANDLER_B_2_ENDPOINT); + AsyncEchoService handlerA1AsyncService = + serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_A_1_ENDPOINT); + AsyncEchoService handlerB1AsyncService = + serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_B_1_ENDPOINT); + AsyncEchoService handlerA2AsyncService = + serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_A_2_ENDPOINT); + AsyncEchoService handlerB2AsyncService = + serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_B_2_ENDPOINT); + + // Start all eight operations before awaiting results. Each keeps its endpoint context. + NexusOperationHandle handlerA1Sync = + Workflow.startNexusOperation(handlerA1SyncService::echo, message); + NexusOperationHandle handlerA1Async = + Workflow.startNexusOperation(handlerA1AsyncService::echoAsync, message); + NexusOperationHandle handlerB1Sync = + Workflow.startNexusOperation(handlerB1SyncService::echo, message); + NexusOperationHandle handlerB1Async = + Workflow.startNexusOperation(handlerB1AsyncService::echoAsync, message); + NexusOperationHandle handlerA2Sync = + Workflow.startNexusOperation(handlerA2SyncService::echo, message); + NexusOperationHandle handlerA2Async = + Workflow.startNexusOperation(handlerA2AsyncService::echoAsync, message); + NexusOperationHandle handlerB2Sync = + Workflow.startNexusOperation(handlerB2SyncService::echo, message); + NexusOperationHandle handlerB2Async = + Workflow.startNexusOperation(handlerB2AsyncService::echoAsync, message); - // Start all operations before awaiting results. Each result keeps its endpoint context. - NexusOperationHandle compressedEncryptedSync = - Workflow.startNexusOperation(compressedEncryptedService::echo, message); - NexusOperationHandle encryptedSync = - Workflow.startNexusOperation(encryptedService::echo, message); - NexusOperationHandle compressedEncryptedAsync = - Workflow.startNexusOperation(compressedEncryptedAsyncService::echoAsync, message); - NexusOperationHandle encryptedAsync = - Workflow.startNexusOperation(encryptedAsyncService::echoAsync, message); return new EndpointResults( - compressedEncryptedSync.getResult().get(), - encryptedSync.getResult().get(), - compressedEncryptedAsync.getResult().get(), - encryptedAsync.getResult().get()); + handlerA1Sync.getResult().get(), + handlerB1Sync.getResult().get(), + handlerA2Sync.getResult().get(), + handlerB2Sync.getResult().get(), + handlerA1Async.getResult().get(), + handlerB1Async.getResult().get(), + handlerA2Async.getResult().get(), + handlerB2Async.getResult().get()); } private static T serviceFor(Class serviceClass, String endpoint) { diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/EndpointResults.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/EndpointResults.java index 741f9286..7d1910bd 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/EndpointResults.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/EndpointResults.java @@ -1,7 +1,11 @@ package io.temporal.samples.nexusserializationcontext.caller; public record EndpointResults( - String compressedEncryptedSyncResult, - String encryptedSyncResult, - String compressedEncryptedAsyncResult, - String encryptedAsyncResult) {} + String handlerA1SyncResult, + String handlerB1SyncResult, + String handlerA2SyncResult, + String handlerB2SyncResult, + String handlerA1AsyncResult, + String handlerB1AsyncResult, + String handlerA2AsyncResult, + String handlerB2AsyncResult) {} diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodec.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodec.java index 3c6284ea..55ea820f 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodec.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodec.java @@ -10,12 +10,12 @@ import javax.annotation.Nonnull; /** - * Selects an endpoint codec for synchronous and asynchronous Nexus payloads. The caller's own - * workflow payloads use a separate codec. + * Selects an endpoint codec for synchronous and asynchronous Nexus payloads. Ordinary workflow + * payloads use a separate codec. */ public final class NexusCodec implements PayloadCodec { private final Map codecsByEndpoint; - // The caller workflow has no Nexus endpoint, so Key C encrypts its input and final result. + // Payloads without Nexus context use the local workflow codec. private final PayloadCodec workflowCodec; public NexusCodec(Map codecsByEndpoint, PayloadCodec workflowCodec) { @@ -29,7 +29,7 @@ public PayloadCodec withContext(@Nonnull SerializationContext context) { if (context instanceof NexusSerializationContext nexusContext) { return codecFor(nexusContext.getEndpoint()).withContext(context); } - // Caller workflow input and result have no Nexus endpoint; use the caller's Key C codec. + // Ordinary workflow payloads use the worker's configured default codec. return workflowCodec.withContext(context); } diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java index 818f3be5..2112282b 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java @@ -6,19 +6,22 @@ import io.temporal.common.converter.DataConverter; import io.temporal.common.converter.DefaultDataConverter; import io.temporal.payload.codec.ChainCodec; +import io.temporal.payload.codec.PayloadCodec; import io.temporal.samples.nexus.options.ClientOptions; import io.temporal.samples.nexusserializationcontext.SampleConfig; import io.temporal.samples.nexusserializationcontext.codec.AesGcmCodec; +import io.temporal.samples.nexusserializationcontext.codec.NexusCodec; import io.temporal.samples.nexusserializationcontext.codec.ZlibCodec; import io.temporal.worker.Worker; import io.temporal.worker.WorkerFactory; import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.Map; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class CompressedEncryptedHandlerWorker { - private static final String TASK_QUEUE = "nexus-serialization-key-a-handler"; + private static final String TASK_QUEUE = "nexus-serialization-handler-a"; public static void main(String[] args) { WorkflowClient client = @@ -33,13 +36,25 @@ public static void main(String[] args) { } public static DataConverter dataConverter() { - SecretKey keyA = - new SecretKeySpec(SampleConfig.KEY_A_VALUE.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey key1 = + new SecretKeySpec(SampleConfig.HANDLER_A_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey key2 = + new SecretKeySpec(SampleConfig.HANDLER_A_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); + PayloadCodec key1Encryption = new AesGcmCodec(SampleConfig.HANDLER_A_KEY_1_ID, key1); + PayloadCodec key2Encryption = new AesGcmCodec(SampleConfig.HANDLER_A_KEY_2_ID, key2); // ChainCodec encodes last to first: compress, then encrypt. + PayloadCodec compressedKey1Encryption = + new ChainCodec(List.of(key1Encryption, new ZlibCodec())); + PayloadCodec compressedKey2Encryption = + new ChainCodec(List.of(key2Encryption, new ZlibCodec())); + Map nexusEndpointCodecs = + Map.of( + SampleConfig.HANDLER_A_1_ENDPOINT, compressedKey1Encryption, + SampleConfig.HANDLER_A_2_ENDPOINT, compressedKey2Encryption); + // Ordinary workflow inputs use this handler's key 1, regardless of the Nexus endpoint. + PayloadCodec handlerWorkflowEncryption = key1Encryption; return new CodecDataConverter( DefaultDataConverter.newDefaultInstance(), - List.of( - new ChainCodec( - List.of(new AesGcmCodec(SampleConfig.KEY_A_ID, keyA), new ZlibCodec())))); + List.of(new NexusCodec(nexusEndpointCodecs, handlerWorkflowEncryption))); } } diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java index ea954ded..115ba8c7 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java @@ -5,18 +5,21 @@ import io.temporal.common.converter.CodecDataConverter; import io.temporal.common.converter.DataConverter; import io.temporal.common.converter.DefaultDataConverter; +import io.temporal.payload.codec.PayloadCodec; import io.temporal.samples.nexus.options.ClientOptions; import io.temporal.samples.nexusserializationcontext.SampleConfig; import io.temporal.samples.nexusserializationcontext.codec.AesGcmCodec; +import io.temporal.samples.nexusserializationcontext.codec.NexusCodec; import io.temporal.worker.Worker; import io.temporal.worker.WorkerFactory; import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.Map; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class EncryptedHandlerWorker { - private static final String TASK_QUEUE = "nexus-serialization-key-b-handler"; + private static final String TASK_QUEUE = "nexus-serialization-handler-b"; public static void main(String[] args) { WorkflowClient client = @@ -31,10 +34,20 @@ public static void main(String[] args) { } public static DataConverter dataConverter() { - SecretKey keyB = - new SecretKeySpec(SampleConfig.KEY_B_VALUE.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey key1 = + new SecretKeySpec(SampleConfig.HANDLER_B_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey key2 = + new SecretKeySpec(SampleConfig.HANDLER_B_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); + PayloadCodec key1Encryption = new AesGcmCodec(SampleConfig.HANDLER_B_KEY_1_ID, key1); + PayloadCodec key2Encryption = new AesGcmCodec(SampleConfig.HANDLER_B_KEY_2_ID, key2); + Map nexusEndpointCodecs = + Map.of( + SampleConfig.HANDLER_B_1_ENDPOINT, key1Encryption, + SampleConfig.HANDLER_B_2_ENDPOINT, key2Encryption); + // Ordinary workflow inputs use this handler's key 1, regardless of the Nexus endpoint. + PayloadCodec handlerWorkflowEncryption = key1Encryption; return new CodecDataConverter( DefaultDataConverter.newDefaultInstance(), - List.of(new AesGcmCodec(SampleConfig.KEY_B_ID, keyB))); + List.of(new NexusCodec(nexusEndpointCodecs, handlerWorkflowEncryption))); } } diff --git a/core/src/test/java/io/temporal/samples/nexusserializationcontext/NexusSerializationContextIntegrationTest.java b/core/src/test/java/io/temporal/samples/nexusserializationcontext/NexusSerializationContextIntegrationTest.java deleted file mode 100644 index 0447d313..00000000 --- a/core/src/test/java/io/temporal/samples/nexusserializationcontext/NexusSerializationContextIntegrationTest.java +++ /dev/null @@ -1,175 +0,0 @@ -package io.temporal.samples.nexusserializationcontext; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import io.temporal.api.common.v1.Payload; -import io.temporal.api.common.v1.WorkflowExecution; -import io.temporal.api.history.v1.WorkflowExecutionStartedEventAttributes; -import io.temporal.client.WorkflowClient; -import io.temporal.client.WorkflowClientOptions; -import io.temporal.client.WorkflowOptions; -import io.temporal.common.WorkflowExecutionHistory; -import io.temporal.common.converter.DataConverter; -import io.temporal.common.converter.EncodingKeys; -import io.temporal.samples.nexusserializationcontext.caller.CallerWorker; -import io.temporal.samples.nexusserializationcontext.caller.CallerWorkflow; -import io.temporal.samples.nexusserializationcontext.caller.CallerWorkflowImpl; -import io.temporal.samples.nexusserializationcontext.caller.EndpointResults; -import io.temporal.samples.nexusserializationcontext.handler.AsyncEchoServiceImpl; -import io.temporal.samples.nexusserializationcontext.handler.CompressedEncryptedHandlerWorker; -import io.temporal.samples.nexusserializationcontext.handler.EchoWorkflowImpl; -import io.temporal.samples.nexusserializationcontext.handler.EncryptedHandlerWorker; -import io.temporal.samples.nexusserializationcontext.handler.SyncEchoServiceImpl; -import io.temporal.samples.nexusserializationcontext.service.AsyncEchoService; -import io.temporal.testing.TemporalDevServerOptions; -import io.temporal.testing.TestEnvironmentOptions; -import io.temporal.testing.TestWorkflowEnvironment; -import io.temporal.worker.Worker; -import io.temporal.worker.WorkerFactory; -import java.time.Duration; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; - -class NexusSerializationContextIntegrationTest { - private static final String DEV_SERVER_VERSION = "v1.9.3-nexus-serialization-context"; - - @Test - @Timeout(120) - void asyncResultsUseTheEndpointContextRecordedByTheServer() throws InterruptedException { - try (TestWorkflowEnvironment environment = - TestWorkflowEnvironment.startLocal( - TestEnvironmentOptions.newBuilder() - .setWorkflowClientOptions( - WorkflowClientOptions.newBuilder() - .setDataConverter(CallerWorker.dataConverter()) - .build()) - .build(), - TemporalDevServerOptions.newBuilder().setDownloadVersion(DEV_SERVER_VERSION).build())) { - String callerQueue = "serialization-caller"; - String handlerAQueue = "serialization-handler-a"; - String handlerBQueue = "serialization-handler-b"; - WorkerFactory handlerA = - handlerFactory( - environment, handlerAQueue, CompressedEncryptedHandlerWorker.dataConverter()); - WorkerFactory handlerB = - handlerFactory(environment, handlerBQueue, EncryptedHandlerWorker.dataConverter()); - try { - environment.createNexusEndpoint(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, handlerAQueue); - environment.createNexusEndpoint(SampleConfig.ENCRYPTED_ENDPOINT, handlerBQueue); - environment - .newWorker(callerQueue) - .registerWorkflowImplementationTypes(CallerWorkflowImpl.class); - handlerA.start(); - handlerB.start(); - environment.start(); - - WorkflowClient client = environment.getWorkflowClient(); - CallerWorkflow caller = - client.newWorkflowStub( - CallerWorkflow.class, - WorkflowOptions.newBuilder() - .setTaskQueue(callerQueue) - .setWorkflowExecutionTimeout(Duration.ofSeconds(60)) - .build()); - String message = "Hello from Nexus"; - assertEquals( - new EndpointResults(message, message, message, message), - caller.echoThroughEndpoints(message)); - - Map expectedKeys = - Map.of( - SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, SampleConfig.KEY_A_ID, - SampleConfig.ENCRYPTED_ENDPOINT, SampleConfig.KEY_B_ID); - Set observedEndpoints = new HashSet<>(); - for (WorkflowExecution execution : backingWorkflows(environment)) { - WorkflowExecutionHistory history = - client.fetchHistory(execution.getWorkflowId(), execution.getRunId()); - WorkflowExecutionStartedEventAttributes started = - history.getEvents().get(0).getWorkflowExecutionStartedEventAttributes(); - assertTrue(started.hasPropagatedNexusSerializationContext()); - String endpoint = started.getPropagatedNexusSerializationContext().getEndpoint(); - assertTrue(expectedKeys.containsKey(endpoint), "Unexpected endpoint: " + endpoint); - assertTrue(observedEndpoints.add(endpoint), "Duplicate endpoint: " + endpoint); - assertEquals( - AsyncEchoService.SERVICE_NAME, - started.getPropagatedNexusSerializationContext().getService()); - assertEquals( - AsyncEchoService.ECHO_ASYNC_OPERATION_NAME, - started.getPropagatedNexusSerializationContext().getOperation()); - - // Native propagation uses the start-event field, not the sample's old custom header. - assertFalse(started.getHeader().containsFields(SampleConfig.ENDPOINT_METADATA_KEY)); - assertFalse( - started - .getInput() - .getPayloads(0) - .containsMetadata(SampleConfig.ENDPOINT_METADATA_KEY)); - assertTrue(history.getLastEvent().hasWorkflowExecutionCompletedEventAttributes()); - Payload result = - history - .getLastEvent() - .getWorkflowExecutionCompletedEventAttributes() - .getResult() - .getPayloads(0); - assertEquals( - "binary/nexus-aes-gcm", - result.getMetadataOrThrow(EncodingKeys.METADATA_ENCODING_KEY).toStringUtf8()); - assertEquals( - endpoint, - result.getMetadataOrThrow(SampleConfig.ENDPOINT_METADATA_KEY).toStringUtf8()); - assertEquals( - expectedKeys.get(endpoint), - result.getMetadataOrThrow("encryption-key-id").toStringUtf8()); - } - assertEquals(expectedKeys.keySet(), observedEndpoints); - } finally { - handlerA.shutdownNow(); - handlerB.shutdownNow(); - handlerA.awaitTermination(10, TimeUnit.SECONDS); - handlerB.awaitTermination(10, TimeUnit.SECONDS); - } - } - } - - private static WorkerFactory handlerFactory( - TestWorkflowEnvironment environment, String taskQueue, DataConverter converter) { - WorkflowClient client = - WorkflowClient.newInstance( - environment.getWorkflowServiceStubs(), - WorkflowClientOptions.newBuilder() - .setNamespace(environment.getNamespace()) - .setDataConverter(converter) - .build()); - WorkerFactory factory = WorkerFactory.newInstance(client); - Worker worker = factory.newWorker(taskQueue); - worker.registerWorkflowImplementationTypes(EchoWorkflowImpl.class); - worker.registerNexusServiceImplementation( - new SyncEchoServiceImpl(), new AsyncEchoServiceImpl()); - return factory; - } - - private static List backingWorkflows(TestWorkflowEnvironment environment) { - // Visibility is eventually consistent; wait for both backing workflows to be indexed. - long deadline = System.nanoTime() + Duration.ofSeconds(10).toNanos(); - do { - List executions; - try (var stream = - environment.getWorkflowClient().listExecutions("WorkflowType = 'EchoWorkflow'")) { - executions = stream.map(metadata -> metadata.getExecution()).collect(Collectors.toList()); - } - if (executions.size() == 2) { - return executions; - } - environment.sleep(Duration.ofMillis(100)); - } while (System.nanoTime() < deadline); - throw new AssertionError("Expected two backing workflows to appear in visibility"); - } -} diff --git a/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java b/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java index 82f271c0..17215498 100644 --- a/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java +++ b/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java @@ -5,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import com.google.protobuf.ByteString; import io.temporal.api.common.v1.Payload; import io.temporal.common.converter.DataConverter; import io.temporal.common.converter.EncodingKeys; @@ -17,75 +16,48 @@ import io.temporal.samples.nexusserializationcontext.caller.EndpointResults; import io.temporal.samples.nexusserializationcontext.handler.CompressedEncryptedHandlerWorker; import io.temporal.samples.nexusserializationcontext.handler.EncryptedHandlerWorker; -import io.temporal.samples.nexusserializationcontext.service.AsyncEchoService; import io.temporal.samples.nexusserializationcontext.service.SyncEchoService; import org.junit.jupiter.api.Test; class NexusCodecTest { @Test - void encryptsSyncAndAsyncOperationsWithTheKeyForTheirEndpoint() { - DataConverter syncA = converterFor(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT); - DataConverter asyncA = converterFor(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, true); - DataConverter syncB = converterFor(SampleConfig.ENCRYPTED_ENDPOINT); - DataConverter asyncB = converterFor(SampleConfig.ENCRYPTED_ENDPOINT, true); - - Payload syncAPayload = syncA.toPayload("hello").orElseThrow(); - Payload asyncAPayload = asyncA.toPayload("hello").orElseThrow(); - Payload syncBPayload = syncB.toPayload("hello").orElseThrow(); - Payload asyncBPayload = asyncB.toPayload("hello").orElseThrow(); + void callerAndHandlersUseTheKeyForEachEndpoint() { + DataConverter handlerA = CompressedEncryptedHandlerWorker.dataConverter(); + DataConverter handlerB = EncryptedHandlerWorker.dataConverter(); - assertEncryptedFor( - syncAPayload, SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, SampleConfig.KEY_A_ID); - assertEncryptedFor( - asyncAPayload, SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, SampleConfig.KEY_A_ID); - assertEncryptedFor(syncBPayload, SampleConfig.ENCRYPTED_ENDPOINT, SampleConfig.KEY_B_ID); - assertEncryptedFor(asyncBPayload, SampleConfig.ENCRYPTED_ENDPOINT, SampleConfig.KEY_B_ID); - assertEquals("hello", syncA.fromPayload(syncAPayload, String.class, String.class)); - assertEquals("hello", asyncA.fromPayload(asyncAPayload, String.class, String.class)); - assertEquals("hello", syncB.fromPayload(syncBPayload, String.class, String.class)); - assertEquals("hello", asyncB.fromPayload(asyncBPayload, String.class, String.class)); - assertThrows( - PayloadCodecException.class, - () -> syncA.fromPayload(syncBPayload, String.class, String.class)); - assertThrows( - PayloadCodecException.class, - () -> asyncB.fromPayload(asyncAPayload, String.class, String.class)); - Payload keyAPayloadMarkedAsB = - syncAPayload.toBuilder() - .putMetadata( - AesGcmCodec.KEY_ID_METADATA_KEY, ByteString.copyFromUtf8(SampleConfig.KEY_B_ID)) - .build(); - assertThrows( - PayloadCodecException.class, - () -> syncB.fromPayload(keyAPayloadMarkedAsB, String.class, String.class)); + assertEndpointRoundTrip( + handlerA, SampleConfig.HANDLER_A_1_ENDPOINT, SampleConfig.HANDLER_A_KEY_1_ID); + assertEndpointRoundTrip( + handlerA, SampleConfig.HANDLER_A_2_ENDPOINT, SampleConfig.HANDLER_A_KEY_2_ID); + assertEndpointRoundTrip( + handlerB, SampleConfig.HANDLER_B_1_ENDPOINT, SampleConfig.HANDLER_B_KEY_1_ID); + assertEndpointRoundTrip( + handlerB, SampleConfig.HANDLER_B_2_ENDPOINT, SampleConfig.HANDLER_B_KEY_2_ID); } @Test - void compressesBeforeEncryptingForBothOperationsOnTheCompressedEndpoint() { - String message = "repeat me ".repeat(100); - Payload syncCompressed = - converterFor(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT).toPayload(message).orElseThrow(); - Payload syncEncryptedOnly = - converterFor(SampleConfig.ENCRYPTED_ENDPOINT).toPayload(message).orElseThrow(); - Payload asyncCompressed = - converterFor(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, true) - .toPayload(message) - .orElseThrow(); - Payload asyncEncrypted = - converterFor(SampleConfig.ENCRYPTED_ENDPOINT, true).toPayload(message).orElseThrow(); - - assertTrue(syncCompressed.getData().size() < syncEncryptedOnly.getData().size()); - assertTrue(asyncCompressed.getData().size() < asyncEncrypted.getData().size()); + void handlerACompressesBeforeEncryptingWithEitherKey() { + assertCompressionReducesPayload( + SampleConfig.HANDLER_A_1_ENDPOINT, SampleConfig.HANDLER_B_1_ENDPOINT); + assertCompressionReducesPayload( + SampleConfig.HANDLER_A_2_ENDPOINT, SampleConfig.HANDLER_B_2_ENDPOINT); } @Test - void encryptsCallerWorkflowInputAndResultWithKeyC() { + void callerWorkflowPayloadsReuseHandlerAKey1() { DataConverter converter = CallerWorker.dataConverter() - .withContext( - new WorkflowSerializationContext("nexus-serialization-caller", "caller-id")); - EndpointResults results = new EndpointResults("reply A", "reply B", "reply C", "reply D"); - + .withContext(new WorkflowSerializationContext("nexus-serialization", "caller-id")); + EndpointResults results = + new EndpointResults( + "reply A1", + "reply B1", + "reply A2", + "reply B2", + "async reply A1", + "async reply B1", + "async reply A2", + "async reply B2"); Payload input = converter.toPayload("Hello from Nexus").orElseThrow(); Payload result = converter.toPayload(results).orElseThrow(); @@ -93,14 +65,11 @@ void encryptsCallerWorkflowInputAndResultWithKeyC() { NexusEncoding.AES_GCM.encodingName(), input.getMetadataOrThrow(EncodingKeys.METADATA_ENCODING_KEY).toStringUtf8()); assertEquals( - SampleConfig.KEY_C_ID, - input.getMetadataOrThrow(AesGcmCodec.KEY_ID_METADATA_KEY).toStringUtf8()); - assertEquals( - SampleConfig.KEY_C_ID, + SampleConfig.HANDLER_A_KEY_1_ID, result.getMetadataOrThrow(AesGcmCodec.KEY_ID_METADATA_KEY).toStringUtf8()); - assertFalse(input.getMetadataMap().containsKey(SampleConfig.ENDPOINT_METADATA_KEY)); + assertFalse(input.containsMetadata(SampleConfig.ENDPOINT_METADATA_KEY)); + assertFalse(result.containsMetadata(SampleConfig.ENDPOINT_METADATA_KEY)); assertFalse(input.getData().toStringUtf8().contains("Hello from Nexus")); - assertFalse(result.getData().toStringUtf8().contains("reply A")); assertEquals("Hello from Nexus", converter.fromPayload(input, String.class, String.class)); assertEquals( results, converter.fromPayload(result, EndpointResults.class, EndpointResults.class)); @@ -108,101 +77,42 @@ void encryptsCallerWorkflowInputAndResultWithKeyC() { @Test void rejectsUnknownEndpoints() { - DataConverter converter = - CallerWorker.dataConverter() - .withContext( - new NexusSerializationContext( - "unknown", SyncEchoService.SERVICE_NAME, SyncEchoService.ECHO_OPERATION_NAME)); - + DataConverter converter = converterFor("unknown"); assertThrows(PayloadCodecException.class, () -> converter.toPayload("hello")); } - @Test - void synchronousHandlersUseTheirOwnCodecWithoutEndpointContext() { - DataConverter callerA = converterFor(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT); - DataConverter callerB = converterFor(SampleConfig.ENCRYPTED_ENDPOINT); - DataConverter handlerA = CompressedEncryptedHandlerWorker.dataConverter(); - DataConverter handlerB = EncryptedHandlerWorker.dataConverter(); - - Payload requestA = callerA.toPayload("hello").orElseThrow(); - Payload requestB = callerB.toPayload("hello").orElseThrow(); - assertEquals("hello", handlerA.fromPayload(requestA, String.class, String.class)); - assertEquals("hello", handlerB.fromPayload(requestB, String.class, String.class)); - - Payload resultA = handlerA.toPayload("reply").orElseThrow(); - Payload resultB = handlerB.toPayload("reply").orElseThrow(); - assertEquals("reply", callerA.fromPayload(resultA, String.class, String.class)); - assertEquals("reply", callerB.fromPayload(resultB, String.class, String.class)); - - Payload contextualResultA = - handlerA - .withContext(contextFor(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT)) - .toPayload("reply") - .orElseThrow(); - Payload contextualResultB = - handlerB - .withContext(contextFor(SampleConfig.ENCRYPTED_ENDPOINT)) - .toPayload("reply") - .orElseThrow(); - assertEquals( - SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, - contextualResultA.getMetadataOrThrow(SampleConfig.ENDPOINT_METADATA_KEY).toStringUtf8()); - assertEquals( - SampleConfig.ENCRYPTED_ENDPOINT, - contextualResultB.getMetadataOrThrow(SampleConfig.ENDPOINT_METADATA_KEY).toStringUtf8()); - - assertThrows( - PayloadCodecException.class, - () -> handlerA.fromPayload(requestB, String.class, String.class)); - assertThrows( - PayloadCodecException.class, - () -> handlerB.fromPayload(requestA, String.class, String.class)); + private static void assertEndpointRoundTrip( + DataConverter handler, String endpoint, String keyId) { + DataConverter caller = converterFor(endpoint); + DataConverter contextualHandler = handler.withContext(contextFor(endpoint)); + Payload request = caller.toPayload("hello").orElseThrow(); + assertEncryptionMetadata(request, endpoint, keyId); + assertEquals("hello", contextualHandler.fromPayload(request, String.class, String.class)); + + Payload result = contextualHandler.toPayload("reply").orElseThrow(); + assertEncryptionMetadata(result, endpoint, keyId); + assertEquals("reply", caller.fromPayload(result, String.class, String.class)); } - @Test - void handlersDecodeAsyncRequestsWithTheirEndpointKey() { - DataConverter handlerA = - CompressedEncryptedHandlerWorker.dataConverter() - .withContext(contextFor(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, true)); - DataConverter handlerB = - EncryptedHandlerWorker.dataConverter() - .withContext(contextFor(SampleConfig.ENCRYPTED_ENDPOINT, true)); - Payload requestA = - converterFor(SampleConfig.COMPRESSED_ENCRYPTED_ENDPOINT, true) - .toPayload("hello") - .orElseThrow(); - Payload requestB = - converterFor(SampleConfig.ENCRYPTED_ENDPOINT, true).toPayload("hello").orElseThrow(); - - assertEquals("hello", handlerA.fromPayload(requestA, String.class, String.class)); - assertEquals("hello", handlerB.fromPayload(requestB, String.class, String.class)); - assertThrows( - PayloadCodecException.class, - () -> handlerA.fromPayload(requestB, String.class, String.class)); + private static void assertCompressionReducesPayload( + String compressedEndpoint, String encryptedOnlyEndpoint) { + String message = "repeat me ".repeat(100); + Payload compressed = converterFor(compressedEndpoint).toPayload(message).orElseThrow(); + Payload encrypted = converterFor(encryptedOnlyEndpoint).toPayload(message).orElseThrow(); + assertTrue(compressed.getData().size() < encrypted.getData().size()); } private static DataConverter converterFor(String endpoint) { return CallerWorker.dataConverter().withContext(contextFor(endpoint)); } - private static DataConverter converterFor(String endpoint, boolean async) { - return CallerWorker.dataConverter().withContext(contextFor(endpoint, async)); - } - private static NexusSerializationContext contextFor(String endpoint) { - return contextFor(endpoint, false); - } - - private static NexusSerializationContext contextFor(String endpoint, boolean async) { - if (async) { - return new NexusSerializationContext( - endpoint, AsyncEchoService.SERVICE_NAME, AsyncEchoService.ECHO_ASYNC_OPERATION_NAME); - } + // Supply context directly to check sample configuration, not SDK context propagation. return new NexusSerializationContext( endpoint, SyncEchoService.SERVICE_NAME, SyncEchoService.ECHO_OPERATION_NAME); } - private static void assertEncryptedFor(Payload payload, String endpoint, String keyId) { + private static void assertEncryptionMetadata(Payload payload, String endpoint, String keyId) { assertEquals( NexusEncoding.AES_GCM.encodingName(), payload.getMetadataOrThrow(EncodingKeys.METADATA_ENCODING_KEY).toStringUtf8()); From e2e8ca8d5153939d4ead81dcc57657783d35a127 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Fri, 13 Nov 2026 14:26:42 -0800 Subject: [PATCH 4/6] Clarify encoding names and separate caller workflow encryption Name endpoints, keys, task queues, and results by their encoding chain. Use a dedicated caller workflow key while retaining the four endpoint keys and their existing secret values. Update the README and codec tests. --- .../nexusserializationcontext/README.md | 45 ++++++----- .../SampleConfig.java | 30 ++++--- .../caller/CallerStarter.java | 24 ++++-- .../caller/CallerWorker.java | 57 +++++++------ .../caller/CallerWorkflowImpl.java | 80 +++++++++---------- .../caller/EndpointResults.java | 16 ++-- .../CompressedEncryptedHandlerWorker.java | 16 ++-- .../handler/EncryptedHandlerWorker.java | 14 ++-- .../codec/NexusCodecTest.java | 45 ++++++----- 9 files changed, 177 insertions(+), 150 deletions(-) diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md index b2edca44..454a2dad 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md @@ -2,32 +2,33 @@ This sample uses `NexusSerializationContext` to select an encryption key for each Nexus endpoint. The caller invokes synchronous and workflow-backed asynchronous operations -through four endpoints. Handler A (`CompressedEncryptedHandlerWorker`) compresses and -then encrypts Nexus payloads. Handler B (`EncryptedHandlerWorker`) encrypts without +through four endpoints. `CompressedEncryptedHandlerWorker` compresses and then +encrypts Nexus payloads. `EncryptedHandlerWorker` encrypts without compression. Each handler selects between its own key 1 and key 2 using the endpoint in the Nexus context. | Endpoint | Encryption key | Nexus payload encoding | |---|---|---| -| `handler-a-1` | `HANDLER_A_KEY_1` | zlib, then AES-GCM | -| `handler-b-1` | `HANDLER_B_KEY_1` | AES-GCM | -| `handler-a-2` | `HANDLER_A_KEY_2` | zlib, then AES-GCM | -| `handler-b-2` | `HANDLER_B_KEY_2` | AES-GCM | +| `nexus-serialization-compressed-encrypted-1` | `COMPRESSED_ENCRYPTED_KEY_1` | zlib, then AES-GCM | +| `nexus-serialization-encrypted-1` | `ENCRYPTED_KEY_1` | AES-GCM | +| `nexus-serialization-compressed-encrypted-2` | `COMPRESSED_ENCRYPTED_KEY_2` | zlib, then AES-GCM | +| `nexus-serialization-encrypted-2` | `ENCRYPTED_KEY_2` | AES-GCM | -These are four distinct 128-bit AES keys. Compression runs before encryption; +These four endpoint keys are distinct 128-bit AES keys. Compression runs before encryption; decoding decrypts before decompressing. AES-GCM authenticates the ciphertext. The AES-GCM and zlib codecs are implemented in this sample; Nexus supplies the endpoint context used to select them. Each worker configures `NexusCodec` with an endpoint-to-codec map and a default codec -for ordinary workflow payloads. The sample reuses key 1 for workflow payloads -to keep the example to four keys: +for ordinary workflow payloads. The caller uses a separate `CALLER_WORKFLOW_KEY` for +its own input and final result, keeping those payloads independent of the keys shared +with Nexus handlers. Each handler reuses its key 1 for backing workflow inputs. | Ordinary workflow payload | Encryption key | |---|---| -| Caller workflow input and final result | `HANDLER_A_KEY_1` | -| Handler A backing workflow inputs | `HANDLER_A_KEY_1` | -| Handler B backing workflow inputs | `HANDLER_B_KEY_1` | +| Caller workflow input and final result | `CALLER_WORKFLOW_KEY` | +| Compressed/encrypted handler workflow inputs | `COMPRESSED_ENCRYPTED_KEY_1` | +| Encrypted handler workflow inputs | `ENCRYPTED_KEY_1` | These workflow payloads use AES-GCM without compression or endpoint metadata. An async Nexus result uses the key and compression chain selected by its endpoint, @@ -51,8 +52,8 @@ payloads, `nexus-endpoint-name` metadata. For example: ```text encoding: binary/nexus-aes-gcm -encryption-key-id: handler-a-1 -nexus-endpoint-name: handler-a-1 +encryption-key-id: compressed-encrypted-1 +nexus-endpoint-name: nexus-serialization-compressed-encrypted-1 ``` A Codec Server can use that metadata to select the key and decompression chain without @@ -85,21 +86,21 @@ Create two endpoints for each handler worker: ```bash ./temporal operator nexus endpoint create \ - --name handler-a-1 \ + --name nexus-serialization-compressed-encrypted-1 \ --target-namespace nexus-serialization \ - --target-task-queue nexus-serialization-handler-a + --target-task-queue nexus-serialization-compressed-encrypted-handler ./temporal operator nexus endpoint create \ - --name handler-a-2 \ + --name nexus-serialization-compressed-encrypted-2 \ --target-namespace nexus-serialization \ - --target-task-queue nexus-serialization-handler-a + --target-task-queue nexus-serialization-compressed-encrypted-handler ./temporal operator nexus endpoint create \ - --name handler-b-1 \ + --name nexus-serialization-encrypted-1 \ --target-namespace nexus-serialization \ - --target-task-queue nexus-serialization-handler-b + --target-task-queue nexus-serialization-encrypted-handler ./temporal operator nexus endpoint create \ - --name handler-b-2 \ + --name nexus-serialization-encrypted-2 \ --target-namespace nexus-serialization \ - --target-task-queue nexus-serialization-handler-b + --target-task-queue nexus-serialization-encrypted-handler ``` Run each of the following in its own terminal from the repository root: diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/SampleConfig.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/SampleConfig.java index 8dfb19a9..1abdf080 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/SampleConfig.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/SampleConfig.java @@ -1,21 +1,25 @@ package io.temporal.samples.nexusserializationcontext; public final class SampleConfig { - public static final String HANDLER_A_1_ENDPOINT = "handler-a-1"; - public static final String HANDLER_B_1_ENDPOINT = "handler-b-1"; - public static final String HANDLER_A_2_ENDPOINT = "handler-a-2"; - public static final String HANDLER_B_2_ENDPOINT = "handler-b-2"; + public static final String COMPRESSED_ENCRYPTED_1_ENDPOINT = + "nexus-serialization-compressed-encrypted-1"; + public static final String ENCRYPTED_1_ENDPOINT = "nexus-serialization-encrypted-1"; + public static final String COMPRESSED_ENCRYPTED_2_ENDPOINT = + "nexus-serialization-compressed-encrypted-2"; + public static final String ENCRYPTED_2_ENDPOINT = "nexus-serialization-encrypted-2"; public static final String ENDPOINT_METADATA_KEY = "nexus-endpoint-name"; - public static final String HANDLER_A_KEY_1_ID = "handler-a-1"; - public static final String HANDLER_B_KEY_1_ID = "handler-b-1"; - public static final String HANDLER_A_KEY_2_ID = "handler-a-2"; - public static final String HANDLER_B_KEY_2_ID = "handler-b-2"; + public static final String COMPRESSED_ENCRYPTED_KEY_1_ID = "compressed-encrypted-1"; + public static final String ENCRYPTED_KEY_1_ID = "encrypted-1"; + public static final String COMPRESSED_ENCRYPTED_KEY_2_ID = "compressed-encrypted-2"; + public static final String ENCRYPTED_KEY_2_ID = "encrypted-2"; + public static final String CALLER_WORKFLOW_KEY_ID = "caller-workflow"; - // Four distinct, hard-coded keys for this local sample only. - public static final String HANDLER_A_KEY_1 = "handler-a-key-01"; - public static final String HANDLER_B_KEY_1 = "handler-b-key-01"; - public static final String HANDLER_A_KEY_2 = "handler-a-key-02"; - public static final String HANDLER_B_KEY_2 = "handler-b-key-02"; + // Hard-coded keys are only for this local sample. + public static final String COMPRESSED_ENCRYPTED_KEY_1 = "handler-a-key-01"; + public static final String ENCRYPTED_KEY_1 = "handler-b-key-01"; + public static final String COMPRESSED_ENCRYPTED_KEY_2 = "handler-a-key-02"; + public static final String ENCRYPTED_KEY_2 = "handler-b-key-02"; + public static final String CALLER_WORKFLOW_KEY = "sample-key-C-123"; private SampleConfig() {} } diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerStarter.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerStarter.java index b5b11b4c..2da33df4 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerStarter.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerStarter.java @@ -19,20 +19,28 @@ public static void main(String[] args) { EndpointResults results = workflow.echoThroughEndpoints("Hello from Nexus"); System.out.println( - SampleConfig.HANDLER_A_1_ENDPOINT + " sync result: " + results.handlerA1SyncResult()); + SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT + + " sync result: " + + results.compressedEncrypted1SyncResult()); System.out.println( - SampleConfig.HANDLER_A_1_ENDPOINT + " async result: " + results.handlerA1AsyncResult()); + SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT + + " async result: " + + results.compressedEncrypted1AsyncResult()); System.out.println( - SampleConfig.HANDLER_B_1_ENDPOINT + " sync result: " + results.handlerB1SyncResult()); + SampleConfig.ENCRYPTED_1_ENDPOINT + " sync result: " + results.encrypted1SyncResult()); System.out.println( - SampleConfig.HANDLER_B_1_ENDPOINT + " async result: " + results.handlerB1AsyncResult()); + SampleConfig.ENCRYPTED_1_ENDPOINT + " async result: " + results.encrypted1AsyncResult()); System.out.println( - SampleConfig.HANDLER_A_2_ENDPOINT + " sync result: " + results.handlerA2SyncResult()); + SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT + + " sync result: " + + results.compressedEncrypted2SyncResult()); System.out.println( - SampleConfig.HANDLER_A_2_ENDPOINT + " async result: " + results.handlerA2AsyncResult()); + SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT + + " async result: " + + results.compressedEncrypted2AsyncResult()); System.out.println( - SampleConfig.HANDLER_B_2_ENDPOINT + " sync result: " + results.handlerB2SyncResult()); + SampleConfig.ENCRYPTED_2_ENDPOINT + " sync result: " + results.encrypted2SyncResult()); System.out.println( - SampleConfig.HANDLER_B_2_ENDPOINT + " async result: " + results.handlerB2AsyncResult()); + SampleConfig.ENCRYPTED_2_ENDPOINT + " async result: " + results.encrypted2AsyncResult()); } } diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorker.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorker.java index a78c5ac3..474be9b9 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorker.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorker.java @@ -34,35 +34,40 @@ public static void main(String[] args) { } public static DataConverter dataConverter() { - SecretKey handlerAKey1 = - new SecretKeySpec(SampleConfig.HANDLER_A_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); - SecretKey handlerBKey1 = - new SecretKeySpec(SampleConfig.HANDLER_B_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); - SecretKey handlerAKey2 = - new SecretKeySpec(SampleConfig.HANDLER_A_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); - SecretKey handlerBKey2 = - new SecretKeySpec(SampleConfig.HANDLER_B_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); - PayloadCodec handlerAKey1Encryption = - new AesGcmCodec(SampleConfig.HANDLER_A_KEY_1_ID, handlerAKey1); - PayloadCodec handlerBKey1Encryption = - new AesGcmCodec(SampleConfig.HANDLER_B_KEY_1_ID, handlerBKey1); - PayloadCodec handlerAKey2Encryption = - new AesGcmCodec(SampleConfig.HANDLER_A_KEY_2_ID, handlerAKey2); - PayloadCodec handlerBKey2Encryption = - new AesGcmCodec(SampleConfig.HANDLER_B_KEY_2_ID, handlerBKey2); + SecretKey compressedEncryptedKey1 = + new SecretKeySpec( + SampleConfig.COMPRESSED_ENCRYPTED_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey encryptedKey1 = + new SecretKeySpec(SampleConfig.ENCRYPTED_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey compressedEncryptedKey2 = + new SecretKeySpec( + SampleConfig.COMPRESSED_ENCRYPTED_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey encryptedKey2 = + new SecretKeySpec(SampleConfig.ENCRYPTED_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); + SecretKey callerWorkflowKey = + new SecretKeySpec(SampleConfig.CALLER_WORKFLOW_KEY.getBytes(StandardCharsets.UTF_8), "AES"); + PayloadCodec compressedEncryptedKey1Encryption = + new AesGcmCodec(SampleConfig.COMPRESSED_ENCRYPTED_KEY_1_ID, compressedEncryptedKey1); + PayloadCodec encryptedKey1Encryption = + new AesGcmCodec(SampleConfig.ENCRYPTED_KEY_1_ID, encryptedKey1); + PayloadCodec compressedEncryptedKey2Encryption = + new AesGcmCodec(SampleConfig.COMPRESSED_ENCRYPTED_KEY_2_ID, compressedEncryptedKey2); + PayloadCodec encryptedKey2Encryption = + new AesGcmCodec(SampleConfig.ENCRYPTED_KEY_2_ID, encryptedKey2); // ChainCodec encodes last to first: compress, then encrypt. Map nexusEndpointCodecs = Map.of( - SampleConfig.HANDLER_A_1_ENDPOINT, - new ChainCodec(List.of(handlerAKey1Encryption, new ZlibCodec())), - SampleConfig.HANDLER_B_1_ENDPOINT, - handlerBKey1Encryption, - SampleConfig.HANDLER_A_2_ENDPOINT, - new ChainCodec(List.of(handlerAKey2Encryption, new ZlibCodec())), - SampleConfig.HANDLER_B_2_ENDPOINT, - handlerBKey2Encryption); - // Reuse handler A's key 1 for the caller's workflow payloads. - PayloadCodec callerWorkflowEncryption = handlerAKey1Encryption; + SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT, + new ChainCodec(List.of(compressedEncryptedKey1Encryption, new ZlibCodec())), + SampleConfig.ENCRYPTED_1_ENDPOINT, + encryptedKey1Encryption, + SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT, + new ChainCodec(List.of(compressedEncryptedKey2Encryption, new ZlibCodec())), + SampleConfig.ENCRYPTED_2_ENDPOINT, + encryptedKey2Encryption); + // Ordinary caller workflow payloads use a separate key from Nexus payloads. + PayloadCodec callerWorkflowEncryption = + new AesGcmCodec(SampleConfig.CALLER_WORKFLOW_KEY_ID, callerWorkflowKey); return new CodecDataConverter( DefaultDataConverter.newDefaultInstance(), List.of(new NexusCodec(nexusEndpointCodecs, callerWorkflowEncryption))); diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorkflowImpl.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorkflowImpl.java index 1944545c..53cb0965 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorkflowImpl.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorkflowImpl.java @@ -12,50 +12,50 @@ public class CallerWorkflowImpl implements CallerWorkflow { @Override public EndpointResults echoThroughEndpoints(String message) { - SyncEchoService handlerA1SyncService = - serviceFor(SyncEchoService.class, SampleConfig.HANDLER_A_1_ENDPOINT); - SyncEchoService handlerB1SyncService = - serviceFor(SyncEchoService.class, SampleConfig.HANDLER_B_1_ENDPOINT); - SyncEchoService handlerA2SyncService = - serviceFor(SyncEchoService.class, SampleConfig.HANDLER_A_2_ENDPOINT); - SyncEchoService handlerB2SyncService = - serviceFor(SyncEchoService.class, SampleConfig.HANDLER_B_2_ENDPOINT); - AsyncEchoService handlerA1AsyncService = - serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_A_1_ENDPOINT); - AsyncEchoService handlerB1AsyncService = - serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_B_1_ENDPOINT); - AsyncEchoService handlerA2AsyncService = - serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_A_2_ENDPOINT); - AsyncEchoService handlerB2AsyncService = - serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_B_2_ENDPOINT); + SyncEchoService compressedEncrypted1SyncService = + serviceFor(SyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT); + SyncEchoService encrypted1SyncService = + serviceFor(SyncEchoService.class, SampleConfig.ENCRYPTED_1_ENDPOINT); + SyncEchoService compressedEncrypted2SyncService = + serviceFor(SyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT); + SyncEchoService encrypted2SyncService = + serviceFor(SyncEchoService.class, SampleConfig.ENCRYPTED_2_ENDPOINT); + AsyncEchoService compressedEncrypted1AsyncService = + serviceFor(AsyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT); + AsyncEchoService encrypted1AsyncService = + serviceFor(AsyncEchoService.class, SampleConfig.ENCRYPTED_1_ENDPOINT); + AsyncEchoService compressedEncrypted2AsyncService = + serviceFor(AsyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT); + AsyncEchoService encrypted2AsyncService = + serviceFor(AsyncEchoService.class, SampleConfig.ENCRYPTED_2_ENDPOINT); // Start all eight operations before awaiting results. Each keeps its endpoint context. - NexusOperationHandle handlerA1Sync = - Workflow.startNexusOperation(handlerA1SyncService::echo, message); - NexusOperationHandle handlerA1Async = - Workflow.startNexusOperation(handlerA1AsyncService::echoAsync, message); - NexusOperationHandle handlerB1Sync = - Workflow.startNexusOperation(handlerB1SyncService::echo, message); - NexusOperationHandle handlerB1Async = - Workflow.startNexusOperation(handlerB1AsyncService::echoAsync, message); - NexusOperationHandle handlerA2Sync = - Workflow.startNexusOperation(handlerA2SyncService::echo, message); - NexusOperationHandle handlerA2Async = - Workflow.startNexusOperation(handlerA2AsyncService::echoAsync, message); - NexusOperationHandle handlerB2Sync = - Workflow.startNexusOperation(handlerB2SyncService::echo, message); - NexusOperationHandle handlerB2Async = - Workflow.startNexusOperation(handlerB2AsyncService::echoAsync, message); + NexusOperationHandle compressedEncrypted1Sync = + Workflow.startNexusOperation(compressedEncrypted1SyncService::echo, message); + NexusOperationHandle compressedEncrypted1Async = + Workflow.startNexusOperation(compressedEncrypted1AsyncService::echoAsync, message); + NexusOperationHandle encrypted1Sync = + Workflow.startNexusOperation(encrypted1SyncService::echo, message); + NexusOperationHandle encrypted1Async = + Workflow.startNexusOperation(encrypted1AsyncService::echoAsync, message); + NexusOperationHandle compressedEncrypted2Sync = + Workflow.startNexusOperation(compressedEncrypted2SyncService::echo, message); + NexusOperationHandle compressedEncrypted2Async = + Workflow.startNexusOperation(compressedEncrypted2AsyncService::echoAsync, message); + NexusOperationHandle encrypted2Sync = + Workflow.startNexusOperation(encrypted2SyncService::echo, message); + NexusOperationHandle encrypted2Async = + Workflow.startNexusOperation(encrypted2AsyncService::echoAsync, message); return new EndpointResults( - handlerA1Sync.getResult().get(), - handlerB1Sync.getResult().get(), - handlerA2Sync.getResult().get(), - handlerB2Sync.getResult().get(), - handlerA1Async.getResult().get(), - handlerB1Async.getResult().get(), - handlerA2Async.getResult().get(), - handlerB2Async.getResult().get()); + compressedEncrypted1Sync.getResult().get(), + encrypted1Sync.getResult().get(), + compressedEncrypted2Sync.getResult().get(), + encrypted2Sync.getResult().get(), + compressedEncrypted1Async.getResult().get(), + encrypted1Async.getResult().get(), + compressedEncrypted2Async.getResult().get(), + encrypted2Async.getResult().get()); } private static T serviceFor(Class serviceClass, String endpoint) { diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/EndpointResults.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/EndpointResults.java index 7d1910bd..ff729460 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/EndpointResults.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/EndpointResults.java @@ -1,11 +1,11 @@ package io.temporal.samples.nexusserializationcontext.caller; public record EndpointResults( - String handlerA1SyncResult, - String handlerB1SyncResult, - String handlerA2SyncResult, - String handlerB2SyncResult, - String handlerA1AsyncResult, - String handlerB1AsyncResult, - String handlerA2AsyncResult, - String handlerB2AsyncResult) {} + String compressedEncrypted1SyncResult, + String encrypted1SyncResult, + String compressedEncrypted2SyncResult, + String encrypted2SyncResult, + String compressedEncrypted1AsyncResult, + String encrypted1AsyncResult, + String compressedEncrypted2AsyncResult, + String encrypted2AsyncResult) {} diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java index 2112282b..27819b84 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/CompressedEncryptedHandlerWorker.java @@ -21,7 +21,7 @@ import javax.crypto.spec.SecretKeySpec; public class CompressedEncryptedHandlerWorker { - private static final String TASK_QUEUE = "nexus-serialization-handler-a"; + private static final String TASK_QUEUE = "nexus-serialization-compressed-encrypted-handler"; public static void main(String[] args) { WorkflowClient client = @@ -37,11 +37,13 @@ public static void main(String[] args) { public static DataConverter dataConverter() { SecretKey key1 = - new SecretKeySpec(SampleConfig.HANDLER_A_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); + new SecretKeySpec( + SampleConfig.COMPRESSED_ENCRYPTED_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); SecretKey key2 = - new SecretKeySpec(SampleConfig.HANDLER_A_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); - PayloadCodec key1Encryption = new AesGcmCodec(SampleConfig.HANDLER_A_KEY_1_ID, key1); - PayloadCodec key2Encryption = new AesGcmCodec(SampleConfig.HANDLER_A_KEY_2_ID, key2); + new SecretKeySpec( + SampleConfig.COMPRESSED_ENCRYPTED_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); + PayloadCodec key1Encryption = new AesGcmCodec(SampleConfig.COMPRESSED_ENCRYPTED_KEY_1_ID, key1); + PayloadCodec key2Encryption = new AesGcmCodec(SampleConfig.COMPRESSED_ENCRYPTED_KEY_2_ID, key2); // ChainCodec encodes last to first: compress, then encrypt. PayloadCodec compressedKey1Encryption = new ChainCodec(List.of(key1Encryption, new ZlibCodec())); @@ -49,8 +51,8 @@ public static DataConverter dataConverter() { new ChainCodec(List.of(key2Encryption, new ZlibCodec())); Map nexusEndpointCodecs = Map.of( - SampleConfig.HANDLER_A_1_ENDPOINT, compressedKey1Encryption, - SampleConfig.HANDLER_A_2_ENDPOINT, compressedKey2Encryption); + SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT, compressedKey1Encryption, + SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT, compressedKey2Encryption); // Ordinary workflow inputs use this handler's key 1, regardless of the Nexus endpoint. PayloadCodec handlerWorkflowEncryption = key1Encryption; return new CodecDataConverter( diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java index 115ba8c7..e31bd842 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/handler/EncryptedHandlerWorker.java @@ -19,7 +19,7 @@ import javax.crypto.spec.SecretKeySpec; public class EncryptedHandlerWorker { - private static final String TASK_QUEUE = "nexus-serialization-handler-b"; + private static final String TASK_QUEUE = "nexus-serialization-encrypted-handler"; public static void main(String[] args) { WorkflowClient client = @@ -35,15 +35,15 @@ public static void main(String[] args) { public static DataConverter dataConverter() { SecretKey key1 = - new SecretKeySpec(SampleConfig.HANDLER_B_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); + new SecretKeySpec(SampleConfig.ENCRYPTED_KEY_1.getBytes(StandardCharsets.UTF_8), "AES"); SecretKey key2 = - new SecretKeySpec(SampleConfig.HANDLER_B_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); - PayloadCodec key1Encryption = new AesGcmCodec(SampleConfig.HANDLER_B_KEY_1_ID, key1); - PayloadCodec key2Encryption = new AesGcmCodec(SampleConfig.HANDLER_B_KEY_2_ID, key2); + new SecretKeySpec(SampleConfig.ENCRYPTED_KEY_2.getBytes(StandardCharsets.UTF_8), "AES"); + PayloadCodec key1Encryption = new AesGcmCodec(SampleConfig.ENCRYPTED_KEY_1_ID, key1); + PayloadCodec key2Encryption = new AesGcmCodec(SampleConfig.ENCRYPTED_KEY_2_ID, key2); Map nexusEndpointCodecs = Map.of( - SampleConfig.HANDLER_B_1_ENDPOINT, key1Encryption, - SampleConfig.HANDLER_B_2_ENDPOINT, key2Encryption); + SampleConfig.ENCRYPTED_1_ENDPOINT, key1Encryption, + SampleConfig.ENCRYPTED_2_ENDPOINT, key2Encryption); // Ordinary workflow inputs use this handler's key 1, regardless of the Nexus endpoint. PayloadCodec handlerWorkflowEncryption = key1Encryption; return new CodecDataConverter( diff --git a/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java b/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java index 17215498..a4abea69 100644 --- a/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java +++ b/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java @@ -22,42 +22,46 @@ class NexusCodecTest { @Test void callerAndHandlersUseTheKeyForEachEndpoint() { - DataConverter handlerA = CompressedEncryptedHandlerWorker.dataConverter(); - DataConverter handlerB = EncryptedHandlerWorker.dataConverter(); + DataConverter compressedEncryptedHandler = CompressedEncryptedHandlerWorker.dataConverter(); + DataConverter encryptedHandler = EncryptedHandlerWorker.dataConverter(); assertEndpointRoundTrip( - handlerA, SampleConfig.HANDLER_A_1_ENDPOINT, SampleConfig.HANDLER_A_KEY_1_ID); + compressedEncryptedHandler, + SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT, + SampleConfig.COMPRESSED_ENCRYPTED_KEY_1_ID); assertEndpointRoundTrip( - handlerA, SampleConfig.HANDLER_A_2_ENDPOINT, SampleConfig.HANDLER_A_KEY_2_ID); + compressedEncryptedHandler, + SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT, + SampleConfig.COMPRESSED_ENCRYPTED_KEY_2_ID); assertEndpointRoundTrip( - handlerB, SampleConfig.HANDLER_B_1_ENDPOINT, SampleConfig.HANDLER_B_KEY_1_ID); + encryptedHandler, SampleConfig.ENCRYPTED_1_ENDPOINT, SampleConfig.ENCRYPTED_KEY_1_ID); assertEndpointRoundTrip( - handlerB, SampleConfig.HANDLER_B_2_ENDPOINT, SampleConfig.HANDLER_B_KEY_2_ID); + encryptedHandler, SampleConfig.ENCRYPTED_2_ENDPOINT, SampleConfig.ENCRYPTED_KEY_2_ID); } @Test - void handlerACompressesBeforeEncryptingWithEitherKey() { + void compressesBeforeEncryptingWithEitherKey() { assertCompressionReducesPayload( - SampleConfig.HANDLER_A_1_ENDPOINT, SampleConfig.HANDLER_B_1_ENDPOINT); + SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT, SampleConfig.ENCRYPTED_1_ENDPOINT); assertCompressionReducesPayload( - SampleConfig.HANDLER_A_2_ENDPOINT, SampleConfig.HANDLER_B_2_ENDPOINT); + SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT, SampleConfig.ENCRYPTED_2_ENDPOINT); } @Test - void callerWorkflowPayloadsReuseHandlerAKey1() { + void callerWorkflowPayloadsUseTheirOwnKey() { DataConverter converter = CallerWorker.dataConverter() .withContext(new WorkflowSerializationContext("nexus-serialization", "caller-id")); EndpointResults results = new EndpointResults( - "reply A1", - "reply B1", - "reply A2", - "reply B2", - "async reply A1", - "async reply B1", - "async reply A2", - "async reply B2"); + "reply compressed-encrypted-1", + "reply encrypted-1", + "reply compressed-encrypted-2", + "reply encrypted-2", + "async reply compressed-encrypted-1", + "async reply encrypted-1", + "async reply compressed-encrypted-2", + "async reply encrypted-2"); Payload input = converter.toPayload("Hello from Nexus").orElseThrow(); Payload result = converter.toPayload(results).orElseThrow(); @@ -65,7 +69,10 @@ void callerWorkflowPayloadsReuseHandlerAKey1() { NexusEncoding.AES_GCM.encodingName(), input.getMetadataOrThrow(EncodingKeys.METADATA_ENCODING_KEY).toStringUtf8()); assertEquals( - SampleConfig.HANDLER_A_KEY_1_ID, + SampleConfig.CALLER_WORKFLOW_KEY_ID, + input.getMetadataOrThrow(AesGcmCodec.KEY_ID_METADATA_KEY).toStringUtf8()); + assertEquals( + SampleConfig.CALLER_WORKFLOW_KEY_ID, result.getMetadataOrThrow(AesGcmCodec.KEY_ID_METADATA_KEY).toStringUtf8()); assertFalse(input.containsMetadata(SampleConfig.ENDPOINT_METADATA_KEY)); assertFalse(result.containsMetadata(SampleConfig.ENDPOINT_METADATA_KEY)); From 6bc47331950b4a4e86eb63ce91986b35138ee017 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Fri, 13 Nov 2026 14:56:00 -0800 Subject: [PATCH 5/6] Restore separate namespaces for the Nexus caller and handlers Keep each handler in its own namespace using descriptive encoding names. Update local setup commands, the namespace table, and caller test context. --- .../nexusserializationcontext/README.md | 33 ++++++++++++------- .../codec/NexusCodecTest.java | 3 +- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md index 454a2dad..a6a1d2b1 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md @@ -6,6 +6,7 @@ through four endpoints. `CompressedEncryptedHandlerWorker` compresses and then encrypts Nexus payloads. `EncryptedHandlerWorker` encrypts without compression. Each handler selects between its own key 1 and key 2 using the endpoint in the Nexus context. +The caller and each handler run in separate namespaces. | Endpoint | Encryption key | Nexus payload encoding | |---|---|---| @@ -73,13 +74,23 @@ Keep this prerelease binary separate from your stable CLI. The commands below us ## Run locally -The caller and both handlers share the `nexus-serialization` namespace. Each worker -polls its own task queue; both endpoints for a handler target that handler's queue. +The caller and each handler use separate namespaces. Each worker polls a task queue +with the same name as its namespace. Both endpoints for a handler target that handler's +namespace and task queue. + +| Worker | Namespace and task queue | +|---|---| +| Caller | `nexus-serialization-caller` | +| Compressed/encrypted handler | `nexus-serialization-compressed-encrypted-handler` | +| Encrypted handler | `nexus-serialization-encrypted-handler` | Start the dev server: ```bash -./temporal server start-dev --namespace nexus-serialization +./temporal server start-dev \ + --namespace nexus-serialization-caller \ + --namespace nexus-serialization-compressed-encrypted-handler \ + --namespace nexus-serialization-encrypted-handler ``` Create two endpoints for each handler worker: @@ -87,19 +98,19 @@ Create two endpoints for each handler worker: ```bash ./temporal operator nexus endpoint create \ --name nexus-serialization-compressed-encrypted-1 \ - --target-namespace nexus-serialization \ + --target-namespace nexus-serialization-compressed-encrypted-handler \ --target-task-queue nexus-serialization-compressed-encrypted-handler ./temporal operator nexus endpoint create \ --name nexus-serialization-compressed-encrypted-2 \ - --target-namespace nexus-serialization \ + --target-namespace nexus-serialization-compressed-encrypted-handler \ --target-task-queue nexus-serialization-compressed-encrypted-handler ./temporal operator nexus endpoint create \ --name nexus-serialization-encrypted-1 \ - --target-namespace nexus-serialization \ + --target-namespace nexus-serialization-encrypted-handler \ --target-task-queue nexus-serialization-encrypted-handler ./temporal operator nexus endpoint create \ --name nexus-serialization-encrypted-2 \ - --target-namespace nexus-serialization \ + --target-namespace nexus-serialization-encrypted-handler \ --target-task-queue nexus-serialization-encrypted-handler ``` @@ -107,22 +118,22 @@ Run each of the following in its own terminal from the repository root: ```bash ./gradlew -q :core:execute -PmainClass=io.temporal.samples.nexusserializationcontext.handler.CompressedEncryptedHandlerWorker \ - --args="-namespace nexus-serialization" + --args="-namespace nexus-serialization-compressed-encrypted-handler" ``` ```bash ./gradlew -q :core:execute -PmainClass=io.temporal.samples.nexusserializationcontext.handler.EncryptedHandlerWorker \ - --args="-namespace nexus-serialization" + --args="-namespace nexus-serialization-encrypted-handler" ``` ```bash ./gradlew -q :core:execute -PmainClass=io.temporal.samples.nexusserializationcontext.caller.CallerWorker \ - --args="-namespace nexus-serialization" + --args="-namespace nexus-serialization-caller" ``` ```bash ./gradlew -q :core:execute -PmainClass=io.temporal.samples.nexusserializationcontext.caller.CallerStarter \ - --args="-namespace nexus-serialization" + --args="-namespace nexus-serialization-caller" ``` The starter prints a sync result and an async result for each endpoint. All eight diff --git a/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java b/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java index a4abea69..27583bb3 100644 --- a/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java +++ b/core/src/test/java/io/temporal/samples/nexusserializationcontext/codec/NexusCodecTest.java @@ -51,7 +51,8 @@ void compressesBeforeEncryptingWithEitherKey() { void callerWorkflowPayloadsUseTheirOwnKey() { DataConverter converter = CallerWorker.dataConverter() - .withContext(new WorkflowSerializationContext("nexus-serialization", "caller-id")); + .withContext( + new WorkflowSerializationContext("nexus-serialization-caller", "caller-id")); EndpointResults results = new EndpointResults( "reply compressed-encrypted-1", From 60d3a4d3872e9a736acb70dc53b01970a8255a09 Mon Sep 17 00:00:00 2001 From: Joie Wu Date: Fri, 13 Nov 2026 16:02:45 -0800 Subject: [PATCH 6/6] Show handler ownership in the Nexus endpoint table --- .../samples/nexusserializationcontext/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md index a6a1d2b1..cc668463 100644 --- a/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md +++ b/core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md @@ -8,12 +8,12 @@ compression. Each handler selects between its own key 1 and key 2 using the endpoint in the Nexus context. The caller and each handler run in separate namespaces. -| Endpoint | Encryption key | Nexus payload encoding | -|---|---|---| -| `nexus-serialization-compressed-encrypted-1` | `COMPRESSED_ENCRYPTED_KEY_1` | zlib, then AES-GCM | -| `nexus-serialization-encrypted-1` | `ENCRYPTED_KEY_1` | AES-GCM | -| `nexus-serialization-compressed-encrypted-2` | `COMPRESSED_ENCRYPTED_KEY_2` | zlib, then AES-GCM | -| `nexus-serialization-encrypted-2` | `ENCRYPTED_KEY_2` | AES-GCM | +| Handler | Endpoint | Encryption key | Nexus payload encoding | +|---|---|---|---| +| `CompressedEncryptedHandlerWorker` | `nexus-serialization-compressed-encrypted-1` | `COMPRESSED_ENCRYPTED_KEY_1` | zlib, then AES-GCM | +| `CompressedEncryptedHandlerWorker` | `nexus-serialization-compressed-encrypted-2` | `COMPRESSED_ENCRYPTED_KEY_2` | zlib, then AES-GCM | +| `EncryptedHandlerWorker` | `nexus-serialization-encrypted-1` | `ENCRYPTED_KEY_1` | AES-GCM | +| `EncryptedHandlerWorker` | `nexus-serialization-encrypted-2` | `ENCRYPTED_KEY_2` | AES-GCM | These four endpoint keys are distinct 128-bit AES keys. Compression runs before encryption; decoding decrypts before decompressing. AES-GCM authenticates the ciphertext.