Skip to content

feat(task): stage the task wrapper and flush hook in S3 behind a sha256-verified fetch (#1274, PR B) - #1293

Merged
scttfrdmn merged 5 commits into
mainfrom
feat/1274-staged-task-wrapper
Oct 25, 2026
Merged

scttfrdmn merged 5 commits into
mainfrom
feat/1274-staged-task-wrapper

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1274

Second half of #1274, after PR A (#1290), which moved the static bootstrap body behind a verifying stub. This PR moves the per-launch content that was still inline for tasks, the task wrapper (the --command) and the terminal-flush hook. A task's user data no longer grows with its command and manifests.

What it does

  • Staging (pkg/tasklaunch). From a release build, Launch uploads the wrapper and flush hook before RunInstances.
    • Where: one object, <task_id>/bootstrap-<sha256>.sh, in a dedicated staging bucket, spawn-staging-<account>-<launch region> (taskproto.StagingBucket). It is one object rather than two because the bootstrap needs both or neither, so there is one fetch, one hash and one way to fail.
    • What: the object's bytes are launcher.StagedFilesObject, the same heredoc shell the inline user data carries. Staging changes where the bytes come from, not what they do.
  • The staging bucket (aws.Client.EnsureStagingBucket) is made sure of on every staged launch.
    • First use: the bucket is created with:
      • ObjectOwnership: BucketOwnerEnforced;
      • Block Public Access, all four settings on;
      • default encryption SSE-S3;
      • one lifecycle rule, spawn-staging-expire-1d: every object expires after 1 day, and incomplete multipart uploads are aborted after 1 day;
      • tags spawn:managed=true and spawn:purpose=bootstrap-staging.
    • Existing bucket: the lifecycle rule is checked: present, enabled, 1 day for both actions, whole-bucket filter. If it is missing or differs, the whole configuration is re-applied, not just a warning. A staging bucket whose objects never expire would keep every task's wrapper indefinitely, and whoever removed the rule may have changed the rest too.
    • Squatting: every call sets ExpectedBucketOwner to the launching account: Head, the config calls, the lifecycle read, List, and the upload's Put. A bucket of that name in another account fails the HeadBucket check, so nothing is written to it.
  • Stub (pkg/launcher). When BootstrapConfig.StagedFiles / Options.StagedFiles is set, the fetch block takes the place of the two inline heredocs. The fetch runs at the same point, before gates, plugins.json and the body, so spored still starts after both files exist.
    • Fetch: aws s3 cp --only-show-errors --region <task region>, using the instance role, with 6 tries 5 s apart and umask 077.
    • Verify and install: it checks the sha256 that the user data carries, sources the object, then removes it.
    • On failure (no aws CLI, no object, wrong sha256) it calls spawn_fatal_retire, so neither spored nor any workload starts.
    • Build-time check: BuildLinuxBootstrap refuses a reference whose sha256 is not that of the config's own command and flush hook.
  • Dry run. UserDataBytes is the staged size. A note under the User data: line gives the object's size and says it is staged.
  • Inline fallback.
    • Dev and pseudo-version builds stay inline. Staging follows launcher.PublishedBodyVersion(), the same switch as the body.
    • Embedders: an embedder whose tasklaunch.AWS lacks the staging methods (found with an optional-interface type assertion) also stays inline, so tasklaunch.AWS gets no new methods.
    • Staging failures: if the staging bucket cannot be made ready, or the upload fails, a task that fits inline launches inline with a warning. One that doesn't fit is refused, naming both causes.
  • MCP. spore-host-mcp uses tasklaunch.NewDeps(*aws.Client), so it stages with no change.
  • Re-runs. A re-run lists <task_id>/bootstrap- in the staging bucket and deletes what it finds before uploading its own object. This is best effort; the 1-day expiry is the backstop.

Trust model

  • The grant. The task role gets s3:GetObject on exactly arn:aws:s3:::spawn-staging-<acct>-<region>/*/bootstrap-<64 ?>.sh (TaskRunPolicyFor, StagedFilesResources). It gets no ListBucket and nothing else on that bucket.
  • Nothing else reaches the bucket. No policy spawn generates grants anything on it: a source-walk gate checks every S3 ARN pattern in the code. A TaskSpec that names a spawn-staging-* bucket is refused, by TaskSpec.Validate and again by tasklaunch.Plan. That covers inputs, outputs, resources.s3_read_write and results_prefix. So are --s3-read/--s3-write on launch, sweeps and pool create.
  • Why the grant is not per task. Roles are shared: they are named by the hash of their policy, and a per-task grant would create one role per task_id. So the grant covers every task's key.
  • Why that is still safe. No role can list the bucket. Each key is the sha256 of bytes holding the attempt's random run id (uuid v4), and only that attempt's user data carries it. In practice, then, a task's role can read only its own object, and objects are gone within a day.
  • What remains. Principals given broad S3 access on purpose can still reach it: the user-chosen --iam-policy s3:ReadOnly/s3:FullAccess templates (Resource *), a user's own --iam-policy-file, and humans with S3 rights in the account. These are explicit grants, so I did not block them. If wanted, a bucket policy denying s3:ListBucket/s3:GetObject to arn:aws:iam::<acct>:role/spawn-instance-* except on the bootstrap key shape would close the template case; I have not built or verified it.
  • The listing-task guard from c0ffc0e is removed. It kept a task whose role could list its results bucket from staging into the results bucket. With the dedicated bucket, no task's data grants reach the staging bucket, so it protected nothing. It was also refusing launches that are now safe.
  • The policy change means the next task run for each set of buckets and region creates one new spawn-instance-* role.
  • Pool workers (cmd/pool.go) are unchanged. Their user data has no Command or TaskFlushScript, because their tasks arrive through the queue. Their policy is gated never to name the staging bucket.

IAM the launching principal now needs (for the umbrella docs/reference/iam-permissions.md)

The documented baseline (spore-host docs/reference/iam-permissions.md) has no S3 statements at all today. It also lacks the spawn-instance-* roles that spawn task run already creates; that gap is older than this PR. spawn's doctor enumerates no S3 actions, so no doctor change is needed.

On arn:aws:s3:::spawn-staging-*:

  • s3:ListBucket (HeadBucket and ListObjectsV2)
  • s3:CreateBucket
  • s3:PutBucketOwnershipControls (required for CreateBucket with ObjectOwnership, and for the re-apply)
  • s3:PutBucketPublicAccessBlock
  • s3:PutEncryptionConfiguration
  • s3:PutLifecycleConfiguration
  • s3:GetLifecycleConfiguration
  • s3:PutBucketTagging

On arn:aws:s3:::spawn-staging-*/*:

  • s3:PutObject
  • s3:DeleteObject

Without these, staging falls back inline when the task fits, with a warning.

Footprint

spawn footprint (pkg/aws/footprint.go) discovers buckets by name prefix, and spawn covers spawn-staging-*. A gate checks the staging bucket's name against the inventory's matcher. I found no other teardown or cleanup inventory that lists spawn-results-* buckets; the reaper teardown covers only its own stack.

Sizes (decoded user data, gzipped, as EC2 counts it)

task dev build (all inline) release, files inline (PR A) release, files staged (this PR) staged object
trivial 9,934 3,965 2,070 7,871 B
20 KB inline command 23,310 (refused) 16,533 (refused) 2,076 48,831 B

Gates (each watched failing against a revert that compiles)

gate reverts it failed against
TestLargeTaskLaunchesWithinBudgetWhenStaged: a 20 KB command is refused from a dev build and launches from a release build within budget, carrying only the URI and sha256. Exactly one object is uploaded, to the staging bucket, under <task_id>/, byte-identical to StagedFilesObject and hashing to the user data's sha256. The bucket is ensured before the put, the put happens before Provision, and every call carries the account as expected owner. staged into the results bucket; bucket not ensured; upload without expected owner; StagedFiles ignored by the launcher; no upload; upload after Provision; body off by one byte
TestEnsureStagingBucketCreatesItConfigured / ...ReappliesAMissingOrChangedRule / ...RefusesABucketItDoesNotOwn: checks the create path (BucketOwnerEnforced, location), BPA×4, SSE-S3, the 1-day expiry and abort rule over the whole bucket, the managed tag, and ExpectedBucketOwner on every call. An existing bucket that is correct is left alone. Re-apply happens when the lifecycle is missing, longer, disabled or prefix-scoped. A 403 or other Head error means no further calls, and so does a missing owner. no BPA; no SSE; no ownership controls; no lifecycle; abort after 7 days; existing bucket trusted without the rule; 403 treated as missing; no managed tag
TestTaskRoleCanReadExactlyItsStagedObjects (default, custom and bucket-root results_prefix, with inputs and s3_read_write): checked with IAM */? matching, the role can Get its own staged key. It has no List, ListBucketVersions or GetBucketLocation on the staging bucket. It has no Get, Put or Delete on other keys there, on wrong-shaped bootstrap names, or on another account's staging bucket, and no Get of a staged-looking key in its results bucket. grant on the whole staging bucket; ListBucket added on it
TestNoGeneratedPolicyReachesTheStagingBucket (pkg/taskiam, walks every non-test .go file, fails if it finds fewer than 10 ARNs): no S3 ARN pattern in the source matches the staging bucket or an object in it, with Printf verbs treated as caller values. spawn-binaries-* widened to spawn-* in the pool worker policy
TestPoolWorkerPolicyNeverNamesTheStagingBucket (unit)
TestValidateRefusesTheStagingBucket (input, output, s3_read_write, results_prefix; any account or region) / TestPlanRefusesASpecNamingTheStagingBucket, which refuses before any AWS call / TestNoFlagGrantsTheStagingBucket (launch and sweep --s3-read/--s3-write, and pool create's, including an s3:// URI) Validate does not refuse; Plan does not refuse; launch flag not refused; pool flag not refused
TestStagingBucketIsInTheFootprint bucket prefix renamed outside the inventory's name rules
TestStagedKeyIsUnguessablePerRun: the identical spec gives different keys, and each object contains its run id. constant run id
TestStagingClearsAPreviousAttemptsObject: the old object is deleted before the new put, and the new one is never deleted. clear lists the wrong prefix
TestStagingFallsBackInlineWhenTheBucketCannotBeReady / ...WhenTheUploadFails: a small task goes inline with a warning; a large one is refused with the cause. no fallback on bucket failure; no fallback on upload failure; fallback keeps the reference
TestDevBuildAndPlainAWSStayInline staging allowed from dev builds
TestStagedFilesAreTheInlineFiles (mvdan tree equality, stub and inline body) / TestStagedFilesRefusesAnotherLaunchsObject / TestStagedFilesFetchRunsOnlyTheStagedObject (bash harness: modes, root chown, region pin, retire on tampered, other, missing, empty or partial object, failing aws or no aws CLI; 6 bounded tries) StagedFiles ignored; consistency check off; sha check off; retire turned into echo; retries cut to 1; --region dropped

The existing budget and user-data tests (TestBareBootstrapFitsItsBudget, TestStubBootstrapFitsItsBudget, TestTaskRunRefusesUserDataOverTheEC2Limit, TestTaskUserDataPreflightMatchesTheLaunch) are unchanged and pass.

Checks

  • make check: everything passes up to check-build-tags. That step fails identically on unmodified main in this /private/tmp worktree.
  • Lint: golangci-lint reports 0 issues in all 10 modules.
  • Semgrep: semgrep scan --config=auto --error . is clean.
  • API snapshot: make api-snapshot is committed; against origin/main it is additions only.

Not verified

  • No AWS. I made no live boot, no IAM simulate or PutRolePolicy, and no real CreateBucket or lifecycle call. In particular I have not confirmed that CreateBucket with ObjectOwnership plus the follow-up configuration calls succeed in one pass, or the exact action names above, against a live account.
  • CLI flags. aws s3 cp --only-show-errors and the --cli-*-timeout options are assumed to be in the AMIs' CLI.

@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

…56-verified fetch (#1274, PR B)

A release build's spawn task run uploads the wrapper and terminal-flush hook
as one content-addressed object, <results_prefix>/bootstrap-<sha256>.sh,
before RunInstances. The user data carries only its URI and sha256; the
bootstrap fetches it with the instance role, verifies it and writes the two
files exactly where the inline user data did, or retires the instance.

The task instance policy gains s3:GetObject on the staged objects' name shape
in the results bucket (TaskRunPolicyFor). Not the task's own prefix: roles
are named by their policy hash, so that would mint a role per task_id.

Trivial task: 3,965 -> 2,075 bytes gzipped. 20 KB command: 16,533 (refused)
-> 2,071. Dev builds and AWS implementations without PutS3Object stay inline;
a failed upload falls back inline when the task fits.
… per-run unguessable keys (#1274, PR B)

The shared s3:GetObject grant on bootstrap-<sha256>.sh is safe because task
roles cannot list the results bucket and each key hashes the attempt's random
run id. A task whose role can list the bucket (it is among its inputs, which
get s3:ListBucket, or in resources.s3_read_write) is now never staged: it
launches inline with a note, or is refused with the reason when it does not
fit. The trust model is documented on the grant.
…staging bucket (#1274, PR B)

The task wrapper and flush hook now go to spawn-staging-<account>-<region>
as <task_id>/bootstrap-<sha256>.sh, not the results prefix. The bucket is
created on first use with Block Public Access, SSE-S3, BucketOwnerEnforced, a
1-day expiry (and incomplete-upload abort) rule and spawn:managed=true; an
existing one missing the rule gets the configuration re-applied; one owned by
another account is never written to (ExpectedBucketOwner on every call).

The task role's grant is s3:GetObject on <task_id>/bootstrap-<64 ?>.sh in that
bucket only. No generated policy reaches the bucket (source-walk gate), and a
TaskSpec or --s3-read/--s3-write naming spawn-staging-* is refused. The
listing-task guard is removed: with the dedicated bucket it protected
nothing.
@scttfrdmn
scttfrdmn force-pushed the feat/1274-staged-task-wrapper branch from 8062921 to 46d3578 Compare October 25, 2026 01:56
@scttfrdmn
scttfrdmn merged commit a729e65 into main Oct 25, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the feat/1274-staged-task-wrapper branch October 25, 2026 02:12
@scttfrdmn

Copy link
Copy Markdown
Contributor Author

Live-checked after merge (dev account, us-west-2, 2026-10-24). I built a throwaway 9.9.9 from main and published its body to us-west-2 only; it was deleted afterwards.

  • Staging: a task with a 20,086-byte incompressible command went out with 2,078 B of user data, with the wrapper and flush hook (50,022 B) staged. Before this change it was over the limit and refused.
  • Payload integrity: command.log showed len=20000, a sha256 prefix matching the generated payload, and big-ok. Exit 0, and spored terminated the instance on completion.
  • Bucket created fresh: spawn-staging-<acct>-us-west-2 had all four Block Public Access settings on, AES256, BucketOwnerEnforced, the lifecycle rule spawn-staging-expire-1d (expire 1 day plus abort multipart after 1 day, whole bucket), tags spawn:managed=true and spawn:purpose=bootstrap-staging, and no bucket policy. One object, under <task_id>/bootstrap-<sha256>.sh.
  • Task role (simulate-principal-policy): GetObject on bootstrap-<sha>.sh keys is allowed. GetObject on any other key, ListBucket, PutObject and DeleteObject are all implicitDeny.

Cleanup: staged object and test bucket deleted, instance terminated, leak check clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

User data: fetch the bootstrap from S3 behind a small verifying stub, removing the 16 KiB ceiling (follow-up to #1246)

1 participant