alchemy 2.0.0-beta.14 → 2.0.0-beta.15
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/bin/alchemy.js +602 -596
- package/bin/alchemy.js.map +1 -1
- package/bin/alchemy.ts +2 -0
- package/bin/commands/deploy.ts +0 -1
- package/bin/commands/state.ts +8 -5
- package/lib/AWS/Lambda/BucketEventSource.d.ts +1 -1
- package/lib/AWS/Lambda/BucketEventSource.d.ts.map +1 -1
- package/lib/AWS/Lambda/EventBridgeEventSource.d.ts +1 -1
- package/lib/AWS/Lambda/EventBridgeEventSource.d.ts.map +1 -1
- package/lib/AWS/Lambda/QueueEventSource.d.ts +1 -1
- package/lib/AWS/Lambda/QueueEventSource.d.ts.map +1 -1
- package/lib/AWS/Lambda/StreamEventSource.d.ts +1 -1
- package/lib/AWS/Lambda/StreamEventSource.d.ts.map +1 -1
- package/lib/AWS/Lambda/TableEventSource.d.ts +1 -1
- package/lib/AWS/Lambda/TableEventSource.d.ts.map +1 -1
- package/lib/AWS/Lambda/TopicEventSource.d.ts +1 -1
- package/lib/AWS/Lambda/TopicEventSource.d.ts.map +1 -1
- package/lib/Agent/lsp/client.d.ts +2 -2
- package/lib/Cli/InkCLI.d.ts +0 -1
- package/lib/Cli/InkCLI.d.ts.map +1 -1
- package/lib/Cli/InkCLI.js +0 -5
- package/lib/Cli/InkCLI.js.map +1 -1
- package/lib/Cli/index.d.ts +0 -1
- package/lib/Cli/index.d.ts.map +1 -1
- package/lib/Cli/index.js +4 -1
- package/lib/Cli/index.js.map +1 -1
- package/lib/Cloudflare/ApiToken/AccountApiToken.d.ts +58 -0
- package/lib/Cloudflare/ApiToken/AccountApiToken.d.ts.map +1 -0
- package/lib/Cloudflare/ApiToken/AccountApiToken.js +137 -0
- package/lib/Cloudflare/ApiToken/AccountApiToken.js.map +1 -0
- package/lib/Cloudflare/ApiToken/Common.d.ts +91 -0
- package/lib/Cloudflare/ApiToken/Common.d.ts.map +1 -0
- package/lib/Cloudflare/ApiToken/Common.js +61 -0
- package/lib/Cloudflare/ApiToken/Common.js.map +1 -0
- package/lib/Cloudflare/ApiToken/PermissionGroups.d.ts +1549 -0
- package/lib/Cloudflare/ApiToken/PermissionGroups.d.ts.map +1 -0
- package/lib/Cloudflare/ApiToken/PermissionGroups.js +1856 -0
- package/lib/Cloudflare/ApiToken/PermissionGroups.js.map +1 -0
- package/lib/Cloudflare/ApiToken/UserApiToken.d.ts +49 -0
- package/lib/Cloudflare/ApiToken/UserApiToken.d.ts.map +1 -0
- package/lib/Cloudflare/ApiToken/UserApiToken.js +113 -0
- package/lib/Cloudflare/ApiToken/UserApiToken.js.map +1 -0
- package/lib/Cloudflare/ApiToken/index.d.ts +5 -0
- package/lib/Cloudflare/ApiToken/index.d.ts.map +1 -0
- package/lib/Cloudflare/ApiToken/index.js +5 -0
- package/lib/Cloudflare/ApiToken/index.js.map +1 -0
- package/lib/Cloudflare/Auth/AuthProvider.js +2 -2
- package/lib/Cloudflare/Auth/AuthProvider.js.map +1 -1
- package/lib/Cloudflare/Providers.d.ts.map +1 -1
- package/lib/Cloudflare/Providers.js +4 -1
- package/lib/Cloudflare/Providers.js.map +1 -1
- package/lib/Cloudflare/StateStore/Api.d.ts +7 -8
- package/lib/Cloudflare/StateStore/Api.d.ts.map +1 -1
- package/lib/Cloudflare/StateStore/Api.js +6 -4
- package/lib/Cloudflare/StateStore/Api.js.map +1 -1
- package/lib/Cloudflare/StateStore/State.d.ts +1 -1
- package/lib/Cloudflare/StateStore/State.d.ts.map +1 -1
- package/lib/Cloudflare/StateStore/Store.d.ts +2 -2
- package/lib/Cloudflare/StateStore/Store.d.ts.map +1 -1
- package/lib/Cloudflare/StateStore/Store.js +6 -4
- package/lib/Cloudflare/StateStore/Store.js.map +1 -1
- package/lib/Cloudflare/StateStore/Token.d.ts +6 -4
- package/lib/Cloudflare/StateStore/Token.d.ts.map +1 -1
- package/lib/Cloudflare/StateStore/Token.js +8 -4
- package/lib/Cloudflare/StateStore/Token.js.map +1 -1
- package/lib/Cloudflare/index.d.ts +1 -0
- package/lib/Cloudflare/index.d.ts.map +1 -1
- package/lib/Cloudflare/index.js +1 -0
- package/lib/Cloudflare/index.js.map +1 -1
- package/lib/Deploy.d.ts +1 -1
- package/lib/Deploy.d.ts.map +1 -1
- package/lib/Destroy.d.ts +1 -1
- package/lib/Destroy.d.ts.map +1 -1
- package/lib/GitHub/Providers.d.ts +3 -0
- package/lib/GitHub/Providers.d.ts.map +1 -0
- package/lib/GitHub/Providers.js +5 -0
- package/lib/GitHub/Providers.js.map +1 -0
- package/lib/GitHub/index.d.ts +1 -0
- package/lib/GitHub/index.d.ts.map +1 -1
- package/lib/GitHub/index.js +1 -0
- package/lib/GitHub/index.js.map +1 -1
- package/lib/Output.d.ts.map +1 -1
- package/lib/Output.js +4 -0
- package/lib/Output.js.map +1 -1
- package/lib/Plan.d.ts.map +1 -1
- package/lib/Plan.js +4 -0
- package/lib/Plan.js.map +1 -1
- package/lib/Stack.d.ts +5 -4
- package/lib/Stack.d.ts.map +1 -1
- package/lib/Stack.js +27 -8
- package/lib/Stack.js.map +1 -1
- package/lib/State/HttpStateStore.js +1 -1
- package/lib/State/HttpStateStore.js.map +1 -1
- package/lib/Test/Bun.d.ts +2 -2
- package/lib/Test/Bun.d.ts.map +1 -1
- package/lib/Test/Bun.js +2 -4
- package/lib/Test/Bun.js.map +1 -1
- package/lib/Test/Vitest.d.ts +1 -1
- package/lib/Test/Vitest.d.ts.map +1 -1
- package/lib/Test/Vitest.js +11 -7
- package/lib/Test/Vitest.js.map +1 -1
- package/lib/cli/index.d.ts +2 -6
- package/lib/cli/index.d.ts.map +1 -1
- package/lib/cli/index.js +2 -43050
- package/lib/cli/index.js.map +1 -1
- package/package.json +5 -5
- package/src/Cli/InkCLI.tsx +0 -10
- package/src/Cli/index.ts +4 -1
- package/src/Cloudflare/ApiToken/AccountApiToken.ts +197 -0
- package/src/Cloudflare/ApiToken/Common.ts +148 -0
- package/src/Cloudflare/ApiToken/PermissionGroups.ts +1910 -0
- package/src/Cloudflare/ApiToken/UserApiToken.ts +168 -0
- package/src/Cloudflare/ApiToken/index.ts +4 -0
- package/src/Cloudflare/Auth/AuthProvider.ts +2 -2
- package/src/Cloudflare/Providers.ts +5 -0
- package/src/Cloudflare/StateStore/Api.ts +6 -4
- package/src/Cloudflare/StateStore/Store.ts +6 -4
- package/src/Cloudflare/StateStore/Token.ts +10 -4
- package/src/Cloudflare/index.ts +1 -0
- package/src/GitHub/Providers.ts +6 -0
- package/src/GitHub/index.ts +1 -0
- package/src/Output.ts +3 -0
- package/src/Plan.ts +3 -0
- package/src/Stack.ts +33 -7
- package/src/State/HttpStateStore.ts +1 -0
- package/src/Test/Bun.ts +2 -2
- package/src/Test/Vitest.ts +11 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alchemy",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.15",
|
|
4
4
|
"homepage": "https://alchemy.run",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Sam Goodwin <sam@alchemy.run>",
|
|
@@ -231,11 +231,11 @@
|
|
|
231
231
|
"@ai-sdk/provider": "^3.0.8",
|
|
232
232
|
"@aws-sdk/credential-providers": "^3.0.0",
|
|
233
233
|
"@clack/prompts": "^0.11.0",
|
|
234
|
-
"@distilled.cloud/aws": "^0.
|
|
235
|
-
"@distilled.cloud/cloudflare": "^0.
|
|
234
|
+
"@distilled.cloud/aws": "^0.12.4",
|
|
235
|
+
"@distilled.cloud/cloudflare": "^0.12.4",
|
|
236
236
|
"@distilled.cloud/cloudflare-rolldown-plugin": "0.3.0",
|
|
237
237
|
"@distilled.cloud/cloudflare-vite-plugin": "0.2.0",
|
|
238
|
-
"@distilled.cloud/core": "^0.
|
|
238
|
+
"@distilled.cloud/core": "^0.12.4",
|
|
239
239
|
"@effect/vitest": "4.0.0-beta.48",
|
|
240
240
|
"@libsql/client": "^0.17.0",
|
|
241
241
|
"@octokit/rest": "^22.0.1",
|
|
@@ -255,7 +255,6 @@
|
|
|
255
255
|
"jszip": "^3.10.1",
|
|
256
256
|
"libsodium-wrappers": "^0.8.3",
|
|
257
257
|
"rolldown": "1.0.0-rc.13",
|
|
258
|
-
"solid-js": "latest",
|
|
259
258
|
"sonda": "^0.11.1",
|
|
260
259
|
"web-tree-sitter": "0.25.10",
|
|
261
260
|
"workerd": "1.20260417.1",
|
|
@@ -277,6 +276,7 @@
|
|
|
277
276
|
"pathe": "^2.0.3",
|
|
278
277
|
"react": "^19.2.0",
|
|
279
278
|
"react-devtools-core": "^7.0.1",
|
|
279
|
+
"solid-js": "latest",
|
|
280
280
|
"tsconfig-paths": "^4.2.0",
|
|
281
281
|
"tsdown": "^0.15.4",
|
|
282
282
|
"tsx": "^4.20.6",
|
package/src/Cli/InkCLI.tsx
CHANGED
|
@@ -19,16 +19,6 @@ export const inkCLI = () =>
|
|
|
19
19
|
}),
|
|
20
20
|
);
|
|
21
21
|
|
|
22
|
-
export const inkCLIAutoApprove = () =>
|
|
23
|
-
Layer.succeed(
|
|
24
|
-
Cli,
|
|
25
|
-
Cli.of({
|
|
26
|
-
approvePlan: () => Effect.succeed(true),
|
|
27
|
-
displayPlan,
|
|
28
|
-
startApplySession,
|
|
29
|
-
}),
|
|
30
|
-
);
|
|
31
|
-
|
|
32
22
|
const approvePlan = Effect.fn(function* <P extends Plan>(plan: P) {
|
|
33
23
|
let approved = false;
|
|
34
24
|
const { waitUntilExit } = render(
|
package/src/Cli/index.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
export * from "./Cli.ts";
|
|
2
|
-
|
|
2
|
+
// Note: do NOT re-export InkCLI here. The Ink-based renderer pulls in
|
|
3
|
+
// `react`/`ink`, which we do not want consumers of `alchemy/Cli` to
|
|
4
|
+
// transitively depend on. Import `alchemy/Cli/InkCLI` directly when needed
|
|
5
|
+
// (currently only from `bin/alchemy.ts`, which is bundled).
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import * as accounts from "@distilled.cloud/cloudflare/accounts";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Redacted from "effect/Redacted";
|
|
4
|
+
import { isResolved } from "../../Diff.ts";
|
|
5
|
+
import { createPhysicalName } from "../../PhysicalName.ts";
|
|
6
|
+
import * as Provider from "../../Provider.ts";
|
|
7
|
+
import { Resource } from "../../Resource.ts";
|
|
8
|
+
import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
|
|
9
|
+
import type { Providers } from "../Providers.ts";
|
|
10
|
+
import {
|
|
11
|
+
buildConditionPayload,
|
|
12
|
+
conditionFingerprint,
|
|
13
|
+
policyFingerprint,
|
|
14
|
+
resolvePolicies,
|
|
15
|
+
type BaseApiTokenProps,
|
|
16
|
+
} from "./Common.ts";
|
|
17
|
+
|
|
18
|
+
export type AccountApiTokenProps = BaseApiTokenProps;
|
|
19
|
+
|
|
20
|
+
export type AccountApiToken = Resource<
|
|
21
|
+
"Cloudflare.AccountApiToken",
|
|
22
|
+
AccountApiTokenProps,
|
|
23
|
+
{
|
|
24
|
+
tokenId: string;
|
|
25
|
+
name: string;
|
|
26
|
+
status: "active" | "disabled" | "expired";
|
|
27
|
+
/**
|
|
28
|
+
* The plaintext token value. Cloudflare returns this only once, on
|
|
29
|
+
* creation, so we persist it here for downstream consumers (e.g. a
|
|
30
|
+
* GitHub Actions secret).
|
|
31
|
+
*/
|
|
32
|
+
value: Redacted.Redacted<string>;
|
|
33
|
+
accountId: string;
|
|
34
|
+
},
|
|
35
|
+
never,
|
|
36
|
+
Providers
|
|
37
|
+
>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A Cloudflare account-owned API token (`POST /accounts/{account_id}/tokens`).
|
|
41
|
+
*
|
|
42
|
+
* Account-owned tokens are managed at the account level and persist
|
|
43
|
+
* independently of any single user. Use these for CI tokens, third-party
|
|
44
|
+
* integrations, or anywhere the token should outlive an individual user's
|
|
45
|
+
* session.
|
|
46
|
+
*
|
|
47
|
+
* Creating account-owned tokens requires the caller to have the
|
|
48
|
+
* `API Tokens > Write` account permission.
|
|
49
|
+
*
|
|
50
|
+
* @section Creating a Token
|
|
51
|
+
* @example A token for managing Workers and KV from CI
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const token = yield* Cloudflare.AccountApiToken("ci-token", {
|
|
54
|
+
* name: "my-ci-token",
|
|
55
|
+
* policies: [
|
|
56
|
+
* {
|
|
57
|
+
* effect: "allow",
|
|
58
|
+
* permissionGroups: [
|
|
59
|
+
* "Workers Scripts Write",
|
|
60
|
+
* "Workers KV Storage Write",
|
|
61
|
+
* ],
|
|
62
|
+
* resources: { "com.cloudflare.api.account": "*" },
|
|
63
|
+
* },
|
|
64
|
+
* ],
|
|
65
|
+
* });
|
|
66
|
+
*
|
|
67
|
+
* yield* GitHub.Secret("cf-api-token", {
|
|
68
|
+
* owner: "me",
|
|
69
|
+
* repository: "my-repo",
|
|
70
|
+
* name: "CLOUDFLARE_API_TOKEN",
|
|
71
|
+
* value: token.value,
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export const AccountApiToken = Resource<AccountApiToken>(
|
|
76
|
+
"Cloudflare.AccountApiToken",
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
type AccountApiTokenAttributes = AccountApiToken["Attributes"];
|
|
80
|
+
|
|
81
|
+
const resolveName = (id: string, name: string | undefined) =>
|
|
82
|
+
Effect.gen(function* () {
|
|
83
|
+
return name ?? (yield* createPhysicalName({ id }));
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
export const AccountApiTokenProvider = () =>
|
|
87
|
+
Provider.effect(
|
|
88
|
+
AccountApiToken,
|
|
89
|
+
Effect.gen(function* () {
|
|
90
|
+
const { accountId } = yield* CloudflareEnvironment;
|
|
91
|
+
const createToken = yield* accounts.createToken;
|
|
92
|
+
const updateToken = yield* accounts.updateToken;
|
|
93
|
+
const deleteToken = yield* accounts.deleteToken;
|
|
94
|
+
const getToken = yield* accounts.getToken;
|
|
95
|
+
|
|
96
|
+
const buildAttributes = (
|
|
97
|
+
tokenData: {
|
|
98
|
+
id?: string | null;
|
|
99
|
+
name?: string | null;
|
|
100
|
+
status?: "active" | "disabled" | "expired" | null;
|
|
101
|
+
},
|
|
102
|
+
value: Redacted.Redacted<string>,
|
|
103
|
+
): AccountApiTokenAttributes => ({
|
|
104
|
+
tokenId: tokenData.id ?? "",
|
|
105
|
+
name: tokenData.name ?? "",
|
|
106
|
+
status: tokenData.status ?? "active",
|
|
107
|
+
value,
|
|
108
|
+
accountId,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
stables: ["tokenId", "accountId"],
|
|
113
|
+
diff: Effect.fn(function* ({ id, olds, news, output }) {
|
|
114
|
+
if (!isResolved(news)) return undefined;
|
|
115
|
+
if ((output?.accountId ?? accountId) !== accountId) {
|
|
116
|
+
return { action: "replace" } as const;
|
|
117
|
+
}
|
|
118
|
+
const oldName = output?.name ?? (yield* resolveName(id, olds?.name));
|
|
119
|
+
const newName = yield* resolveName(id, news.name);
|
|
120
|
+
const oldPolicyFp = policyFingerprint(
|
|
121
|
+
resolvePolicies(olds?.policies ?? [], accountId),
|
|
122
|
+
);
|
|
123
|
+
const newPolicyFp = policyFingerprint(
|
|
124
|
+
resolvePolicies(news.policies, accountId),
|
|
125
|
+
);
|
|
126
|
+
const oldCondFp = conditionFingerprint(olds?.condition);
|
|
127
|
+
const newCondFp = conditionFingerprint(news.condition);
|
|
128
|
+
if (
|
|
129
|
+
oldName !== newName ||
|
|
130
|
+
oldPolicyFp !== newPolicyFp ||
|
|
131
|
+
oldCondFp !== newCondFp ||
|
|
132
|
+
(olds?.expiresOn ?? undefined) !== (news.expiresOn ?? undefined) ||
|
|
133
|
+
(olds?.notBefore ?? undefined) !== (news.notBefore ?? undefined)
|
|
134
|
+
) {
|
|
135
|
+
return { action: "update" } as const;
|
|
136
|
+
}
|
|
137
|
+
}),
|
|
138
|
+
create: Effect.fn(function* ({ id, news }) {
|
|
139
|
+
const name = yield* resolveName(id, news.name);
|
|
140
|
+
const policies = resolvePolicies(news.policies, accountId);
|
|
141
|
+
const result = yield* createToken({
|
|
142
|
+
accountId,
|
|
143
|
+
name,
|
|
144
|
+
policies,
|
|
145
|
+
condition: buildConditionPayload(news.condition),
|
|
146
|
+
expiresOn: news.expiresOn,
|
|
147
|
+
notBefore: news.notBefore,
|
|
148
|
+
});
|
|
149
|
+
if (!result.value) {
|
|
150
|
+
return yield* Effect.die(
|
|
151
|
+
`Cloudflare did not return a value for token "${name}".`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return buildAttributes(result, Redacted.make(result.value));
|
|
155
|
+
}),
|
|
156
|
+
update: Effect.fn(function* ({ id, news, output }) {
|
|
157
|
+
const name = yield* resolveName(id, news.name);
|
|
158
|
+
const policies = resolvePolicies(news.policies, accountId);
|
|
159
|
+
const result = yield* updateToken({
|
|
160
|
+
accountId,
|
|
161
|
+
tokenId: output.tokenId,
|
|
162
|
+
name,
|
|
163
|
+
policies,
|
|
164
|
+
condition: buildConditionPayload(news.condition),
|
|
165
|
+
expiresOn: news.expiresOn,
|
|
166
|
+
notBefore: news.notBefore,
|
|
167
|
+
});
|
|
168
|
+
// Cloudflare doesn't return the value on update; preserve the
|
|
169
|
+
// value captured at creation.
|
|
170
|
+
return buildAttributes(result, output.value);
|
|
171
|
+
}),
|
|
172
|
+
delete: Effect.fn(function* ({ output }) {
|
|
173
|
+
yield* deleteToken({
|
|
174
|
+
accountId: output.accountId,
|
|
175
|
+
tokenId: output.tokenId,
|
|
176
|
+
}).pipe(
|
|
177
|
+
// Already gone — Cloudflare may report this as either an
|
|
178
|
+
// `InvalidRoute` (token-id no longer routable) or a generic
|
|
179
|
+
// `TokenNotFound`. Either is fine; we just want the resource gone.
|
|
180
|
+
Effect.catchTag("InvalidRoute", () => Effect.void),
|
|
181
|
+
Effect.catchTag("TokenNotFound", () => Effect.void),
|
|
182
|
+
);
|
|
183
|
+
}),
|
|
184
|
+
read: Effect.fn(function* ({ output }) {
|
|
185
|
+
if (!output?.tokenId) return undefined;
|
|
186
|
+
return yield* getToken({
|
|
187
|
+
accountId: output.accountId,
|
|
188
|
+
tokenId: output.tokenId,
|
|
189
|
+
}).pipe(
|
|
190
|
+
Effect.map((token) => buildAttributes(token, output.value)),
|
|
191
|
+
Effect.catchTag("InvalidRoute", () => Effect.succeed(undefined)),
|
|
192
|
+
Effect.catchTag("TokenNotFound", () => Effect.succeed(undefined)),
|
|
193
|
+
);
|
|
194
|
+
}),
|
|
195
|
+
};
|
|
196
|
+
}),
|
|
197
|
+
);
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PERMISSION_GROUPS_BY_NAME,
|
|
3
|
+
type PermissionGroupName,
|
|
4
|
+
} from "./PermissionGroups.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Resource keys recognized by Cloudflare API token policies.
|
|
8
|
+
*
|
|
9
|
+
* The literal `"com.cloudflare.api.account"` is rewritten by the account-scoped
|
|
10
|
+
* provider to `"com.cloudflare.api.account.${accountId}"` so callers don't have
|
|
11
|
+
* to splice the account ID themselves.
|
|
12
|
+
*
|
|
13
|
+
* @see https://developers.cloudflare.com/fundamentals/api/reference/permissions/
|
|
14
|
+
*/
|
|
15
|
+
export type ApiTokenResourceKey =
|
|
16
|
+
| "com.cloudflare.api.account"
|
|
17
|
+
| `com.cloudflare.api.account.${string}`
|
|
18
|
+
| `com.cloudflare.api.account.zone.${string}`
|
|
19
|
+
| `com.cloudflare.edge.r2.bucket.${string}`
|
|
20
|
+
| (string & {});
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A permission group reference: either a typed Cloudflare permission-group
|
|
24
|
+
* name (resolved against the static catalog) or an explicit `{ id }` for
|
|
25
|
+
* names that aren't in the catalog or have multiple scopes.
|
|
26
|
+
*/
|
|
27
|
+
export type ApiTokenPermissionGroupRef =
|
|
28
|
+
| PermissionGroupName
|
|
29
|
+
| { id: string; meta?: { key?: string; value?: string } };
|
|
30
|
+
|
|
31
|
+
export interface ApiTokenPolicy {
|
|
32
|
+
effect: "allow" | "deny";
|
|
33
|
+
permissionGroups: ApiTokenPermissionGroupRef[];
|
|
34
|
+
resources: { [K in ApiTokenResourceKey]?: string };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ApiTokenCondition {
|
|
38
|
+
requestIp?: {
|
|
39
|
+
in?: string[];
|
|
40
|
+
notIn?: string[];
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type BaseApiTokenProps = {
|
|
45
|
+
/**
|
|
46
|
+
* Token name. Defaults to a generated physical name based on the
|
|
47
|
+
* resource's logical id, app name, and stage.
|
|
48
|
+
*/
|
|
49
|
+
name?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Access policies attached to the token. At least one policy is required
|
|
52
|
+
* by Cloudflare.
|
|
53
|
+
*/
|
|
54
|
+
policies: ApiTokenPolicy[];
|
|
55
|
+
/** ISO 8601 expiration timestamp. */
|
|
56
|
+
expiresOn?: string;
|
|
57
|
+
/** ISO 8601 "not before" timestamp. */
|
|
58
|
+
notBefore?: string;
|
|
59
|
+
/** Optional usage conditions (e.g. IP allowlist). */
|
|
60
|
+
condition?: ApiTokenCondition;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type ResolvedPolicy = {
|
|
64
|
+
effect: "allow" | "deny";
|
|
65
|
+
permissionGroups: { id: string; meta?: { key?: string; value?: string } }[];
|
|
66
|
+
resources: Record<string, unknown>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const resolvePermissionGroup = (ref: ApiTokenPermissionGroupRef) => {
|
|
70
|
+
if (typeof ref === "string") {
|
|
71
|
+
const group = PERMISSION_GROUPS_BY_NAME[ref];
|
|
72
|
+
if (!group) {
|
|
73
|
+
// Should be unreachable due to the typed union, but guard anyway in
|
|
74
|
+
// case Cloudflare retires a name we still have in the catalog.
|
|
75
|
+
throw new Error(
|
|
76
|
+
`Unknown Cloudflare permission group: "${ref}". Pass an explicit { id } instead.`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return { id: group.id };
|
|
80
|
+
}
|
|
81
|
+
return ref.meta ? { id: ref.id, meta: ref.meta } : { id: ref.id };
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Rewrite the bare `"com.cloudflare.api.account"` key to the
|
|
86
|
+
* account-scoped form. Pass `accountId: undefined` to leave keys unchanged
|
|
87
|
+
* (e.g. for user-owned tokens whose policies don't carry an account scope).
|
|
88
|
+
*/
|
|
89
|
+
export const resolveResources = (
|
|
90
|
+
resources: ApiTokenPolicy["resources"],
|
|
91
|
+
accountId: string | undefined,
|
|
92
|
+
): Record<string, string> => {
|
|
93
|
+
const out: Record<string, string> = {};
|
|
94
|
+
for (const [key, value] of Object.entries(resources)) {
|
|
95
|
+
if (value === undefined) continue;
|
|
96
|
+
if (key === "com.cloudflare.api.account" && accountId) {
|
|
97
|
+
out[`com.cloudflare.api.account.${accountId}`] = value;
|
|
98
|
+
} else {
|
|
99
|
+
out[key] = value;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const resolvePolicies = (
|
|
106
|
+
policies: ApiTokenPolicy[],
|
|
107
|
+
accountId: string | undefined,
|
|
108
|
+
): ResolvedPolicy[] =>
|
|
109
|
+
policies.map((policy) => ({
|
|
110
|
+
effect: policy.effect,
|
|
111
|
+
permissionGroups: policy.permissionGroups.map(resolvePermissionGroup),
|
|
112
|
+
resources: resolveResources(policy.resources, accountId),
|
|
113
|
+
}));
|
|
114
|
+
|
|
115
|
+
export const policyFingerprint = (policies: ResolvedPolicy[]): string =>
|
|
116
|
+
JSON.stringify(
|
|
117
|
+
policies.map((p) => ({
|
|
118
|
+
effect: p.effect,
|
|
119
|
+
permissionGroups: [...p.permissionGroups]
|
|
120
|
+
.map((g) => ({ id: g.id, meta: g.meta ?? null }))
|
|
121
|
+
.sort((a, b) => a.id.localeCompare(b.id)),
|
|
122
|
+
resources: Object.keys(p.resources)
|
|
123
|
+
.sort()
|
|
124
|
+
.map((k) => [k, p.resources[k]]),
|
|
125
|
+
})),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
export const conditionFingerprint = (
|
|
129
|
+
condition: ApiTokenCondition | undefined,
|
|
130
|
+
): string =>
|
|
131
|
+
JSON.stringify({
|
|
132
|
+
in: [...(condition?.requestIp?.in ?? [])].sort(),
|
|
133
|
+
notIn: [...(condition?.requestIp?.notIn ?? [])].sort(),
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
export const buildConditionPayload = (
|
|
137
|
+
condition: ApiTokenCondition | undefined,
|
|
138
|
+
) =>
|
|
139
|
+
condition
|
|
140
|
+
? {
|
|
141
|
+
requestIp: condition.requestIp
|
|
142
|
+
? {
|
|
143
|
+
in: condition.requestIp.in,
|
|
144
|
+
notIn: condition.requestIp.notIn,
|
|
145
|
+
}
|
|
146
|
+
: undefined,
|
|
147
|
+
}
|
|
148
|
+
: undefined;
|