@norskvideo/ctl-product-template-schema 0.1.73 → 0.1.75
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/index.d.ts +22 -0
- package/index.js +26 -0
- package/package.json +1 -1
- package/secret-references.d.ts +4 -0
- package/secret-references.js +4 -0
- package/secrets/config.d.ts +36 -3
- package/secrets/config.js +132 -22
- package/secrets/index.d.ts +6 -2
- package/secrets/index.js +12 -7
- package/secrets/providers/aws.d.ts +2 -0
- package/secrets/providers/aws.js +1 -1
package/index.d.ts
CHANGED
|
@@ -103,6 +103,10 @@ export declare const ProductTemplateWorkdirSchema: z.ZodObject<{
|
|
|
103
103
|
services: z.ZodArray<z.ZodString>;
|
|
104
104
|
}, z.core.$strip>;
|
|
105
105
|
export type ProductTemplateWorkdir = z.infer<typeof ProductTemplateWorkdirSchema>;
|
|
106
|
+
export declare const ProductTemplateRuntimeSecretsSchema: z.ZodObject<{
|
|
107
|
+
services: z.ZodArray<z.ZodString>;
|
|
108
|
+
}, z.core.$strip>;
|
|
109
|
+
export type ProductTemplateRuntimeSecrets = z.infer<typeof ProductTemplateRuntimeSecretsSchema>;
|
|
106
110
|
export declare const ProductTemplateAcceleratorSchema: z.ZodEnum<{
|
|
107
111
|
none: "none";
|
|
108
112
|
nvidia: "nvidia";
|
|
@@ -415,6 +419,9 @@ export declare const ProductTemplateManifestSchema: z.ZodObject<{
|
|
|
415
419
|
hostTls: z.ZodOptional<z.ZodObject<{
|
|
416
420
|
services: z.ZodArray<z.ZodString>;
|
|
417
421
|
}, z.core.$strip>>;
|
|
422
|
+
runtimeSecrets: z.ZodOptional<z.ZodObject<{
|
|
423
|
+
services: z.ZodArray<z.ZodString>;
|
|
424
|
+
}, z.core.$strip>>;
|
|
418
425
|
workload: z.ZodOptional<z.ZodObject<{
|
|
419
426
|
role: z.ZodEnum<{
|
|
420
427
|
viewer: "viewer";
|
|
@@ -460,6 +467,10 @@ export declare const ProductTemplateParameterSchema: z.ZodObject<{
|
|
|
460
467
|
}>>;
|
|
461
468
|
default: z.ZodOptional<z.ZodString>;
|
|
462
469
|
secret: z.ZodOptional<z.ZodBoolean>;
|
|
470
|
+
deliver: z.ZodOptional<z.ZodEnum<{
|
|
471
|
+
value: "value";
|
|
472
|
+
reference: "reference";
|
|
473
|
+
}>>;
|
|
463
474
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
464
475
|
min: z.ZodOptional<z.ZodNumber>;
|
|
465
476
|
max: z.ZodOptional<z.ZodNumber>;
|
|
@@ -481,6 +492,10 @@ export declare const ParametersFileSchema: z.ZodObject<{
|
|
|
481
492
|
}>>;
|
|
482
493
|
default: z.ZodOptional<z.ZodString>;
|
|
483
494
|
secret: z.ZodOptional<z.ZodBoolean>;
|
|
495
|
+
deliver: z.ZodOptional<z.ZodEnum<{
|
|
496
|
+
value: "value";
|
|
497
|
+
reference: "reference";
|
|
498
|
+
}>>;
|
|
484
499
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
485
500
|
min: z.ZodOptional<z.ZodNumber>;
|
|
486
501
|
max: z.ZodOptional<z.ZodNumber>;
|
|
@@ -706,6 +721,9 @@ export declare const ProductTemplateMaterialsSchema: z.ZodObject<{
|
|
|
706
721
|
hostTls: z.ZodOptional<z.ZodObject<{
|
|
707
722
|
services: z.ZodArray<z.ZodString>;
|
|
708
723
|
}, z.core.$strip>>;
|
|
724
|
+
runtimeSecrets: z.ZodOptional<z.ZodObject<{
|
|
725
|
+
services: z.ZodArray<z.ZodString>;
|
|
726
|
+
}, z.core.$strip>>;
|
|
709
727
|
workload: z.ZodOptional<z.ZodObject<{
|
|
710
728
|
role: z.ZodEnum<{
|
|
711
729
|
viewer: "viewer";
|
|
@@ -744,6 +762,10 @@ export declare const ProductTemplateMaterialsSchema: z.ZodObject<{
|
|
|
744
762
|
}>>;
|
|
745
763
|
default: z.ZodOptional<z.ZodString>;
|
|
746
764
|
secret: z.ZodOptional<z.ZodBoolean>;
|
|
765
|
+
deliver: z.ZodOptional<z.ZodEnum<{
|
|
766
|
+
value: "value";
|
|
767
|
+
reference: "reference";
|
|
768
|
+
}>>;
|
|
747
769
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
748
770
|
min: z.ZodOptional<z.ZodNumber>;
|
|
749
771
|
max: z.ZodOptional<z.ZodNumber>;
|
package/index.js
CHANGED
|
@@ -342,6 +342,19 @@ export const ProductTemplateWorkdirSchema = z
|
|
|
342
342
|
}),
|
|
343
343
|
})
|
|
344
344
|
.meta({ id: "ProductTemplateWorkdir", outputId: "ProductTemplateWorkdir" });
|
|
345
|
+
// Runtime secret resolution (norsk-ctl RFC 0011 §3.3). WHY THIS EXISTS: a
|
|
346
|
+
// product that learns which secrets it needs only while it runs (a relay given
|
|
347
|
+
// an input's passphrase as a reference) cannot have them resolved into its
|
|
348
|
+
// environment at launch. The named services get NORSK_SECRETS_URL and
|
|
349
|
+
// NORSK_SECRETS_TOKEN, and POST references there. A product relying on it
|
|
350
|
+
// declares the runner capability `secret-references-runtime`.
|
|
351
|
+
export const ProductTemplateRuntimeSecretsSchema = z
|
|
352
|
+
.object({
|
|
353
|
+
services: z.array(z.string().min(1)).min(1).meta({
|
|
354
|
+
description: "Compose services that may resolve secret references at runtime. Each must be a service in the template's own compose.yml. The runner sets NORSK_SECRETS_URL and NORSK_SECRETS_TOKEN in each, for the resolver in `@norskvideo/ctl-product-template-schema/secrets`.",
|
|
355
|
+
}),
|
|
356
|
+
})
|
|
357
|
+
.meta({ id: "ProductTemplateRuntimeSecrets", outputId: "ProductTemplateRuntimeSecrets" });
|
|
345
358
|
// Placement requirements guidance (requirements-authoring.md). Each dimension
|
|
346
359
|
// the placer reasons over — capacity (vCPUs), cores, GPU, capabilities, host
|
|
347
360
|
// ports — is optionally declared with a `mode`:
|
|
@@ -525,6 +538,9 @@ export const ProductTemplateManifestSchema = z
|
|
|
525
538
|
hostTls: ProductTemplateHostTlsSchema.optional().meta({
|
|
526
539
|
description: "Mount the host's TLS certificate and key, read-only, into the named compose services — for a product that serves TLS itself. Copied per launch, so a rotated certificate is picked up on relaunch. Absent = nothing mounted.",
|
|
527
540
|
}),
|
|
541
|
+
runtimeSecrets: ProductTemplateRuntimeSecretsSchema.optional().meta({
|
|
542
|
+
description: "Let the named compose services resolve `secretref:` references at runtime, through the runner, instead of only at launch. Absent = no service can. Declare the runner capability `secret-references-runtime` alongside it.",
|
|
543
|
+
}),
|
|
528
544
|
workload: ProductTemplateWorkloadSchema.optional().meta({
|
|
529
545
|
description: "The role each instance's own workload (`urn:norsk:workload:instance:<id>`) holds on its product (`product/<productName>`), granted when it launches, whoever launches it — for an instance that creates or manages its product's other instances (a capture launching recorders). At most `builder`: registering a template takes builder, and that registration is the consent. Absent = the workload holds nothing it was not passed at launch. An older ctl ignores it, and its instances hold nothing.",
|
|
530
546
|
}),
|
|
@@ -563,6 +579,9 @@ export const ProductTemplateParameterSchema = z
|
|
|
563
579
|
type: z.enum(["string", "int", "number", "boolean", "enum"]).default("string"),
|
|
564
580
|
default: z.string().optional().meta({ description: "Stringified default (even for numeric/boolean/enum types)" }),
|
|
565
581
|
secret: z.boolean().optional(),
|
|
582
|
+
deliver: z.enum(["value", "reference"]).optional().meta({
|
|
583
|
+
description: "How a `secretref:` reference in this parameter reaches the container. `value` (the default): the runner resolves it into the environment. `reference`: the runner checks its syntax and passes it through, for the application to resolve at runtime (`runtimeSecrets`). Only for a `secret` parameter; declare the runner capability `secret-references-runtime` alongside it.",
|
|
584
|
+
}),
|
|
566
585
|
/** Mandatory input. Only bites when there is no `default` to fall back
|
|
567
586
|
* on — the runner/composer blocks launch on an empty required value. */
|
|
568
587
|
required: z.boolean().optional(),
|
|
@@ -581,6 +600,13 @@ export const ProductTemplateParameterSchema = z
|
|
|
581
600
|
}),
|
|
582
601
|
})
|
|
583
602
|
.superRefine((p, ctx) => {
|
|
603
|
+
if (p.deliver === "reference" && p.secret !== true) {
|
|
604
|
+
ctx.addIssue({
|
|
605
|
+
code: "custom",
|
|
606
|
+
path: ["deliver"],
|
|
607
|
+
message: "`deliver: reference` is only for a `secret` parameter",
|
|
608
|
+
});
|
|
609
|
+
}
|
|
584
610
|
if (p.type === "enum" && (!p.values || p.values.length === 0)) {
|
|
585
611
|
ctx.addIssue({ code: "custom", path: ["values"], message: "enum parameters require a non-empty `values` list" });
|
|
586
612
|
}
|
package/package.json
CHANGED
package/secret-references.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ export declare const SECRET_REFERENCE_PREFIX = "secretref:";
|
|
|
4
4
|
* references, so placement can keep a job whose parameters hold one off an
|
|
5
5
|
* older worker — which would hand the container the reference itself. */
|
|
6
6
|
export declare const SECRET_REFERENCES_CAPABILITY = "norsk.io/secret-references";
|
|
7
|
+
/** The worker capability for runtime resolution (RFC 0011 §3.3): the worker
|
|
8
|
+
* answers a resolve request from the services a template's `runtimeSecrets`
|
|
9
|
+
* names, and passes `deliver: reference` parameters through unresolved. */
|
|
10
|
+
export declare const SECRET_REFERENCES_RUNTIME_CAPABILITY = "norsk.io/secret-references-runtime";
|
|
7
11
|
/** What a secret parameter's literal value is shown as wherever ctl prints
|
|
8
12
|
* launch parameters back. */
|
|
9
13
|
export declare const SECRET_PLACEHOLDER = "<secret>";
|
package/secret-references.js
CHANGED
|
@@ -14,6 +14,10 @@ export const SECRET_REFERENCE_PREFIX = "secretref:";
|
|
|
14
14
|
* references, so placement can keep a job whose parameters hold one off an
|
|
15
15
|
* older worker — which would hand the container the reference itself. */
|
|
16
16
|
export const SECRET_REFERENCES_CAPABILITY = "norsk.io/secret-references";
|
|
17
|
+
/** The worker capability for runtime resolution (RFC 0011 §3.3): the worker
|
|
18
|
+
* answers a resolve request from the services a template's `runtimeSecrets`
|
|
19
|
+
* names, and passes `deliver: reference` parameters through unresolved. */
|
|
20
|
+
export const SECRET_REFERENCES_RUNTIME_CAPABILITY = "norsk.io/secret-references-runtime";
|
|
17
21
|
/** What a secret parameter's literal value is shown as wherever ctl prints
|
|
18
22
|
* launch parameters back. */
|
|
19
23
|
export const SECRET_PLACEHOLDER = "<secret>";
|
package/secrets/config.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { SecretProviders } from "./provider.js";
|
|
|
4
4
|
/** Names a configured provider may not take: the built-ins, plus `norsk`
|
|
5
5
|
* (resolved by norsk-ctl) and `plain` (what a literal is). */
|
|
6
6
|
export declare const RESERVED_PROVIDER_NAMES: readonly ["env", "file", "k8s", "aws", "norsk", "plain"];
|
|
7
|
+
/** The production config: the file's root, which never redirects a name. */
|
|
7
8
|
export declare const SecretProvidersConfigSchema: z.ZodObject<{
|
|
8
9
|
fileDir: z.ZodOptional<z.ZodString>;
|
|
9
10
|
k8s: z.ZodOptional<z.ZodObject<{
|
|
@@ -12,6 +13,7 @@ export declare const SecretProvidersConfigSchema: z.ZodObject<{
|
|
|
12
13
|
aws: z.ZodOptional<z.ZodObject<{
|
|
13
14
|
region: z.ZodOptional<z.ZodString>;
|
|
14
15
|
cacheTtlMs: z.ZodOptional<z.ZodNumber>;
|
|
16
|
+
prefix: z.ZodOptional<z.ZodString>;
|
|
15
17
|
}, z.core.$strict>>;
|
|
16
18
|
providers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
17
19
|
exec: z.ZodArray<z.ZodString>;
|
|
@@ -19,6 +21,30 @@ export declare const SecretProvidersConfigSchema: z.ZodObject<{
|
|
|
19
21
|
}, z.core.$strict>>>;
|
|
20
22
|
}, z.core.$strict>;
|
|
21
23
|
export type SecretProvidersConfig = z.infer<typeof SecretProvidersConfigSchema>;
|
|
24
|
+
/** The env variable naming the profile to use. Unset, empty or `production`
|
|
25
|
+
* is the config's root. */
|
|
26
|
+
export declare const SECRETS_PROFILE_ENV = "NORSK_SECRETS_PROFILE";
|
|
27
|
+
/** A profile: any top-level key but the root's own. It redirects provider
|
|
28
|
+
* names; `norsk` (held by ctl) and `plain` (a literal) are not providers it
|
|
29
|
+
* can move. */
|
|
30
|
+
export declare const SecretProfileSchema: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodObject<{
|
|
31
|
+
file: z.ZodObject<{
|
|
32
|
+
dir: z.ZodString;
|
|
33
|
+
}, z.core.$strict>;
|
|
34
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
35
|
+
k8s: z.ZodObject<{
|
|
36
|
+
root: z.ZodOptional<z.ZodString>;
|
|
37
|
+
}, z.core.$strict>;
|
|
38
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
39
|
+
aws: z.ZodObject<{
|
|
40
|
+
region: z.ZodOptional<z.ZodString>;
|
|
41
|
+
cacheTtlMs: z.ZodOptional<z.ZodNumber>;
|
|
42
|
+
prefix: z.ZodOptional<z.ZodString>;
|
|
43
|
+
}, z.core.$strict>;
|
|
44
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
45
|
+
exec: z.ZodArray<z.ZodString>;
|
|
46
|
+
timeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
47
|
+
}, z.core.$strict>]>>;
|
|
22
48
|
export interface LoadSecretProvidersOptions {
|
|
23
49
|
env: Record<string, string | undefined>;
|
|
24
50
|
/** A config file. Missing is the same as empty; others being able to write
|
|
@@ -29,8 +55,15 @@ export interface LoadSecretProvidersOptions {
|
|
|
29
55
|
/** Where `file` reads when the config names no `fileDir`. Without either,
|
|
30
56
|
* there is no `file` provider. */
|
|
31
57
|
defaultFileDir?: string;
|
|
58
|
+
/** Where a resolve through a profile's redirect is recorded; stderr by default. */
|
|
59
|
+
log?: (line: string) => void;
|
|
32
60
|
}
|
|
33
|
-
/** The providers to resolve with: `env` always, `file` where
|
|
34
|
-
* known, `k8s` and `aws` when configured, and any exec
|
|
35
|
-
*
|
|
61
|
+
/** The providers to resolve with. With no profile: `env` always, `file` where
|
|
62
|
+
* a directory is known, `k8s` and `aws` when configured, and any exec
|
|
63
|
+
* providers named. With a profile: `env` and that profile's redirects, and
|
|
64
|
+
* none of the root's stores, so a name the profile leaves out cannot reach
|
|
65
|
+
* production. Read fresh on each call, so an edit needs no restart. */
|
|
36
66
|
export declare function loadSecretProviders(opts: LoadSecretProvidersOptions): Result<SecretProviders, string>;
|
|
67
|
+
/** The line a process logs at startup when a profile is in use, naming it and
|
|
68
|
+
* each redirect; nothing in production. */
|
|
69
|
+
export declare function secretsProfileNotice(opts: LoadSecretProvidersOptions): Result<string | undefined, string>;
|
package/secrets/config.js
CHANGED
|
@@ -6,35 +6,74 @@ import { awsSecretProvider } from "./providers/aws.js";
|
|
|
6
6
|
import { envSecretProvider } from "./providers/env.js";
|
|
7
7
|
import { execSecretProvider } from "./providers/exec.js";
|
|
8
8
|
import { fileSecretProvider } from "./providers/file.js";
|
|
9
|
-
import { k8sSecretProvider } from "./providers/k8s.js";
|
|
9
|
+
import { DEFAULT_K8S_SECRETS_ROOT, k8sSecretProvider } from "./providers/k8s.js";
|
|
10
10
|
/** Names a configured provider may not take: the built-ins, plus `norsk`
|
|
11
11
|
* (resolved by norsk-ctl) and `plain` (what a literal is). */
|
|
12
12
|
export const RESERVED_PROVIDER_NAMES = ["env", "file", "k8s", "aws", "norsk", "plain"];
|
|
13
|
+
const PROVIDER_NAME = /^[a-z][a-z0-9-]*$/;
|
|
14
|
+
const K8sConfigSchema = z.strictObject({ root: z.string().min(1).optional() });
|
|
15
|
+
const AwsConfigSchema = z.strictObject({
|
|
16
|
+
region: z.string().min(1).optional(),
|
|
17
|
+
cacheTtlMs: z.number().int().nonnegative().optional(),
|
|
18
|
+
prefix: z.string().min(1).optional(),
|
|
19
|
+
});
|
|
20
|
+
const ExecConfigSchema = z.strictObject({
|
|
21
|
+
exec: z.array(z.string().min(1)).min(1),
|
|
22
|
+
timeoutMs: z.number().int().positive().optional(),
|
|
23
|
+
});
|
|
24
|
+
/** The production config: the file's root, which never redirects a name. */
|
|
13
25
|
export const SecretProvidersConfigSchema = z.strictObject({
|
|
14
26
|
fileDir: z.string().min(1).optional(),
|
|
15
|
-
k8s:
|
|
16
|
-
aws:
|
|
17
|
-
|
|
18
|
-
region: z.string().min(1).optional(),
|
|
19
|
-
cacheTtlMs: z.number().int().nonnegative().optional(),
|
|
20
|
-
})
|
|
21
|
-
.optional(),
|
|
22
|
-
providers: z
|
|
23
|
-
.record(z.string().regex(/^[a-z][a-z0-9-]*$/), z.strictObject({
|
|
24
|
-
exec: z.array(z.string().min(1)).min(1),
|
|
25
|
-
timeoutMs: z.number().int().positive().optional(),
|
|
26
|
-
}))
|
|
27
|
-
.optional(),
|
|
27
|
+
k8s: K8sConfigSchema.optional(),
|
|
28
|
+
aws: AwsConfigSchema.optional(),
|
|
29
|
+
providers: z.record(z.string().regex(PROVIDER_NAME), ExecConfigSchema).optional(),
|
|
28
30
|
});
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
*
|
|
31
|
+
const ROOT_KEYS = new Set(Object.keys(SecretProvidersConfigSchema.shape));
|
|
32
|
+
/** The env variable naming the profile to use. Unset, empty or `production`
|
|
33
|
+
* is the config's root. */
|
|
34
|
+
export const SECRETS_PROFILE_ENV = "NORSK_SECRETS_PROFILE";
|
|
35
|
+
const PRODUCTION = "production";
|
|
36
|
+
/** What a profile may point a provider name at: one store, configured. */
|
|
37
|
+
const RedirectSchema = z.union([
|
|
38
|
+
z.strictObject({ file: z.strictObject({ dir: z.string().min(1) }) }),
|
|
39
|
+
z.strictObject({ k8s: K8sConfigSchema }),
|
|
40
|
+
z.strictObject({ aws: AwsConfigSchema }),
|
|
41
|
+
ExecConfigSchema,
|
|
42
|
+
]);
|
|
43
|
+
/** A profile: any top-level key but the root's own. It redirects provider
|
|
44
|
+
* names; `norsk` (held by ctl) and `plain` (a literal) are not providers it
|
|
45
|
+
* can move. */
|
|
46
|
+
export const SecretProfileSchema = z.record(z
|
|
47
|
+
.string()
|
|
48
|
+
.regex(PROVIDER_NAME)
|
|
49
|
+
.refine((name) => name !== "norsk" && name !== "plain", { message: "norsk and plain cannot be redirected" }), RedirectSchema);
|
|
50
|
+
/** The providers to resolve with. With no profile: `env` always, `file` where
|
|
51
|
+
* a directory is known, `k8s` and `aws` when configured, and any exec
|
|
52
|
+
* providers named. With a profile: `env` and that profile's redirects, and
|
|
53
|
+
* none of the root's stores, so a name the profile leaves out cannot reach
|
|
54
|
+
* production. Read fresh on each call, so an edit needs no restart. */
|
|
32
55
|
export function loadSecretProviders(opts) {
|
|
33
56
|
const source = readConfig(opts);
|
|
34
57
|
if (source.status === "error")
|
|
35
58
|
return source;
|
|
36
|
-
const { config, where } = source.value;
|
|
59
|
+
const { config, profiles, where } = source.value;
|
|
37
60
|
const providers = { env: envSecretProvider(opts.env) };
|
|
61
|
+
const selected = selectProfile(opts.env, profiles, where);
|
|
62
|
+
if (selected.status === "error")
|
|
63
|
+
return selected;
|
|
64
|
+
if (selected.value) {
|
|
65
|
+
const { name, redirects } = selected.value;
|
|
66
|
+
const log = opts.log ?? ((line) => console.warn(line));
|
|
67
|
+
for (const [provider, redirect] of Object.entries(redirects)) {
|
|
68
|
+
const inner = providerFor(redirect);
|
|
69
|
+
const target = describeRedirect(redirect);
|
|
70
|
+
providers[provider] = (key) => {
|
|
71
|
+
log(`secrets profile '${name}': ${provider}:${key} redirected to ${target}`);
|
|
72
|
+
return inner(key);
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return ok(providers);
|
|
76
|
+
}
|
|
38
77
|
const fileDir = config.fileDir ?? opts.defaultFileDir;
|
|
39
78
|
if (fileDir)
|
|
40
79
|
providers.file = fileSecretProvider(fileDir);
|
|
@@ -46,10 +85,61 @@ export function loadSecretProviders(opts) {
|
|
|
46
85
|
if (RESERVED_PROVIDER_NAMES.includes(name)) {
|
|
47
86
|
return err(`secret provider config ${where} redefines the built-in provider '${name}'`);
|
|
48
87
|
}
|
|
49
|
-
providers[name] =
|
|
88
|
+
providers[name] = providerFor(def);
|
|
50
89
|
}
|
|
51
90
|
return ok(providers);
|
|
52
91
|
}
|
|
92
|
+
/** The line a process logs at startup when a profile is in use, naming it and
|
|
93
|
+
* each redirect; nothing in production. */
|
|
94
|
+
export function secretsProfileNotice(opts) {
|
|
95
|
+
const source = readConfig(opts);
|
|
96
|
+
if (source.status === "error")
|
|
97
|
+
return source;
|
|
98
|
+
const selected = selectProfile(opts.env, source.value.profiles, source.value.where);
|
|
99
|
+
if (selected.status === "error" || !selected.value)
|
|
100
|
+
return selected.status === "error" ? selected : ok(undefined);
|
|
101
|
+
const redirects = Object.entries(selected.value.redirects)
|
|
102
|
+
.map(([provider, redirect]) => `${provider} -> ${describeRedirect(redirect)}`)
|
|
103
|
+
.join(", ");
|
|
104
|
+
return ok(`secrets profile '${selected.value.name}' is active (config ${source.value.where}): ${redirects || "no redirects"}`);
|
|
105
|
+
}
|
|
106
|
+
function selectProfile(env, profiles, where) {
|
|
107
|
+
const name = env[SECRETS_PROFILE_ENV];
|
|
108
|
+
if (!name || name === PRODUCTION)
|
|
109
|
+
return ok(undefined);
|
|
110
|
+
const redirects = profiles[name];
|
|
111
|
+
if (!redirects) {
|
|
112
|
+
const known = Object.keys(profiles).sort().join(", ") || "none";
|
|
113
|
+
return err(`${SECRETS_PROFILE_ENV} is '${name}', but secret provider config ${where} has no such profile (it has: ${known})`);
|
|
114
|
+
}
|
|
115
|
+
return ok({ name, redirects });
|
|
116
|
+
}
|
|
117
|
+
function providerFor(redirect) {
|
|
118
|
+
if ("file" in redirect)
|
|
119
|
+
return fileSecretProvider(redirect.file.dir);
|
|
120
|
+
if ("k8s" in redirect)
|
|
121
|
+
return k8sSecretProvider(redirect.k8s);
|
|
122
|
+
if ("aws" in redirect)
|
|
123
|
+
return awsSecretProvider(redirect.aws);
|
|
124
|
+
return execSecretProvider({
|
|
125
|
+
command: redirect.exec,
|
|
126
|
+
...(redirect.timeoutMs ? { timeoutMs: redirect.timeoutMs } : {}),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function describeRedirect(redirect) {
|
|
130
|
+
if ("file" in redirect)
|
|
131
|
+
return `file (${redirect.file.dir})`;
|
|
132
|
+
if ("k8s" in redirect)
|
|
133
|
+
return `k8s (${redirect.k8s.root ?? DEFAULT_K8S_SECRETS_ROOT})`;
|
|
134
|
+
if ("aws" in redirect) {
|
|
135
|
+
const parts = [
|
|
136
|
+
...(redirect.aws.region ? [redirect.aws.region] : []),
|
|
137
|
+
...(redirect.aws.prefix ? [`prefix ${redirect.aws.prefix}`] : []),
|
|
138
|
+
];
|
|
139
|
+
return parts.length > 0 ? `aws (${parts.join(", ")})` : "aws";
|
|
140
|
+
}
|
|
141
|
+
return `exec (${redirect.exec[0]})`;
|
|
142
|
+
}
|
|
53
143
|
function readConfig(opts) {
|
|
54
144
|
let text;
|
|
55
145
|
let where;
|
|
@@ -68,13 +158,33 @@ function readConfig(opts) {
|
|
|
68
158
|
text = read.value;
|
|
69
159
|
}
|
|
70
160
|
else {
|
|
71
|
-
return ok({ config: {}, where: "(none)" });
|
|
161
|
+
return ok({ config: {}, profiles: {}, where: "(none)" });
|
|
72
162
|
}
|
|
73
163
|
const raw = tryResult(() => parseYaml(text) ?? {}, (e) => (e instanceof Error ? e.message : String(e)));
|
|
74
164
|
if (raw.status === "error")
|
|
75
165
|
return err(`secret provider config ${where} is not valid YAML: ${raw.error}`);
|
|
76
|
-
|
|
166
|
+
if (typeof raw.value !== "object" || raw.value === null || Array.isArray(raw.value)) {
|
|
167
|
+
return err(`secret provider config ${where} is invalid: it is not a mapping`);
|
|
168
|
+
}
|
|
169
|
+
const root = {};
|
|
170
|
+
const profiles = {};
|
|
171
|
+
for (const [key, value] of Object.entries(raw.value)) {
|
|
172
|
+
if (ROOT_KEYS.has(key)) {
|
|
173
|
+
root[key] = value;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (!PROVIDER_NAME.test(key) || key === PRODUCTION) {
|
|
177
|
+
return err(`secret provider config ${where} is invalid: '${key}' is neither a setting (${[...ROOT_KEYS].join(", ")}) ` +
|
|
178
|
+
`nor a profile name (lower case, not '${PRODUCTION}', which is the root)`);
|
|
179
|
+
}
|
|
180
|
+
const profile = SecretProfileSchema.safeParse(value);
|
|
181
|
+
if (!profile.success) {
|
|
182
|
+
return err(`secret provider config ${where} is invalid: profile '${key}': ${z.prettifyError(profile.error)}`);
|
|
183
|
+
}
|
|
184
|
+
profiles[key] = profile.data;
|
|
185
|
+
}
|
|
186
|
+
const parsed = SecretProvidersConfigSchema.safeParse(root);
|
|
77
187
|
if (!parsed.success)
|
|
78
188
|
return err(`secret provider config ${where} is invalid: ${z.prettifyError(parsed.error)}`);
|
|
79
|
-
return ok({ config: parsed.data, where });
|
|
189
|
+
return ok({ config: parsed.data, profiles, where });
|
|
80
190
|
}
|
package/secrets/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type SecretResolver } from "./resolver.js";
|
|
2
2
|
export { isSecretReference, parseSecretReference, SECRET_PLACEHOLDER, SECRET_REFERENCE_PREFIX, type SecretReference, secretReference, } from "../secret-references.js";
|
|
3
|
-
export { type LoadSecretProvidersOptions, loadSecretProviders, RESERVED_PROVIDER_NAMES, type SecretProvidersConfig, SecretProvidersConfigSchema, } from "./config.js";
|
|
3
|
+
export { type LoadSecretProvidersOptions, loadSecretProviders, 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 { deobfuscate, isObfuscated, OBFUSCATED_PREFIX, obfuscate } from "./obfuscation.js";
|
|
6
6
|
export type { SecretProvider, SecretProviders, SecretResolutionError } from "./provider.js";
|
|
@@ -13,6 +13,10 @@ export { localResolver, type ResolveContext, resolveSecretReferences, type Secre
|
|
|
13
13
|
export { Secret } from "./secret.js";
|
|
14
14
|
export * from "./wire.js";
|
|
15
15
|
export declare const SECRET_PROVIDERS_ENV = "NORSK_SECRET_PROVIDERS";
|
|
16
|
-
|
|
16
|
+
/** In local mode, a profile in use (NORSK_SECRETS_PROFILE) is announced once
|
|
17
|
+
* here and each redirected resolve again, through `log` (stderr by default). */
|
|
18
|
+
export declare function createResolverFromEnv(env?: Record<string, string | undefined>, opts?: {
|
|
19
|
+
log?: (line: string) => void;
|
|
20
|
+
}): SecretResolver & {
|
|
17
21
|
mode: "delegate" | "local";
|
|
18
22
|
};
|
package/secrets/index.js
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
// and is resolved with the providers on its host. Anywhere else (dev, a
|
|
6
6
|
// customer's Kubernetes) the providers come from NORSK_SECRET_PROVIDERS.
|
|
7
7
|
import { err, ok } from "@norskvideo/ctl-foundation";
|
|
8
|
-
import { loadSecretProviders } from "./config.js";
|
|
8
|
+
import { loadSecretProviders, secretsProfileNotice } from "./config.js";
|
|
9
9
|
import { delegateResolver } from "./delegate.js";
|
|
10
10
|
import { localResolver } from "./resolver.js";
|
|
11
11
|
import { SECRETS_TOKEN_ENV, SECRETS_URL_ENV } from "./wire.js";
|
|
12
12
|
export { isSecretReference, parseSecretReference, SECRET_PLACEHOLDER, SECRET_REFERENCE_PREFIX, secretReference, } from "../secret-references.js";
|
|
13
|
-
export { loadSecretProviders, RESERVED_PROVIDER_NAMES, SecretProvidersConfigSchema, } from "./config.js";
|
|
13
|
+
export { loadSecretProviders, RESERVED_PROVIDER_NAMES, SECRETS_PROFILE_ENV, SecretProfileSchema, SecretProvidersConfigSchema, secretsProfileNotice, } from "./config.js";
|
|
14
14
|
export { delegateResolver } from "./delegate.js";
|
|
15
15
|
export { deobfuscate, isObfuscated, OBFUSCATED_PREFIX, obfuscate } from "./obfuscation.js";
|
|
16
16
|
export { awsSecretProvider } from "./providers/aws.js";
|
|
@@ -27,17 +27,22 @@ const noNorskHere = async () => err("norsk: references are resolved by norsk-ctl
|
|
|
27
27
|
/** NORSK_SECRET_PROVIDERS is inline config when it looks like YAML or JSON
|
|
28
28
|
* rather than a path: it starts with '{' or holds a newline. */
|
|
29
29
|
const isInline = (v) => v.trimStart().startsWith("{") || v.includes("\n");
|
|
30
|
-
|
|
30
|
+
/** In local mode, a profile in use (NORSK_SECRETS_PROFILE) is announced once
|
|
31
|
+
* here and each redirected resolve again, through `log` (stderr by default). */
|
|
32
|
+
export function createResolverFromEnv(env = process.env, opts = {}) {
|
|
31
33
|
const url = env[SECRETS_URL_ENV];
|
|
32
34
|
const token = env[SECRETS_TOKEN_ENV];
|
|
33
35
|
if (url && token)
|
|
34
36
|
return { mode: "delegate", ...delegateResolver({ url, token }) };
|
|
35
37
|
const configured = env[SECRET_PROVIDERS_ENV];
|
|
38
|
+
const log = opts.log ?? ((line) => console.warn(line));
|
|
39
|
+
const source = configured ? (isInline(configured) ? { inline: configured } : { path: configured }) : {};
|
|
40
|
+
// A config that cannot be read is reported by the first resolve that needs it.
|
|
41
|
+
const notice = secretsProfileNotice({ env, ...source });
|
|
42
|
+
if (notice.status === "ok" && notice.value)
|
|
43
|
+
log(notice.value);
|
|
36
44
|
const resolver = localResolver(() => {
|
|
37
|
-
const loaded = loadSecretProviders({
|
|
38
|
-
env,
|
|
39
|
-
...(configured ? (isInline(configured) ? { inline: configured } : { path: configured }) : {}),
|
|
40
|
-
});
|
|
45
|
+
const loaded = loadSecretProviders({ env, ...source, log });
|
|
41
46
|
return loaded.status === "ok" ? ok({ norsk: noNorskHere, ...loaded.value }) : loaded;
|
|
42
47
|
});
|
|
43
48
|
return { mode: "local", ...resolver };
|
|
@@ -5,6 +5,8 @@ export interface AwsSecretProviderOptions {
|
|
|
5
5
|
region?: string;
|
|
6
6
|
/** How long a fetched secret is reused; 0 fetches on every resolution. */
|
|
7
7
|
cacheTtlMs?: number;
|
|
8
|
+
/** Put in front of every secret name (not an ARN): `qa/` makes `yt` read `qa/yt`. */
|
|
9
|
+
prefix?: string;
|
|
8
10
|
/** Replaces the SDK call, for tests. */
|
|
9
11
|
getSecretString?: GetSecretString;
|
|
10
12
|
/** Replaces loading the SDK, for tests. */
|
package/secrets/providers/aws.js
CHANGED
|
@@ -39,7 +39,7 @@ export function awsSecretProvider(opts = {}) {
|
|
|
39
39
|
const field = hash < 0 ? undefined : key.slice(hash + 1);
|
|
40
40
|
if (secretId === "" || field === "")
|
|
41
41
|
return err(`'${key}' is not <secret-id>[#<field>]`);
|
|
42
|
-
const got = await fetchSecret(secretId);
|
|
42
|
+
const got = await fetchSecret(opts.prefix && !secretId.startsWith("arn:") ? opts.prefix + secretId : secretId);
|
|
43
43
|
if (got.status === "error" || field === undefined)
|
|
44
44
|
return got;
|
|
45
45
|
return pickField(secretId, field, got.value);
|