Skip to content

Read the cluster key and bucket from the environment - #8752

Merged
piscisaureus merged 1 commit into
mainfrom
piscisaureus/cluster-env-key
Nov 23, 2026
Merged

piscisaureus merged 1 commit into
mainfrom
piscisaureus/cluster-env-key

Conversation

@piscisaureus

@piscisaureus piscisaureus commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

A cluster node's config had to hold the cluster key, so a container
image built from it, or a binary from workerd compile, carried the
secret, and one image could not serve two clusters.

  • An empty cluster.key is filled from WORKERD_CLUSTER_KEY, or from
    the file that WORKERD_CLUSTER_KEY_FILE names, such as a mounted
    Kubernetes Secret. An empty cluster.bucket is filled from
    WORKERD_CLUSTER_BUCKET. Values in the config win, as they do for
    bucketOptions.
  • The key file must be a regular UTF-8 file of at most 4096 bytes.
    Trailing whitespace is ignored in the key, from either source, and in
    the bucket, as from a mounted ConfigMap.
  • A key from the environment shorter than 32 bytes is an error that
    names its source.
  • The fallback runs only when serving, for a config file and for a
    binary from workerd compile, so compile never embeds the
    environment.
  • If config and environment both leave a key or bucket unset, serve
    exits with an error naming the variables, under --watch too.
Kind Added Removed Where
Implementation 156 6 CLI config.rs and lib.rs, server check
Tests 422 15 config, cluster and server tests
Documentation 16 5 workerd.capnp, serve help, AGENTS.md

Comments in code files are counted with that file, except the
workerd.capnp comments and the help text, which are documentation.

@clanky-the-clanker clanky-the-clanker Bot 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.

Superseded by the review of 4819872f.

This review, of dd1d57ee

🛑 Significant concerns — One BLOCKING item: config-test.rs calls an undefined read helper, so the CLI crate's Rust test target does not compile. The production changes look correct.

