@norskvideo/ctl-product-template-schema 0.1.74 → 0.1.76

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.74",
3
+ "version": "0.1.76",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -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,36 @@ 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
+ /** The root of the config, and the profile a process has when none is named. */
28
+ export declare const PRODUCTION_SECRETS_PROFILE = "production";
29
+ /** The profile this process resolves with: `production` unless
30
+ * NORSK_SECRETS_PROFILE names another. A fleet agrees on one, so a worker
31
+ * reports it and norsk-mgr places only on workers reporting its own. */
32
+ export declare function activeSecretsProfile(env: Record<string, string | undefined>): string;
33
+ /** A profile: any top-level key but the root's own. It redirects provider
34
+ * names; `norsk` (held by ctl) and `plain` (a literal) are not providers it
35
+ * can move. */
36
+ export declare const SecretProfileSchema: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodObject<{
37
+ file: z.ZodObject<{
38
+ dir: z.ZodString;
39
+ }, z.core.$strict>;
40
+ }, z.core.$strict>, z.ZodObject<{
41
+ k8s: z.ZodObject<{
42
+ root: z.ZodOptional<z.ZodString>;
43
+ }, z.core.$strict>;
44
+ }, z.core.$strict>, z.ZodObject<{
45
+ aws: z.ZodObject<{
46
+ region: z.ZodOptional<z.ZodString>;
47
+ cacheTtlMs: z.ZodOptional<z.ZodNumber>;
48
+ prefix: z.ZodOptional<z.ZodString>;
49
+ }, z.core.$strict>;
50
+ }, z.core.$strict>, z.ZodObject<{
51
+ exec: z.ZodArray<z.ZodString>;
52
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
53
+ }, z.core.$strict>]>>;
22
54
  export interface LoadSecretProvidersOptions {
23
55
  env: Record<string, string | undefined>;
24
56
  /** A config file. Missing is the same as empty; others being able to write
@@ -29,8 +61,15 @@ export interface LoadSecretProvidersOptions {
29
61
  /** Where `file` reads when the config names no `fileDir`. Without either,
30
62
  * there is no `file` provider. */
31
63
  defaultFileDir?: string;
64
+ /** Where a resolve through a profile's redirect is recorded; stderr by default. */
65
+ log?: (line: string) => void;
32
66
  }
33
- /** The providers to resolve with: `env` always, `file` where a directory is
34
- * known, `k8s` and `aws` when configured, and any exec providers named. Read
35
- * fresh on each call, so an edit needs no restart. */
67
+ /** The providers to resolve with. With no profile: `env` always, `file` where
68
+ * a directory is known, `k8s` and `aws` when configured, and any exec
69
+ * providers named. With a profile: `env` and that profile's redirects, and
70
+ * none of the root's stores, so a name the profile leaves out cannot reach
71
+ * production. Read fresh on each call, so an edit needs no restart. */
36
72
  export declare function loadSecretProviders(opts: LoadSecretProvidersOptions): Result<SecretProviders, string>;
