Skip to content

Commit ea41543

Browse files
committed
fix(stack-encrypt): address the review of the context views
A JSON document's open paths now record their context, as the seal path does. A call that has already failed says so in `context()` instead of showing no context. Text that could read as a placeholder or an encoded context is quoted, with the module's own escapes, so two contexts never print the same text. `under` and `extend` no longer need `Clone` on the context type. `ContextTree` and `Supplier` are `#[non_exhaustive]` and `SuppliedContext` is sealed, so the API can grow. The docs say what a placeholder stands for, and that the field form of `assert_context!` is partial; the macro also takes a string for a field name an identifier cannot spell. The Go `Contexts` method moves above `Seal`'s doc comment, and `encrypttest` gets tests of its own. Refs #1568, #1590.
1 parent d3b4b6d commit ea41543

15 files changed

Lines changed: 458 additions & 63 deletions

File tree

‎languages/golang/encrypt/encrypttest/encrypttest.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func Contexts(ctx context.Context, d gensupport.Declaration) (string, error) {
3939
// "age\n context: users / age\n")
4040
func AssertContexts(tb testing.TB, d gensupport.Declaration, want string) {
4141
tb.Helper()
42-
got, err := Contexts(context.Background(), d)
42+
got, err := Contexts(tb.Context(), d)
4343
if err != nil {
4444
tb.Fatalf("encrypttest: the engine refused the declaration: %v", err)
4545
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package encrypttest_test
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"strings"
8+
"testing"
9+
10+
"github.com/cipherstash/stack/languages/golang/encrypt"
11+
"github.com/cipherstash/stack/languages/golang/encrypt/encrypttest"
12+
"github.com/cipherstash/stack/languages/golang/encrypt/gensupport"
13+
)
14+
15+
// recorder is a testing.TB whose Fatalf records the message instead of
16+
// stopping the test, so the failure text of AssertContexts can be checked.
17+
type recorder struct {
18+
testing.TB
19+
failed string
20+
}
21+
22+
func (r *recorder) Helper() {}
23+
func (r *recorder) Fatalf(format string, args ...any) {
24+
r.failed = strings.TrimSpace(fmt.Sprintf(format, args...))
25+
}
26+
27+
func users() gensupport.Declaration {
28+
return gensupport.Declare("users").
29+
EncryptIndex("email", gensupport.String, encrypt.Equality).
30+
Encrypt("notes", gensupport.String)
31+
}
32+
33+
func skipWithoutGuest(t *testing.T) {
34+
t.Helper()
35+
if _, err := encrypttest.Contexts(context.Background(), users()); errors.Is(err, encrypt.ErrGuestNotBuilt) {
36+
t.Skip(err)
37+
}
38+
}
39+
40+
func TestAssertContextsPassesOnTheEnginesText(t *testing.T) {
41+
skipWithoutGuest(t)
42+
encrypttest.AssertContexts(t, users(),
43+
"email\n context: users / email\n"+
44+
"notes\n context: users / notes\n")
45+
// A trailing newline is optional.
46+
encrypttest.AssertContexts(t, users(),
47+
"email\n context: users / email\n"+
48+
"notes\n context: users / notes")
49+
}
50+
51+
func TestAssertContextsNamesTheFirstLineThatDiffers(t *testing.T) {
52+
skipWithoutGuest(t)
53+
r := &recorder{TB: t}
54+
encrypttest.AssertContexts(r, users(),
55+
"email\n context: users / email\n"+
56+
"notes\n context: users / name\n")
57+
if !strings.Contains(r.failed, "differ at line 4") || !strings.Contains(r.failed, `want: " context: users / name"`) {
58+
t.Fatalf("failure text: %q", r.failed)
59+
}
60+
r = &recorder{TB: t}
61+
encrypttest.AssertContexts(r, users(), "email\n context: users / email\n")
62+
if !strings.Contains(r.failed, "have 4 lines, and 2 were expected") {
63+
t.Fatalf("failure text: %q", r.failed)
64+
}
65+
}
66+
67+
func TestAssertContextsReportsARefusedDeclaration(t *testing.T) {
68+
skipWithoutGuest(t)
69+
bad := gensupport.Declare("users").EncryptIndex("age", gensupport.Uint32, encrypt.Match())
70+
r := &recorder{TB: t}
71+
encrypttest.AssertContexts(r, bad, "")
72+
if !strings.Contains(r.failed, "the engine refused the declaration") {
73+
t.Fatalf("failure text: %q", r.failed)
74+
}
75+
}

‎languages/golang/encrypt/records.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ type Decrypter interface {
2727
Open(ctx context.Context, plan *record.Plan, records []record.Sealed) ([]record.Source, error)
2828
}
2929

30-
// Seal encrypts rows under the plan, through this cipher's keyset and with
31-
// its extension: the sealed fields of every row, in order, with one ZeroKMS
32-
// request for each 500 sealed values. For generated code.
3330
// Contexts shows the contexts this cipher seals and indexes the plan's
3431
// fields under, with the cipher's extension applied, before any key is
3532
// requested: what [Cipher.Seal] will use for every row. The text is
@@ -43,6 +40,9 @@ func (cph *Cipher) Contexts(ctx context.Context, plan *record.Plan) (string, err
4340
return contextsOf(ctx, cph.client, p)
4441
}
4542

43+
// Seal encrypts rows under the plan, through this cipher's keyset and with
44+
// its extension: the sealed fields of every row, in order, with one ZeroKMS
45+
// request for each 500 sealed values. For generated code.
4646
func (cph *Cipher) Seal(ctx context.Context, plan *record.Plan, rows []record.Source) ([]record.Sealed, error) {
4747
p, err := cph.plan(plan)
4848
if err != nil {

‎packages/eql/crates/eql-bindings/src/encryption.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,7 @@ impl<'a> IntoContext<'a> for Identifier {
190190
/// and keep the identifier for the next call.
191191
impl<'a> IntoContext<'a> for &'a Identifier {
192192
fn into_context(self) -> ContextPiece<'a> {
193-
ContextPiece::List(vec![
194-
ContextPiece::Text(std::borrow::Cow::Borrowed(self.t.as_str())),
195-
ContextPiece::Text(std::borrow::Cow::Borrowed(self.c.as_str())),
196-
])
193+
self.to_context()
197194
}
198195
}
199196

‎packages/eql/tests/encryption/tests/contexts.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ fn a_shim_shows_the_column_read_from_the_plaintext() {
9191
let contexts = <Shim as EncryptTarget<V3Plaintext>>::encryption::<()>().contexts();
9292
assert_eq!(
9393
contexts.to_string(),
94-
"c\n context: {context: Identifier, from the plaintext}\nhm\n context: {context: Identifier, from the plaintext}\n"
94+
"c\n context: {context: NonEmpty<Identifier>, from the plaintext}\nhm\n context: {context: NonEmpty<Identifier>, from the plaintext}\n"
9595
);
9696
assert_context!(
9797
contexts,
98-
c = [from_plaintext::<Identifier>("context")],
99-
hm = [from_plaintext::<Identifier>("context")],
98+
c = [from_plaintext::<NonEmpty<Identifier>>("context")],
99+
hm = [from_plaintext::<NonEmpty<Identifier>>("context")],
100100
);
101101
}

‎packages/stack-encrypt-derive/src/owned_encrypt.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn contexts_fn(input: &DeriveInput, record: &Record) -> TokenStream {
107107
let source = &sources[0];
108108
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
109109
let doc = format!(
110-
"The contexts this record's fields are sealed and indexed under, with a placeholder for each part the caller supplies. No key is requested. Emitted by `#[derive(EncryptTarget)]`; see [`Contexts`]({}::Contexts).",
110+
"The contexts this record's fields are sealed and indexed under, with a placeholder for each part the caller supplies. No key is requested. Emitted by `#[derive(EncryptTarget)]`, so a record cannot define another method named `contexts`; see [`Contexts`]({}::Contexts).",
111111
quote!(#krate)
112112
);
113113
// A record that names no plaintext is generic over it. Then the

‎packages/stack-encrypt/CHANGELOG.md‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Breaking
1111

12-
- **The context parameter of the cipher-directed methods is named
13-
`context`, not `aad`** (#1568): `KeysetCipher::encrypt_tree`,
14-
`KeysetCipher::decrypt_tree`, `StackCipher::decrypt`,
15-
`PendingStackCipherText::plan`, `seal`, and `into_pending`. A positional
16-
call does not change. The stored format does not change.
17-
1812
- **`OpaqueDecode` no longer requires vitaminc's `Decrypt` trait.** If you
1913
only name types that implement `OpaqueDecode`, nothing changes, because
2014
the trait is sealed. If your generic code is bounded on `P: OpaqueDecode`
@@ -365,6 +359,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
365359

366360
### Changed
367361

362+
- **The context parameter of the cipher-directed methods is named
363+
`context`, not `aad`** (#1568): `KeysetCipher::encrypt_tree`,
364+
`KeysetCipher::decrypt_tree`, `StackCipher::decrypt`,
365+
`PendingStackCipherText::plan`, `seal`, and `into_pending`. A positional
366+
call does not change. The stored format does not change.
368367
- `vitaminc-aead-frame` is now always built, with its `serde` feature.
369368
Before, it was built only with the `dynamic` feature. The `dynamic`
370369
feature now adds no dependency.

‎packages/stack-encrypt/docs/adr/0004-one-context-per-target-threaded-through-the-declaration-tree.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,11 @@ see the context. A test must be able to state it.
412412
names the first part that differs. The Go module has
413413
`encrypttest.AssertContexts`.
414414

415-
The views are not a second description of the context. Three combinators
416-
change the context of a subtree: `under`, `extend`, and `context_from`.
417-
Each one records the change beside the closure it builds. Each leaf records
418-
the context it validated. So the same code path builds the view, the key
419-
descriptor, and the AAD.
415+
The views are not written by hand. Three combinators change the context
416+
of a subtree: `under`, `extend`, and `context_from`. Each one records the
417+
change beside the closure it builds, in the same call. Each leaf records
418+
the context it validated. So the declaration view changes only where the
419+
context does, and the call view is the context the engine used.
420420

421421
The view shows each part's type, because `7u64`, `7i64`, the text `7`, and
422422
the bytes `0x37` are four different contexts. The descriptor that ZeroKMS

0 commit comments

Comments
 (0)