@protobase/cli 0.57.5 → 0.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/package.js +1 -1
- package/dist/dev/dev-jobs.js +5 -4
- package/dist/dev/entry.js +1 -1
- package/dist/mobile/live-update-settings.js +2 -3
- package/dist/mobile/package.js +1 -1
- package/dist/mobile/run.js +0 -1
- package/dist/serve/lifecycle.js +1 -0
- package/dist/worker/start-worker.js +2 -1
- package/package.json +7 -7
- package/skills/protobase/references/features/build-and-run.md +1 -1
- package/skills/protobase/references/features/data-model.md +1 -0
- package/skills/protobase/references/guides/api-functions.md +1 -0
- package/skills/protobase/references/guides/mobile-app.md +2 -2
- package/skills/protobase/references/reference/api.md +5 -2
- package/skills/protobase/references/reference/cli.md +4 -4
- package/skills/protobase/references/reference/config/app.md +1 -1
- package/skills/protobase/references/reference/data-config.md +13 -1
- package/skills/protobase/references/reference/jobs.md +1 -1
package/dist/cli/package.js
CHANGED
package/dist/dev/dev-jobs.js
CHANGED
|
@@ -3,16 +3,17 @@ import { checkJobsSchema, startJobs } from "@protobase/server";
|
|
|
3
3
|
const store = globalThis;
|
|
4
4
|
/**
|
|
5
5
|
* Runs the project's jobs in the dev server: started on the first load, once the jobs schema checks out, with the app's
|
|
6
|
-
* own mailer when it has one, and given the reloaded definitions
|
|
7
|
-
* code from the next job on. A mailer added or changed later is the jobs' from the next start.
|
|
6
|
+
* own mailer when it has one, and given the reloaded definitions and the rebuilt app's `decryptField` on every load
|
|
7
|
+
* after it, so an edited job runs its new code from the next job on. A mailer added or changed later is the jobs' from the next start.
|
|
8
8
|
*/
|
|
9
|
-
const devJobs = async (db, definitions, mailer) => {
|
|
10
|
-
if (store.__protobaseJobs) return store.__protobaseJobs.update(definitions);
|
|
9
|
+
const devJobs = async (db, definitions, decryptField, mailer) => {
|
|
10
|
+
if (store.__protobaseJobs) return store.__protobaseJobs.update(definitions, decryptField);
|
|
11
11
|
if (definitions.length === 0) return;
|
|
12
12
|
await checkJobsSchema(db);
|
|
13
13
|
store.__protobaseJobs = await startJobs({
|
|
14
14
|
db,
|
|
15
15
|
jobs: definitions,
|
|
16
|
+
decryptField,
|
|
16
17
|
...mailer && { mailer },
|
|
17
18
|
onError: (error) => console.error(error)
|
|
18
19
|
});
|
package/dist/dev/entry.js
CHANGED
|
@@ -49,7 +49,7 @@ const ui = {
|
|
|
49
49
|
icons: projectIcons(projectDir)
|
|
50
50
|
};
|
|
51
51
|
const { admin, jobs } = projectAdmin(project, db, ui, reporter, upgradeWebSocket);
|
|
52
|
-
await devJobs(db, jobs, project.mailer);
|
|
52
|
+
await devJobs(db, jobs, admin.decryptField, project.mailer);
|
|
53
53
|
devRealtimePrune(admin);
|
|
54
54
|
var entry_default = withDevServiceWorker(new Hono(), Boolean(project.notifications)).route("/", admin);
|
|
55
55
|
/** Whether the auth store has a user yet, for the banner's sign-in line; undefined without one. */
|
|
@@ -12,12 +12,11 @@ const ownOrigin = (origin) => {
|
|
|
12
12
|
return url.protocol === "https:" || url.protocol === "http:" && loopback.includes(url.hostname);
|
|
13
13
|
};
|
|
14
14
|
/**
|
|
15
|
-
* Whether the app gets the live updater
|
|
15
|
+
* Whether the app gets the live updater, on Android and iOS alike: with `mobile.publicKey`, the order of its own files and an
|
|
16
16
|
* API origin it may take releases from, never with `--live`. `note` says why not, when the project could change it.
|
|
17
17
|
*/
|
|
18
|
-
const liveUpdateSettings = ({
|
|
18
|
+
const liveUpdateSettings = ({ publicKey, builtInOrder, apiOrigin, live }) => {
|
|
19
19
|
if (live) return {};
|
|
20
|
-
if (platform !== "android") return publicKey === void 0 ? {} : { note: "the iOS app does not update itself yet: its live updater is Android only" };
|
|
21
20
|
if (publicKey === void 0) return { note: "the app does not update itself: set mobile.publicKey to the public key protobase mobile keys prints" };
|
|
22
21
|
if (!builtInOrder) return { note: "the app does not update itself: its files name no build time, so it could not refuse a release older than them" };
|
|
23
22
|
if (!ownOrigin(apiOrigin)) return { note: `the app does not update itself: it takes releases over https only, or http to this device, not from ${apiOrigin}` };
|
package/dist/mobile/package.js
CHANGED
package/dist/mobile/run.js
CHANGED
|
@@ -57,7 +57,6 @@ const prepare = async ({ projectDir, platform, api, extensions, live, release },
|
|
|
57
57
|
}) : await placeholderWebPart(ws);
|
|
58
58
|
const fingerprint = nativeFingerprint(pinnedNative, mobile.plugins);
|
|
59
59
|
const liveUpdate = liveUpdateSettings({
|
|
60
|
-
platform,
|
|
61
60
|
...mobile.publicKey && { publicKey: mobile.publicKey },
|
|
62
61
|
...builtInOrder && { builtInOrder },
|
|
63
62
|
apiOrigin: url.origin,
|
package/dist/serve/lifecycle.js
CHANGED
|
@@ -48,6 +48,7 @@ const startServe = async ({ config, env, openDb = (url) => openAppDatabase(pg, u
|
|
|
48
48
|
db,
|
|
49
49
|
jobs: definitions,
|
|
50
50
|
work: env.runJobs,
|
|
51
|
+
decryptField: admin.decryptField,
|
|
51
52
|
...project.mailer && { mailer: project.mailer },
|
|
52
53
|
onError: (error) => console.error(error)
|
|
53
54
|
}) : void 0;
|
|
@@ -29,13 +29,14 @@ const startWorker = async ({ config, databaseUrl, openDb = (url) => openAppDatab
|
|
|
29
29
|
return isCaptured(model) || model.comments || Object.values(model.fields).some((field) => field.ai);
|
|
30
30
|
});
|
|
31
31
|
if (project.jobs.length === 0 && !builtIn) throw new Error("The bundle has no jobs: name their module in protobase.config.ts as `jobs`, or give a resource events, history, comments or an AI field");
|
|
32
|
-
const { jobs: definitions } = projectAdmin(project, db);
|
|
32
|
+
const { admin, jobs: definitions } = projectAdmin(project, db);
|
|
33
33
|
await checkJobsSchema(db);
|
|
34
34
|
return {
|
|
35
35
|
definitions,
|
|
36
36
|
runtime: await startJobs({
|
|
37
37
|
db,
|
|
38
38
|
jobs: definitions,
|
|
39
|
+
decryptField: admin.decryptField,
|
|
39
40
|
...project.mailer && { mailer: project.mailer },
|
|
40
41
|
onError: (error) => console.error(error)
|
|
41
42
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.58.0",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@better-auth/core": "^1.7.12",
|
|
44
44
|
"@hono/node-server": "^2.1.3",
|
|
45
|
-
"@protobase/config": "0.
|
|
46
|
-
"@protobase/layout": "0.
|
|
47
|
-
"@protobase/query": "0.
|
|
48
|
-
"@protobase/schema": "0.
|
|
49
|
-
"@protobase/server": "0.
|
|
50
|
-
"@protobase/ui": "0.
|
|
45
|
+
"@protobase/config": "0.58.0",
|
|
46
|
+
"@protobase/layout": "0.58.0",
|
|
47
|
+
"@protobase/query": "0.58.0",
|
|
48
|
+
"@protobase/schema": "0.58.0",
|
|
49
|
+
"@protobase/server": "0.58.0",
|
|
50
|
+
"@protobase/ui": "0.58.0",
|
|
51
51
|
"@react-email/render": "^2.1.0",
|
|
52
52
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
53
53
|
"@tailwindcss/vite": "^4.3.3",
|
|
@@ -25,4 +25,4 @@ Develop, build and run an app, on servers, in containers and on phones.
|
|
|
25
25
|
- [Protobase Cloud deploys](../guides/deploy.md#protobase-cloud): A push to the app's GitHub branch builds it, migrates and checks its database in one transaction, and switches to the new version once it answers its health check.
|
|
26
26
|
- [Mobile apps](../guides/mobile-app.md) (beta): Runs an app on iOS and Android, also on your own iPhone, with sign-in, and builds signed store releases from a section in its config.
|
|
27
27
|
- [Mobile releases](../reference/cli.md#mobile-releases) (in development): The server offers the mobile app a newer copy of its files, signed, and any host that serves bundles can offer the same from the bundle's files without running its config.
|
|
28
|
-
- [Live updates](../reference/cli.md#the-live-updater) (in development): The Android app
|
|
28
|
+
- [Live updates](../reference/cli.md#the-live-updater) (in development): The Android and iOS app update themselves in the background to the server's release, downloading only the files that changed, and go back to the files they were built with when a release fails to start.
|
|
@@ -21,6 +21,7 @@ Describe the tables of an existing Postgres database as resources, with their fi
|
|
|
21
21
|
- [Recent strong sign-in](../reference/data-config.md#recent-strong-sign-in): A sensitive field can ask for a passkey or a second step in the last minutes before it is revealed. The record page asks the person to confirm it's them, then shows it.
|
|
22
22
|
- [Computed reveals](../reference/data-config.md#computed-reveals) (beta): A secret kept elsewhere, or built from several values, is revealed like a sensitive field, with the same access, audit and recent sign-in check, but made when it is asked for.
|
|
23
23
|
- [Encrypted fields](../reference/data-config.md#encrypted-fields): A sensitive text field is encrypted with the app's own key before it is stored, so dumps, backups and SQL readers see only ciphertext. A command encrypts the values a column already holds.
|
|
24
|
+
- [Encrypted fields in jobs and functions](../reference/data-config.md#reading-in-jobs-and-functions) (beta): A job or an API function reads an encrypted value the app must use itself, such as a share's password, and each read is audited like a reveal.
|
|
24
25
|
- [Encryption key rotation](../reference/data-config.md#the-key): The encryption key can be replaced without downtime. CLI commands re-encrypt stored values and report what is left.
|
|
25
26
|
- [Schema API reference](../reference/data-config.md#schema-api-reference): Every builder, option and type of `@protobase/schema` has a doc comment, shown in your editor and in the generated API reference, and the types the builders return are exported for helpers of your own.
|
|
26
27
|
- [Extending a config](../reference/cli.md#extending-a-config): A project can merge other configs and UI configs into its own. A name defined twice is an error, never a silent override.
|
|
@@ -50,6 +50,7 @@ The second argument of a handler (`c.env` in a [Hono app](#hono-apps)):
|
|
|
50
50
|
| `records` | The app's resources, acting as the caller: `list`, `get`, `create`, `update`, `delete`, `softDeleteField`. |
|
|
51
51
|
| `db` | The Kysely instance itself. It skips every access rule, row filter and tenant scope. |
|
|
52
52
|
| `caller()` | Resolves to `{ session, records }`. Public functions use it to ask for a caller when they need one. |
|
|
53
|
+
| `decryptField(resource, key, field)` | An [encrypted field](../reference/data-config.md#reading-in-jobs-and-functions)'s value, skipping access rules like `db`; each read is audited, naming the function and the caller. |
|
|
53
54
|
| `acceptWebSocket(events)` | Answers a WebSocket upgrade with a socket; return its response. See [WebSockets](#websockets). |
|
|
54
55
|
| `name` | The function's name. |
|
|
55
56
|
|
|
@@ -122,7 +122,7 @@ Only Gradle gets them, for the length of the build: npm, its install scripts and
|
|
|
122
122
|
|
|
123
123
|
## 7. Live updates
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
The app updates its files itself from your server, on Android and iOS alike, so it gets UI changes without a store release. Make a key pair once:
|
|
126
126
|
|
|
127
127
|
```sh
|
|
128
128
|
protobase mobile keys
|
|
@@ -139,7 +139,7 @@ mobile: {
|
|
|
139
139
|
},
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
-
Build the binary again. Each deploy of the app then reaches it: it downloads the files that changed in the background and runs them at its next start, and a release that fails to start goes back to the files the binary carries. Releases come from a server of the same Protobase minor as the binary (below 1.0); a newer minor needs a store release. See [the live updater](../reference/cli.md#the-live-updater).
|
|
142
|
+
Build the binary again. Each deploy of the app then reaches it: it downloads the files that changed in the background and runs them at its next start, and a release that fails to start goes back to the files the binary carries. Releases come from a server of the same Protobase minor as the binary (below 1.0); a newer minor needs a store release. See [the live updater](../reference/cli.md#the-live-updater).
|
|
143
143
|
|
|
144
144
|
## 8. What the stores accept
|
|
145
145
|
|
|
@@ -123,8 +123,9 @@ Before answering, the server publishes an audit event to the app's audit queue,
|
|
|
123
123
|
type AuditEvent = {
|
|
124
124
|
type: 'field.revealed'
|
|
125
125
|
at: string // ISO 8601
|
|
126
|
-
actor
|
|
127
|
-
|
|
126
|
+
actor?: { id: string | number; roles: string[] } // absent when a job read the value
|
|
127
|
+
reader?: { job: string } | { function: string } // who read it with decryptField; absent for a reveal
|
|
128
|
+
tenant?: string | number // the caller's organization (the record's for decryptField), for tenant-scoped resources
|
|
128
129
|
resource: string
|
|
129
130
|
recordKey: string // as in the record's URL
|
|
130
131
|
field: string
|
|
@@ -132,6 +133,8 @@ type AuditEvent = {
|
|
|
132
133
|
}
|
|
133
134
|
```
|
|
134
135
|
|
|
136
|
+
A job or API function that reads an encrypted field with [`decryptField`](data-config.md#reading-in-jobs-and-functions) publishes the same event, with `reader` set and, for a job, no `actor` or `origin` headers.
|
|
137
|
+
|
|
135
138
|
With [organizations](auth.md#organizations), someone with a global role switching into an organization they are not a member of publishes `{ type: 'organization.entered', at, actor, organization, origin }` first, where `actor.roles` are their global roles; a failed publish refuses the switch. `AuditEvent` is the union of the two.
|
|
136
139
|
|
|
137
140
|
### Audit events
|
|
@@ -424,7 +424,7 @@ The app's address and secret are `PROTOBASE_URL` and `PROTOBASE_SECRET` unless i
|
|
|
424
424
|
### Mobile releases
|
|
425
425
|
|
|
426
426
|
:::caution[In development]
|
|
427
|
-
|
|
427
|
+
Mobile releases and the live updater are in development.
|
|
428
428
|
:::
|
|
429
429
|
|
|
430
430
|
With a `mobile` section, the server serves the [mobile app](../guides/mobile-app.md) a newer copy of its files, for its live updater, so the app gets UI changes without a store release. The release is what the server serves the web, composed at startup: every file of `public/`, with the server's own shell where `protobase serve` uses it, and `index.html` naming the API origin, the app's `url` (https, or plain http to `localhost`, `127.0.0.1` or `[::1]`). There is none when the server serves the API alone, has no such `url`, or the bundle names no `native`, and with `PROTOBASE_MOBILE_SIGNING_KEY` none for a bundle that names no build time (one built before Protobase recorded it: build it again). Where `protobase serve` serves its own shell, the release's fingerprint names the `native` of that shell's `protobase.shell.json`, and there is none when it names none.
|
|
@@ -432,11 +432,11 @@ With a `mobile` section, the server serves the [mobile app](../guides/mobile-app
|
|
|
432
432
|
- `GET /api/mobile/release?fingerprint=<native fingerprint>&shell_version=<x.y.z>` answers `{ "bundleId", "url", "required" }`: the release's id, a hash of its files, and its absolute base URL, both `null` when the release does not run on the asking binary, whose native fingerprint must equal the release's. `required` is true when the shell the app runs no longer works with the server's API: below 1.0 another minor, from 1.0 another major. A missing fingerprint or a shell version that is no x.y.z version gets 400.
|
|
433
433
|
- `GET /api/mobile/releases/<bundleId>?href=<file>` serves the release's file `<file>`, such as `assets/index-abc123.js`, and with `href=capawesome-live-update-manifest.json` its manifest, a JSON array of `{ "href", "checksum", "sizeInBytes" }`. Each comes with its sha256 in hex in `X-Checksum` and, with `PROTOBASE_MOBILE_SIGNING_KEY`, its signature in `X-Signature`. Any other file, or another release's id, gets 404.
|
|
434
434
|
- Signed, a release also serves `href=protobase-release.json`, its statement: `{ "payload", "signature" }`, the payload the hex of the JSON `{ "manifest", "order", "appId", "fingerprint", "shellVersion" }` and the signature the hex of its SHA256withRSA over the line `protobase-mobile-release` and the payload. It binds the manifest's sha256 to the release's order, when the bundle was built and then when the server's own shell it serves was built (0 with the bundle's copy), to the app id and native fingerprint of the binaries it runs on, and to the server's Protobase version. Its id is then a hash of the statement and the public key, so the same files built again get another id. A file of the app may not be named like the statement or start with that line.
|
|
435
|
-
-
|
|
435
|
+
- The app, on Android and iOS alike, downloads a release itself and takes it only when its statement is signed with the key the binary pins, names the release's id, this app, this binary and a server its shell works with, and its order is not below the highest it downloaded; then every file must hash to what the manifest lists, under a path inside the release, before any file is written. It follows no redirect and reads no file past the size its manifest lists. It runs a downloaded release only above the running one's order and holding nothing but the files its manifest lists. It never takes a release at or below the order of its own files, which `protobase mobile` records in the binary as the server would order the same bundle with its copy of the shell, so a reinstalled app is not taken below them, and a release built before the binary is not taken. <gap>A phone cannot take a rollback of the bundle or of the server's shell before a later build, because ordering releases by an explicit release number is not implemented.</gap> <gap>An app with no download recorded, such as after a reinstall, cannot refuse a genuine signed release older than the latest but newer than its own files, because a signed answer naming the latest release is not implemented.</gap>
|
|
436
436
|
|
|
437
437
|
#### The live updater
|
|
438
438
|
|
|
439
|
-
An Android app built with `mobile.publicKey` updates itself from the server it calls. The shell asks `GET /api/mobile/release` when the app starts and each time it comes back to the foreground, in the background, and downloads a release it does not run, only the files that changed, then runs it at the next cold start. The session stays in the device's cookie store, so the user stays signed in across the switch.
|
|
439
|
+
An Android or iOS app built with `mobile.publicKey` updates itself from the server it calls. The shell asks `GET /api/mobile/release` when the app starts and each time it comes back to the foreground, in the background, and downloads a release it does not run, only the files that changed, then runs it at the next cold start. The session stays in the device's cookie store, so the user stays signed in across the switch.
|
|
440
440
|
|
|
441
441
|
- Once the shell has drawn, it tells the updater the release started. A release that does not within 30 seconds goes back to the app's own files at once and is never taken again.
|
|
442
442
|
- No answer, any status but 200, a body that is not the answer, or an answer naming no release changes nothing: the app keeps the files it runs, and never goes back to its own files because of the answer, which is not signed.
|
|
@@ -602,7 +602,7 @@ Builds the project in the current directory as an iOS or Android app with Capaci
|
|
|
602
602
|
- npm and Capacitor get the CLI's environment without any variable named in the current directory's `.env*` files, nor the four `PROTOBASE_ANDROID_*` signing variables, which only Gradle gets, and with `CI=true`, which keeps Capacitor's telemetry off.
|
|
603
603
|
- It prints the workspace, the API origin and the native fingerprint, which the app also sends in its user agent as `ProtobaseNative/<fingerprint>`: a hash of the native packages `@protobase/mobile` pins, at their versions, its own native contract, and the section's `plugins`, at the versions they give. A [mobile release](#mobile-releases) runs only on a binary of its own fingerprint. A Protobase release that pins the same native packages and keeps the same native contract keeps it; raise a plugin's version when the app's files start using a newer native feature of it, which changes it.
|
|
604
604
|
- The app's files carry the live updater's manifest of them, so an update downloads only the files that changed.
|
|
605
|
-
- With `mobile.publicKey`, the Android app
|
|
605
|
+
- With `mobile.publicKey`, the Android and iOS app get the [live updater](#the-live-updater), unless `--live` is given, the app's files name no build time, or the API is neither https nor http to this machine; it prints a note why when it leaves it out.
|
|
606
606
|
- `keys` prints a fresh RSA key pair: `PROTOBASE_MOBILE_SIGNING_KEY=<key>`, on one line with its newlines written `\n`, for the server's environment, a secret never to commit, and the public key, PEM, for `mobile.publicKey`. It reads no project and writes no file.
|
|
607
607
|
|
|
608
608
|
## `token`
|
|
@@ -104,7 +104,7 @@ The app's iOS and Android build: with it, `protobase mobile` builds the app with
|
|
|
104
104
|
|
|
105
105
|
- `appId`: the app's id on both iOS and Android, such as `com.example.shop`: two or more dot-separated parts, each a letter then letters and digits.
|
|
106
106
|
- `plugins`: extra native Capacitor plugins, by npm name and version range. A range fetched over plain `http:` or `git:` is refused.
|
|
107
|
-
- `publicKey`: the public key, PEM, of the RSA key the server signs the app's [releases](../cli.md#the-live-updater) with, from [`protobase mobile keys`](../cli.md#mobile): with it the Android app
|
|
107
|
+
- `publicKey`: the public key, PEM, of the RSA key the server signs the app's [releases](../cli.md#the-live-updater) with, from [`protobase mobile keys`](../cli.md#mobile): with it the Android and iOS app update themselves. Anything but an RSA public key of at least 2048 bits stops the build.
|
|
108
108
|
|
|
109
109
|
```ts
|
|
110
110
|
export default defineConfig({
|
|
@@ -255,7 +255,7 @@ resource('apps')
|
|
|
255
255
|
|
|
256
256
|
## Encrypted fields
|
|
257
257
|
|
|
258
|
-
An encrypted field is a [sensitive field](#sensitive-fields) whose value the server encrypts before it reaches the database, so a dump, a backup, a replica, a realtime subscriber or anyone running SQL sees only ciphertext. Only its reveal decrypts it, for a caller who may read the record and the field, and every
|
|
258
|
+
An encrypted field is a [sensitive field](#sensitive-fields) whose value the server encrypts before it reaches the database, so a dump, a backup, a replica, a realtime subscriber or anyone running SQL sees only ciphertext. Only its reveal decrypts it, for a caller who may read the record and the field, and your own [jobs and functions](#reading-in-jobs-and-functions) on the server; every read is audited. Every API write encrypts it: the record form, REST, batches, imports, [public forms](forms.md) and `records` in [API functions](../guides/api-functions.md). The [Supabase-compatible REST API](supabase-rest.md#encrypted-fields) refuses to write or filter it.
|
|
259
259
|
|
|
260
260
|
```ts
|
|
261
261
|
api_key: f.text().encrypted().access({ read: roles.can('keys.read') }),
|
|
@@ -275,6 +275,18 @@ When you change a `.sensitive()` field to `.encrypted()`, change every rule, val
|
|
|
275
275
|
|
|
276
276
|
No other field of the resource may be over the column, as its own or as a relation's column, and another resource over the same table must leave the column out, or declare it `.encrypted()` too with the same `.table()`, tenant and key; the server does not start otherwise. A resource over a view of the table cannot be checked, so leave the column out of the view itself, not only out of the resource.
|
|
277
277
|
|
|
278
|
+
### Reading in jobs and functions
|
|
279
|
+
|
|
280
|
+
A value the app must use itself, such as the password of a backup share, is read on the server with `decryptField(resource, key, field)` of a [job's](jobs.md) context and of a protected [API function's](../guides/api-functions.md#the-context):
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
const password = await context.decryptField('backup_targets', target.id, 'password')
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
It returns the cleartext, or `null` when the field is empty. Like the context's `db`, it skips access rules, row filters and tenant scope, and reads only live records. `key` is the record's key, or its parts in the order of a composite key. A resource that is unknown, a field that does not exist or is not `.encrypted()`, and a record that does not exist are errors that name them, as is a value that does not decrypt.
|
|
287
|
+
|
|
288
|
+
Every read publishes a `field.revealed` [audit event](api.md#sensitive-fields-and-audit), whose `reader` names the job (`{ job }`, with no `actor`) or the function (`{ function }`, with its caller as `actor`), and the value is only returned once the event is published: when publishing fails, so does the read. A public function has no `decryptField`, since anyone may call it: enqueue a job that reads the value instead. No HTTP request reaches it.
|
|
289
|
+
|
|
278
290
|
### The key
|
|
279
291
|
|
|
280
292
|
The app's key is `PROTOBASE_ENCRYPTION_KEY`: 32 random bytes in base64, such as the output of `openssl rand -base64 32`. It is its own: never the app's `secret` (`PROTOBASE_SECRET`) or an API key, nor derived from one, so rotating those loses nothing. Keep it apart from the database's backups, since whoever holds both reads every value. The server does not start with an encrypted field and no key, or a key that is not 32 bytes in base64.
|
|
@@ -53,7 +53,7 @@ export default (async ({ invoiceId }, { db }) => {
|
|
|
53
53
|
| its name | Letters, digits, `_`, `-`, `.` and `/`; the file `jobs/<name>.ts` holds what it does. |
|
|
54
54
|
| `description` | What the job does, for the people who read the config. |
|
|
55
55
|
| `arguments` | Its arguments by name, each a [field type](data-config.md#field-types) such as `'integer'` or `'bigint'`, or the type with options: `{ type: 'enum', values: ['day', 'week'] }`, `{ type: 'text', min: 1 }`, `{ type: 'integer', optional: true }`, `{ type: 'decimal', precision: 8, scale: 2 }`. Each value is checked as a field of that type checks it, when the job is enqueued and again before it runs, and no other argument is taken. A payload refused at run time fails the job for good. The same is JSON in a `protobase.config.json`. |
|
|
56
|
-
| the default export of `jobs/<name>.ts` | `(payload, { db, job, signal, mail, sendNotification })`, typed with `JobRun<Payload>` from `@protobase/server`, its payload `JobPayload<typeof config, '<name>'>` (see [Typed names and payloads](#typed-names-and-payloads)): the work. `db` is the app's pool: the enqueuer's transaction has committed by then. `job` holds its `id`, `name`, `attempt`, `maxAttempts` and `startedOn`, when this try took it. `signal` aborts when the try times out or the worker shuts down. `mail` sends mail, see [Mail](#mail). `sendNotification` sends a notification, see [Sending](notifications.md#sending); inside a transaction of `db`, a try that throws in it queues nothing. It is `undefined` in an app without [`notifications`](config/jobs-and-events.md#notifications). |
|
|
56
|
+
| the default export of `jobs/<name>.ts` | `(payload, { db, job, signal, decryptField, mail, sendNotification })`, typed with `JobRun<Payload>` from `@protobase/server`, its payload `JobPayload<typeof config, '<name>'>` (see [Typed names and payloads](#typed-names-and-payloads)): the work. `db` is the app's pool: the enqueuer's transaction has committed by then. `job` holds its `id`, `name`, `attempt`, `maxAttempts` and `startedOn`, when this try took it. `signal` aborts when the try times out or the worker shuts down. `decryptField(resource, key, field)` reads an encrypted field's value, audited, see [Reading in jobs and functions](data-config.md#reading-in-jobs-and-functions). `mail` sends mail, see [Mail](#mail). `sendNotification` sends a notification, see [Sending](notifications.md#sending); inside a transaction of `db`, a try that throws in it queues nothing. It is `undefined` in an app without [`notifications`](config/jobs-and-events.md#notifications). |
|
|
57
57
|
| `maxAttempts` | Tries before the job is kept as failed; default 10. |
|
|
58
58
|
| `retryDelay`, `maxRetryDelay` | The wait before the first retry, doubling with each one (with some jitter), and its ceiling; default `2s` and `1 hour`. Milliseconds or text such as `30 seconds`. |
|
|
59
59
|
| `timeout` | How long one try may run before it counts as failed; default `15 minutes`. |
|