Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
arize_tracing \
bedrock_agentcore/strands_agent \
lambda_worker \
nexus_serialization_context \
microsoft_agent_framework_plugin/mcp \
openai_agents/mcp_v2
do
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Some examples require extra dependencies. See each sample's directory for specif
* [nexus_messaging_temporal_operation](nexus_messaging_temporal_operation): Demonstrates how to send signal, update and query messages through Nexus.
This version uses `@nexus.temporal_operation` to allow Nexus operation handlers to either return a synchronous result or start a Workflow as the async backing operation.
* [nexus_multiple_args](nexus_multiple_args) - Map a Nexus operation to a handler workflow that takes multiple arguments.
* [nexus_serialization_context](nexus_serialization_context) - Select encryption keys and compression by endpoint for synchronous and workflow-backed asynchronous Nexus operations.
* [nexus_standalone_activity](nexus_standalone_activity) - Back a Nexus operation with a standalone Activity.
* [nexus_standalone_operations](nexus_standalone_operations) - Execute Nexus operations directly from client code,
without wrapping them in a workflow.
Expand Down
192 changes: 192 additions & 0 deletions nexus_serialization_context/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Nexus serialization context

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. The `compressed-encrypted` worker compresses and then encrypts
Nexus payloads. The `encrypted` worker 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.

| Handler worker | Endpoint | Key ID | Nexus payload encoding |
|---|---|---|---|
| Compressed/encrypted | `nexus-serialization-compressed-encrypted-1` | `compressed-encrypted-1` | zlib, then AES-GCM |
| Compressed/encrypted | `nexus-serialization-compressed-encrypted-2` | `compressed-encrypted-2` | zlib, then AES-GCM |
| Encrypted | `nexus-serialization-encrypted-1` | `encrypted-1` | AES-GCM |
| Encrypted | `nexus-serialization-encrypted-2` | `encrypted-2` | AES-GCM |

Each endpoint uses a different 128-bit AES key. Compression runs before encryption,
and decoding decrypts before decompressing. AES-GCM authenticates the ciphertext.
The encryption codec uses `cryptography`'s AES-GCM implementation and Python's `zlib` module.
Nexus supplies the endpoint context used to select a codec. The context also identifies
the service and operation, which can be used to select keys.

## Adapt this pattern

`NexusCodec` maps endpoints to codecs and uses a default codec for ordinary workflow
payloads. Set it as the `payload_codec` of a `DataConverter`, then pass the converter to
`Client.connect` using `data_converter` on the caller and handler clients. See [config.py](config.py) for the key maps and [worker.py](worker.py) for client setup.

Because AES-GCM uses symmetric encryption, the caller and handler share a secret key
for each endpoint. This sample uses that key for both requests and results.
Both sides also need the same payload format, including whether to compress.

The caller uses a separate key for its workflow input and final result.
For this example, each handler reuses its endpoint-1 key for backing workflow inputs.

When adapting this sample, pass your existing workflow codec as the `workflow_codec`
argument to [`NexusCodec`](codec.py) so workers can still read existing histories.

| Ordinary workflow payload | Default key ID |
|---|---|
| Caller workflow input and final result | `caller-workflow` |
| Compressed/encrypted handler's backing workflow inputs | `compressed-encrypted-1` |
| Encrypted handler's backing workflow inputs | `encrypted-1` |

These workflow payloads use AES-GCM without compression or endpoint metadata.

On decode, the AES-GCM codec passes other encodings through for ordinary decoding.
For AES-GCM payloads, it checks the key ID against the codec selected by context and
verifies the ciphertext before returning the decoded value.

The SDK supplies Nexus context for operation requests and results. For workflow-backed
async operations, the server stores the context in workflow history and the SDK uses it
to select the result's key and compression. To see the two keys in one execution, call
`nexus-serialization-encrypted-2` and check the backing workflow's history. Its input
records `encrypted-1` and its result records `encrypted-2`.

The caller starts all eight operations before waiting for their results. Each operation
keeps its own endpoint context while calls using different keys run concurrently.
`NexusCodec.with_context` returns a copy so binding one operation does not change another.
No custom interceptor or workflow-header propagation is required.

