Skip to content

Opt-in per-account ExternalId + tag-scoped EC2 in bot cross-account role (#374) - #368

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/374-bot-role-hardening
Jul 18, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/374-bot-role-hardening

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Completes the additive half of the cross-account-role hardening in #374.1, on the spawn side (this template is where the customer SpawnBotCrossAccount role is defined; the spore-host Makefile references it cross-repo).

Both changes are default-off and backward-compatible — re-deploying the stack with defaults reproduces the current behavior byte-for-byte. Each account opts into the hardened posture on its own schedule.

What changed (deployment/cloudformation/bot-cross-account-role.yaml)

  • ExternalId parameter (default ''): when set, the trust policy requires this per-account high-entropy value — the one spawn bot register now returns as external_id (spore-host #412) — instead of the shared ${TagPrefix}-bot. Empty preserves the legacy shared value, which the Lambda still falls back to.
  • ScopeStartStopByTag parameter (default 'false'): when 'true', ec2:StartInstances/StopInstances are restricted to ${TagPrefix}:managed=true instances (the tag spawn applies to every launch) via an aws:ResourceTag condition; otherwise Resource "*" as before, gated by the spore-bot registry enabled flag.
  • Updated the stale ExternalId output description ("hardcoded in the Lambda").

Notes

  • IAM Condition map keys must be literal strings (no Fn::Sub), so the tag key is selected by TagPrefix (spawn|prism) with an Fn::If.
  • cfn-lint clean.

Still gated (not in this PR)

Removing the code-side static BOT_EXTERNAL_ID fallback (rest-api/spore-bot) stays gated until every customer role has re-deployed with a per-account ExternalId. This PR is the prerequisite that makes that adoption possible.

Refs #374.

…ount role

Adds two default-off parameters to bot-cross-account-role.yaml so each account
can harden its spore-bot cross-account role without breaking already-deployed
roles (#374):

- ExternalId (default ''): when set, the trust policy requires this per-account
  high-entropy value (returned by `spawn bot register`) instead of the shared
  "<prefix>-bot". Empty preserves the legacy value the Lambda falls back to.
- ScopeStartStopByTag (default 'false'): when 'true', Start/StopInstances are
  restricted to <prefix>:managed=true instances via an aws:ResourceTag
  condition; otherwise Resource "*" as before (registry-gated).

IAM condition map keys must be literals, so the tag key is selected by
TagPrefix (spawn|prism) via an Fn::If. Re-deploying with defaults reproduces
current behavior exactly. Removing the code-side static fallback stays gated on
customer-role adoption. cfn-lint clean.
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 60a5389 into main Jul 18, 2026
7 checks passed
@scttfrdmn
scttfrdmn deleted the feat/374-bot-role-hardening branch July 18, 2026 07:52
scttfrdmn added a commit that referenced this pull request Aug 3, 2026
Semgrep's dangerous-exec-command rule is blocking in this repo (#368), and
it fires on every `exec.Command` with a non-literal argument — which is all
six of them here. Annotated in the repo's existing style rather than
widened in .semgrepignore, so each one carries its own reason:

- `runQuietly`: the binary is a literal at both call sites ("scp", "ssh")
  and the args come from scpArgs/SSHExecArgs. No shell is involved, so an
  argument holding shell metacharacters is one argv element, not a command.
- `RunLocal`: running a caller-named binary IS the contract. The path comes
  from the operator invoking spawn, who already has a local shell.
- The four test sites: fixtures. Two of them run a shell deliberately —
  the forged-readiness-line reproduction needs one, and WrapRemoteCommand
  produces a shell script by design, so verifying the reap-on-EOF wrapper
  means running it.

Also replaces the test server's `fmt.Fprint(w, body)` with `w.Write`, which
is what it should have been: there is nothing to format, and it drops the
no-fprintf-to-responsewriter finding rather than suppressing it.
scttfrdmn added a commit that referenced this pull request Aug 3, 2026
#481)

Adds a verb for the shape spawn had no answer to: run a long-lived HTTP
service on an instance and get a working local URL for it. `spawn launch`
assumes a job that ends; `spawn connect -- cmd` gives no way to learn the
port. Neither closes the loop.

The seam is one JSON line on the service's stdout:

    {"event":"ready","addr":"127.0.0.1:54321","provenance":{...}}

The service picks its own port (`:0`) and announces what it bound; spawn
forwards to that. Nothing guesses a port, and nothing polls one — "is 8080
open yet?" cannot tell *my service starting* from *someone else's already
there*. spawn does not name any workload and no workload names spawn.

pkg/service is the reusable core (local child, readiness scan, tunnel,
remote-over-SSH ladder); cmd/service.go is the verb;
docs/service-readiness-contract.md is the public spec.

Two hazards drove the design, both covered by tests:

- The readiness line is forgeable. `init()` runs before `main`, so a
  workload can always print a well-formed line of its choosing first —
  "first match wins" is defeated by construction. Each candidate address
  is verified before it is trusted.
- Verification is an HTTP request, not a TCP connect, because `ssh -L`
  accepts on the local end whether or not anything listens on the far
  side. A connect to a forward proves only that ssh is running. Any HTTP
  status counts, including 401/404 — the question is whether a server is
  there, not whether the caller is authorized.

Lifetime: closing an SSH session does not reliably kill what it started
(no TTY, so no SIGHUP), so the remote command is wrapped to TERM→KILL the
service on stdin EOF. That is a request, not a guarantee — a partition can
strand it. The instance TTL is the guarantee, so the launch path goes
through the same mandatory-TTL zombie guard as every other launch, and
`--dry-run` quotes the bound (rate × ttl) before spending anything.
`--host` attaches to an instance spawn did not launch and promises no
teardown.

Exposure is loopback-only on both ends. A supplied token goes into the
printed URL but never into the loggable address form.

Tested at $0 over a local loopback path — the Substrate emulator is
control-plane only (instances never boot, no SSH), so the emulator could
not keep the first real spawn from being the first test. `service` is
declared Tier 2-deferred in the coverage gate for that reason. Semgrep's
exec and ResponseWriter findings are triaged inline per the repo's #368
convention, each with its own reason.

Closes #409.
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.

1 participant