Skip to content

feat(credentials): read a secret from a declared host program - #489

Merged
yesyayen merged 6 commits into
mainfrom
feature/secret-source-command
Nov 18, 2026
Merged

yesyayen merged 6 commits into
mainfrom
feature/secret-source-command

Conversation

@yesyayen

@yesyayen yesyayen commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

This is the first of four PRs that build the secret provider design (#467). With all four, a box reads a secret from a vault, such as 1Password, through a host program that the operator declares. The workload holds only a placeholder, and the operator needs no op run wrapper.

What this PR does

It changes the credentials crate only. The crate can now run a declared host program and read one secret from its stdout. No box.toml key uses it yet, so a box behaves as before.

use credentials::{Backend, Locator, SecretSourceCommand};

let command = ["/opt/homebrew/bin/op", "read", "op://{ref}"].map(String::from);
let declaration = SecretSourceCommand::new("op", SecretSourceCommand::PROTOCOL, &command)?
    .pass_env(["OP_ACCOUNT", "TMPDIR"])?
    .pass_secret_env(["OP_SERVICE_ACCOUNT_TOKEN", "OP_SESSION_*"])?;
let backend = Backend::declared([declaration])?;
let secret = backend.resolve(&Locator::parse_uri("op://Private/GitHub/token")?)?;

This runs /opt/homebrew/bin/op read op://Private/GitHub/token and returns its stdout without the trailing newline. The example passes TMPDIR, because op keeps the session of op signin in a folder under it.

What to expect:

  • No shell. The program is an absolute path. It gets no stdin, and it starts in /.

  • A small environment. The program gets HOME, PATH=/usr/bin:/bin, and each variable in the two pass lists that is set. An entry that ends with * passes each variable with that prefix.

  • Bounds. A run of more than 30 seconds, more than 1 MiB of stdout, or a non-zero exit fails the fetch.

  • Output. By default, stdout is the secret. With .output("json"), stdout is one JSON string. A reference that ends with #FIELD returns one string field of a JSON object. Without #FIELD, a secret that is a JSON object is refused.

  • One run for each secret. Two references that differ only in #FIELD run the program once.

  • Errors. A failure names the scheme and the program. After a non-zero exit it also shows the end of stderr. The reference and the value of each passed pass_secret_env variable are replaced with [REDACTED]. This one is from a test with a stand-in script:

    credential error: the secret source "vault" ("/bin/sh") for vault://[REDACTED] exited unsuccessfully (exit status: 3): no such entry
    

The full rules are in crates/credentials/README.md, in the section "Declared secret sources". The known limits are in crates/credentials/AGENTS.md.

Interface changes

This PR follows the design as #467 revises it, so #467 merges first. Each change below needs a maintainer's approval:

  • One new public name: SecretSourceCommand.
  • SecretSourceCommand has the constant PROTOCOL and the functions new, check_scheme, output, pass_env, and pass_secret_env. Its fields are private.
  • A declaration cannot claim a reserved scheme: env, file, aws, credsd, secret, cmd, oauth2, aws-signed, or strip-phantom. check_scheme is the check. The list and the {ref} text stay private to the crate, because no other crate reads them. This differs from the text of the design in two places. The design names a {ref} constant in the credentials API, and this PR exports none. The design lists eight reserved schemes, and this PR adds strip-phantom, the scheme of the crate's second route marker (strip-phantom://route).
  • Backend::declared(declarations) is a new constructor, and Backend is now Clone. A declared op takes the place of the built-in op:// source.
  • A reference in a declared scheme has the form SCHEME://NAME[#FIELD].
  • A declared op refuses a secret that is a JSON object when the reference has no #FIELD, with each output. The built-in op:// source returns such a secret whole. So this is a behavior change for a caller that moves from the built-in source to a declared op.
  • Backend::local() and the built-in op:// source do not change.

The PRs that follow

Each one builds on the one before it, so each opens when the one before it merges. The feature is experimental and macOS first. It stays off unless box.toml sets [experimental-features] secret-sources = true.

PR What it adds
1 (this PR) The credentials crate runs a declared program and reads one secret.
2 box.toml gets the [secret-source.<scheme>] table and the secret.env key, with the load checks.
3 The box reads each secret at startup and gives the workload a placeholder.
4 The box keeps the vault's files, its program, and its credential from each workload.

Two of them change the public surface of this crate again:

  • PR 2 adds the first caller of SecretSourceCommand::check_scheme: the box.toml loader, which refuses a reserved or malformed scheme with it. PR 2 also makes SecretSourceCommand::passes_as_secret public. The loader calls it, so the rule for a pass-list entry that ends with * keeps one owner. In this PR that match is private to the crate.
  • PR 3 changes Backend::declared. It takes each declaration with the opened program file that the box checked, and the source compares the program with that file at each start. So a replaced program stops the fetch. PR 3 also adds Backend::close_declared, which wipes each held secret.

After the four, Box CLI adds plugins, such as box/1password, that write the table for the operator.

Related Issues

Type of Change

New feature

Testing

Each test uses a stand-in program: a shell script in a temp dir. No test runs a real op or aws.

  • cargo test -p strands-box-credentials --all-features --locked: 295 unit tests and 33 integration tests passed on macOS (arm64). 292 unit tests and 33 integration tests passed on Linux (x86_64, an Amazon Linux 2023 container). 58 unit tests are new.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings, cargo fmt --all --check, and the rustdoc gate of the crate are clean.
  • Each new test fails when the code that it checks is undone: 82 undo patches, each run once.
  • By hand on macOS, with the three PRs that follow and a Box CLI plugin on top: the real op 2.42.0 read a GitHub token at startup. The workload held a placeholder, and gh api user in the box returned the login. op needed TMPDIR in pass_env to find a session from op signin.

Not run: the full workspace suite, because no other crate calls the new code yet. CI runs it.

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)
  • If I touched Rust, I ran cargo fmt --all and cargo clippy --workspace --all-targets --all-features -- -D warnings

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@yesyayen yesyayen added the api/needs-review Public API change requires maintainer review. label Nov 17, 2026
@github-actions github-actions Bot added Area - Egress/Creds enhancement New feature or request labels Nov 17, 2026
@yesyayen
yesyayen marked this pull request as ready for review November 17, 2026 20:09
@github-actions

