Skip to content

Commit e2e8ca8

Browse files
committed
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.
1 parent fbf43c2 commit e2e8ca8

9 files changed

Lines changed: 177 additions & 150 deletions

File tree

‎core/src/main/java/io/temporal/samples/nexusserializationcontext/README.md‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,33 @@
22

33
This sample uses `NexusSerializationContext` to select an encryption key for each Nexus
44
endpoint. The caller invokes synchronous and workflow-backed asynchronous operations
5-
through four endpoints. Handler A (`CompressedEncryptedHandlerWorker`) compresses and
6-
then encrypts Nexus payloads. Handler B (`EncryptedHandlerWorker`) encrypts without
5+
through four endpoints. `CompressedEncryptedHandlerWorker` compresses and then
6+
encrypts Nexus payloads. `EncryptedHandlerWorker` encrypts without
77
compression. Each handler selects between its own key 1 and key 2 using
88
the endpoint in the Nexus context.
99

1010
| Endpoint | Encryption key | Nexus payload encoding |
1111
|---|---|---|
12-
| `handler-a-1` | `HANDLER_A_KEY_1` | zlib, then AES-GCM |
13-
| `handler-b-1` | `HANDLER_B_KEY_1` | AES-GCM |
14-
| `handler-a-2` | `HANDLER_A_KEY_2` | zlib, then AES-GCM |
15-
| `handler-b-2` | `HANDLER_B_KEY_2` | AES-GCM |
12+
| `nexus-serialization-compressed-encrypted-1` | `COMPRESSED_ENCRYPTED_KEY_1` | zlib, then AES-GCM |
13+
| `nexus-serialization-encrypted-1` | `ENCRYPTED_KEY_1` | AES-GCM |
14+
| `nexus-serialization-compressed-encrypted-2` | `COMPRESSED_ENCRYPTED_KEY_2` | zlib, then AES-GCM |
15+
| `nexus-serialization-encrypted-2` | `ENCRYPTED_KEY_2` | AES-GCM |
1616

17-
These are four distinct 128-bit AES keys. Compression runs before encryption;
17+
These four endpoint keys are distinct 128-bit AES keys. Compression runs before encryption;
1818
decoding decrypts before decompressing. AES-GCM authenticates the ciphertext.
1919
The AES-GCM and zlib codecs are implemented in this sample; Nexus supplies the endpoint
2020
context used to select them.
2121

2222
Each worker configures `NexusCodec` with an endpoint-to-codec map and a default codec
23-
for ordinary workflow payloads. The sample reuses key 1 for workflow payloads
24-
to keep the example to four keys:
23+
for ordinary workflow payloads. The caller uses a separate `CALLER_WORKFLOW_KEY` for
24+
its own input and final result, keeping those payloads independent of the keys shared
25+
with Nexus handlers. Each handler reuses its key 1 for backing workflow inputs.
2526

2627
| Ordinary workflow payload | Encryption key |
2728
|---|---|
28-
| Caller workflow input and final result | `HANDLER_A_KEY_1` |
29-
| Handler A backing workflow inputs | `HANDLER_A_KEY_1` |
30-
| Handler B backing workflow inputs | `HANDLER_B_KEY_1` |
29+
| Caller workflow input and final result | `CALLER_WORKFLOW_KEY` |
30+
| Compressed/encrypted handler workflow inputs | `COMPRESSED_ENCRYPTED_KEY_1` |
31+
| Encrypted handler workflow inputs | `ENCRYPTED_KEY_1` |
3132

3233
These workflow payloads use AES-GCM without compression or endpoint metadata.
3334
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:
5152

5253
```text
5354
encoding: binary/nexus-aes-gcm
54-
encryption-key-id: handler-a-1
55-
nexus-endpoint-name: handler-a-1
55+
encryption-key-id: compressed-encrypted-1
56+
nexus-endpoint-name: nexus-serialization-compressed-encrypted-1
5657
```
5758

5859
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:
8586

