Skip to content

feat: decrypt temporal payloads with previous keys - #5430

Open
epipav wants to merge 3 commits into
mainfrom
feat/temporal-encryption-key-rotation
Open

epipav wants to merge 3 commits into
mainfrom
feat/temporal-encryption-key-rotation

Conversation

@epipav

@epipav epipav commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

Lets the Temporal payload encryption key rotate without breaking payloads that are already stored. EncryptionCodec keeps encrypting with the current key and can now also decrypt with older keys listed in a new, optional CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS.

Today the codec holds a single key, and every key id decrypts with it. Swapping the key would make running workflow histories, signals and schedule arguments undecodable.

Changes

services/libs/temporal/src/encryption/encryption-codec.ts

  • EncryptionCodec.create(keyId) loads the current key (CROWD_TEMPORAL_ENCRYPTION_KEY, unchanged) plus every entry of CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS, a JSON object of key id to key, for example {"old-id":"<32-byte key>"}.
  • encode is unchanged: new payloads carry the current key id.
  • decode picks the key by the payload's encryption-key-id. Unknown ids fall back to the current key, which is how every id was decrypted before, so behaviour is identical while the new variable is unset.
  • A malformed value fails at startup with an error that does not echo the value.
  • temporal_codec_server and every worker get this through EncryptionCodec.create and getDataConverter, with no change on their side.

Rotation with this in place

  1. Add the new key to CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS everywhere and restart, so every process can decrypt it before anything encrypts with it.
  2. Make the new key current (key and id) and move the old one into CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS, then restart.
  3. Recreate schedules so their stored arguments are re-encrypted, then drop the old key once workflows started before step 2 have closed and history retention has passed.

Testing

  • New encryption-codec.test.ts (5 tests, real AES-GCM keys): round trip; payloads written with a previous key decrypt after rotation while new payloads carry the new id; a payload fails once its key is dropped; unknown ids decrypt with the current key; a malformed value is rejected without echoing it.
  • tsc -b for services/libs/temporal and services/apps/temporal_codec_server, oxlint and oxfmt pass.

Signed-off-by: anilb <epipav@gmail.com>
@epipav
epipav requested review from a team and lfx-one as code owners November 23, 2026 15:28
@epipav
epipav requested review from skwowet and a balanced review from Copilot November 23, 2026 15:28
@epipav

epipav commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator Author

@greptileai review

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@cursor

cursor Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes Temporal payload encrypt/decrypt behavior and startup key loading; misconfiguration could break workflow history decoding, though backward compatibility is preserved when previous-keys env is unset.

Overview
Adds encryption key rotation for Temporal payloads by letting EncryptionCodec hold multiple AES keys at startup instead of a single env key.

EncryptionCodec.create now builds a map from optional CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS (JSON key id → key strings) plus the current CROWD_TEMPORAL_ENCRYPTION_KEY. Encode still tags payloads with the current key id; decode uses the metadata key id when present, otherwise falls back to the current key (same as pre-rotation behavior when the new env is unset). Invalid CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS values fail fast with an error that does not include key material.

New encryption-codec.test.ts covers round-trip, rotation (old ciphertext + new writes), missing retired keys, unknown key id fallback, and safe rejection of malformed config.

Reviewed by Cursor Bugbot for commit fda04ba. Bugbot is set up for automated code reviews on this repo. Configure here.

@lfx-one

lfx-one commented Nov 23, 2026 •

Copy link
Copy Markdown

@epipav I'm starting review 1 of this pull request now (started 2026-11-23 15:28 UTC using review agent v0.1.34). This usually takes a few minutes. I'll update this comment with the summary when I'm done. If there's no summary here after about 45 minutes, the review stopped: push a new commit, or post a comment that begins with a mention of me followed by re-review, to start another. For help with the review agent, see https://github.com/linuxfoundation/lfx-review-agent/issues.


Hi anilb,

Summary

This pull request lets the Temporal payload encryption key rotate: the codec keeps encrypting with the current key and can decrypt with older keys from a new optional environment variable. The logic is sound and tested; the main concern is a style comment and a question about the Insights codec.

Decision

✅ Approved with minor comments

🟡 1 minor · ❔ 1 question

Details

Overall impression: Small, focused change. Behavior is identical while CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS is unset, a malformed value fails at startup without echoing key material, and the five new tests use real AES-GCM keys and cover round trip, rotation, dropped key, unknown key id and bad config. Key ids that are not in the map decrypt with the current key, so rotation needs coordination with the Insights team, which keeps its own codec (see the question).

🟡 Minor: 1 issue: Comment above create breaks the repo's no-comments rule
❔ Question: 1 item: Does the Insights codec need to rotate in step with this one?

Other reviewers and CI checks: Greptile and Copilot flagged the same comment (agreed, linked in the inline comment). Cursor Bugbot's summary raised nothing further. The required DCO check passed on f57273d.

Documentation reminder: changed CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS. Please check product pages and external docs; the author and maintainers own those updates.

Rule files: read AGENTS.md; skipped none (the root CONTRIBUTING.md, .claude/rules/commit-workflow.md and the README were also read for context)

Questions, problems, or feedback: https://github.com/linuxfoundation/lfx-review-agent/issues

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Adds support for decrypting Temporal payloads with older keys while keeping new payloads on the current key.

  • Temporal payloads can be read with a configured previous key.

Reviews (3) · Last reviewed commit: "style: fix codec formatting" · Reviewed by Greptile

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A redundant key-loading comment in encryption-codec.ts still violates AGENTS.md’s prohibition on comments that merely describe the code.

1 open issue
What changed in this PR

Adds key-rotation support to CDP’s shared Temporal payload codec so stored payloads can remain readable after the current encryption key changes.

