@norskvideo/ctl-product-template-schema 0.1.86 → 0.1.87

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@norskvideo/ctl-product-template-schema",
3
- "version": "0.1.86",
3
+ "version": "0.1.87",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,6 +12,7 @@ export { fileSecretProvider } from "./providers/file.js";
12
12
  export { DEFAULT_K8S_SECRETS_ROOT, k8sSecretProvider } from "./providers/k8s.js";
13
13
  export { localResolver, type ResolveContext, resolveSecretReferences, type SecretResolver } from "./resolver.js";
14
14
  export { Secret } from "./secret.js";
15
+ export { autoKeyFile, escrowedKids, escrowMarker, generateKeyLine, isSealed, type Keyring, keyIdOf, type LoadKeyringOptions, loadKeyring, type OpenedVault, type OpenVaultOptions, openSealed, openVault, parseKeyring, recordEscrow, rewrapSealed, SEALED_PREFIX, type SealedFor, seal, sealedKeyId, VAULT_DIR, type VaultKey, type VaultState, writeNewKeyring, } from "./vault.js";
15
16
  export * from "./wire.js";
16
17
  export declare const SECRET_PROVIDERS_ENV = "NORSK_SECRET_PROVIDERS";
17
18
  /** In local mode, a profile in use (NORSK_SECRETS_PROFILE) is announced once
package/secrets/index.js CHANGED
@@ -21,6 +21,7 @@ export { fileSecretProvider } from "./providers/file.js";
21
21
  export { DEFAULT_K8S_SECRETS_ROOT, k8sSecretProvider } from "./providers/k8s.js";
22
22
  export { localResolver, resolveSecretReferences } from "./resolver.js";
23
23
  export { Secret } from "./secret.js";
24
+ export { autoKeyFile, escrowedKids, escrowMarker, generateKeyLine, isSealed, keyIdOf, loadKeyring, openSealed, openVault, parseKeyring, recordEscrow, rewrapSealed, SEALED_PREFIX, seal, sealedKeyId, VAULT_DIR, writeNewKeyring, } from "./vault.js";
24
25
  export * from "./wire.js";
25
26
  export const SECRET_PROVIDERS_ENV = "NORSK_SECRET_PROVIDERS";
26
27
  /** `norsk` secrets are held by norsk-ctl or norsk-mgr, so only ctl resolves them. */
@@ -20,8 +20,9 @@ export declare function keyIdOf(key: Buffer): string;
20
20
  export declare function parseKeyring(text: string): Result<Keyring, string>;
21
21
  /** A new key, as a keyring line. */
22
22
  export declare function generateKeyLine(): string;
23
- /** Where norsk-mgr keeps the keyring it made itself when none is configured:
24
- * the store's private vault directory, which no backup takes. */
23
+ /** Where the keyring made when none is configured lives: under the store's
24
+ * (norsk-mgr) or state directory (norsk-ctl), in a private vault directory
25
+ * no backup takes. */
25
26
  export declare const VAULT_DIR = "vault";
26
27
  export declare function autoKeyFile(storeDir: string): string;
27
28
  export declare function isSealed(text: string): boolean;
