File size: 3,217 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import type { ErrorObject } from "ajv";
import { isKnownSecretTargetId } from "../../secrets/target-registry.js";
import {
ErrorCodes,
errorShape,
validateSecretsResolveParams,
validateSecretsResolveResult,
} from "../protocol/index.js";
import type { GatewayRequestHandlers } from "./types.js";
function invalidSecretsResolveField(
errors: ErrorObject[] | null | undefined,
): "commandName" | "targetIds" {
for (const issue of errors ?? []) {
if (
issue.instancePath === "/commandName" ||
(issue.instancePath === "" &&
String((issue.params as { missingProperty?: unknown })?.missingProperty) === "commandName")
) {
return "commandName";
}
}
return "targetIds";
}
export function createSecretsHandlers(params: {
reloadSecrets: () => Promise<{ warningCount: number }>;
resolveSecrets: (params: { commandName: string; targetIds: string[] }) => Promise<{
assignments: Array<{
path: string;
pathSegments: string[];
value: unknown;
}>;
diagnostics: string[];
inactiveRefPaths: string[];
}>;
}): GatewayRequestHandlers {
return {
"secrets.reload": async ({ respond }) => {
try {
const result = await params.reloadSecrets();
respond(true, { ok: true, warningCount: result.warningCount });
} catch (err) {
respond(false, undefined, errorShape(ErrorCodes.UNAVAILABLE, String(err)));
}
},
"secrets.resolve": async ({ params: requestParams, respond }) => {
if (!validateSecretsResolveParams(requestParams)) {
const field = invalidSecretsResolveField(validateSecretsResolveParams.errors);
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_REQUEST, `invalid secrets.resolve params: ${field}`),
);
return;
}
const commandName = requestParams.commandName.trim();
if (!commandName) {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_REQUEST, "invalid secrets.resolve params: commandName"),
);
return;
}
const targetIds = requestParams.targetIds
.map((entry) => entry.trim())
.filter((entry) => entry.length > 0);
for (const targetId of targetIds) {
if (!isKnownSecretTargetId(targetId)) {
respond(
false,
undefined,
errorShape(
ErrorCodes.INVALID_REQUEST,
`invalid secrets.resolve params: unknown target id "${String(targetId)}"`,
),
);
return;
}
}
try {
const result = await params.resolveSecrets({
commandName,
targetIds,
});
const payload = {
ok: true,
assignments: result.assignments,
diagnostics: result.diagnostics,
inactiveRefPaths: result.inactiveRefPaths,
};
if (!validateSecretsResolveResult(payload)) {
throw new Error("secrets.resolve returned invalid payload.");
}
respond(true, payload);
} catch (err) {
respond(false, undefined, errorShape(ErrorCodes.UNAVAILABLE, String(err)));
}
},
};
}
|