Spacelift.md 16 KB

Spacelift

Before this page: What is infrastructure as code explains plan, apply and state, and Git, GitHub and pull requests explains the pull request and CI steps that come before a Spacelift run.

Unfamiliar word or acronym? The Glossary defines it.

Want to build and destroy a stack of your own first, in your own account, before reading how this project runs its stacks? Spacelift from zero is a hands-on side page for that.

Spacelift is where tofu plan and tofu apply actually run. It holds the state, waits for a human to approve every apply, and is the reason nothing ever applies from a laptop or from GitHub Actions.

The run lifecycle

Diagram source (Mermaid) ```mermaid %%{init: {"flowchart": {"nodeSpacing": 25, "rankSpacing": 40, "padding": 8}}}%% flowchart TD subgraph A["Pull request"] direction LR pr["Pull request opened"]:::you prop["Push policy: no Spacelift run
for a pull request"]:::ctrl ci["GitHub Actions
tofu test with mocked providers,
cost gate, scanners"]:::ctrl green{{"CI green?"}}:::warn pr --> prop pr --> ci --> green green -- "no" --> pr end subgraph B["Merge"] direction LR merge["Merge to main"]:::you affected{{"Push policy: did the merge change
this stack, modules/, the hook
or .spacelift/?"}}:::warn skip["No run
(docs-only change)"]:::ext merge --> affected affected -- "no" --> skip end subgraph C["Spacelift run"] direction LR tracked["Tracked run on main"]:::ctrl init["before_init hook
fetch named secrets
from Doppler"]:::secret plan["tofu plan"]:::ctrl tracked --> init --> plan end subgraph D["Human decision"] direction LR again["before_apply hook
fetch secrets again
(fresh container)"]:::secret apply["tofu apply"]:::cloud discard["Discard: nothing changes"]:::ext wait{{"Autodeploy is off:
human reads the plan"}}:::warn wait -- "confirm" --> again --> apply wait -- "discard" --> discard end A -- "CI green: yes" --> B B -- "changed: yes" --> C C --> D classDef you fill:#132a1f,stroke:#4ade80,color:#dcfce7 classDef ext fill:#3b1d1d,stroke:#f87171,color:#fde2e2 classDef ctrl fill:#1e1b3a,stroke:#a78bfa,color:#ede9fe classDef cloud fill:#0f2a3d,stroke:#38bdf8,color:#e0f2fe classDef secret fill:#3a2a0f,stroke:#fbbf24,color:#fef3c7 classDef warn fill:#3a2a0f,stroke:#fbbf24,color:#fef3c7 ```

Screenshot to take yourself: the run view of a stack in the Spacelift console, once for a proposed run and once for a tracked run. Capture: the stack's run page for a tracked run that is waiting at the confirm step (the plan finished, the Confirm button visible), then a proposed run's page if your plan has one, with the run type and the plan summary in frame. Look for: the run labeled tracked or proposed, the plan summary (resources to add, change, destroy), and the Confirm and Discard buttons that exist only because autodeploy is off. Redact: the account or organization name in the address bar and header, the stack and space names if they are yours, commit authors, email addresses, and any resource id in the plan output.

Decisions and why

Why not just tofu apply from GitHub Actions?

You can, and plenty of tutorials do. The problems:

  • The CI runner needs a write-scoped cloud token, and every workflow in the repo, including future ones, is one mistake away from exposing it.
  • State has to live somewhere, which means a bucket and a credential for that too.
  • "Wait for a human to read the plan" is clumsy to build in Actions.

Spacelift holds state, runs the plan, stops, and shows a human the diff. GitHub Actions only ever tests; it never holds a cloud credential. In fact, the CI workflow here declares permissions: {} at the top and grants each job only what it needs.

Why a pull request plan gets no credentials

This repository is public. A Spacelift proposed run (the plan for a pull request) executes code from the pull request: provider configuration and data sources can run arbitrary lookups. If a proposed run held the DigitalOcean token, anyone able to open a pull request that triggered a run could write HCL that sends it somewhere.

So the Doppler hook refuses to fetch anything unless the run is a tracked or destroy run on main. The cost: a pull request plan cannot talk to DigitalOcean and fails at the provider. That is covered by tofu test with mocked providers in CI, and the real plan is read on main before anyone confirms it.

Why a pull request gets no Spacelift run at all

A proposed run that can only fail is noise: it posts a red check on every pull request, and people learn to ignore red checks, which is worse than having none. It is also code from the pull request running in Spacelift with the Doppler context attached, which is the risk in the next section. So a push policy (.spacelift/policies/push.rego) decides which Git events start runs:

  • a push or merge to main that changes the stack's own directory, modules/, the secrets hook or .spacelift/ starts a tracked run;
  • everything else starts nothing: pull requests, docs-only merges, tags, other branches, forks.

The policy is written in Rego, the language of Open Policy Agent, and has its own tests (push_test.rego) that CI runs on every pull request. Spacelift does not read it from the repository: it is created once in Spacelift with the file as its body, and updated by hand after a change merges.

Push policies: paid plan versus free plan

This is the one place this wiki states the caveat in full; other pages link here.

Push policies are a paid Spacelift feature. On the free plan the same effect is only half available. Spacelift's built-in behavior already ignores changes outside a stack's project root and project globs, so setting those narrowly (the stack's own directory plus modules/**, scripts/spacelift/** and .spacelift/**) stops docs-only pull requests and merges from starting anything. A pull request that changes a stack still gets a proposed run that fails without credentials. That is a real limit of the free plan, written down rather than hidden. The steps are in section 9 of the secrets runbook. Lessons learned, "A red check from a run that could never pass" is the story of the red check that prompted it.

The plan policy: no open ports in a plan

A second policy, .spacelift/policies/plan.rego, reads the plan before an apply and denies it if it would open a port on a core machine: a firewall inbound rule with a source address or 0.0.0.0/0, an AWS ingress rule from a CIDR range, or IPv6 turned on for a droplet or an AWS network. A rule that admits only a tag (the only kind the firewall module emits) passes. It also reads outbound rules: one that reaches the whole internet (judged by prefix length, so 0::/0, 0.0.0.0/00, the halves 0.0.0.0/1 plus 128.0.0.0/1, the four /2 ranges and any prefix of /8 or smaller count too; /9 and narrower do not) is denied unless a reviewed outbound_allowlist entry names its stack, address, protocol, ports and destinations, and an icmp or all-port rule to the whole internet is never allowed. A firewall rule list or address list that is unknown until apply is denied too, since it could resolve to the whole internet. The firewall module's tests catch the same mistakes in CI; this catches them in the plan that is about to be applied.

A deliberate exception is a reviewed code change: an entry in an allowlist inside the policy file, with a threat-model note. A stack label or a plan field cannot grant one. How the allowlists work is in the allowlist model below. It is written and tested but not yet attached, and it is a policy in Spacelift like the push policy, so the same paid-plan caveat applies; whether the plan policy type is available on the maintainer's plan is unconfirmed. The steps are in section 9a of the secrets runbook.

The plan policy: destroy and autodeploy guards

The same policy also turns two conventions into rules. A plan that deletes or replaces a core network piece is denied: a VPC, a project, a firewall, a Twingate remote network, an AWS VPC or security group, or anything tagged protected. Connector droplets are deliberately not on that list, because replacing one is a normal, reviewed event. And a stack with autodeploy on is denied, since the plan input carries that setting.

An exception to the destroy guard is a reviewed code change: the stack id and the resource address go in destroy_allowlist in the policy file, in a pull request, and come out again once applied. A stack that is torn down one whole service at a time can also opt in to one shortcut: a plan that deletes a firewall together with the droplet it protects is allowed there without an entry (see the allowlist model below). The autodeploy guard has no exception; turn autodeploy off. The autodeploy check blocks an apply, it does not stop a stack being created with autodeploy on, so the stack factory (issue #96) should enforce that setting too. Same caveat as above: written and tested, not yet attached, and the paid-plan question is unconfirmed. Details are in section 9a of the secrets runbook.

The plan policy: the allowlist model

An address in a plan is relative to its stack. When one module is reused for several stacks (one stack per service), each stack has its own module.service_host.digitalocean_firewall.this, so an exception keyed only by address would cover all of them. The policy keys both allowlists by stack id first, then resource address (or a named group of addresses in that stack). The stack id comes from the plan input, not from anything the branch can set.

What an entry allows depends on the device and the traffic, so an entry carries its own scope rather than one blanket exception per stack:

Field Meaning
direction inbound (the only direction the policy reads today)
protocol exact, for example tcp
ports exact port ranges, for example 5201
sources every non-tag source address the rule has, for example one /32, or both 0.0.0.0/0 and ::/0 for a rule open to the world

Rules are judged one at a time, so a firewall with two public ports needs two entries. Anything missing or unknown (an unknown stack, a missing field, a port not listed) is denied. An entry is added only when that service's stack is approved, in the same pull request as the approval and its threat-model note.

For the destroy guard, destroy_allowlist is keyed the same way. One opt-in shortcut exists for taking a service down: in a stack listed in whole_service_teardown_stacks, a plan that deletes a firewall and the droplet it protects (the two share a tag, or the firewall names the droplet) is allowed without an entry. A lone firewall delete or a replacement is still denied.

All of these lists are empty today, so every current stack gets exactly the verdicts it had before; tests assert that. The stack id and the nested structure were written against the published policy contract and tested with fixtures, not against a real plan from the maintainer's account: compare with a real plan in the policy workbench when the policy is first attached. Details are in section 9a of the secrets runbook.

Why autodeploy is off

Every apply needs a human to read the plan and press confirm. For infrastructure that costs money and controls access, the plan is the review. A plan that shows "1 to destroy" on a connector when you expected an in-place change is exactly the thing you want a person to catch.

Why before_apply fetches secrets again

After a run waits for confirmation, the apply phase can start in a fresh container where the environment from the plan phase is gone. So .spacelift/config.yml loads the secrets in both before_init and before_apply, and a test checks that the two lists are identical.

OpenTofu inside Spacelift

The stacks use Spacelift's Terraform workflow with the OPEN_TOFU setting, because the standalone OpenTofu vendor option was not enabled on this account when it was set up.

The trust boundary is Spacelift, not the script

The hook script's checks only protect against honest mistakes. The script and .spacelift/config.yml are read from the branch being run, so anyone who can push a branch that triggers a run could change them to print $DOPPLER_TOKEN. What actually keeps this safe:

  • one writer on the repository;
  • branch protection on main (pull request plus CI green required);
  • the push policy above, which never proposes a run, so no pull request's code runs next to the context (it must be attached in Spacelift to count; see the runbook);
  • the Doppler token being read-only and scoped to one config, so a leak reaches only these secrets.

This is written down because "the script checks the run type" sounds like a security control and is not one on its own. See Threat model.

No OIDC on this plan

The ideal is that Spacelift holds no long-lived secret at all: it would present a short-lived OIDC identity token and Doppler would trust it. Spacelift's run identity token was not issued on this account's plan (checked with a task run: the variable was absent), so a read-only Doppler service token with a 90-day expiry is the compromise. If OIDC becomes available, only the login step in doppler-env.sh changes.

More than one cloud

When AWS, Azure or GCP stacks are added, each cloud gets its own Doppler config and its own Spacelift context with its own read-only token, attached only to that cloud's stacks. The reason is blast radius: a Doppler token can read its whole config, so keys for several clouds in one config would turn one compromised run into several compromised accounts. A test checks that no stack's .spacelift/config.yml entry names another cloud's secrets (it checks names in the file, not what Doppler or Spacelift actually allow). Naming, the native-integration route and what the test cannot prove are in docs/spacelift-secrets.md.

Adding a stack

  1. Create it in Spacelift. A DigitalOcean stack gets the label cah, so the Doppler context and the push policy attach themselves. An AWS, Azure or GCP stack must not get cah (it would receive the DigitalOcean context); give it its own cloud label, and attach the push policy to it before its secret context (see "More than one cloud").
  2. Add its entry to .spacelift/config.yml, keyed by the stack id, listing only the secrets it needs.
  3. Add stack dependencies for any outputs it consumes.
  4. Pull request, CI green, merge.

Full step-by-step for secrets is on Doppler and secrets and in docs/spacelift-secrets.md.

Next: Doppler and secrets, step 17 of the core path on the Reading order page.