8687
```bash
8788
./temporal operator nexus endpoint create \
88-
--name handler-a-1 \
89+
--name nexus-serialization-compressed-encrypted-1 \
8990
--target-namespace nexus-serialization \
90-
--target-task-queue nexus-serialization-handler-a
91+
--target-task-queue nexus-serialization-compressed-encrypted-handler
9192
./temporal operator nexus endpoint create \
92-
--name handler-a-2 \
93+
--name nexus-serialization-compressed-encrypted-2 \
9394
--target-namespace nexus-serialization \
94-
--target-task-queue nexus-serialization-handler-a
95+
--target-task-queue nexus-serialization-compressed-encrypted-handler
9596
./temporal operator nexus endpoint create \
96-
--name handler-b-1 \
97+
--name nexus-serialization-encrypted-1 \
9798
--target-namespace nexus-serialization \
98-
--target-task-queue nexus-serialization-handler-b
99+
--target-task-queue nexus-serialization-encrypted-handler
99100
./temporal operator nexus endpoint create \
100-
--name handler-b-2 \
101+
--name nexus-serialization-encrypted-2 \
101102
--target-namespace nexus-serialization \
102-
--target-task-queue nexus-serialization-handler-b
103+
--target-task-queue nexus-serialization-encrypted-handler
103104
```
104105

105106
Run each of the following in its own terminal from the repository root:
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
package io.temporal.samples.nexusserializationcontext;
22

33
public final class SampleConfig {
4-
public static final String HANDLER_A_1_ENDPOINT = "handler-a-1";
5-
public static final String HANDLER_B_1_ENDPOINT = "handler-b-1";
6-
public static final String HANDLER_A_2_ENDPOINT = "handler-a-2";
7-
public static final String HANDLER_B_2_ENDPOINT = "handler-b-2";
4+
public static final String COMPRESSED_ENCRYPTED_1_ENDPOINT =
5+
"nexus-serialization-compressed-encrypted-1";
6+
public static final String ENCRYPTED_1_ENDPOINT = "nexus-serialization-encrypted-1";
7+
public static final String COMPRESSED_ENCRYPTED_2_ENDPOINT =
8+
"nexus-serialization-compressed-encrypted-2";
9+
public static final String ENCRYPTED_2_ENDPOINT = "nexus-serialization-encrypted-2";
810
public static final String ENDPOINT_METADATA_KEY = "nexus-endpoint-name";
9-
public static final String HANDLER_A_KEY_1_ID = "handler-a-1";
10-
public static final String HANDLER_B_KEY_1_ID = "handler-b-1";
11-
public static final String HANDLER_A_KEY_2_ID = "handler-a-2";
12-
public static final String HANDLER_B_KEY_2_ID = "handler-b-2";
11+
public static final String COMPRESSED_ENCRYPTED_KEY_1_ID = "compressed-encrypted-1";
12+
public static final String ENCRYPTED_KEY_1_ID = "encrypted-1";
13+
public static final String COMPRESSED_ENCRYPTED_KEY_2_ID = "compressed-encrypted-2";
14+
public static final String ENCRYPTED_KEY_2_ID = "encrypted-2";
15+
public static final String CALLER_WORKFLOW_KEY_ID = "caller-workflow";
1316

14-
// Four distinct, hard-coded keys for this local sample only.
15-
public static final String HANDLER_A_KEY_1 = "handler-a-key-01";
16-
public static final String HANDLER_B_KEY_1 = "handler-b-key-01";
17-
public static final String HANDLER_A_KEY_2 = "handler-a-key-02";
18-
public static final String HANDLER_B_KEY_2 = "handler-b-key-02";
17+
// Hard-coded keys are only for this local sample.
18+
public static final String COMPRESSED_ENCRYPTED_KEY_1 = "handler-a-key-01";
19+
public static final String ENCRYPTED_KEY_1 = "handler-b-key-01";
20+
public static final String COMPRESSED_ENCRYPTED_KEY_2 = "handler-a-key-02";
21+
public static final String ENCRYPTED_KEY_2 = "handler-b-key-02";
22+
public static final String CALLER_WORKFLOW_KEY = "sample-key-C-123";
1923

2024
private SampleConfig() {}
2125
}

