@fabrika/provider-cloudflare 0.0.26 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +12 -6
- package/src/assets.ts +87 -0
- package/src/cli-args.ts +22 -4
- package/src/cli.ts +7 -8
- package/src/codec.ts +4 -65
- package/src/collaborators.ts +36 -9
- package/src/command.ts +20 -36
- package/src/control.ts +309 -96
- package/src/index.ts +8 -6
- package/src/installation/cloudflare.ts +184 -0
- package/src/installation/envfile.ts +180 -0
- package/src/installation/index.ts +2 -0
- package/src/installation/init.ts +597 -0
- package/src/installation/installation.ts +77 -0
- package/src/installation/scaffold.ts +106 -0
- package/src/installation/templates/README.md +64 -0
- package/src/installation/templates/fabrika.ref +1 -0
- package/src/installation/templates/gitignore +2 -0
- package/src/installation/templates/platform.yml +181 -0
- package/src/plan.ts +231 -29
- package/src/provider.ts +115 -70
- package/src/proxy-worker.ts +1 -1
- package/src/proxy.ts +1 -1
- package/src/runner-protocol.ts +40 -0
- package/src/schema.ts +34 -1
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The per-account base repo `<org>/fabrika-platform`: which files it carries, and the one migration that
|
|
3
|
+
* must stop a refresh. The create/clone/commit/push mechanics are shared
|
|
4
|
+
* (`@fabrika/installation-init`); everything here is Cloudflare's answer to what gets written.
|
|
5
|
+
*
|
|
6
|
+
* `fabrika.ref` is written ONLY when absent (operator-owned after creation — bumping it is how you roll a new
|
|
7
|
+
* base); `platform.yml` / `README.md` / `.gitignore` are CLI-owned and refreshed every run.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
INITIAL_SCAFFOLD_COMMIT_MESSAGE,
|
|
12
|
+
type ProtectionOutcome,
|
|
13
|
+
REFRESH_SCAFFOLD_COMMIT_MESSAGE,
|
|
14
|
+
scaffoldSidecarRepository,
|
|
15
|
+
} from '@fabrika/installation-init'
|
|
16
|
+
import { existsSync } from 'node:fs'
|
|
17
|
+
import { resolve } from 'node:path'
|
|
18
|
+
|
|
19
|
+
const TEMPLATES = resolve(import.meta.dir, 'templates')
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* All scaffold files we ever `git add` (the CLI-owned trio + the operator-owned, write-once `fabrika.ref` pin).
|
|
23
|
+
*/
|
|
24
|
+
const ALL_FILES = ['.gitignore', 'README.md', 'fabrika.ref', '.github/workflows/platform.yml']
|
|
25
|
+
const LEGACY_REF_FILES = ['vozka.ref', 'propustka.ref']
|
|
26
|
+
|
|
27
|
+
export { INITIAL_SCAFFOLD_COMMIT_MESSAGE, REFRESH_SCAFFOLD_COMMIT_MESSAGE }
|
|
28
|
+
|
|
29
|
+
export interface ScaffoldInput {
|
|
30
|
+
/** The base repo, e.g. `manGoweb/fabrika-platform`. */
|
|
31
|
+
repo: string
|
|
32
|
+
/** The account label (positional `init` arg) — substituted into the templates + the env/summary names. */
|
|
33
|
+
account: string
|
|
34
|
+
/** Local directory to check the repo out into. */
|
|
35
|
+
dir: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Result of scaffolding: the local checkout dir + whether the repo was freshly created. */
|
|
39
|
+
export interface ScaffoldResult {
|
|
40
|
+
dir: string
|
|
41
|
+
created: boolean
|
|
42
|
+
/** Set only for a repository this run created; absent means an existing repo was left as it is. */
|
|
43
|
+
branchProtection?: ProtectionOutcome
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Render a template: replace every `{{ACCOUNT}}` token. (Account is the only placeholder today.) */
|
|
47
|
+
async function renderTemplate(name: string, account: string): Promise<string> {
|
|
48
|
+
const raw = await Bun.file(resolve(TEMPLATES, name)).text()
|
|
49
|
+
return raw.replaceAll('{{ACCOUNT}}', account)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Write the file at `rel` under `dir` with `content`, creating parent dirs as needed. */
|
|
53
|
+
async function writeFile(dir: string, rel: string, content: string): Promise<void> {
|
|
54
|
+
await Bun.write(resolve(dir, rel), content)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Materialize the CLI-owned files and the write-once source pin.
|
|
59
|
+
*
|
|
60
|
+
* Legacy two-repository pins need an operator-chosen merged-repository ref. Stop before writing anything
|
|
61
|
+
* so a refresh cannot hide the migration requirement or delete operator-owned files.
|
|
62
|
+
*/
|
|
63
|
+
export async function materializePlatformScaffold(dir: string, account: string): Promise<void> {
|
|
64
|
+
const legacyRefs = LEGACY_REF_FILES.filter((name) => existsSync(resolve(dir, name)))
|
|
65
|
+
if (legacyRefs.length > 0) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`Legacy two-repository scaffold detected (${legacyRefs.join(', ')}). Choose the matching `
|
|
68
|
+
+ 'contember/fabrika-platform commit or tag, write it to fabrika.ref, then remove the legacy '
|
|
69
|
+
+ 'pin files manually and run `fabrika platform init --provider=cloudflare` again. '
|
|
70
|
+
+ 'The CLI cannot infer the merged ref.',
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
await writeFile(dir, '.gitignore', await Bun.file(resolve(TEMPLATES, 'gitignore')).text())
|
|
74
|
+
await writeFile(dir, 'README.md', await renderTemplate('README.md', account))
|
|
75
|
+
await writeFile(dir, '.github/workflows/platform.yml', await renderTemplate('platform.yml', account))
|
|
76
|
+
if (!existsSync(resolve(dir, 'fabrika.ref'))) {
|
|
77
|
+
await writeFile(dir, 'fabrika.ref', await Bun.file(resolve(TEMPLATES, 'fabrika.ref')).text())
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Ensure `<org>/fabrika-platform` exists + carries the current scaffold. Returns the local checkout dir.
|
|
83
|
+
* Requires `gh` authed with rights to create/admin the repo.
|
|
84
|
+
*/
|
|
85
|
+
export async function scaffoldPlatformRepo(input: ScaffoldInput): Promise<ScaffoldResult> {
|
|
86
|
+
return scaffoldSidecarRepository({
|
|
87
|
+
repo: input.repo,
|
|
88
|
+
dir: input.dir,
|
|
89
|
+
files: ALL_FILES,
|
|
90
|
+
materialize: (dir) => materializePlatformScaffold(dir, input.account),
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The default local checkout dir for an account: `./fabrika-platform-<account>` under the cwd. */
|
|
95
|
+
export function defaultCheckoutDir(account: string): string {
|
|
96
|
+
return resolve(process.cwd(), `fabrika-platform-${account}`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Read the configured `fabrika.ref` (for display), or 'main' when absent. Used in the final summary. */
|
|
100
|
+
export async function readFabrikaRef(dir: string): Promise<string> {
|
|
101
|
+
const file = Bun.file(resolve(dir, 'fabrika.ref'))
|
|
102
|
+
if (!(await file.exists())) {
|
|
103
|
+
return 'main'
|
|
104
|
+
}
|
|
105
|
+
return (await file.text()).trim() || 'main'
|
|
106
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# fabrika-platform ({{ACCOUNT}})
|
|
2
|
+
|
|
3
|
+
The **per-account root of trust** for the {{ACCOUNT}} Cloudflare account. Its GitHub pipeline deploys the
|
|
4
|
+
fabrika control-plane base so the control plane never deploys itself:
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
Stage 1 — IAM (root authz)
|
|
8
|
+
Stage 2 — Operations
|
|
9
|
+
Stage 3 — fabrika runner + control plane
|
|
10
|
+
apps (poplach, revizor, …) ← deployed through fabrika, not here
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Generated and maintained by **`fabrika platform init --provider=cloudflare {{ACCOUNT}}`** (`@fabrika/cli`). Secrets and variables live in
|
|
14
|
+
the **`{{ACCOUNT}}` GitHub Environment** (Settings → Environments) and are written there by the CLI.
|
|
15
|
+
Scaffold commits use `[skip ci]`; init explicitly dispatches the workflow only after that Environment is configured.
|
|
16
|
+
|
|
17
|
+
Every credential this installation has is in that Environment — the Cloudflare API token, the vault KEK,
|
|
18
|
+
IAM's signing key, the provisioning key and `FABRIKA_RUNNER_KEY` (the shared key the
|
|
19
|
+
control plane presents to the deploy runner; Stage 3 pushes it to both Workers). Init therefore protects
|
|
20
|
+
`main` (no force pushes, no deletions) on a repository it creates, and restricts the Environment to
|
|
21
|
+
protected branches. The two go together: the Environment policy admits any branch that carries no
|
|
22
|
+
protection rule, so it is worth nothing on its own. GitHub Free offers neither on a private repository —
|
|
23
|
+
if init warned that one was refused, run the command it printed once the plan allows it.
|
|
24
|
+
|
|
25
|
+
Whatever the plan, add yourself as a **required reviewer** on the `{{ACCOUNT}}` Environment
|
|
26
|
+
(Settings → Environments → {{ACCOUNT}} → Required reviewers). It is the only rule that stops a push to
|
|
27
|
+
`main` from spending these credentials without a human first agreeing to it. Init deliberately does not
|
|
28
|
+
set it: it turns every deploy into an approval click, which is the operator's call and not the CLI's.
|
|
29
|
+
|
|
30
|
+
`contember/fabrika-platform` is public and pinned in [`fabrika.ref`](./fabrika.ref). The pipeline checks it
|
|
31
|
+
out once, then runs `fabrika platform deploy --provider=cloudflare` three times: `--component-config` for
|
|
32
|
+
IAM, `--component-config` for Operations, and the `--runner-config`/`--worker-config` pair for
|
|
33
|
+
`packages/runner-cloudflare` and `packages/control`. Every stage is a PLATFORM deploy because each names a
|
|
34
|
+
platform resource; `fabrika app deploy` is for applications and is refused one. The workflow builds the runner image from that same checkout into this account's
|
|
35
|
+
registry. It never depends on an image published by upstream CI or another account. Re-running it is
|
|
36
|
+
a safe redeploy.
|
|
37
|
+
|
|
38
|
+
## First bring-up
|
|
39
|
+
|
|
40
|
+
Run `fabrika platform init --provider=cloudflare {{ACCOUNT}}` from a laptop with the Cloudflare API token in hand. It creates this repo,
|
|
41
|
+
the Environment with its secrets and variables, and triggers the workflow. The run builds and pushes the
|
|
42
|
+
runner container image into the account.
|
|
43
|
+
|
|
44
|
+
Public repositories deploy without a GitHub App. To deploy private repositories, connect GitHub from the
|
|
45
|
+
control plane at **Settings → Source**: each connection is an organization-owned GitHub App whose key
|
|
46
|
+
Control keeps in its encrypted vault, never in this Environment.
|
|
47
|
+
|
|
48
|
+
To close the escape hatch once IAM grants you admin, set the `{{ACCOUNT}}` Environment variable
|
|
49
|
+
`FABRIKA_CONTROL_BOOTSTRAP_ADMINS` to `[]` and re-run the workflow.
|
|
50
|
+
|
|
51
|
+
IAM authentication methods are independent Environment switches: `FABRIKA_IAM_OIDC_ENABLED` and
|
|
52
|
+
`FABRIKA_IAM_PASSWORD_ENABLED`. At least one must be `true`. Password enrollment is managed through
|
|
53
|
+
IAM provisioning and its admin surface. Email delivery is optional; `FABRIKA_EMAIL_PROVIDER=none`
|
|
54
|
+
keeps password enrollment and reset admin-driven, while `resend` also requires the sender variable
|
|
55
|
+
and the Resend API key secret.
|
|
56
|
+
|
|
57
|
+
## Routine redeploy or version bump
|
|
58
|
+
|
|
59
|
+
- Bump [`fabrika.ref`](./fabrika.ref) to a new `contember/fabrika-platform` commit or tag and push.
|
|
60
|
+
- Or run _Actions → platform → Run workflow_ manually.
|
|
61
|
+
|
|
62
|
+
Both paths rebuild the account-owned runner image from the exact checkout selected by `fabrika.ref`.
|
|
63
|
+
|
|
64
|
+
`fabrika.ref` is operator-owned. `fabrika platform init` creates it once and does not overwrite later version pins.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
main
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
name: platform
|
|
2
|
+
|
|
3
|
+
# Deploys the {{ACCOUNT}} account's fabrika BASE: IAM (Stage 1, root authz) → Operations (Stage 2) →
|
|
4
|
+
# runner + control plane (Stage 3). THIS repo is the per-account root of trust, so the control plane
|
|
5
|
+
# never deploys itself.
|
|
6
|
+
# Idempotent: re-run for a first bring-up OR a routine redeploy. Concrete apps deploy through fabrika.
|
|
7
|
+
#
|
|
8
|
+
# Generated + maintained by `fabrika platform init --provider=cloudflare {{ACCOUNT}}`. Secrets + variables live in the `{{ACCOUNT}}` GitHub
|
|
9
|
+
# Environment (Settings → Environments → {{ACCOUNT}}) and are written there by the CLI — don't hand-edit them.
|
|
10
|
+
#
|
|
11
|
+
# contember/fabrika-platform is public and checked out tokenless. The version this account runs is pinned
|
|
12
|
+
# in `fabrika.ref` (bump it and push to roll out a new base).
|
|
13
|
+
|
|
14
|
+
on:
|
|
15
|
+
workflow_dispatch:
|
|
16
|
+
inputs:
|
|
17
|
+
retire_legacy_propustka_worker:
|
|
18
|
+
description: >-
|
|
19
|
+
Name of a LEGACY propustka Worker to delete BEFORE Stage 1 (one-off migration off an old
|
|
20
|
+
deploy — e.g. a CF-Access-era `mangoweb-propustka-worker`). Deleting it releases its Custom
|
|
21
|
+
Domain so the new env-prod propustka can claim it. Empty = skip (the normal case).
|
|
22
|
+
type: string
|
|
23
|
+
default: ''
|
|
24
|
+
push:
|
|
25
|
+
branches: [main]
|
|
26
|
+
# Only a version bump (or a workflow edit) redeploys — not a README tweak.
|
|
27
|
+
paths: ['fabrika.ref', '.github/workflows/platform.yml']
|
|
28
|
+
|
|
29
|
+
concurrency:
|
|
30
|
+
group: platform-deploy
|
|
31
|
+
cancel-in-progress: false
|
|
32
|
+
|
|
33
|
+
permissions:
|
|
34
|
+
contents: read
|
|
35
|
+
|
|
36
|
+
jobs:
|
|
37
|
+
deploy:
|
|
38
|
+
runs-on: ubuntu-latest
|
|
39
|
+
timeout-minutes: 25
|
|
40
|
+
# Secrets + variables resolve from this GitHub Environment (created + populated by `fabrika platform init`).
|
|
41
|
+
environment: {{ACCOUNT}}
|
|
42
|
+
# Cloudflare creds at the JOB level so EVERY oblaka/wrangler step (provision, migrate, deploy, secret
|
|
43
|
+
# put) inherits them — wrangler errors in a non-interactive shell without CLOUDFLARE_API_TOKEN.
|
|
44
|
+
env:
|
|
45
|
+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
46
|
+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
47
|
+
steps:
|
|
48
|
+
- name: Checkout this repo (base config + pinned refs)
|
|
49
|
+
uses: actions/checkout@v5
|
|
50
|
+
|
|
51
|
+
- name: Resolve pinned refs
|
|
52
|
+
id: ref
|
|
53
|
+
run: echo "fabrika=$(tr -d '[:space:]' < fabrika.ref)" >> "$GITHUB_OUTPUT"
|
|
54
|
+
|
|
55
|
+
- name: Checkout contember/fabrika-platform@${{ steps.ref.outputs.fabrika }}
|
|
56
|
+
uses: actions/checkout@v5
|
|
57
|
+
with:
|
|
58
|
+
repository: contember/fabrika-platform
|
|
59
|
+
ref: ${{ steps.ref.outputs.fabrika }}
|
|
60
|
+
path: fabrika-platform
|
|
61
|
+
|
|
62
|
+
- uses: oven-sh/setup-bun@v2
|
|
63
|
+
with:
|
|
64
|
+
bun-version: latest
|
|
65
|
+
# wrangler launches under Node even via Bun and needs Node 22+.
|
|
66
|
+
- uses: actions/setup-node@v5
|
|
67
|
+
with:
|
|
68
|
+
node-version: '24'
|
|
69
|
+
|
|
70
|
+
- name: Install fabrika
|
|
71
|
+
working-directory: fabrika-platform
|
|
72
|
+
run: bun install
|
|
73
|
+
|
|
74
|
+
# The Cloudflare provider spawns a BARE `wrangler` (Bun.spawn), so the workspace bins must be on PATH.
|
|
75
|
+
- name: Put fabrika workspace bins on PATH
|
|
76
|
+
working-directory: fabrika-platform
|
|
77
|
+
# `fabrika` itself is asserted rather than assumed: `bun install` links a workspace package's bin
|
|
78
|
+
# only when the root package depends on it, so an older pinned tag can install cleanly and still
|
|
79
|
+
# have no `fabrika` at all — a bare `command not found` several steps later, naming nothing.
|
|
80
|
+
run: |
|
|
81
|
+
if [ ! -x node_modules/.bin/fabrika ]; then
|
|
82
|
+
echo "fabrika-platform@${{ steps.ref.outputs.fabrika }} installed no \`fabrika\` binary." >&2
|
|
83
|
+
echo "Pin a tag whose root package.json depends on @fabrika/cli." >&2
|
|
84
|
+
exit 1
|
|
85
|
+
fi
|
|
86
|
+
echo "$PWD/node_modules/.bin" >> "$GITHUB_PATH"
|
|
87
|
+
echo "$PWD/packages/control/node_modules/.bin" >> "$GITHUB_PATH"
|
|
88
|
+
echo "$PWD/packages/runner-cloudflare/node_modules/.bin" >> "$GITHUB_PATH"
|
|
89
|
+
|
|
90
|
+
# ── Migration escape hatch — retire a legacy propustka Worker ─────────────────────────────────────
|
|
91
|
+
# Off by default. When `retire_legacy_propustka_worker` is set, delete that Worker so its Custom Domain
|
|
92
|
+
# is freed for the new env-prod propustka below. Its D1 database is NOT deleted (a Worker delete leaves
|
|
93
|
+
# databases) — orphaned, prune it by hand later. One-off: clear the input on subsequent runs.
|
|
94
|
+
- name: Retire legacy propustka Worker
|
|
95
|
+
if: ${{ inputs.retire_legacy_propustka_worker != '' }}
|
|
96
|
+
env:
|
|
97
|
+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
98
|
+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
99
|
+
LEGACY: ${{ inputs.retire_legacy_propustka_worker }}
|
|
100
|
+
# `wrangler delete` is non-interactive in CI (no TTY → no confirmation prompt). Removing the Worker
|
|
101
|
+
# detaches its Custom Domain. If the domain attach below races a just-freed hostname, re-run (idempotent).
|
|
102
|
+
run: bunx wrangler delete --name "$LEGACY"
|
|
103
|
+
|
|
104
|
+
# ── Stage 1 — IAM (root authz) ───────────────────────────────────────────────────────────────────
|
|
105
|
+
# IAM is a regular Cloudflare provider bundle. Its empty schema needs no self-reconcile.
|
|
106
|
+
- name: Deploy IAM
|
|
107
|
+
working-directory: fabrika-platform/packages/iam
|
|
108
|
+
env:
|
|
109
|
+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
110
|
+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
111
|
+
CLOUDFLARE_STATE_NAMESPACE: propustka-state
|
|
112
|
+
FABRIKA_CONTROL_DOMAIN: ${{ vars.FABRIKA_IAM_HOSTNAME }}
|
|
113
|
+
# Non-secret config (Environment Variables).
|
|
114
|
+
FABRIKA_IAM_HUMAN_EMAIL_DOMAINS: ${{ vars.FABRIKA_IAM_HUMAN_EMAIL_DOMAINS }}
|
|
115
|
+
FABRIKA_IAM_HUMAN_EMAILS: ${{ vars.FABRIKA_IAM_HUMAN_EMAILS }}
|
|
116
|
+
FABRIKA_IAM_BOOTSTRAP_ADMINS: ${{ vars.FABRIKA_IAM_BOOTSTRAP_ADMINS }}
|
|
117
|
+
FABRIKA_IAM_OIDC_ENABLED: ${{ vars.FABRIKA_IAM_OIDC_ENABLED }}
|
|
118
|
+
FABRIKA_IAM_PASSWORD_ENABLED: ${{ vars.FABRIKA_IAM_PASSWORD_ENABLED }}
|
|
119
|
+
FABRIKA_IAM_OIDC_ISSUER: ${{ vars.FABRIKA_IAM_OIDC_ISSUER }}
|
|
120
|
+
FABRIKA_IAM_OIDC_CLIENT_ID: ${{ vars.FABRIKA_IAM_OIDC_CLIENT_ID }}
|
|
121
|
+
FABRIKA_IAM_OIDC_SCOPES: ${{ vars.FABRIKA_IAM_OIDC_SCOPES }}
|
|
122
|
+
FABRIKA_IAM_OIDC_REQUIRE_VERIFIED_EMAIL: ${{ vars.FABRIKA_IAM_OIDC_REQUIRE_VERIFIED_EMAIL }}
|
|
123
|
+
FABRIKA_EMAIL_PROVIDER: ${{ vars.FABRIKA_EMAIL_PROVIDER }}
|
|
124
|
+
FABRIKA_EMAIL_FROM: ${{ vars.FABRIKA_EMAIL_FROM }}
|
|
125
|
+
FABRIKA_IAM_SIGNING_KEYS: ${{ secrets.FABRIKA_IAM_SIGNING_KEYS }}
|
|
126
|
+
OIDC_CLIENT_SECRET: ${{ secrets.FABRIKA_IAM_OIDC_CLIENT_SECRET }}
|
|
127
|
+
FABRIKA_EMAIL_RESEND_API_KEY: ${{ secrets.FABRIKA_EMAIL_RESEND_API_KEY }}
|
|
128
|
+
FABRIKA_IAM_PROVISIONING_KEY: ${{ secrets.FABRIKA_IAM_PROVISIONING_KEY }}
|
|
129
|
+
# `platform deploy`, not `app deploy`: IAM's own Worker is a PLATFORM resource name, and only the
|
|
130
|
+
# platform verb may name one. The single-component form deploys just this config.
|
|
131
|
+
run: fabrika platform deploy --provider=cloudflare --env=prod --component-config=fabrika.config.ts
|
|
132
|
+
|
|
133
|
+
# ── Stage 2 — Operations ────────────────────────────────────────────────────────────────────────
|
|
134
|
+
# Operations must exist before control: control carries a private service binding to it.
|
|
135
|
+
- name: Deploy Operations
|
|
136
|
+
working-directory: fabrika-platform/packages/operations
|
|
137
|
+
env:
|
|
138
|
+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
139
|
+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
140
|
+
# The provider's app-deploy adapter reads FABRIKA_CONTROL_DOMAIN as the selected app domain.
|
|
141
|
+
FABRIKA_CONTROL_DOMAIN: ${{ vars.OPERATIONS_HOSTNAME }}
|
|
142
|
+
FABRIKA_IAM_ISSUER: ${{ vars.FABRIKA_IAM_ISSUER }}
|
|
143
|
+
OPERATIONS_SYNC_KEY: ${{ secrets.OPERATIONS_SYNC_KEY }}
|
|
144
|
+
run: fabrika platform deploy --provider=cloudflare --env=prod --component-config=fabrika.config.ts
|
|
145
|
+
|
|
146
|
+
# ── Stage 3 — fabrika platform (executor + control plane) ────────────────────────────────────────
|
|
147
|
+
- name: Deploy fabrika runner + control plane
|
|
148
|
+
id: platform
|
|
149
|
+
working-directory: fabrika-platform
|
|
150
|
+
env:
|
|
151
|
+
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
|
152
|
+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
|
153
|
+
FABRIKA_CONTROL_VAULT_KEY: ${{ secrets.FABRIKA_CONTROL_VAULT_KEY }}
|
|
154
|
+
# The seeded IAM provisioning key — the control plane reconciles its own schema with it (Bearer px_).
|
|
155
|
+
FABRIKA_IAM_PROVISIONING_KEY: ${{ secrets.FABRIKA_IAM_PROVISIONING_KEY }}
|
|
156
|
+
OPERATIONS_SYNC_KEY: ${{ secrets.OPERATIONS_SYNC_KEY }}
|
|
157
|
+
# `wrangler secret put FABRIKA_RUNNER_KEY` on BOTH Workers this step deploys: the control plane
|
|
158
|
+
# presents it to the deploy runner, and the runner rejects an RPC caller without it. One step,
|
|
159
|
+
# one value, because a mismatched pair breaks every deploy.
|
|
160
|
+
FABRIKA_RUNNER_KEY: ${{ secrets.FABRIKA_RUNNER_KEY }}
|
|
161
|
+
# Non-secret config (Environment Variables).
|
|
162
|
+
FABRIKA_CONTROL_DOMAIN: ${{ vars.FABRIKA_CONTROL_DOMAIN }}
|
|
163
|
+
FABRIKA_IAM_ISSUER: ${{ vars.FABRIKA_IAM_ISSUER }}
|
|
164
|
+
OPERATIONS_ARTIFACT_ORIGIN: https://${{ vars.OPERATIONS_HOSTNAME }}
|
|
165
|
+
FABRIKA_CONTROL_BOOTSTRAP_ADMINS: ${{ vars.FABRIKA_CONTROL_BOOTSTRAP_ADMINS }}
|
|
166
|
+
run: |
|
|
167
|
+
fabrika platform deploy --provider=cloudflare \
|
|
168
|
+
--runner-config=packages/runner-cloudflare/fabrika-runner.config.ts \
|
|
169
|
+
--worker-config=packages/control/fabrika.config.ts
|
|
170
|
+
|
|
171
|
+
- name: Summary
|
|
172
|
+
if: always()
|
|
173
|
+
run: |
|
|
174
|
+
{
|
|
175
|
+
echo "### platform deploy → {{ACCOUNT}}"
|
|
176
|
+
echo ""
|
|
177
|
+
echo "- fabrika ref: \`${{ steps.ref.outputs.fabrika || '(unresolved)' }}\`"
|
|
178
|
+
echo "- platform deploy: \`${{ steps.platform.outcome || 'not run' }}\`"
|
|
179
|
+
echo "- runner image source: account-local build from the pinned fabrika checkout"
|
|
180
|
+
echo "- retired legacy propustka: \`${{ inputs.retire_legacy_propustka_worker || '(none)' }}\`"
|
|
181
|
+
} >> "$GITHUB_STEP_SUMMARY"
|
package/src/plan.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Worker } from 'oblaka-iac'
|
|
1
|
+
import { isProxyHost } from '@fabrika/proxy-core/manifest'
|
|
2
|
+
import { Container, D1Database, KVNamespace, Queue, R2Bucket, ServiceReference, Worker } from 'oblaka-iac'
|
|
3
3
|
import type { CloudflareAppConfig } from './authoring'
|
|
4
4
|
|
|
5
5
|
export type CloudflareStepKind = 'build' | 'provision-resources' | 'migrate' | 'deploy-worker' | 'reconcile-schema' | 'sync-secrets'
|
|
6
6
|
|
|
7
|
-
export interface
|
|
7
|
+
export interface CloudflareStep {
|
|
8
|
+
readonly id: string
|
|
8
9
|
readonly kind: CloudflareStepKind
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export interface CloudflarePlan extends ProviderDeployPlan {
|
|
12
|
-
readonly steps: readonly CloudflareJobSpec[]
|
|
10
|
+
readonly description: string
|
|
13
11
|
}
|
|
14
12
|
|
|
15
13
|
export interface MigratableDatabase {
|
|
@@ -49,44 +47,248 @@ export const findMigratableDatabases = (worker: Worker): MigratableDatabase[] =>
|
|
|
49
47
|
return databases
|
|
50
48
|
}
|
|
51
49
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Every resource name fabrika's own components declare (control, IAM, Operations, the runner and the
|
|
52
|
+
* installation's oblaka state). oblaka ADOPTS an existing `${env}-${name}`, so a checkout that names
|
|
53
|
+
* one would take over the platform's own Worker, database or bucket.
|
|
54
|
+
*/
|
|
55
|
+
export const PLATFORM_RESOURCE_NAMES: ReadonlySet<string> = new Set([
|
|
56
|
+
'vozka',
|
|
57
|
+
'vozka-proxy',
|
|
58
|
+
'vozka-runner',
|
|
59
|
+
'vozka-deploy',
|
|
60
|
+
'vozka-deploy-runner',
|
|
61
|
+
'vozka-run-logs',
|
|
62
|
+
'propustka',
|
|
63
|
+
'propustka-worker',
|
|
64
|
+
'propustka-state',
|
|
65
|
+
'operations',
|
|
66
|
+
'operations-proxy',
|
|
67
|
+
'operations-payloads',
|
|
68
|
+
'operations-ingest',
|
|
69
|
+
'operations-ingest-dlq',
|
|
70
|
+
])
|
|
71
|
+
|
|
72
|
+
const namedResource = (value: unknown): { readonly kind: string; readonly name: string } | null => {
|
|
73
|
+
if (value instanceof Worker) return { kind: 'Worker', name: value.options.name }
|
|
74
|
+
if (value instanceof D1Database) return { kind: 'D1 database', name: value.options.name }
|
|
75
|
+
if (value instanceof KVNamespace) return { kind: 'KV namespace', name: value.options.name }
|
|
76
|
+
if (value instanceof R2Bucket) return { kind: 'R2 bucket', name: value.options.name }
|
|
77
|
+
if (value instanceof Queue) return { kind: 'queue', name: value.options.name }
|
|
78
|
+
if (value instanceof Container) return { kind: 'container', name: value.options.name }
|
|
79
|
+
return null
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A `ServiceReference` binds `${env}-${name}` without declaring it, so it never reaches `namedResource`
|
|
84
|
+
* — it is how an app would call the control plane or the runner directly. IAM is the one exception:
|
|
85
|
+
* every app binds `propustka-worker` (`proxy.ts`), which is the platform's intended front door.
|
|
86
|
+
*/
|
|
87
|
+
const serviceReferenceTarget = (value: unknown): string | null => {
|
|
88
|
+
if (!(value instanceof ServiceReference)) return null
|
|
89
|
+
const target = Reflect.get(value, 'serviceName')
|
|
90
|
+
return typeof target === 'string' && target !== '' ? target : '(unreadable)'
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const routeHostname = (route: unknown): string | null => {
|
|
94
|
+
const pattern = typeof route === 'string' ? route : typeof route === 'object' && route !== null ? Reflect.get(route, 'pattern') : undefined
|
|
95
|
+
if (typeof pattern !== 'string') return null
|
|
96
|
+
const host = pattern.split('/')[0]
|
|
97
|
+
return host === undefined ? null : host.toLowerCase()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const applicationChain = (root: Worker): ReadonlySet<Worker> => {
|
|
101
|
+
const chain = new Set<Worker>()
|
|
102
|
+
let current: unknown = root
|
|
103
|
+
while (current instanceof Worker) {
|
|
104
|
+
chain.add(current)
|
|
105
|
+
current = current.options.bindings?.['APP']
|
|
106
|
+
}
|
|
107
|
+
return chain
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The only `Worker` options an app may set. oblaka's `ResourceProcessor` spreads everything but
|
|
112
|
+
* `dir`/`bindings`/`deleteDurableObjectsOnRemoval` verbatim into the generated wrangler config, so any
|
|
113
|
+
* other key — `services`, `durable_objects`, `r2_buckets`, `build` — binds or runs something the typed
|
|
114
|
+
* `bindings` walk never sees. Fail closed: a new wrangler key is refused until reviewed.
|
|
115
|
+
*/
|
|
116
|
+
const APP_WORKER_OPTIONS: ReadonlySet<string> = new Set([
|
|
117
|
+
'assets',
|
|
118
|
+
'bindings',
|
|
119
|
+
'compatibility_date',
|
|
120
|
+
'compatibility_flags',
|
|
121
|
+
'deleteDurableObjectsOnRemoval',
|
|
122
|
+
'dir',
|
|
123
|
+
'main',
|
|
124
|
+
'name',
|
|
125
|
+
'observability',
|
|
126
|
+
'preview_urls',
|
|
127
|
+
'routes',
|
|
128
|
+
'triggers',
|
|
129
|
+
'vars',
|
|
130
|
+
'workers_dev',
|
|
131
|
+
// Every other key is deliberately absent: `build` runs a command, the rest bind a resource by raw name.
|
|
132
|
+
])
|
|
133
|
+
|
|
134
|
+
/** Everything wrangler reads out of `assets`. `directory` is the only one that names a path. */
|
|
135
|
+
const APP_ASSETS_OPTIONS: ReadonlySet<string> = new Set(['binding', 'directory', 'html_handling', 'not_found_handling', 'run_worker_first'])
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* `assets.directory` is resolved against the generated `wrangler.jsonc`'s own directory and uploaded
|
|
139
|
+
* PUBLICLY: wrangler walks it recursively and ignores only `.assetsignore`/`_redirects`/`_headers`, so
|
|
140
|
+
* `.` on a root `workerDir` would publish the checkout's `.git/config` and the clone token in it.
|
|
141
|
+
*/
|
|
142
|
+
const isContainedAssetDirectory = (value: unknown): value is string => {
|
|
143
|
+
if (typeof value !== 'string' || value.startsWith('/')) return false
|
|
144
|
+
const segments = value.split('/')
|
|
145
|
+
if (segments.includes('..') || segments.includes('.git')) return false
|
|
146
|
+
return segments.some((segment) => segment !== '' && segment !== '.')
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** `assets` is the one raw key that names a path, so it is bounded rather than refused outright. */
|
|
150
|
+
const assertBoundedAssets = (worker: Worker): void => {
|
|
151
|
+
const assets: unknown = worker.options.assets
|
|
152
|
+
if (assets === undefined) return
|
|
153
|
+
if (typeof assets !== 'object' || assets === null || Array.isArray(assets)) {
|
|
154
|
+
throw new Error(`cloudflare: Worker \`${worker.options.name}\` sets \`assets\` to something that is not an object`)
|
|
155
|
+
}
|
|
156
|
+
for (const key of Object.keys(assets)) {
|
|
157
|
+
if (!APP_ASSETS_OPTIONS.has(key)) {
|
|
158
|
+
throw new Error(`cloudflare: Worker \`${worker.options.name}\` sets \`assets.${key}\`, which an app may not configure`)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const directory: unknown = Reflect.get(assets, 'directory')
|
|
162
|
+
if (!isContainedAssetDirectory(directory)) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`cloudflare: Worker \`${worker.options.name}\` sets \`assets.directory\` to \`${
|
|
165
|
+
typeof directory === 'string' ? directory : '(not a string)'
|
|
166
|
+
}\`; it must be a relative path strictly inside the worker directory, with no \`..\` or \`.git\` segment`,
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
interface PlatformAdoption {
|
|
172
|
+
readonly adopted: string
|
|
173
|
+
readonly platform: string
|
|
56
174
|
}
|
|
57
175
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Every oblaka resource kind adopts the remote name `${env}-${name}`, and an env name may contain `-`
|
|
178
|
+
* (`ENV_NAME` in `control.ts`), so an app in env `prod-propustka` naming `worker` lands on IAM's
|
|
179
|
+
* `prod-propustka-worker`. Bound the ADOPTED name, which holds wherever the env/name boundary falls.
|
|
180
|
+
*/
|
|
181
|
+
const platformAdoption = (env: string, name: string): PlatformAdoption | null => {
|
|
182
|
+
const adopted = `${env}-${name}`
|
|
183
|
+
for (const platform of PLATFORM_RESOURCE_NAMES) {
|
|
184
|
+
if (adopted === platform || adopted.endsWith(`-${platform}`)) {
|
|
185
|
+
return { adopted, platform }
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return null
|
|
189
|
+
}
|
|
65
190
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
191
|
+
export interface AuthoredGraphLimits {
|
|
192
|
+
/** The environment oblaka provisions into. Every remote name is `${env}-${name}`, so the bound is on that. */
|
|
193
|
+
readonly env: string
|
|
194
|
+
/** The one hostname this environment may route; a route on any other host belongs to another app. */
|
|
195
|
+
readonly domain?: string
|
|
196
|
+
/** The KV namespace oblaka keeps this deploy's state in; naming the platform's own would clobber it. */
|
|
197
|
+
readonly stateNamespace: string
|
|
198
|
+
/** Fabrika deploying its own components is the one caller allowed to name a platform resource. */
|
|
199
|
+
readonly platformResources: 'refuse' | 'allow'
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Refuse an authored graph before any Cloudflare call: the config comes from the checkout and the
|
|
204
|
+
* deploy holds an account-wide token, so the names and routes it may claim are bounded here (ADR-0014).
|
|
205
|
+
*/
|
|
206
|
+
export const assertAuthoredGraph = (root: Worker, limits: AuthoredGraphLimits): void => {
|
|
207
|
+
const deployable = applicationChain(root)
|
|
208
|
+
const refuse = limits.platformResources === 'refuse'
|
|
209
|
+
const assertAdoptable = (kind: string, name: string): void => {
|
|
210
|
+
const adoption = refuse ? platformAdoption(limits.env, name) : null
|
|
211
|
+
if (adoption !== null) {
|
|
212
|
+
throw new Error(
|
|
213
|
+
`cloudflare: ${kind} \`${name}\` becomes \`${adoption.adopted}\` on Cloudflare, which would take over the Fabrika platform resource \`${adoption.platform}\``,
|
|
214
|
+
)
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const visit = (worker: Worker): void => {
|
|
218
|
+
for (const key of refuse ? Object.keys(worker.options) : []) {
|
|
219
|
+
if (!APP_WORKER_OPTIONS.has(key)) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
`cloudflare: Worker \`${worker.options.name}\` sets \`${key}\`, which an app may not configure; declare resources in \`bindings\``,
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (refuse) {
|
|
226
|
+
assertBoundedAssets(worker)
|
|
227
|
+
}
|
|
228
|
+
for (const route of worker.options.routes ?? []) {
|
|
229
|
+
const host = routeHostname(route)
|
|
230
|
+
if (host === null || !isProxyHost(host) || host !== limits.domain?.toLowerCase()) {
|
|
231
|
+
throw new Error(`cloudflare: Worker \`${worker.options.name}\` routes \`${host ?? '(unreadable)'}\`, which is not this environment's domain`)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
for (const value of Object.values(worker.options.bindings ?? {})) {
|
|
235
|
+
const resource = namedResource(value)
|
|
236
|
+
if (resource !== null) {
|
|
237
|
+
assertAdoptable(resource.kind, resource.name)
|
|
238
|
+
}
|
|
239
|
+
const service = serviceReferenceTarget(value)
|
|
240
|
+
if (service !== null && refuse && service !== 'propustka-worker') {
|
|
241
|
+
if (service === '(unreadable)') {
|
|
242
|
+
throw new Error('cloudflare: service binding to `(unreadable)` cannot be checked against the Fabrika platform services')
|
|
243
|
+
}
|
|
244
|
+
assertAdoptable('service binding to', service)
|
|
245
|
+
}
|
|
246
|
+
if (value instanceof Worker) {
|
|
247
|
+
if (!deployable.has(value)) {
|
|
248
|
+
throw new Error(`cloudflare: config declares Worker \`${value.options.name}\`; only the application Worker and its proxy are deployable`)
|
|
249
|
+
}
|
|
250
|
+
visit(value)
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
assertAdoptable('Worker', root.options.name)
|
|
255
|
+
// The state namespace is the ONE name oblaka uses verbatim (`KVStateStorage`), so no env prefix applies.
|
|
256
|
+
if (refuse && PLATFORM_RESOURCE_NAMES.has(limits.stateNamespace)) {
|
|
257
|
+
throw new Error(`cloudflare: state namespace \`${limits.stateNamespace}\` is a Fabrika platform resource and cannot be used by an app`)
|
|
69
258
|
}
|
|
259
|
+
visit(root)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export const buildPlan = (config: CloudflareAppConfig, worker: Worker): readonly CloudflareStep[] => {
|
|
263
|
+
const steps: CloudflareStep[] = []
|
|
70
264
|
|
|
71
265
|
if (config.pipeline?.build !== undefined) {
|
|
72
|
-
|
|
266
|
+
steps.push({ id: 'build', kind: 'build', description: `Build the Worker (\`${config.pipeline.build}\`)` })
|
|
73
267
|
}
|
|
74
268
|
|
|
75
|
-
|
|
269
|
+
steps.push({ id: 'provision-resources', kind: 'provision-resources', description: 'Provision Cloudflare resources via oblaka' })
|
|
76
270
|
|
|
77
271
|
for (const database of findMigratableDatabases(worker)) {
|
|
78
|
-
|
|
272
|
+
steps.push({
|
|
273
|
+
id: `migrate:${database.binding}`,
|
|
274
|
+
kind: 'migrate',
|
|
275
|
+
description: `Apply D1 migrations for \`${database.binding}\` (${database.name})`,
|
|
276
|
+
})
|
|
79
277
|
}
|
|
80
278
|
|
|
81
|
-
|
|
279
|
+
steps.push({ id: 'deploy-worker', kind: 'deploy-worker', description: 'Deploy the Worker (`wrangler deploy`)' })
|
|
82
280
|
|
|
83
|
-
if (config.schema !== undefined
|
|
84
|
-
|
|
281
|
+
if (config.schema !== undefined) {
|
|
282
|
+
steps.push({ id: 'reconcile-schema', kind: 'reconcile-schema', description: 'Report the authz schema for the control plane to reconcile into IAM' })
|
|
85
283
|
}
|
|
86
284
|
|
|
87
285
|
if (config.pipeline?.secrets !== undefined && config.pipeline.secrets.length > 0) {
|
|
88
|
-
|
|
286
|
+
steps.push({
|
|
287
|
+
id: 'sync-secrets',
|
|
288
|
+
kind: 'sync-secrets',
|
|
289
|
+
description: `Sync ${config.pipeline.secrets.length} secret(s) via \`wrangler secret put\``,
|
|
290
|
+
})
|
|
89
291
|
}
|
|
90
292
|
|
|
91
|
-
return
|
|
293
|
+
return steps
|
|
92
294
|
}
|