Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions conformance/stack-encrypt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ these rules.
| `record-raw-frames-v1.cases.json` | cases | `tests/raw_frame_conformance.rs` | `raw_frame_conformance_test.go` |
| `single-value-v1.cases.json` | cases | `tests/single_value_conformance.rs` | — (pending, issue #2010) |
| `single-value-v1.expected.rust-native.json` | expected | `tests/single_value_conformance.rs` (writes it) | — (pending, issue #2010) |
| `record-encrypted-v1.cases.json` | cases | `tests/encrypted_record_conformance.rs` | — (pending: the Go guest does not use typed records yet) |
| `artifact-exchange-v1.cases.json` | cases | `tests/exchange_conformance.rs`, run by `scripts/stack-exchange-conformance.mjs` | `exchange_conformance_test.go`, run by `scripts/stack-exchange-conformance.mjs` |
| `eql-json-v1.cases.json` | cases | `tests/json_operators.rs`; EQL's `tests/encryption/tests/json_tree_data_plan.rs` | `json_conformance_test.go` |
| `eql-json-v1.enrollment.json` | enrollment | `scripts/stack-eql-conformance.mjs` runs the tests it names | `scripts/stack-eql-conformance.mjs` runs the tests it names |
Expand Down Expand Up @@ -92,8 +93,8 @@ fixtures described below. It ends with the fresh artifact exchange in both
directions (`test:conformance:exchange`, which also needs the deterministic
EQL guest). Set no fixture-update variables when checking conformance.

The task runs the single-value cases in native Rust only, because the Go
runner of that profile is pending.
The task runs the single-value cases and the encrypted-record cases in native
Rust only, because the Go runners of those profiles are pending.

The Rust runner is `packages/stack-encrypt/tests/conformance.rs`; the Go runner
is `languages/golang/encrypt/conformance_test.go`. Go calls the record interface
Expand Down Expand Up @@ -207,6 +208,28 @@ The native runner is `single_value_conformance` in the Rust crate.
with the typed interface. The Go runner is pending. The Go runner will be
written with the single-value calls of the Go SDK (issue #2010).

## An encrypted record as typed values

The [encrypted-record contract](encrypted-records-contract.md) defines
`record-encrypted-v1` in `v1/record-encrypted-v1.cases.json`. A binding
asks for an encrypted record as an `EncryptedRecord`, and moves it to its
language and back as a frame.

Each case encrypts rows under a plan, writes
the record as a frame, and checks the frame's form. It then reads the frame
back, after a change where the case has one. It decrypts the frame, or
checks the refusal at the stage the case names. The cases cover:

- every output kind, and a passthrough field of each value kind
- a batch, and an empty batch
- both null modes, and a `"null": "sql"` field left out
- one sealed value under `c`: a list, a map, or an empty one is refused
- the refusals, with the field, the output, and the row they name

The native runner is `encrypted_record_conformance` in the Rust crate, and
`test:conformance` runs it. The Go runner is pending until the Go guest uses
typed records.

## `record-reads-v1` contract

The [read contract](read-contract.md) defines cases in `v1/record-reads-v1.cases.json` for
Expand Down
1 change: 1 addition & 0 deletions conformance/stack-encrypt/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Paths are relative to the repository root. New profile cases run from `v1/`.
| Context binding and composition | `packages/stack-encrypt/tests/target.rs`, `descriptor.rs`, `record_lowering.rs`; `languages/golang/encrypt/context_field_test.go`; initial wrong-context cases | Shared composition vectors; expected-context and required-input matrix |
| Query terms and diagnostics | [Query-operator requirements](fixture-contract.md#query-operator-requirements), existing Rust/Go runners, initial equality/query-error cases and the [query-refusal profile](query-refusals-contract.md) (`v1/record-query-refusals-v1.cases.json`: every scalar operator against every field capability that does not answer it, the listed range shapes, other-kind values and nulls, by class, with the typed query's refusal stage) | JSON operator and EQL target refusals |
| Missing, SQL NULL and sealed null | `packages/stack-encrypt/src/dynamic/record.rs` null tests; `languages/golang/encrypt/null_test.go` | Explicit language mappings and common missing/null fixtures |
| An encrypted record as typed values, moved across a binding as a frame | The [encrypted-record profile](encrypted-records-contract.md), run by native Rust (`v1/record-encrypted-v1.cases.json`: every output kind, a passthrough of each value kind, a batch, both null modes, the refusal of a list, a map or an empty container under `c`, and the refusals at each stage with their field, output and row); `packages/stack-encrypt/src/dynamic/record/encrypted/tests/mod.rs` (a property test of any record through its frame and back, reading any frame without a panic, a field that names a target type, one field's frame, and the owned API); `packages/recipher-frame/src/serde/raw/tests/mod.rs` (a raw frame node written and read as it is) | The Go runner, once the Go guest uses typed records; Python and TypeScript runners |
| Fresh cross-language storage | `.github/workflows/tests-golang.yml` exports fresh Go EQL rows for Rust, the storage-only `Json` documents among them; initial scalar artifact exchange; fixed storage-blob vectors read by both Rust (`opaque_blob.rs`) and Go (`TestOpaqueReadsARustWrittenBlob`); the [exchange profile](exchange-contract.md): values, records, blobs and EQL rows written fresh and opened in both Rust/Go directions, with producer, engine, backend and storage versions in the report | Python/TypeScript pairs and backends when their runners exist; supported released versions once the compatibility policy is defined; fresh exchange of searchable JSON documents, and of `Json` documents from Rust to Go; a SQL lane storing one language's fresh rows through the other |
| JSON queries, extraction and tampering | `packages/stack-encrypt/tests/json_tree_vectors.rs`; `packages/eql/tests/encryption/tests/json_tree.rs`; `languages/golang/encrypt/json_tree_test.go`, `json_postgres_test.go` | Formal operation profile; JSON vectors consumed by all runners |
| Element and predicate matching | Go `json_elements_test.go`, `json_match_test.go` and corresponding PostgreSQL tests; Rust/SQL predicate suites | Map exact supported grammar and documented semantic differences to case IDs |
Expand Down
177 changes: 177 additions & 0 deletions conformance/stack-encrypt/encrypted-records-contract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Encrypted-record profile

`record-encrypted-v1` is the conformance profile for an encrypted record as
typed values, and for the frame that a language binding moves it in. The
cases are in `v1/record-encrypted-v1.cases.json`. The profile is an
experimental test contract.

A binding asks `record::encrypt` for an `EncryptedRecord`, or a batch of
them, and gives one back to `record::decrypt`. It moves the record to its
language and back as a frame, with `EncryptedRecord::to_frame` and
`EncryptedRecord::from_frame`. The cases fix the form of that frame, so
every binding builds and reads the same values.

## Terms

- A frame is one value written as bytes, in the format that Stack Encrypt
reads.
- An encrypted record has one entry for each field. Each field has one
entry for each output.
- An output is one of these:
- `c`, the sealed value
- an index term: `eq`, `match`, `ore`, `ope`, or `ore_block`
- `passthrough`, a field's value stored without encryption
- `target`, the value of a field that names a target type
- A sealed value is the encrypted form of one scalar. Its bytes are stable.
- A refusal is an error that Stack Encrypt returns for a value or a frame
it does not accept.

## Runners

The Rust runner is
`packages/stack-encrypt/tests/encrypted_record_conformance.rs`. The Go
runner is pending: the Go guest does not use typed records yet. The Python
and TypeScript runners are pending too. A missing runner is not a passing
result.

## Cases

Each case has these keys:

| Key | Meaning |
|---|---|
| `id` | The name of the case |
| `plan` | The plan, in the [declaration grammar](declaration-grammar.md) |
| `batch` | Whether the rows are a batch. Without a batch, `rows` has one row. |
| `rows` | The rows to encrypt, as values |
| `frame` | The frame form that the encrypted record must have |
| `mutation` | A change to the frame before it is read back |
| `opened` | The rows that the frame, read back, decrypts to |
| `expected_error` | The `stage` of the refusal, and its `code` and payload `fields` |

A case has `opened` or `expected_error`, never both.

### Values

`rows` and `opened` write a value in one of these ways:

| Form | The value |
|---|---|
| `{"type": ..., "text": ...}` | A scalar, as the [value profile](values-contract.md#cases) defines it |
| an array of values | A list |
| `{"$object": {...}}` | A map of values |
| `null` | A null |

A row leaves out a field that it does not have.

### Frame nodes

`frame` and the `value` of a mutation write a node of the frame in one of
these ways:

| Form | The node |
|---|---|
| `"$sealed"` | The bytes of a sealed value |
| `"$bytes"` | Any bytes, such as a term. `frame` only. |
| `{"$value": <value>}` | The frame of a value, such as a passthrough field's value |
| `{"$hex": "..."}` | These bytes |

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): the table says frame and a mutation value both take {"$hex": "..."}. The Rust runner reads $hex only in a mutation (shaped). In check, an object with $hex is compared as an object with the key $hex, so a case that uses it in frame fails with "is not an object". Fix: mark $hex as "Mutations only", as $passthrough is, or handle it in check.

| `{"$passthrough": <node>}` | A passthrough node of the frame, around another node. Mutations only. |
| other text | That text |
| `null` | A null |
| an array | An array of nodes |
| another object | An object of nodes |

In a mutation, `"$sealed"` is the bytes of the first sealed value in the
frame that the runner wrote.

Objects compare by key. The order of the keys in the corpus file is not
significant. The order of a frame's keys is Stack Encrypt's. The runner
does not check it.

### Mutations

A mutation has a `path`, an `op`, and, for `set`, a `value`. Each step of
the path is a key of an object, or a position in an array, counted from 0.

- `set` puts `value` at the path. It adds the key when the object has none.
- `remove` takes the key at the path out of its object.
- `duplicate` writes the entry at the path a second time.
- An empty path sets the whole frame.

## Required behavior

For each case, the runner does this:

1. It reads the plan, and encrypts the rows as one `EncryptedRecord`, or as
a batch of them.
2. It writes the record as a frame. Where the case has `frame`, the frame
has that form.
3. It applies the mutation, where the case has one.
4. It parses the frame, reads it back as a record, and decrypts the record.
5. For a case with `opened`, every step succeeds. With no mutation, the
record read back writes the same frame again. The record decrypts to
the `opened` rows.
6. For a case with `expected_error`, the step that its `stage` names is
refused, with the `code`, and with a payload equal to `fields`. Every
step before it succeeds.

| Stage | The step that is refused |
|---|---|
| `encrypt` | `record::encrypt`, before any key is requested |
| `frame` | Parsing the frame. The case has no `code` or `fields`. |
| `read` | Reading the frame as a record |
| `decrypt` | `record::decrypt`, before any key is retrieved |

The cases cover these things:

- Every output kind: a sealed value, each index term, an index term alone,
and a passthrough field.
- A passthrough field of each value kind, and a passthrough that is a list
or a map.
- A batch, and an empty batch.
- A field with `"null": "sql"` and no value. It has no entry, and decrypts
as a null.
- A field with `"null": "sealed"` and no value. It is sealed.
- A `"null": "sql"` field that the frame leaves out. It decrypts as a null.
- Index terms left out of the frame. `decrypt` does not read them.
- The refusals, listed in the next table.

| Refusal | Stage | Reason |
|---|---|---|
| A sealed field whose value is a list, a map, or an empty one | `encrypt` | `not_one_value` |
| A frame that is not an object, or not an array of objects | `read` | `not_an_object` |
| A field that is not an object of outputs, a null included | `read` | `outputs_not_map` |
| A key that is not an output, such as `eql` | `read` | `not_an_output` |
| Anything under `c` but one sealed value's bytes | `read` | `not_sealed` |
| A term that is not bytes | `read` | `not_passthrough` |
| A field name given twice | `frame` | none |
| A sealed field with no `c` | `decrypt` | `no_ciphertext_node` |
| A passthrough of another type than its field declares | `decrypt` | `field_type` |
| A refusal of a row of a batch | any | the refusal's reason, with the row |

## Refusals

The code of an `encrypt` refusal is `stack_encrypt::dynamic_source`. The
code of a `read` or `decrypt` refusal is `stack_encrypt::dynamic_record`.
The payload has these fields, where they apply:

| Field | Meaning |
|---|---|
| `reason` | What was wrong, such as `not_an_output` or `not_sealed` |
| `field` | The field at fault |
| `output` | The output at fault, such as `c` or `eq` |
| `row` | The row of a batch, counted from 0 |
| `found` | The kind of a frame, or a row, that is not an object |

No refusal contains a value, or a key that is not an output.

## Scope and remaining work

The profile does not cover these:

- A field that names a target type. The EQL profile covers target values,
and the native tests cover their `target` output.
- One field's frame alone (`EncryptedField::to_frame` and `from_frame`).
The native tests cover it.
- The stored tree, `StackCipherText`. Other profiles cover it.
- Frames exchanged between languages. That needs a second runner.
Loading
Loading