diff --git a/languages/golang/CHANGELOG.md b/languages/golang/CHANGELOG.md index 6833efd30..b009c9de8 100644 --- a/languages/golang/CHANGELOG.md +++ b/languages/golang/CHANGELOG.md @@ -22,6 +22,14 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit. `[16]byte` (`github.com/google/uuid.UUID`, say), which is converted to one. `stashgen` does not yet map a Go type to it. +### Changed + +- The `encrypt` package documentation, its README, and the example teach + `Cipher.Scope` (#1854). Doc comments that said "extension" now say + "scope", such as those of `Cipher` and `encrypttest.Extension`. + `Cipher.Extend` and `Cipher.Extension` still work, as deprecated aliases + until 0.1.0. No behaviour changes. + ## [0.1.0-rc.7] - 2026-11-25 ### Upgrading from rc.6 diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 6e67a1e8a..450561868 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -199,23 +199,30 @@ key is consumed by `NewClient` and wiped, whatever the outcome. ## The cipher -A `Cipher` is the client bound to one keyset, and to any extension of the +A `Cipher` is the client bound to one keyset, and to any scopes of the context. Every generated function takes one. ```go -cipher := client.Keyset(encrypt.KeysetName("tenant-42")).Extend("tenant-42") +cipher := client.Keyset(encrypt.KeysetName("tenant-42")).Scope("tenant-42") ``` -`Extend` appends to the context that the tags declare, for every field, in -every call through the cipher: the write, the query and the read. A row -written through `Extend("tenant-42")` opens and matches only through a cipher -with the same extension. No call takes a keyset or a context, so the three +`Scope` returns a new cipher, and leaves the one it was called on unchanged. +The new cipher adds the scope to the context the tags declare, for every +field and every call: the write, the query and the read. A row +written through `Scope("tenant-42")` opens and matches only through a cipher +with the same scope. No call takes a keyset or a context, so the three cannot use different ones. +Each part of a scope is a string, a byte slice or an integer: `int32`, +`int64`, `uint32`, `uint64` or `int`. An `int` is sent as `int64`. Several parts +nest in order, so `Scope(a, b)` is the same as `Scope(a).Scope(b)`. A part of +any other type makes the first write, query or read through the cipher fail +with `ErrEncoding`. + > **Take care** > > The context binds each value to its table, its column and the cipher's -> extension. It does not bind the value to its row, so a value copied to +> scopes. It does not bind the value to its row, so a value copied to > another row of the same table decrypts there with no error. To bind each > row to a context of its own, such as its tenant, give the struct a > `context_field` instead of a `context=` tag: @@ -345,14 +352,18 @@ request. ## Reading -`users.Decrypt` takes a `Decrypter`: the `*Cipher`, which refuses a row -another keyset sealed with `ErrForeignKeyset` before any key is retrieved, or -the `*Client`, which opens each row under the keyset that sealed it. The -`*Client` opens only rows that a cipher with no extension sealed; a row -written through `Extend` opens only through a cipher with the same extension. +`users.Decrypt` takes a `Decrypter`: the `*Cipher` or the `*Client`. The +`*Cipher` refuses a row another keyset sealed with `ErrForeignKeyset`, before +any key is retrieved. The `*Client` opens each row under the keyset that +sealed it. + +The `*Client` opens only rows that a cipher with no scope and no binding +sealed. A row written through `Scope` or `Bind` opens only through a cipher +with the same scopes and bindings. + For a type with a `context_field`, the `*Cipher` from `cipher.Context(..)` also refuses a row whose stored context is another, with -`ErrContextMismatch`; the `*Client` opens each row under the context it +`ErrContextMismatch`. The `*Client` opens each row under the context it stores. ## What is stored diff --git a/languages/golang/encrypt/checker.go b/languages/golang/encrypt/checker.go index ca448fbc3..ba3318f8c 100644 --- a/languages/golang/encrypt/checker.go +++ b/languages/golang/encrypt/checker.go @@ -121,7 +121,7 @@ func (k *Checker) Operators(ctx context.Context, plan *record.Plan, field string // context read from a field of each record shows as a placeholder. The text // is the engine's, and the same as a Rust record's contexts() for the same // declaration, so a test that pins it agrees with Rust. A plan the engine -// refuses is [ErrEncoding]. For the contexts a cipher's extension adds, use +// refuses is [ErrEncoding]. For the contexts a cipher's scopes add, use // [Cipher.Contexts]. func (k *Checker) Contexts(ctx context.Context, plan *record.Plan) (string, error) { if err := plan.Validate(); err != nil { diff --git a/languages/golang/encrypt/cipher.go b/languages/golang/encrypt/cipher.go index 6f4a1b77a..15ccd3f2c 100644 --- a/languages/golang/encrypt/cipher.go +++ b/languages/golang/encrypt/cipher.go @@ -8,22 +8,25 @@ import ( "github.com/cipherstash/stack/languages/golang/internal/record" ) -// Cipher is a [Client] bound to one keyset, and to any extension of the +// Cipher is a [Client] bound to one keyset, and to any scopes of the // context: the Go form of the Rust crate's KeysetCipher. Generated code seals // values, derives terms and opens records through it; a program never calls // the engine directly. A Cipher opens only its own keyset's ciphertexts — a // leaf sealed under another keyset is refused as [ErrForeignKeyset] before // any key is retrieved. To open ciphertexts from any keyset, pass the // [Client] where a [Decrypter] is taken. A Client opens only rows that a -// cipher with no extension encrypted; rows sealed through [Cipher.Extend] -// open only through a cipher with the same extension. +// cipher with no scope and no binding encrypted; rows sealed through +// [Cipher.Scope] or [Cipher.Bind] open only through a cipher with the same +// scopes and bindings. // // A Cipher holds no guest state: the keyset is selected on every call, and // loaded by the guest on first use, so one is cheap to make per request or // per tenant. type Cipher struct { - client *Client - keyset KeysetSelector + client *Client + keyset KeysetSelector + // extension is the caller's scopes, in order, from [Cipher.Scope]: in + // the stored context and the query context of every field. extension []any // bindings is the caller's bindings, in order, after the scopes: in the // stored context of every field, and in no query context. @@ -35,8 +38,9 @@ type Cipher struct { // request; nil sends none. The facts are never changed after they are // made, so ciphers share them. audit auditFacts - // err is a refused extension part, context or audit value, reported by - // the first call rather than by Extend, Context or Audit: a cipher is + // err is a refused scope or binding part, context or audit value, + // reported by the first call rather than by Scope, Bind, Context or + // Audit: a cipher is // made without a request, and no call on this package panics. err error } diff --git a/languages/golang/encrypt/doc.go b/languages/golang/encrypt/doc.go index 3003f54b6..d61ba71cd 100644 --- a/languages/golang/encrypt/doc.go +++ b/languages/golang/encrypt/doc.go @@ -100,8 +100,8 @@ // wiped before the instance is freed. // // A [Cipher] is the client bound to one keyset ([Client.Keyset] and -// [Client.DefaultKeyset]) and to any extension of the context -// ([Cipher.Extend]): what changes from one caller to the next — a tenant, a +// [Client.DefaultKeyset]) and to any scopes of the context +// ([Cipher.Scope]): what changes from one caller to the next — a tenant, a // region — attaches here and not to each call, so the write, the query and // the read cannot use different ones. A Cipher opens only its own keyset's // records; the [Client] opens records from any of its keysets. Both are a diff --git a/languages/golang/encrypt/encrypttest/encrypttest.go b/languages/golang/encrypt/encrypttest/encrypttest.go index d72919d43..813af08e9 100644 --- a/languages/golang/encrypt/encrypttest/encrypttest.go +++ b/languages/golang/encrypt/encrypttest/encrypttest.go @@ -88,8 +88,9 @@ type Placeholder struct { FromPlaintext bool } -// Extension is the optional extension a plan accepts from the call that -// runs it: "{extension?: CallerContext}". +// Extension is the optional part a plan accepts from the call that runs +// it: the scopes of an [encrypt.Cipher.Scope] cipher. The engine prints it +// by its own name: "{extension?: CallerContext}". func Extension() Placeholder { return Placeholder{Name: "extension", Type: "CallerContext", Optional: true} } diff --git a/languages/golang/encrypt/example/main.go b/languages/golang/encrypt/example/main.go index d4234d800..8e996dadd 100644 --- a/languages/golang/encrypt/example/main.go +++ b/languages/golang/encrypt/example/main.go @@ -55,9 +55,9 @@ func run(ctx context.Context) error { fmt.Printf("id %d matches bob@example.com: %v\n", e.ID, term.Equal(e.Email.Equality)) } - // Decrypt through the cipher. It carries the extension that Encrypt - // used, and it refuses rows from another keyset. The client would refuse - // these rows: it opens only rows sealed with no extension. + // Decrypt through the cipher. It carries the scope that Encrypt used, + // and it refuses rows from another keyset. The client would refuse these + // rows: it opens only rows sealed with no scope and no binding. // // Audit gives the cipher audit metadata: facts about the call, which // ZeroKMS writes to its audit log with the request for the data keys. diff --git a/languages/golang/encrypt/live_test.go b/languages/golang/encrypt/live_test.go index ce127a13d..d86b3c965 100644 --- a/languages/golang/encrypt/live_test.go +++ b/languages/golang/encrypt/live_test.go @@ -69,8 +69,8 @@ func TestLiveRecordsAndTerms(t *testing.T) { t.Fatalf("Decrypt through the client: %v %+v", err, back) } - // A context extension is part of the identity: the write, the query - // and the read all go through the extended cipher. + // A scope is part of the identity: the write, the query and the read + // all go through the scoped cipher. tenant7, tenant8 := cipher.Extend(uint64(7)), cipher.Extend(uint64(8)) ext, err := testusers.Encrypt(ctx, tenant7, livePeople) if err != nil { diff --git a/languages/golang/encrypt/records.go b/languages/golang/encrypt/records.go index 6072b2e99..3cb5676f7 100644 --- a/languages/golang/encrypt/records.go +++ b/languages/golang/encrypt/records.go @@ -20,15 +20,15 @@ import ( // Decrypter opens records: a *Cipher, which refuses a record sealed under // another keyset before any key is retrieved, or a *Client, which opens each // record under the keyset that sealed it. A *Client opens only records that -// a cipher with no extension sealed, and checks no stored context field -// against a label: that is [Cipher.Context]. Generated Decrypt functions -// take one. Its method is for generated code; a program does not call it. +// a cipher with no scope and no binding sealed, and checks no stored +// context field against a label: that is [Cipher.Context]. Generated +// Decrypt functions take one. Its method is for generated code; a program does not call it. type Decrypter interface { Open(ctx context.Context, plan *record.Plan, records []record.Sealed) ([]record.Source, error) } // Contexts shows the contexts this cipher seals and indexes the plan's -// fields under, with the cipher's extension applied, before any key is +// fields under, with the cipher's scopes applied, before any key is // requested: what [Cipher.Seal] will use for every row. The text is // [Checker.Contexts]'s, one line per field and one indented line with its // context. A plan the engine refuses is [ErrEncoding]. @@ -41,7 +41,7 @@ func (cph *Cipher) Contexts(ctx context.Context, plan *record.Plan) (string, err } // Seal encrypts rows under the plan, through this cipher's keyset and with -// its extension: the sealed fields of every row, in order, with one ZeroKMS +// its scopes: the sealed fields of every row, in order, with one ZeroKMS // request for each 500 sealed values. For generated code. func (cph *Cipher) Seal(ctx context.Context, plan *record.Plan, rows []record.Source) ([]record.Sealed, error) { p, err := cph.plan(plan) @@ -82,7 +82,7 @@ func (cph *Cipher) Seal(ctx context.Context, plan *record.Plan, rows []record.So return inst.call(ctx, inst.encryptRecord, buf(encodedSource), buf(encodedPlan), buf(opts)) }) if errors.Is(err, ErrTerm) { - // The plan as given: derive applies the extension itself. + // The plan as given: derive applies the scopes itself. return nil, cph.locateTermFailure(ctx, plan, rows, err) } if err != nil { @@ -127,7 +127,7 @@ func (cph *Cipher) Open(ctx context.Context, plan *record.Plan, records []record } // derive derives one index term for one value of a field, under the plan's -// context for that field and this cipher's extension: the term the record +// context for that field and this cipher's scopes: the term the record // call stores for it. It is how locateTermFailure names the field // behind a record's term failure; a query asks [Cipher.QueryTerm], which // names an operator. A plan that takes its context from a field has none of @@ -175,7 +175,7 @@ func (cph *Cipher) derive(ctx context.Context, plan *record.Plan, field string, } // queryPlan is the plan a query on field derives under: the declaration's -// plan with this cipher's extension and, for a plan that takes its context +// plan with this cipher's scopes and, for a plan that takes its context // from a field, the context named with [Cipher.Context] in that field's // place. A cipher that names none is refused as [ErrEncoding]: a term // derives under a context, and such a plan has none until a record names it. @@ -265,7 +265,8 @@ func (c *Client) openAny(ctx context.Context, audit auditFacts, plan *record.Pla return c.open(ctx, callOptions{keyset: anyKeyset{}, audit: audit}, plan, records) } -// plan is the declaration's plan with this cipher's extension, checked. A +// plan is the declaration's plan with this cipher's scopes and bindings, +// checked. A // cipher that names a context serves a plan that takes its context from a // field and no other: for one with a context of its own there is nothing // the label could be, so it is refused as two sources, as the engine @@ -560,7 +561,7 @@ func (c *Client) openEncoded(ctx context.Context, call callOptions, p *record.Pl // as the empty filter), so a term failure on a record is the scheme refusing // the value's kind under that index. derive asks the query path, which does // refuse text with no token, so that one refusal is skipped here: it cannot -// be why the record failed. p is the plan without this cipher's extension, +// be why the record failed. p is the plan without this cipher's scopes, // which derive adds. func (cph *Cipher) locateTermFailure(ctx context.Context, p *record.Plan, rows []record.Source, err error) error { for i, row := range rows { diff --git a/languages/golang/internal/record/record.go b/languages/golang/internal/record/record.go index f11905647..10ad6ee6a 100644 --- a/languages/golang/internal/record/record.go +++ b/languages/golang/internal/record/record.go @@ -12,7 +12,8 @@ // "float32", "float64", "decimal", "date", "timestamp", "string", // "bytes", "uuid" and "json". A field's context is // its label — the plan's context segments and the field's identity — nested -// to the left under each part the caller extends it by. +// to the left under each scope the caller adds. The caller's bindings come +// after the scopes, in the stored context only. // // Passthrough fields do not appear in a Plan. The Go SDK keeps them on the // host: the FFI codec cannot carry every Go type a program stores beside a @@ -243,8 +244,8 @@ func (f Field) HasCiphertext() bool { return false } -// Plan is a declaration as the engine reads it: one context, any extension, -// and the sealed fields. +// Plan is a declaration as the engine reads it: one context, any scopes and +// bindings, and the sealed fields. type Plan struct { // Context is the label every field's label extends: at least one plain // segment. The guest requires a field's label to have two or more @@ -319,7 +320,7 @@ func CheckSegment(s string) error { // list as Rust's Label::INVISIBLE. const invisible = "\u00ad\u061c\u180e\u200b\u200c\u200d\u200e\u200f\u202a\u202b\u202c\u202d\u202e\u2060\u2061\u2062\u2063\u2064\u2066\u2067\u2068\u2069\ufeff" -// CheckPart refuses a context extension part that is not a string, a byte +// CheckPart refuses a scope part that is not a string, a byte // slice or an integer the codec carries. func CheckPart(part any) error { switch part.(type) { @@ -360,7 +361,7 @@ var Reserved = []string{"context_field", "bind"} // Validate checks what the host can check before the engine sees the plan: // a context, plain segments, at least one field, names and identities once, // outputs once and at least one, known kinds, a kind on every indexed field, -// and extension parts the codec carries. The engine's own rules — which index a kind admits, what the +// and scope and binding parts the codec carries. The engine's own rules — which index a kind admits, what the // builder refuses — are the engine's, asked through se_plan_check. func (p *Plan) Validate() error { switch { @@ -556,7 +557,7 @@ func (p *Plan) Wire() vcvalue.Object { } // FieldContext is one field's context as the guest reads it: the label as a -// flat list of its segments, then each extension part nested to the left — +// flat list of its segments, then each scope nested to the left — // [[["users", "age"], 7], "eu"] — exactly the shape se_term takes for a // probe of that field. Under a context field the label is the identity // alone: [["age"], 7]. diff --git a/languages/golang/stashgen/engine.go b/languages/golang/stashgen/engine.go index 8b3db53ed..9cc279bdd 100644 --- a/languages/golang/stashgen/engine.go +++ b/languages/golang/stashgen/engine.go @@ -441,7 +441,7 @@ func kindWord(k record.Kind) string { // describeField names what the engine was asked for a field: the EQL type // it names, or its outputs. For an EQL type the engine refuses, the refusal // is the engine's (an unknown or unproducible type, a kind other than its -// plaintext, an extended context); the name is what a reader needs. +// plaintext, a context with scopes or bindings); the name is what a reader needs. func describeField(f record.Field) string { if f.IsTarget() { return "the EQL type " + f.Target diff --git a/languages/golang/stashgen/testdata/stubsdk/encrypt/encrypt.go b/languages/golang/stashgen/testdata/stubsdk/encrypt/encrypt.go index e15eb2e04..a89e1560a 100644 --- a/languages/golang/stashgen/testdata/stubsdk/encrypt/encrypt.go +++ b/languages/golang/stashgen/testdata/stubsdk/encrypt/encrypt.go @@ -12,7 +12,7 @@ import ( "time" ) -// Cipher is a keyset with any extension of the context. +// Cipher is a keyset with any scopes of the context. type Cipher struct{ _ struct{} } // Client decrypts under the keyset that sealed each value.