@geekmidas/edge 10.0.0-alpha.189 → 10.0.0-alpha.190

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.
@@ -59,7 +59,8 @@ interface AcmeDnsChallenge {
59
59
  environment?: Readonly<Record<string, string>>;
60
60
  }
61
61
  /**
62
- * Every DNS provider the edge can get a wildcard certificate through, by the
62
+ * Every DNS provider the edge can get a certificate through over the DNS
63
+ * challenge — a wildcard's, or a host a CDN answers — by the
63
64
  * name a workspace's `dns` entry gives it. Another provider is one entry
64
65
  * here — lego's code for it and the variables it reads — and nothing else:
65
66
  * its resolver, its ACME storage and its edge environment follow from it.
@@ -81,6 +82,10 @@ declare const ACME_DNS_CHALLENGES: {
81
82
  readonly provider: 'godaddy';
82
83
  readonly secrets: readonly ["GODADDY_API_KEY", "GODADDY_API_SECRET"];
83
84
  };
85
+ readonly cloudflare: {
86
+ readonly provider: 'cloudflare';
87
+ readonly secrets: readonly ["CF_DNS_API_TOKEN"];
88
+ };
84
89
  };
85
90
  /** A DNS provider the edge answers the DNS challenge through. */
86
91
  type AcmeDnsProvider = keyof typeof ACME_DNS_CHALLENGES;
@@ -111,6 +116,14 @@ declare function edgeCertificatePaths(project: string): {
111
116
  certFile: string;
112
117
  keyFile: string;
113
118
  };
119
+ /**
120
+ * A stack's origin certificate — the one its CDN issued, served for the
121
+ * hosts the CDN answers — as the edge's container reads it.
122
+ */
123
+ declare function edgeOriginCertificatePaths(project: string): {
124
+ certFile: string;
125
+ keyFile: string;
126
+ };
114
127
  /** The edge's ports on the host. */