‎core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerStarter.java‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,28 @@ public static void main(String[] args) {
1919

2020
EndpointResults results = workflow.echoThroughEndpoints("Hello from Nexus");
2121
System.out.println(
22-
SampleConfig.HANDLER_A_1_ENDPOINT + " sync result: " + results.handlerA1SyncResult());
22+
SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT
23+
+ " sync result: "
24+
+ results.compressedEncrypted1SyncResult());
2325
System.out.println(
24-
SampleConfig.HANDLER_A_1_ENDPOINT + " async result: " + results.handlerA1AsyncResult());
26+
SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT
27+
+ " async result: "
28+
+ results.compressedEncrypted1AsyncResult());
2529
System.out.println(
26-
SampleConfig.HANDLER_B_1_ENDPOINT + " sync result: " + results.handlerB1SyncResult());
30+
SampleConfig.ENCRYPTED_1_ENDPOINT + " sync result: " + results.encrypted1SyncResult());
2731
System.out.println(
28-
SampleConfig.HANDLER_B_1_ENDPOINT + " async result: " + results.handlerB1AsyncResult());
32+
SampleConfig.ENCRYPTED_1_ENDPOINT + " async result: " + results.encrypted1AsyncResult());
2933
System.out.println(
30-
SampleConfig.HANDLER_A_2_ENDPOINT + " sync result: " + results.handlerA2SyncResult());
34+
SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT
35+
+ " sync result: "
36+
+ results.compressedEncrypted2SyncResult());
3137
System.out.println(
32-
SampleConfig.HANDLER_A_2_ENDPOINT + " async result: " + results.handlerA2AsyncResult());
38+
SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT
39+
+ " async result: "
40+
+ results.compressedEncrypted2AsyncResult());
3341
System.out.println(
34-
SampleConfig.HANDLER_B_2_ENDPOINT + " sync result: " + results.handlerB2SyncResult());
42+
SampleConfig.ENCRYPTED_2_ENDPOINT + " sync result: " + results.encrypted2SyncResult());
3543
System.out.println(
36-
SampleConfig.HANDLER_B_2_ENDPOINT + " async result: " + results.handlerB2AsyncResult());
44+
SampleConfig.ENCRYPTED_2_ENDPOINT + " async result: " + results.encrypted2AsyncResult());
3745
}
3846
}

‎core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorker.java‎

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,40 @@ public static void main(String[] args) {
3434
}
3535

3636
public static DataConverter dataConverter() {
37-
SecretKey handlerAKey1 =
38-
new SecretKeySpec(SampleConfig.HANDLER_A_KEY_1.getBytes(StandardCharsets.UTF_8), "AES");
39-
SecretKey handlerBKey1 =
40-
new SecretKeySpec(SampleConfig.HANDLER_B_KEY_1.getBytes(StandardCharsets.UTF_8), "AES");
41-
SecretKey handlerAKey2 =
42-
new SecretKeySpec(SampleConfig.HANDLER_A_KEY_2.getBytes(StandardCharsets.UTF_8), "AES");
43-
SecretKey handlerBKey2 =
44-
new SecretKeySpec(SampleConfig.HANDLER_B_KEY_2.getBytes(StandardCharsets.UTF_8), "AES");
45-
PayloadCodec handlerAKey1Encryption =
46-
new AesGcmCodec(SampleConfig.HANDLER_A_KEY_1_ID, handlerAKey1);
47-
PayloadCodec handlerBKey1Encryption =
48-
new AesGcmCodec(SampleConfig.HANDLER_B_KEY_1_ID, handlerBKey1);
49-
PayloadCodec handlerAKey2Encryption =
50-
new AesGcmCodec(SampleConfig.HANDLER_A_KEY_2_ID, handlerAKey2);
51-
PayloadCodec handlerBKey2Encryption =
52-
new AesGcmCodec(SampleConfig.HANDLER_B_KEY_2_ID, handlerBKey2);
37+
SecretKey compressedEncryptedKey1 =
38+
new SecretKeySpec(
39+
SampleConfig.COMPRESSED_ENCRYPTED_KEY_1.getBytes(StandardCharsets.UTF_8), "AES");
40+
SecretKey encryptedKey1 =
41+
new SecretKeySpec(SampleConfig.ENCRYPTED_KEY_1.getBytes(StandardCharsets.UTF_8), "AES");
42+
SecretKey compressedEncryptedKey2 =
43+
new SecretKeySpec(
44+
SampleConfig.COMPRESSED_ENCRYPTED_KEY_2.getBytes(StandardCharsets.UTF_8), "AES");
45+
SecretKey encryptedKey2 =
46+
new SecretKeySpec(SampleConfig.ENCRYPTED_KEY_2.getBytes(StandardCharsets.UTF_8), "AES");
47+
SecretKey callerWorkflowKey =
48+
new SecretKeySpec(SampleConfig.CALLER_WORKFLOW_KEY.getBytes(StandardCharsets.UTF_8), "AES");
49+
PayloadCodec compressedEncryptedKey1Encryption =
50+
new AesGcmCodec(SampleConfig.COMPRESSED_ENCRYPTED_KEY_1_ID, compressedEncryptedKey1);
51+
PayloadCodec encryptedKey1Encryption =
52+
new AesGcmCodec(SampleConfig.ENCRYPTED_KEY_1_ID, encryptedKey1);
53+
PayloadCodec compressedEncryptedKey2Encryption =
54+
new AesGcmCodec(SampleConfig.COMPRESSED_ENCRYPTED_KEY_2_ID, compressedEncryptedKey2);
55+
PayloadCodec encryptedKey2Encryption =
56+
new AesGcmCodec(SampleConfig.ENCRYPTED_KEY_2_ID, encryptedKey2);
5357
// ChainCodec encodes last to first: compress, then encrypt.
5458
Map<String, PayloadCodec> nexusEndpointCodecs =
5559
Map.of(
56-
SampleConfig.HANDLER_A_1_ENDPOINT,
57-
new ChainCodec(List.of(handlerAKey1Encryption, new ZlibCodec())),
58-
SampleConfig.HANDLER_B_1_ENDPOINT,
59-
handlerBKey1Encryption,
60-
SampleConfig.HANDLER_A_2_ENDPOINT,
61-
new ChainCodec(List.of(handlerAKey2Encryption, new ZlibCodec())),
62-
SampleConfig.HANDLER_B_2_ENDPOINT,
63-
handlerBKey2Encryption);
64-
// Reuse handler A's key 1 for the caller's workflow payloads.
65-
PayloadCodec callerWorkflowEncryption = handlerAKey1Encryption;
60+
SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT,
61+
new ChainCodec(List.of(compressedEncryptedKey1Encryption, new ZlibCodec())),
62+
SampleConfig.ENCRYPTED_1_ENDPOINT,
63+
encryptedKey1Encryption,
64+
SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT,
65+
new ChainCodec(List.of(compressedEncryptedKey2Encryption, new ZlibCodec())),
66+
SampleConfig.ENCRYPTED_2_ENDPOINT,
67+
encryptedKey2Encryption);
68+
// Ordinary caller workflow payloads use a separate key from Nexus payloads.
69+
PayloadCodec callerWorkflowEncryption =
70+
new AesGcmCodec(SampleConfig.CALLER_WORKFLOW_KEY_ID, callerWorkflowKey);
6671
return new CodecDataConverter(
6772
DefaultDataConverter.newDefaultInstance(),
6873
List.of(new NexusCodec(nexusEndpointCodecs, callerWorkflowEncryption)));