Copy link
Copy Markdown

Assessment: Comment

The runner is solid: no shell, a composed environment, bounded pipes, and position-based redaction of stderr. The two inline comments are about the edges of the contract, not the runner. One is a behaviour change for anyone who switches from the built-in op:// to a declared op. The other is a reserved-scheme list kept in sync by hand. The PR is large (+2307), but most of that is tests and docs, and the API surface is clearly listed and already labelled api/needs-review.

Assumptions and rules checked that held
  • Compose, never inherit (root AGENTS.md): env_clear plus HOME, a fixed PATH, and the passed variables only. Values that are not passed are wiped. A PA* or HOME* prefix cannot replace PATH or HOME.
  • Frozen pub use set (root AGENTS.md): the one new name, SecretSourceCommand, and the new constructor Backend::declared are listed for maintainer approval. Cargo.lock is unchanged, and serde_json was already a dependency.
  • Flag injection: no shell, and a name that starts with - is refused. {ref} substitution is a single pass.
  • Bounds: more than 1 MiB of stdout kills the program. A noisy stderr is drained, so the pipe never blocks. The deadline covers a descendant that holds stdout open, and the descendant that outlives the kill is a documented residual.
  • Fetch cache: a failed run or an output that cannot be decoded is not kept. References that differ only in #FIELD share one run, and clones share the cache through Arc<Profile>.
  • Duplicate schemes: the registry refuses a duplicate declaration, and a declared op replaces the built-in one.
  • Structured locator with a declared source tag: refused hard by uri_reference.
  • Tests: failures are asserted as whole strings, not field by field.

The whole-object JSON finding was checked with two temporary probe tests: the built-in op returns the value whole, and a declared op refuses it. The probes were reverted afterwards.

