@zabaca/zbc 0.23.0 → 0.24.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zabaca/zbc",
3
- "version": "0.23.0",
3
+ "version": "0.24.0",
4
4
  "description": "Zabaca stack CLI — scaffold and deploy infrastructure",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -891,9 +891,11 @@ the landing page render every Private sentence from. With it false, no document
891
891
  mentions readers, privacy or the credential helper.
892
892
 
893
893
  Readers need no account and no token: `@zabaca/agentgit` ships a credential
894
- helper, configured once per machine with `git config --global
895
- credential.https://<host>.helper '!agentgit credential'`, after which git needs
896
- nothing typed. A refused read answers **401 with the challenge** — never 404,
894
+ helper, configured once per machine with `agentgit setup` — an empty
895
+ `credential.https://<host>.helper` followed by `'!agentgit credential'`, the
896
+ empty one clearing any storing helper inherited from the system config, which
897
+ would otherwise replay an expired signature — after which git needs nothing
898
+ typed. A refused read answers **401 with the challenge** — never 404,
897
899
  because the name stopped being the secret when ownership landed — and the body
898
900
  names the helper and the by-hand exchange.
899
901
 
@@ -486,7 +486,8 @@ So configure the helper — *Read one*, below — **before** you write \`readers
486
486
  not after:
487
487
 
488
488
  \`\`\`sh
489
- git config --global credential.https://${host}.helper '!agentgit credential'
489
+ git config --global --replace-all credential.https://${host}.helper ''
490
+ git config --global --add credential.https://${host}.helper '!agentgit credential'
490
491
  \`\`\`
491
492
 
492
493
  Write one exactly as you wrote the Signer List — a signed push, judged by the
@@ -515,9 +516,16 @@ once per machine and git needs nothing typed afterwards — no account, no token
515
516
 
516
517
  \`\`\`sh
517
518
  bun add -g @zabaca/agentgit # or npm i -g
518
- git config --global credential.https://${host}.helper '!agentgit credential'
519
+ git config --global --replace-all credential.https://${host}.helper ''
520
+ git config --global --add credential.https://${host}.helper '!agentgit credential'
519
521
  \`\`\`
520
522
 
523
+ Both lines, the empty one first: it clears every helper git would otherwise
524
+ inherit for this host. A storing helper asked before agentgit (Homebrew's git
525
+ sets \`osxkeychain\` system-wide) saves the signature and replays it once the
526
+ challenge has moved on, so reads fail with \`Authentication failed\` every few
527
+ minutes and pass on the retry. \`agentgit setup\`, run in a clone, writes both.
528
+
521
529
  After that \`git clone\`, \`git fetch\`, \`git push\` and \`agentgit watch\` work on a
522
530
  Private repository with the key you already sign your pushes with. By hand, if you would
523
531
  rather see the exchange:
@@ -169,7 +169,7 @@ export function readAllowed({ enabled, claim, presented }: ReadRequest): boolean
169
169
  * Rendered rather than written as prose in `src/http.ts` for the reason every
170
170
  * other agent-facing document in this package is: the 401 is where discovery
171
171
  * actually lands — the moment it is relevant, on our server — so it names the
172
- * helper, the one config line, and the by-hand exchange for a reader who would
172
+ * helper, the two config lines, and the by-hand exchange for a reader who would
173
173
  * rather see the mechanism than install anything.
174
174
  *
175
175
  * `origin` is the host the agent TYPED (behind the Worker the request URL
@@ -185,7 +185,11 @@ export function renderReadChallenge(origin: string): string {
185
185
  '',
186
186
  'Once per machine, and then git needs nothing typed:',
187
187
  '',
188
- ` git config --global credential.${origin}.helper '!agentgit credential'`,
188
+ ` git config --global --replace-all credential.${origin}.helper ''`,
189
+ ` git config --global --add credential.${origin}.helper '!agentgit credential'`,
190
+ '',
191
+ 'Both lines: the empty helper clears any inherited from the system config',
192
+ '(osxkeychain), which would save the signature and replay it once expired.',
189
193
  '',
190
194
  'By hand, if you would rather see the exchange:',
191
195
  '',
@@ -70,18 +70,30 @@ import { cf, cfRaw } from '../cloudflare-api'
70
70
  // the account's plan; one absent from the zone's own list is an id Cloudflare
71
71
  // does not offer. Both refuse in the plan, ahead of the first record write —
72
72
  // discovering either from a half-applied PATCH would leave the zone in a state
73
- // neither the file nor the previous apply describes.
73
+ // neither the file nor the previous apply describes. The one thing that can
74
+ // already exist by then is the zone itself, when this apply created it, and
75
+ // the refusal says so.
74
76
  //
75
77
  // ── Account-parameterised ─────────────────────────────────────────────────
76
78
  // `accountId` is instance config, never a constant: each client's
77
79
  // zone in that Client's own Cloudflare account. Apply refuses outright if the
78
- // looked-up zone belongs to a different account than the one declared — the
80
+ // looked-up zone belongs to a different account than the one declared, the
79
81
  // cheapest possible guard against converging the right records into the wrong
80
82
  // tenant.
81
83
  //
82
- // There is deliberately no `destroy`. Deleting a zone detaches a live domain
83
- // from its nameservers and is registrar-adjacent; it is a human action with a
84
- // human's attention on it, not something a converge loop should be able to do.
84
+ // ── Creating the zone is opt-in ───────────────────────────────────────────
85
+ // A domain registered anywhere but Cloudflare Registrar has no zone until
86
+ // someone makes one. With `createIfAbsent: true` an absent zone is created
87
+ // (`type: full`, in the declared account) and the next apply finds it. Every
88
+ // apply prints the zone's nameservers and status as soon as the zone is
89
+ // resolved, before anything else can fail, because the pair is what an
90
+ // operator carries to the registrar.
91
+ //
92
+ // There is deliberately no `destroy`, and the asymmetry with creation is
93
+ // intended. A new zone answers for nothing until a registrar points at it;
94
+ // deleting one detaches a live domain from its nameservers and is
95
+ // registrar-adjacent, a human action with a human's attention on it, not
96
+ // something a converge loop should be able to do.
85
97
 
86
98
  // ── Schema ────────────────────────────────────────────────────────────────
87
99
 
@@ -223,6 +235,28 @@ const configSchema = z
223
235
  * records are always reported, never removed, until an operator opts in.
224
236
  */
225
237
  allowDelete: z.boolean().default(false),
238
+ /**
239
+ * Create the zone when the lookup does not find it.
240
+ *
241
+ * The creating apply needs "Zone Write" granted account-wide, on a token
242
+ * minted with no `zones` restriction: a grant scoped to a zone that does
243
+ * not exist yet cannot create it, and a `cloudflare-token` instance naming
244
+ * the zone in `zones` fails at mint time, with nothing to resolve the name
245
+ * to. Narrow the token to the zone once it exists, if wanted.
246
+ *
247
+ * Defaults to false, and an absent zone stays a hard error. An empty zone
248
+ * list is the answer to a mistyped name and to a token that lost "Zone
249
+ * Read" as much as to a domain nobody has added, so creation is one
250
+ * declared line per domain rather than what every empty lookup turns into.
251
+ *
252
+ * An account-owned token lists only its own account's zones, so a zone
253
+ * held in ANOTHER account reads as absent and the wrong-account refusal
254
+ * cannot fire for it. Creating then either fails (and the error says the
255
+ * zone may exist where this token cannot read it) or, if Cloudflare
256
+ * accepts it, makes a second zone here, pending, which answers for nothing
257
+ * unless the registrar is pointed at it.
258
+ */
259
+ createIfAbsent: z.boolean().default(false),
226
260
  })
227
261
  .superRefine((config, ctx) => {
228
262
  config.records.forEach((record, index) => {
@@ -655,13 +689,60 @@ function recordBody(record: DesiredRecord): Record<string, unknown> {
655
689
  return body
656
690
  }
657
691
 
692
+ interface CfZone {
693
+ id: string
694
+ name: string
695
+ /** `pending` until the registrar points at `name_servers`, then `active`. */
696
+ status?: string
697
+ account?: { id: string }
698
+ name_servers?: string[]
699
+ }
700
+
701
+ /**
702
+ * Create the zone in the declared account.
703
+ *
704
+ * `type: full`, not `partial`: a full setup is the one that gets its own
705
+ * nameserver pair, and moving the domain onto that pair is the point. A partial
706
+ * (CNAME) setup leaves the nameservers where they are.
707
+ *
708
+ * A failure is wrapped rather than passed through, because the likeliest one is
709
+ * not what it looks like. The zone list comes back EMPTY rather than forbidden
710
+ * for a token that cannot read the zone, so the lookup that decided this zone
711
+ * was absent cannot tell "no such zone" from "no permission to see it".
712
+ * Cloudflare catches the difference at the create, and the message has to say
713
+ * so, or the next reader concludes the account is empty and makes a second one.
714
+ */
715
+ async function createZone(token: string, name: string, accountId: string): Promise<CfZone> {
716
+ try {
717
+ return await cf<CfZone>(token, 'POST', '/zones', {
718
+ name,
719
+ account: { id: accountId },
720
+ type: 'full',
721
+ })
722
+ } catch (cause) {
723
+ throw new Error(
724
+ `refusing to apply: creating zone "${name}" in Cloudflare account ${accountId} failed: ` +
725
+ `${(cause as Error).message}. The lookup just before reported no such zone, but a token ` +
726
+ `that cannot read a zone gets an empty list rather than a refusal, so "absent" and ` +
727
+ `"invisible to this token" read the same. If Cloudflare says the zone already exists, it ` +
728
+ `exists somewhere this token cannot read: check the token holds "Zone Read" across the ` +
729
+ `account, and find which account holds the zone, rather than making a second one.`,
730
+ { cause },
731
+ )
732
+ }
733
+ }
734
+
658
735
  export const cloudflareZoneModule = defineModule({
659
736
  name: 'cloudflare-zone',
660
737
  configSchema,
661
738
  outputs: z.object({
662
739
  zoneId: z.string(),
663
740
  zoneName: z.string(),
664
- /** Cloudflare's assigned nameservers — what the registrar must be pointing at. */
741
+ /** True when this apply is the one that brought the zone into existence. */
742
+ zoneCreated: z.boolean(),
743
+ /** Cloudflare's word for the zone: `pending` until the registrar points here, then `active`. */
744
+ zoneStatus: z.string(),
745
+ /** Cloudflare's assigned nameservers, which the registrar must be pointing at. */
665
746
  nameServers: z.array(z.string()),
666
747
  created: z.number(),
667
748
  updated: z.number(),
@@ -677,25 +758,57 @@ export const cloudflareZoneModule = defineModule({
677
758
  async apply(config, ctx) {
678
759
  const token = ctx.output(config.apiToken, 'apiToken')
679
760
 
680
- // 1. Resolve the zone, and refuse a zone that lives somewhere else.
681
- const zones = await cf<
682
- Array<{ id: string; name: string; account?: { id: string }; name_servers?: string[] }>
683
- >(token, 'GET', `/zones?name=${encodeURIComponent(config.zone)}`)
684
- const zone = zones.find((z) => z.name.toLowerCase() === config.zone.toLowerCase())
685
- if (!zone) {
686
- throw new Error(
687
- `Zone "${config.zone}" not found — either it is not in this Cloudflare account, or the ` +
688
- `imported token lacks "Zone Read".`,
689
- )
690
- }
691
- if (zone.account && zone.account.id !== config.accountId) {
761
+ // 1. Resolve the zone: refuse one that lives somewhere else, and create one
762
+ // that is not there at all only when the instance opts in.
763
+ //
764
+ // The order matters. A zone this token can see in another account is
765
+ // refused BEFORE anything decides whether to create, so that refusal
766
+ // cannot turn into a second zone for the same name beside the first. A
767
+ // zone the token cannot see is a different case; see `createIfAbsent`.
768
+ const zones = await cf<CfZone[]>(token, 'GET', `/zones?name=${encodeURIComponent(config.zone)}`)
769
+ const found = zones.find((each) => each.name.toLowerCase() === config.zone.toLowerCase())
770
+ if (found?.account && found.account.id !== config.accountId) {
692
771
  throw new Error(
693
- `refusing to apply: zone "${config.zone}" lives in Cloudflare account ${zone.account.id}, ` +
772
+ `refusing to apply: zone "${config.zone}" lives in Cloudflare account ${found.account.id}, ` +
694
773
  `but this instance declares ${config.accountId}. Each zone belongs to exactly one ` +
695
774
  `account; converging the right records into the wrong tenant is not a ` +
696
775
  `mistake worth being able to make.`,
697
776
  )
698
777
  }
778
+ if (!found && !config.createIfAbsent) {
779
+ throw new Error(
780
+ `Zone "${config.zone}" not found. Either it is not in this Cloudflare account, or the ` +
781
+ `imported token lacks "Zone Read". Set createIfAbsent: true on this instance to create ` +
782
+ `it (the token then also needs "Zone Write" across the account).`,
783
+ )
784
+ }
785
+ const zone = found ?? (await createZone(token, config.zone, config.accountId))
786
+ const zoneCreated = found === undefined
787
+ if (zoneCreated) {
788
+ console.log(
789
+ ` Created zone ${config.zone} (${zone.id}) in Cloudflare account ${config.accountId}`,
790
+ )
791
+ }
792
+
793
+ // The nameserver pair, printed here rather than at the end so that an
794
+ // apply which creates the zone and then fails on a later step still hands
795
+ // the operator the pair to carry to the registrar.
796
+ const nameServers = zone.name_servers ?? []
797
+ const zoneStatus = zone.status ?? 'unknown'
798
+ if (nameServers.length > 0) {
799
+ console.log(` Nameservers (${zoneStatus}): ${nameServers.join(', ')}`)
800
+ if (zoneStatus !== 'active') {
801
+ console.log(
802
+ ` ${config.zone} answers from these only once its registrar points at them; until ` +
803
+ `then its records have no effect.`,
804
+ )
805
+ }
806
+ } else {
807
+ console.log(
808
+ ` Cloudflare reported no nameservers for ${config.zone} (status ${zoneStatus}). Read ` +
809
+ `them off the dashboard before touching the registrar.`,
810
+ )
811
+ }
699
812
 
700
813
  // 2. Diff the whole zone before mutating any of it.
701
814
  const desired = config.records.map((record) => desiredRecord(record, config.zone))
@@ -744,8 +857,12 @@ export const cloudflareZoneModule = defineModule({
744
857
  }
745
858
  throw new Error(
746
859
  `refusing to apply: zone "${config.zone}" cannot converge every declared setting. ` +
747
- `${parts.join('; ')}. Nothing was written — not the settings that could be, and not ` +
748
- `the records — so this zone is exactly as the last apply left it.`,
860
+ `${parts.join('; ')}. ` +
861
+ (zoneCreated
862
+ ? `This apply created the zone itself (${zoneStatus}) and wrote nothing else: not ` +
863
+ `the settings that could be, and not the records.`
864
+ : `Nothing was written, not the settings that could be and not the records, so ` +
865
+ `this zone is exactly as the last apply left it.`),
749
866
  )
750
867
  }
751
868
  }
@@ -797,7 +914,9 @@ export const cloudflareZoneModule = defineModule({
797
914
  return {
798
915
  zoneId: zone.id,
799
916
  zoneName: zone.name,
800
- nameServers: zone.name_servers ?? [],
917
+ zoneCreated,
918
+ zoneStatus,
919
+ nameServers,
801
920
  created: plan.create.length,
802
921
  updated: plan.update.length,
803
922
  deleted,
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "cloudflare-zone",
3
3
  "modules": ["cloudflare-api"],
4
- "description": "A DNS zone's records and settings — authoritative but delete-gated over records, forward-only over settings",
4
+ "description": "A DNS zone's records and settings: authoritative but delete-gated over records, forward-only over settings; creates the zone itself on opt-in",
5
5
  "files": [{ "path": "index.ts" }],
6
6
  "dependencies": {
7
7
  "zod": "^3.24.0"
8
8
  },
9
9
  "secrets": [],
10
- "instructions": "Reads the Cloudflare API through `../cloudflare-api`; needs a token with DNS write on the zone, plus Zone Settings Edit if the instance declares `settings`. Authoritative over the record types it declares: a record present in Cloudflare and absent here is reported, and deleting it takes an explicit opt-in, because a zone is the one place where converging by deletion can take a domain off the internet between two applies. The `settings` key converges zone-level settings (`always_use_https`, `ssl`, `min_tls_version`, `automatic_https_rewrites`) forward-only: what is declared is converged, what is not declared is never read or touched."
10
+ "instructions": "Reads the Cloudflare API through `../cloudflare-api`; needs a token with Zone Read (to look the zone up) and DNS Write on the zone, plus Zone Settings Read and Write if the instance declares `settings` (the module reads a setting before it patches one), plus, if it sets `createIfAbsent`, Zone Write across the account on a token minted with no `zones` restriction (a zone-scoped `cloudflare-token` cannot be minted for a zone that does not exist yet; narrow it once the zone does). With `createIfAbsent: true` an absent zone is created (type full) in the declared account; every apply prints the zone's nameservers and status, which is what the registrar must be pointed at. Authoritative over the record types it declares: a record present in Cloudflare and absent here is reported, and deleting it takes an explicit opt-in, because a zone is the one place where converging by deletion can take a domain off the internet between two applies. The `settings` key converges zone-level settings (`always_use_https`, `ssl`, `min_tls_version`, `automatic_https_rewrites`) forward-only: what is declared is converged, what is not declared is never read or touched."
11
11
  }