Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settingsSecretValue.ts

93 lines3.7 KB

1import * as Schema from "effect/Schema";
2import * as SchemaAST from "effect/SchemaAST";
3​
4import { TrimmedString } from "./baseSchemas.ts";
5import { SecretReference } from "./secretReference.ts";
6​
7/**
8 * A credential setting: the literal, which is kept in the secret store and redacted for
9 * clients, or the secret reference to read it from, which is not itself a secret and is
10 * stored and shown as written. Typing a settings field with it is the whole declaration:
11 * forms offer a secret provider for it and the server reads the secret for its consumer.
12 */
13export const SettingsSecretValue = Schema.Union([TrimmedString, SecretReference]).pipe(
14 Schema.annotateKey({ settingsSecret: true, settingsSecretReference: true }),
15);
16export type SettingsSecretValue = typeof SettingsSecretValue.Type;
17​
18export const isSecretReference = Schema.is(SecretReference);
19​
20/** The literal a credential setting holds, or `""` when it names a secret instead. */
21export function settingsSecretLiteral(value: SettingsSecretValue | undefined): string {
22 return typeof value === "string" ? value : "";
24​
25export function acceptsSecretReference(schema: Schema.Top): boolean {
26 return Schema.resolveAnnotationsKey(schema)?.settingsSecretReference === true;
28​
29const declaresSecretReference = (ast: SchemaAST.AST) =>
30 ast.context?.annotations?.settingsSecretReference === true;
31​
32interface SecretReferenceField {
33 readonly field: string;
34 readonly keyed: boolean;
36​
37/**
38 * Fields of a settings struct typed `SettingsSecretValue`, or a record of it such as one value
39 * per host; any other schema has none.
40 */
41function secretReferenceSettingsFields(schema: Schema.Top): ReadonlyArray<SecretReferenceField> {
42 if (!SchemaAST.isObjects(schema.ast)) return [];
43 return schema.ast.propertySignatures.flatMap<SecretReferenceField>(({ name, type }) => {
44 if (typeof name !== "string") return [];
45 if (declaresSecretReference(type)) return [{ field: name, keyed: false }];
46 return SchemaAST.isObjects(type) &&
47 type.indexSignatures.some((signature) => declaresSecretReference(signature.type))
48 ? [{ field: name, keyed: true }]
49 : [];
50 });
52​
53/** Where a reference sits: a field, or one key of a record field. */
54export interface SettingsSecretSlot {
55 readonly field: string;
56 readonly key: string | undefined;
58​
59interface SettingsSecretReference extends SettingsSecretSlot {
60 readonly reference: SecretReference;
62​
63/** Each declared reference slot of `settings` that holds a reference, in field order. */
64export function secretReferencesInSettings(
65 schema: Schema.Top,
66 settings: unknown,
67): ReadonlyArray<SettingsSecretReference> {
68 if (settings === null || typeof settings !== "object") return [];
69 const values = settings as Readonly<Record<string, unknown>>;
70 return secretReferenceSettingsFields(schema).flatMap<SettingsSecretReference>(
71 ({ field, keyed }) => {
72 const value = values[field];
73 if (!keyed)
74 return isSecretReference(value) ? [{ field, key: undefined, reference: value }] : [];
75 if (value === null || typeof value !== "object") return [];
76 return Object.entries(value).flatMap(([key, entry]) =>
77 isSecretReference(entry) ? [{ field, key, reference: entry }] : [],
78 );
79 },
80 );
82​
83/** `settings` with each slot set to its value, keeping the other keys of a record field. */
84export function withSettingsSecrets<Settings extends Readonly<Record<string, unknown>>>(
85 settings: Settings,
86 slots: ReadonlyArray<SettingsSecretSlot & { readonly value: string }>,
87): Settings {
88 const next: Record<string, unknown> = { ...settings };
89 for (const { field, key, value } of slots) {
90 next[field] = key === undefined ? value : { ...(next[field] as object), [key]: value };
91 }
92 return next as Settings;