@notation/factory-cloudflare 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/image/Dockerfile +17 -0
- package/implementations/bindings.ts +8 -0
- package/implementations/code.ts +18 -0
- package/implementations/deployer.ts +212 -0
- package/implementations/index.ts +4 -0
- package/implementations/operator-auth.ts +47 -0
- package/implementations/router.ts +173 -0
- package/implementations/vault.ts +59 -0
- package/lib/access.ts +37 -0
- package/lib/artifacts.ts +74 -0
- package/lib/builds-token.ts +95 -0
- package/lib/cloudflare-api.ts +55 -0
- package/lib/outpost-workers.ts +41 -0
- package/lib/router-config.ts +27 -0
- package/package.json +39 -3
- package/resources/access.ts +74 -0
- package/resources/artifacts.ts +55 -0
- package/resources/builds-token.ts +55 -0
- package/resources/outpost-workers.ts +43 -0
- package/workers/build-watch.ts +128 -0
- package/workers/channel.ts +83 -0
- package/workers/cloudflare-workers.d.ts +34 -0
- package/workers/deployer.test.ts +97 -0
- package/workers/deployer.ts +106 -0
- package/workers/loader.ts +59 -0
- package/workers/outpost-api.ts +16 -0
- package/workers/outpost-settings.ts +23 -0
- package/workers/outpost-source.test.ts +44 -0
- package/workers/outpost-source.ts +59 -0
- package/workers/outpost.gen.ts +2 -0
- package/workers/outpost.ts +171 -0
- package/workers/outposts.ts +315 -0
- package/workers/router.ts +37 -0
- package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite-wal +0 -0
- package/workers/test/evaluate-host.ts +22 -0
- package/workers/test/wrangler.jsonc +7 -0
- package/workers/types.ts +44 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Notation Labs Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/image/Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
FROM oven/bun:1-debian
|
|
2
|
+
|
|
3
|
+
RUN apt-get update \
|
|
4
|
+
&& apt-get install -y --no-install-recommends ca-certificates curl git gh jq openssh-client ripgrep \
|
|
5
|
+
&& rm -rf /var/lib/apt/lists/*
|
|
6
|
+
|
|
7
|
+
# factory:runtimes
|
|
8
|
+
|
|
9
|
+
# Claude Code refuses --dangerously-skip-permissions as root.
|
|
10
|
+
RUN useradd --create-home --home-dir /data/home --shell /bin/bash factory \
|
|
11
|
+
&& chown -R factory:factory /data
|
|
12
|
+
USER factory
|
|
13
|
+
ENV HOME=/data/home
|
|
14
|
+
# Bun's image works in /home/bun/app, which the factory user can't read.
|
|
15
|
+
WORKDIR /data/home
|
|
16
|
+
|
|
17
|
+
EXPOSE 8080
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The name that the hub's Workers read the project's KV namespace as. */
|
|
2
|
+
export const KV_BINDING = "FACTORY_KV";
|
|
3
|
+
|
|
4
|
+
/** The name that the router reads the project's R2 bucket as. */
|
|
5
|
+
export const R2_BINDING = "FACTORY_BUCKET";
|
|
6
|
+
|
|
7
|
+
/** The name that the router reaches the deployer Worker by. */
|
|
8
|
+
export const DEPLOYER_BINDING = "FACTORY_DEPLOYER";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { bundle } from "@notation/build/resources";
|
|
3
|
+
|
|
4
|
+
/** The compatibility date of the hub's Workers. */
|
|
5
|
+
export const COMPATIBILITY_DATE = "2026-10-12";
|
|
6
|
+
|
|
7
|
+
/** The code of one of the hub's Workers, from `workers/<worker>.ts` in this package, which deploy bundles. */
|
|
8
|
+
export const workerBundle = (
|
|
9
|
+
id: string,
|
|
10
|
+
worker: "router" | "deployer" | "channel",
|
|
11
|
+
modules?: Record<string, string>,
|
|
12
|
+
) =>
|
|
13
|
+
bundle({
|
|
14
|
+
id,
|
|
15
|
+
platform: "workerd",
|
|
16
|
+
entry: fileURLToPath(new URL(`../workers/${worker}.ts`, import.meta.url)),
|
|
17
|
+
modules,
|
|
18
|
+
});
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { deferredOutputRef, type OutputRef, ResourceGroup } from "@notation/resource";
|
|
3
|
+
import {
|
|
4
|
+
type Account,
|
|
5
|
+
type Deployer as DeployerContract,
|
|
6
|
+
scopedName,
|
|
7
|
+
} from "@notation/factory";
|
|
8
|
+
import { kvJson } from "@notation/cloudflare/kv";
|
|
9
|
+
import {
|
|
10
|
+
$durableObject,
|
|
11
|
+
addBindings,
|
|
12
|
+
json,
|
|
13
|
+
type Worker,
|
|
14
|
+
type WorkerGroup,
|
|
15
|
+
worker,
|
|
16
|
+
workerLoader,
|
|
17
|
+
} from "@notation/cloudflare/workers";
|
|
18
|
+
import * as v from "valibot";
|
|
19
|
+
import { DEPLOYER_BINDING, KV_BINDING, R2_BINDING } from "./bindings.ts";
|
|
20
|
+
import { COMPATIBILITY_DATE, workerBundle } from "./code.ts";
|
|
21
|
+
import { ArtifactsNamespace, type ArtifactsNamespaceInstance } from "../resources/artifacts.ts";
|
|
22
|
+
import { BuildsToken, type BuildsTokenInstance } from "../resources/builds-token.ts";
|
|
23
|
+
import { OutpostWorkers } from "../resources/outpost-workers.ts";
|
|
24
|
+
import { OutpostSettingsSchema } from "../workers/outpost-settings.ts";
|
|
25
|
+
import type { Router } from "./router.ts";
|
|
26
|
+
import {
|
|
27
|
+
assertCloudflareSecret,
|
|
28
|
+
type CloudflareSecret,
|
|
29
|
+
type Vault,
|
|
30
|
+
} from "./vault.ts";
|
|
31
|
+
|
|
32
|
+
const deployerConfig = v.object({
|
|
33
|
+
/**
|
|
34
|
+
* The Cloudflare account that outpost Workers are created in.
|
|
35
|
+
* Defaults to `CLOUDFLARE_ACCOUNT_ID`.
|
|
36
|
+
*/
|
|
37
|
+
accountId: v.optional(v.string()),
|
|
38
|
+
/**
|
|
39
|
+
* The regions where outpost containers may run, such as `["WEUR"]`.
|
|
40
|
+
* Without it, Cloudflare starts a container in the location nearest to
|
|
41
|
+
* the request that has the image, which can be far from the repo's users.
|
|
42
|
+
*/
|
|
43
|
+
regions: v.optional(
|
|
44
|
+
v.array(v.picklist(["ENAM", "WNAM", "EEUR", "WEUR", "APAC", "SAM", "ME", "OC", "AFR"])),
|
|
45
|
+
),
|
|
46
|
+
/**
|
|
47
|
+
* How long an outpost container stays up with no run in progress, such as
|
|
48
|
+
* `"30s"`, `"10m"` or `"1h"`. A shorter time costs less, and makes more
|
|
49
|
+
* runs wait for a cold start.
|
|
50
|
+
*/
|
|
51
|
+
sleepAfter: v.optional(
|
|
52
|
+
v.pipe(
|
|
53
|
+
v.string(),
|
|
54
|
+
v.regex(/^[1-9]\d*[smh]$/, 'sleepAfter is a number of seconds, minutes or hours, such as "30s", "10m" or "1h".'),
|
|
55
|
+
),
|
|
56
|
+
"10m",
|
|
57
|
+
),
|
|
58
|
+
});
|
|
59
|
+
export type DeployerConfig = v.InferInput<typeof deployerConfig>;
|
|
60
|
+
|
|
61
|
+
export type DeployerOptions = DeployerConfig & {
|
|
62
|
+
router: Router;
|
|
63
|
+
vault: Vault;
|
|
64
|
+
accounts?: Account[];
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** A module whose default export is Factory's default outpost Dockerfile. */
|
|
68
|
+
const dockerfileModule = () =>
|
|
69
|
+
`export default ${JSON.stringify(readFileSync(new URL("../image/Dockerfile", import.meta.url), "utf8"))};`;
|
|
70
|
+
|
|
71
|
+
const workerRegistration = Symbol.for("@notation/cloudflare/WorkerDeclaration");
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A ref to the generated name of a Worker from `worker()`. Notation builds
|
|
75
|
+
* the Worker's script once the infra modules have loaded, and has no public
|
|
76
|
+
* API for its name yet, so this reads Notation's registration of the Worker.
|
|
77
|
+
*/
|
|
78
|
+
const scriptNameRef = (declaration: Worker) =>
|
|
79
|
+
deferredOutputRef({
|
|
80
|
+
resourceId: `worker:${declaration.id}`,
|
|
81
|
+
key: "ScriptName",
|
|
82
|
+
resolve: () => {
|
|
83
|
+
const entry = (declaration as unknown as Record<symbol, { build(): WorkerGroup } | undefined>)[workerRegistration];
|
|
84
|
+
if (!entry) throw new Error(`The Worker ${declaration.id} has no registration in @notation/cloudflare.`);
|
|
85
|
+
return entry.build().script;
|
|
86
|
+
},
|
|
87
|
+
}) as OutputRef<string>;
|
|
88
|
+
|
|
89
|
+
/** Leaves room in an outpost Worker's name for `-` and a 12-character hash, within a DNS label. */
|
|
90
|
+
const OUTPOST_PREFIX_MAX_LENGTH = 50;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* `new cloudflare.Deployer(...)`: a Worker that evaluates outposts' configs.
|
|
94
|
+
* For each repo's container, it commits the container's source to an
|
|
95
|
+
* Artifacts repo, and Workers Builds builds the image and deploys an outpost
|
|
96
|
+
* Worker that hosts the container. It builds with a user API token, which
|
|
97
|
+
* deploy asks you for once, and reports each build to the router. It binds
|
|
98
|
+
* the root key and the accounts' secrets. Destroying it deletes the outpost
|
|
99
|
+
* Workers and their container applications.
|
|
100
|
+
*/
|
|
101
|
+
export class Deployer
|
|
102
|
+
extends ResourceGroup<DeployerConfig>
|
|
103
|
+
implements DeployerContract
|
|
104
|
+
{
|
|
105
|
+
readonly router: Router;
|
|
106
|
+
readonly vault: Vault;
|
|
107
|
+
readonly accounts: Account[];
|
|
108
|
+
/** Holds each outpost Worker's source. */
|
|
109
|
+
readonly namespace: ArtifactsNamespaceInstance;
|
|
110
|
+
/** The user API token that the deployer builds outpost Workers with. */
|
|
111
|
+
readonly buildsToken: BuildsTokenInstance;
|
|
112
|
+
/** Holds the builds token's value. */
|
|
113
|
+
readonly buildsTokenSecret: CloudflareSecret;
|
|
114
|
+
readonly worker: Worker;
|
|
115
|
+
|
|
116
|
+
constructor(
|
|
117
|
+
id: string,
|
|
118
|
+
{ router, vault, accounts = [], ...config }: DeployerOptions,
|
|
119
|
+
) {
|
|
120
|
+
const {
|
|
121
|
+
regions,
|
|
122
|
+
sleepAfter,
|
|
123
|
+
accountId = process.env.CLOUDFLARE_ACCOUNT_ID,
|
|
124
|
+
} = v.parse(deployerConfig, config);
|
|
125
|
+
if (!accountId) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`The deployer ${id} needs the Cloudflare account to create outpost Workers in. Set CLOUDFLARE_ACCOUNT_ID, or pass accountId.`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
super("cloudflare/Deployer", id, config);
|
|
131
|
+
this.router = router;
|
|
132
|
+
this.vault = vault;
|
|
133
|
+
this.accounts = accounts;
|
|
134
|
+
|
|
135
|
+
this.namespace = this.add(
|
|
136
|
+
new ArtifactsNamespace(`artifacts:${id}-outposts`, { config: { AccountId: accountId } }),
|
|
137
|
+
);
|
|
138
|
+
this.buildsToken = this.add(
|
|
139
|
+
new BuildsToken(`token:${id}-builds`, { config: { AccountId: accountId, Name: scopedName(`${id}-builds`, 100) } }),
|
|
140
|
+
);
|
|
141
|
+
this.buildsTokenSecret = vault.secret({
|
|
142
|
+
id: `${id}-builds-token`,
|
|
143
|
+
value: this.buildsToken.credentials.Value,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const settingsKey = `outpost-settings/${id}`;
|
|
147
|
+
const prefix = scopedName(`${id}-outpost`, OUTPOST_PREFIX_MAX_LENGTH);
|
|
148
|
+
this.worker = worker({
|
|
149
|
+
id,
|
|
150
|
+
compatibilityDate: COMPATIBILITY_DATE,
|
|
151
|
+
// Cloudflare refuses a Worker's fetch of another workers.dev URL on the
|
|
152
|
+
// same account without it, and containers reply through the router's.
|
|
153
|
+
// isomorphic-git and memfs need Node's APIs.
|
|
154
|
+
compatibilityFlags: ["global_fetch_strictly_public", "nodejs_compat"],
|
|
155
|
+
code: workerBundle(`${id}-deployer`, "deployer", { "factory:dockerfile": dockerfileModule() }),
|
|
156
|
+
url: false,
|
|
157
|
+
bindings: () => ({
|
|
158
|
+
[KV_BINDING]: router.kv,
|
|
159
|
+
// Keeps outposts' worktrees and sessions while their containers sleep.
|
|
160
|
+
[R2_BINDING]: router.bucket,
|
|
161
|
+
// Runs outposts' configs, each in an isolate with no network.
|
|
162
|
+
LOADER: workerLoader(),
|
|
163
|
+
// Polls Workers Builds for the builds that the deployer starts. The
|
|
164
|
+
// class is a stand-in: Notation reads only its name, and the
|
|
165
|
+
// deployer's bundle exports the real one.
|
|
166
|
+
BUILDS: $durableObject({ id: `${id}-builds`, class: class BuildWatch {} }),
|
|
167
|
+
FACTORY_ROOT_KEY: router.rootKey.secret,
|
|
168
|
+
CLOUDFLARE_BUILDS_TOKEN: this.buildsTokenSecret.secret,
|
|
169
|
+
OUTPOST_SETTINGS_KEY: json(settingsKey),
|
|
170
|
+
ACCOUNTS: json(
|
|
171
|
+
Object.fromEntries(
|
|
172
|
+
accounts.map((a) => [
|
|
173
|
+
a.runtime.id,
|
|
174
|
+
{ secrets: Object.keys(a.secrets), ...(a.runtime.setup && { setup: a.runtime.setup }) },
|
|
175
|
+
]),
|
|
176
|
+
),
|
|
177
|
+
),
|
|
178
|
+
}),
|
|
179
|
+
});
|
|
180
|
+
this.add(
|
|
181
|
+
kvJson({
|
|
182
|
+
id: `${id}-outpost-settings`,
|
|
183
|
+
namespace: router.kv,
|
|
184
|
+
key: settingsKey,
|
|
185
|
+
schema: OutpostSettingsSchema,
|
|
186
|
+
value: {
|
|
187
|
+
accountId,
|
|
188
|
+
namespace: this.namespace.outputRef("Name"),
|
|
189
|
+
prefix,
|
|
190
|
+
deployer: scriptNameRef(this.worker),
|
|
191
|
+
router: router.url,
|
|
192
|
+
compatibilityDate: COMPATIBILITY_DATE,
|
|
193
|
+
sleepAfter,
|
|
194
|
+
...(regions && { regions }),
|
|
195
|
+
},
|
|
196
|
+
}),
|
|
197
|
+
);
|
|
198
|
+
this.add(
|
|
199
|
+
new OutpostWorkers(`outposts:${id}-outposts`, {
|
|
200
|
+
config: { AccountId: accountId, Prefix: prefix, Deployer: scriptNameRef(this.worker) },
|
|
201
|
+
}),
|
|
202
|
+
);
|
|
203
|
+
// The router hands the deployer the repos that a push or an install changed.
|
|
204
|
+
addBindings(router.worker, { [DEPLOYER_BINDING]: this.worker });
|
|
205
|
+
for (const account of accounts) {
|
|
206
|
+
for (const [name, secret] of Object.entries(account.secrets)) {
|
|
207
|
+
assertCloudflareSecret(secret);
|
|
208
|
+
addBindings(this.worker, { [name]: secret.secret });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ResourceGroup } from "@notation/resource";
|
|
2
|
+
import {
|
|
3
|
+
type OperatorAuth as OperatorAuthContract,
|
|
4
|
+
scopedName,
|
|
5
|
+
} from "@notation/factory";
|
|
6
|
+
import * as v from "valibot";
|
|
7
|
+
import {
|
|
8
|
+
AccessApplication,
|
|
9
|
+
type AccessApplicationInstance,
|
|
10
|
+
} from "../resources/access.ts";
|
|
11
|
+
import type { Router } from "./router.ts";
|
|
12
|
+
|
|
13
|
+
const operatorAuthConfig = v.object({ emails: v.array(v.string()) });
|
|
14
|
+
export type OperatorAuthConfig = v.InferInput<typeof operatorAuthConfig>;
|
|
15
|
+
|
|
16
|
+
export type OperatorAuthOptions = OperatorAuthConfig & { router: Router };
|
|
17
|
+
|
|
18
|
+
/** Operators sign in to Cloudflare Access, which decides who gets in. The router verifies Access's token before it returns an admin session. */
|
|
19
|
+
export class OperatorAuth
|
|
20
|
+
extends ResourceGroup<OperatorAuthConfig>
|
|
21
|
+
implements OperatorAuthContract
|
|
22
|
+
{
|
|
23
|
+
readonly provider = "cloudflare";
|
|
24
|
+
readonly application: AccessApplicationInstance;
|
|
25
|
+
|
|
26
|
+
constructor(id: string, { router, ...config }: OperatorAuthOptions) {
|
|
27
|
+
const { emails } = v.parse(operatorAuthConfig, config);
|
|
28
|
+
super("cloudflare/OperatorAuth", id, config);
|
|
29
|
+
this.application = this.add(
|
|
30
|
+
new AccessApplication(`${id}-access`, {
|
|
31
|
+
config: {
|
|
32
|
+
AccountId: router.kv.outputRef("AccountId"),
|
|
33
|
+
Name: scopedName(id, 255),
|
|
34
|
+
Domain: router.url.map((url) => `${new URL(url).host}/admin`),
|
|
35
|
+
Emails: emails,
|
|
36
|
+
},
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
router.store.contribute({
|
|
40
|
+
operators: {
|
|
41
|
+
provider: "cloudflare",
|
|
42
|
+
aud: this.application.outputRef("Aud"),
|
|
43
|
+
emails,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { type OutputRef, ResourceGroup } from "@notation/resource";
|
|
2
|
+
import {
|
|
3
|
+
bindingName,
|
|
4
|
+
RandomKey,
|
|
5
|
+
RouterSync,
|
|
6
|
+
type RandomKeyInstance,
|
|
7
|
+
type Router as RouterContract,
|
|
8
|
+
type RouterChannelOptions,
|
|
9
|
+
scopedName,
|
|
10
|
+
type VaultSecret,
|
|
11
|
+
} from "@notation/factory";
|
|
12
|
+
import { kvJson, type KVJsonGroup } from "@notation/cloudflare/kv";
|
|
13
|
+
import { kv, r2 } from "@notation/cloudflare/resources";
|
|
14
|
+
import {
|
|
15
|
+
addBindings,
|
|
16
|
+
type Bindings,
|
|
17
|
+
type Worker,
|
|
18
|
+
worker,
|
|
19
|
+
} from "@notation/cloudflare/workers";
|
|
20
|
+
import {
|
|
21
|
+
ROUTER_CONFIG_KEY,
|
|
22
|
+
RouterConfigSchema,
|
|
23
|
+
} from "../lib/router-config.ts";
|
|
24
|
+
import { KV_BINDING, R2_BINDING } from "./bindings.ts";
|
|
25
|
+
import { COMPATIBILITY_DATE, workerBundle } from "./code.ts";
|
|
26
|
+
import {
|
|
27
|
+
assertCloudflareSecret,
|
|
28
|
+
type CloudflareSecret,
|
|
29
|
+
type Vault,
|
|
30
|
+
} from "./vault.ts";
|
|
31
|
+
|
|
32
|
+
export type RouterOptions = { vault: Vault };
|
|
33
|
+
|
|
34
|
+
/** A Worker id from `parts`: lowercase letters, digits and single hyphens. */
|
|
35
|
+
const workerId = (...parts: string[]) =>
|
|
36
|
+
parts
|
|
37
|
+
.join("-")
|
|
38
|
+
.toLowerCase()
|
|
39
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
40
|
+
.replace(/^-|-$/g, "");
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* `new cloudflare.Router(...)`: a Worker, the hub's KV namespace and R2
|
|
44
|
+
* bucket, and the KV entry its config is contributed to. It generates the
|
|
45
|
+
* root key, and keeps it in the vault. Each connection's channel is a Worker
|
|
46
|
+
* of its own, which the router binds as a service.
|
|
47
|
+
*/
|
|
48
|
+
export class Router extends ResourceGroup implements RouterContract {
|
|
49
|
+
readonly vault: Vault;
|
|
50
|
+
readonly rootKeyValue: RandomKeyInstance;
|
|
51
|
+
readonly rootKey: CloudflareSecret;
|
|
52
|
+
readonly kv: kv.KVNamespaceInstance;
|
|
53
|
+
readonly bucket: r2.R2BucketInstance;
|
|
54
|
+
readonly worker: Worker;
|
|
55
|
+
readonly url: OutputRef<string>;
|
|
56
|
+
readonly store: KVJsonGroup<typeof RouterConfigSchema>;
|
|
57
|
+
/** The bundle of each provider's channel, which its connections share. */
|
|
58
|
+
readonly #channelBundles = new Map<
|
|
59
|
+
string,
|
|
60
|
+
{ module: string; bundle: ReturnType<typeof workerBundle> }
|
|
61
|
+
>();
|
|
62
|
+
|
|
63
|
+
constructor(id: string, { vault }: RouterOptions) {
|
|
64
|
+
super("cloudflare/Router", id);
|
|
65
|
+
this.vault = vault;
|
|
66
|
+
|
|
67
|
+
this.rootKeyValue = this.add(
|
|
68
|
+
new RandomKey(`${id}-root-key`, { config: { Bytes: 32 } }),
|
|
69
|
+
);
|
|
70
|
+
this.rootKey = vault.secret({
|
|
71
|
+
id: `${id}-root-key`,
|
|
72
|
+
value: this.rootKeyValue.credentials.Value,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
this.kv = this.add(
|
|
76
|
+
new kv.KVNamespace(`${id}-kv`, {
|
|
77
|
+
config: { Title: scopedName(id, 512) },
|
|
78
|
+
}),
|
|
79
|
+
);
|
|
80
|
+
this.bucket = this.add(
|
|
81
|
+
new r2.R2Bucket(`${id}-bucket`, {
|
|
82
|
+
config: {
|
|
83
|
+
BucketName: scopedName(workerId(id), 64),
|
|
84
|
+
Jurisdiction: "default",
|
|
85
|
+
StorageClass: "Standard",
|
|
86
|
+
},
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const router = worker({
|
|
91
|
+
id: workerId(id),
|
|
92
|
+
compatibilityDate: COMPATIBILITY_DATE,
|
|
93
|
+
code: workerBundle(`${id}-router`, "router"),
|
|
94
|
+
bindings: () => ({
|
|
95
|
+
[KV_BINDING]: this.kv,
|
|
96
|
+
[R2_BINDING]: this.bucket,
|
|
97
|
+
FACTORY_ROOT_KEY: this.rootKey.secret,
|
|
98
|
+
}),
|
|
99
|
+
});
|
|
100
|
+
this.worker = router;
|
|
101
|
+
this.url = router.url;
|
|
102
|
+
|
|
103
|
+
this.store = this.add(
|
|
104
|
+
kvJson({
|
|
105
|
+
id: `${id}-config`,
|
|
106
|
+
namespace: this.kv,
|
|
107
|
+
key: ROUTER_CONFIG_KEY,
|
|
108
|
+
schema: RouterConfigSchema,
|
|
109
|
+
// A hub with no connections and no operator auth contributes nothing.
|
|
110
|
+
value: {},
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
this.add(
|
|
115
|
+
new RouterSync(`${id}-sync`, {
|
|
116
|
+
config: {
|
|
117
|
+
Url: this.url,
|
|
118
|
+
Config: this.store.value
|
|
119
|
+
.outputRef("Value")
|
|
120
|
+
.map((value) => JSON.stringify(value)),
|
|
121
|
+
},
|
|
122
|
+
}),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
bind(secret: VaultSecret, name = bindingName(secret.id)): string {
|
|
127
|
+
assertCloudflareSecret(secret);
|
|
128
|
+
addBindings(this.worker, { [name]: secret.secret });
|
|
129
|
+
return name;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Runs the connection's channel as a Worker with no URL, which binds the
|
|
134
|
+
* channel's secrets and values under their own names. The router binds
|
|
135
|
+
* it as `CHANNEL_<connection>`.
|
|
136
|
+
*/
|
|
137
|
+
channel({ connection, module, secrets = {}, values = {} }: RouterChannelOptions) {
|
|
138
|
+
const provider = connection.split("/")[0]!;
|
|
139
|
+
const code = this.#channelBundle(provider, module);
|
|
140
|
+
const bindings: Bindings = { ...values };
|
|
141
|
+
for (const [name, secret] of Object.entries(secrets)) {
|
|
142
|
+
assertCloudflareSecret(secret);
|
|
143
|
+
bindings[name] = secret.secret;
|
|
144
|
+
}
|
|
145
|
+
const channel = worker({
|
|
146
|
+
id: workerId(this.id, connection),
|
|
147
|
+
compatibilityDate: COMPATIBILITY_DATE,
|
|
148
|
+
code,
|
|
149
|
+
url: false,
|
|
150
|
+
bindings: () => bindings,
|
|
151
|
+
});
|
|
152
|
+
const name = `CHANNEL_${bindingName(connection)}`;
|
|
153
|
+
addBindings(this.worker, { [name]: channel });
|
|
154
|
+
this.store.contribute({ connections: { [connection]: { channel: name } } });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
#channelBundle(provider: string, module: string) {
|
|
158
|
+
const existing = this.#channelBundles.get(provider);
|
|
159
|
+
if (existing) {
|
|
160
|
+
if (existing.module !== module) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
`The router ${this.id} runs the ${provider} channel from ${existing.module}, so it can't also run one from ${module}.`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
return existing.bundle;
|
|
166
|
+
}
|
|
167
|
+
const bundle = workerBundle(`${this.id}-channel-${provider}`, "channel", {
|
|
168
|
+
"factory:channel": `export { channel } from ${JSON.stringify(module)};`,
|
|
169
|
+
});
|
|
170
|
+
this.#channelBundles.set(provider, { module, bundle });
|
|
171
|
+
return bundle;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { ResourceGroup } from "@notation/resource";
|
|
2
|
+
import {
|
|
3
|
+
type Vault as VaultContract,
|
|
4
|
+
type VaultSecret,
|
|
5
|
+
type VaultSecretOptions,
|
|
6
|
+
} from "@notation/factory";
|
|
7
|
+
import { secretsStore } from "@notation/cloudflare/resources";
|
|
8
|
+
|
|
9
|
+
/** One vault secret, as a Secrets Store secret that holds a credential. */
|
|
10
|
+
export class CloudflareSecret extends ResourceGroup {
|
|
11
|
+
platform = "cloudflare";
|
|
12
|
+
readonly secret: secretsStore.SecretsStoreSecretInstance;
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
{ id, value }: VaultSecretOptions,
|
|
16
|
+
store: secretsStore.SecretsStoreInstance,
|
|
17
|
+
) {
|
|
18
|
+
super("cloudflare/Secret", id, { id });
|
|
19
|
+
this.secret = this.add(
|
|
20
|
+
new secretsStore.SecretsStoreSecret(`secret:${id}`, {
|
|
21
|
+
config: { Value: value },
|
|
22
|
+
dependencies: { store },
|
|
23
|
+
}),
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* `new cloudflare.Vault(id)`: the hub's secrets, in the account's Secrets
|
|
30
|
+
* Store. Notation names each secret `<app>--<stage>--secret-<id>-<instance>`.
|
|
31
|
+
* Values can't be read back through the API.
|
|
32
|
+
*/
|
|
33
|
+
export class Vault
|
|
34
|
+
extends ResourceGroup
|
|
35
|
+
implements VaultContract<CloudflareSecret>
|
|
36
|
+
{
|
|
37
|
+
readonly provider = "cloudflare";
|
|
38
|
+
readonly store: secretsStore.SecretsStoreInstance;
|
|
39
|
+
|
|
40
|
+
constructor(id: string) {
|
|
41
|
+
super("cloudflare/Vault", id);
|
|
42
|
+
this.store = this.add(new secretsStore.SecretsStore(`${id}-store`));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
secret(opts: VaultSecretOptions): CloudflareSecret {
|
|
46
|
+
return new CloudflareSecret(opts, this.store);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Throws unless `secret` came from a `cloudflare.Vault`. */
|
|
51
|
+
export function assertCloudflareSecret(
|
|
52
|
+
secret: VaultSecret,
|
|
53
|
+
): asserts secret is CloudflareSecret {
|
|
54
|
+
if (!(secret instanceof CloudflareSecret)) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`${secret.id} is not a secret from a cloudflare.Vault, so a Cloudflare Worker can't bind it.`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
package/lib/access.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** The Access API calls operator auth makes. Stubs. */
|
|
2
|
+
|
|
3
|
+
export type ApplicationKey = { AccountId: string; ApplicationId: string };
|
|
4
|
+
|
|
5
|
+
export const createApplication = async (_params: {
|
|
6
|
+
AccountId: string;
|
|
7
|
+
Name: string;
|
|
8
|
+
Domain: string;
|
|
9
|
+
Emails: string[];
|
|
10
|
+
}): Promise<{ ApplicationId: string }> => {
|
|
11
|
+
throw new Error(
|
|
12
|
+
"not implemented: create an Access application, with a policy allowing `Emails`",
|
|
13
|
+
);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const getApplication = async (
|
|
17
|
+
_key: ApplicationKey,
|
|
18
|
+
): Promise<{ Aud: string }> => {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"not implemented: read an Access application and its audience tag",
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const updateApplication = async (
|
|
25
|
+
_key: ApplicationKey,
|
|
26
|
+
_params: { Domain: string; Emails: string[] },
|
|
27
|
+
): Promise<void> => {
|
|
28
|
+
throw new Error(
|
|
29
|
+
"not implemented: update an Access application and its policy",
|
|
30
|
+
);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const deleteApplication = async (
|
|
34
|
+
_key: ApplicationKey,
|
|
35
|
+
): Promise<void> => {
|
|
36
|
+
throw new Error("not implemented: delete an Access application");
|
|
37
|
+
};
|
package/lib/artifacts.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { cloudflare, cloudflareOrNone } from "./cloudflare-api.ts";
|
|
2
|
+
|
|
3
|
+
const namespacePath = (accountId: string, namespace: string) =>
|
|
4
|
+
`/accounts/${accountId}/artifacts/namespaces/${encodeURIComponent(namespace)}`;
|
|
5
|
+
|
|
6
|
+
export type Repo = { id: string; name: string; remote: string };
|
|
7
|
+
|
|
8
|
+
export const createNamespace = (token: string, accountId: string, namespace: string) =>
|
|
9
|
+
cloudflare(token, "POST", `/accounts/${accountId}/artifacts/namespaces`, { namespace });
|
|
10
|
+
|
|
11
|
+
export const getNamespace = (token: string, accountId: string, namespace: string) =>
|
|
12
|
+
cloudflareOrNone(token, "GET", namespacePath(accountId, namespace));
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Deletes a namespace and its repos. Artifacts refuses to delete a namespace
|
|
16
|
+
* with repos in it, and deletes a repo after it answers, so the namespace's
|
|
17
|
+
* delete is retried until the repos are gone.
|
|
18
|
+
*/
|
|
19
|
+
export const deleteNamespace = async (token: string, accountId: string, namespace: string) => {
|
|
20
|
+
const path = namespacePath(accountId, namespace);
|
|
21
|
+
for (const repo of await listRepos(token, accountId, namespace)) {
|
|
22
|
+
await cloudflareOrNone(token, "DELETE", `${path}/repos/${encodeURIComponent(repo.name)}`);
|
|
23
|
+
}
|
|
24
|
+
for (let attempt = 1; ; attempt++) {
|
|
25
|
+
try {
|
|
26
|
+
await cloudflareOrNone(token, "DELETE", path);
|
|
27
|
+
return;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (attempt === 30) throw error;
|
|
30
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const listRepos = async (token: string, accountId: string, namespace: string) => {
|
|
36
|
+
const repos: { name: string }[] = [];
|
|
37
|
+
let cursor = "";
|
|
38
|
+
do {
|
|
39
|
+
const query = new URLSearchParams({ limit: "100", ...(cursor && { cursor }) });
|
|
40
|
+
const response = await fetch(
|
|
41
|
+
`https://api.cloudflare.com/client/v4${namespacePath(accountId, namespace)}/repos?${query}`,
|
|
42
|
+
{ headers: { authorization: `Bearer ${token}` } },
|
|
43
|
+
);
|
|
44
|
+
if (response.status === 404) return repos;
|
|
45
|
+
const body = (await response.json()) as {
|
|
46
|
+
result?: { name: string }[];
|
|
47
|
+
result_info?: { cursor?: string };
|
|
48
|
+
};
|
|
49
|
+
repos.push(...(body.result ?? []));
|
|
50
|
+
cursor = body.result_info?.cursor ?? "";
|
|
51
|
+
} while (cursor);
|
|
52
|
+
return repos;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** The repo `name` in `namespace`, which this creates if it doesn't exist. */
|
|
56
|
+
export const ensureRepo = async (token: string, accountId: string, namespace: string, name: string): Promise<Repo> => {
|
|
57
|
+
const path = namespacePath(accountId, namespace);
|
|
58
|
+
const found = await cloudflareOrNone<Repo>(token, "GET", `${path}/repos/${encodeURIComponent(name)}`);
|
|
59
|
+
if (found) return found;
|
|
60
|
+
return cloudflare<Repo>(token, "POST", `${path}/repos`, { name, default_branch: "main" });
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const deleteRepo = (token: string, accountId: string, namespace: string, name: string) =>
|
|
64
|
+
cloudflareOrNone(token, "DELETE", `${namespacePath(accountId, namespace)}/repos/${encodeURIComponent(name)}`);
|
|
65
|
+
|
|
66
|
+
/** A write token for the repo `name`, which lasts `ttl` seconds. */
|
|
67
|
+
export const repoToken = async (token: string, accountId: string, namespace: string, name: string, ttl = 600) =>
|
|
68
|
+
(
|
|
69
|
+
await cloudflare<{ plaintext: string }>(token, "POST", `${namespacePath(accountId, namespace)}/tokens`, {
|
|
70
|
+
repo: name,
|
|
71
|
+
scope: "write",
|
|
72
|
+
ttl,
|
|
73
|
+
})
|
|
74
|
+
).plaintext;
|