Skip to content

Commit ffeed7a

Browse files
authored
Merge pull request #1402 from cipherstash/docs/go-typed-json-documentation
docs(go): typed JSON walkthrough and accurate EQL reference
2 parents b0c1513 + 238ac9a commit ffeed7a

14 files changed

Lines changed: 655 additions & 19 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Contributing to CipherStash Stack
22

3-
Thank you for your interest in contributing to the CipherStash Stack for TypeScript! This document walks you through the repository's structure, how to build and run the project locally, and how to make contributions effectively.
3+
Thank you for your interest in contributing to CipherStash Stack. This document walks you through the repository's structure, how to build and run the project locally, and how to make contributions effectively.
44

55
## I want to report a bug, or make a feature request
66

@@ -20,6 +20,7 @@ This is a [Turborepo](https://turbo.build/) monorepo managed with [pnpm](https:/
2020
│ │ ├── cli/ <-- The `stash` CLI
2121
│ │ └── ... <-- stack-drizzle, stack-supabase, stack-prisma, nextjs, migrate, wizard, ...
2222
│ └── examples/ <-- Runnable example apps
23+
├── languages/golang/ <-- Go SDK, stashgen and credential packages
2324
├── packages/eql/ <-- EQL (@cipherstash/eql and its Rust crates)
2425
├── e2e/ <-- Cross-package end-to-end tests
2526
├── skills/ <-- Agent skills
@@ -29,6 +30,8 @@ This is a [Turborepo](https://turbo.build/) monorepo managed with [pnpm](https:/
2930

3031
See [AGENTS.md](./AGENTS.md) for a detailed layout, key APIs, environment variables, and gotchas — it's written for coding agents but is the most complete developer reference in the repo.
3132

33+
For the Go module, start with [languages/golang/README.md](languages/golang/README.md).
34+
3235
### `languages/typescript/packages/stack`
3336

3437
**@cipherstash/stack** is the main package published to npm. It contains the encryption client and all integrations (Drizzle, Supabase, DynamoDB, secrets, identity). This is likely where you'll spend most of your time.
@@ -44,6 +47,7 @@ The `languages/typescript/examples/` directory contains applications demonstrati
4447
- **Node.js** >= 22
4548
- **pnpm** (the version pinned in `package.json`'s `packageManager` field)
4649
- CipherStash credentials if you want to run integration tests or examples (see [AGENTS.md](./AGENTS.md) for the required environment variables)
50+
- For Go work: mise and the Go and Rust toolchains pinned in `mise.toml`
4751

4852
### 1. Clone the Repo
4953

@@ -76,6 +80,27 @@ pnpm run dev
7680

7781
Then navigate to one of the examples in `languages/typescript/examples/` and follow its README.
7882

83+
### Go module
84+
85+
The Go module is in `languages/golang/`, outside the pnpm workspace. Its
86+
`encrypt` and `auth` packages embed WASI guests built from Rust. In a source
87+
checkout, build those guests before running the Go suite:
88+
89+
```bash
90+
mise run wasm:guest:build
91+
mise run wasm:guest:build:eql
92+
mise run wasm:guest:build:deterministic
93+
mise run wasm:guest:build:eql:deterministic
94+
mise run wasm:auth-guest:build
95+
mise run go:test
96+
```
97+
98+
For EQL v4 PostgreSQL coverage, set `EQL_TEST_DATABASE_URL` and run
99+
`mise run go:test:postgres`. Go examples that connect to ZeroKMS need
100+
CipherStash credentials. The [Go module guide](languages/golang/README.md)
101+
links to the SDK, generator, credentials, examples and release notes. Commit
102+
generated `*_stash.go` files when changing Go model tags; CI checks for drift.
103+
79104
## Making Changes
80105

81106
1. **Create a new branch** from `main`:

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ npx stash init
5555

5656
> The Stack also ships a `stash` CLI for auth, schema, and database setup. See the [SDK reference][reference].
5757
58+
Building in Go? Start with the [Go SDK guide](languages/golang/README.md) for
59+
typed field encryption, generated methods and EQL v4 PostgreSQL queries.
60+
5861
## What's in the Stack
5962

6063
### 🔐 Searchable encryption

‎languages/golang/CHANGELOG.md‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ listed under **Breaking**. Releases are tagged `languages/golang/vX.Y.Z`.
9292
`eql.JSONSearchLeafQuery` and its entry `eql.JSONEntry`,
9393
generated from the EQL catalog. The names are the ones EQL v3 gives its
9494
searchable JSON document; `eql.JSON` stays the storage-only type. The
95-
engine stores one from the module's own plans; `stashgen` does not
96-
generate a field of it yet.
95+
engine stores one from the module's own plans, and `stashgen` generates
96+
fields tagged `encrypt_into=JsonSearch`.
9797

9898
- **Decimals, dates and timestamps take an order index (#1236).** An
9999
`encrypt.Decimal`, `encrypt.Date` or `time.Time` field takes `ore` and
@@ -252,7 +252,8 @@ listed under **Breaking**. Releases are tagged `languages/golang/vX.Y.Z`.
252252
implements `driver.Valuer`, `sql.Scanner` and JSON like the other query
253253
types. They are not in
254254
`eql.Types`, which lists stored types, and no generated method returns one
255-
yet: the ordered EQL types are not producible.
255+
yet: `stashgen` does not construct a range query operand, although the
256+
ordered EQL types are producible and expose single-bound comparisons.
256257
- **`encrypt.MatchQuery` and `encrypt.EQLMatchQuery`**: what a `Matches`
257258
method returns. Each carries the term or EQL query value to bind,
258259
`Verify` (check decrypted rows, since a match can return false positives)
@@ -290,12 +291,13 @@ listed under **Breaking**. Releases are tagged `languages/golang/vX.Y.Z`.
290291
these types: it is not a tag word.
291292
- **`TextSearch`**: a `string` field tagged `encrypt_into=TextSearch` gets
292293
`Eq`, `Lt`, `Lte`, `Gt`, `Gte` and `Matches`, one column for all of them.
293-
The stored value keeps all three terms. Text shorter than three
294-
characters, an empty string included, is stored with an empty match
294+
The stored value keeps all three terms. Text that cuts no match token,
295+
such as an empty string or one letter, is stored with an empty match
295296
filter, where `TextMatch` refuses it with `ErrTerm`: `Eq`, `Lt`, `Lte`,
296297
`Gt` and `Gte` find the row, and no `Matches` query does. Each query
297298
carries only the term its operator reads, so `Eq` and `Lt` take a
298-
two-letter value and `Matches` refuses one (`ErrTerm`). Needs the EQL v4
299+
one-letter value and `Matches` refuses one (`ErrTerm`). A two-letter
300+
word has its own match token, as described above. Needs the EQL v4
299301
SQL from this release, whose `eql_v4.query_text_search` accepts one term.
300302
- **Ordering methods on text.** A string field with `index=ore` or
301303
`index=ope` gets `Lt`, `Lte`, `Gt`, `Gte` and `Between`. Text orders by its

‎languages/golang/README.md‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# CipherStash Stack for Go
2+
3+
This module provides searchable field encryption for Go structs. `stashgen`
4+
turns `stash` tags into typed encryption, decryption and query methods. The
5+
`encrypt` package runs the Rust encryption engine in an embedded WASI guest
6+
with wazero; applications do not need cgo.
7+
8+
Start with the [SDK guide](encrypt/README.md) for installation, credentials,
9+
generation and PostgreSQL storage. The [stashgen reference](cmd/stashgen/README.md)
10+
explains tags, supported field types and generated methods. The
11+
[typed JSON document example](encrypt/example/json/README.md) walks through
12+
an EQL v4 table, queries and partial reads. The [credential guide](auth/README.md)
13+
covers profile, access key and federation strategies. See the
14+
[Go changelog](CHANGELOG.md) for release and compatibility notes.
15+
16+
## Using a released module
17+
18+
Use Go 1.26.10 or later (or 1.27.3 or later on the 1.27 line). Add the
19+
generator to your application module, declare a struct with `stash`
20+
tags, then generate and commit its `*_stash.go` file:
21+
22+
```sh
23+
go get -tool github.com/cipherstash/stack/languages/golang/cmd/stashgen
24+
go generate ./...
25+
```
26+
27+
Use a tagged Go module version. Release tags include the embedded WASI guests;
28+
a checkout or an untagged commit does not. The [SDK guide](encrypt/README.md#building)
29+
explains how to build them locally. Searchable `encrypt_into` fields require
30+
EQL v4 in PostgreSQL; the EQL v4 SQL bundle is attached to the matching Go
31+
release. Go and TypeScript currently use different EQL versions, so follow
32+
the [EQL column guide](encrypt/README.md#eql-columns) when choosing a schema.
33+
34+
## Working in this repository
35+
36+
From the repository root, install the toolchain pinned by `mise.toml` and
37+
build the guests before running Go tests:
38+
39+
```sh
40+
mise run wasm:guest:build
41+
mise run wasm:guest:build:eql
42+
mise run wasm:guest:build:deterministic
43+
mise run wasm:guest:build:eql:deterministic
44+
mise run wasm:auth-guest:build
45+
mise run go:test
46+
```
47+
48+
The `.wasm` files are build outputs and are not committed on development
49+
branches. `mise run go:test:postgres` also runs the PostgreSQL suite against
50+
the EQL v4 bundle built from this tree; it needs the database named by
51+
`EQL_TEST_DATABASE_URL`. See `mise.toml` and [AGENTS.md](../../AGENTS.md)
52+
for the build and release details.

‎languages/golang/cmd/stashgen/README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ These words are the same as the Rust API's words for the same behaviour, with tw
114114
### Encrypted JSON documents
115115

116116
A field with `encrypt_into=JsonSearch` is stored as one encrypted JSON document, an `eql.JSONSearch` in a `public.eql_v4_json_search` column.
117+
For a complete generated model and PostgreSQL query flow, see the [runnable typed-document example](../../encrypt/example/json/README.md).
117118
Declare it as a struct, map or slice type, or as a `json.RawMessage` that holds the document's JSON text:
118119

119120
```go
@@ -289,12 +290,15 @@ The engine produces these EQL types today, each from one Go field type:
289290
| `Real`, `RealEq`, `RealOrd`, `RealOrdOpe`, `RealOrdOre` | `float32` |
290291
| `Double`, `DoubleEq`, `DoubleOrd`, `DoubleOrdOpe`, `DoubleOrdOre` | `float64` |
291292
| `Date`, `DateEq` | `encrypt.Date` |
293+
| `DateOrd`, `DateOrdOpe`, `DateOrdOre` | `encrypt.Date` |
292294
| `Timestamp`, `TimestampEq` | `time.Time` |
295+
| `TimestampOrd`, `TimestampOrdOpe`, `TimestampOrdOre` | `time.Time` |
293296
| `Numeric`, `NumericEq` | `encrypt.Decimal` |
297+
| `NumericOrd`, `NumericOrdOpe`, `NumericOrdOre` | `encrypt.Decimal` |
298+
| `JsonSearch` | struct, map, slice, or `json.RawMessage`; see [Encrypted JSON documents](#encrypted-json-documents) |
294299

295300
The `Eq` types answer `Eq`; the `Ord`, `OrdOpe` and `OrdOre` types answer `Lt`, `Lte`, `Gt` and `Gte`, and on text `Eq` too; `TextMatch` answers `Matches`, and `TextSearch` and `TextSearchOre` all of `Eq`, `Lt`, `Lte`, `Gt`, `Gte` and `Matches`, each query carrying only the term its operator reads; `Text`, `Smallint`, `Integer`, `Bigint`, `Boolean`, `Real`, `Double`, `Date`, `Timestamp` and `Numeric` are stored and read only, with no query method.
296-
The ordered date, timestamp and numeric types are refused: the engine derives no order term for these kinds yet.
297-
An ordered type has no `Between` method yet, because a `BETWEEN` operand needs EQL v4's range query domains.
301+
The ordered date, timestamp and numeric types answer the four comparisons, as do the other ordered EQL types. The generated EQL fields have no `Between` method yet: EQL v4 has range query domains, but `stashgen` does not construct their operands. A JSON document's declared ordered paths have their own `Between` method, which returns two leaf query values; see [Encrypted JSON documents](#encrypted-json-documents).
298302

299303
Any built-in Go integer field takes an integer type (ADR-0002) and seals at the type's width: an `int16` field always fits an `Integer`, and an `int64` field is range checked value by value, `Encrypt` refusing a value that does not fit with `encrypt.ErrEncoding`. `encrypt.Int128` and `encrypt.Uint128` are not converted. A float's width must match: a `float64` field is not a `Real`.
300304
A `TextMatch` field indexes the 3-grams of its text and each two-letter word whole, so a two-letter surname such as "Ng" is stored and found by the query "ng"; a query's two-letter words must be whole words of the row.

‎languages/golang/encrypt/README.md‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ operand of `eql_v4.between(column, $1::eql_v4.query_integer_range_ore)`. Keep
349349
the cast to the exact range domain: an uncast `$1`, a `jsonb` parameter, or a
350350
range of another term or family fails with SQLSTATE 42725 ("function ... is
351351
not unique"), unlike the single-bound operators, which resolve an untyped
352-
`$1`. No generated method returns one yet, because the ordered EQL types are
353-
not producible.
352+
`$1`. The ordered EQL types are producible, but `stashgen` does not yet
353+
generate a `Between` method or construct these range query values. Their
354+
generated fields expose the four single-bound comparisons.
354355
355356
`Eq`, the orderings and `Between` are exact: the server's answer is the
356357
answer. `Matches` is probabilistic: the server answers with every row whose
@@ -512,10 +513,14 @@ The EQL types the engine produces, and the Go field types each takes:
512513
| `DoubleOrd`, `DoubleOrdOpe`, `DoubleOrdOre` | `float64` | `=`, `<>`, `<`, `<=`, `>`, `>=`, `ORDER BY` |
513514
| `Date` | `encrypt.Date` | none |
514515
| `DateEq` | `encrypt.Date` | `=`, `<>` |
516+
| `DateOrd`, `DateOrdOpe`, `DateOrdOre` | `encrypt.Date` | `<`, `<=`, `>`, `>=`, `ORDER BY` |
515517
| `Timestamp` | `time.Time` | none |
516518
| `TimestampEq` | `time.Time` | `=`, `<>` (to the microsecond, truncated) |
519+
| `TimestampOrd`, `TimestampOrdOpe`, `TimestampOrdOre` | `time.Time` | `<`, `<=`, `>`, `>=`, `ORDER BY` |
517520
| `Numeric` | `encrypt.Decimal` | none |
518521
| `NumericEq` | `encrypt.Decimal` | `=`, `<>` (`1.5 = 1.50`) |
522+
| `NumericOrd`, `NumericOrdOpe`, `NumericOrdOre` | `encrypt.Decimal` | `<`, `<=`, `>`, `>=`, `ORDER BY` |
523+
| `JsonSearch` (`eql.JSONSearch`) | struct, map, slice, or `json.RawMessage` | containment, equality at a path, ordering at declared paths, selection, subtree reads; see [Encrypted JSON documents](#encrypted-json-documents) |
519524

520525
Any built-in Go integer field takes an integer EQL type (ADR-0002): the field
521526
is sealed at the type's width, and opens back into the field's type. A type
@@ -552,8 +557,8 @@ decimal with its scale and a `time.Time` with its nanoseconds, in UTC. A
552557
`time.Time` is sealed as its instant, whatever its location. A zero
553558
`encrypt.Date`, a date or instant outside years -262143 to 262142 and a
554559
decimal with more than 28 digits after the point are refused with
555-
`ErrEncoding`. The ordered date, timestamp and numeric types are not
556-
produced yet: the engine derives no order term for these kinds.
560+
`ErrEncoding`. Ordered dates compare by day, timestamps by the truncated
561+
microsecond, and decimals by value (`1.5` ties `1.50`).
557562

558563
The `Ord` and `OrdOpe` types store an OPE term, which EQL compares as bytes,
559564
and order by ADR-0002's order key: integers by value, floats as PostgreSQL's
@@ -570,7 +575,8 @@ answers `Eq` through an equality term only. A text order type also stores
570575
`TextEq`'s equality term, and `=` reads that, so `Café` and `cafe` are
571576
neither `<` nor `>` each other and not `=` either. Text holding a code point
572577
Unicode 16 does not assign has no order term and is refused. There is no
573-
`Between` method yet: a `BETWEEN` needs EQL v4's range query domains.
578+
generated `Between` method on these EQL fields yet, although EQL v4 has
579+
range query domains and `eql_v4.between` for them.
574580

575581
The `OrdOre` types and `TextSearchOre` order by the same key, through a block
576582
ORE term (ore-rs's 6-bit schemes) in place of the OPE term. Each term
@@ -582,9 +588,9 @@ the installer disables these domains, and the `Ord` types are the ones to
582588
use. Text whose fold is longer than 1023 bytes, or ends in NUL, has no block
583589
ORE term and is refused, stored or queried.
584590

585-
The rest of the catalog (ordered dates, timestamps and decimals, the v3 JSON types) is listed in
586-
`eql.Types` with the reason it is not produced yet, and `stashgen` refuses it
587-
with that reason. The guest builds the value: the generated code
591+
The catalog in `eql.Types` also lists EQL types the engine does not produce,
592+
with the reason for each; `stashgen` refuses those types with that reason.
593+
The guest builds a producible value: the generated code
588594
imports `encrypt/eql`, which embeds the build of the engine that holds the
589595
EQL types and registers it on import, so the program runs that build and
590596
stores what it returns. `Fields.Email.Eq` returns the `eql.TextEqQuery`
@@ -601,8 +607,10 @@ column.
601607

602608
`JsonSearch` stores a whole JSON document in one `public.eql_v4_json_search`
603609
column, from a struct, map or slice field, or a `json.RawMessage`
604-
(`stash:"profile,encrypt_into=JsonSearch(ordered=$.age)"`; see the
605-
stashgen README). A struct is marshalled with `encoding/json` when it is
610+
(`stash:"profile,encrypt_into=JsonSearch(ordered=$.age)"`). See the
611+
[stashgen reference](../cmd/stashgen/README.md#encrypted-json-documents) and
612+
the [runnable typed-document example](example/json/README.md). A struct is
613+
marshalled with `encoding/json` when it is
606614
sealed and read back into its type when it is opened. Its numbers come back
607615
exactly; the stashgen README lists what `encoding/json` does not keep
608616
(invalid UTF-8, a nil map or slice, a time's zone). Unlike the types above,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Typed encrypted JSON document example
2+
3+
This program stores a Go `Profile` struct as one encrypted EQL v4 JSON column.
4+
It finds a row by containment, compares an ordered path, decrypts a whole
5+
profile, and opens only the visit times from another query.
6+
7+
## Run
8+
9+
You need Go 1.26.10 or later, PostgreSQL with the EQL v4 SQL bundle installed, a `DATABASE_URL`
10+
pointing to it, and CipherStash credentials. `stash auth login` writes a
11+
developer profile; the `CS_*` environment variables also work. The example
12+
creates a temporary table, so it does not leave data behind.
13+
14+
From the repository root:
15+
16+
```sh
17+
mise run wasm:guest:build:eql wasm:auth-guest:build
18+
export DATABASE_URL='postgres://localhost:5432/your_database'
19+
cd languages/golang
20+
go run ./encrypt/example/json
21+
```
22+
23+
For a fresh database, install the `eql-v4-install.sql` asset attached to the
24+
Go module release before running the program. The guest builds are needed in
25+
a repository checkout because its WASM files are not committed. A tagged Go
26+
module version carries them.
27+
28+
## Change the model
29+
30+
The declaration is in `model.go`. `stashgen` writes `customer_stash.go` from
31+
its `stash` tags. After changing the struct or tags, run this from
32+
`languages/golang` and commit the changed generated file:
33+
34+
```sh
35+
go generate ./encrypt/example/json
36+
```
37+
38+
`Profile` uses `encoding/json` for the document shape. The `ordered=$.age`
39+
tag adds an ordering term for that path. A containment needle made from a
40+
struct includes every field without `omitempty`, including zero values;
41+
`main.go` uses a map to name only the member being queried. The prepared
42+
subtree read returns JSON text, which `encrypt.DecodeJSON[int64]` decodes.
43+
44+
The example's `SELECT` statements cast parameters to EQL's operand types.
45+
The age comparison uses the selector derived from the same ordered path as
46+
its query term. A JSON selector or read token is a capability over a column;
47+
do not log it. Partial reads authenticate returned entries against the
48+
prepared request, but do not prove that the database returned every entry.

0 commit comments

Comments
 (0)