73
+ /** The line a process logs at startup when a profile is in use, naming it and
74
+ * each redirect; nothing in production. */
75
+ export declare function secretsProfileNotice(opts: LoadSecretProvidersOptions): Result<string | undefined, string>;
package/secrets/config.js CHANGED
@@ -6,35 +6,81 @@ 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: z.strictObject({ root: z.string().min(1).optional() }).optional(),
16
- aws: z
17
- .strictObject({
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
- /** The providers to resolve with: `env` always, `file` where a directory is
30
- * known, `k8s` and `aws` when configured, and any exec providers named. Read
31
- * fresh on each call, so an edit needs no restart. */
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
+ /** The root of the config, and the profile a process has when none is named. */
36
+ export const PRODUCTION_SECRETS_PROFILE = "production";
37
+ /** The profile this process resolves with: `production` unless
38
+ * NORSK_SECRETS_PROFILE names another. A fleet agrees on one, so a worker
39
+ * reports it and norsk-mgr places only on workers reporting its own. */
40
+ export function activeSecretsProfile(env) {
41
+ return env[SECRETS_PROFILE_ENV] || PRODUCTION_SECRETS_PROFILE;
42
+ }
43
+ /** What a profile may point a provider name at: one store, configured. */
44
+ const RedirectSchema = z.union([
45
+ z.strictObject({ file: z.strictObject({ dir: z.string().min(1) }) }),
46
+ z.strictObject({ k8s: K8sConfigSchema }),
47
+ z.strictObject({ aws: AwsConfigSchema }),
48
+ ExecConfigSchema,
49
+ ]);
50
+ /** A profile: any top-level key but the root's own. It redirects provider
51
+ * names; `norsk` (held by ctl) and `plain` (a literal) are not providers it
52
+ * can move. */
53
+ export const SecretProfileSchema = z.record(z
54
+ .string()
55
+ .regex(PROVIDER_NAME)
56
+ .refine((name) => name !== "norsk" && name !== "plain", { message: "norsk and plain cannot be redirected" }), RedirectSchema);
57
+ /** The providers to resolve with. With no profile: `env` always, `file` where
58
+ * a directory is known, `k8s` and `aws` when configured, and any exec
59
+ * providers named. With a profile: `env` and that profile's redirects, and
60
+ * none of the root's stores, so a name the profile leaves out cannot reach
61
+ * production. Read fresh on each call, so an edit needs no restart. */
32
62
  export function loadSecretProviders(opts) {
33
63
  const source = readConfig(opts);
34
64
  if (source.status === "error")
35
65
  return source;
36
- const { config, where } = source.value;
66
+ const { config, profiles, where } = source.value;
37
67
  const providers = { env: envSecretProvider(opts.env) };
68
+ const selected = selectProfile(opts.env, profiles, where);
69
+ if (selected.status === "error")
70
+ return selected;
71
+ if (selected.value) {
72
+ const { name, redirects } = selected.value;
73
+ const log = opts.log ?? ((line) => console.warn(line));
74
+ for (const [provider, redirect] of Object.entries(redirects)) {
75
+ const inner = providerFor(redirect);
76
+ const target = describeRedirect(redirect);
77
+ providers[provider] = (key) => {
78
+ log(`secrets profile '${name}': ${provider}:${key} redirected to ${target}`);
79
+ return inner(key);
80
+ };
81
+ }
82
+ return ok(providers);
83
+ }
38
84
  const fileDir = config.fileDir ?? opts.defaultFileDir;
39
85
  if (fileDir)
40
86
  providers.file = fileSecretProvider(fileDir);
@@ -46,10 +92,61 @@ export function loadSecretProviders(opts) {
46
92
  if (RESERVED_PROVIDER_NAMES.includes(name)) {
47
93
  return err(`secret provider config ${where} redefines the built-in provider '${name}'`);
48
94
  }
49
- providers[name] = execSecretProvider({ command: def.exec, ...(def.timeoutMs ? { timeoutMs: def.timeoutMs } : {}) });
95
+ providers[name] = providerFor(def);
50
96
  }
51
97
  return ok(providers);
52
98
  }
99
+ /** The line a process logs at startup when a profile is in use, naming it and
100
+ * each redirect; nothing in production. */
101
+ export function secretsProfileNotice(opts) {
102
+ const source = readConfig(opts);
103
+ if (source.status === "error")
104
+ return source;
105
+ const selected = selectProfile(opts.env, source.value.profiles, source.value.where);
106
+ if (selected.status === "error" || !selected.value)
107
+ return selected.status === "error" ? selected : ok(undefined);
108
+ const redirects = Object.entries(selected.value.redirects)
109
+ .map(([provider, redirect]) => `${provider} -> ${describeRedirect(redirect)}`)
110
+ .join(", ");
111
+ return ok(`secrets profile '${selected.value.name}' is active (config ${source.value.where}): ${redirects || "no redirects"}`);
112
+ }
113
+ function selectProfile(env, profiles, where) {
114
+ const name = activeSecretsProfile(env);
115
+ if (name === PRODUCTION_SECRETS_PROFILE)
116
+ return ok(undefined);
117
+ const redirects = profiles[name];
118
+ if (!redirects) {
119
+ const known = Object.keys(profiles).sort().join(", ") || "none";
120
+ return err(`${SECRETS_PROFILE_ENV} is '${name}', but secret provider config ${where} has no such profile (it has: ${known})`);
121
+ }
122
+ return ok({ name, redirects });
123
+ }
124
+ function providerFor(redirect) {
125
+ if ("file" in redirect)
126
+ return fileSecretProvider(redirect.file.dir);
127
+ if ("k8s" in redirect)
128
+ return k8sSecretProvider(redirect.k8s);
129
+ if ("aws" in redirect)
130
+ return awsSecretProvider(redirect.aws);
131
+ return execSecretProvider({
132
+ command: redirect.exec,
133
+ ...(redirect.timeoutMs ? { timeoutMs: redirect.timeoutMs } : {}),
134
+ });
135
+ }
136
+ function describeRedirect(redirect) {
137
+ if ("file" in redirect)
138
+ return `file (${redirect.file.dir})`;
139
+ if ("k8s" in redirect)
140
+ return `k8s (${redirect.k8s.root ?? DEFAULT_K8S_SECRETS_ROOT})`;
141
+ if ("aws" in redirect) {
142
+ const parts = [
143
+ ...(redirect.aws.region ? [redirect.aws.region] : []),
144
+ ...(redirect.aws.prefix ? [`prefix ${redirect.aws.prefix}`] : []),
145
+ ];
146
+ return parts.length > 0 ? `aws (${parts.join(", ")})` : "aws";
147
+ }
148
+ return `exec (${redirect.exec[0]})`;
149
+ }
53
150
  function readConfig(opts) {
54
151
  let text;
55
152
  let where;
@@ -68,13 +165,33 @@ function readConfig(opts) {
68
165
  text = read.value;
69
166
  }
70
167
  else {
71
- return ok({ config: {}, where: "(none)" });
168
+ return ok({ config: {}, profiles: {}, where: "(none)" });
72
169
  }
73
170
  const raw = tryResult(() => parseYaml(text) ?? {}, (e) => (e instanceof Error ? e.message : String(e)));
74
171
  if (raw.status === "error")
75
172
  return err(`secret provider config ${where} is not valid YAML: ${raw.error}`);
76
- const parsed = SecretProvidersConfigSchema.safeParse(raw.value);
173
+ if (typeof raw.value !== "object" || raw.value === null || Array.isArray(raw.value)) {
174
+ return err(`secret provider config ${where} is invalid: it is not a mapping`);
175
+ }
176
+ const root = {};
177
+ const profiles = {};
178
+ for (const [key, value] of Object.entries(raw.value)) {
179
+ if (ROOT_KEYS.has(key)) {
180
+ root[key] = value;
181
+ continue;
182
+ }
183
+ if (!PROVIDER_NAME.test(key) || key === PRODUCTION_SECRETS_PROFILE) {
184
+ return err(`secret provider config ${where} is invalid: '${key}' is neither a setting (${[...ROOT_KEYS].join(", ")}) ` +
185
+ `nor a profile name (lower case, not '${PRODUCTION_SECRETS_PROFILE}', which is the root)`);
186
+ }
187
+ const profile = SecretProfileSchema.safeParse(value);
188
+ if (!profile.success) {
189
+ return err(`secret provider config ${where} is invalid: profile '${key}': ${z.prettifyError(profile.error)}`);
190
+ }
191
+ profiles[key] = profile.data;
192
+ }
193
+ const parsed = SecretProvidersConfigSchema.safeParse(root);
77
194
  if (!parsed.success)
78
195
  return err(`secret provider config ${where} is invalid: ${z.prettifyError(parsed.error)}`);
79
- return ok({ config: parsed.data, where });
196
+ return ok({ config: parsed.data, profiles, where });
80
197
  }
@@ -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 { 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 { 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
- export declare function createResolverFromEnv(env?: Record<string, string | undefined>): SecretResolver & {
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 { 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 { 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
- export function createResolverFromEnv(env = process.env) {
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. */
@@ -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);