@theholocron/holocron-plugin-cloudflare 5.0.0-alpha.556 → 5.0.0-alpha.558

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/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { AuthError, DeployScriptConfig, DeployScriptResult, Deployment, DeploymentProject, DeploymentProjectSettings, DeploymentRecord, DeploymentTarget, DeploymentTrigger, Dns, DnsRecord, DnsRecordRequest, EnsureRelaySourceInput, EnsureRelaySourceResult, EnsuredTunnel, InspectRelaySourceInput, PluginToken, RelaySourceState, ResolveTokenInput, TunnelRoute, Tunnels, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess, WikiProxyConfig, WorkerDeploymentState, WorkerScriptState, Workers } from "@theholocron/cli";
1
+ import { AuthError, DeployScriptConfig, DeployScriptResult, Deployment, DeploymentProject, DeploymentProjectSettings, DeploymentRecord, DeploymentTarget, DeploymentTrigger, Dns, DnsRecord, DnsRecordRequest, EnsureRelaySourceInput, EnsureRelaySourceResult, EnsuredTunnel, InspectRelaySourceInput, MintTokenInput, PluginToken, RelaySourceState, ResolveTokenInput, TunnelRoute, Tunnels, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess, WikiProxyConfig, WorkerDeploymentState, WorkerScriptState, Workers } from "@theholocron/cli";
2
2
  import { CloudflareClient, CloudflareClient as CloudflareClient$1, CloudflareClientOptions, createCloudflareClient } from "@theholocron/cloudflare-client";
3
3
  /** Resolves the Cloudflare API token from `--token`, `HOLOCRON_CLOUDFLARE_TOKEN`, `CLOUDFLARE_API_TOKEN` or the keyring, and throws an `AuthError` when none is set. */
4
4
  export declare const resolveToken: import("@theholocron/cli").TokenResolver;
@@ -177,6 +177,8 @@ export declare function createPlugin(options?: CloudflarePluginOptions): {
177
177
  token: import("@theholocron/cli").Probe;
178
178
  connection: import("@theholocron/cli").Probe;
179
179
  };
180
+ whoami: import("@theholocron/cli").ProviderAction;
181
+ mintToken: (input: MintTokenInput) => Promise<import("@theholocron/cli").MintTokenResult>;
180
182
  capabilities: {
181
183
  deployment?: (() => Deployment) | undefined;
182
184
  workers?: (() => Workers) | undefined;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { AuthError, DEFAULT_TOKEN_ROTATION, ProviderApiError, connectionProbe, createResolveToken, resolveValue, tokenExpectations, tokenRotationRows } from "@theholocron/cli";
1
+ import { AuthError, DEFAULT_TOKEN_ROTATION, ProviderApiError, connectionProbe, createResolveToken, resolveValue, tokenExpectations, tokenRotationRows, whoamiAction } from "@theholocron/cli";
2
2
  import { PREFIX } from "@theholocron/env-utils/lookup";
3
3
  import { errorMessage } from "@theholocron/misc-utils";
4
4
  import { createCloudflareClient } from "@theholocron/cloudflare-client";
@@ -589,6 +589,94 @@ function generateProxyScript(config) {
589
589
  `};`
590
590
  ].join("\n");
591
591
  }
