Files
instance-metadata-service/src/daemon/routes/secrets.ts
T
2026-05-02 02:30:12 -07:00

51 lines
1.2 KiB
TypeScript

import type { ServerResponse } from "node:http";
import { sendJson, sendText } from "../http-utils.js";
import type { SecretCache } from "../store/secret-cache.js";
const SECRETS_PREFIX = "/latest/meta-data/secrets/";
function decodeSecretKey(rawKey: string): string | null {
try {
const decoded = decodeURIComponent(rawKey);
if (!decoded || decoded.includes("/")) return null;
return decoded;
} catch {
return null;
}
}
export function handleSecretsDirectory(res: ServerResponse, cache: SecretCache): void {
const keys = cache.list();
sendText(res, 200, keys.length === 0 ? "" : `${keys.join("\n")}\n`);
}
export function handleSecretValue(pathname: string, res: ServerResponse, cache: SecretCache): void {
const rawKey = pathname.slice(SECRETS_PREFIX.length);
const key = decodeSecretKey(rawKey);
if (!key || !cache.isAllowed(key)) {
sendText(res, 404);
return;
}
const value = cache.get(key);
if (value === undefined) {
sendText(res, 404);
return;
}
sendText(res, 200, value);
}
export function handleSecretsBundle(
res: ServerResponse,
cache: SecretCache,
enabled: boolean,
): void {
if (!enabled) {
sendText(res, 404);
return;
}
sendJson(res, 200, cache.bundle());
}