‎core/src/main/java/io/temporal/samples/nexusserializationcontext/caller/CallerWorkflowImpl.java‎

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,50 @@
1212
public class CallerWorkflowImpl implements CallerWorkflow {
1313
@Override
1414
public EndpointResults echoThroughEndpoints(String message) {
15-
SyncEchoService handlerA1SyncService =
16-
serviceFor(SyncEchoService.class, SampleConfig.HANDLER_A_1_ENDPOINT);
17-
SyncEchoService handlerB1SyncService =
18-
serviceFor(SyncEchoService.class, SampleConfig.HANDLER_B_1_ENDPOINT);
19-
SyncEchoService handlerA2SyncService =
20-
serviceFor(SyncEchoService.class, SampleConfig.HANDLER_A_2_ENDPOINT);
21-
SyncEchoService handlerB2SyncService =
22-
serviceFor(SyncEchoService.class, SampleConfig.HANDLER_B_2_ENDPOINT);
23-
AsyncEchoService handlerA1AsyncService =
24-
serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_A_1_ENDPOINT);
25-
AsyncEchoService handlerB1AsyncService =
26-
serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_B_1_ENDPOINT);
27-
AsyncEchoService handlerA2AsyncService =
28-
serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_A_2_ENDPOINT);
29-
AsyncEchoService handlerB2AsyncService =
30-
serviceFor(AsyncEchoService.class, SampleConfig.HANDLER_B_2_ENDPOINT);
15+
SyncEchoService compressedEncrypted1SyncService =
16+
serviceFor(SyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT);
17+
SyncEchoService encrypted1SyncService =
18+
serviceFor(SyncEchoService.class, SampleConfig.ENCRYPTED_1_ENDPOINT);
19+
SyncEchoService compressedEncrypted2SyncService =
20+
serviceFor(SyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT);
21+
SyncEchoService encrypted2SyncService =
22+
serviceFor(SyncEchoService.class, SampleConfig.ENCRYPTED_2_ENDPOINT);
23+
AsyncEchoService compressedEncrypted1AsyncService =
24+
serviceFor(AsyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_1_ENDPOINT);
25+
AsyncEchoService encrypted1AsyncService =
26+
serviceFor(AsyncEchoService.class, SampleConfig.ENCRYPTED_1_ENDPOINT);
27+
AsyncEchoService compressedEncrypted2AsyncService =
28+
serviceFor(AsyncEchoService.class, SampleConfig.COMPRESSED_ENCRYPTED_2_ENDPOINT);
29+
AsyncEchoService encrypted2AsyncService =
30+
serviceFor(AsyncEchoService.class, SampleConfig.ENCRYPTED_2_ENDPOINT);
3131