Findings: 1 blocking · 1 info (2 are inline comments on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8752.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## BLOCKING (1)

### [Code Quality] config-test.rs calls an undefined `read` helper, so the crate's Rust test target does not compile
- **File:** src/workerd/server/cli/config-test.rs:43

## INFO (1)

### [Documentation] Clarify that the environment supplies the cluster key only when the config key is empty
- **File:** src/workerd/server/server/config.rs:2187

📋 Code Quality: 🛑 1 blocking

The new tests call an undefined read helper, so the Rust test target does not compile.

Recommendation: ⚠️ Major Comments

  • [BLOCKING] config-test.rs calls an undefined read helper, so the crate's Rust test target does not compile (src/workerd/server/cli/config-test.rs:43)
View full review

src/workerd/server/cli/config-test.rs:43 calls read(config), which is not defined in that module.

  • config-test.rs is mounted as config.rs's test module (#[path = "config-test.rs"] mod tests;) and starts with use super::*;.
  • config.rs defines no item named read. It defines read_file, read_stdin and read_key_file, and imports the std::io::Read trait.
  • The only fn read in the cli directory is a private helper in wrangler-test.rs:40, which is not visible here. That helper also takes Config by value, while cluster_of has a &Config.

The tests that go through cluster_of cannot compile, so the crate's whole test target fails, existing tests included. Fix: read the message inside cluster_of, for example capnp::serialize::read_message(config.to_bytes().as_slice(), capnp::message::ReaderOptions::new()).unwrap(), or add a local helper that borrows the config.

The production logic in with_cluster_environment, read_key_file and the serve and compiled-binary paths in lib.rs looks correct.


📝 Documentation: 💬 1 info

The short-key error suggests an environment override that cannot replace a non-empty config key.

Recommendation: 💬 Optional Nits

  • [INFO] Clarify that the environment supplies the cluster key only when the config key is empty (src/workerd/server/server/config.rs:2187)
View full review

src/workerd/server/server/config.rs:2187-2188 suggests setting the key in WORKERD_CLUSTER_KEY or its file. A non-empty key in the config takes precedence, though, so the environment only helps when the config key is empty.


✅ All other checks passed

🔒 Security: ✅ LGTM

Key-file handling has a size cap and a regular-file check. The fallback runs only when serving, so compile does not embed environment secrets.


📖 AGENTS.md: ✅ LGTM

The AGENTS.md update is consistent with the change.


⚡ Performance: ✅ LGTM

No performance concerns.


Reviewed commit: dd1d57ee

Comment thread src/workerd/server/cli/config-test.rs Resolved
Comment thread src/workerd/server/server/config.rs Resolved
@piscisaureus
piscisaureus requested a review from ry November 20, 2026 19:26
@piscisaureus
piscisaureus force-pushed the piscisaureus/cluster-env-key branch from dd1d57e to 4819872 Compare November 20, 2026 19:52

@clanky-the-clanker clanky-the-clanker Bot 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.

Superseded by the review of c3a2cf67.

This review, of 4819872f

✅ Approved — The earlier BLOCKING item, the undefined read helper in config-test.rs, is fixed: the test module now defines read(&Config) using config.rs's private helpers. The earlier note about the short-key message is addressed too, and nothing new was found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The key file is limited in size and must be a regular file. The fallback runs only when serving, so environment secrets are not embedded by compile.


📖 AGENTS.md: ✅ LGTM

The AGENTS.md update matches the change.


📋 Code Quality: ✅ LGTM

🔄 Since last review: 2 findings resolved, 0 preserved, 0 new

The earlier BLOCKING finding is fixed. src/workerd/server/cli/config-test.rs:3-6 now defines read(&Config) using bytes_from_words and reader_options. Both are private to config.rs, and the child test module can see them through use super::*. The call clusterAuth(node, target) in cluster-test.mjs now matches the current signature.

The environment fallback runs only on the serve and compiled-binary paths, so workerd compile does not embed the key or bucket.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The short-key error message (src/workerd/server/server/config.rs:2187-2189) now says to fix the key in the config, or, for workerd serve, to leave it empty and set WORKERD_CLUSTER_KEY or a file named by WORKERD_CLUSTER_KEY_FILE. server-test.c++ expects the same text.


Reviewed commit: 4819872f

@codecov-commenter

codecov-commenter commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 108 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.28%. Comparing base (1a676ed) to head (9fa8c66).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/workerd/server/cli/config.rs 0.00% 98 Missing ⚠️
src/workerd/server/cli/lib.rs 0.00% 6 Missing ⚠️
src/workerd/server/server/config.rs 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8752      +/-   ##
==========================================
- Coverage   67.30%   67.28%   -0.03%     
==========================================
  Files         838      838              
  Lines      213835   213942     +107     
  Branches    32490    32490              
==========================================
+ Hits       143927   143942      +15     
- Misses      50811    50904      +93     
+ Partials    19097    19096       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/workerd/server/cli/config.rs Resolved
Comment thread src/workerd/server/server/config.rs Resolved
@piscisaureus
piscisaureus force-pushed the piscisaureus/cluster-env-key branch from 4819872 to c3a2cf6 Compare November 21, 2026 00:27

@clanky-the-clanker clanky-the-clanker Bot 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.

Superseded by the review of 9fa8c668.

This review, of c3a2cf67

⚠️ Minor issues — One WARNING: WORKERD_CLUSTER_BUCKET is used without trimming, so a value with a trailing newline (common from mounted ConfigMaps) gives the cluster a prefix that workerd deploy never writes to, and nodes quietly see no deployments. Also one INFO about the short-key error text.

Findings: 1 warning · 1 info (2 are inline comments on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8752.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Code Quality] WORKERD_CLUSTER_BUCKET is not trimmed like the key sources, so a trailing newline gives a bucket prefix that deploys never write to, and nodes quietly see no deployments.
- **File:** src/workerd/server/cli/config.rs:109

## INFO (1)

### [Code Quality] The short-key error tells the user to set WORKERD_CLUSTER_KEY even when the short key came from that variable, and it limits the advice to `workerd serve` although compiled binaries read the variables too.
- **File:** src/workerd/server/server/config.rs:2188

📋 Code Quality: ⚠️ 1 warning · 1 info

The bucket from WORKERD_CLUSTER_BUCKET is not trimmed the way the key is, so a trailing newline quietly breaks the cluster. The short-key error can also point users the wrong way.

Recommendation: 📝 Minor Comments

  • [WARNING] WORKERD_CLUSTER_BUCKET is not trimmed like the key sources, so a trailing newline gives a bucket prefix that deploys never write to, and nodes quietly see no deployments. (src/workerd/server/cli/config.rs:109)
  • [INFO] The short-key error tells the user to set WORKERD_CLUSTER_KEY even when the short key came from that variable, and it limits the advice to workerd serve although compiled binaries read the variables too. (src/workerd/server/server/config.rs:2188)
View full review

🔄 Since last review: 0 findings resolved, 0 preserved, 2 new (both raised by @​ry [non-author] in the discussion and confirmed against the code)

The earlier fix still holds: config-test.rs defines its own read(&Config) helper, and the cluster test matches the clusterAuth(node, target) signature.

Bucket value is not trimmed (src/workerd/server/cli/config.rs:108-112)
Both key sources go through non_blank, which strips trailing whitespace (config.rs:181-185). var(CLUSTER_BUCKET) is used exactly as given. The bucket prefix code trims only / (cluster/bucket.rs:521). A bucket that ends in \n, which is common for a YAML | value or a ConfigMap built --from-file, gives the prefix cluster\n, while workerd deploy --bucket s3://b/cluster writes under cluster/. The nodes then never see a deployment, and nothing reports an error. A whitespace-only value also skips the "cluster.bucket is not set" error.

Short-key message (src/workerd/server/server/config.rs:2186-2193)
The length check runs after the environment fill. When the short key came from WORKERD_CLUSTER_KEY or the key file, the message tells the user to set those variables, which they already did. It also says the variables apply to workerd serve, but a binary from workerd compile reads them too.


✅ All other checks passed

🔒 Security: ✅ LGTM

The key file must be a regular file, and the read is capped at 4096 bytes, so a FIFO or device cannot hang the process. Setting both WORKERD_CLUSTER_KEY and WORKERD_CLUSTER_KEY_FILE is rejected. Error messages name the variable and path but never print the key. The fallback runs only when serving, so workerd compile never embeds the key from the environment.


📖 AGENTS.md: ✅ LGTM

The AGENTS.md update matches the change.


⚡ Performance: ✅ LGTM

The environment fallback runs once at startup, and once per config reload under --watch. It re-serializes the config at most once and reads a key file of at most 4096 bytes.


📝 Documentation: ✅ LGTM

The workerd.capnp comments, the serve help and AGENTS.md describe the new environment fallback. The wording of the short-key error is covered in the Code Quality section.


Reviewed commit: c3a2cf67

Comment thread src/workerd/server/cli/config.rs Resolved
Comment thread src/workerd/server/server/config.rs Resolved
A cluster node's config had to hold the cluster key, so a container
image built from it, or a binary from `workerd compile`, carried the
secret, and one image could not serve two clusters.

`serve` now fills an empty `cluster.key` from WORKERD_CLUSTER_KEY, or
from the file that WORKERD_CLUSTER_KEY_FILE names, such as a mounted
Kubernetes Secret, ignoring trailing whitespace in either. The file
must be a regular file of at most 4096 bytes that holds UTF-8 text. An
empty `cluster.bucket` falls back to WORKERD_CLUSTER_BUCKET. Values in
the config win, as they do for `bucketOptions`. The fallback runs in
the serve path only, for a config file and for a binary from
`workerd compile`, so `compile` never embeds the environment.

A key or bucket that neither the config nor the environment sets ends
the process with an error that names the variables. It does so under
`--watch` too, since the fix may lie outside the config files.
@piscisaureus
piscisaureus force-pushed the piscisaureus/cluster-env-key branch from c3a2cf6 to 9fa8c66 Compare November 21, 2026 00:58

@clanky-the-clanker clanky-the-clanker Bot 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.

✅ Approved — Both earlier findings (bucket trimming and the short-key error text) are fixed at this commit, and no new issues were found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The key file must be a regular file and is read with a 4096-byte cap. Setting both key variables is rejected. Errors never print the key. compile never embeds the key from the environment.


📖 AGENTS.md: ✅ LGTM

The AGENTS.md update matches the change.


📋 Code Quality: ✅ LGTM

The bucket value is now trimmed by non_blank like the key sources (cli/config.rs:109), and tests cover a trailing newline and a blank value. cluster_key checks the key length itself and names where the key came from. The server's message (server/config.rs:2186-2191) now covers only a key set in the config.


⚡ Performance: ✅ LGTM

The fallback runs once at startup, and once per reload under --watch. It re-serializes the config at most once and reads a key file of at most 4096 bytes.


📝 Documentation: ✅ LGTM

The workerd.capnp comments and the serve help describe the environment fallback accurately.


Reviewed commit: 9fa8c668

@piscisaureus
piscisaureus requested a review from ry November 21, 2026 06:46
@piscisaureus
piscisaureus merged commit d9dcac8 into main Nov 23, 2026
36 of 38 checks passed
@piscisaureus
piscisaureus deleted the piscisaureus/cluster-env-key branch November 23, 2026 17:48
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.

4 participants