@norskvideo/ctl-product-template-schema 0.1.74 → 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/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.75",
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,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 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. */
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: 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
+ 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] = execSecretProvider({ command: def.exec, ...(def.timeoutMs ? { timeoutMs: def.timeoutMs } : {}) });
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
- const parsed = SecretProvidersConfigSchema.safeParse(raw.value);
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
  }
@@ -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
- 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 { 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
- 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);