Repository navigation
feat(stack-kms)!: stop a key source type from being both kinds, with KeySourceKind - #1738
Conversation
|
Mutation testing (cargo-mutants,
|
b09754a to
7d3a79d
Compare
7df19c0 to
2e6d3bc
Compare
c6abfcc to
47b2cc3
Compare
2e6d3bc to
5c3a651
Compare
47b2cc3 to
3990804
Compare
5c3a651 to
3052a7e
Compare
3990804 to
0156249
Compare
3052a7e to
81aaaa1
Compare
coderdan
left a comment
There was a problem hiding this comment.
Human pass. LGTM.
…`KeySourceKind`
A key source makes every request as itself (`DataKeySource` and
`IndexKeySource`), or as the caller whose credential comes with the call
(`CredentialKeySource`). The docs said a type is one kind and never
both. Nothing checked it. A type that implemented all three traits
compiled, and Stack Encrypt then accepted it as both kinds: after
`init::<OwnIdentity>()`, a call through it could be awaited with no
credential, and was made as the key source itself. That is a forgotten
credential that compiles.
`KeySourceKind` is the check. A key source states its kind once:
impl KeySourceKind for MyKeySource {
type Kind = OwnIdentity;
}
`DataKeySource` and `IndexKeySource` require `Kind = OwnIdentity`, and
`CredentialKeySource` requires `Kind = CredentialPerCall`. A type has
one `Kind`, so a key source of both kinds does not compile (E0271).
`StackKms` does not choose its kind. It is the kind of its auth
strategy, read from `stack_auth::StrategyKind`, so stack-auth decides
it.
Unlike `StrategyKind`, no blanket impl gives existing key sources their
kind. The kind with an identity of its own is two traits, and a blanket
impl can be written over only one. So every key source adds the impl
above. That is a breaking change for a key source written outside this
crate, most often a test double. The compiler names the missing impl.
The key sources in this repository each gain the impl: the three test
doubles of this crate, and the test doubles in stack-encrypt, the EQL
encryption tests and the Go guest. No behaviour changes.
Refs #1708
81aaaa1 to
2cf6e1e
Compare
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the changes extending credential-safety guarantees to custom key sources.
No open issues 🎉
What changed in this PR
This PR extends #1709’s credential-safety guarantees to custom key sources. KeySourceKind makes implementing both own-identity and per-call-credential key-source traits a compile error, without changing Stack Encrypt’s runtime logic.
Changes:
- Adds kind constraints, strategy-derived classification for
StackKms, compiler guidance, and positive/compile-fail doctests. - Declares kinds for built-in sources and test doubles across Stack Encrypt, EQL, and the Go guest.
- Documents the breaking change and the implementation custom key sources must add.
| File | Description |
|---|---|
packages/stack-kms/src/lib.rs |
Exports KeySourceKind and both kind markers. |
packages/stack-kms/src/key_source.rs |
Enforces exclusive kinds and adds doctests. |
packages/stack-kms/CHANGELOG.md |
Documents the breaking change and migration. |
packages/stack-encrypt/tests/scalar.rs |
Classifies the fixed-key test source. |
packages/stack-encrypt/tests/opaque_blob.rs |
Classifies the opaque-blob test source. |
packages/stack-encrypt/tests/lifecycle_conformance.rs |
Classifies the lifecycle test source. |
packages/stack-encrypt/tests/keysets.rs |
Classifies both keyset test sources. |
packages/stack-encrypt/tests/json_operators.rs |
Marks the shared source as credential-per-call. |
packages/stack-encrypt/tests/heap_hygiene.rs |
Classifies memory-hygiene test sources. |
packages/stack-encrypt/tests/frozen_bytes.rs |
Classifies the oversized-tag test source. |
packages/stack-encrypt/tests/credential.rs |
Declares each credential test source’s kind. |
packages/stack-encrypt/tests/common/mod.rs |
Classifies shared counting and recording sources. |
packages/stack-encrypt/tests/audit.rs |
Classifies the audit recording source. |
packages/stack-encrypt/src/target/pending.rs |
Classifies a unit-test source only. |
packages/stack-encrypt/src/dynamic/record.rs |
Classifies a unit-test source only. |
packages/stack-encrypt/src/credential.rs |
Clarifies the key-source contract in documentation. |
packages/stack-encrypt/CHANGELOG.md |
Explains migration through the kms re-export. |
packages/eql/tests/encryption/tests/heap_hygiene.rs |
Classifies the stalling encryption test source. |
packages/eql/tests/encryption/tests/common/mod.rs |
Classifies the shared recording source. |
languages/golang/encrypt/guest/tests/native_ops.rs |
Classifies the native-operation counting source. |
languages/golang/encrypt/guest/tests/leak.rs |
Classifies the failure-path test source. |
Review effort: Balanced
Summary
This PR was stacked on #1709, which is now merged into
next. It is one commit on top ofnext.A key source is what a Stack Encrypt cipher asks for keys. There are two kinds. One has an identity of its own, such as an access key, and makes every request as itself. The other takes a credential with each call, such as an end user's JWT, and makes each request as that caller.
The docs of stack-kms said a key source is one kind and never both. Nothing checked it. This PR makes a key source of both kinds a compile error.
Why it matters: with a key source of both kinds, a request handler that forgot
.with_credential(..)still compiled, and its call was made as the key source itself, not as the user. #1709 closed that for real ZeroKMS clients. This closes it for a key source that someone writes by hand, such as a test double.The problem, in code
On #1709 this compiles:
The first
encryptis the forgotten credential. I ran this as a test against #1709, and it passed.The change
stack-kms has a new trait,
KeySourceKind. A key source states its kind once, and each key source trait requires its own kind:A type implements
KeySourceKindonce, so it has oneKind.Bothnow fails:StackKms, the real ZeroKMS client, does not choose its kind. It takes the kind of its auth strategy, fromstack_auth::StrategyKind(added in #1709):Changes
stack-kms
KeySourceKind, and the three key source traits require it.StackKmstakes its kind from its auth strategy.FakeDataKeySource,DeterministicSourceandFakeCredentialKeySourcestate their kinds.CredentialPerCallbesideOwnIdentity, so a key source needs no direct dependency on stack-auth to state its kind.Every other key source in the repository
Docs
Breaking change
A key source written outside stack-kms needs one new impl:
Without it the compiler says "
MyKeySourcedoes not say which kind of key source it is", and names the impl for each kind.A cipher built with
auth_strategy(..), or overStackKmsor the test doubles of stack-kms, needs no change.stack-auth's
StrategyKinddid not need this from existing strategies, because one blanket impl covers everyAuthStrategy. Here the kind with an identity of its own is two traits (DataKeySourceandIndexKeySource), and Rust allows a blanket impl over only one of them.Verification
#1687 and #1709 are merged into
next, and this PR is rebased ontonext: the head is2cf6e1e28, on the merge commit of #1709 (271b16f49). Neither merge changed a file ofnextbeyond the PR's own commits, so every file at the head is the same as at81aaaa192and at3052a7ec4, two earlier pushes (git diffbetween them is empty). The local runs below are from3052a7ec4and were not repeated. CI passed on the head,2cf6e1e28: 50 checks passed, 7 skipped. The skipped checks are the TypeScript test jobs and the long stack-auth soak, which this PR's files do not reach, the nightly fuzz campaign, and the review job.Local runs on
3052a7ec4, which sat on #1709 at39908044band #1687 atfa2a15849:cargo nextest run -p stack-auth -p stack-kms -p stack-encrypt -p stack-encrypt-derive -p stack-encrypt-jsonpath -p stack-auth-node -p stack-auth-wasm --all-features, which includes the compile tests and the tests that watch freed memory for plaintext: 2051 passed, 2 failed, 2 skipped. The two failures are stack-auth'stest_poll_for_token_clamps_an_oversized_device_code_lifetimeandtest_poll_for_token_clamps_an_oversized_token_lifetime. They are not caused by this PR: they fail the same way on feat(stack-encrypt,eql-bindings): typed JSON queries from a field reference #1687's head with neither this PR nor feat(stack-encrypt)!: attach a credential to a call, and load a keyset when a call needs it #1709 applied (7 of 8 runs of the two tests), and neither PR changes the device-code files. They passed in every earlier full run here and in CI on3052a7ec4, and the machine was under heavy load from other builds during this run. The compiler is the list of key sources that need the new impl, and it found none missing.cargo clippy --workspace --all-features --all-targets -- -D warningsandcargo fmt --all -- --check: clean.cargo test -p eql-bindings -p eql-encryption-tests --all-features: 414 passed, 64 ignored. The ignored tests need a PostgreSQL database.Local runs on
e8a057444, two pushes before3052a7ec4, before #1687 gained three commits that change none of this PR's files:cargo test --no-default-features: stack-kms 106 passed, stack-encrypt 1008 passed.cargo docwith warnings as errors,mise run wasm:wasi-check, andcargo check -p stack-auth-wasm --target wasm32-unknown-unknown: clean.cargo test --features eqlon the native build of the encrypt guest, 77 passed.cargo check --target wasm32-wasip1for the encrypt guest witheqland for the auth guest: clean.Not run locally: the mutation gate, which covers stack-auth and stack-encrypt. This PR changes no code of either outside test modules and one doc comment. CI runs the gate, and it passed on the head.
Related
Refs #1708. Builds on #1709, merged.
Review notes
packages/stack-kms/src/key_source.rsfirst: the trait, the three supertraits, and theStackKmsimpl. The rest of the diff is the one-line impl repeated.KeySource<I>trait stays as it is. Only its test doubles and one doc sentence change.KeySourceKind::Kindhas no bound. AKindother than the two is useless, because none of the three traits accepts it.KeySourceKind: a key source of your own, and a key source of both kinds that must not compile. Stack Encrypt adds no test of it.