592
+ // src/mint-token/mint-token.ts
593
+ /**
594
+ * The Cloudflare permission group each declared permission needs, by the names Cloudflare has used for it, first match
595
+ * wins (the dashboard says `Cloudflare Pages: Edit`, the API's group is named differently, and neither is certain, so a
596
+ * name that matches none is an error that lists the near ones, never a guess).
597
+ */
598
+ const GROUPS = {
599
+ zone: {
600
+ resource: "zone",
601
+ names: ["Zone Read"]
602
+ },
603
+ dns: {
604
+ resource: "zone",
605
+ names: ["DNS Write"]
606
+ },
607
+ routes: {
608
+ resource: "zone",
609
+ names: ["Workers Routes Write"]
610
+ },
611
+ scripts: {
612
+ resource: "account",
613
+ names: ["Workers Scripts Write"]
614
+ },
615
+ pages: {
616
+ resource: "account",
617
+ names: ["Pages Write", "Cloudflare Pages Write"]
618
+ },
619
+ tunnels: {
620
+ resource: "account",
621
+ names: ["Cloudflare Tunnel Write", "Tunnel Write"]
622
+ },
623
+ notifications: {
624
+ resource: "account",
625
+ names: ["Notifications Write", "Account Notifications Write"]
626
+ }
627
+ };
628
+ const DAY_MS = 864e5;
629
+ /**
630
+ * Makes a Cloudflare API token for a layer, scoped to exactly the permissions the plugin declares it `required` there
631
+ * (`PluginToken.expect`), through `POST /user/tokens`. The calling token needs `API Tokens: Edit`. It refuses (nothing is
632
+ * made) a layer with no declared permissions, a missing account id, and a permission whose group Cloudflare does not
633
+ * list, so a token is never broader than the declaration. The value is returned, never printed.
634
+ */
635
+ async function mintCloudflareToken(ctx, input) {
636
+ const declared = tokenExpectations(tokens, { layer: input.layer })[0];
637
+ if (!declared || declared.required.length === 0) throw new Error(`the Cloudflare token declares no permissions for \`${input.layer}\`, so none can be made for it; a token with no declared scope is never made`);
638
+ const { accountId } = ctx.options;
639
+ if (!accountId) throw new Error("a Cloudflare accountId is required to make a token: set it in the plugin options or CLOUDFLARE_ACCOUNT_ID");
640
+ const client = ctx.client();
641
+ const available = await client.tokens.permissionGroups();
642
+ const wanted = declared.required.map((permission) => {
643
+ const group = GROUPS[permission];
644
+ const found = available.find((g) => group.names.includes(g.name));
645
+ if (!found) {
646
+ const near = available.filter((g) => g.name.toLowerCase().includes(permission.toLowerCase().slice(0, 4))).map((g) => g.name);
647
+ throw new Error(`Cloudflare has no permission group for \`${permission}\` (looked for ${group.names.join(" or ")})` + (near.length > 0 ? `; near names: ${near.slice(0, 5).join(", ")}` : ""));
648
+ }
649
+ return {
650
+ permission,
651
+ resource: group.resource,
652
+ id: found.id
653
+ };
654
+ });
655
+ const policies = ["account", "zone"].flatMap((resource) => {
656
+ const ids = wanted.filter((w) => w.resource === resource).map((w) => ({ id: w.id }));
657
+ if (ids.length === 0) return [];
658
+ return [{
659
+ effect: "allow",
660
+ resources: { [resource === "account" ? `com.cloudflare.api.account.${accountId}` : `com.cloudflare.api.account.zone.*`]: "*" },
661
+ permission_groups: ids
662
+ }];
663
+ });
664
+ const now = (input.now ?? (() => /* @__PURE__ */ new Date()))();
665
+ const days = input.expiresInDays ?? declared.rotation.maxAgeDays;
666
+ const created = await client.tokens.create({
667
+ name: input.name,
668
+ policies,
669
+ expires_on: new Date(now.getTime() + days * DAY_MS).toISOString()
670
+ });
671
+ if (!created.value) throw new Error("Cloudflare made the token but returned no value");
672
+ return {
673
+ value: created.value,
674
+ envName: resolveToken.token.envName,
675
+ id: created.id,
676
+ ...created.expires_on ? { expiresAt: created.expires_on } : {},
677
+ notes: [`scoped to ${wanted.map((w) => `${w.permission} (${w.resource})`).join(", ")}, the permissions declared for \`${input.layer}\``, ...wanted.some((w) => w.resource === "zone") ? ["zone permissions cover every zone in the account"] : []]
678
+ };
679
+ }
592
680
  // src/probes/token/parse-token-args.ts
593
681
  /** Reads the words after the probe name: `--layer <word>`, `--token-name <VAR>` and any number of zone names. */
594
682
  function parseTokenArgs(args) {
@@ -895,6 +983,27 @@ function createPlugin(options = {}) {
895
983
  })
896
984
  })
897
985
  },
986
+ whoami: whoamiAction({
987
+ provider: "cloudflare",
988
+ token: () => resolveToken(options),
989
+ verify: (token) => verifyToken(token, {
990
+ baseUrl: options.baseUrl,
991
+ fetch: options.fetch
992
+ }),
993
+ tokens,
994
+ lifetime: async (token) => {
995
+ const verified = await createCloudflareClient({
996
+ token,
997
+ baseUrl: options.baseUrl,
998
+ fetch: options.fetch
999
+ }).tokens.verify();
1000
+ return {
1001
+ ...verified.not_before ? { createdAt: verified.not_before } : {},
1002
+ ...verified.expires_on ? { expiresAt: verified.expires_on } : {}
1003
+ };
1004
+ }
1005
+ }),
1006
+ mintToken: (input) => mintCloudflareToken(ctx, input),
898
1007
  capabilities: {
899
1008
  dns: () => dns(ctx),
900
1009
  ...ctx.options.accountId ? {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-cloudflare",
3
- "version": "5.0.0-alpha.556",
3
+ "version": "5.0.0-alpha.558",
4
4
  "description": "Holocron plugin for Cloudflare. Implements the dns capability against Cloudflare's REST API — zone resolution, DNS record management.",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -44,11 +44,11 @@
44
44
  "tsx": "4.23.15",
45
45
  "typescript": "^6.0.3",
46
46
  "vitest": "^5.1.1",
47
- "@theholocron/cli": "5.0.0-alpha.556",
48
- "@theholocron/cloudflare-client": "^1.58.0-alpha.60",
47
+ "@theholocron/cli": "5.0.0-alpha.558",
48
+ "@theholocron/cloudflare-client": "^1.58.0-alpha.61",
49
49
  "@theholocron/eslint-config": "^8.9.0-alpha.24",
50
- "@theholocron/http-client": "^1.58.0-alpha.60",
51
- "@theholocron/kyber": "5.0.0-alpha.556",
50
+ "@theholocron/http-client": "^1.58.0-alpha.61",
51
+ "@theholocron/kyber": "5.0.0-alpha.558",
52
52
  "@theholocron/misc-utils": "^1.17.0-alpha.8",
53
53
  "@theholocron/tsconfig": "^8.9.0-alpha.24",
54
54
  "@theholocron/tsdown-config": "^8.9.0-alpha.24",
@@ -57,7 +57,7 @@
57
57
  "peerDependencies": {
58
58
  "@theholocron/cloudflare-client": "^1.53.1",
59
59
  "@theholocron/misc-utils": "^1.16.0",
60
- "@theholocron/cli": "5.0.0-alpha.556"
60
+ "@theholocron/cli": "5.0.0-alpha.558"
61
61
  },
62
62
  "peerDependenciesMeta": {
63
63
  "@theholocron/cloudflare-client": {}