docs: PDFs page, plans cap, pdf-host README
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+330
−21
README.md+2 −0docs/site/nav.json+5 −0docs/site/pdfs.md+155 −0docs/site/plans.md+9 −5infra/modules/README.md+5 −5infra/modules/pdf-host/README.md+136 −0infra/modules/pdf-host/outputs.tf+2 −1infra/modules/pdf-host/variables.tf+16 −10
diff --git a/README.md b/README.md| index 559ad7d4b..ce4db033a 100644 |
| --- a/README.md |
| +++ b/README.md |
| @@ -1192,6 +1192,8 @@ from private networks: |
| "doc_engine": true |
| } |
| +For most apps, the [platform PDF service](docs/site/pdfs.md) is simpler than running your own engine with `doc_engine`. |
| + |
| `command`, `daemon`, `children`, `runtime_dir` and `data` are self-service — |
| they shape only the app's own sandbox and materialize from the manifest |
| alone. `ports`, `host_role` and `doc_engine` are operator-consented: declaring |
diff --git a/docs/site/nav.json b/docs/site/nav.json| index 9380398b8..3a7cef08c 100644 |
| --- a/docs/site/nav.json |
| +++ b/docs/site/nav.json |
| @@ -33,6 +33,11 @@ |
| "label": "Receiving scan results", |
| "blurb": "Ask for one object to be scanned, read the verdict, and verify the callback that tells you it is done." |
| }, |
| + { |
| + "slug": "pdfs", |
| + "label": "PDFs", |
| + "blurb": "Render an invoice, attach it to an email, and use Gotenberg from your app." |
| + }, |
| { |
| "slug": "plans", |
| "label": "Plans", |
diff --git a/docs/site/pdfs.md b/docs/site/pdfs.md| new file mode 100644 |
| index 000000000..4171b86f1 |
| --- /dev/null |
| +++ b/docs/site/pdfs.md |
| @@ -0,0 +1,155 @@ |
| +# PDFs |
| + |
| +Your app can turn an HTML invoice into a PDF with one request. The platform |
| +runs Gotenberg for you. You can also convert Office documents, merge PDFs |
| +and use the other routes listed below. |
| + |
| +## Creating a credential |
| + |
| +Create a Service credential for your app and save the returned secret: |
| + |
| +```sh |
| +carlos services create --role service --app shop pdf |
| +carlos secrets set --app shop CARLOS_SERVICE_TOKEN=<secret> |
| +``` |
| + |
| +Replace `<secret>` with the secret from the first command. Your app reads it |
| +from `CARLOS_SERVICE_TOKEN`. If your app already has a Service credential |
| +for scanning, you can use that one. |
| + |
| +The credential must belong to the account in the URL and cover your app. |
| +An app-bound credential reaches only that app. An account-wide Service |
| +credential can reach the account's apps. |
| + |
| +## Rendering an invoice |
| + |
| +Have your app render the invoice as HTML and save it as `index.html`. |
| +For a page behind a login, send the HTML rather than its URL. |
| + |
| +The endpoint is: |
| + |
| +```http |
| +POST /api/cli/apps/{account}/{app}/pdf/forms/chromium/convert/html |
| +Authorization: Bearer <service token> |
| +``` |
| + |
| +For `shop`, with your console at [https://example.com](https://example.com), |
| +run this where `CARLOS_SERVICE_TOKEN` holds the secret. Replace `{account}` |
| +with your account ID: |
| + |
| +```sh |
| +curl --fail-with-body \ |
| + --header "Authorization: Bearer $CARLOS_SERVICE_TOKEN" \ |
| + --form 'files=@index.html' \ |
| + --output invoice.pdf \ |
| + 'https://example.com/api/cli/apps/{account}/shop/pdf/forms/chromium/convert/html' |
| +``` |
| + |
| +`files` is Gotenberg's multipart form field. Keep the HTML filename |
| +`index.html`. You can add images and stylesheets as more `files` fields. |
| +Public URLs can supply fonts, images and stylesheets. The engine cannot |
| +fetch private addresses or cloud instance metadata. |
| + |
| +Your app receives the PDF in the response body. Gotenberg's status code, |
| +`Content-Type`, `Content-Disposition` and `Gotenberg-Trace` pass through. |
| +Check the status before serving the file to a customer. |
| + |
| +## Attaching the PDF to an email |
| + |
| +Attach `invoice.pdf` with your app's mail library and send it through the |
| +SMTP credentials delivered to your app. See [the email docs](/docs/cli#carlos-email) |
| +for setup. Your app builds the message and its attachments. |
| + |
| +## Using a Gotenberg client |
| + |
| +Set your client's base URL to |
| +[https://example.com/api/cli/apps/{account}/shop/pdf](https://example.com/api/cli/apps/{account}/shop/pdf), |
| +using your console origin and account ID. Set this header on each request: |
| + |
| +```http |
| +Authorization: Bearer <service token> |
| +``` |
| + |
| +Keep the client's Gotenberg routes and form fields. For example, appending |
| +`/forms/chromium/convert/html` to that base reaches the invoice endpoint. |
| +Your app can read its console origin from `CARLOS_CONSOLE` or use the origin |
| +you configured. |
| + |
| +`Gotenberg-Output-Filename` and `Gotenberg-Trace` request headers pass |
| +through. Webhook headers are refused. Other `Gotenberg-*` request headers |
| +are stripped. Rendering is synchronous: your app waits for the response. |
| + |
| +## Routes |
| + |
| +Each route below accepts `POST` under `/api/cli/apps/{account}/{app}/pdf/`: |
| + |
| +```text |
| +forms/chromium/convert/html |
| +forms/chromium/convert/url |
| +forms/chromium/convert/markdown |
| +forms/chromium/screenshot/html |
| +forms/chromium/screenshot/url |
| +forms/chromium/screenshot/markdown |
| +forms/libreoffice/convert |
| +forms/pdfengines/merge |
| +forms/pdfengines/split |
| +forms/pdfengines/flatten |
| +forms/pdfengines/convert |
| +forms/pdfengines/metadata/read |
| +forms/pdfengines/metadata/write |
| +forms/pdfengines/encrypt |
| +forms/pdfengines/embed |
| +``` |
| + |
| +URL rendering works for public pages. `downloadFrom` form fields are also |
| +allowed. The same private-address restrictions apply to both. |
| + |
| +Other routes return `404`. Your app cannot use this endpoint to reach the |
| +engine's `/health`, `/version`, `/prometheus/metrics` or `/debug` routes. |
| + |
| +## Limits |
| + |
| +Fair-use limits apply on every deployment. If your deployment has a plan |
| +catalog, your account's monthly PDF allowance also applies. Your apps share |
| +the account limits. |
| + |
| +The fair-use defaults are: |
| + |
| +- Up to 4 requests running at once per account. |
| +- 60 requests a minute per account, with a burst of 20. |
| +- Up to 16 requests running at once across the deployment. |
| + |
| +Your operator can change these defaults. When a rate or concurrency limit |
| +refuses a request, wait for the number of seconds in `Retry-After` before |
| +retrying. The default concurrency wait is 2 seconds. |
| + |
| +The request body can be at most 32 MiB. A response over 64 MiB is cut off, |
| +so do not use a partial file. The console waits up to 60 seconds for the |
| +engine's response headers. A stalled transfer can also be cut off. |
| + |
| +One completed request with a `2xx` engine response counts toward your |
| +monthly allowance. Engine errors, console refusals and aborted transfers |
| +do not count. At twice your allowance, new requests are refused until the |
| +1st of the UTC month or until you move to a larger plan. See [Plans](/docs/plans). |
| +With no plan catalog, only fair-use limits apply. |
| + |
| +These are the PDF refusal messages. `%d` stands for a count filled in by |
| +the console. In the monthly refusal, the counts are your usage and your |
| +monthly limit, in that order. |
| + |
| +| Status | Message | |
| +| --- | --- | |
| +| `400` | `webhooks are not available here. send the PDF request without a webhook header` | |
| +| `402` | `this account has rendered %d PDFs this month, twice its monthly limit of %d. rendering starts again on the 1st of the UTC month, or move to a larger plan` | |
| +| `404` | `that is not a PDF route. the PDFs docs page lists the routes this door carries` | |
| +| `413` | `the request is over 32 MiB. send a smaller request` | |
| +| `429` | `this account already has %d PDFs rendering. wait for one to finish and try again` | |
| +| `429` | `this account has asked for more than %d PDFs in a minute. try again in a moment` | |
| +| `502` | `the PDF engine did not answer. try again shortly` | |
| +| `503` | `no PDF engine is configured for this deployment (CARLOS_PDF_ADDR)` | |
| +| `503` | `the PDF engine is busy. try again in a few seconds` | |
| +| `504` | `the PDF engine took more than 60 seconds and was stopped. try again` | |
| + |
| +If no engine is configured, your operator needs to configure one before |
| +your app can render PDFs. Gotenberg can also return its own errors for the |
| +files or form fields you send. |
diff --git a/docs/site/plans.md b/docs/site/plans.md| index 5f7e15dd1..c2c0a3ae5 100644 |
| --- a/docs/site/plans.md |
| +++ b/docs/site/plans.md |
| @@ -34,6 +34,8 @@ The console reads the file when it starts. If the file is missing or wrong, the |
| Every account without a plan of its own is on `default_plan`. Packs may only raise counts and sizes: apps and instances, disk, storage, email and bandwidth. A ceiling is the most any plan with packs can reach. |
| +Set `pdf_renders_month` in a plan's limits to give your account a monthly PDF allowance shared by its apps. A value of 0 means no monthly limit. The PDF service's fair-use limits still apply. |
| + |
| Change `version` whenever you change the file. The console notices an edit that kept the old version and treats it as a new catalog anyway, so every account's limits are written again. |
| ## Assigning a plan |
| @@ -66,28 +68,30 @@ A plan can cap how far an account may raise an instance's rate limits, with `rat |
| ## Warnings and grace |
| -Once an hour the console compares each account's usage with its limits, for five caps: |
| +Once an hour the console compares each account's usage with its limits, for six caps: |
| - apps and instances running; |
| - object storage; |
| - emails sent today; |
| - bandwidth this month; |
| -- awake hours this month. |
| +- awake hours this month; |
| +- PDFs rendered this month. |
| -The figures come from what the console already records: its own app list, the monthly usage statements, the daily storage sweep and the mail counts. A cap whose limit is 0 is skipped. |
| +The figures come from what the console already records: its own app list, the monthly usage statements, the daily storage sweep, the mail counts and completed PDF requests. A cap whose limit is 0 is skipped. |
| At 80% of a limit, the account's owners get an email, and a banner appears on every account page. A `carlos` command run against the account prints a line starting `warning:`, from a CLI new enough to print it. |
| Going over the limit starts a grace period: |
| - 7 days for apps and instances, and for storage; |
| - the rest of the UTC day for email; |
| -- the rest of the UTC month for bandwidth and awake hours. |
| +- the rest of the UTC month for bandwidth, awake hours and PDFs. |
| While an account is over, its owners get one reminder a day, saying when grace ends and how to get back under: buy a pack, move to a bigger plan, or clean up. |
| After grace, only the capped action is refused, and each refusal lifts by itself: |
| - **Email.** When an account has sent twice its daily limit, every sender of every app is paused until midnight UTC. This does not wait for notice, because sending reputation is shared by every tenant. The catalog's email ceiling caps it. |
| +- **PDFs.** At twice your account's monthly PDF allowance, new requests are refused with `402` until the 1st of the UTC month or until you move to a larger plan. This does not wait for notice. Your apps share the allowance, and the PDF service's fair-use limits still apply. |
| - **Storage.** New uploads to the account's stores are refused once grace has ended and an owner has had notice for 3 days, or straight away at twice the limit. Downloads, listing and deletes keep working, so the account can clean up. Uploads come back once usage is under the limit or the limit is raised. |
| - **Bandwidth.** At twice the month's limit, the account's apps are served more slowly until the month ends or the plan grows: one shared allowance of 5 requests a second, with bursts up to 30, across all its instances. This does not wait for notice, because twice the limit is bandwidth's hard ceiling. |
| @@ -97,7 +101,7 @@ The slowdown is a plan stage, not abuse protection, so it applies whatever `CARL |
| The account banner and the CLI warning line say which stage is in force. |
| -The console also records when an owner actually received notice: an email that was accepted for delivery, an owner viewing an account page with the banner, or an owner's CLI printing the warning. Apart from the ceilings above (email, storage and bandwidth at twice their limits), whatever acts after grace starts only once grace has ended and an owner received notice at least 3 days earlier. So an account nobody has heard from is never cut off for anything short of a ceiling. |
| +The console also records when an owner actually received notice: an email that was accepted for delivery, an owner viewing an account page with the banner, or an owner's CLI printing the warning. Apart from the ceilings above (email, storage, bandwidth and PDFs at twice their limits), whatever acts after grace starts only once grace has ended and an owner received notice at least 3 days earlier. So an account nobody has heard from is never cut off for anything short of a ceiling. |
| ## When a plan lapses |
diff --git a/infra/modules/README.md b/infra/modules/README.md| index 3553d493e..ffd2578e1 100644 |
| --- a/infra/modules/README.md |
| +++ b/infra/modules/README.md |
| @@ -109,11 +109,11 @@ everything outside `ssm_parameter_arn`. It touches no other action, so |
| Session Manager is unaffected. If your boxes read a parameter outside that |
| pattern, widen the pattern rather than turning the deny off. |
| -Not a module: Tito's `svc/pdf` box stays in Tito's own root (`svc-pdf.tf`) — it |
| -is deliberately deployment-local (a stateless Gotenberg whose SG admits |
| -only the fleet, with *no* bucket or parameter grants; statelessness is |
| -imposed, not promised). Generalise it only when a second deployment |
| -wants one. |
| +Tito's hand-built `svc/pdf` box moves to `pdf-host` after Carloku proves the |
| +platform PDF service. Its current `svc-pdf.tf` stays in Tito's deployment |
| +root until that separately approved migration retires it. |
| + |
| +- `pdf-host`: one fenced Gotenberg box per deployment; set the console's `CARLOS_PDF_ADDR` from its `pdf_addr` output. |
| ## scanner-host |
diff --git a/infra/modules/pdf-host/README.md b/infra/modules/pdf-host/README.md| new file mode 100644 |
| index 000000000..aea112ff2 |
| --- /dev/null |
| +++ b/infra/modules/pdf-host/README.md |
| @@ -0,0 +1,136 @@ |
| +# pdf-host: a Gotenberg box for a deployment's console |
| + |
| +This module runs one Gotenberg container on a Debian arm64 EC2 box. The |
| +console sends PDF requests to its private address. TCP 3000 accepts only the |
| +console's security group. Administration uses SSM Session Manager; the role |
| +explicitly denies every SSM parameter read and has no bucket grants. |
| + |
| +```hcl |
| +module "pdf" { |
| + source = "git::https://github.com/carlosframework/platform.git//infra/modules/pdf-host?ref=<release-tag>" |
| + name = "carlos-flagship-pdf" |
| + ami_id = module.host_ie.ami_id |
| + client_security_group_id = module.host_ie.security_group_id |
| + region = "eu-west-1" |
| +} |
| +``` |
| + |
| +Replace `<release-tag>` with the platform release you are deploying. Pass the |
| +console box's base AMI and security group. The default instance is |
| +`t4g.medium`, with a 20 GiB encrypted gp3 root volume. For a non-default VPC, |
| +pass `subnet_id` in the client security group's VPC. The subnet needs internet |
| +access for packages, the image and public page assets. It must provide a private |
| +IPv4 address; IPv6-only subnets are unsupported. |
| + |
| +## Wiring and replacement |
| + |
| +Set the console's `CARLOS_PDF_ADDR` to the module's `pdf_addr` output |
| +(`http://<private_ip>:3000`) and restart the console after the acceptance |
| +checks below. The other outputs are `instance_id` and `security_group_id`. |
| + |
| +An AMI change, an init-script edit, a new `gotenberg_image` or a changed |
| +`api_timeout` or `memory_max` does not update a running box. Replace it explicitly: |
| + |
| +```sh |
| +tofu apply -replace=module.pdf.aws_instance.pdf |
| +``` |
| + |
| +Replacement changes the private address. Update `CARLOS_PDF_ADDR` and restart |
| +the console again. Production infrastructure changes require scoped approval. |
| + |
| +## Container fence |
| + |
| +The container uses Docker's default bridge. A separate nftables table checks |
| +forwarded container traffic before Docker's filter rules and checks traffic |
| +from the container to the host. It blocks every IPv4 destination from |
| +`internal/caps/render.go`'s `docEngineEgressDeny` and drops all container IPv6 |
| +traffic, including global IPv6 addresses and NAT64. It also reads every |
| +associated VPC IPv4 CIDR from instance metadata and drops those destinations, |
| +including VPC ranges outside the fixed list. The VPC resolver on UDP and TCP |
| +port 53 is the only destination exception. Its address is the primary VPC |
| +CIDR's base plus two, even when the instance uses a secondary CIDR. IPv4 |
| +replies to incoming console requests and host health probes can return. |
| + |
| +The host keeps its own metadata and private-network access, so Session |
| +Manager can work. The instance requires IMDSv2 with a hop limit of one. |
| +Gotenberg's published port binds only to the private IPv4 address. The network |
| +fence permits other public IPv4 destinations on all ports and protocols. |
| +Peered-VPC or transit-gateway destinations outside the fixed deny list and |
| +this VPC's own CIDRs remain reachable. Scope those routes separately before |
| +using this module where they expose private services on public IPv4 ranges. |
| + |
| +`pdf-fence.service` reads metadata and loads the rules before Docker on every |
| +boot. Docker requires that unit and repeats the metadata read and table load |
| +before every start. Unreadable metadata, empty or invalid VPC CIDRs, a list |
| +missing the primary CIDR, or a failed rule load prevents startup. CIDRs added |
| +while Docker is running enter the fence on its next start. The |
| +generic nftables service is masked so it cannot flush the table during boot. |
| +Docker's own rules live outside this table. The image starts with |
| +`--restart=always`; the API timeout defaults to `55s`. `memory_max` defaults to |
| +`3g`: the container has a 3 GiB RAM limit and no extra swap allowance, leaving |
| +1 GiB of the default t4g.medium's memory for the host and SSM agent. Set this |
| +limit for the instance size when overriding the default. The container drops |
| +all Linux capabilities and uses `no-new-privileges`; no capabilities are added |
| +back. |
| + |
| +## Version checked |
| + |
| +The default is Gotenberg **8.39.0**, pinned to the multi-architecture index |
| +`sha256:13672f317b443c793e972b1bd2155ec62a3e7b6891b411e8196442cc053ee5f5`. |
| +Its linux/arm64 manifest is |
| +`sha256:7d84f99323c1a9052f5d89881fcada68f8efcde32b64daf3c029f99cab23fd26`. |
| + |
| +The registry index was fetched and its SHA-256 checked without downloading |
| +image layers. The flag declarations at tag `v8.39.0` confirm `--api-timeout`, |
| +`--chromium-deny-list`, `--chromium-deny-private-ips`, |
| +`--api-download-from-deny-private-ips` and `--webhook-disable`. Chromium and |
| +`downloadFrom` reject private IP destinations inside the engine as a second |
| +layer. Webhooks are disabled, so the former webhook deny-list is redundant |
| +and removed. The Chromium pattern remains `^file:(?!//\/tmp/).*`, that |
| +version's default restriction on local file access; the IP flags do not |
| +replace it. These are source checks; the image was not run here. |
| + |
| +All 15 routes in the console's allow-list were checked against that tag's |
| +route definitions and registration: Chromium convert and screenshot for |
| +HTML, URL and Markdown; LibreOffice convert; PDF engines merge, split, |
| +flatten, convert, metadata read/write, encrypt and embed. The version's other |
| +routes remain outside the console allow-list. |
| + |
| +Sources: |
| + |
| +- [https://hub.docker.com/v2/repositories/gotenberg/gotenberg/tags/8.39.0](https://hub.docker.com/v2/repositories/gotenberg/gotenberg/tags/8.39.0) |
| +- [https://github.com/gotenberg/gotenberg/tree/v8.39.0/pkg/modules](https://github.com/gotenberg/gotenberg/tree/v8.39.0/pkg/modules) |
| + |
| +## Acceptance checks on the first real box |
| + |
| +Run these before wiring the console, as required by spec section 4.2. Record |
| +the results for the deployment. |
| + |
| +1. Render HTML referencing the metadata service, the console's private IP, |
| + another box's private IP, the bridge gateway and the VPC resolver on port |
| + 80. None may be fetched. Check the targets or packet counters as well as |
| + the output: a successful PDF response alone does not prove a blocked fetch. |
| +2. Render a page with a public image addressed by hostname. It must load. |
| + Check DNS over both UDP and TCP to the VPC resolver. |
| +3. Confirm Session Manager opens a session. Check that the console can reach |
| + port 3000 and a box outside its security group cannot. |
| +4. Restart Docker and reboot the box. Repeat the fetch checks and verify the |
| + fence is active before the container starts. In a controlled acceptance |
| + test, force the fence loader's nft invocation to fail, then separately make |
| + its metadata read fail. After each failure, reboot and confirm Docker stays |
| + stopped, port 3000 is closed and SSM still works. Restore the loader and |
| + repeat the checks. The loader regenerates `fence.nft` on startup, so editing |
| + only that generated file does not exercise a failed load. |
| +5. `ss -ltn` shows Docker publishing only the private IPv4 address on port |
| + 3000. Confirm the container has no global IPv6 address and |
| + `sysctl net.ipv6.conf.all.forwarding` reports `0`. |
| +6. With the private-IP flags, capability drop and memory cap enabled, convert |
| + uploaded HTML (its `file://` input under `/tmp`) and a LibreOffice document. |
| + Both must succeed; a public image by hostname must still load. |
| + |
| +First boot logs `PDF-HOST READY` or `PDF-HOST FAILED` in cloud-init output and, |
| +where available, the serial console. A ready message confirms the health |
| +endpoint answered; it does not replace the acceptance checks. |
| + |
| +--- |
| + |
diff --git a/infra/modules/pdf-host/outputs.tf b/infra/modules/pdf-host/outputs.tf| index 99b20f52e..e680a1f3e 100644 |
| --- a/infra/modules/pdf-host/outputs.tf |
| +++ b/infra/modules/pdf-host/outputs.tf |
| @@ -1,5 +1,6 @@ |
| output "pdf_addr" { |
| - value = "http://${aws_instance.pdf.private_ip}:3000" |
| + description = "Set the console's `CARLOS_PDF_ADDR` to the module's `pdf_addr` output\n(`http://<private_ip>:3000`)" |
| + value = "http://${aws_instance.pdf.private_ip}:3000" |
| } |
| output "instance_id" { |
diff --git a/infra/modules/pdf-host/variables.tf b/infra/modules/pdf-host/variables.tf| index 8426adfb8..27576f6af 100644 |
| --- a/infra/modules/pdf-host/variables.tf |
| +++ b/infra/modules/pdf-host/variables.tf |
| @@ -3,11 +3,13 @@ variable "name" { |
| } |
| variable "ami_id" { |
| - type = string |
| + description = "console box's base AMI" |
| + type = string |
| } |
| variable "client_security_group_id" { |
| - type = string |
| + description = "console's security group" |
| + type = string |
| } |
| variable "region" { |
| @@ -19,13 +21,15 @@ variable "region" { |
| } |
| variable "instance_type" { |
| - type = string |
| - default = "t4g.medium" |
| + description = "The default instance is\n`t4g.medium`, with a 20 GiB encrypted gp3 root volume." |
| + type = string |
| + default = "t4g.medium" |
| } |
| variable "subnet_id" { |
| - type = string |
| - default = null |
| + description = "For a non-default VPC,\npass `subnet_id` in the client security group's VPC." |
| + type = string |
| + default = null |
| } |
| variable "gotenberg_image" { |
| @@ -39,8 +43,9 @@ variable "gotenberg_image" { |
| } |
| variable "api_timeout" { |
| - type = string |
| - default = "55s" |
| + description = "the API timeout defaults to `55s`." |
| + type = string |
| + default = "55s" |
| validation { |
| condition = can(regex("^([0-9]+(\\.[0-9]+)?(ms|s|m|h))+$", var.api_timeout)) |
| error_message = "api_timeout must be a duration using ms, s, m or h." |
| @@ -48,8 +53,9 @@ variable "api_timeout" { |
| } |
| variable "memory_max" { |
| - type = string |
| - default = "3g" |
| + description = "the container has a 3 GiB RAM limit and no extra swap allowance" |
| + type = string |
| + default = "3g" |
| validation { |
| condition = can(regex("^[1-9][0-9]*[mMgG]$", var.memory_max)) |
| error_message = "memory_max must be a positive whole number of MiB or GiB using m or g." |