Diffinery

codician/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

29 lines · 1.5 KB · executableRaw History
1#!/usr/bin/env bash
2# Generate the Worker's own secrets once (never rotated automatically):
3# MASTER_KEY root key for encrypting Actions secrets
4# OIDC_PRIVATE_KEY ES256 key that signs workflow OIDC id tokens
5# Values are created on the runner and sent straight to Cloudflare; they are never printed.
6set -euo pipefail
7APP="$(cd "$(dirname "$0")/../apps/diffinery" && pwd)"
8WRANGLER=(pnpm --dir "$APP" exec wrangler)
9
10# Fail closed: if listing fails we must not mistake "unknown" for "missing" and
11# overwrite MASTER_KEY, which would make every stored Actions secret unreadable.
12LIST="$("${WRANGLER[@]}" secret list -c wrangler.deploy.jsonc --format json)"
13EXISTING="$(jq -er '.[].name' <<<"$LIST" || true)"
14jq -e 'type == "array"' >/dev/null <<<"$LIST" || { echo "unexpected output from wrangler secret list" >&2; exit 1; }
15
16if ! grep -qx MASTER_KEY <<<"$EXISTING"; then
17 openssl rand -base64 32 | tr -d '\n' | "${WRANGLER[@]}" secret put MASTER_KEY -c wrangler.deploy.jsonc >/dev/null
18 echo "set MASTER_KEY"
19fi
20if ! grep -qx OIDC_PRIVATE_KEY <<<"$EXISTING"; then
21 node --input-type=module -e '
22 const { subtle } = globalThis.crypto;
23 const k = await subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, true, ["sign", "verify"]);
24 const jwk = await subtle.exportKey("jwk", k.privateKey);
25 jwk.kid = "diffinery-" + new Date().toISOString().slice(0, 10);
26 process.stdout.write(JSON.stringify(jwk));
27 ' | "${WRANGLER[@]}" secret put OIDC_PRIVATE_KEY -c wrangler.deploy.jsonc >/dev/null
28 echo "set OIDC_PRIVATE_KEY"
29fi