Each workflow execution has one serialized result, shared by all Nexus operations
attached to it. Operations that need results encrypted with different keys must use
separate workflow executions. This sample starts one backing workflow per async
operation, using the Nexus request ID as its workflow ID suffix. Retrying the same
request reuses that workflow ID.

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: compressed-encrypted-1
nexus-endpoint-name: nexus-serialization-compressed-encrypted-1
```

The metadata identifies the key and endpoint. The key material stays in the codec
configuration.

The AES-GCM codec and hard-coded keys are for this example. For production, use an
established encryption implementation and secure key storage.

## Prerequisites

Use Python 3.11 or newer, [uv](https://docs.astral.sh/uv/), and Temporal Python SDK
1.36.0 or later. This sample has its own uv project and lockfile.

Use a Temporal Server that supports Nexus serialization-context propagation for
workflow-backed asynchronous operations. For the local walkthrough, download the prerelease
[Temporal CLI `v1.9.3-nexus-serialization-context`](https://github.com/temporalio/cli/releases/tag/v1.9.3-nexus-serialization-context).
Extract it and place the `temporal` executable in the repository root. The commands below
use `../temporal` to run this build separately from your installed CLI.

## Run locally

From the repository root, enter the sample directory and install its dependencies:

```shell
cd nexus_serialization_context
uv sync
../temporal --version
```

The CLI version should be `1.9.3-nexus-serialization-context`. Run the remaining
commands from the `nexus_serialization_context` folder, including in new terminals.

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 in its own terminal:

```shell
../temporal server start-dev \
--namespace nexus-serialization-caller \
--namespace nexus-serialization-compressed-encrypted-handler \
--namespace nexus-serialization-encrypted-handler
```

In another terminal, create two endpoints for each handler worker:

```shell
../temporal operator nexus endpoint create \
--name nexus-serialization-compressed-encrypted-1 \
--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-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-encrypted-handler \
--target-task-queue nexus-serialization-encrypted-handler
../temporal operator nexus endpoint create \
--name nexus-serialization-encrypted-2 \
--target-namespace nexus-serialization-encrypted-handler \
--target-task-queue nexus-serialization-encrypted-handler
```

Run each worker in its own terminal:

```shell
uv run worker.py compressed-encrypted
```

```shell
uv run worker.py encrypted
```

```shell
uv run worker.py caller
```

Start the caller workflow in another terminal:

```shell
uv run starter.py
```

### Output

Expected output:

```text
Started workflow: ID=nexus-serialization-<workflow UUID> RunID=<run UUID>
nexus-serialization-compressed-encrypted-1: sync="Hello from Nexus" async="Hello from Nexus"
nexus-serialization-compressed-encrypted-2: sync="Hello from Nexus" async="Hello from Nexus"
nexus-serialization-encrypted-1: sync="Hello from Nexus" async="Hello from Nexus"
nexus-serialization-encrypted-2: sync="Hello from Nexus" async="Hello from Nexus"
```

All eight results should be `Hello from Nexus`.

The run creates five workflow executions: one caller and four async backing workflows.
The four sync operations return directly from their handlers.

## Test

Run the codec and workflow tests:

```shell
uv run poe test
```

The workflow test starts its own server and checks all eight results, the stored Nexus
context, and the backing workflows' input and result encryption metadata.
110 changes: 110 additions & 0 deletions nexus_serialization_context/codec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import copy
import os
import zlib
from collections.abc import Mapping, Sequence
from types import MappingProxyType
from typing import Self

from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from temporalio.api.common.v1 import Payload
from temporalio.converter import (
NexusSerializationContext,
PayloadCodec,
SerializationContext,
WithSerializationContext,
)


class NexusCodec(PayloadCodec, WithSerializationContext):
"""Select an endpoint's codec, with a default for ordinary workflow payloads."""

def __init__(
self, endpoint_codecs: Mapping[str, PayloadCodec], workflow_codec: PayloadCodec
) -> None:
self._endpoint_codecs = MappingProxyType(dict(endpoint_codecs))
self._workflow_codec = workflow_codec
self._selected_codec = workflow_codec

def with_context(self, context: SerializationContext) -> Self:
selected = self._workflow_codec
if isinstance(context, NexusSerializationContext):
try:
selected = self._endpoint_codecs[context.endpoint]
except KeyError:
raise ValueError(
f"Unknown Nexus endpoint: {context.endpoint}"
) from None
if isinstance(selected, WithSerializationContext):
selected = selected.with_context(context)
# Keep each operation's selection separate and share the read-only configuration.
bound = copy.copy(self)
bound._selected_codec = selected
return bound

async def encode(self, payloads: Sequence[Payload]) -> list[Payload]:
return await self._selected_codec.encode(payloads)

async def decode(self, payloads: Sequence[Payload]) -> list[Payload]:
return await self._selected_codec.decode(payloads)


class EncryptionCodec(PayloadCodec, WithSerializationContext):
"""Encrypt the whole payload so its encoding metadata is restored on decode."""

def __init__(self, key_id: str, key: bytes, *, compress: bool = False) -> None:
self._key_id = key_id
self._encryptor = AESGCM(key)
self._compress = compress
self._endpoint: str | None = None

def with_context(self, context: SerializationContext) -> Self:
bound = copy.copy(self)
bound._endpoint = (
context.endpoint if isinstance(context, NexusSerializationContext) else None
)
return bound

async def encode(self, payloads: Sequence[Payload]) -> list[Payload]:
encoded = []
for payload in payloads:
if self._compress:
payload = Payload(
metadata={"encoding": b"binary/nexus-zlib"},
data=zlib.compress(payload.SerializeToString()),
)
# Store a fresh public 12-byte nonce before the ciphertext and 16-byte tag.
nonce = os.urandom(12)
metadata = {
"encoding": b"binary/nexus-aes-gcm",
"encryption-key-id": self._key_id.encode(),
}
if self._endpoint:
metadata["nexus-endpoint-name"] = self._endpoint.encode()
encoded.append(
Payload(
metadata=metadata,
data=nonce
+ self._encryptor.encrypt(nonce, payload.SerializeToString(), None),
)
)
return encoded

async def decode(self, payloads: Sequence[Payload]) -> list[Payload]:
decoded = []
for payload in payloads:
if payload.metadata.get("encoding") != b"binary/nexus-aes-gcm":
decoded.append(payload)
continue
if payload.metadata.get("encryption-key-id") != self._key_id.encode():
raise ValueError("Unexpected encryption key ID")
if len(payload.data) < 28:
raise ValueError("AES-GCM payload is too short")
plaintext = Payload.FromString(
self._encryptor.decrypt(payload.data[:12], payload.data[12:], None)
)
if self._compress:
if plaintext.metadata.get("encoding") != b"binary/nexus-zlib":
raise ValueError("Expected a Nexus zlib payload")
plaintext = Payload.FromString(zlib.decompress(plaintext.data))
decoded.append(plaintext)
return decoded
65 changes: 65 additions & 0 deletions nexus_serialization_context/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from temporalio.converter import DataConverter

from codec import EncryptionCodec, NexusCodec
from service import (
COMPRESSED_ENCRYPTED_1_ENDPOINT,
COMPRESSED_ENCRYPTED_2_ENDPOINT,
ENCRYPTED_1_ENDPOINT,
ENCRYPTED_2_ENDPOINT,
)

# These keys are for this local example. Load production keys from secure storage.
COMPRESSED_ENCRYPTED_KEY_1 = b"handler-a-key-01"
COMPRESSED_ENCRYPTED_KEY_2 = b"handler-a-key-02"
ENCRYPTED_KEY_1 = b"handler-b-key-01"
ENCRYPTED_KEY_2 = b"handler-b-key-02"
CALLER_WORKFLOW_KEY = b"sample-key-C-123"


def _compressed_encrypted_codecs() -> dict[str, EncryptionCodec]:
return {
COMPRESSED_ENCRYPTED_1_ENDPOINT: EncryptionCodec(
"compressed-encrypted-1", COMPRESSED_ENCRYPTED_KEY_1, compress=True
),
COMPRESSED_ENCRYPTED_2_ENDPOINT: EncryptionCodec(
"compressed-encrypted-2", COMPRESSED_ENCRYPTED_KEY_2, compress=True
),
}


def _encrypted_codecs() -> dict[str, EncryptionCodec]:
return {
ENCRYPTED_1_ENDPOINT: EncryptionCodec("encrypted-1", ENCRYPTED_KEY_1),
ENCRYPTED_2_ENDPOINT: EncryptionCodec("encrypted-2", ENCRYPTED_KEY_2),
}


def caller_data_converter() -> DataConverter:
return DataConverter(
payload_codec=NexusCodec(
endpoint_codecs={**_compressed_encrypted_codecs(), **_encrypted_codecs()},
workflow_codec=EncryptionCodec("caller-workflow", CALLER_WORKFLOW_KEY),
)
)


# In this sample, each handler's workflow_codec reuses its endpoint-1 key for
# backing workflow inputs. Nexus results still use the called endpoint's codec.
def compressed_encrypted_data_converter() -> DataConverter:
return DataConverter(
payload_codec=NexusCodec(
endpoint_codecs=_compressed_encrypted_codecs(),
workflow_codec=EncryptionCodec(
"compressed-encrypted-1", COMPRESSED_ENCRYPTED_KEY_1
),
)
)


def encrypted_data_converter() -> DataConverter:
return DataConverter(
payload_codec=NexusCodec(
endpoint_codecs=_encrypted_codecs(),
workflow_codec=EncryptionCodec("encrypted-1", ENCRYPTED_KEY_1),
)
)
Loading
Loading