Skip to content

fix(bootstrap): instance ID from IMDSv2, install the AWS CLI when absent, fail closed on tag reads (#1490) - #1496

Merged
scttfrdmn merged 6 commits into
mainfrom
fix/1490-ubuntu-bootstrap-tools
Nov 2, 2026
Merged

scttfrdmn merged 6 commits into
mainfrom
fix/1490-ubuntu-bootstrap-tools

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1490

⚠️ The published bootstrap body changes

spawn bootstrap-body sha256: b447ce0938c5fafd7acc827e0f956bc884ff207e72f0dd0bea7d46c31d56fcbb (main, #1484) → 63421a2d49c0564a1289bdcd26c7e0375d8667f000f53ebdfebcf8094f033d23. That is 24421 → 23501 bytes raw and 6617 → 7191 gzipped. Deploy the sweep orchestrator from the release that carries this: it embeds the body.

gzip bytes main this PR budget
inline bare bootstrap (dev build) 7412 7954 8448 → 9088 (raised in review, see below)
release stub, bare launch 1839 1839 3072
release stub, staged task launch 2132 2598 (16 KiB user data)

Budget: 7954 is under the 8448 budget, but it would leave only 494 bytes of the 1024 margin #1422 requires. Main had 12 bytes to spare above that margin. I raised the budget by 640, as #1459 did, rather than shrink the margin. The cost lands in the published body. A release launch's stub is unchanged unless it stages files (a task launch), and then it carries spawn_need_aws (+466) because the staged fetch runs before the body. Raw size went down: the 16 describe-tags blocks are now one function. But those blocks compressed almost to nothing, so the net gzip change is the installer.

Inventory (discovered from the rendered script)

TestBootstrapToolsExistOnEveryBase parses every shape of the bootstrap: inline and stub, the stub with the body spliced in where it is sourced, ± key, command, flush hook, staged files, gates, plugins and launch config. It walks the AST, including the here-docs that are scripts. That gives 45 tools: /tmp/spawn-awscli/aws/install /usr/local/bin/spored awk aws base64 cat chmod chown cp curl cut date dirname dnf grep hostname hostnamectl id logger mkdir mkfifo mktemp mv openssl poweroff ps python3 restorecon rm sed selinuxenabled sha256sum shutdown sleep su systemctl tee timeout touch tr uname useradd usermod yum.

The tools were checked against Canonical's 22.04 and 24.04 server-cloudimg manifests. Ubuntu lacked ec2-metadata, aws, dnf, yum, restorecon, selinuxenabled and unzip. It has curl, python3, gpg, openssl, bsdutils (logger), passwd, systemd-sysv and util-linux. service was in the old script as a fallback after systemctl; it was removed, since every base runs systemd.

aws CLI calls, reads vs writes:

where call R/W when
body 16 ec2 describe-tags (now spawn_tag) read after spored starts
staged-files fetch (task launches) s3 cp s3://…staged → /etc/spawn read before spored, before the body
spawn_fatal_retire ec2 terminate-instances (self) write before spored; guarded by command -v aws
task wrapper (pkg/taskproto) spawn_aws s3 cp stage-in / stage-out / completion.json, ecr get-login-password read + write after the body
flush hook s3 cp outputs write spored lifecycle exit
queue runner, container ECR login (pkg/userdata) s3 cp, ecr get-login-password read after the body

Decision: (c), with (b) as the mechanism. The CLI is installed when absent; the instance ID comes from IMDSv2.

Why not IMDS instance tags (a) for the reads:

  • Key restrictions. AWS says that with InstanceMetadataTags=enabled, tag keys may contain only letters, digits and + - = . , _ : @. They may not contain spaces or /, and may not be ., .. or _index. "Non-compliance will result in failed launches for new instances" (docs). Colons are allowed, so every spawn: key, including spawn:param:<name>, would be readable. The problem is the user's --tag key=value (cmd/launch_flags.go): enabling IMDS tags would make every launch carrying a common key like Cost Center or team/project fail at RunInstances. That is a regression on every distro to fix one.
  • Every launch path would have to enable it. spawn sets no MetadataOptions today, except HttpTokens under IMDSv2Enforced. The CLI, the sweep orchestrator Lambda (which runs its own RunInstances) and the pipeline orchestrator would all have to change. Every path that missed it would break tag reads on AL too.
  • The CLI is needed anyway. The staged fetch, the task wrapper, the flush hook and the queue runner write to S3, so Ubuntu task launches need a CLI regardless.

Why not spored as the tag reader: a dev build installs spored from spawn/current/, so a new subcommand would be missing at the branch live check. It also would not solve the S3 writes.

What this PR does:

  • Instance ID: from IMDSv2 with curl, using the body's existing $TOKEN (--retry 3). Anything not shaped i-* stops the bootstrap: spawn: no instance ID from IMDS; stopping (#1490).
  • spawn_need_aws: returns at once if aws exists, so Amazon Linux installs nothing. Otherwise it downloads awscli-exe-linux-$(uname -m)-2.39.3.zip from awscli.amazonaws.com and checks it against a pinned sha256 for that architecture. It unpacks with python3, because Ubuntu has no unzip and cloud-init runs on python3 there; the zip's file modes are kept. Then it runs AWS's own aws/install, into /usr/local/aws-cli and /usr/local/bin/aws, removes the unpacked files and confirms aws is on PATH. It is called in two places:
    • in the body, right before the first tag read (spored is already up): spawn_need_aws || { echo …; exit 1; };
    • in the staged fetch, which runs before spored: spawn_need_aws || spawn_fatal_retire …. On a release stub that fetch runs before the body is loaded, so the stub carries its own copy of the function in that case only.
  • spawn_tag <key> [query]: one describe-tags call, retried up to 6 times 5 s apart (for a new role's IAM propagation). If every try fails, the bootstrap stops: spawn: could not read tag K; stopping rather than use a default (#1490). The aws CLI's stderr is no longer hidden. An absent tag still reads as None, as before, so a healthy AL boot behaves identically. I kept the old success-path value rather than "fixing" None to the old || echo defaults: those defaults were only ever reached on failure, and mapping None to them would have changed AL behaviour (for example, an absent spawn:session-timeout would become 30m instead of disabled).
  • Fail closed: all three failures exit non-zero after SPAWN_SPORED_UP=1, so A bootstrap failure after spored starts but before --command launches writes no completion record; the instance bills to TTL #1484's guard writes {"status": "failed", "exit_code": 1, "source": "bootstrap"}, and --on-complete fires.
  • The acpid install now checks command -v dnf / command -v yum first. The service sshd reload fallback is gone.

Behaviour change (CHANGELOG, Changed): a bootstrap that used to continue with wrong defaults now stops. That happens when IMDS gives no instance ID, when the CLI is absent and cannot be installed, or when a tag read keeps failing (for example, a caller-supplied instance profile without ec2:DescribeTags, though spored itself needs that permission). On Ubuntu with no route to awscli.amazonaws.com (a private subnet with only endpoints, i.e. --no-public-ip), the launch now fails closed. Filed as #1494 (mirror the pinned zips in spawn-binaries-<region>). No new IAM: ec2:DescribeTags is already in the spored role, and the download is anonymous.

Supply chain

  • Pin: AWS CLI 2.39.3.
    • x86_64 14cb35809ad7547dd3c69fa9899a3975fbd850b6a6791f3fe264856490cd9a63 (74,724,393 bytes)
    • aarch64 f6eb845fbacb11bee87de3f5fbe8043773cbe89d0414607b44b71e216eb9226b (72,175,673 bytes)
  • How the pin was established: I downloaded both zips and their .sig files and ran gpg --verify against the AWS CLI team key taken from the AWS install guide, fingerprint FB5D B77F D5C1 18B8 0511 ADA8 A631 0ACC 4672 475C. Both reported Good signature from "AWS CLI Team <[email protected]>".
  • On the instance, only sha256sum against the pin is checked. The pin itself sits in the signed, sha-pinned published body, so it is covered by the same trust chain as spored.
  • Why not GPG on the instance: the key costs about 1.2 KB gzipped of the inline budget and needs gpg.
  • Moving the pin is documented in the comment above the constants. Nothing automates it yet (filed bootstrap: nothing moves the pinned AWS CLI version/sha256 the Linux bootstrap installs #1495). The AWS key expires on 2027-07-01.
  • For comparison, the Windows bootstrap's MSI install is Invoke-WebRequest + msiexec with no pin.

Real install, outside the suite: I ran the rendered spawn_need_aws in ubuntu:24.04 (arm64) and ubuntu:22.04 (--platform linux/amd64), with network, after apt-get install curl python3 (the cloud images ship both). Neither image has unzip. Both returned 0 and aws --version reported aws-cli/2.39.3 … exe/aarch64.ubuntu.24 and … exe/x86_64.ubuntu.22. /tmp/spawn-awscli was removed. The install took 1 to 2 s on this link; EC2 timing is for the live check.

Gates, each watched failing against a compiling revert (go vet clean)

Gate Revert What failed
TestBootstrapToolsExistOnEveryBase (inventory, discovered from the AST; guarded tools must sit behind command -v; python3 and the installer are scoped to spawn_need_aws) R1: INSTANCE_ID=$(ec2-metadata …) back runs "ec2-metadata", which is not a base tool of every supported Linux base in every shape
same R4: old acpid line runs "dnf" outside a 'command -v dnf' check (and yum)
TestBootstrapUsesAWSOnlyAfterProvidingIt (every unguarded aws comes after a fail-closed spawn_need_aws, which must be defined by then) R3: drop the body's spawn_need_aws calls aws at offset N, and nothing provides it first
same R6: stub+staged without its copy of the function calls spawn_need_aws (offset 2638) before defining it: the call fails with 127 (and TestStagedFilesAreTheInlineFiles: defines spawn_need_aws 0 times, want once)
TestEveryTagReadFailsClosed (discovers the 16 keys from a healthy boot that takes the array and sweep branches, then fails each in turn: exit ≠ 0, a bootstrap record, the tag named, the CLI error shown, exactly 6 tries, user data not run) R2: spawn_tag back to 2>/dev/null || echo None all 16: the bootstrap exited 0 although reading K failed every time: it carried on with a default
TestNoInstanceIDFailsClosed (IMDS 404 on instance-id) R1 the bootstrap exited 0 with no instance ID; the healthy boot's the tag reads do not name the instance from IMDS
TestAWSCLIInstalledWhenAbsent (aws absent; a test zip served at the rewritten URL, its sha substituted for the pin: installed once, then used for the reads, /tmp cleaned up; a tampered zip or none gives no install, a failed record, no MOTD, no command) R5: sha check → true the bootstrap exited 0 with no aws CLI (tampered zip installed)
same R3 all three cases fail
TestBootstrapOnUbuntuReadsTheInstancesTags (the whole inline bootstrap in ubuntu:24.04 and amazonlinux:2 (bash 4.2.46), --network none, stand-in curl serving IMDSv2 and the bucket from files; only AWS-touching tools are stand-ins) R1 a tool the bootstrap runs is missing from ubuntu:24.04 (and amazonlinux:2), the MOTD lacks "Instance: i-0ubuntu", every a tag read does not name the instance: …Values= …, the --command once-marker does not carry the instance ID
same, no-aws/no-network case R3 the failure does not say the AWS CLI could not be installed
TestToolScanSeesTheOldInstanceIDAndTagReads — proves the scan sees ec2-metadata and an unguarded aws in the pre-#1490 lines

The container runs skip unless docker and the image are present locally; they never pull. The Ubuntu run checks the MOTD (Instance: i-0ubuntu, TTL: 1h, On Complete: terminate, Instance Index: 1 / 2), job-array.sh, hostnamectl set-hostname sim-1, command-ran-i-0ubuntu and a completed record. Its no-aws case and its no-instance-ID case each check for a failed bootstrap record and no MOTD.

Also updated:

  • TestSpawnTagContract: the scan now recognises $(spawn_tag spawn:x) as a read. Without that it reported spawn:command and spawn:session-timeout as read by nothing.
  • The hostname and core-content tests: new markers.
  • TestStagedFilesAreTheInlineFiles: the stub's extra function copy.
  • The sandbox gained the awscli.amazonaws.com rewrite, IMDS 404s and "tool absent" stand-ins.

AL2: the inline bootstrap runs end to end under bash 4.2.46 in the container, and bash -n of the published body passes there.

Checks

  • make check (from /private/tmp): pass.
  • make lint: 0 issues.
  • semgrep scan --config=auto --error .: 0 findings.
  • TestBootstrap_Shellcheck: pass.
  • No exported API change, so no api-snapshot. No help text change.

Live check before merge (branch-built CLI, so the body is inline)

  1. Ubuntu 24.04 and 22.04, arm64 and amd64, the bootstrap: INSTANCE_ID is empty on Ubuntu (no ec2-metadata), so every tag read and the --command once-marker use an empty ID #1490 shape: --mpi --count 2 --name …, plus one plain --name u-test --command 'hostname; cat /etc/profile.d/job-array.sh 2>/dev/null; true' --on-complete terminate --ttl 1h. Expect:
    • cloud-init-output.log: spawn: no aws CLI; installing AWS CLI 2.39.3 (#1490), no ec2-metadata: command not found, no could not read tag. Note the time from that line to Configuring session timeouts (the install cost, while spored is already up).
    • aws --version = aws-cli/2.39.3, command -v aws = /usr/local/bin/aws, /tmp/spawn-awscli gone.
    • hostname = the --name (…-0/…-1 on array members); /etc/profile.d/job-array.sh present on arrays; /etc/motd shows the instance ID and the real TTL and on-complete; ls /var/lib/spawn/command-ran-i-* names the instance on the --command run.
    • Every member self-terminates on completion.
  2. AL2023 regression: cloud-init-output.log has no installing AWS CLI line, and the MOTD and hostname are as before.
  3. Fail-closed, optional: an Ubuntu launch with --no-public-ip in a subnet with no NAT should record {"status":"failed","exit_code":1,"source":"bootstrap"} with could not install AWS CLI 2.39.3 and terminate under --on-complete terminate. That confirms bootstrap: Ubuntu in a private subnet cannot install the AWS CLI (awscli.amazonaws.com unreachable), so it now fails closed #1494's premise.
  4. Staged task on Ubuntu, optional: spawn task run on an Ubuntu AMI. The CLI is installed before the staged fetch (before spored), and the wrapper's S3 copies work.

Afterwards, verify no instance from these launches is left running.

Not covered here: pkg/userdata's storage script installs nfs-utils/lustre-client with dnf only, so EFS/FSx on Ubuntu is still part of #54's audit.

@scttfrdmn scttfrdmn added this to the v0.155.0 milestone Nov 2, 2026
@scttfrdmn scttfrdmn added the bug Something isn't working label Nov 2, 2026
@codecov

codecov Bot commented Nov 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn

Copy link
Copy Markdown
Contributor Author

Live-verified on EC2 (2026-11-02) with a CLI built from 9586658f (includes main e1ecc2fa) in account 942542972736, us-east-1:

Run Result
Ubuntu 24.04 arm64 (t4g.small), --command PASS. installing AWS CLI 2.39.3 (#1490); aws-cli/2.39.3 at /usr/local/bin/aws; install about 4.8 s; hostname = --name; MOTD shows the instance ID and TTL: 1h / On Complete: terminate; the command-ran marker names the instance; completed/0/command; self-terminated
Ubuntu 22.04 amd64 (t3.small) PASS, same checks; install about 5.7 s
Ubuntu 24.04 job array --count 2 PASS. Hostnames -0/-1; job-array.sh has INDEX/SIZE; MOTD shows Instance Index: N / 2
AL2023 (stock /usr/bin/aws 2.38.0) PASS. No install line; hostname, MOTD and completion unchanged

None of these appeared anywhere: ec2-metadata: command not found, could not read tag, no instance ID.

Pinned zips independently verified: SHA-256 matches fresh downloads for x86_64 and aarch64, and the GPG signature is valid from the AWS CLI key FB5DB77FD5C118B80511ADA8A6310ACC4672475C.

Cost under $0.01; the leak check is clean.

@scttfrdmn
scttfrdmn force-pushed the fix/1490-ubuntu-bootstrap-tools branch from 9586658 to fb5cba6 Compare November 2, 2026 06:02
@scttfrdmn
scttfrdmn merged commit a0c8e03 into main Nov 2, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1490-ubuntu-bootstrap-tools branch November 2, 2026 06:25
@scttfrdmn scttfrdmn mentioned this pull request Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bootstrap: INSTANCE_ID is empty on Ubuntu (no ec2-metadata), so every tag read and the --command once-marker use an empty ID

1 participant