@github-actions

Copy link
Copy Markdown

Assessment: Approve (from the review side; the api/needs-review approval is still the maintainers')

I re-reviewed at b9f658e, which adds three commits since the last review at ae82236 (ac1c4af, 5d47599, b9f658e). Both earlier threads are addressed, and I found nothing new.

What I checked at this head
  • JSON object with no #FIELD (thread on command.rs:546): the refusal now applies with each output, as docs: make a secret provider a plugin #467 states it, and the difference from the built-in source is recorded in four places:

    • the README rows for output and #FIELD;
    • the crate's AGENTS.md;
    • the "Interface changes" section of the PR description;
    • two tests: a_declared_op_refuses_a_json_object_without_a_field_in_text_output and op.rs::returns_a_json_object_whole.

    This is the documented option I suggested, so I'd resolve the thread.

  • Reserved list ownership (thread on secret_source.rs): the list is now built from the constant of each owner: ENV_SCHEME, FILE_SCHEME, AWS_SCHEME, CREDSD_SCHEME, CMD_SCHEME, OAUTH2_SCHEME, plus scheme_of applied to both route markers. Only secret remains a literal, because the box crate owns it.

    • scheme_of("aws-signed://route") returns aws-signed, and scheme_of("strip-phantom://route") returns strip-phantom, the same as Locator::scheme.
    • The newly visible constants are only pub(crate), so the pub use set does not change.
    • the_reserved_schemes_are_the_nine_the_box_owns still pins the nine literal names.
  • Gates at this head, which I ran: cargo test -p strands-box-credentials --all-features --locked (292 unit and 33 integration tests passed), cargo clippy ... -D warnings (clean), and cargo fmt --check (clean).

The pass_env and redaction design still holds up after the changes.

@github-actions

Copy link
Copy Markdown

Assessment: Approve (unchanged; the api/needs-review approval is still the maintainers')

I re-checked at eb35ecc. This head is a rebase onto main, and the PR's own commits are unchanged. git range-diff shows each of the six commits as = against the version I reviewed at b9f658e. The diff of crates/credentials against the merge base is byte-identical, apart from the index lines.

What I checked after the rebase

@ferdingler ferdingler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me, and it matches the design in #467. Thanks Anand!

## Problem

The credentials crate reads a secret from a vault only through its built-in `op://` source. A second vault, such as AWS Secrets Manager, needs a new source in the crate, with its own dependencies and its own review. The secret provider design (#467) replaces that with one contract. The caller declares a host program, and the box runs it at startup and reads one secret from its stdout. The crate has no way to run a declared program.

## Solution

Add a declared secret source to the credentials crate. Nothing reads it from `box.toml` yet, so a box behaves as before.

- `SecretSourceCommand` declares one program for one scheme: the protocol (`box.secret-source.v1`), the command, the output form (`text` or `json`), and two pass lists. Its constructor refuses a reserved or malformed scheme, another protocol, and an empty command. It also refuses a program that is not an absolute path, and a command with no `{ref}` in its arguments. A pass list refuses the entries `*`, `PATH`, and `HOME`, and an entry with a `*` before its end. Each refusal names the key and the entry.
- The runner starts the program with no shell and no stdin, in `/`. The environment is `HOME`, `PATH=/usr/bin:/bin`, and each pass-list variable that is set. An entry that ends with `*` passes each variable with that prefix. A run of more than 30 seconds, more than 1 MiB of stdout, or a non-zero exit fails the fetch. A failure carries the end of stderr, at most 512 bytes, with the reference name and each passed `pass_secret_env` value replaced.
- `{ref}` (`SECRET_REFERENCE_TOKEN`) is the text after `scheme://` without `#FIELD`. The source replaces it inside each argument, and refuses a reference whose text after `scheme://` starts with `-`.
- With `output = "json"`, stdout is one JSON string. With `#FIELD`, the secret is a JSON object, and the source returns that one string field. Without `#FIELD`, a secret that is a JSON object is refused. The JSON field step and its wipe helpers follow #448.
- `RESERVED_SECRET_SCHEMES` lists the schemes that no declaration can claim, and `SecretSourceCommand::check_scheme` is the check.
- `Backend::declared(declarations)` registers `env`, `file`, and one source for each declaration. A declared `op` takes the place of the built-in `op://` source. The backend keeps each fetched secret, so two references that differ only in `#FIELD` run the program once.

Interfaces that moved:

- The `pub use` set of the crate grows from 20 names to 23: `SecretSourceCommand`, `SECRET_REFERENCE_TOKEN`, `RESERVED_SECRET_SCHEMES`.
- `SecretSourceCommand` has the constant `PROTOCOL` and the functions `new`, `check_scheme`, `output`, `pass_env`, and `pass_secret_env`.
- The URI vocabulary of the crate gains `SCHEME://NAME[#FIELD]` for a declared scheme. `#` starts a field only for a declared scheme.
- `Backend` gains the constructor `declared`, and is now `Clone`. Each clone shares the same sources and the same fetched secrets. The held secrets have no accessor.
- `Backend::local()` is the same: `env`, `file`, and the built-in `op`.

## Tests

Each test uses a stand-in program, a shell script in a temp dir. No test runs a real `op` or `aws`.

- `cargo test -p strands-box-credentials --all-features --locked`: 292 unit tests and 33 integration tests passed on macOS. 289 unit tests and 33 integration tests passed in an Amazon Linux 2023 container. 55 unit tests are new.
- The declaration: `each_reserved_scheme_is_refused_by_name`, `a_command_with_no_reference_token_in_its_arguments_is_refused`, `a_pass_list_entry_that_has_no_effect_or_passes_every_variable_is_refused`.
- The environment: `the_program_gets_only_home_the_fixed_path_and_the_passed_variables`, `a_variable_with_a_name_that_is_not_utf8_does_not_pass`.
- No stdin, the directory `/`, no shell, `{ref}`: `the_program_gets_no_stdin`, `the_program_starts_in_the_root_directory`, `each_argument_reaches_the_program_as_one_argument_with_no_shell`, `a_malformed_reference_is_refused_before_the_program_starts`.
- The bounds: `a_program_that_runs_past_the_timeout_is_stopped`, `stdout_up_to_the_limit_is_read_and_one_more_byte_is_refused`, `a_program_that_never_stops_writing_is_stopped_at_the_limit`, `a_program_that_leaves_its_stdout_open_fails_at_the_deadline`.
- The failure text: `a_non_zero_exit_is_a_failure_that_carries_stderr`, `a_failure_hides_the_reference_and_each_passed_credential_and_each_control_character`, `a_failure_hides_a_passed_credential_that_is_not_utf8`, `a_failure_carries_at_most_512_bytes_of_stderr`, `the_excerpt_hides_a_whole_hidden_value_near_the_start_of_a_cut_capture`.
- The output and the field: `json_output_is_one_json_string_and_any_other_value_is_refused`, `a_field_selects_one_string_of_a_json_object_and_any_other_case_is_refused`, `a_json_object_without_a_field_is_refused`.
- One fetch: `two_references_that_differ_only_in_the_field_run_the_program_once`, `each_clone_of_a_declared_backend_shares_one_fetch`, `two_callers_at_one_time_run_the_program_once`.
- The backend: `a_declared_op_takes_the_place_of_the_built_in_source`, `local_holds_the_env_file_and_op_schemes`.
- Clippy for the workspace (`--all-targets --all-features -D warnings`) and the rustdoc gate of the crate are clean in the container.

Not run: the full workspace suite, because no other crate calls the new surface. No test can observe the wipe of a value that drops, a parsed JSON value or a copied environment value; `a_wipe_empties_each_string_of_a_parsed_value` tests the helper.
The README example names `OP_SESSION_*`, the variable of `op signin`, and did not pass `TMPDIR`. `op` 2.42.0 on macOS keeps the session of `op signin` in a folder under `TMPDIR`. With `HOME`, `PATH`, and `OP_SESSION_*` alone it says that no session is active, and with `TMPDIR` too it finds the session.

The code does not change. `pass_env` accepts `TMPDIR`.
… the crate

The reserved scheme list did not hold `strip-phantom`, the scheme of the crate's second route marker (`strip-phantom://route`). So a declaration could claim it. The list now holds it, and `SecretSourceCommand::check_scheme` refuses a declaration of that scheme.

`RESERVED_SECRET_SCHEMES` and `SECRET_REFERENCE_TOKEN` were public, and no other crate reads them. The crate adds a public item only for a named external caller, so both are now private to the crate. `SecretSourceCommand::check_scheme` stays the one check of a scheme for a caller. The `pub use` set of the crate is now 21 names, one more than before this PR: `SecretSourceCommand`.

Tests: `the_reserved_schemes_are_the_nine_the_box_owns` and `each_reserved_scheme_is_refused_by_name` fail when `strip-phantom` leaves the list.
… each owner

The reserved scheme list held each scheme as a second literal, beside the constant of the code that owns it. An earlier revision of this change missed `strip-phantom` for that reason.

The list now takes each name from its owner. Six names come from the scheme constants of the crate: `env`, `file`, `aws`, `credsd`, `cmd`, and `oauth2`. Two come from the route markers `aws-signed://route` and `strip-phantom://route`. Only `secret` stays a literal, because the box crate owns it. The nine names and their order do not change.

Tests: `each_scheme_constant_and_each_route_marker_of_the_crate_is_reserved` fails when the list holds a literal and the constant of its owner changes. `the_reserved_schemes_are_the_nine_the_box_owns` still compares the nine literal names, because a refusal prints the list.
…ut a field

A declared source refuses a secret that is a JSON object when the reference has no `#FIELD`, with each `output`. The built-in `op://` source returns such a secret whole. So a declared `op`, which takes the place of the built-in source, differs from it here. The docs did not say so, and the README row for `text` read as if stdout always passes.

The rule stays for each `output`, as the design (#467) states it: one destination never gets each key of an object. A check for `json` alone lets a program that prints an object as text pass it whole.

- `a_declared_op_refuses_a_json_object_without_a_field_in_text_output` pins the refusal for scheme `op`.
- `returns_a_json_object_whole` pins what the built-in source does with the same stdout.
- The README rows for `output` and `#FIELD` state that the rule holds for each `output`, and name the difference.
…itself

`each_scheme_constant_and_each_route_marker_of_the_crate_is_reserved` asked `check_scheme` for a refusal. A malformed scheme is refused too. So the test passed when a scheme constant held a malformed text, and when a route marker had a form that `scheme_of` and `Locator::scheme` read differently. The test now looks for each scheme in the list.

- `crates/credentials/AGENTS.md` says that no code collects the constants, so each new scheme constant and each new route marker goes into the list and into this test.
- The README row for `#FIELD` says "all the keys".
- `a_declared_op_refuses_a_json_object_without_a_field_in_text_output` builds its backend with the helper of its file, which gives the program an empty operator environment, as the other tests of a refusal do.
@yesyayen
yesyayen merged commit 37af631 into main Nov 18, 2026
20 checks passed
@yesyayen
yesyayen deleted the feature/secret-source-command branch November 18, 2026 23:26
yesyayen added a commit that referenced this pull request Nov 18, 2026
…-fetch

This brings main into this branch, through the merge of the branch below. main now holds the change of feature/secret-source-command as one squashed commit (#489), with the same content as the commits of it that this branch holds, so each file of crates/credentials stays as this branch has it.

The merge also brings six files under .agents/ from main (#521). No other file changes.
yesyayen added a commit that referenced this pull request Nov 19, 2026
…-protect

This brings main into this branch, through the merge of the branch below. main now holds the change of feature/secret-source-command as one squashed commit (#489), with the same content as the commits of it that this branch holds, so each file of crates/credentials stays as this branch has it.

The merge also brings six files under .agents/ from main (#521). No other file changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api/needs-review Public API change requires maintainer review. Area - Egress/Creds enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants