Skip to content

Commit 565aca4

Browse files
agjsclaude
andauthored
feat(infra): opt-in Cloudflare R2 remote-state backend (#167)
* feat(infra): add opt-in Cloudflare R2 remote-state backend Closes the last single point of failure in the protection story: state was local-only, so losing the operator's machine/tfstate meant losing the ability to plan, reconcile, or safely destroy the stack — worse now that the VPS carries delete_protection. Adds a pre-wired, opt-in path to Cloudflare R2 (S3-compatible, no egress fees, an account we already use). Default clone stays on local state and works unchanged; enabling remote state is a documented ~2-minute step. - main.tf: commented backend "s3" block pre-filled with the R2-specific flags (region=auto, use_path_style, skip_* preflight, skip_s3_checksum) and use_lockfile for native locking — no DynamoDB. Deployment-specific values (bucket, key, endpoints) come from backend.hcl via partial config. - backend.hcl.example: template for the per-deployment values; credentials stay in AWS_* env vars, never on disk. - .gitignore: ignore backend.hcl, keep the example. - README: "Remote state (optional)" section with the migrate-state steps. Verified on OpenTofu 1.12.0: fmt + validate clean; the s3 backend accepts every argument (a scratch init parsed the full config and only failed at the network layer against a deliberately-invalid endpoint). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs(infra): align tofu provisioning docs with VPS protection + R2 state The provisioning-with-tofu page lagged behind the infra changes: - "Destroying" told users `tofu destroy` wipes the server — now WRONG, since prevent_destroy + Hetzner delete_protection refuse it. Rewritten to document the deliberate two-step gate-lowering (apply to lift locks, then destroy). - "State management" showed a bare s3 block (bucket/key/region only). Replaced with the pre-wired Cloudflare R2 flow this PR adds: backend.hcl partial config, env-var credentials, use_lockfile, and init -migrate-state. - Prerequisites now call out the OpenTofu 1.12+ requirement. - terraform.tfvars shape documents prevent_server_destroy. - Design choices: updated the state bullet and added a defense-in-depth bullet covering both protection layers. Verified: astro build succeeds, check:rendered-markdown passes, and check:fragments confirms the new #destroying / #state-management / #design-choices anchors resolve. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * chore(security): allowlist two historical false-positive gitleaks findings The local pre-push hook runs full-history `gitleaks detect` (CI is path-filtered, so these slipped past on unrelated pushes). Two findings live only in old commit diffs; both current files are already clean: - add-service-to-compose.mdx: a Meilisearch example MEILI_MASTER_KEY value before it became the `<set-from-your-secret-manager>` placeholder. - Auth.session.mutations.utils.test.ts: fixture token `abcdef1234567890`, since changed to `test-challenge-token-stub`. Pin both by fingerprint in .gitleaksignore — narrowly scoped to these exact commits/lines; rules stay active everywhere else and for HEAD. Verified `gitleaks detect` (v8.30.1, the CI-pinned version) now reports no leaks. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent d53f168 commit 565aca4

6 files changed

Lines changed: 126 additions & 12 deletions

File tree

.gitleaksignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# gitleaks fingerprint allowlist for specific HISTORICAL findings.
2+
#
3+
# Format: <commit>:<file>:<rule>:<line>. Each entry ignores exactly one
4+
# finding at one commit — rules stay fully active for every other commit and
5+
# for the current working tree. Used for false positives that live only in old
6+
# commit diffs (the current files are already clean), where a line/value regex
7+
# in .gitleaks.toml can't match because the offending value no longer exists
8+
# in HEAD. The repo-wide allowlist for current placeholders lives in
9+
# .gitleaks.toml; this file is only for pinned past commits.
10+
11+
# Meilisearch recipe: an intermediate commit had a non-empty MEILI_MASTER_KEY
12+
# example value before it was changed to the `<set-from-your-secret-manager>`
13+
# placeholder (now allowlisted in .gitleaks.toml). Docs example, never a real key.
14+
9262aa98aebe8b93d055b28a98fb44b72097935b:apps/docs/src/content/docs/recipes/add-service-to-compose.mdx:generic-api-key:100
15+
16+
# Auth session test: the fixture token was `abcdef1234567890` at this commit
17+
# (looks hex-key-ish to generic-api-key) and has since been changed to the
18+
# clearer `test-challenge-token-stub`. Test stub, never a real secret.
19+
901fd55367183293112f393dbc4cb99127af3913:apps/ui/src/features/auth/Auth.session.mutations.utils.test.ts:generic-api-key:13

apps/docs/src/content/docs/topics/provisioning-with-tofu.mdx

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You'll need the following before starting:
1717
- **Cloudflare API token**: Cloudflare, My Profile, API Tokens, Custom Token. Scope: `Zone:DNS:Edit`, `Zone:Zone Settings:Edit`, `Zone:Rulesets:Edit` on the target zone.
1818
- **Cloudflare zone ID**: Zone overview page in the dashboard, right sidebar.
1919
- **SSH key**: Run `ssh-keygen -t ed25519` if you don't have one. You'll paste the `.pub` contents.
20-
- **OpenTofu binary**: `brew install opentofu` on macOS. See [install docs](https://opentofu.org/docs/intro/install/) for other OSes.
20+
- **OpenTofu binary**: `brew install opentofu` on macOS — version **1.12 or newer** (the stack uses variable-driven `prevent_destroy` and native state locking). See [install docs](https://opentofu.org/docs/intro/install/) for other OSes.
2121

2222
## Apply
2323

@@ -97,6 +97,10 @@ domain = "boringstack.example"
9797
vps_type = "cx32" # 4 vCPU / 8 GB
9898
vps_location = "fsn1"
9999
100+
# Protection (default true). Blocks tofu *and* Hetzner from destroying the
101+
# VPS; set false only for a deliberate rebuild or teardown.
102+
prevent_server_destroy = true
103+
100104
# Stack secrets
101105
jwt_secret = "..." # 32+ chars
102106
postgres_password = "..."
@@ -126,20 +130,31 @@ Everything in `terraform.tfvars.example` ships with comments explaining what it'
126130

127131
## State management
128132

129-
For a single operator: state file is local and gitignored (default config).
133+
State defaults to a local `terraform.tfstate` — gitignored, and fine for a single operator spinning something up quickly. But local state is a single point of failure: lose the file and you can no longer plan, reconcile, or safely destroy the stack — more so now that the VPS is delete-protected (see [Destroying](#destroying)).
130134

131-
For a team: point the OpenTofu backend at S3 (or any S3-compatible store like Cloudflare R2, Backblaze B2, or Hetzner Object Storage). Add one block to `main.tf`:
135+
For anything long-lived, move state to **Cloudflare R2** (S3-compatible, no egress fees, an account you already have for DNS). `main.tf` ships a ready-to-uncomment `backend "s3"` block pre-filled with the R2-specific flags (`use_path_style`, the `skip_*` preflight toggles, `skip_s3_checksum`) and `use_lockfile = true` for native locking — no DynamoDB table. Deployment-specific values live in a gitignored `backend.hcl`, copied from `backend.hcl.example`:
132136

133137
```hcl
134-
terraform {
135-
backend "s3" {
136-
bucket = "boringstack-tofu-state"
137-
key = "boringstack/terraform.tfstate"
138-
region = "..."
139-
}
138+
# backend.hcl
139+
bucket = "boringstack-tfstate"
140+
key = "bootstrap/terraform.tfstate"
141+
endpoints = {
142+
s3 = "https://<CLOUDFLARE_ACCOUNT_ID>.r2.cloudflarestorage.com"
140143
}
141144
```
142145

146+
Create an R2 bucket and an R2 API token (Object Read & Write), then migrate the existing local state:
147+
148+
```bash
149+
cp backend.hcl.example backend.hcl # fill in bucket + account id
150+
export AWS_ACCESS_KEY_ID=<r2 access key id>
151+
export AWS_SECRET_ACCESS_KEY=<r2 secret access key>
152+
# uncomment the backend "s3" block in main.tf, then:
153+
tofu init -backend-config=backend.hcl -migrate-state
154+
```
155+
156+
Credentials stay in the `AWS_*` env vars, never on disk. Hetzner Object Storage, Backblaze B2, or AWS S3 work the same way — only `endpoints.s3` (and the `skip_*`/`use_path_style` flags, for true AWS) differ.
157+
143158
The state file contains secrets (cloud-init renders with sensitive values). Encrypt at rest and restrict bucket access. Same security posture as everywhere else in BoringStack.
144159

145160
## Updating
@@ -176,11 +191,17 @@ The `bootstrap` module talks to cloud-init, which every major cloud accepts. Swa
176191

177192
## Destroying
178193

194+
The VPS is protected by two independent layers (see [Design choices](#design-choices)): OpenTofu's `prevent_destroy` and Hetzner's API-level `delete_protection`/`rebuild_protection`. Both are driven by the `prevent_server_destroy` variable, which defaults to `true`. So a plain `tofu destroy` is **refused** — by design, you can't nuke production in a single command.
195+
196+
To deliberately tear down, lower the gate first, then destroy:
197+
179198
```bash
180-
tofu destroy
199+
# Set prevent_server_destroy = false in terraform.tfvars (or pass -var on each command).
200+
tofu apply # lifts the Hetzner delete/rebuild locks in place
201+
tofu destroy # now succeeds
181202
```
182203

183-
This wipes the Hetzner server, removes Cloudflare records, deletes the firewall and SSH key, and reverts Cloudflare zone settings to defaults. The state file remains. Run `rm terraform.tfstate*` for full cleanup.
204+
The `apply` step is required: Hetzner won't honour a delete while the API lock is still set, so the lock must be lifted by an apply *before* `destroy` can remove the server. This wipes the Hetzner server, removes Cloudflare records, deletes the firewall and SSH key, and reverts Cloudflare zone settings to defaults. With remote state the state object stays in the bucket; with local state, run `rm terraform.tfstate*` for full cleanup.
184205

185206
## Troubleshooting
186207

@@ -209,7 +230,8 @@ The runtime repos work fine without this one. It's a convenience layer, not a de
209230
- **Sane Cloudflare zone defaults**: SSL strict, HSTS 6 months, TLS min 1.2, browser integrity on. Matches what `production-labels.yml` expects. Each setting is one override away.
210231
- **DNS: apex and www only**: one A/AAAA pair on the apex serves both the SPA and `/api/*` via same-origin path routing. `www.` is a CNAME to apex with a redirect rule. No `api.` subdomain. Traefik path-routes `/api/*` on the same host.
211232
- **Edge bot-blocking, on by default**: a single Cloudflare WAF custom rule (`http_request_firewall_custom`) blocks common scanner probes — `/.env`, `/.git/`, `/wp-admin`, `/xmlrpc`, … — at the edge, so they never reach Traefik or the API. Configurable via `bot_block_paths`; disable with `enable_bot_blocking = false`. Works on the Cloudflare Free plan.
212-
- **State stays local by default**: single-operator default. An S3 backend block is one paste away for teams.
233+
- **State stays local by default, R2 one step away**: single-operator default is a local state file. `main.tf` ships a pre-wired, commented Cloudflare R2 backend (S3-compatible, native locking, no DynamoDB); enabling it is `cp backend.hcl.example backend.hcl`, uncomment the block, and `tofu init -migrate-state`. See [State management](#state-management).
234+
- **Production is hard to nuke, on purpose**: the VPS carries OpenTofu `prevent_destroy` *and* Hetzner API-level `delete_protection`/`rebuild_protection`, both gated behind a single `prevent_server_destroy` variable (default on). Defense in depth — destroying or replacing the box (and the Postgres, ACME, and GlitchTip volumes it holds) takes a deliberate, explicit gate-lowering, not a stray `tofu destroy` or a misclick in the Hetzner console. See [Destroying](#destroying).
213235
- **Secrets in terraform.tfvars (gitignored)**: same pragmatic floor as `compose/.env`. Upgrade to a secret manager when team size demands it.
214236
- **Outputs print, never side-effect**: apply prints the IP, ssh command, and site URL. Never auto-opens anything.
215237
- **Optional bootstrap repo, separate from infra-compose**: same logic as the planned Kubernetes template. Separation lets operators skip the tool entirely.

infra/bootstrap/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ crash.*.log
1515
*.tfvars.json
1616
!terraform.tfvars.example
1717

18+
# Remote-state backend config (bucket + account id). Commit the example only.
19+
backend.hcl
20+
!backend.hcl.example
21+
1822
# Editor / OS
1923
.DS_Store
2024
*.swp

infra/bootstrap/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ Part of [BoringStack](https://boringstack.xyz).
2222

2323
For documentation on how to run, configure, and extend the OpenTofu bootstrap, see the [OpenTofu provisioning docs](https://boringstack.xyz/topics/provisioning-with-tofu/).
2424

25+
## Remote state (optional)
26+
27+
State defaults to a local `terraform.tfstate`, which is fine for a quick spin-up but a single point of failure for anything long-lived — lose the file and you can no longer plan, reconcile, or safely destroy the stack. To move state to **Cloudflare R2** (S3-compatible, no egress fees, native locking — no DynamoDB):
28+
29+
1. Create an R2 bucket and an R2 API token with **Object Read & Write**.
30+
2. `cp backend.hcl.example backend.hcl` and fill in the bucket name + account id (`backend.hcl` is gitignored).
31+
3. Export the token as S3 credentials:
32+
```sh
33+
export AWS_ACCESS_KEY_ID=<r2 access key id>
34+
export AWS_SECRET_ACCESS_KEY=<r2 secret access key>
35+
```
36+
4. Uncomment the `backend "s3"` block in `main.tf`, then migrate:
37+
```sh
38+
tofu init -backend-config=backend.hcl -migrate-state
39+
```
40+
41+
R2 uses the `s3` backend with AWS-specific preflight disabled; the block in `main.tf` is pre-filled with the right flags. Hetzner Object Storage or AWS S3 work the same way — only the `endpoints.s3` value (and the `skip_*`/`use_path_style` flags, for true AWS) differ.
42+
2543
## License
2644

2745
MIT
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Cloudflare R2 remote-state backend config (OPT-IN).
2+
#
3+
# Copy to backend.hcl (gitignored) and fill in the values below, then enable
4+
# the `backend "s3"` block in main.tf and run:
5+
#
6+
# export AWS_ACCESS_KEY_ID=<r2 access key id>
7+
# export AWS_SECRET_ACCESS_KEY=<r2 secret access key>
8+
# tofu init -backend-config=backend.hcl -migrate-state
9+
#
10+
# Credentials are NOT stored here — they come from the AWS_* env vars above so
11+
# this file (and backend.hcl) stay safe to keep on disk. The bucket name and
12+
# account id are not secrets, but backend.hcl is gitignored anyway.
13+
14+
bucket = "boringstack-tfstate"
15+
key = "bootstrap/terraform.tfstate"
16+
17+
endpoints = {
18+
# Find your account id in the Cloudflare dashboard → R2 → "S3 API" endpoint.
19+
s3 = "https://<CLOUDFLARE_ACCOUNT_ID>.r2.cloudflarestorage.com"
20+
}

infra/bootstrap/main.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ terraform {
2929
version = "~> 3.4"
3030
}
3131
}
32+
33+
# --------------------------------------------------------------------------
34+
# Remote state (OPT-IN). Default is local state — a terraform.tfstate file on
35+
# the operator's machine. That's fine for a quick spin-up, but it is a single
36+
# point of failure for anything long-lived: lose the machine (or the file)
37+
# and you lose the ability to plan, reconcile, or safely destroy the stack —
38+
# made worse now that the VPS carries delete_protection. Move state to
39+
# Cloudflare R2 (S3-compatible, no egress fees, an account you already have):
40+
#
41+
# 1. Create an R2 bucket and an R2 API token (Object Read & Write).
42+
# 2. cp backend.hcl.example backend.hcl # gitignored; fill in bucket + account id
43+
# 3. export AWS_ACCESS_KEY_ID=<r2 access key id>
44+
# export AWS_SECRET_ACCESS_KEY=<r2 secret access key>
45+
# 4. Uncomment the block below, then migrate the existing local state:
46+
# tofu init -backend-config=backend.hcl -migrate-state
47+
#
48+
# R2 speaks the S3 API, so we use the s3 backend with AWS-specific preflight
49+
# disabled and native lockfile locking (S3 conditional writes — no DynamoDB;
50+
# requires OpenTofu >= 1.10, already guaranteed by required_version above).
51+
# --------------------------------------------------------------------------
52+
# backend "s3" {
53+
# region = "auto" # R2 ignores it, but the s3 backend still requires a value
54+
# use_lockfile = true # state locking via S3 conditional writes (no DynamoDB)
55+
# use_path_style = true
56+
# skip_credentials_validation = true
57+
# skip_region_validation = true
58+
# skip_requesting_account_id = true
59+
# skip_metadata_api_check = true
60+
# skip_s3_checksum = true # R2 rejects the AWS streaming-checksum trailer
61+
# # bucket, key, endpoints.s3 -> backend.hcl (see backend.hcl.example)
62+
# }
3263
}
3364

3465
provider "hcloud" {

0 commit comments

Comments
 (0)