@@ -39,7 +40,7 @@ export interface LoadKeyringOptions {
39
40
  * `secretref:` resolved through the deployment's providers (not `norsk:`,
40
41
  * and not `file:`, which does not check a file's mode: name the file). */
41
42
  export declare function loadKeyring(source: string, opts?: LoadKeyringOptions): Promise<Result<Keyring, string>>;
42
- /** `norsk-mgr secret keygen`: a new keyring file, or with `rotate` a new
43
+ /** `secret keygen` (norsk-mgr's and norsk-ctl's): a new keyring file, or with `rotate` a new
43
44
  * current key in front of an existing file's. Answers the kids, never a key.
44
45
  * Written to a fresh temp file (never through one planted there), synced,
45
46
  * then renamed over the keyring and the directory synced, so a crash leaves
@@ -49,3 +50,44 @@ export declare function writeNewKeyring(path: string, opts: {
49
50
  }): Result<{
50
51
  kids: string[];
51
52
  }, string>;
53
+ /** The vault's master key: loaded, or why not. With none loaded nothing new
54
+ * can be sealed, and no sealed value opens. */
55
+ export type VaultState = {
56
+ status: "on";
57
+ keyring: Keyring;
58
+ } | {
59
+ status: "unavailable";
60
+ reason: string;
61
+ };
62
+ export interface OpenVaultOptions {
63
+ /** The configured keyring: an owner-only file's absolute path or a
64
+ * `secretref:`. Undefined: the generated one at `autoKeyFile`. */
65
+ source: string | undefined;
66
+ /** Where a generated keyring lives (`autoKeyFile(<dir>)`). */
67
+ autoKeyFile: string;
68
+ /** Whether one may be generated there when none exists (not on a member
69
+ * of a cluster, whose members must share one). */
70
+ generate: boolean;
71
+ /** Why none may be generated, when `generate` is false. */
72
+ refusal?: string;
73
+ keyring?: LoadKeyringOptions;
74
+ }
75
+ export interface OpenedVault {
76
+ state: VaultState;
77
+ /** Set when the keyring is the generated one: the operator must keep a copy. */
78
+ autoKey?: string;
79
+ /** The current key's id when one was generated just now. */
80
+ generatedKid?: string;
81
+ }
82
+ /** The master key, by the one rule norsk-mgr and norsk-ctl share: the
83
+ * configured keyring, else the generated one (made on the first start
84
+ * without one). A configured key that does not load leaves the vault
85
+ * unavailable; it never falls back to a generated one. */
86
+ export declare function openVault(opts: OpenVaultOptions): Promise<OpenedVault>;
87
+ /** Beside a generated keyring: the key ids the operator has said are kept
88
+ * off the host. */
89
+ export declare const escrowMarker: (keyFile: string) => string;
90
+ export declare function escrowedKids(keyFile: string): string[];
91
+ /** Record that the generated keyring's current key is kept elsewhere.
92
+ * Answers whether it was newly recorded. */
93
+ export declare function recordEscrow(keyFile: string, kid: string): boolean;
package/secrets/vault.js CHANGED
@@ -1,6 +1,7 @@
1
- // norsk-mgr's secrets vault (norsk-ctl RFC 0013 §3): a `norsk` secret's value
2
- // at rest, sealed with AES-256-GCM under a data key of its own, which is
3
- // wrapped by a master key that is never in the store or a backup.
1
+ // The secrets vault (norsk-ctl RFC 0013 §3), norsk-mgr's and a standalone
2
+ // norsk-ctl's alike: a `norsk` secret's value at rest, sealed with AES-256-GCM
3
+ // under a data key of its own, which is wrapped by a master key that is never
4
+ // in the store or a backup.
4
5
  //
5
6
  // nsenc:v1:<kid>:<wrapNonce>:<wrappedKey>:<nonce>:<ciphertext> (base64url)
6
7
  //
@@ -12,7 +13,9 @@ import { createCipheriv, createDecipheriv, createHmac, randomBytes } from "node:
12
13
  import { chmodSync, closeSync, constants, existsSync, fstatSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeSync, } from "node:fs";
13
14
  import { dirname, isAbsolute, join } from "node:path";
14
15
  import { err, ok, tryResult } from "@norskvideo/ctl-foundation";
15
- import { createResolverFromEnv, isSecretReference, parseSecretReference, SECRETS_TOKEN_ENV, SECRETS_URL_ENV, } from "@norskvideo/ctl-product-template-schema/secrets";
16
+ import { isSecretReference, parseSecretReference } from "../secret-references.js";
17
+ import { createResolverFromEnv } from "./index.js";
18
+ import { SECRETS_TOKEN_ENV, SECRETS_URL_ENV } from "./wire.js";
16
19
  export const SEALED_PREFIX = "nsenc:v1:";
17
20
  const KEY_BYTES = 32;
18
21
  const NONCE_BYTES = 12;
@@ -50,8 +53,9 @@ export function parseKeyring(text) {
50
53
  export function generateKeyLine() {
51
54
  return randomBytes(KEY_BYTES).toString("base64url");
52
55
  }
53
- /** Where norsk-mgr keeps the keyring it made itself when none is configured:
54
- * the store's private vault directory, which no backup takes. */
56
+ /** Where the keyring made when none is configured lives: under the store's
57
+ * (norsk-mgr) or state directory (norsk-ctl), in a private vault directory
58
+ * no backup takes. */
55
59
  export const VAULT_DIR = "vault";
56
60
  export function autoKeyFile(storeDir) {
57
61
  return join(storeDir, VAULT_DIR, "secrets-master.key");
@@ -160,7 +164,7 @@ export function rewrapSealed(keyring, text, at) {
160
164
  async function resolveWithLocalProviders(reference) {
161
165
  // Always local: the master key is never fetched from a norsk-ctl.
162
166
  const env = { ...process.env, [SECRETS_URL_ENV]: undefined, [SECRETS_TOKEN_ENV]: undefined };
163
- const resolved = await createResolverFromEnv(env).resolve({ secretsMasterKeyFile: reference }, { purpose: "norsk-mgr secrets master key" });
167
+ const resolved = await createResolverFromEnv(env).resolve({ secretsMasterKeyFile: reference }, { purpose: "secrets master key" });
164
168
  if (resolved.status === "error")
165
169
  return err(resolved.error.message);
166
170
  const value = resolved.value.secretsMasterKeyFile;
@@ -220,7 +224,7 @@ function readOwnerOnly(path) {
220
224
  closeSync(fd);
221
225
  }
222
226
  }
223
- /** `norsk-mgr secret keygen`: a new keyring file, or with `rotate` a new
227
+ /** `secret keygen` (norsk-mgr's and norsk-ctl's): a new keyring file, or with `rotate` a new
224
228
  * current key in front of an existing file's. Answers the kids, never a key.
225
229
  * Written to a fresh temp file (never through one planted there), synced,
226
230
  * then renamed over the keyring and the directory synced, so a crash leaves
@@ -270,3 +274,61 @@ export function writeNewKeyring(path, opts) {
270
274
  }
271
275
  return ok({ kids: parsed.value.keys.map((k) => k.kid) });
272
276
  }
277
+ /** The master key, by the one rule norsk-mgr and norsk-ctl share: the
278
+ * configured keyring, else the generated one (made on the first start
279
+ * without one). A configured key that does not load leaves the vault
280
+ * unavailable; it never falls back to a generated one. */
281
+ export async function openVault(opts) {
282
+ if (opts.source !== undefined) {
283
+ const loaded = await loadKeyring(opts.source, opts.keyring ?? {});
284
+ return {
285
+ state: loaded.status === "ok"
286
+ ? { status: "on", keyring: loaded.value }
287
+ : { status: "unavailable", reason: loaded.error },
288
+ };
289
+ }
290
+ let generatedKid;
291
+ if (!existsSync(opts.autoKeyFile)) {
292
+ if (!opts.generate)
293
+ return { state: { status: "unavailable", reason: opts.refusal ?? "no secrets master key" } };
294
+ const made = writeNewKeyring(opts.autoKeyFile, { rotate: false });
295
+ if (made.status === "error")
296
+ return { state: { status: "unavailable", reason: made.error } };
297
+ generatedKid = made.value.kids[0];
298
+ }
299
+ const loaded = await loadKeyring(opts.autoKeyFile);
300
+ if (loaded.status === "error")
301
+ return { state: { status: "unavailable", reason: loaded.error } };
302
+ return {
303
+ state: { status: "on", keyring: loaded.value },
304
+ autoKey: opts.autoKeyFile,
305
+ ...(generatedKid ? { generatedKid } : {}),
306
+ };
307
+ }
308
+ /** Beside a generated keyring: the key ids the operator has said are kept
309
+ * off the host. */
310
+ export const escrowMarker = (keyFile) => join(dirname(keyFile), "escrowed");
311
+ export function escrowedKids(keyFile) {
312
+ const marker = escrowMarker(keyFile);
313
+ return existsSync(marker) ? readFileSync(marker, "utf-8").split("\n").filter(Boolean) : [];
314
+ }
315
+ /** Record that the generated keyring's current key is kept elsewhere.
316
+ * Answers whether it was newly recorded. */
317
+ export function recordEscrow(keyFile, kid) {
318
+ const kids = escrowedKids(keyFile);
319
+ if (kids.includes(kid))
320
+ return false;
321
+ const marker = escrowMarker(keyFile);
322
+ const tmp = `${marker}.tmp`;
323
+ rmSync(tmp, { force: true });
324
+ const fd = openSync(tmp, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
325
+ try {
326
+ writeSync(fd, `${[...kids, kid].join("\n")}\n`);
327
+ fsyncSync(fd);
328
+ }
329
+ finally {
330
+ closeSync(fd);
331
+ }
332
+ renameSync(tmp, marker);
333
+ return true;
334
+ }