115
128
  interface EdgePorts {
116
129
  https: number;
@@ -317,7 +330,28 @@ interface EdgeRoute {
317
330
  * certificate is had. Named hosts always win over it.
318
331
  */
319
332
  wildcard?: EdgeWildcard;
333
+ /**
334
+ * Where its hosts' certificate comes from when it is not the stack's —
335
+ * for a route a CDN answers, whose hosts resolve to the CDN and not to
336
+ * this edge, so Let's Encrypt's HTTP request never reliably reaches it:
337
+ *
338
+ * - `files`: a certificate and key as the proxy reads them — one the CDN
339
+ * issued for its origin (Cloudflare's Origin CA), trusted by the CDN
340
+ * alone, so served for this route's hosts and nobody else's;
341
+ * - `dns`: Let's Encrypt over the DNS challenge, through this DNS
342
+ * provider. Rendered by Traefik; Caddy is never handed one.
343
+ */
344
+ certificate?: EdgeRouteCertificate;
320
345
  }
346
+ /** A route's own certificate — see {@link EdgeRoute.certificate}. */
347
+ type EdgeRouteCertificate = {
348
+ kind: 'files';
349
+ certFile: string;
350
+ keyFile: string;
351
+ } | {
352
+ kind: 'dns';
353
+ provider: AcmeDnsProvider;
354
+ };
321
355
  /**
322
356
  * Every host a route answers on — its own, its aliases and its redirects —
323
357
  * in that order: what DNS, certificates and the edge's claims cover.
@@ -423,5 +457,5 @@ declare const EDGE_LIMITS: {
423
457
  /** The proxies a stack can be served by — `deploy.compose.proxy`. */
424
458
  type ComposeProxy = 'caddy' | 'traefik';
425
459
  //#endregion
426
- export { EdgeClaim as A, edgeComposeFile as B, EDGE_AWS_CREDENTIALS as C, EDGE_PROJECT as D, EDGE_NETWORK as E, claimsOverlap as F, traefikDynamic as G, edgeProject as H, edgeAlias as I, traefikStaticFile as J, traefikDynamicFile as K, edgeCertificatePaths as L, TRAEFIK_IMAGE as M, TraefikStackOptions as N, EDGE_PROJECT_ENV as O, acmeDnsResolver as P, edgeClaims as R, DEFAULT_EDGE_PROJECT as S, EDGE_DYNAMIC_DIR as T, isAcmeDnsProvider as U, edgeDnsSecretPath as V, readEdgeClaims as W, ACME_DNS_RESOLVER as _, EdgeRouteConflict as a, AcmeDnsChallenge as b, HOST_GATEWAY as c, ServiceUpstream as d, Upstream as f, ACME_DNS_CHALLENGES as g, upstreamAddress as h, EdgeRoute as i, EdgePorts as j, EDGE_SERVICE as k, HostUpstream as l, routeHosts as m, EDGE_LIMITS as n, EdgeTls as o, assertRouteRenderable as p, traefikStatic as q, EdgeHandler as r, EdgeWildcard as s, ComposeProxy as t, NOFILE as u, ACME_DNS_SECRET_KEYS as v, EDGE_CERTS_DIR as w, AcmeDnsProvider as x, ACME_RESOLVER as y, edgeCompose as z };
427
- //# sourceMappingURL=index-BVE0VFku.d.cts.map
460
+ export { EDGE_SERVICE as A, edgeCompose as B, DEFAULT_EDGE_PROJECT as C, EDGE_NETWORK as D, EDGE_DYNAMIC_DIR as E, acmeDnsResolver as F, isAcmeDnsProvider as G, edgeDnsSecretPath as H, claimsOverlap as I, traefikDynamicFile as J, readEdgeClaims as K, edgeAlias as L, EdgePorts as M, TRAEFIK_IMAGE as N, EDGE_PROJECT as O, TraefikStackOptions as P, edgeCertificatePaths as R, AcmeDnsProvider as S, EDGE_CERTS_DIR as T, edgeOriginCertificatePaths as U, edgeComposeFile as V, edgeProject as W, traefikStaticFile as X, traefikStatic as Y, ACME_DNS_CHALLENGES as _, EdgeRouteCertificate as a, ACME_RESOLVER as b, EdgeWildcard as c, NOFILE as d, ServiceUpstream as f, upstreamAddress as g, routeHosts as h, EdgeRoute as i, EdgeClaim as j, EDGE_PROJECT_ENV as k, HOST_GATEWAY as l, assertRouteRenderable as m, EDGE_LIMITS as n, EdgeRouteConflict as o, Upstream as p, traefikDynamic as q, EdgeHandler as r, EdgeTls as s, ComposeProxy as t, HostUpstream as u, ACME_DNS_RESOLVER as v, EDGE_AWS_CREDENTIALS as w, AcmeDnsChallenge as x, ACME_DNS_SECRET_KEYS as y, edgeClaims as z };
461
+ //# sourceMappingURL=index-BhrEC4ux.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-BhrEC4ux.d.cts","names":[],"sources":["../src/proxy/traefik.ts","../src/routes/index.ts"],"mappings":";;;;;cAwCa;;cAGA;;;;;;;;cASA;;iBAGG,YAAY,MAAK,OAAO;;;;;cAQ3B;;;;;cAMA;;cAGA;;cAGA;cACA;;cAGA;;;;;;;cAQA;;;;;UAMI;;EAEhB;;;;;;;;EAQA;;EAEA,cAAc,SAAS;;;;;;;;;;;;;cAcX;;aAEX;aACA;aACA;eACC;e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U;;IACvB;IAAmB;;;;;;;;;;;EAUxB;IAAU,UAAU;IAAU;;;;UAId;;EAEhB;;EAEA;EACA,oBAAoB;;;;;;EAMpB;;;cAIY,0BAA0B;WACjB;EAArB,YAAqB;;;iBAWN,sBAAsB,OAAO;;KAUjC;;;EAEP;;;;;;;;EAMA;EAAe;EAAkB;;;cAGzB;EAAW;EAAiB;;;;;;;;cAQ5B;EACZ,SAAS;EACT,SAAS;IAAiB;IAAc;;;;KAO7B"}
@@ -59,7 +59,8 @@ interface AcmeDnsChallenge {
59
59
  environment?: Readonly<Record<string, string>>;
60
60
  }
61
61
  /**
62
- * Every DNS provider the edge can get a wildcard certificate through, by the
62
+ * Every DNS provider the edge can get a certificate through over the DNS
63
+ * challenge — a wildcard's, or a host a CDN answers — by the
63
64
  * name a workspace's `dns` entry gives it. Another provider is one entry
64
65
  * here — lego's code for it and the variables it reads — and nothing else:
65
66
  * its resolver, its ACME storage and its edge environment follow from it.
@@ -81,6 +82,10 @@ declare const ACME_DNS_CHALLENGES: {
81
82
  readonly provider: 'godaddy';
82
83
  readonly secrets: readonly ["GODADDY_API_KEY", "GODADDY_API_SECRET"];
83
84
  };
85
+ readonly cloudflare: {
86
+ readonly provider: 'cloudflare';
87
+ readonly secrets: readonly ["CF_DNS_API_TOKEN"];
88
+ };
84
89
  };
85
90
  /** A DNS provider the edge answers the DNS challenge through. */
86
91
  type AcmeDnsProvider = keyof typeof ACME_DNS_CHALLENGES;
@@ -111,6 +116,14 @@ declare function edgeCertificatePaths(project: string): {
111
116
  certFile: string;
112
117
  keyFile: string;
113
118
  };
119
+ /**
120
+ * A stack's origin certificate — the one its CDN issued, served for the
121
+ * hosts the CDN answers — as the edge's container reads it.
122
+ */
123
+ declare function edgeOriginCertificatePaths(project: string): {
124
+ certFile: string;
125
+ keyFile: string;
126
+ };
114
127
  /** The edge's ports on the host. */
115
128
  interface EdgePorts {
116
129
  https: number;
@@ -317,7 +330,28 @@ interface EdgeRoute {
317
330
  * certificate is had. Named hosts always win over it.
318
331
  */
319
332
  wildcard?: EdgeWildcard;
333
+ /**
334
+ * Where its hosts' certificate comes from when it is not the stack's —
335
+ * for a route a CDN answers, whose hosts resolve to the CDN and not to
336
+ * this edge, so Let's Encrypt's HTTP request never reliably reaches it:
337
+ *
338
+ * - `files`: a certificate and key as the proxy reads them — one the CDN
339
+ * issued for its origin (Cloudflare's Origin CA), trusted by the CDN
340
+ * alone, so served for this route's hosts and nobody else's;
341
+ * - `dns`: Let's Encrypt over the DNS challenge, through this DNS
342
+ * provider. Rendered by Traefik; Caddy is never handed one.
343
+ */
344
+ certificate?: EdgeRouteCertificate;
320
345
  }
346
+ /** A route's own certificate — see {@link EdgeRoute.certificate}. */
347
+ type EdgeRouteCertificate = {
348
+ kind: 'files';
349
+ certFile: string;
350
+ keyFile: string;
351
+ } | {
352
+ kind: 'dns';
353
+ provider: AcmeDnsProvider;
354
+ };
321
355
  /**
322
356
  * Every host a route answers on — its own, its aliases and its redirects —
323
357
  * in that order: what DNS, certificates and the edge's claims cover.
@@ -423,5 +457,5 @@ declare const EDGE_LIMITS: {
423
457
  /** The proxies a stack can be served by — `deploy.compose.proxy`. */
424
458
  type ComposeProxy = 'caddy' | 'traefik';
425
459
  //#endregion
426
- export { EdgeClaim as A, edgeComposeFile as B, EDGE_AWS_CREDENTIALS as C, EDGE_PROJECT as D, EDGE_NETWORK as E, claimsOverlap as F, traefikDynamic as G, edgeProject as H, edgeAlias as I, traefikStaticFile as J, traefikDynamicFile as K, edgeCertificatePaths as L, TRAEFIK_IMAGE as M, TraefikStackOptions as N, EDGE_PROJECT_ENV as O, acmeDnsResolver as P, edgeClaims as R, DEFAULT_EDGE_PROJECT as S, EDGE_DYNAMIC_DIR as T, isAcmeDnsProvider as U, edgeDnsSecretPath as V, readEdgeClaims as W, ACME_DNS_RESOLVER as _, EdgeRouteConflict as a, AcmeDnsChallenge as b, HOST_GATEWAY as c, ServiceUpstream as d, Upstream as f, ACME_DNS_CHALLENGES as g, upstreamAddress as h, EdgeRoute as i, EdgePorts as j, EDGE_SERVICE as k, HostUpstream as l, routeHosts as m, EDGE_LIMITS as n, EdgeTls as o, assertRouteRenderable as p, traefikStatic as q, EdgeHandler as r, EdgeWildcard as s, ComposeProxy as t, NOFILE as u, ACME_DNS_SECRET_KEYS as v, EDGE_CERTS_DIR as w, AcmeDnsProvider as x, ACME_RESOLVER as y, edgeCompose as z };
427
- //# sourceMappingURL=index-BVE0VFku.d.mts.map
460
+ export { EDGE_SERVICE as A, edgeCompose as B, DEFAULT_EDGE_PROJECT as C, EDGE_NETWORK as D, EDGE_DYNAMIC_DIR as E, acmeDnsResolver as F, isAcmeDnsProvider as G, edgeDnsSecretPath as H, claimsOverlap as I, traefikDynamicFile as J, readEdgeClaims as K, edgeAlias as L, EdgePorts as M, TRAEFIK_IMAGE as N, EDGE_PROJECT as O, TraefikStackOptions as P, edgeCertificatePaths as R, AcmeDnsProvider as S, EDGE_CERTS_DIR as T, edgeOriginCertificatePaths as U, edgeComposeFile as V, edgeProject as W, traefikStaticFile as X, traefikStatic as Y, ACME_DNS_CHALLENGES as _, EdgeRouteCertificate as a, ACME_RESOLVER as b, EdgeWildcard as c, NOFILE as d, ServiceUpstream as f, upstreamAddress as g, routeHosts as h, EdgeRoute as i, EdgeClaim as j, EDGE_PROJECT_ENV as k, HOST_GATEWAY as l, assertRouteRenderable as m, EDGE_LIMITS as n, EdgeRouteConflict as o, Upstream as p, traefikDynamic as q, EdgeHandler as r, EdgeTls as s, ComposeProxy as t, HostUpstream as u, ACME_DNS_RESOLVER as v, EDGE_AWS_CREDENTIALS as w, AcmeDnsChallenge as x, ACME_DNS_SECRET_KEYS as y, edgeClaims as z };
461
+ //# sourceMappingURL=index-BhrEC4ux.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-BhrEC4ux.d.mts","names":[],"sources":["../src/proxy/traefik.ts","../src/routes/index.ts"],"mappings":";;;;;cAwCa;;cAGA;;;;;;;;cASA;;iBAGG,YAAY,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MA,WAAW;;;;;;;;;;;;EAYX,cAAc;;;KAIH;EACP;EAAe;EAAkB;;EACjC;EAAa,UAAU;;;;;;iBAMZ,WAAW,OAAO;;UASjB;;EAEhB;;;;;;EAMA;;;;;;;;;;EAUA;IACK;;IACA;IAAa,UAAU;;IACvB;IAAmB;;;;;;;;;;;EAUxB;IAAU,UAAU;IAAU;;;;UAId;;EAEhB;;EAEA;EACA,oBAAoB;;;;;;EAMpB;;;cAIY,0BAA0B;WACjB;EAArB,YAAqB;;;iBAWN,sBAAsB,OAAO;;KAUjC;;;EAEP;;;;;;;;EAMA;EAAe;EAAkB;;;cAGzB;EAAW;EAAiB;;;;;;;;cAQ5B;EACZ,SAAS;EACT,SAAS;IAAiB;IAAc;;;;KAO7B"}
@@ -17,6 +17,11 @@ let _geekmidas_manifest = require("@geekmidas/manifest");
17
17
  */
18
18
  /** Where Caddy reads a stage's own certificate, inside its container. */
19
19
  const CADDY_TLS_DIR = "/etc/caddy/tls";
20
+ /** The origin certificate a CDN issued, as the stack's Caddy reads it. */
21
+ const CADDY_ORIGIN_CERTIFICATE = {
22
+ certFile: `${CADDY_TLS_DIR}/origin-cert.pem`,
23
+ keyFile: `${CADDY_TLS_DIR}/origin-key.pem`
24
+ };
20
25
  /** An upstream on the stack's own network — its service name — or on the host. */
21
26
  function address(upstream) {
22
27
  return require_routes_index.upstreamAddress(upstream);
@@ -38,6 +43,8 @@ function edgeCaddyfile(routes, options) {
38
43
  tls {
39
44
  on_demand
40
45
  }
46
+ ` : route.certificate?.kind === "files" ? `
47
+ tls ${route.certificate.certFile} ${route.certificate.keyFile}
41
48
  ` : certificate));
42
49
  return `${HEADER}${global ? `${global}\n\n` : ""}${blocks.join("\n\n")}\n`;
43
50
  }
@@ -162,6 +169,7 @@ function clientIpRule(edgeToken) {
162
169
  request_header @gkm_outside -${_geekmidas_manifest.CLIENT_IP_HEADER}`;
163
170
  }
164
171
  //#endregion
172
+ exports.CADDY_ORIGIN_CERTIFICATE = CADDY_ORIGIN_CERTIFICATE;
165
173
  exports.CADDY_TLS_DIR = CADDY_TLS_DIR;
166
174
  exports.clientIpRule = clientIpRule;
167
175
  exports.edgeCaddyfile = edgeCaddyfile;
@@ -1 +1 @@
1
- {"version":3,"file":"caddy.cjs","names":["upstreamAddress","CLIENT_IP_HEADER","EDGE_TOKEN_HEADER"],"sources":["../../src/proxy/caddy.ts"],"sourcesContent":["/**\n * The stack's own edge, rendered for Caddy: one site block per route.\n *\n * Deployed, each host is the stage's real domain and Caddy gets it a\n * certificate from Let's Encrypt on first request — the same automatic HTTPS\n * it does anywhere, with nothing to configure but the name — or serves the\n * stage's own certificate (`deploy.compose.tls`). Locally the hosts are\n * `*.localhost` and the certificate is the one gkm's local authority issued,\n * as the `gkm dev` edge's is, so a local stack is `https://` like a deployed\n * one.\n *\n * Pure: the routes are data (`@geekmidas/edge/routes`), and rendering them is one string.\n */\n\nimport { CLIENT_IP_HEADER, EDGE_TOKEN_HEADER } from '@geekmidas/manifest';\nimport {\n\tassertRouteRenderable,\n\ttype EdgeRoute,\n\ttype EdgeTls,\n\ttype Upstream,\n\tupstreamAddress,\n} from '../routes';\n\n/** Where Caddy reads a stage's own certificate, inside its container. */\nexport const CADDY_TLS_DIR = '/etc/caddy/tls';\n\nexport interface CaddyfileOptions {\n\ttls: EdgeTls;\n\t/**\n\t * The stack's token, on which a service of the stack keeps gkm's client\n\t * address through the edge — see `clientIpRule`.\n\t */\n\tedgeToken?: string;\n\t/** Where ACME sends expiry notices, when someone wants them. */\n\temail?: string;\n\t/**\n\t * `false` on the local stage: plain HTTP is not sent to HTTPS, whose\n\t * published port there may not be 443.\n\t */\n\tredirects?: boolean;\n}\n\n/** An upstream on the stack's own network — its service name — or on the host. */\nfunction address(upstream: Upstream): string {\n\treturn upstreamAddress(upstream);\n}\n\n/** The Caddyfile for a stack's edge. */\nexport function edgeCaddyfile(\n\troutes: readonly EdgeRoute[],\n\toptions: CaddyfileOptions,\n): string {\n\tconst { tls } = options;\n\t// A wildcard site certified host by host: Caddy asks its `hosts` route\n\t// before it gets a certificate for one, so a name no tenant has is never\n\t// sent to Let's Encrypt.\n\tconst ask = routes.find((route) => route.wildcard?.tls.kind === 'on-demand')\n\t\t?.wildcard?.tls;\n\tconst onDemand =\n\t\task?.kind === 'on-demand'\n\t\t\t? `\n\ton_demand_tls {\n\t\task ${ask.ask}\n\t}`\n\t\t\t: '';\n\tconst settings = `${options.email ? `\\n\\temail ${options.email}` : ''}${\n\t\toptions.redirects === false ? '\\n\\tauto_https disable_redirects' : ''\n\t}${onDemand}`;\n\tconst global = settings ? `{${settings}\\n}` : '';\n\n\tconst certificate =\n\t\ttls.kind === 'files'\n\t\t\t? `\n\ttls ${tls.certFile} ${tls.keyFile}\n`\n\t\t\t: '';\n\n\t// The wildcard site last: Caddy prefers a named host to `*.<domain>`\n\t// wherever the blocks are, and reading it last says the same.\n\tconst blocks = [\n\t\t...routes.filter((route) => !route.wildcard),\n\t\t...routes.filter((route) => route.wildcard),\n\t].map((route) =>\n\t\tsiteBlock(\n\t\t\troute,\n\t\t\toptions.edgeToken,\n\t\t\troute.wildcard?.tls.kind === 'on-demand'\n\t\t\t\t? `\n\ttls {\n\t\ton_demand\n\t}\n`\n\t\t\t\t: certificate,\n\t\t),\n\t);\n\n\t// No blank line under the header: `caddy fmt` wants the first block there.\n\treturn `${HEADER}${global ? `${global}\\n\\n` : ''}${blocks.join('\\n\\n')}\\n`;\n}\n\n/** One route's site block. */\nfunction siteBlock(\n\troute: EdgeRoute,\n\tedgeToken: string | undefined,\n\tcertificate: string,\n): string {\n\tassertRouteRenderable(route);\n\tconst { host, allow, auth, paths, prefixes, requireHeader, handlers } = route;\n\tconst allowed = [\n\t\t...(paths ?? []),\n\t\t...(prefixes ?? []).map((prefix) => `${prefix}*`),\n\t];\n\n\tconst except = route.wildcard?.except ?? [];\n\tconst checks = `${\n\t\texcept.length\n\t\t\t? `\n\t# Labels no tenant may take, although nothing in this stack answers them.\n\t@protected host ${except.map((label) => `${label}.${route.wildcard?.domain}`).join(' ')}\n\trespond @protected 404\n`\n\t\t\t: ''\n\t}${\n\t\tallow\n\t\t\t? `\n\t# Only these addresses — the peer Caddy sees, never a header a client\n\t# could set. Everything else is refused before it reaches the upstream.\n\t@denied not remote_ip ${allow.join(' ')}\n\trespond @denied 403\n`\n\t\t\t: ''\n\t}${\n\t\trequireHeader\n\t\t\t? `\n\t# Only what the CDN sends: it adds this header to every request, and this\n\t# host would be reached around the CDN otherwise.\n\t@forged not header ${requireHeader.name} \"${requireHeader.value}\"\n\trespond @forged 403\n`\n\t\t\t: ''\n\t}${\n\t\tauth\n\t\t\t? `\n\t# The stage's login, asked for before anything reaches the upstream.\n\tbasic_auth {\n\t\t${auth.user} ${auth.hash}\n\t}\n`\n\t\t\t: ''\n\t}${\n\t\tallowed.length\n\t\t\t? `\n\t# Only these paths answer. Everything else on this host is refused before\n\t# it reaches the upstream.\n\t@elsewhere not path ${allowed.join(' ')}\n\trespond @elsewhere 404\n`\n\t\t\t: ''\n\t}`;\n\n\tconst proxy = proxyBlock(route);\n\tconst body = handlers?.length\n\t\t? `${handlers\n\t\t\t\t.map(\n\t\t\t\t\t(handler, index) => `\n\t# Answered by another service: ${handler.name}.\n\t@handler${index} path ${handler.prefixes.map((prefix) => `${prefix}*`).join(' ')}\n\thandle @handler${index} {\n\t\treverse_proxy ${handler.upstreams.map(address).join(' ')}${\n\t\t\thandler.under\n\t\t\t\t? ` {\n\t\t\trewrite ${handler.under}{uri}\n\t\t}`\n\t\t\t\t: ''\n\t\t}\n\t}\n`,\n\t\t\t\t)\n\t\t\t\t.join('')}\n\thandle {${proxy.replace(/\\n/g, '\\n\\t')}\n\t}`\n\t\t: proxy;\n\n\t// Its aliases join the block: every check and handler holds on each.\n\tconst names = [host, ...(route.aliases ?? [])]\n\t\t.map((name) => `https://${name}`)\n\t\t.join(', ');\n\t// Each redirect host, a block of its own that sends the browser to the\n\t// canonical host, path and query kept, and proxies nothing.\n\tconst redirects = (route.redirects?.hosts ?? [])\n\t\t.map(\n\t\t\t(from) => `\n\nhttps://${from} {${certificate}\n\tredir ${route.redirects?.to}{uri} permanent\n}`,\n\t\t)\n\t\t.join('');\n\n\treturn `${names} {${certificate}${clientIpRule(edgeToken)}\n${checks}${body}\n}${redirects}`;\n}\n\n/** Where a route's requests go, when no handler took them. */\nfunction proxyBlock(route: EdgeRoute): string {\n\tconst { upstreams, prefix, streaming } = route;\n\tconst health = healthCheck(route);\n\treturn prefix\n\t\t? `\n\t# A bucket, served at a host of its own: the bucket is a prefix on MinIO,\n\t# which routes and signs on the Host header — so it is sent the upstream's.\n\treverse_proxy ${upstreams.map(address).join(' ')} {\n\t\trewrite ${prefix}{uri}\n\t\theader_up Host {upstream_hostport}${health}\n\t}`\n\t\t: `\n\t# Host and X-Forwarded-* are passed through as Caddy does by default, so\n\t# an app builds redirects, cookies and links on the address a caller used.\n\treverse_proxy ${upstreams.map(address).join(' ')}${\n\t\tstreaming || health\n\t\t\t? ` {${\n\t\t\t\t\tstreaming\n\t\t\t\t\t\t? `\n\t\t# Never buffer: a streamed response — server-sent events, a Next.js\n\t\t# RSC payload — reaches the client as each chunk is written.\n\t\tflush_interval -1`\n\t\t\t\t\t\t: ''\n\t\t\t\t}${health}\n\t}`\n\t\t\t: ''\n\t}`;\n}\n\n/**\n * Caddy's own check of each upstream, as the Traefik edge's\n * `loadBalancer.healthCheck`: one that stops answering is taken out of\n * rotation until it does again. Caddy starts once its apps have — not once\n * they are healthy — so this is what keeps a request from an upstream that\n * is not up, and a route to an app with none up answers an error (503)\n * rather than going missing.\n */\nfunction healthCheck({ health }: EdgeRoute): string {\n\treturn health\n\t\t? `\n\t\t# Each upstream asked on its own: one that stops answering is out of\n\t\t# rotation until it answers again.\n\t\thealth_uri ${health}\n\t\thealth_interval 5s\n\t\thealth_timeout 3s`\n\t\t: '';\n}\n\nconst HEADER = `# Generated by gkm compose from the construct manifest — do not edit.\n#\n# One host per API and site, each in front of its own container, one per\n# file server in front of the stack's MinIO — its processors' paths sent to\n# their own service — one per worker SNS pushes to and per processor unit a\n# CDN reaches, answering only those paths, and — when its logs are served\n# publicly — one for OpenObserve, to the addresses it allows. A site the\n# stage puts behind a password asks for its login first.\n`;\n\n/**\n * The lines every site block of an edge opens with, for gkm's own\n * `x-gkm-client-ip` — the client address the auth server rate-limits by.\n *\n * Only a service of this stack may send it. One that calls a sibling at its\n * public address comes through this edge, and proves where it comes from with\n * the stack's token (`x-gkm-edge-token`), derived from the stage's seed and\n * handed to every process of the stack — never to a browser. A request\n * without it has the header removed, so nobody outside can choose the address\n * they are rate-limited by. Without a token, it is removed from every request.\n */\nexport function clientIpRule(edgeToken: string | undefined): string {\n\tif (!edgeToken) {\n\t\treturn `\n\t# gkm's own header, which only a service of this stack may send: the auth\n\t# server rate-limits by the client address it carries.\n\trequest_header -${CLIENT_IP_HEADER}`;\n\t}\n\treturn `\n\t# gkm's own header, which only a service of this stack may send — proven by\n\t# the stack's token: the auth server rate-limits by the client address it\n\t# carries. Removed from every other request.\n\t@gkm_outside not header ${EDGE_TOKEN_HEADER} ${edgeToken}\n\trequest_header @gkm_outside -${CLIENT_IP_HEADER}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAwBA,MAAa,gBAAgB;;AAmB7B,SAAS,QAAQ,UAA4B;CAC5C,OAAOA,qBAAAA,gBAAgB,QAAQ;AAChC;;AAGA,SAAgB,cACf,QACA,SACS;CACT,MAAM,EAAE,QAAQ;CAIhB,MAAM,MAAM,OAAO,MAAM,UAAU,MAAM,UAAU,IAAI,SAAS,WAAW,CAAC,EACzE,UAAU;CACb,MAAM,WACL,KAAK,SAAS,cACX;;QAEG,IAAI,IAAI;MAEX;CACJ,MAAM,WAAW,GAAG,QAAQ,QAAQ,aAAa,QAAQ,UAAU,KAClE,QAAQ,cAAc,QAAQ,oCAAqC,KACjE;CACH,MAAM,SAAS,WAAW,IAAI,SAAS,OAAO;CAE9C,MAAM,cACL,IAAI,SAAS,UACV;OACE,IAAI,SAAS,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CAAC,KAAK,GAAG,IACtD,QAAQ,QACL;aACO,QAAQ,MAAM;OAErB,GACH;;CAGC,CAAC,CACA,KAAK,EAAE,EAAE;WACH,MAAM,QAAQ,OAAO,KAAM,EAAE;MAEpC;CAGH,MAAM,QAAQ,CAAC,MAAM,GAAI,MAAM,WAAW,CAAC,CAAE,CAAC,CAC5C,KAAK,SAAS,WAAW,MAAM,CAAC,CAChC,KAAK,IAAI;CAGX,MAAM,aAAa,MAAM,WAAW,SAAS,CAAC,EAAA,CAC5C,KACC,SAAS;;UAEH,KAAK,IAAI,YAAY;SACtB,MAAM,WAAW,GAAG;EAE3B,CAAC,CACA,KAAK,EAAE;CAET,OAAO,GAAG,MAAM,IAAI,cAAc,aAAa,SAAS,EAAE;EACzD,SAAS,KAAK;GACb;AACH;;AAGA,SAAS,WAAW,OAA0B;CAC7C,MAAM,EAAE,WAAW,QAAQ,cAAc;CACzC,MAAM,SAAS,YAAY,KAAK;CAChC,OAAO,SACJ;;;iBAGa,UAAU,IAAI,OAAO,CAAC,CAAC,KAAK,GAAG,EAAE;YACtC,OAAO;sCACmB,OAAO;MAEzC;;;iBAGa,UAAU,IAAI,OAAO,CAAC,CAAC,KAAK,GAAG,IAC9C,aAAa,SACV,KACA,YACG;;;uBAIA,KACD,OAAO;MAET;AAEL;;;;;;;;;AAUA,SAAS,YAAY,EAAE,UAA6B;CACnD,OAAO,SACJ;;;eAGW,OAAO;;uBAGlB;AACJ;AAEA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;AAqBf,SAAgB,aAAa,WAAuC;CACnE,IAAI,CAAC,WACJ,OAAO;;;mBAGUC,oBAAAA;CAElB,OAAO;;;;2BAImBC,oBAAAA,kBAAkB,GAAG,UAAU;gCAC1BD,oBAAAA;AAChC"}
1
+ {"version":3,"file":"caddy.cjs","names":["upstreamAddress","CLIENT_IP_HEADER","EDGE_TOKEN_HEADER"],"sources":["../../src/proxy/caddy.ts"],"sourcesContent":["/**\n * The stack's own edge, rendered for Caddy: one site block per route.\n *\n * Deployed, each host is the stage's real domain and Caddy gets it a\n * certificate from Let's Encrypt on first request — the same automatic HTTPS\n * it does anywhere, with nothing to configure but the name — or serves the\n * stage's own certificate (`deploy.compose.tls`). Locally the hosts are\n * `*.localhost` and the certificate is the one gkm's local authority issued,\n * as the `gkm dev` edge's is, so a local stack is `https://` like a deployed\n * one.\n *\n * Pure: the routes are data (`@geekmidas/edge/routes`), and rendering them is one string.\n */\n\nimport { CLIENT_IP_HEADER, EDGE_TOKEN_HEADER } from '@geekmidas/manifest';\nimport {\n\tassertRouteRenderable,\n\ttype EdgeRoute,\n\ttype EdgeTls,\n\ttype Upstream,\n\tupstreamAddress,\n} from '../routes';\n\n/** Where Caddy reads a stage's own certificate, inside its container. */\nexport const CADDY_TLS_DIR = '/etc/caddy/tls';\n\n/** The origin certificate a CDN issued, as the stack's Caddy reads it. */\nexport const CADDY_ORIGIN_CERTIFICATE = {\n\tcertFile: `${CADDY_TLS_DIR}/origin-cert.pem`,\n\tkeyFile: `${CADDY_TLS_DIR}/origin-key.pem`,\n} as const;\n\nexport interface CaddyfileOptions {\n\ttls: EdgeTls;\n\t/**\n\t * The stack's token, on which a service of the stack keeps gkm's client\n\t * address through the edge — see `clientIpRule`.\n\t */\n\tedgeToken?: string;\n\t/** Where ACME sends expiry notices, when someone wants them. */\n\temail?: string;\n\t/**\n\t * `false` on the local stage: plain HTTP is not sent to HTTPS, whose\n\t * published port there may not be 443.\n\t */\n\tredirects?: boolean;\n}\n\n/** An upstream on the stack's own network — its service name — or on the host. */\nfunction address(upstream: Upstream): string {\n\treturn upstreamAddress(upstream);\n}\n\n/** The Caddyfile for a stack's edge. */\nexport function edgeCaddyfile(\n\troutes: readonly EdgeRoute[],\n\toptions: CaddyfileOptions,\n): string {\n\tconst { tls } = options;\n\t// A wildcard site certified host by host: Caddy asks its `hosts` route\n\t// before it gets a certificate for one, so a name no tenant has is never\n\t// sent to Let's Encrypt.\n\tconst ask = routes.find((route) => route.wildcard?.tls.kind === 'on-demand')\n\t\t?.wildcard?.tls;\n\tconst onDemand =\n\t\task?.kind === 'on-demand'\n\t\t\t? `\n\ton_demand_tls {\n\t\task ${ask.ask}\n\t}`\n\t\t\t: '';\n\tconst settings = `${options.email ? `\\n\\temail ${options.email}` : ''}${\n\t\toptions.redirects === false ? '\\n\\tauto_https disable_redirects' : ''\n\t}${onDemand}`;\n\tconst global = settings ? `{${settings}\\n}` : '';\n\n\tconst certificate =\n\t\ttls.kind === 'files'\n\t\t\t? `\n\ttls ${tls.certFile} ${tls.keyFile}\n`\n\t\t\t: '';\n\n\t// The wildcard site last: Caddy prefers a named host to `*.<domain>`\n\t// wherever the blocks are, and reading it last says the same.\n\tconst blocks = [\n\t\t...routes.filter((route) => !route.wildcard),\n\t\t...routes.filter((route) => route.wildcard),\n\t].map((route) =>\n\t\tsiteBlock(\n\t\t\troute,\n\t\t\toptions.edgeToken,\n\t\t\troute.wildcard?.tls.kind === 'on-demand'\n\t\t\t\t? `\n\ttls {\n\t\ton_demand\n\t}\n`\n\t\t\t\t: route.certificate?.kind === 'files'\n\t\t\t\t\t? `\n\ttls ${route.certificate.certFile} ${route.certificate.keyFile}\n`\n\t\t\t\t\t: certificate,\n\t\t),\n\t);\n\n\t// No blank line under the header: `caddy fmt` wants the first block there.\n\treturn `${HEADER}${global ? `${global}\\n\\n` : ''}${blocks.join('\\n\\n')}\\n`;\n}\n\n/** One route's site block. */\nfunction siteBlock(\n\troute: EdgeRoute,\n\tedgeToken: string | undefined,\n\tcertificate: string,\n): string {\n\tassertRouteRenderable(route);\n\tconst { host, allow, auth, paths, prefixes, requireHeader, handlers } = route;\n\tconst allowed = [\n\t\t...(paths ?? []),\n\t\t...(prefixes ?? []).map((prefix) => `${prefix}*`),\n\t];\n\n\tconst except = route.wildcard?.except ?? [];\n\tconst checks = `${\n\t\texcept.length\n\t\t\t? `\n\t# Labels no tenant may take, although nothing in this stack answers them.\n\t@protected host ${except.map((label) => `${label}.${route.wildcard?.domain}`).join(' ')}\n\trespond @protected 404\n`\n\t\t\t: ''\n\t}${\n\t\tallow\n\t\t\t? `\n\t# Only these addresses — the peer Caddy sees, never a header a client\n\t# could set. Everything else is refused before it reaches the upstream.\n\t@denied not remote_ip ${allow.join(' ')}\n\trespond @denied 403\n`\n\t\t\t: ''\n\t}${\n\t\trequireHeader\n\t\t\t? `\n\t# Only what the CDN sends: it adds this header to every request, and this\n\t# host would be reached around the CDN otherwise.\n\t@forged not header ${requireHeader.name} \"${requireHeader.value}\"\n\trespond @forged 403\n`\n\t\t\t: ''\n\t}${\n\t\tauth\n\t\t\t? `\n\t# The stage's login, asked for before anything reaches the upstream.\n\tbasic_auth {\n\t\t${auth.user} ${auth.hash}\n\t}\n`\n\t\t\t: ''\n\t}${\n\t\tallowed.length\n\t\t\t? `\n\t# Only these paths answer. Everything else on this host is refused before\n\t# it reaches the upstream.\n\t@elsewhere not path ${allowed.join(' ')}\n\trespond @elsewhere 404\n`\n\t\t\t: ''\n\t}`;\n\n\tconst proxy = proxyBlock(route);\n\tconst body = handlers?.length\n\t\t? `${handlers\n\t\t\t\t.map(\n\t\t\t\t\t(handler, index) => `\n\t# Answered by another service: ${handler.name}.\n\t@handler${index} path ${handler.prefixes.map((prefix) => `${prefix}*`).join(' ')}\n\thandle @handler${index} {\n\t\treverse_proxy ${handler.upstreams.map(address).join(' ')}${\n\t\t\thandler.under\n\t\t\t\t? ` {\n\t\t\trewrite ${handler.under}{uri}\n\t\t}`\n\t\t\t\t: ''\n\t\t}\n\t}\n`,\n\t\t\t\t)\n\t\t\t\t.join('')}\n\thandle {${proxy.replace(/\\n/g, '\\n\\t')}\n\t}`\n\t\t: proxy;\n\n\t// Its aliases join the block: every check and handler holds on each.\n\tconst names = [host, ...(route.aliases ?? [])]\n\t\t.map((name) => `https://${name}`)\n\t\t.join(', ');\n\t// Each redirect host, a block of its own that sends the browser to the\n\t// canonical host, path and query kept, and proxies nothing.\n\tconst redirects = (route.redirects?.hosts ?? [])\n\t\t.map(\n\t\t\t(from) => `\n\nhttps://${from} {${certificate}\n\tredir ${route.redirects?.to}{uri} permanent\n}`,\n\t\t)\n\t\t.join('');\n\n\treturn `${names} {${certificate}${clientIpRule(edgeToken)}\n${checks}${body}\n}${redirects}`;\n}\n\n/** Where a route's requests go, when no handler took them. */\nfunction proxyBlock(route: EdgeRoute): string {\n\tconst { upstreams, prefix, streaming } = route;\n\tconst health = healthCheck(route);\n\treturn prefix\n\t\t? `\n\t# A bucket, served at a host of its own: the bucket is a prefix on MinIO,\n\t# which routes and signs on the Host header — so it is sent the upstream's.\n\treverse_proxy ${upstreams.map(address).join(' ')} {\n\t\trewrite ${prefix}{uri}\n\t\theader_up Host {upstream_hostport}${health}\n\t}`\n\t\t: `\n\t# Host and X-Forwarded-* are passed through as Caddy does by default, so\n\t# an app builds redirects, cookies and links on the address a caller used.\n\treverse_proxy ${upstreams.map(address).join(' ')}${\n\t\tstreaming || health\n\t\t\t? ` {${\n\t\t\t\t\tstreaming\n\t\t\t\t\t\t? `\n\t\t# Never buffer: a streamed response — server-sent events, a Next.js\n\t\t# RSC payload — reaches the client as each chunk is written.\n\t\tflush_interval -1`\n\t\t\t\t\t\t: ''\n\t\t\t\t}${health}\n\t}`\n\t\t\t: ''\n\t}`;\n}\n\n/**\n * Caddy's own check of each upstream, as the Traefik edge's\n * `loadBalancer.healthCheck`: one that stops answering is taken out of\n * rotation until it does again. Caddy starts once its apps have — not once\n * they are healthy — so this is what keeps a request from an upstream that\n * is not up, and a route to an app with none up answers an error (503)\n * rather than going missing.\n */\nfunction healthCheck({ health }: EdgeRoute): string {\n\treturn health\n\t\t? `\n\t\t# Each upstream asked on its own: one that stops answering is out of\n\t\t# rotation until it answers again.\n\t\thealth_uri ${health}\n\t\thealth_interval 5s\n\t\thealth_timeout 3s`\n\t\t: '';\n}\n\nconst HEADER = `# Generated by gkm compose from the construct manifest — do not edit.\n#\n# One host per API and site, each in front of its own container, one per\n# file server in front of the stack's MinIO — its processors' paths sent to\n# their own service — one per worker SNS pushes to and per processor unit a\n# CDN reaches, answering only those paths, and — when its logs are served\n# publicly — one for OpenObserve, to the addresses it allows. A site the\n# stage puts behind a password asks for its login first.\n`;\n\n/**\n * The lines every site block of an edge opens with, for gkm's own\n * `x-gkm-client-ip` — the client address the auth server rate-limits by.\n *\n * Only a service of this stack may send it. One that calls a sibling at its\n * public address comes through this edge, and proves where it comes from with\n * the stack's token (`x-gkm-edge-token`), derived from the stage's seed and\n * handed to every process of the stack — never to a browser. A request\n * without it has the header removed, so nobody outside can choose the address\n * they are rate-limited by. Without a token, it is removed from every request.\n */\nexport function clientIpRule(edgeToken: string | undefined): string {\n\tif (!edgeToken) {\n\t\treturn `\n\t# gkm's own header, which only a service of this stack may send: the auth\n\t# server rate-limits by the client address it carries.\n\trequest_header -${CLIENT_IP_HEADER}`;\n\t}\n\treturn `\n\t# gkm's own header, which only a service of this stack may send — proven by\n\t# the stack's token: the auth server rate-limits by the client address it\n\t# carries. Removed from every other request.\n\t@gkm_outside not header ${EDGE_TOKEN_HEADER} ${edgeToken}\n\trequest_header @gkm_outside -${CLIENT_IP_HEADER}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAwBA,MAAa,gBAAgB;;AAG7B,MAAa,2BAA2B;CACvC,UAAU,GAAG,cAAc;CAC3B,SAAS,GAAG,cAAc;AAC3B;;AAmBA,SAAS,QAAQ,UAA4B;CAC5C,OAAOA,qBAAAA,gBAAgB,QAAQ;AAChC;;AAGA,SAAgB,cACf,QACA,SACS;CACT,MAAM,EAAE,QAAQ;CAIhB,MAAM,MAAM,OAAO,MAAM,UAAU,MAAM,UAAU,IAAI,SAAS,WAAW,CAAC,EACzE,UAAU;CACb,MAAM,WACL,KAAK,SAAS,cACX;;QAEG,IAAI,IAAI;MAEX;CACJ,MAAM,WAAW,GAAG,QAAQ,QAAQ,aAAa,QAAQ,UAAU,KAClE,QAAQ,cAAc,QAAQ,oCAAqC,KACjE;CACH,MAAM,SAAS,WAAW,IAAI,SAAS,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 -1,7 +1,12 @@
1
- import { i as EdgeRoute, o as EdgeTls } from "../index-BVE0VFku.cjs";
1
+ import { i as EdgeRoute, s as EdgeTls } from "../index-BhrEC4ux.cjs";
2
2
  //#region src/proxy/caddy.d.ts
3
3
  /** Where Caddy reads a stage's own certificate, inside its container. */
4
4
  export declare const CADDY_TLS_DIR = "/etc/caddy/tls";
5
+ /** The origin certificate a CDN issued, as the stack's Caddy reads it. */
6
+ export declare const CADDY_ORIGIN_CERTIFICATE: {
7
+ readonly certFile: "/etc/caddy/tls/origin-cert.pem";
8
+ readonly keyFile: "/etc/caddy/tls/origin-key.pem";
9
+ };
5
10
  export interface CaddyfileOptions {
6
11
  tls: EdgeTls;
7
12
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"caddy.d.cts","names":[],"sources":["../../src/proxy/caddy.ts"],"mappings":";;;qBAwBa;iBAEI;EAChB,KAAK;;;;;EAKL;;EAEA;;;;;EAKA;;;wBASe,cACf,iBAAiB,aACjB,SAAS;;;;;;;;;;;;wBAgOM,aAAa"}
1
+ {"version":3,"file":"caddy.d.cts","names":[],"sources":["../../src/proxy/caddy.ts"],"mappings":";;;qBAwBa;;qBAGA;WACZ;WACA;;iBAGgB;EAChB,KAAK;;;;;EAKL;;EAEA;;;;;EAKA;;;wBASe,cACf,iBAAiB,aACjB,SAAS;;;;;;;;;;;;wBAoOM,aAAa"}
@@ -1,7 +1,12 @@
1
- import { i as EdgeRoute, o as EdgeTls } from "../index-BVE0VFku.mjs";
1
+ import { i as EdgeRoute, s as EdgeTls } from "../index-BhrEC4ux.mjs";
2
2
  //#region src/proxy/caddy.d.ts
3
3
  /** Where Caddy reads a stage's own certificate, inside its container. */
4
4
  export declare const CADDY_TLS_DIR = "/etc/caddy/tls";
5
+ /** The origin certificate a CDN issued, as the stack's Caddy reads it. */
6
+ export declare const CADDY_ORIGIN_CERTIFICATE: {
7
+ readonly certFile: "/etc/caddy/tls/origin-cert.pem";
8
+ readonly keyFile: "/etc/caddy/tls/origin-key.pem";
9
+ };
5
10
  export interface CaddyfileOptions {
6
11
  tls: EdgeTls;
7
12
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"caddy.d.mts","names":[],"sources":["../../src/proxy/caddy.ts"],"mappings":";;;qBAwBa;iBAEI;EAChB,KAAK;;;;;EAKL;;EAEA;;;;;EAKA;;;wBASe,cACf,iBAAiB,aACjB,SAAS;;;;;;;;;;;;wBAgOM,aAAa"}
1
+ {"version":3,"file":"caddy.d.mts","names":[],"sources":["../../src/proxy/caddy.ts"],"mappings":";;;qBAwBa;;qBAGA;WACZ;WACA;;iBAGgB;EAChB,KAAK;;;;;EAKL;;EAEA;;;;;EAKA;;;wBASe,cACf,iBAAiB,aACjB,SAAS;;;;;;;;;;;;wBAoOM,aAAa"}
@@ -16,6 +16,11 @@ import { CLIENT_IP_HEADER, EDGE_TOKEN_HEADER } from "@geekmidas/manifest";
16
16
  */
17
17
  /** Where Caddy reads a stage's own certificate, inside its container. */
18
18
  const CADDY_TLS_DIR = "/etc/caddy/tls";
19
+ /** The origin certificate a CDN issued, as the stack's Caddy reads it. */
20
+ const CADDY_ORIGIN_CERTIFICATE = {
21
+ certFile: `${CADDY_TLS_DIR}/origin-cert.pem`,
22
+ keyFile: `${CADDY_TLS_DIR}/origin-key.pem`
23
+ };
19
24
  /** An upstream on the stack's own network — its service name — or on the host. */
20
25
  function address(upstream) {
21
26
  return upstreamAddress(upstream);
@@ -37,6 +42,8 @@ function edgeCaddyfile(routes, options) {
37
42
  tls {
38
43
  on_demand
39
44
  }
45
+ ` : route.certificate?.kind === "files" ? `
46
+ tls ${route.certificate.certFile} ${route.certificate.keyFile}
40
47
  ` : certificate));
41
48
  return `${HEADER}${global ? `${global}\n\n` : ""}${blocks.join("\n\n")}\n`;
42
49
  }
@@ -161,6 +168,6 @@ function clientIpRule(edgeToken) {
161
168
  request_header @gkm_outside -${CLIENT_IP_HEADER}`;
162
169
  }
163
170
  //#endregion
164
- export { CADDY_TLS_DIR, clientIpRule, edgeCaddyfile };
171
+ export { CADDY_ORIGIN_CERTIFICATE, CADDY_TLS_DIR, clientIpRule, edgeCaddyfile };
165
172
 
166
173
  //# sourceMappingURL=caddy.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"caddy.mjs","names":[],"sources":["../../src/proxy/caddy.ts"],"sourcesContent":["/**\n * The stack's own edge, rendered for Caddy: one site block per route.\n *\n * Deployed, each host is the stage's real domain and Caddy gets it a\n * certificate from Let's Encrypt on first request — the same automatic HTTPS\n * it does anywhere, with nothing to configure but the name — or serves the\n * stage's own certificate (`deploy.compose.tls`). Locally the hosts are\n * `*.localhost` and the certificate is the one gkm's local authority issued,\n * as the `gkm dev` edge's is, so a local stack is `https://` like a deployed\n * one.\n *\n * Pure: the routes are data (`@geekmidas/edge/routes`), and rendering them is one string.\n */\n\nimport { CLIENT_IP_HEADER, EDGE_TOKEN_HEADER } from '@geekmidas/manifest';\nimport {\n\tassertRouteRenderable,\n\ttype EdgeRoute,\n\ttype EdgeTls,\n\ttype Upstream,\n\tupstreamAddress,\n} from '../routes';\n\n/** Where Caddy reads a stage's own certificate, inside its container. */\nexport const CADDY_TLS_DIR = '/etc/caddy/tls';\n\nexport interface CaddyfileOptions {\n\ttls: EdgeTls;\n\t/**\n\t * The stack's token, on which a service of the stack keeps gkm's client\n\t * address through the edge — see `clientIpRule`.\n\t */\n\tedgeToken?: string;\n\t/** Where ACME sends expiry notices, when someone wants them. */\n\temail?: string;\n\t/**\n\t * `false` on the local stage: plain HTTP is not sent to HTTPS, whose\n\t * published port there may not be 443.\n\t */\n\tredirects?: boolean;\n}\n\n/** An upstream on the stack's own network — its service name — or on the host. */\nfunction address(upstream: Upstream): string {\n\treturn upstreamAddress(upstream);\n}\n\n/** The Caddyfile for a stack's edge. */\nexport function edgeCaddyfile(\n\troutes: readonly EdgeRoute[],\n\toptions: CaddyfileOptions,\n): string {\n\tconst { tls } = options;\n\t// A wildcard site certified host by host: Caddy asks its `hosts` route\n\t// before it gets a certificate for one, so a name no tenant has is never\n\t// sent to Let's Encrypt.\n\tconst ask = routes.find((route) => route.wildcard?.tls.kind === 'on-demand')\n\t\t?.wildcard?.tls;\n\tconst onDemand =\n\t\task?.kind === 'on-demand'\n\t\t\t? `\n\ton_demand_tls {\n\t\task ${ask.ask}\n\t}`\n\t\t\t: '';\n\tconst settings = `${options.email ? `\\n\\temail ${options.email}` : ''}${\n\t\toptions.redirects === false ? '\\n\\tauto_https disable_redirects' : ''\n\t}${onDemand}`;\n\tconst global = settings ? `{${settings}\\n}` : '';\n\n\tconst certificate =\n\t\ttls.kind === 'files'\n\t\t\t? `\n\ttls ${tls.certFile} ${tls.keyFile}\n`\n\t\t\t: '';\n\n\t// The wildcard site last: Caddy prefers a named host to `*.<domain>`\n\t// wherever the blocks are, and reading it last says the same.\n\tconst blocks = [\n\t\t...routes.filter((route) => !route.wildcard),\n\t\t...routes.filter((route) => route.wildcard),\n\t].map((route) =>\n\t\tsiteBlock(\n\t\t\troute,\n\t\t\toptions.edgeToken,\n\t\t\troute.wildcard?.tls.kind === 'on-demand'\n\t\t\t\t? `\n\ttls {\n\t\ton_demand\n\t}\n`\n\t\t\t\t: certificate,\n\t\t),\n\t);\n\n\t// No blank line under the header: `caddy fmt` wants the first block there.\n\treturn `${HEADER}${global ? `${global}\\n\\n` : ''}${blocks.join('\\n\\n')}\\n`;\n}\n\n/** One route's site block. */\nfunction siteBlock(\n\troute: EdgeRoute,\n\tedgeToken: string | undefined,\n\tcertificate: string,\n): string {\n\tassertRouteRenderable(route);\n\tconst { host, allow, auth, paths, prefixes, requireHeader, handlers } = route;\n\tconst allowed = [\n\t\t...(paths ?? []),\n\t\t...(prefixes ?? []).map((prefix) => `${prefix}*`),\n\t];\n\n\tconst except = route.wildcard?.except ?? [];\n\tconst checks = `${\n\t\texcept.length\n\t\t\t? `\n\t# Labels no tenant may take, although nothing in this stack answers them.\n\t@protected host ${except.map((label) => `${label}.${route.wildcard?.domain}`).join(' ')}\n\trespond @protected 404\n`\n\t\t\t: ''\n\t}${\n\t\tallow\n\t\t\t? `\n\t# Only these addresses — the peer Caddy sees, never a header a client\n\t# could set. Everything else is refused before it reaches the upstream.\n\t@denied not remote_ip ${allow.join(' ')}\n\trespond @denied 403\n`\n\t\t\t: ''\n\t}${\n\t\trequireHeader\n\t\t\t? `\n\t# Only what the CDN sends: it adds this header to every request, and this\n\t# host would be reached around the CDN otherwise.\n\t@forged not header ${requireHeader.name} \"${requireHeader.value}\"\n\trespond @forged 403\n`\n\t\t\t: ''\n\t}${\n\t\tauth\n\t\t\t? `\n\t# The stage's login, asked for before anything reaches the upstream.\n\tbasic_auth {\n\t\t${auth.user} ${auth.hash}\n\t}\n`\n\t\t\t: ''\n\t}${\n\t\tallowed.length\n\t\t\t? `\n\t# Only these paths answer. Everything else on this host is refused before\n\t# it reaches the upstream.\n\t@elsewhere not path ${allowed.join(' ')}\n\trespond @elsewhere 404\n`\n\t\t\t: ''\n\t}`;\n\n\tconst proxy = proxyBlock(route);\n\tconst body = handlers?.length\n\t\t? `${handlers\n\t\t\t\t.map(\n\t\t\t\t\t(handler, index) => `\n\t# Answered by another service: ${handler.name}.\n\t@handler${index} path ${handler.prefixes.map((prefix) => `${prefix}*`).join(' ')}\n\thandle @handler${index} {\n\t\treverse_proxy ${handler.upstreams.map(address).join(' ')}${\n\t\t\thandler.under\n\t\t\t\t? ` {\n\t\t\trewrite ${handler.under}{uri}\n\t\t}`\n\t\t\t\t: ''\n\t\t}\n\t}\n`,\n\t\t\t\t)\n\t\t\t\t.join('')}\n\thandle {${proxy.replace(/\\n/g, '\\n\\t')}\n\t}`\n\t\t: proxy;\n\n\t// Its aliases join the block: every check and handler holds on each.\n\tconst names = [host, ...(route.aliases ?? [])]\n\t\t.map((name) => `https://${name}`)\n\t\t.join(', ');\n\t// Each redirect host, a block of its own that sends the browser to the\n\t// canonical host, path and query kept, and proxies nothing.\n\tconst redirects = (route.redirects?.hosts ?? [])\n\t\t.map(\n\t\t\t(from) => `\n\nhttps://${from} {${certificate}\n\tredir ${route.redirects?.to}{uri} permanent\n}`,\n\t\t)\n\t\t.join('');\n\n\treturn `${names} {${certificate}${clientIpRule(edgeToken)}\n${checks}${body}\n}${redirects}`;\n}\n\n/** Where a route's requests go, when no handler took them. */\nfunction proxyBlock(route: EdgeRoute): string {\n\tconst { upstreams, prefix, streaming } = route;\n\tconst health = healthCheck(route);\n\treturn prefix\n\t\t? `\n\t# A bucket, served at a host of its own: the bucket is a prefix on MinIO,\n\t# which routes and signs on the Host header — so it is sent the upstream's.\n\treverse_proxy ${upstreams.map(address).join(' ')} {\n\t\trewrite ${prefix}{uri}\n\t\theader_up Host {upstream_hostport}${health}\n\t}`\n\t\t: `\n\t# Host and X-Forwarded-* are passed through as Caddy does by default, so\n\t# an app builds redirects, cookies and links on the address a caller used.\n\treverse_proxy ${upstreams.map(address).join(' ')}${\n\t\tstreaming || health\n\t\t\t? ` {${\n\t\t\t\t\tstreaming\n\t\t\t\t\t\t? `\n\t\t# Never buffer: a streamed response — server-sent events, a Next.js\n\t\t# RSC payload — reaches the client as each chunk is written.\n\t\tflush_interval -1`\n\t\t\t\t\t\t: ''\n\t\t\t\t}${health}\n\t}`\n\t\t\t: ''\n\t}`;\n}\n\n/**\n * Caddy's own check of each upstream, as the Traefik edge's\n * `loadBalancer.healthCheck`: one that stops answering is taken out of\n * rotation until it does again. Caddy starts once its apps have — not once\n * they are healthy — so this is what keeps a request from an upstream that\n * is not up, and a route to an app with none up answers an error (503)\n * rather than going missing.\n */\nfunction healthCheck({ health }: EdgeRoute): string {\n\treturn health\n\t\t? `\n\t\t# Each upstream asked on its own: one that stops answering is out of\n\t\t# rotation until it answers again.\n\t\thealth_uri ${health}\n\t\thealth_interval 5s\n\t\thealth_timeout 3s`\n\t\t: '';\n}\n\nconst HEADER = `# Generated by gkm compose from the construct manifest — do not edit.\n#\n# One host per API and site, each in front of its own container, one per\n# file server in front of the stack's MinIO — its processors' paths sent to\n# their own service — one per worker SNS pushes to and per processor unit a\n# CDN reaches, answering only those paths, and — when its logs are served\n# publicly — one for OpenObserve, to the addresses it allows. A site the\n# stage puts behind a password asks for its login first.\n`;\n\n/**\n * The lines every site block of an edge opens with, for gkm's own\n * `x-gkm-client-ip` — the client address the auth server rate-limits by.\n *\n * Only a service of this stack may send it. One that calls a sibling at its\n * public address comes through this edge, and proves where it comes from with\n * the stack's token (`x-gkm-edge-token`), derived from the stage's seed and\n * handed to every process of the stack — never to a browser. A request\n * without it has the header removed, so nobody outside can choose the address\n * they are rate-limited by. Without a token, it is removed from every request.\n */\nexport function clientIpRule(edgeToken: string | undefined): string {\n\tif (!edgeToken) {\n\t\treturn `\n\t# gkm's own header, which only a service of this stack may send: the auth\n\t# server rate-limits by the client address it carries.\n\trequest_header -${CLIENT_IP_HEADER}`;\n\t}\n\treturn `\n\t# gkm's own header, which only a service of this stack may send — proven by\n\t# the stack's token: the auth server rate-limits by the client address it\n\t# carries. Removed from every other request.\n\t@gkm_outside not header ${EDGE_TOKEN_HEADER} ${edgeToken}\n\trequest_header @gkm_outside -${CLIENT_IP_HEADER}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAwBA,MAAa,gBAAgB;;AAmB7B,SAAS,QAAQ,UAA4B;CAC5C,OAAO,gBAAgB,QAAQ;AAChC;;AAGA,SAAgB,cACf,QACA,SACS;CACT,MAAM,EAAE,QAAQ;CAIhB,MAAM,MAAM,OAAO,MAAM,UAAU,MAAM,UAAU,IAAI,SAAS,WAAW,CAAC,EACzE,UAAU;CACb,MAAM,WACL,KAAK,SAAS,cACX;;QAEG,IAAI,IAAI;MAEX;CACJ,MAAM,WAAW,GAAG,QAAQ,QAAQ,aAAa,QAAQ,UAAU,KAClE,QAAQ,cAAc,QAAQ,oCAAqC,KACjE;CACH,MAAM,SAAS,WAAW,IAAI,SAAS,OAAO;CAE9C,MAAM,cACL,IAAI,SAAS,UACV;OACE,IAAI,SAAS,GAAG,IAAI,QAAQ;IAE9B;CAIJ,MAAM,SAAS,CACd,GAAG,OA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CAGC,CAAC,CACA,KAAK,EAAE,EAAE;WACH,MAAM,QAAQ,OAAO,KAAM,EAAE;MAEpC;CAGH,MAAM,QAAQ,CAAC,MAAM,GAAI,MAAM,WAAW,CAAC,CAAE,CAAC,CAC5C,KAAK,SAAS,WAAW,MAAM,CAAC,CAChC,KAAK,IAAI;CAGX,MAAM,aAAa,MAAM,WAAW,SAAS,CAAC,EAAA,CAC5C,KACC,SAAS;;UAEH,KAAK,IAAI,YAAY;SACtB,MAAM,WAAW,GAAG;EAE3B,CAAC,CACA,KAAK,EAAE;CAET,OAAO,GAAG,MAAM,IAAI,cAAc,aAAa,SAAS,EAAE;EACzD,SAAS,KAAK;GACb;AACH;;AAGA,SAAS,WAAW,OAA0B;CAC7C,MAAM,EAAE,WAAW,QAAQ,cAAc;CACzC,MAAM,SAAS,YAAY,KAAK;CAChC,OAAO,SACJ;;;iBAGa,UAAU,IAAI,OAAO,CAAC,CAAC,KAAK,GAAG,EAAE;YACtC,OAAO;sCACmB,OAAO;MAEzC;;;iBAGa,UAAU,IAAI,OAAO,CAAC,CAAC,KAAK,GAAG,IAC9C,aAAa,SACV,KACA,YACG;;;uBAIA,KACD,OAAO;MAET;AAEL;;;;;;;;;AAUA,SAAS,YAAY,EAAE,UAA6B;CACnD,OAAO,SACJ;;;eAGW,OAAO;;uBAGlB;AACJ;AAEA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;AAqBf,SAAgB,aAAa,WAAuC;CACnE,IAAI,CAAC,WACJ,OAAO;;;mBAGU;CAElB,OAAO;;;;2BAImB,kBAAkB,GAAG,UAAU;gCAC1B;AAChC"}
1
+ {"version":3,"file":"caddy.mjs","names":[],"sources":["../../src/proxy/caddy.ts"],"sourcesContent":["/**\n * The stack's own edge, rendered for Caddy: one site block per route.\n *\n * Deployed, each host is the stage's real domain and Caddy gets it a\n * certificate from Let's Encrypt on first request — the same automatic HTTPS\n * it does anywhere, with nothing to configure but the name — or serves the\n * stage's own certificate (`deploy.compose.tls`). Locally the hosts are\n * `*.localhost` and the certificate is the one gkm's local authority issued,\n * as the `gkm dev` edge's is, so a local stack is `https://` like a deployed\n * one.\n *\n * Pure: the routes are data (`@geekmidas/edge/routes`), and rendering them is one string.\n */\n\nimport { CLIENT_IP_HEADER, EDGE_TOKEN_HEADER } from '@geekmidas/manifest';\nimport {\n\tassertRouteRenderable,\n\ttype EdgeRoute,\n\ttype EdgeTls,\n\ttype Upstream,\n\tupstreamAddress,\n} from '../routes';\n\n/** Where Caddy reads a stage's own certificate, inside its container. */\nexport const CADDY_TLS_DIR = '/etc/caddy/tls';\n\n/** The origin certificate a CDN issued, as the stack's Caddy reads it. */\nexport const CADDY_ORIGIN_CERTIFICATE = {\n\tcertFile: `${CADDY_TLS_DIR}/origin-cert.pem`,\n\tkeyFile: `${CADDY_TLS_DIR}/origin-key.pem`,\n} as const;\n\nexport interface CaddyfileOptions {\n\ttls: EdgeTls;\n\t/**\n\t * The stack's token, on which a service of the stack keeps gkm's client\n\t * address through the edge — see `clientIpRule`.\n\t */\n\tedgeToken?: string;\n\t/** Where ACME sends expiry notices, when someone wants them. */\n\temail?: string;\n\t/**\n\t * `false` on the local stage: plain HTTP is not sent to HTTPS, whose\n\t * published port there may not be 443.\n\t */\n\tredirects?: boolean;\n}\n\n/** An upstream on the stack's own network — its service name — or on the host. */\nfunction address(upstream: Upstream): string {\n\treturn upstreamAddress(upstream);\n}\n\n/** The Caddyfile for a stack's edge. */\nexport function edgeCaddyfile(\n\troutes: readonly EdgeRoute[],\n\toptions: CaddyfileOptions,\n): string {\n\tconst { tls } = options;\n\t// A wildcard site certified host by host: Caddy asks its `hosts` route\n\t// before it gets a certificate for one, so a name no tenant has is never\n\t// sent to Let's Encrypt.\n\tconst ask = routes.find((route) => route.wildcard?.tls.kind === 'on-demand')\n\t\t?.wildcard?.tls;\n\tconst onDemand =\n\t\task?.kind === 'on-demand'\n\t\t\t? `\n\ton_demand_tls {\n\t\task ${ask.ask}\n\t}`\n\t\t\t: '';\n\tconst settings = `${options.email ? `\\n\\temail ${options.email}` : ''}${\n\t\toptions.redirects === false ? '\\n\\tauto_https disable_redirects' : ''\n\t}${onDemand}`;\n\tconst global = settings ? `{${settings}\\n}` : '';\n\n\tconst certificate =\n\t\ttls.kind === 'files'\n\t\t\t? `\n\ttls ${tls.certFile} ${tls.keyFile}\n`\n\t\t\t: '';\n\n\t// The wildcard site last: Caddy prefers a named host to `*.<domain>`\n\t// wherever the blocks are, and reading it last says the same.\n\tconst blocks = [\n\t\t...routes.filter((route) => !route.wildcard),\n\t\t...routes.filter((route) => route.wildcard),\n\t].map((route) =>\n\t\tsiteBlock(\n\t\t\troute,\n\t\t\toptions.edgeToken,\n\t\t\troute.wildcard?.tls.kind === 'on-demand'\n\t\t\t\t? `\n\ttls {\n\t\ton_demand\n\t}\n`\n\t\t\t\t: route.certificate?.kind === 'files'\n\t\t\t\t\t? `\n\ttls ${route.certificate.certFile} ${route.certificate.keyFile}\n`\n\t\t\t\t\t: certificate,\n\t\t),\n\t);\n\n\t// No blank line under the header: `caddy fmt` wants the first block there.\n\treturn `${HEADER}${global ? `${global}\\n\\n` : ''}${blocks.join('\\n\\n')}\\n`;\n}\n\n/** One route's site block. */\nfunction siteBlock(\n\troute: EdgeRoute,\n\tedgeToken: string | undefined,\n\tcertificate: string,\n): string {\n\tassertRouteRenderable(route);\n\tconst { host, allow, auth, paths, prefixes, requireHeader, handlers } = route;\n\tconst allowed = [\n\t\t...(paths ?? []),\n\t\t...(prefixes ?? []).map((prefix) => `${prefix}*`),\n\t];\n\n\tconst except = route.wildcard?.except ?? [];\n\tconst checks = `${\n\t\texcept.length\n\t\t\t? `\n\t# Labels no tenant may take, although nothing in this stack answers them.\n\t@protected host ${except.map((label) => `${label}.${route.wildcard?.domain}`).join(' ')}\n\trespond @protected 404\n`\n\t\t\t: ''\n\t}${\n\t\tallow\n\t\t\t? `\n\t# Only these addresses — the peer Caddy sees, never a header a client\n\t# could set. Everything else is refused before it reaches the upstream.\n\t@denied not remote_ip ${allow.join(' ')}\n\trespond @denied 403\n`\n\t\t\t: ''\n\t}${\n\t\trequireHeader\n\t\t\t? `\n\t# Only what the CDN sends: it adds this header to every request, and this\n\t# host would be reached around the CDN otherwise.\n\t@forged not header ${requireHeader.name} \"${requireHeader.value}\"\n\trespond @forged 403\n`\n\t\t\t: ''\n\t}${\n\t\tauth\n\t\t\t? `\n\t# The stage's login, asked for before anything reaches the upstream.\n\tbasic_auth {\n\t\t${auth.user} ${auth.hash}\n\t}\n`\n\t\t\t: ''\n\t}${\n\t\tallowed.length\n\t\t\t? `\n\t# Only these paths answer. Everything else on this host is refused before\n\t# it reaches the upstream.\n\t@elsewhere not path ${allowed.join(' ')}\n\trespond @elsewhere 404\n`\n\t\t\t: ''\n\t}`;\n\n\tconst proxy = proxyBlock(route);\n\tconst body = handlers?.length\n\t\t? `${handlers\n\t\t\t\t.map(\n\t\t\t\t\t(handler, index) => `\n\t# Answered by another service: ${handler.name}.\n\t@handler${index} path ${handler.prefixes.map((prefix) => `${prefix}*`).join(' ')}\n\thandle @handler${index} {\n\t\treverse_proxy ${handler.upstreams.map(address).join(' ')}${\n\t\t\thandler.under\n\t\t\t\t? ` {\n\t\t\trewrite ${handler.under}{uri}\n\t\t}`\n\t\t\t\t: ''\n\t\t}\n\t}\n`,\n\t\t\t\t)\n\t\t\t\t.join('')}\n\thandle {${proxy.replace(/\\n/g, '\\n\\t')}\n\t}`\n\t\t: proxy;\n\n\t// Its aliases join the block: every check and handler holds on each.\n\tconst names = [host, ...(route.aliases ?? [])]\n\t\t.map((name) => `https://${name}`)\n\t\t.join(', ');\n\t// Each redirect host, a block of its own that sends the browser to the\n\t// canonical host, path and query kept, and proxies nothing.\n\tconst redirects = (route.redirects?.hosts ?? [])\n\t\t.map(\n\t\t\t(from) => `\n\nhttps://${from} {${certificate}\n\tredir ${route.redirects?.to}{uri} permanent\n}`,\n\t\t)\n\t\t.join('');\n\n\treturn `${names} {${certificate}${clientIpRule(edgeToken)}\n${checks}${body}\n}${redirects}`;\n}\n\n/** Where a route's requests go, when no handler took them. */\nfunction proxyBlock(route: EdgeRoute): string {\n\tconst { upstreams, prefix, streaming } = route;\n\tconst health = healthCheck(route);\n\treturn prefix\n\t\t? `\n\t# A bucket, served at a host of its own: the bucket is a prefix on MinIO,\n\t# which routes and signs on the Host header — so it is sent the upstream's.\n\treverse_proxy ${upstreams.map(address).join(' ')} {\n\t\trewrite ${prefix}{uri}\n\t\theader_up Host {upstream_hostport}${health}\n\t}`\n\t\t: `\n\t# Host and X-Forwarded-* are passed through as Caddy does by default, so\n\t# an app builds redirects, cookies and links on the address a caller used.\n\treverse_proxy ${upstreams.map(address).join(' ')}${\n\t\tstreaming || health\n\t\t\t? ` {${\n\t\t\t\t\tstreaming\n\t\t\t\t\t\t? `\n\t\t# Never buffer: a streamed response — server-sent events, a Next.js\n\t\t# RSC payload — reaches the client as each chunk is written.\n\t\tflush_interval -1`\n\t\t\t\t\t\t: ''\n\t\t\t\t}${health}\n\t}`\n\t\t\t: ''\n\t}`;\n}\n\n/**\n * Caddy's own check of each upstream, as the Traefik edge's\n * `loadBalancer.healthCheck`: one that stops answering is taken out of\n * rotation until it does again. Caddy starts once its apps have — not once\n * they are healthy — so this is what keeps a request from an upstream that\n * is not up, and a route to an app with none up answers an error (503)\n * rather than going missing.\n */\nfunction healthCheck({ health }: EdgeRoute): string {\n\treturn health\n\t\t? `\n\t\t# Each upstream asked on its own: one that stops answering is out of\n\t\t# rotation until it answers again.\n\t\thealth_uri ${health}\n\t\thealth_interval 5s\n\t\thealth_timeout 3s`\n\t\t: '';\n}\n\nconst HEADER = `# Generated by gkm compose from the construct manifest — do not edit.\n#\n# One host per API and site, each in front of its own container, one per\n# file server in front of the stack's MinIO — its processors' paths sent to\n# their own service — one per worker SNS pushes to and per processor unit a\n# CDN reaches, answering only those paths, and — when its logs are served\n# publicly — one for OpenObserve, to the addresses it allows. A site the\n# stage puts behind a password asks for its login first.\n`;\n\n/**\n * The lines every site block of an edge opens with, for gkm's own\n * `x-gkm-client-ip` — the client address the auth server rate-limits by.\n *\n * Only a service of this stack may send it. One that calls a sibling at its\n * public address comes through this edge, and proves where it comes from with\n * the stack's token (`x-gkm-edge-token`), derived from the stage's seed and\n * handed to every process of the stack — never to a browser. A request\n * without it has the header removed, so nobody outside can choose the address\n * they are rate-limited by. Without a token, it is removed from every request.\n */\nexport function clientIpRule(edgeToken: string | undefined): string {\n\tif (!edgeToken) {\n\t\treturn `\n\t# gkm's own header, which only a service of this stack may send: the auth\n\t# server rate-limits by the client address it carries.\n\trequest_header -${CLIENT_IP_HEADER}`;\n\t}\n\treturn `\n\t# gkm's own header, which only a service of this stack may send — proven by\n\t# the stack's token: the auth server rate-limits by the client address it\n\t# carries. Removed from every other request.\n\t@gkm_outside not header ${EDGE_TOKEN_HEADER} ${edgeToken}\n\trequest_header @gkm_outside -${CLIENT_IP_HEADER}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAwBA,MAAa,gBAAgB;;AAG7B,MAAa,2BAA2B;CACvC,UAAU,GAAG,cAAc;CAC3B,SAAS,GAAG,cAAc;AAC3B;;AAmBA,SAAS,QAAQ,UAA4B;CAC5C,OAAO,gBAAgB,QAAQ;AAChC;;AAGA,SAAgB,cACf,QACA,SACS;CACT,MAAM,EAAE,QAAQ;CAIhB,MAAM,MAAM,OAAO,MAAM,UAAU,MAAM,UAAU,IAAI,SAAS,WAAW,CAAC,EACzE,UAAU;CACb,MAAM,WACL,KAAK,SAAS,cACX;;QAEG,IAAI,IAAI;MAEX;CACJ,MAAM,WAAW,GAAG,QAAQ,QAAQ,aAAa,QAAQ,UAAU,KAClE,QAAQ,cAAc,QAAQ,oCAAqC,KACjE;CACH,MAAM,SAAS,WAAW,IAAI,SAAS,OAAO;CAE9C,MAAM,cACL,IAAI,SAAS,UACV;OACE,IAAI,SAAS,GAAG,IAAI,QAAQ;IAE9B;CAIJ,MAAM,SAAS,CACd,GAAG,OAAO,QAAQ,UAAU,CAAC,MAAM,QAAQ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,QAAQ,QACL;aACO,QAAQ,MAAM;OAErB,GACH;;CAGC,CAAC,CACA,KAAK,EAAE,EAAE;WACH,MAAM,QAAQ,OAAO,KAAM,EAAE;MAEpC;CAGH,MAAM,QAAQ,CAAC,MAAM,GAAI,MAAM,WAAW,CAAC,CAAE,CAAC,CAC5C,KAAK,SAAS,WAAW,MAAM,CAAC,CAChC,KAAK,IAAI;CAGX,MAAM,aAAa,MAAM,WAAW,SAAS,CAAC,EAAA,CAC5C,KACC,SAAS;;UAEH,KAAK,IAAI,YAAY;SACtB,MAAM,WAAW,GAAG;EAE3B,CAAC,CACA,KAAK,EAAE;CAET,OAAO,GAAG,MAAM,IAAI,cAAc,aAAa,SAAS,EAAE;EACzD,SAAS,KAAK;GACb;AACH;;AAGA,SAAS,WAAW,OAA0B;CAC7C,MAAM,EAAE,WAAW,QAAQ,cAAc;CACzC,MAAM,SAAS,YAAY,KAAK;CAChC,OAAO,SACJ;;;iBAGa,UAAU,IAAI,OAAO,CAAC,CAAC,KAAK,GAAG,EAAE;YACtC,OAAO;sCACmB,OAAO;MAEzC;;;iBAGa,UAAU,IAAI,OAAO,CAAC,CAAC,KAAK,GAAG,IAC9C,aAAa,SACV,KACA,YACG;;;uBAIA,KACD,OAAO;MAET;AAEL;;;;;;;;;AAUA,SAAS,YAAY,EAAE,UAA6B;CACnD,OAAO,SACJ;;;eAGW,OAAO;;uBAGlB;AACJ;AAEA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;AAqBf,SAAgB,aAAa,WAAuC;CACnE,IAAI,CAAC,WACJ,OAAO;;;mBAGU;CAElB,OAAO;;;;2BAImB,kBAAkB,GAAG,UAAU;gCAC1B;AAChC"}
@@ -69,7 +69,8 @@ const ACME_RESOLVER = "letsencrypt";
69
69
  */
70
70
  const EDGE_AWS_CREDENTIALS = `${EDGE_CERTS_DIR}/aws-credentials`;
71
71
  /**
72
- * Every DNS provider the edge can get a wildcard certificate through, by the
72
+ * Every DNS provider the edge can get a certificate through over the DNS
73
+ * challenge — a wildcard's, or a host a CDN answers — by the
73
74
  * name a workspace's `dns` entry gives it. Another provider is one entry
74
75
  * here — lego's code for it and the variables it reads — and nothing else:
75
76
  * its resolver, its ACME storage and its edge environment follow from it.
@@ -90,6 +91,10 @@ const ACME_DNS_CHALLENGES = {
90
91
  godaddy: {
91
92
  provider: "godaddy",
92
93
  secrets: ["GODADDY_API_KEY", "GODADDY_API_SECRET"]
94
+ },
95
+ cloudflare: {
96
+ provider: "cloudflare",
97
+ secrets: ["CF_DNS_API_TOKEN"]
93
98
  }
94
99
  };
95
100
  /** Whether the edge can answer the DNS challenge through `provider`. */
@@ -149,6 +154,16 @@ function edgeCertificatePaths(project) {
149
154
  };
150
155
  }
151
156
  /**
157
+ * A stack's origin certificate — the one its CDN issued, served for the
158
+ * hosts the CDN answers — as the edge's container reads it.
159
+ */
160
+ function edgeOriginCertificatePaths(project) {
161
+ return {
162
+ certFile: `${EDGE_CERTS_DIR}/${project}-origin.crt`,
163
+ keyFile: `${EDGE_CERTS_DIR}/${project}-origin.key`
164
+ };
165
+ }
166
+ /**
152
167
  * The edge's static configuration: the entrypoints, the redirect, ACME, and
153
168
  * the directory it watches. No API and no dashboard — nothing about the edge
154
169
  * is reachable but the routes stacks register.
@@ -248,6 +263,7 @@ function traefikDynamic(routes, options) {
248
263
  const routers = {};
249
264
  const services = {};
250
265
  const middlewares = {};
266
+ const stored = new Map(tls.kind === "files" ? [[tls.certFile, tls.keyFile]] : []);
251
267
  const strip = `${project}-strip-gkm-headers`;
252
268
  if (routes.length > 0) middlewares[strip] = { headers: { customRequestHeaders: { [_geekmidas_manifest.CLIENT_IP_HEADER]: "" } } };
253
269
  for (const route of routes) {
@@ -284,10 +300,11 @@ function traefikDynamic(routes, options) {
284
300
  const allowed = [...(route.paths ?? []).map((path) => `Path(\`${path}\`)`), ...(route.prefixes ?? []).map((prefix) => `PathPrefix(\`${prefix}\`)`)];
285
301
  const paths = allowed.length ? ` && (${allowed.join(" || ")})` : "";
286
302
  const header = route.requireHeader ? ` && Header(\`${route.requireHeader.name}\`, \`${route.requireHeader.value}\`)` : "";
303
+ if (route.certificate?.kind === "files") stored.set(route.certificate.certFile, route.certificate.keyFile);
287
304
  const certificate = () => route.wildcard?.tls.kind === "dns" ? {
288
305
  certResolver: acmeDnsResolver(route.wildcard.tls.provider),
289
306
  domains: [{ main: `*.${route.wildcard.domain}` }]
290
- } : tls.kind === "acme" ? { certResolver: ACME_RESOLVER } : {};
307
+ } : route.certificate?.kind === "files" || tls.kind !== "acme" ? {} : route.certificate?.kind === "dns" ? { certResolver: acmeDnsResolver(route.certificate.provider) } : { certResolver: ACME_RESOLVER };
291
308
  for (const handler of route.handlers ?? []) {
292
309
  const handlerName = name(route, handler.name);
293
310
  const under = name(route, `${handler.name}-under`);
@@ -356,10 +373,10 @@ function traefikDynamic(routes, options) {
356
373
  services,
357
374
  ...Object.keys(middlewares).length > 0 ? { middlewares } : {}
358
375
  },
359
- ...tls.kind === "files" ? { tls: { certificates: [{
360
- certFile: tls.certFile,
361
- keyFile: tls.keyFile
362
- }] } } : {}
376
+ ...stored.size > 0 ? { tls: { certificates: [...stored].map(([certFile, keyFile]) => ({
377
+ certFile,
378
+ keyFile
379
+ })) } } : {}
363
380
  };
364
381
  }
365
382
  /**
@@ -516,6 +533,7 @@ exports.edgeClaims = edgeClaims;
516
533
  exports.edgeCompose = edgeCompose;
517
534
  exports.edgeComposeFile = edgeComposeFile;
518
535
  exports.edgeDnsSecretPath = edgeDnsSecretPath;
536
+ exports.edgeOriginCertificatePaths = edgeOriginCertificatePaths;
519
537
  exports.edgeProject = edgeProject;
520
538
  exports.isAcmeDnsProvider = isAcmeDnsProvider;
521
539
  exports.readEdgeClaims = readEdgeClaims;