@norskvideo/ctl-product-template-schema 0.1.88 → 0.1.90
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 +5 -0
- package/index.js +75 -0
- package/package.json +1 -1
- package/params.d.ts +54 -0
- package/params.js +137 -0
- package/secret-references.d.ts +22 -5
- package/secret-references.js +95 -6
package/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { type JsonParamSchema } from "./params.js";
|
|
2
3
|
import { LISTENER_KINDS } from "./port-bindings.js";
|
|
3
4
|
export declare const ProductTemplateTargetSchema: z.ZodEnum<{
|
|
4
5
|
"docker-compose": "docker-compose";
|
|
@@ -498,6 +499,7 @@ export declare const STANDARD_ADVANCED_OVERRIDES: {
|
|
|
498
499
|
readonly containerUser: {};
|
|
499
500
|
readonly publicHost: {};
|
|
500
501
|
};
|
|
502
|
+
export declare const JsonParamSchemaSchema: z.ZodType<JsonParamSchema>;
|
|
501
503
|
export declare const ProductTemplateParameterSchema: z.ZodObject<{
|
|
502
504
|
name: z.ZodString;
|
|
503
505
|
purpose: z.ZodString;
|
|
@@ -518,6 +520,7 @@ export declare const ProductTemplateParameterSchema: z.ZodObject<{
|
|
|
518
520
|
min: z.ZodOptional<z.ZodNumber>;
|
|
519
521
|
max: z.ZodOptional<z.ZodNumber>;
|
|
520
522
|
values: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
523
|
+
schema: z.ZodOptional<z.ZodType<JsonParamSchema, unknown, z.core.$ZodTypeInternals<JsonParamSchema, unknown>>>;
|
|
521
524
|
label: z.ZodOptional<z.ZodString>;
|
|
522
525
|
group: z.ZodOptional<z.ZodString>;
|
|
523
526
|
}, z.core.$strip>;
|
|
@@ -543,6 +546,7 @@ export declare const ParametersFileSchema: z.ZodObject<{
|
|
|
543
546
|
min: z.ZodOptional<z.ZodNumber>;
|
|
544
547
|
max: z.ZodOptional<z.ZodNumber>;
|
|
545
548
|
values: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
549
|
+
schema: z.ZodOptional<z.ZodType<JsonParamSchema, unknown, z.core.$ZodTypeInternals<JsonParamSchema, unknown>>>;
|
|
546
550
|
label: z.ZodOptional<z.ZodString>;
|
|
547
551
|
group: z.ZodOptional<z.ZodString>;
|
|
548
552
|
}, z.core.$strip>>>;
|
|
@@ -829,6 +833,7 @@ export declare const ProductTemplateMaterialsSchema: z.ZodObject<{
|
|
|
829
833
|
min: z.ZodOptional<z.ZodNumber>;
|
|
830
834
|
max: z.ZodOptional<z.ZodNumber>;
|
|
831
835
|
values: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
836
|
+
schema: z.ZodOptional<z.ZodType<JsonParamSchema, unknown, z.core.$ZodTypeInternals<JsonParamSchema, unknown>>>;
|
|
832
837
|
label: z.ZodOptional<z.ZodString>;
|
|
833
838
|
group: z.ZodOptional<z.ZodString>;
|
|
834
839
|
}, z.core.$strip>>>;
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { ProductTemplateInterfaceSchema } from "./interfaces.js";
|
|
3
3
|
import { ProductTemplateLogsSchema } from "./logs.js";
|
|
4
|
+
import { validateParamValue } from "./params.js";
|
|
4
5
|
import { LISTENER_ID_PATTERN, LISTENER_KINDS } from "./port-bindings.js";
|
|
5
6
|
// ProductTemplate format v1. A product template is a tar file at the wire; the schemas
|
|
6
7
|
// below describe its parts before packing and how the runner validates
|
|
@@ -602,6 +603,62 @@ export const STANDARD_ADVANCED_OVERRIDES = {
|
|
|
602
603
|
containerUser: {},
|
|
603
604
|
publicHost: {},
|
|
604
605
|
};
|
|
606
|
+
// The shape of a structured parameter's value: a small subset of JSON Schema
|
|
607
|
+
// (params.ts `JsonParamSchema`, which every form and the runner validate
|
|
608
|
+
// against). `secret` on a string is ours: the field is masked in reads,
|
|
609
|
+
// rendered as a password input, and may hold a `secretref:`.
|
|
610
|
+
const jsonNodeBase = {
|
|
611
|
+
title: z.string().min(1).optional().meta({ description: "A form's label for the field" }),
|
|
612
|
+
description: z.string().optional().meta({ description: "A form's help text for the field" }),
|
|
613
|
+
};
|
|
614
|
+
export const JsonParamSchemaSchema = z
|
|
615
|
+
.lazy(() => z.discriminatedUnion("type", [
|
|
616
|
+
z
|
|
617
|
+
.object({
|
|
618
|
+
...jsonNodeBase,
|
|
619
|
+
type: z.literal("object"),
|
|
620
|
+
properties: z.record(z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/), JsonParamSchemaSchema),
|
|
621
|
+
required: z.array(z.string()).optional(),
|
|
622
|
+
})
|
|
623
|
+
.strict()
|
|
624
|
+
.superRefine((o, ctx) => {
|
|
625
|
+
for (const name of o.required ?? []) {
|
|
626
|
+
if (!(name in o.properties))
|
|
627
|
+
ctx.addIssue({
|
|
628
|
+
code: "custom",
|
|
629
|
+
path: ["required"],
|
|
630
|
+
message: `required names ${name}, which is not a property`,
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
}),
|
|
634
|
+
z
|
|
635
|
+
.object({
|
|
636
|
+
...jsonNodeBase,
|
|
637
|
+
type: z.literal("array"),
|
|
638
|
+
items: JsonParamSchemaSchema,
|
|
639
|
+
minItems: z.number().int().min(0).optional(),
|
|
640
|
+
maxItems: z.number().int().min(0).optional(),
|
|
641
|
+
})
|
|
642
|
+
.strict(),
|
|
643
|
+
z
|
|
644
|
+
.object({
|
|
645
|
+
...jsonNodeBase,
|
|
646
|
+
type: z.literal("string"),
|
|
647
|
+
enum: z.array(z.string()).min(1).optional(),
|
|
648
|
+
secret: z.boolean().optional(),
|
|
649
|
+
})
|
|
650
|
+
.strict(),
|
|
651
|
+
z
|
|
652
|
+
.object({
|
|
653
|
+
...jsonNodeBase,
|
|
654
|
+
type: z.enum(["number", "integer"]),
|
|
655
|
+
minimum: z.number().optional(),
|
|
656
|
+
maximum: z.number().optional(),
|
|
657
|
+
})
|
|
658
|
+
.strict(),
|
|
659
|
+
z.object({ ...jsonNodeBase, type: z.literal("boolean") }).strict(),
|
|
660
|
+
]))
|
|
661
|
+
.meta({ id: "JsonParamSchema", outputId: "JsonParamSchema" });
|
|
605
662
|
export const ProductTemplateParameterSchema = z
|
|
606
663
|
.object({
|
|
607
664
|
name: z.string().regex(/^[A-Z][A-Z0-9_]*$/, "env var name; uppercase, digits, underscores"),
|
|
@@ -620,6 +677,9 @@ export const ProductTemplateParameterSchema = z
|
|
|
620
677
|
max: z.number().optional(),
|
|
621
678
|
/** Allowed values for `enum` parameters (rendered as a dropdown). */
|
|
622
679
|
values: z.array(z.string()).optional(),
|
|
680
|
+
schema: JsonParamSchemaSchema.optional().meta({
|
|
681
|
+
description: "For a `string` parameter whose value is JSON: its shape (a JSON Schema subset), which a form renders and every surface validates. The value is still one environment variable holding the JSON text. Additive: a runner that predates it ignores the key and takes the text as it is.",
|
|
682
|
+
}),
|
|
623
683
|
// Display metadata for a form built from the template. Declaration order is
|
|
624
684
|
// display order, so there is no `order` field to disagree with it.
|
|
625
685
|
label: z.string().min(1).optional().meta({
|
|
@@ -649,6 +709,21 @@ export const ProductTemplateParameterSchema = z
|
|
|
649
709
|
if ((p.min !== undefined || p.max !== undefined) && p.type !== "int" && p.type !== "number") {
|
|
650
710
|
ctx.addIssue({ code: "custom", path: ["min"], message: "`min`/`max` are only valid for int/number parameters" });
|
|
651
711
|
}
|
|
712
|
+
if (p.schema !== undefined) {
|
|
713
|
+
if (p.type !== "string")
|
|
714
|
+
ctx.addIssue({ code: "custom", path: ["schema"], message: "`schema` is only valid for string parameters" });
|
|
715
|
+
if (p.secret === true)
|
|
716
|
+
ctx.addIssue({
|
|
717
|
+
code: "custom",
|
|
718
|
+
path: ["secret"],
|
|
719
|
+
message: "a parameter with a `schema` is not secret as a whole: mark its secret fields `secret` in the schema",
|
|
720
|
+
});
|
|
721
|
+
if (p.default !== undefined && p.default !== "") {
|
|
722
|
+
const err = validateParamValue(p, p.default);
|
|
723
|
+
if (err)
|
|
724
|
+
ctx.addIssue({ code: "custom", path: ["default"], message: `default: ${err}` });
|
|
725
|
+
}
|
|
726
|
+
}
|
|
652
727
|
})
|
|
653
728
|
.meta({ id: "ProductTemplateParameter", outputId: "ProductTemplateParameter" });
|
|
654
729
|
// Wire shape of the optional parameters.yaml file inside a product-template tar.
|
package/package.json
CHANGED
package/params.d.ts
CHANGED
|
@@ -9,7 +9,38 @@ export type ParamSpec = {
|
|
|
9
9
|
min?: number;
|
|
10
10
|
max?: number;
|
|
11
11
|
values?: string[];
|
|
12
|
+
/** For a string parameter whose value is JSON: its shape. */
|
|
13
|
+
schema?: JsonParamSchema;
|
|
12
14
|
};
|
|
15
|
+
/** The shape of a structured parameter's value (a `string` parameter with a
|
|
16
|
+
* `schema`, whose text is JSON): a small subset of JSON Schema.
|
|
17
|
+
* `secret` (on a string) is ours: the field is masked in reads, rendered as
|
|
18
|
+
* a password input, and may hold a `secretref:`. Every node may carry a
|
|
19
|
+
* `title` (a form's label) and a `description` (its help text). */
|
|
20
|
+
export type JsonParamSchema = (JsonParamNodeBase & {
|
|
21
|
+
type: "object";
|
|
22
|
+
properties: Record<string, JsonParamSchema>;
|
|
23
|
+
required?: string[];
|
|
24
|
+
}) | (JsonParamNodeBase & {
|
|
25
|
+
type: "array";
|
|
26
|
+
items: JsonParamSchema;
|
|
27
|
+
minItems?: number;
|
|
28
|
+
maxItems?: number;
|
|
29
|
+
}) | (JsonParamNodeBase & {
|
|
30
|
+
type: "string";
|
|
31
|
+
enum?: string[];
|
|
32
|
+
secret?: boolean;
|
|
33
|
+
}) | (JsonParamNodeBase & {
|
|
34
|
+
type: "number" | "integer";
|
|
35
|
+
minimum?: number;
|
|
36
|
+
maximum?: number;
|
|
37
|
+
}) | (JsonParamNodeBase & {
|
|
38
|
+
type: "boolean";
|
|
39
|
+
});
|
|
40
|
+
interface JsonParamNodeBase {
|
|
41
|
+
title?: string;
|
|
42
|
+
description?: string;
|
|
43
|
+
}
|
|
13
44
|
/** A required parameter only blocks launch when it has no `default` to fall
|
|
14
45
|
* back on — supplying a default is itself a way of satisfying it. */
|
|
15
46
|
export declare function isParamRequired(p: Pick<ProductTemplateParameter, "required" | "default">): boolean;
|
|
@@ -18,3 +49,26 @@ export declare function isParamRequired(p: Pick<ProductTemplateParameter, "requi
|
|
|
18
49
|
* empty value for a non-required parameter (the default / runner fills it).
|
|
19
50
|
* Boolean/number/enum values arrive as strings from form inputs. */
|
|
20
51
|
export declare function validateParamValue(p: ParamSpec, raw: string): string | undefined;
|
|
52
|
+
/** A structured parameter's text as a value, or why it is not JSON. */
|
|
53
|
+
export declare function parseJsonParam(raw: string): {
|
|
54
|
+
ok: true;
|
|
55
|
+
value: unknown;
|
|
56
|
+
} | {
|
|
57
|
+
ok: false;
|
|
58
|
+
error: string;
|
|
59
|
+
};
|
|
60
|
+
/** Check a value against a structured parameter's schema. Answers the first
|
|
61
|
+
* problem, naming where it is (`servers[1].url is required`), or undefined.
|
|
62
|
+
* A `secret` string field may hold a `secretref:`; any string does. */
|
|
63
|
+
export declare function validateJsonParamValue(schema: JsonParamSchema, value: unknown, path?: string): string | undefined;
|
|
64
|
+
/** Rewrite every string the schema marks `secret` (`fn` answers the new
|
|
65
|
+
* string), leaving the rest of the value as it is. A value that does not
|
|
66
|
+
* fit the schema is walked as far as it does. */
|
|
67
|
+
export declare function mapSecretFields(schema: JsonParamSchema, value: unknown, fn: (v: string, path: string) => string, path?: string): unknown;
|
|
68
|
+
/** Whether the schema marks any field `secret`. */
|
|
69
|
+
export declare function hasSecretFields(schema: JsonParamSchema): boolean;
|
|
70
|
+
/** Every string anywhere in a JSON value, rewritten by `fn` (the runner uses
|
|
71
|
+
* it to resolve the `secretref:`s inside a structured parameter, which are
|
|
72
|
+
* resolved wherever they sit, as a whole value that is one is). */
|
|
73
|
+
export declare function mapJsonStrings(value: unknown, fn: (v: string) => string): unknown;
|
|
74
|
+
export {};
|
package/params.js
CHANGED
|
@@ -16,6 +16,16 @@ export function validateParamValue(p, raw) {
|
|
|
16
16
|
const v = raw.trim();
|
|
17
17
|
if (v === "")
|
|
18
18
|
return isParamRequired(p) ? "required" : undefined;
|
|
19
|
+
if (p.schema) {
|
|
20
|
+
// A structured parameter (a string whose value is JSON of this shape). A
|
|
21
|
+
// whole-value secretref: is the secret store's JSON, checked at launch.
|
|
22
|
+
if (v.startsWith("secretref:"))
|
|
23
|
+
return undefined;
|
|
24
|
+
const parsed = parseJsonParam(v);
|
|
25
|
+
if (!parsed.ok)
|
|
26
|
+
return parsed.error;
|
|
27
|
+
return validateJsonParamValue(p.schema, parsed.value);
|
|
28
|
+
}
|
|
19
29
|
switch (p.type) {
|
|
20
30
|
case "int":
|
|
21
31
|
if (!/^-?\d+$/.test(v))
|
|
@@ -33,6 +43,133 @@ export function validateParamValue(p, raw) {
|
|
|
33
43
|
return undefined; // string — anything goes
|
|
34
44
|
}
|
|
35
45
|
}
|
|
46
|
+
/** A structured parameter's text as a value, or why it is not JSON. */
|
|
47
|
+
export function parseJsonParam(raw) {
|
|
48
|
+
try {
|
|
49
|
+
return { ok: true, value: JSON.parse(raw) };
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
return { ok: false, error: `must be JSON: ${e instanceof Error ? e.message : String(e)}` };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Where in a value a problem is: `servers[1].url`; the root is `value`. */
|
|
56
|
+
function at(path) {
|
|
57
|
+
return path === "" ? "value" : path;
|
|
58
|
+
}
|
|
59
|
+
/** Check a value against a structured parameter's schema. Answers the first
|
|
60
|
+
* problem, naming where it is (`servers[1].url is required`), or undefined.
|
|
61
|
+
* A `secret` string field may hold a `secretref:`; any string does. */
|
|
62
|
+
export function validateJsonParamValue(schema, value, path = "") {
|
|
63
|
+
switch (schema.type) {
|
|
64
|
+
case "object": {
|
|
65
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
66
|
+
return `${at(path)} must be an object`;
|
|
67
|
+
const obj = value;
|
|
68
|
+
for (const name of schema.required ?? []) {
|
|
69
|
+
const v = obj[name];
|
|
70
|
+
if (v === undefined || v === null || v === "")
|
|
71
|
+
return `${join(path, name)} is required`;
|
|
72
|
+
}
|
|
73
|
+
for (const [name, v] of Object.entries(obj)) {
|
|
74
|
+
const prop = schema.properties[name];
|
|
75
|
+
if (!prop)
|
|
76
|
+
return `${join(path, name)} is not a field this parameter takes`;
|
|
77
|
+
if (v === undefined || v === null)
|
|
78
|
+
continue;
|
|
79
|
+
const err = validateJsonParamValue(prop, v, join(path, name));
|
|
80
|
+
if (err)
|
|
81
|
+
return err;
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
case "array": {
|
|
86
|
+
if (!Array.isArray(value))
|
|
87
|
+
return `${at(path)} must be a list`;
|
|
88
|
+
if (schema.minItems !== undefined && value.length < schema.minItems)
|
|
89
|
+
return `${at(path)} needs at least ${schema.minItems} item${schema.minItems === 1 ? "" : "s"}`;
|
|
90
|
+
if (schema.maxItems !== undefined && value.length > schema.maxItems)
|
|
91
|
+
return `${at(path)} takes at most ${schema.maxItems} item${schema.maxItems === 1 ? "" : "s"}`;
|
|
92
|
+
for (const [i, item] of value.entries()) {
|
|
93
|
+
const err = validateJsonParamValue(schema.items, item, `${path}[${i}]`);
|
|
94
|
+
if (err)
|
|
95
|
+
return err;
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
case "string":
|
|
100
|
+
if (typeof value !== "string")
|
|
101
|
+
return `${at(path)} must be text`;
|
|
102
|
+
if (schema.enum && !schema.enum.includes(value))
|
|
103
|
+
return `${at(path)} must be one of: ${schema.enum.join(", ")}`;
|
|
104
|
+
return undefined;
|
|
105
|
+
case "integer":
|
|
106
|
+
case "number": {
|
|
107
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
108
|
+
return `${at(path)} must be a number`;
|
|
109
|
+
if (schema.type === "integer" && !Number.isInteger(value))
|
|
110
|
+
return `${at(path)} must be a whole number`;
|
|
111
|
+
if (schema.minimum !== undefined && value < schema.minimum)
|
|
112
|
+
return `${at(path)} must be >= ${schema.minimum}`;
|
|
113
|
+
if (schema.maximum !== undefined && value > schema.maximum)
|
|
114
|
+
return `${at(path)} must be <= ${schema.maximum}`;
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
case "boolean":
|
|
118
|
+
return typeof value === "boolean" ? undefined : `${at(path)} must be true or false`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function join(path, name) {
|
|
122
|
+
return path === "" ? name : `${path}.${name}`;
|
|
123
|
+
}
|
|
124
|
+
/** Rewrite every string the schema marks `secret` (`fn` answers the new
|
|
125
|
+
* string), leaving the rest of the value as it is. A value that does not
|
|
126
|
+
* fit the schema is walked as far as it does. */
|
|
127
|
+
export function mapSecretFields(schema, value, fn, path = "") {
|
|
128
|
+
switch (schema.type) {
|
|
129
|
+
case "object": {
|
|
130
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
131
|
+
return value;
|
|
132
|
+
const out = {};
|
|
133
|
+
for (const [name, v] of Object.entries(value)) {
|
|
134
|
+
const prop = schema.properties[name];
|
|
135
|
+
out[name] = prop ? mapSecretFields(prop, v, fn, join(path, name)) : v;
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
case "array":
|
|
140
|
+
return Array.isArray(value) ? value.map((v, i) => mapSecretFields(schema.items, v, fn, `${path}[${i}]`)) : value;
|
|
141
|
+
case "string":
|
|
142
|
+
return schema.secret === true && typeof value === "string" ? fn(value, path) : value;
|
|
143
|
+
default:
|
|
144
|
+
return value;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/** Whether the schema marks any field `secret`. */
|
|
148
|
+
export function hasSecretFields(schema) {
|
|
149
|
+
switch (schema.type) {
|
|
150
|
+
case "object":
|
|
151
|
+
return Object.values(schema.properties).some(hasSecretFields);
|
|
152
|
+
case "array":
|
|
153
|
+
return hasSecretFields(schema.items);
|
|
154
|
+
case "string":
|
|
155
|
+
return schema.secret === true;
|
|
156
|
+
default:
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** Every string anywhere in a JSON value, rewritten by `fn` (the runner uses
|
|
161
|
+
* it to resolve the `secretref:`s inside a structured parameter, which are
|
|
162
|
+
* resolved wherever they sit, as a whole value that is one is). */
|
|
163
|
+
export function mapJsonStrings(value, fn) {
|
|
164
|
+
if (typeof value === "string")
|
|
165
|
+
return fn(value);
|
|
166
|
+
if (Array.isArray(value))
|
|
167
|
+
return value.map((v) => mapJsonStrings(v, fn));
|
|
168
|
+
if (typeof value === "object" && value !== null) {
|
|
169
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, mapJsonStrings(v, fn)]));
|
|
170
|
+
}
|
|
171
|
+
return value;
|
|
172
|
+
}
|
|
36
173
|
function rangeError(p, n) {
|
|
37
174
|
if (p.min !== undefined && n < p.min)
|
|
38
175
|
return `must be >= ${p.min}`;
|
package/secret-references.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Result } from "@norskvideo/ctl-foundation/browser";
|
|
2
|
+
import { type JsonParamSchema } from "./params.js";
|
|
2
3
|
export declare const SECRET_REFERENCE_PREFIX = "secretref:";
|
|
3
4
|
/** The NodeInventory capability a worker advertises when it resolves secret
|
|
4
5
|
* references, so placement can keep a job whose parameters hold one off an
|
|
@@ -21,11 +22,27 @@ export interface SecretReference {
|
|
|
21
22
|
export declare function isSecretReference(value: string): boolean;
|
|
22
23
|
export declare function parseSecretReference(value: string): Result<SecretReference, string>;
|
|
23
24
|
export declare function secretReference(provider: string, key: string): string;
|
|
25
|
+
/** The slice of a parameter declaration masking reads. */
|
|
26
|
+
export interface DeclaredSecrecy {
|
|
27
|
+
name: string;
|
|
28
|
+
secret?: boolean;
|
|
29
|
+
type?: string;
|
|
30
|
+
schema?: JsonParamSchema;
|
|
31
|
+
}
|
|
24
32
|
/** Launch parameters as they may be shown: a parameter the template declares
|
|
25
33
|
* `secret` has its literal value masked. A reference is shown as it is — it
|
|
26
34
|
* is the point of a reference that it reveals nothing — and an empty value
|
|
27
|
-
* stays visibly empty.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
* stays visibly empty. In a structured parameter, each field its schema marks
|
|
36
|
+
* `secret` is masked by the same rule; one that is not JSON, and has secret
|
|
37
|
+
* fields, is masked whole. */
|
|
38
|
+
export declare function redactSecretParams(params: Record<string, string>, declared: readonly DeclaredSecrecy[]): Record<string, string>;
|
|
39
|
+
/** A structured parameter's text with its secret fields masked. */
|
|
40
|
+
export declare function redactJsonSecrets(schema: JsonParamSchema, text: string): string;
|
|
41
|
+
/** A structured parameter's text as an editor sent it back, with each secret field
|
|
42
|
+
* that holds the placeholder given the value it stands for in `stored` (the
|
|
43
|
+
* same field of the value held now). Answers the text, or the paths whose
|
|
44
|
+
* placeholder has nothing behind it. */
|
|
45
|
+
export declare function restoreJsonSecrets(schema: JsonParamSchema, incoming: string, stored: string | undefined): Result<string, string[]>;
|
|
46
|
+
/** Every `secretref:` a parameter's value holds: itself, when it is one, or
|
|
47
|
+
* each one inside it, when it is JSON (a structured parameter's fields). */
|
|
48
|
+
export declare function secretReferencesIn(value: string): string[];
|
package/secret-references.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
// The browser entry: the API contract reaches this module (the resolve
|
|
12
12
|
// endpoint's wire schemas), and the contract is loaded by the web UIs.
|
|
13
13
|
import { err, ok } from "@norskvideo/ctl-foundation/browser";
|
|
14
|
+
import { hasSecretFields, mapJsonStrings, mapSecretFields } from "./params.js";
|
|
14
15
|
export const SECRET_REFERENCE_PREFIX = "secretref:";
|
|
15
16
|
/** The NodeInventory capability a worker advertises when it resolves secret
|
|
16
17
|
* references, so placement can keep a job whose parameters hold one off an
|
|
@@ -49,14 +50,102 @@ export function parseSecretReference(value) {
|
|
|
49
50
|
export function secretReference(provider, key) {
|
|
50
51
|
return `${SECRET_REFERENCE_PREFIX}${provider}:${key}`;
|
|
51
52
|
}
|
|
53
|
+
const maskOne = (v) => (v !== "" && !isSecretReference(v) ? SECRET_PLACEHOLDER : v);
|
|
52
54
|
/** Launch parameters as they may be shown: a parameter the template declares
|
|
53
55
|
* `secret` has its literal value masked. A reference is shown as it is — it
|
|
54
56
|
* is the point of a reference that it reveals nothing — and an empty value
|
|
55
|
-
* stays visibly empty.
|
|
57
|
+
* stays visibly empty. In a structured parameter, each field its schema marks
|
|
58
|
+
* `secret` is masked by the same rule; one that is not JSON, and has secret
|
|
59
|
+
* fields, is masked whole. */
|
|
56
60
|
export function redactSecretParams(params, declared) {
|
|
57
|
-
const
|
|
58
|
-
return Object.fromEntries(Object.entries(params).map(([k, v]) =>
|
|
59
|
-
k
|
|
60
|
-
secret
|
|
61
|
-
|
|
61
|
+
const byName = new Map(declared.map((p) => [p.name, p]));
|
|
62
|
+
return Object.fromEntries(Object.entries(params).map(([k, v]) => {
|
|
63
|
+
const p = byName.get(k);
|
|
64
|
+
if (p?.secret === true)
|
|
65
|
+
return [k, maskOne(v)];
|
|
66
|
+
if (p?.schema && hasSecretFields(p.schema))
|
|
67
|
+
return [k, redactJsonSecrets(p.schema, v)];
|
|
68
|
+
return [k, v];
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
/** A structured parameter's text with its secret fields masked. */
|
|
72
|
+
export function redactJsonSecrets(schema, text) {
|
|
73
|
+
if (text.trim() === "")
|
|
74
|
+
return text;
|
|
75
|
+
let value;
|
|
76
|
+
try {
|
|
77
|
+
value = JSON.parse(text);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return SECRET_PLACEHOLDER;
|
|
81
|
+
}
|
|
82
|
+
return JSON.stringify(mapSecretFields(schema, value, maskOne));
|
|
83
|
+
}
|
|
84
|
+
/** A structured parameter's text as an editor sent it back, with each secret field
|
|
85
|
+
* that holds the placeholder given the value it stands for in `stored` (the
|
|
86
|
+
* same field of the value held now). Answers the text, or the paths whose
|
|
87
|
+
* placeholder has nothing behind it. */
|
|
88
|
+
export function restoreJsonSecrets(schema, incoming, stored) {
|
|
89
|
+
if (!incoming.includes(SECRET_PLACEHOLDER))
|
|
90
|
+
return ok(incoming);
|
|
91
|
+
let value;
|
|
92
|
+
try {
|
|
93
|
+
value = JSON.parse(incoming);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// Not JSON: a whole-value placeholder (redactJsonSecrets of a value that
|
|
97
|
+
// was not JSON) keeps what is stored, verbatim.
|
|
98
|
+
return incoming.trim() === SECRET_PLACEHOLDER && stored !== undefined ? ok(stored) : err(["value"]);
|
|
99
|
+
}
|
|
100
|
+
let old;
|
|
101
|
+
try {
|
|
102
|
+
old = stored === undefined ? undefined : JSON.parse(stored);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
old = undefined;
|
|
106
|
+
}
|
|
107
|
+
const missing = [];
|
|
108
|
+
const restored = mapSecretFields(schema, value, (v, path) => {
|
|
109
|
+
if (v !== SECRET_PLACEHOLDER)
|
|
110
|
+
return v;
|
|
111
|
+
const was = valueAt(old, path);
|
|
112
|
+
if (typeof was === "string" && was !== SECRET_PLACEHOLDER)
|
|
113
|
+
return was;
|
|
114
|
+
missing.push(path === "" ? "value" : path);
|
|
115
|
+
return v;
|
|
116
|
+
});
|
|
117
|
+
return missing.length > 0 ? err(missing) : ok(JSON.stringify(restored));
|
|
118
|
+
}
|
|
119
|
+
/** The value at a path like `servers[1].credential`. */
|
|
120
|
+
function valueAt(value, path) {
|
|
121
|
+
let cur = value;
|
|
122
|
+
for (const part of path.match(/[^.[\]]+/g) ?? []) {
|
|
123
|
+
if (cur === null || typeof cur !== "object")
|
|
124
|
+
return undefined;
|
|
125
|
+
cur = Array.isArray(cur) ? cur[Number(part)] : cur[part];
|
|
126
|
+
}
|
|
127
|
+
return cur;
|
|
128
|
+
}
|
|
129
|
+
/** Every `secretref:` a parameter's value holds: itself, when it is one, or
|
|
130
|
+
* each one inside it, when it is JSON (a structured parameter's fields). */
|
|
131
|
+
export function secretReferencesIn(value) {
|
|
132
|
+
if (isSecretReference(value))
|
|
133
|
+
return [value];
|
|
134
|
+
const t = value.trimStart();
|
|
135
|
+
if (!t.startsWith("{") && !t.startsWith("["))
|
|
136
|
+
return [];
|
|
137
|
+
let parsed;
|
|
138
|
+
try {
|
|
139
|
+
parsed = JSON.parse(value);
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
const found = [];
|
|
145
|
+
mapJsonStrings(parsed, (s) => {
|
|
146
|
+
if (isSecretReference(s))
|
|
147
|
+
found.push(s);
|
|
148
|
+
return s;
|
|
149
|
+
});
|
|
150
|
+
return found;
|
|
62
151
|
}
|