@quatronic/sdk 0.8.0-test.3e82420 → 0.8.0-test.fbe581b

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.
@@ -165,7 +165,7 @@ extension file.
165
165
 
166
166
  \`qore-add-env-key\`, \`qore-add-integration\`, \`qore-add-connection\`,
167
167
  \`qore-use-permissions\`, \`qore-observability\` and \`qore-use-database\`
168
- produce code the portal reads: the Secrets tab, the Integrations tab,
168
+ produce code the portal reads: the Variables tab, the Integrations tab,
169
169
  connections, roles, Monitoring, the local database. An
170
170
  extension may add to them but not relax them, and turning one off does not turn
171
171
  off the SDK's lint rules that check the same things.
@@ -164,7 +164,7 @@ extension file.
164
164
 
165
165
  \`qore-add-env-key\`, \`qore-add-integration\`, \`qore-add-connection\`,
166
166
  \`qore-use-permissions\`, \`qore-observability\` and \`qore-use-database\`
167
- produce code the portal reads: the Secrets tab, the Integrations tab,
167
+ produce code the portal reads: the Variables tab, the Integrations tab,
168
168
  connections, roles, Monitoring, the local database. An
169
169
  extension may add to them but not relax them, and turning one off does not turn
170
170
  off the SDK's lint rules that check the same things.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatronic/sdk",
3
- "version": "0.8.0-test.3e82420",
3
+ "version": "0.8.0-test.fbe581b",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -13,7 +13,7 @@ provider slug uppercased, non-alphanumerics collapsed to `_`. The SDK reads only
13
13
  this env — it never calls the portal at runtime.
14
14
 
15
15
  > A single-module app has no connections. Only follow this once the portal shows
16
- > a wired connection for this module (its env keys exist in the Secrets tab).
16
+ > the connection as Active in this module's Connections tab.
17
17
 
18
18
  ## Kinds of connection
19
19
 
@@ -50,6 +50,8 @@ clear startup error, not a mystery 500. For a service connection to `billing-api
50
50
  CONN_BILLING_API_TOKEN: z.string().min(1),
51
51
  ```
52
52
 
53
+ How to declare, mark and check a key: `/qore-add-env-key`.
54
+
53
55
  ## 2. Fail-fast at startup
54
56
 
55
57
  In a module-scoped init path (e.g. a server-only module imported by your feature
@@ -13,7 +13,7 @@ provider slug uppercased, non-alphanumerics collapsed to `_`. The SDK reads only
13
13
  this env — it never calls the portal at runtime.
14
14
 
15
15
  > A single-module app has no connections. Only follow this once the portal shows
16
- > a wired connection for this module (its env keys exist in the Secrets tab).
16
+ > the connection as Active in this module's Connections tab.
17
17
 
18
18
  This module is a plain `node:http` service (no framework), so the provider side
19
19
  needs one small adapter — step 4 covers it.
@@ -53,6 +53,8 @@ startup error, not a mystery failure. For a service connection to `billing-api`
53
53
  CONN_BILLING_API_TOKEN: z.string().min(1),
54
54
  ```
55
55
 
56
+ How to declare, mark and check a key: `/qore-add-env-key`.
57
+
56
58
  ## 2. Fail-fast at startup
57
59
 
58
60
  In `src/index.ts`, before `server.listen(...)`, assert every wire this module
@@ -1,23 +1,74 @@
1
1
  ---
2
2
  name: qore-add-env-key
3
- description: Add an env key to src/config/env.ts and decide whether it is a secret or configuration (`.meta({ kind: "config" })`). Use whenever this app reads a new environment variable, or when a key shows up under the wrong heading in the portal.
4
- allowed-tools: Edit, Read
3
+ description: Declare, read and protect this app's settings and secrets (env.ts, .env files, API keys, tokens, passwords, connection strings, feature flags and per-environment config). Use whenever a key is added, renamed or read, a value differs per environment, a third-party credential arrives, something must work locally, or a secret may end up in code, logs or errors, even if the word secret never comes up.
4
+ allowed-tools: Edit, Read, Write
5
5
  ---
6
6
 
7
- # Add an env key
7
+ # Settings and secrets
8
8
 
9
9
  `src/config/env.ts` is the one list of env this app reads. The manifest
10
10
  extractor reads it on every deploy and sends the portal each key's **name**
