Repository navigation
feat(credentials): read a secret from a declared host program - #489
Conversation
|
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 Assumptions and rules checked that held
The whole-object JSON finding was checked with two temporary probe tests: the built-in |
|
Assessment: Approve (from the review side; the I re-reviewed at What I checked at this head
The |
b9f658e to
eb35ecc
Compare
|
Assessment: Approve (unchanged; the I re-checked at What I checked after the rebase
|
ferdingler
left a comment
There was a problem hiding this comment.
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.
eb35ecc to
efc305c
Compare
…-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.
…-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.
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 runwrapper.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.tomlkey uses it yet, so a box behaves as before.This runs
/opt/homebrew/bin/op read op://Private/GitHub/tokenand returns its stdout without the trailing newline. The example passesTMPDIR, becauseopkeeps the session ofop signinin 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#FIELDreturns 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
#FIELDrun 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_envvariable are replaced with[REDACTED]. This one is from a test with a stand-in script:The full rules are in
crates/credentials/README.md, in the section "Declared secret sources". The known limits are incrates/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:
SecretSourceCommand.SecretSourceCommandhas the constantPROTOCOLand the functionsnew,check_scheme,output,pass_env, andpass_secret_env. Its fields are private.env,file,aws,credsd,secret,cmd,oauth2,aws-signed, orstrip-phantom.check_schemeis 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 addsstrip-phantom, the scheme of the crate's second route marker (strip-phantom://route).Backend::declared(declarations)is a new constructor, andBackendis nowClone. A declaredoptakes the place of the built-inop://source.SCHEME://NAME[#FIELD].oprefuses a secret that is a JSON object when the reference has no#FIELD, with eachoutput. The built-inop://source returns such a secret whole. So this is a behavior change for a caller that moves from the built-in source to a declaredop.Backend::local()and the built-inop://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.tomlsets[experimental-features] secret-sources = true.box.tomlgets the[secret-source.<scheme>]table and thesecret.envkey, with the load checks.Two of them change the public surface of this crate again:
SecretSourceCommand::check_scheme: thebox.tomlloader, which refuses a reserved or malformed scheme with it. PR 2 also makesSecretSourceCommand::passes_as_secretpublic. 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.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 addsBackend::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
oporaws.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.op2.42.0 read a GitHub token at startup. The workload held a placeholder, andgh api userin the box returned the login.opneededTMPDIRinpass_envto find a session fromop signin.Not run: the full workspace suite, because no other crate calls the new code yet. CI runs it.
just check, orcargo test --workspace --all-features)cargo fmt --allandcargo clippy --workspace --all-targets --all-features -- -D warningsChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.