Changes:

  • Loads optional previous keys from CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS, while encrypting new payloads with the current key.
  • Selects decryption keys by payload key ID, preserving the current-key fallback for unknown IDs.
  • Adds tests for rotation, key retirement, compatibility, and malformed configuration.
File Description
services/​libs/​temporal/​src/​encryption/​encryption-codec.ts Loads and validates previous keys and selects the decryption key.
services/​libs/​temporal/​src/​encryption/​encryption-codec.test.ts Adds five tests covering codec behavior and configuration errors.

Review effort: Balanced

lfx-one
lfx-one previously approved these changes Nov 23, 2026

@lfx-one lfx-one left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key-rotation logic is correct and well tested. Behavior is unchanged while the new variable is unset. One minor comment-style finding and one question about the Insights contract ride along; neither blocks approval.

Documentation

This pull request changes behavior that may also be described outside this repository, such as product pages or an external docs site. I can't read those, so please check them; the author and maintainers own those updates. Changed here:

  • CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS (new optional environment variable)

this.keys.set(keyId, key)
}
// Unknown key ids fall back to the current key, which is how every id was decrypted before.
const key = this.keys.get(decode(keyIdBytes)) ?? this.keys.get(this.defaultKeyId)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] Does the Insights codec need to rotate in step with this one?

Issue: decode sends every key id that is not in the map to the current key. AGENTS.md (Downstream consumer: LFX Insights) says Insights keeps its own implementation of the payload encryption codec for the two workflows it starts on this repo's workers, and that the codec of those workflows is part of the external contract.
Proof: In the rotation steps in the PR description, step 2 makes a new key current. A payload from Insights carries Insights' own key id, which is not in this map, so it is decrypted with the new current key. Insights would have to switch to the new key at that moment, or its payloads fail to decrypt. The description doesn't mention Insights.
Why it matters: A rotation done without the Insights team could break upsertOSPSBaselineSecurityInsights and processProjectDocsReadiness runs in production.
Fix: Confirm how the Insights codec gets its key and what key id it writes, then add a step to the rotation procedure for coordinating with the Insights team (the contract doc linked in AGENTS.md is the place to check). If Insights needs to decrypt under its own key id, register that id in CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insights sets NUXT_TEMPORAL_ENCRYPTION_KEY_ID and INSIGHTS_TEMPORAL_ENCRYPTION_KEY_ID to the same id and key as CROWD_TEMPORAL_ENCRYPTION_KEY_ID, so its payloads already decrypt through this map. linuxfoundation/insights#2550 adds the same previous-keys support there, and both sides get the new key in one config change.

Signed-off-by: anilb <epipav@gmail.com>
@lfx-one

lfx-one commented Nov 23, 2026

Copy link
Copy Markdown

@epipav A recent review was posted within the last 5 minutes: #5430 (comment). I won't re-review yet. Push another update after 5 minutes if you want another pass.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Production rotation requires cross-repo confirmation of consumer readiness and historical key retention across CDP and Insights, whose independent Nuxt codec remains single-key.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Signed-off-by: anilb <epipav@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 15:53
@lfx-one

lfx-one commented Nov 23, 2026 •

Copy link
Copy Markdown

@epipav I'm starting review 2 of this pull request now (started 2026-11-23 15:53 UTC using review agent v0.1.34). This usually takes a few minutes. I'll update this comment with the summary when I'm done. If there's no summary here after about 45 minutes, the review stopped: push a new commit, or post a comment that begins with a mention of me followed by re-review, to start another. For help with the review agent, see https://github.com/linuxfoundation/lfx-review-agent/issues.


Hi anilb, thanks for addressing the earlier feedback so quickly.

Summary

These new commits remove the narrating comments from encryption-codec.ts and fix its formatting. The key-rotation logic is unchanged and no new concerns came up.

Decision

✅ Approved

Details

Overall impression: The follow-up is small and scoped to the earlier feedback. Behavior is identical to the version reviewed in review 1: encryption uses the current key, decryption picks the key by the payload's key id, and a malformed CROWD_TEMPORAL_ENCRYPTION_KEYS_PREVIOUS fails at startup without echoing key material. The remaining comment above previousKeys states a non-obvious constraint (the error must not carry key material), which fits the 2-line allowance in AGENTS.md.

👏 Nice work: you dropped both narrating comments, as the repo's no-comments rule asks, and kept the one that guards against leaking key material.

  • ✅ Resolved: Comment above create breaks the repo's no-comments rule. Removed in f14a46b.
  • ⚠️ Partially addressed: Does the Insights codec need to rotate in step with this one? You replied that Insights uses the same key id and key as CROWD_TEMPORAL_ENCRYPTION_KEY_ID, and that feat: decrypt temporal payloads with previous keys insights#2550 adds the same previous-keys support. That answers the question, but I can't see that PR from here and the rotation steps in the PR description still don't mention Insights. Consider adding a line there so both sides get the new key in the same config change. This is a question and does not block approval.

Other reviewers and CI checks: Greptile scored the PR 5/5 and Copilot reported no open issues; both agree with this review. The required DCO check passed on fda04ba.

Rule files: read AGENTS.md; skipped none

Questions, problems, or feedback: https://github.com/linuxfoundation/lfx-review-agent/issues

@lfx-one lfx-one left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commits remove the narrating comments and fix formatting; the key-rotation logic is unchanged and still correct. The earlier comment-style finding is fixed, and the Insights question was answered in the thread.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human confirmation is needed for coordinated key provisioning, rollout, and retirement with Insights, whose separate single-key codec is not updated by this PR or validated by its local tests.

No open issues 🎉

Review effort: Balanced

@gaspergrom gaspergrom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants