import type { SecretAccess } from "../runs/trust.ts"; import { SecretBox, type SecretScope } from "./secret-box.ts"; export interface JobSecretRequest { readonly repoId: string; readonly namespaceId: string | null; readonly environment: string | null; readonly access: SecretAccess; /** True when a person approved this job, which opens its environment's secrets to a run with `repo` access. */ readonly approved: boolean; readonly names: readonly string[]; } export interface OpenedSecrets { readonly values: Readonly>; /** Lines for the job log that say why a named secret is not there. They never hold a value. */ readonly notes: readonly string[]; } type Row = { level: number; environment: string; name: string; ciphertext: string; wrapped_key: string }; const LEVEL_NAMESPACE = 0; const LEVEL_REPO = 1; const LEVEL_ENVIRONMENT = 2; /** * Opens the secrets a job names, and only those. An environment secret wins over a repository secret, and a * repository secret wins over an organization secret of the same name. */ export async function openJobSecrets(db: D1Database, masterKey: string | undefined, request: JobSecretRequest): Promise { if (!request.names.length) return { values: {}, notes: [] }; if (request.access === "none") return { values: {}, notes: ["This run gets no secrets, because it is untrusted."] }; if (!masterKey) throw new Error("CI secrets are not set up on this server: CI_SECRETS_KEY is missing."); const withEnvironment = request.environment !== null && (request.access === "all" || request.approved); const { results } = await db .prepare( `SELECT ${LEVEL_NAMESPACE} AS level, '' AS environment, name, ciphertext, wrapped_key FROM ci_namespace_secrets WHERE namespace_id = ?1 AND name IN (SELECT value FROM json_each(?3)) UNION ALL SELECT CASE environment WHEN '' THEN ${LEVEL_REPO} ELSE ${LEVEL_ENVIRONMENT} END, environment, name, ciphertext, wrapped_key FROM ci_secrets WHERE repo_id = ?2 AND (environment = '' OR environment = ?4) AND name IN (SELECT value FROM json_each(?3))`, ) .bind(request.namespaceId ?? "", request.repoId, JSON.stringify(request.names), withEnvironment ? request.environment : "") .all(); const chosen = new Map(); for (const row of results) if ((chosen.get(row.name)?.level ?? -1) < row.level) chosen.set(row.name, row); const box = await SecretBox.fromBase64(masterKey); const values: Record = {}; for (const row of chosen.values()) values[row.name] = await box.open({ ciphertext: row.ciphertext, wrappedKey: row.wrapped_key }, scopeOf(row, request)); const notes = request.names.filter((name) => !(name in values)).map((name) => `The secret ${name} is not set for this repository${withEnvironment ? ` or the ${request.environment} environment` : ""}.`); if (request.environment !== null && !withEnvironment) notes.push(`Secrets of the ${request.environment} environment are held back: this run needs a person to approve the job, or a push by a person to a protected branch.`); return { values, notes }; } export function repoSecretScope(repoId: string, environment: string | null, name: string): SecretScope { return { owner: `repo:${repoId}`, environment, name }; } export function namespaceSecretScope(namespaceId: string, name: string): SecretScope { return { owner: `namespace:${namespaceId}`, environment: null, name }; } function scopeOf(row: Row, request: JobSecretRequest): SecretScope { if (row.level === LEVEL_NAMESPACE) return namespaceSecretScope(request.namespaceId ?? "", row.name); return repoSecretScope(request.repoId, row.environment || null, row.name); }