11
- (never its value), whether it is optional, and whether it is labelled
12
- configuration. Values are set per environment in the portal's Secrets tab.
11
+ (never its value), whether it is optional, whether it is labelled
12
+ configuration, and the value rules below. Values are set per environment in the
13
+ portal's Variables tab.
14
+
15
+ If you were about to...
16
+
17
+ | ...do this | do this instead |
18
+ |---|---|
19
+ | read `process.env.X` in a route | add `X` to `env.ts`, read it from there |
20
+ | paste a real key to make it work locally | `qore env pull`, or list it in `localSecrets` |
21
+ | add a Stripe key | `sk_` secret and `pk_` config, see section 1; the API itself is `/qore-add-integration` |
22
+ | add a feature flag that differs per environment | `z.enum([...])` or `z.stringbool()` marked config |
23
+ | log a failed call's response | log the status only, see section 5 |
13
24
 
14
25
  ## 1. Declare it
15
26
 
16
- Add the key to the zod server schema, and to `.env.example` with a blank value
17
- (never a real one). A required key that is unset fails the env parse, so make a
27
+ Add the key to the zod server schema{{#if runtime=next}}, and to `.env.example` with a **blank** value
28
+ (never a real one){{/if}}. A required key that is unset fails the env parse, so make a
18
29
  key required only when the app cannot work without it; otherwise add
19
30
  `.optional()`.
20
31
 
32
+ Give a value the tightest rule you know. The portal checks the rules when
33
+ someone applies a value in the Variables tab, and in the Integrations tab's
34
+ value window. A stored value that breaks a rule you add later shows as invalid
35
+ and counts as missing.
36
+
37
+ | You write | The portal checks |
38
+ |---|---|
39
+ | `z.string().min(n)` / `.max(n)` | at least / at most `n` characters |
40
+ | `z.string().length(n)` | exactly `n` (sets both bounds) |
41
+ | `.startsWith("sk_")` / `.endsWith("...")` | that prefix / suffix |
42
+ | `z.hex()` / `z.base64()` / `z.base64url()` | that encoding |
43
+ | `z.coerce.number().int()` | a whole number |
44
+ | `.min(n)` / `.max(n)` / `.gte(n)` / `.lte(n)` on a number | that bound |
45
+
46
+ Only literal arguments count: `.min(32)` yes, `.min(MIN_LEN)` no. The portal
47
+ ignores everything not in this table (`.regex()`, `.refine()`, `.transform()`,
48
+ `.positive()`, `.gt()`, `z.uuid()`, `.nonempty()`); the app still checks those
49
+ at boot.
50
+
51
+ ```ts
52
+ STRIPE_SECRET_KEY: z.string().startsWith("sk_"),
53
+ STRIPE_PUBLISHABLE_KEY: z.string().startsWith("pk_").meta({ kind: "config" }),
54
+ ENCRYPTION_KEY: z.hex().length(64),
55
+ {{#if runtime=next}}
56
+ SESSION_COOKIE_SECRET: z.hex().min(32),
57
+ {{/if}}
58
+ NEW_CHECKOUT: z.stringbool().meta({ kind: "config" }),
59
+ EXPORT_MODE: z.enum(["off", "csv", "xlsx"]).meta({ kind: "config" }),
60
+ ```
61
+ {{#if runtime=next}}
62
+
63
+ A secret never gets the `NEXT_PUBLIC_` prefix. Neither does anything else:
64
+ Qore builds the Next image once and promotes it, so no value from the Variables
65
+ tab is a build arg, and a `NEXT_PUBLIC_` value is `undefined` after deploy.
66
+ Reading it in the browser is also a `process.env` read outside `env.ts`. A
67
+ config value the browser needs (the Stripe publishable key) is read on the
68
+ server with `serverEnv()` and passed down as a prop or in the server-rendered
69
+ page.
70
+ {{/if}}
71
+
21
72
  ## 2. Secret or configuration?
22
73
 
23
74
  Every key is a **secret** unless you label it configuration:
@@ -30,7 +81,7 @@ Every key is a **secret** unless you label it configuration:
30
81
  ```
31
82
 
32
83
  The label decides two things in the portal: where a person looks for the key
33
- (Secrets or Configuration), and whether it is governed as a secret (age,
84
+ (the secret or the configuration list), and whether it is governed as a secret (age,
34
85
  rotation reminders, findings). So judge a key by what it belongs to, not only
35
86
  by what leaking it would cost.
36
87
 
@@ -52,6 +103,120 @@ environment's `env.ts` labels it so. An integration's credential fields
52
103
  /qore-add-integration) are always shown as secrets on the Integrations tab,
53
104
  whatever the label says, so never label one of them config.
54
105
 
106
+ ## 3. Where values live
107
+
108
+ - **Per environment:** a value is set in the Variables tab, once per
109
+ environment. A flag that differs between staging and production is two values
110
+ there, not two code paths. Staging and production need their values there
111
+ whatever you set locally.
112
+ - **Platform keys:** the portal writes some keys itself (database and
113
+ connection URLs, auth settings). The app cannot set them; do not ask the user
114
+ for them.
115
+ - **Locally:** `qore env pull` writes the module's env file
116
+ (`{{#if runtime=next}}.env.local{{else}}.env{{/if}}`) from the portal.
117
+ `qore dev` syncs that file and starts the dev server. `qore run -- <command>`
118
+ does not use the file: it fetches the portal's values fresh and injects them
119
+ into the command.
120
+ - **Third-party keys** (Stripe, SAP): set a local or test value for the key in
121
+ the portal, then `qore env pull`. Do not invent one.
122
+ - **App-owned secrets:** a key the app can generate itself, such as an
123
+ encryption key, is listed in `.qore.json` under `localSecrets`. `qore env pull`
124
+ then generates a local value that passes the key's rules (`z.hex().length(64)`
125
+ gives 64 hex characters). A recent qore honours the rules; an older one always
126
+ writes 64 hex characters, so if a generated value fails a rule at boot, update
127
+ qore. Never copy a production value to a laptop.
128
+ {{#if runtime=next}}
129
+
130
+ A `.qore.json` `localSecrets` list replaces qore's default for a Next module,
131
+ which is `["SESSION_COOKIE_SECRET"]`, and generated apps ship no `.qore.json`.
132
+ So when you create the file, keep `SESSION_COOKIE_SECRET` in the list:
133
+ {{/if}}
134
+
135
+ ```json
136
+ {{#if runtime=next}}
137
+ { "localSecrets": ["SESSION_COOKIE_SECRET", "ENCRYPTION_KEY"] }
138
+ {{else}}
139
+ { "localSecrets": ["ENCRYPTION_KEY"] }
140
+ {{/if}}
141
+ ```
142
+
143
+ ## 4. Rotation
144
+
145
+ The portal reminds you when a secret is old; it does not rotate it for you
146
+ (it manages rotation for some platform keys itself).
147
+
148
+ - **API key:** create a new one at the provider, set it in the Variables tab,
149
+ apply, then revoke the old one.
150
+ {{#if runtime=next}}
151
+ - **`SESSION_COOKIE_SECRET`:** everyone is signed out.
152
+ {{/if}}
153
+ - **Encryption key:** data written with the old key must be re-encrypted with
154
+ the new one first. Changing the value alone makes it unreadable.
155
+
156
+ ## 5. Never
157
+
158
+ {{#if runtime=next}}
159
+ - Never put a secret in code, in `.env.example`, in a log line, an error
160
+ message, a span attribute, the client bundle or a URL.
161
+ {{else}}
162
+ - Never put a secret in code, in a log line, an error message, a span
163
+ attribute or a URL.
164
+ {{/if}}
165
+ - Never read `process.env` outside `env.ts`.
166
+ - Never commit an env file.
167
+ - When a call fails, log the status. Never the response body, a token or an
168
+ env value. How to log: `/qore-observability`.
169
+
170
+ ## 6. Older apps
171
+
172
+ An app generated before these rules needs a one-time pass:
173
+
174
+ 1. Tighten the known keys.
175
+ {{#if runtime=next}}
176
+ `SESSION_COOKIE_SECRET` is `z.hex().min(32)`.
177
+ {{else}}
178
+ `PORT` is `z.coerce.number().int().min(1).max(65535)` marked config, and
179
+ `DATABASE_URL`, when the module has a database, is `z.string().startsWith("postgres")`.
180
+ {{/if}}
181
+ 2. Mark config keys with `.meta({ kind: "config" })` (section 2).
182
+ 3. Make `.gitignore` cover the env files (`.env*`{{#if runtime=next}}, except `.env.example`{{/if}}).
183
+ 4. Register the secret values with the Sentry scrubber once, right after the
184
+ env parse, so a value that slips into an error is masked. Register every key
185
+ that is not marked config, so a key added later is covered too. Strings
186
+ shorter than 8 characters and non-strings are ignored.
187
+
188
+ {{#if runtime=next}}
189
+ ```ts
190
+ import { registerSecretValues } from "@quatronic/sdk/next/sentry"
191
+
192
+ export function serverEnv(): ServerEnv {
193
+ if (!_env) {
194
+ // ...parse as before, then:
195
+ _env = parsed.data
196
+ registerSecretValues(
197
+ (Object.keys(serverSchema.shape) as (keyof ServerEnv)[])
198
+ .filter((key) => serverSchema.shape[key].meta()?.kind !== "config")
199
+ .map((key) => parsed.data[key]),
200
+ )
201
+ }
202
+ return _env
203
+ }
204
+ ```
205
+ {{else}}
206
+ ```ts
207
+ import { registerSecretValues } from "@quatronic/sdk/node/sentry"
208
+
209
+ export const env = schema.parse(raw)
210
+
211
+ registerSecretValues(
212
+ (Object.keys(schema.shape) as (keyof typeof env)[])
213
+ .filter((key) => schema.shape[key].meta()?.kind !== "config")
214
+ .map((key) => env[key]),
215
+ )
216
+ ```
217
+ {{/if}}
218
+
55
219
  ## Don't
56
220
 
57
- - **Don't** put a real value in `.env.example`, in code, or in a log line.
221
+ - **Don't** put a real value{{#if runtime=next}} in `.env.example`,{{/if}} in code, or in a log line.
222
+ - **Don't** label a login's identifier config to "tidy" the portal.
@@ -40,7 +40,7 @@ Code written with these skills is read by the portal:
40
40
 
41
41
  | Skill | What the portal reads |
42
42
  |---|---|
43
- | `qore-add-env-key` | The Secrets tab: which keys are secrets, which configuration |
43
+ | `qore-add-env-key` | The Variables tab: which keys are secrets, which configuration |
44
44
  | `qore-add-integration` | The Integrations tab |
45
45
  | `qore-add-connection` | Connection wiring |
46
46
  | `qore-use-permissions` | Roles on the Access page |
@@ -15,6 +15,8 @@ skill is the whole contract.
15
15
  `src/config/env.ts` requires `DATABASE_URL` (`z.string().min(1)`), so the service
16
16
  refuses to boot without it.
17
17
 
18
+ How to declare, mark and check a key: `/qore-add-env-key`.
19
+
18
20
  | Where | How it arrives |
19
21
  |-------|----------------|
20
22
  | **Local** | `pnpm bootstrap` (or `qore env pull`) fetches it from the portal into `.env`. It points at the docker-compose database below on this module's own port: `postgresql://postgres:postgres@localhost:<DB_PORT>/app`, with `DB_PORT` written alongside it |
@@ -18,7 +18,7 @@ environments:
18
18
  | `MAIL_FROM` | the full from-header, e.g. `Acme Support <support@acme.nl>` |
19
19
 
20
20
  > These are system-managed: they never appear in `.env.example`, never show up
21
- > in this module's Secrets tab, and you cannot set them yourself. Until a
21
+ > in this module's Variables tab, and you cannot set them yourself. Until a
22
22
  > domain is linked they're simply absent.
23
23
 
24
24
  ## Hard rule: never present in `local` or preview
@@ -17,8 +17,8 @@ environments:
17
17
  | `MAIL_SENDING_DOMAIN` | the domain, e.g. `acme.nl` — informational only, already baked into `MAIL_FROM` |
18
18
  | `MAIL_FROM` | the full from-header, e.g. `Acme Support <support@acme.nl>` |
19
19
 
20
- > These are system-managed: they never appear in `.env.example`, never show up
21
- > in this module's Secrets tab, and you cannot set them yourself. Until a
20
+ > These are system-managed: they never show up
21
+ > in this module's Variables tab, and you cannot set them yourself. Until a
22
22
  > domain is linked they're simply absent.
23
23
 
24
24
  ## Hard rule: never present in `local` or preview