3232
// Start all eight operations before awaiting results. Each keeps its endpoint context.
33-
NexusOperationHandle<String> handlerA1Sync =
34-
Workflow.startNexusOperation(handlerA1SyncService::echo, message);
35-
NexusOperationHandle<String> handlerA1Async =
36-
Workflow.startNexusOperation(handlerA1AsyncService::echoAsync, message);
37-
NexusOperationHandle<String> handlerB1Sync =
38-
Workflow.startNexusOperation(handlerB1SyncService::echo, message);
39-
NexusOperationHandle<String> handlerB1Async =
40-
Workflow.startNexusOperation(handlerB1AsyncService::echoAsync, message);
41-
NexusOperationHandle<String> handlerA2Sync =
42-
Workflow.startNexusOperation(handlerA2SyncService::echo, message);
43-
NexusOperationHandle<String> handlerA2Async =
44-
Workflow.startNexusOperation(handlerA2AsyncService::echoAsync, message);
45-
NexusOperationHandle<String> handlerB2Sync =
46-
Workflow.startNexusOperation(handlerB2SyncService::echo, message);
47-
NexusOperationHandle<String> handlerB2Async =
48-
Workflow.startNexusOperation(handlerB2AsyncService::echoAsync, message);
33+
NexusOperationHandle<String> compressedEncrypted1Sync =
34+
Workflow.startNexusOperation(compressedEncrypted1SyncService::echo, message);
35+
NexusOperationHandle<String> compressedEncrypted1Async =
36+
Workflow.startNexusOperation(compressedEncrypted1AsyncService::echoAsync, message);
37+
NexusOperationHandle<String> encrypted1Sync =
38+
Workflow.startNexusOperation(encrypted1SyncService::echo, message);
39+
NexusOperationHandle<String> encrypted1Async =
40+
Workflow.startNexusOperation(encrypted1AsyncService::echoAsync, message);
41+
NexusOperationHandle<String> compressedEncrypted2Sync =
42+
Workflow.startNexusOperation(compressedEncrypted2SyncService::echo, message);
43+
NexusOperationHandle<String> compressedEncrypted2Async =
44+
Workflow.startNexusOperation(compressedEncrypted2AsyncService::echoAsync, message);
45+
NexusOperationHandle<String> encrypted2Sync =
46+
Workflow.startNexusOperation(encrypted2SyncService::echo, message);
47+
NexusOperationHandle<String> encrypted2Async =
48+
Workflow.startNexusOperation(encrypted2AsyncService::echoAsync, message);
4949

5050
return new EndpointResults(
51-
handlerA1Sync.getResult().get(),
52-
handlerB1Sync.getResult().get(),
53-
handlerA2Sync.getResult().get(),
54-
handlerB2Sync.getResult().get(),
55-
handlerA1Async.getResult().get(),
56-
handlerB1Async.getResult().get(),
57-
handlerA2Async.getResult().get(),
58-
handlerB2Async.getResult().get());
51+
compressedEncrypted1Sync.getResult().get(),
52+
encrypted1Sync.getResult().get(),
53+
compressedEncrypted2Sync.getResult().get(),
54+
encrypted2Sync.getResult().get(),
55+
compressedEncrypted1Async.getResult().get(),
56+
encrypted1Async.getResult().get(),
57+
compressedEncrypted2Async.getResult().get(),
58+
encrypted2Async.getResult().get());
5959
}
6060

6161
private static <T> T serviceFor(Class<T> serviceClass, String endpoint) {
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.temporal.samples.nexusserializationcontext.caller;
22

33
public record EndpointResults(
4-
String handlerA1SyncResult,
5-
String handlerB1SyncResult,
6-
String handlerA2SyncResult,
7-
String handlerB2SyncResult,
8-
String handlerA1AsyncResult,
9-
String handlerB1AsyncResult,
10-
String handlerA2AsyncResult,
11-
String handlerB2AsyncResult) {}
4+
String compressedEncrypted1SyncResult,
5+
String encrypted1SyncResult,
6+
String compressedEncrypted2SyncResult,
7+
String encrypted2SyncResult,
8+
String compressedEncrypted1AsyncResult,
9+
String encrypted1AsyncResult,
10+
String compressedEncrypted2AsyncResult,
11+
String encrypted2AsyncResult) {}

0 commit comments

Comments
 (0)