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

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.84",
3
+ "version": "0.1.86",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -3,7 +3,7 @@ export { isSecretReference, parseSecretReference, SECRET_PLACEHOLDER, SECRET_REF
3
3
  export { activeSecretsProfile, type LoadSecretProvidersOptions, loadSecretProviders, PRODUCTION_SECRETS_PROFILE, RESERVED_PROVIDER_NAMES, SECRETS_PROFILE_ENV, SecretProfileSchema, type SecretProvidersConfig, SecretProvidersConfigSchema, secretsProfileNotice, } from "./config.js";
4
4
  export { type DelegateOptions, delegateResolver } from "./delegate.js";
5
5
  export { INVALID_SECRET_NAME_MESSAGE, MAX_SECRET_VALUE_BYTES, validSecretName } from "./names.js";
6
- export { deobfuscate, isObfuscated, OBFUSCATED_PREFIX, obfuscate } from "./obfuscation.js";
6
+ export { deobfuscate, freshObfuscationParams, isObfuscated, OBFUSCATED_PREFIX, obfuscate, obfuscationParams, } from "./obfuscation.js";
7
7
  export type { SecretProvider, SecretProviders, SecretResolutionError } from "./provider.js";
8
8
  export { type AwsSecretProviderOptions, awsSecretProvider } from "./providers/aws.js";
9
9
  export { envSecretProvider } from "./providers/env.js";
package/secrets/index.js CHANGED
@@ -13,7 +13,7 @@ export { isSecretReference, parseSecretReference, SECRET_PLACEHOLDER, SECRET_REF
13
13
  export { activeSecretsProfile, loadSecretProviders, PRODUCTION_SECRETS_PROFILE, RESERVED_PROVIDER_NAMES, SECRETS_PROFILE_ENV, SecretProfileSchema, SecretProvidersConfigSchema, secretsProfileNotice, } from "./config.js";
14
14
  export { delegateResolver } from "./delegate.js";
15
15
  export { INVALID_SECRET_NAME_MESSAGE, MAX_SECRET_VALUE_BYTES, validSecretName } from "./names.js";
16
- export { deobfuscate, isObfuscated, OBFUSCATED_PREFIX, obfuscate } from "./obfuscation.js";
16
+ export { deobfuscate, freshObfuscationParams, isObfuscated, OBFUSCATED_PREFIX, obfuscate, obfuscationParams, } from "./obfuscation.js";
17
17
  export { awsSecretProvider } from "./providers/aws.js";
18
18
  export { envSecretProvider } from "./providers/env.js";
19
19
  export { execSecretProvider } from "./providers/exec.js";
@@ -1,6 +1,14 @@
1
1
  import { type Result } from "@norskvideo/ctl-foundation";
2
2
  export declare const OBFUSCATED_PREFIX = "nsob:v1:";
3
3
  export declare function isObfuscated(text: string): boolean;
4
- export declare function obfuscate(value: string): string;
4
+ /** A fresh salt and nonce, `<salt>:<nonce>` in base64url, for a holder that
5
+ * must re-send one value's envelope unchanged (norsk-mgr's delivery to jobs). */
6
+ export declare function freshObfuscationParams(): string;
7
+ /** The salt and nonce of an envelope, as `freshObfuscationParams` gives them. */
8
+ export declare function obfuscationParams(text: string): string | undefined;
9
+ /** With `params`, the envelope is deterministic: the same value gives the same
10
+ * bytes. Only ever reuse params for the SAME value; a fresh value gets fresh
11
+ * ones. */
12
+ export declare function obfuscate(value: string, params?: string): string;
5
13
  /** The value, or an error that never contains any of the text it was given. */
6
14
  export declare function deobfuscate(text: string): Result<string, string>;
@@ -21,9 +21,35 @@ const keyFor = (salt) => Buffer.from(hkdfSync("sha256", CONSTANT, salt, "nsob:v1
21
21
  export function isObfuscated(text) {
22
22
  return text.startsWith(OBFUSCATED_PREFIX);
23
23
  }
24
- export function obfuscate(value) {
25
- const salt = randomBytes(SALT_BYTES);
26
- const nonce = randomBytes(NONCE_BYTES);
24
+ /** A fresh salt and nonce, `<salt>:<nonce>` in base64url, for a holder that
25
+ * must re-send one value's envelope unchanged (norsk-mgr's delivery to jobs). */
26
+ export function freshObfuscationParams() {
27
+ return `${randomBytes(SALT_BYTES).toString("base64url")}:${randomBytes(NONCE_BYTES).toString("base64url")}`;
28
+ }
29
+ /** The salt and nonce of an envelope, as `freshObfuscationParams` gives them. */
30
+ export function obfuscationParams(text) {
31
+ if (!isObfuscated(text))
32
+ return undefined;
33
+ const parts = text.slice(OBFUSCATED_PREFIX.length).split(":");
34
+ if (parts.length !== 3)
35
+ return undefined;
36
+ return parsedParams(`${parts[0]}:${parts[1]}`) ? `${parts[0]}:${parts[1]}` : undefined;
37
+ }
38
+ function parsedParams(params) {
39
+ const [salt, nonce, extra] = params.split(":").map((p) => Buffer.from(p, "base64url"));
40
+ if (!salt || !nonce || extra || salt.length !== SALT_BYTES || nonce.length !== NONCE_BYTES)
41
+ return undefined;
42
+ return { salt, nonce };
43
+ }
44
+ /** With `params`, the envelope is deterministic: the same value gives the same
45
+ * bytes. Only ever reuse params for the SAME value; a fresh value gets fresh
46
+ * ones. */
47
+ export function obfuscate(value, params) {
48
+ const given = params === undefined ? undefined : parsedParams(params);
49
+ if (params !== undefined && !given)
50
+ throw new Error("malformed obfuscation parameters");
51
+ const salt = given?.salt ?? randomBytes(SALT_BYTES);
52
+ const nonce = given?.nonce ?? randomBytes(NONCE_BYTES);
27
53
  const cipher = createCipheriv("aes-256-gcm", keyFor(salt), nonce);
28
54
  const body = Buffer.concat([cipher.update(value, "utf-8"), cipher.final(), cipher.getAuthTag()]);
29
55
  return `${OBFUSCATED_PREFIX}${[salt, nonce, body].map((b) => b.toString("base64url")).join(":")}`;
@@ -0,0 +1,51 @@
1
+ import { type Result } from "@norskvideo/ctl-foundation";
2
+ export declare const SEALED_PREFIX = "nsenc:v1:";
3
+ export interface VaultKey {
4
+ kid: string;
5
+ key: Buffer;
6
+ }
7
+ /** `keys[0]` is `current`: it seals every new write. The rest only open. */
8
+ export interface Keyring {
9
+ current: VaultKey;
10
+ keys: VaultKey[];
11
+ }
12
+ /** The record a value belongs to: what its ciphertext is bound to. */
13
+ export interface SealedFor {
14
+ name: string;
15
+ version: number;
16
+ }
17
+ export declare function keyIdOf(key: Buffer): string;
18
+ /** One key per line (base64 or base64url of 32 bytes), first current; blank
19
+ * lines and `#` comments ignored. */
20
+ export declare function parseKeyring(text: string): Result<Keyring, string>;
21
+ /** A new key, as a keyring line. */
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. */
25
+ export declare const VAULT_DIR = "vault";
26
+ export declare function autoKeyFile(storeDir: string): string;
27
+ export declare function isSealed(text: string): boolean;
28
+ export declare function sealedKeyId(text: string): string | undefined;
29
+ export declare function seal(keyring: Keyring, value: string, at: SealedFor): string;
30
+ export declare function openSealed(keyring: Keyring, text: string, at: SealedFor): Result<string, string>;
31
+ /** The same value under the keyring's current key: the data key is unwrapped
32
+ * and wrapped again, and the value is opened only to prove the result does. */
33
+ export declare function rewrapSealed(keyring: Keyring, text: string, at: SealedFor): Result<string, string>;
34
+ export interface LoadKeyringOptions {
35
+ /** Resolves a `secretref:` source; the deployment's local providers by default. */
36
+ resolve?: (reference: string) => Promise<Result<string, string>>;
37
+ }
38
+ /** The keyring from `source`: an absolute path to an owner-only file, or a
39
+ * `secretref:` resolved through the deployment's providers (not `norsk:`,
40
+ * and not `file:`, which does not check a file's mode: name the file). */
41
+ 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
+ * current key in front of an existing file's. Answers the kids, never a key.
44
+ * Written to a fresh temp file (never through one planted there), synced,
45
+ * then renamed over the keyring and the directory synced, so a crash leaves
46
+ * the old keyring or the new one, never an empty one. */
47
+ export declare function writeNewKeyring(path: string, opts: {
48
+ rotate: boolean;
49
+ }): Result<{
50
+ kids: string[];
51
+ }, string>;
@@ -0,0 +1,272 @@
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.
4
+ //
5
+ // nsenc:v1:<kid>:<wrapNonce>:<wrappedKey>:<nonce>:<ciphertext> (base64url)
6
+ //
7
+ // The data key's wrap is bound to its kid, and the value to its record's name
8
+ // and version, so a sealed value moved to another record does not open.
9
+ // Re-wrapping under a new master key changes only the wrapped data key.
10
+ // Nothing here logs, and no error carries a key, a value or an envelope.
11
+ import { createCipheriv, createDecipheriv, createHmac, randomBytes } from "node:crypto";
12
+ import { chmodSync, closeSync, constants, existsSync, fstatSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeSync, } from "node:fs";
13
+ import { dirname, isAbsolute, join } from "node:path";
14
+ 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
+ export const SEALED_PREFIX = "nsenc:v1:";
17
+ const KEY_BYTES = 32;
18
+ const NONCE_BYTES = 12;
19
+ const TAG_BYTES = 16;
20
+ const KID_INFO = "norsk-mgr secrets vault key id";
21
+ export function keyIdOf(key) {
22
+ return createHmac("sha256", key).update(KID_INFO).digest().subarray(0, 9).toString("base64url");
23
+ }
24
+ const BASE64ISH = /^[A-Za-z0-9+/_-]+={0,2}$/;
25
+ /** One key per line (base64 or base64url of 32 bytes), first current; blank
26
+ * lines and `#` comments ignored. */
27
+ export function parseKeyring(text) {
28
+ const keys = [];
29
+ const lines = text
30
+ .split(/\r?\n/)
31
+ .map((l) => l.trim())
32
+ .filter((l) => l !== "" && !l.startsWith("#"));
33
+ for (const [i, line] of lines.entries()) {
34
+ // Node's base64 decoder takes both alphabets; the pattern stops it
35
+ // skipping characters that are neither.
36
+ const key = BASE64ISH.test(line) ? Buffer.from(line, "base64") : undefined;
37
+ if (!key || key.length !== KEY_BYTES)
38
+ return err(`keyring line ${i + 1} is not a 32-byte key in base64`);
39
+ const kid = keyIdOf(key);
40
+ if (keys.some((k) => k.kid === kid))
41
+ return err(`keyring line ${i + 1} repeats key ${kid}`);
42
+ keys.push({ kid, key });
43
+ }
44
+ const [current] = keys;
45
+ if (!current)
46
+ return err("the keyring holds no key");
47
+ return ok({ current, keys });
48
+ }
49
+ /** A new key, as a keyring line. */
50
+ export function generateKeyLine() {
51
+ return randomBytes(KEY_BYTES).toString("base64url");
52
+ }
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. */
55
+ export const VAULT_DIR = "vault";
56
+ export function autoKeyFile(storeDir) {
57
+ return join(storeDir, VAULT_DIR, "secrets-master.key");
58
+ }
59
+ export function isSealed(text) {
60
+ return text.startsWith(SEALED_PREFIX);
61
+ }
62
+ export function sealedKeyId(text) {
63
+ return isSealed(text) ? text.slice(SEALED_PREFIX.length).split(":")[0] || undefined : undefined;
64
+ }
65
+ const b64 = (b) => b.toString("base64url");
66
+ function gcmSeal(key, plain, aad) {
67
+ const nonce = randomBytes(NONCE_BYTES);
68
+ const cipher = createCipheriv("aes-256-gcm", key, nonce);
69
+ cipher.setAAD(Buffer.from(aad, "utf-8"));
70
+ return { nonce, body: Buffer.concat([cipher.update(plain), cipher.final(), cipher.getAuthTag()]) };
71
+ }
72
+ function gcmOpen(key, nonce, body, aad) {
73
+ return tryResult(() => {
74
+ const decipher = createDecipheriv("aes-256-gcm", key, nonce);
75
+ decipher.setAAD(Buffer.from(aad, "utf-8"));
76
+ decipher.setAuthTag(body.subarray(body.length - TAG_BYTES));
77
+ return Buffer.concat([decipher.update(body.subarray(0, body.length - TAG_BYTES)), decipher.final()]);
78
+ }, () => "sealed value does not open (wrong key, or damaged)");
79
+ }
80
+ const wrapAad = (kid) => `${SEALED_PREFIX}${kid}`;
81
+ const valueAad = (at) => `nsenc:v1|${at.name}|${at.version}`;
82
+ function parse(text) {
83
+ if (!isSealed(text))
84
+ return err("not a sealed value");
85
+ const parts = text.slice(SEALED_PREFIX.length).split(":");
86
+ if (parts.length !== 5 || parts.some((p) => p === "" || !/^[A-Za-z0-9_-]+$/.test(p))) {
87
+ return err("malformed sealed value");
88
+ }
89
+ const [kid, wrapNonce, wrappedKey, nonce, ciphertext] = parts;
90
+ const parsed = {
91
+ kid,
92
+ wrapNonce: Buffer.from(wrapNonce, "base64url"),
93
+ wrappedKey: Buffer.from(wrappedKey, "base64url"),
94
+ nonce,
95
+ ciphertext,
96
+ };
97
+ if (parsed.wrapNonce.length !== NONCE_BYTES ||
98
+ parsed.wrappedKey.length !== KEY_BYTES + TAG_BYTES ||
99
+ Buffer.from(nonce, "base64url").length !== NONCE_BYTES ||
100
+ Buffer.from(ciphertext, "base64url").length < TAG_BYTES) {
101
+ return err("malformed sealed value");
102
+ }
103
+ return ok(parsed);
104
+ }
105
+ function unwrapDataKey(keyring, p) {
106
+ const master = keyring.keys.find((k) => k.kid === p.kid);
107
+ if (!master)
108
+ return err(`sealed under key ${p.kid}, which the keyring does not hold`);
109
+ return gcmOpen(master.key, p.wrapNonce, p.wrappedKey, wrapAad(p.kid));
110
+ }
111
+ function wrapDataKey(keyring, dataKey) {
112
+ const { kid, key } = keyring.current;
113
+ const wrapped = gcmSeal(key, dataKey, wrapAad(kid));
114
+ return `${SEALED_PREFIX}${kid}:${b64(wrapped.nonce)}:${b64(wrapped.body)}`;
115
+ }
116
+ export function seal(keyring, value, at) {
117
+ const dataKey = randomBytes(KEY_BYTES);
118
+ try {
119
+ const sealed = gcmSeal(dataKey, Buffer.from(value, "utf-8"), valueAad(at));
120
+ return `${wrapDataKey(keyring, dataKey)}:${b64(sealed.nonce)}:${b64(sealed.body)}`;
121
+ }
122
+ finally {
123
+ dataKey.fill(0);
124
+ }
125
+ }
126
+ export function openSealed(keyring, text, at) {
127
+ const p = parse(text);
128
+ if (p.status === "error")
129
+ return p;
130
+ const dataKey = unwrapDataKey(keyring, p.value);
131
+ if (dataKey.status === "error")
132
+ return dataKey;
133
+ try {
134
+ const plain = gcmOpen(dataKey.value, Buffer.from(p.value.nonce, "base64url"), Buffer.from(p.value.ciphertext, "base64url"), valueAad(at));
135
+ return plain.status === "ok" ? ok(plain.value.toString("utf-8")) : plain;
136
+ }
137
+ finally {
138
+ dataKey.value.fill(0);
139
+ }
140
+ }
141
+ /** The same value under the keyring's current key: the data key is unwrapped
142
+ * and wrapped again, and the value is opened only to prove the result does. */
143
+ export function rewrapSealed(keyring, text, at) {
144
+ const opened = openSealed(keyring, text, at);
145
+ if (opened.status === "error")
146
+ return opened;
147
+ const p = parse(text);
148
+ if (p.status === "error")
149
+ return p;
150
+ const dataKey = unwrapDataKey(keyring, p.value);
151
+ if (dataKey.status === "error")
152
+ return dataKey;
153
+ try {
154
+ return ok(`${wrapDataKey(keyring, dataKey.value)}:${p.value.nonce}:${p.value.ciphertext}`);
155
+ }
156
+ finally {
157
+ dataKey.value.fill(0);
158
+ }
159
+ }
160
+ async function resolveWithLocalProviders(reference) {
161
+ // Always local: the master key is never fetched from a norsk-ctl.
162
+ 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" });
164
+ if (resolved.status === "error")
165
+ return err(resolved.error.message);
166
+ const value = resolved.value.secretsMasterKeyFile;
167
+ return value === undefined ? err("no value") : ok(value.reveal());
168
+ }
169
+ /** The keyring from `source`: an absolute path to an owner-only file, or a
170
+ * `secretref:` resolved through the deployment's providers (not `norsk:`,
171
+ * and not `file:`, which does not check a file's mode: name the file). */
172
+ export async function loadKeyring(source, opts = {}) {
173
+ if (isSecretReference(source)) {
174
+ const ref = parseSecretReference(source);
175
+ if (ref.status === "error")
176
+ return err(`the secrets master key reference is malformed: ${ref.error}`);
177
+ if (ref.value.provider === "norsk")
178
+ return err("the secrets master key cannot be a norsk: secret");
179
+ if (ref.value.provider === "file") {
180
+ return err("the secrets master key cannot be a file: reference; give the key file's path, so its mode is checked");
181
+ }
182
+ const resolved = await (opts.resolve ?? resolveWithLocalProviders)(source);
183
+ if (resolved.status === "error")
184
+ return err(`the secrets master key could not be resolved: ${resolved.error}`);
185
+ return parseKeyring(resolved.value);
186
+ }
187
+ if (!isAbsolute(source))
188
+ return err(`the secrets master key file must be an absolute path, not '${source}'`);
189
+ const text = readOwnerOnly(source);
190
+ return text.status === "ok" ? parseKeyring(text.value) : text;
191
+ }
192
+ /** The file's text, checked on the descriptor it is read from (no window
193
+ * between the check and the read), and never through a symlink. */
194
+ function readOwnerOnly(path) {
195
+ let fd;
196
+ try {
197
+ fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
198
+ }
199
+ catch (e) {
200
+ const code = e.code;
201
+ if (code === "ENOENT")
202
+ return err(`the secrets master key file ${path} does not exist`);
203
+ if (code === "ELOOP")
204
+ return err(`the secrets master key file ${path} is a symlink; name the file itself`);
205
+ return err(`the secrets master key file ${path} cannot be opened (${code ?? "error"})`);
206
+ }
207
+ try {
208
+ const st = fstatSync(fd);
209
+ if (!st.isFile())
210
+ return err(`the secrets master key file ${path} is not a regular file`);
211
+ if ((st.mode & 0o077) !== 0) {
212
+ return err(`the secrets master key file ${path} can be read by others (mode ${(st.mode & 0o777).toString(8)}); make it 0600`);
213
+ }
214
+ if (typeof process.getuid === "function" && st.uid !== process.getuid()) {
215
+ return err(`the secrets master key file ${path} is not owned by this user`);
216
+ }
217
+ return ok(readFileSync(fd, "utf-8"));
218
+ }
219
+ finally {
220
+ closeSync(fd);
221
+ }
222
+ }
223
+ /** `norsk-mgr secret keygen`: a new keyring file, or with `rotate` a new
224
+ * current key in front of an existing file's. Answers the kids, never a key.
225
+ * Written to a fresh temp file (never through one planted there), synced,
226
+ * then renamed over the keyring and the directory synced, so a crash leaves
227
+ * the old keyring or the new one, never an empty one. */
228
+ export function writeNewKeyring(path, opts) {
229
+ if (!isAbsolute(path))
230
+ return err(`give the key file as an absolute path, not '${path}'`);
231
+ let existing = "";
232
+ if (existsSync(path)) {
233
+ if (!opts.rotate)
234
+ return err(`${path} exists; pass --rotate to add a new current key to it`);
235
+ const read = readOwnerOnly(path);
236
+ if (read.status === "error")
237
+ return read;
238
+ existing = read.value;
239
+ const old = parseKeyring(existing);
240
+ if (old.status === "error")
241
+ return old;
242
+ }
243
+ else if (opts.rotate) {
244
+ return err(`${path} does not exist; leave out --rotate to create it`);
245
+ }
246
+ const text = `${generateKeyLine()}\n${existing}`;
247
+ const parsed = parseKeyring(text);
248
+ if (parsed.status === "error")
249
+ return parsed;
250
+ const dir = dirname(path);
251
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
252
+ chmodSync(dir, 0o700);
253
+ const tmp = `${path}.tmp`;
254
+ rmSync(tmp, { force: true }); // a stale or planted one; a symlink is removed, not followed
255
+ const fd = openSync(tmp, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
256
+ try {
257
+ writeSync(fd, text);
258
+ fsyncSync(fd);
259
+ }
260
+ finally {
261
+ closeSync(fd);
262
+ }
263
+ renameSync(tmp, path);
264
+ const dirFd = openSync(dir, constants.O_RDONLY);
265
+ try {
266
+ fsyncSync(dirFd);
267
+ }
268
+ finally {
269
+ closeSync(dirFd);
270
+ }
271
+ return ok({ kids: parsed.value.keys.map((k) => k.kid) });
272
+ }