Skip to content
refs
History
64 lines · 3.6 KBRaw
1import type { SecretAccess } from "../runs/trust.ts";
2import { SecretBox, type SecretScope } from "./secret-box.ts";
3
4export interface JobSecretRequest {
5 readonly repoId: string;
6 readonly namespaceId: string | null;
7 readonly environment: string | null;
8 readonly access: SecretAccess;
9 /** True when a person approved this job, which opens its environment's secrets to a run with `repo` access. */
10 readonly approved: boolean;
11 readonly names: readonly string[];
12}
13
14export interface OpenedSecrets {
15 readonly values: Readonly<Record<string, string>>;
16 /** Lines for the job log that say why a named secret is not there. They never hold a value. */
17 readonly notes: readonly string[];
18}
19
20type Row = { level: number; environment: string; name: string; ciphertext: string; wrapped_key: string };
21
22const LEVEL_NAMESPACE = 0;
23const LEVEL_REPO = 1;
24const LEVEL_ENVIRONMENT = 2;
25
26/**
27 * Opens the secrets a job names, and only those. An environment secret wins over a repository secret, and a
28 * repository secret wins over an organization secret of the same name.
29 */
30export async function openJobSecrets(db: D1Database, masterKey: string | undefined, request: JobSecretRequest): Promise<OpenedSecrets> {
31 if (!request.names.length) return { values: {}, notes: [] };
32 if (request.access === "none") return { values: {}, notes: ["This run gets no secrets, because it is untrusted."] };
33 if (!masterKey) throw new Error("CI secrets are not set up on this server: CI_SECRETS_KEY is missing.");
34 const withEnvironment = request.environment !== null && (request.access === "all" || request.approved);
35 const { results } = await db
36 .prepare(
37 `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))
38 UNION ALL
39 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))`,
40 )
41 .bind(request.namespaceId ?? "", request.repoId, JSON.stringify(request.names), withEnvironment ? request.environment : "")
42 .all<Row>();
43 const chosen = new Map<string, Row>();
44 for (const row of results) if ((chosen.get(row.name)?.level ?? -1) < row.level) chosen.set(row.name, row);
45 const box = await SecretBox.fromBase64(masterKey);
46 const values: Record<string, string> = {};
47 for (const row of chosen.values()) values[row.name] = await box.open({ ciphertext: row.ciphertext, wrappedKey: row.wrapped_key }, scopeOf(row, request));
48 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` : ""}.`);
49 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.`);
50 return { values, notes };
51}
52
53export function repoSecretScope(repoId: string, environment: string | null, name: string): SecretScope {
54 return { owner: `repo:${repoId}`, environment, name };
55}
56
57export function namespaceSecretScope(namespaceId: string, name: string): SecretScope {
58 return { owner: `namespace:${namespaceId}`, environment: null, name };
59}
60
61function scopeOf(row: Row, request: JobSecretRequest): SecretScope {
62 if (row.level === LEVEL_NAMESPACE) return namespaceSecretScope(request.namespaceId ?? "", row.name);
63 return repoSecretScope(request.repoId, row.environment || null, row.name);
64}