Skip to content

fix(connect,launch): run one-shot commands over SSM (#1306); launch flags for spored metrics/tracing/idle-cpu (#1308) - #1337

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/1306-1308-connect-tags
Oct 26, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/1306-1308-connect-tags

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1306
Fixes #1308

#1306: one-shot spawn connect <linux> -- <cmd> over SSM

The four SSM paths (no public IP, --session-manager, security groups closed to SSH, failed key injection) all go through a new connectOverSSM. A one-shot command runs over SSM RunCommand (AWS-RunShellScript via the existing Client.RunShellScript); only a command-less connect opens the interactive session. The v0.145.0 "closed port + one-shot = immediate error" is gone. That case now runs the command too, with a note saying so.

Why RunCommand, not start-session --document-name AWS-StartInteractiveCommand: RunCommand returns the exit code (ResponseCode), keeps stdout and stderr apart, and needs neither the AWS CLI nor the session-manager-plugin locally. A session runs on a PTY, which merges the two streams, and it does not hand the remote status back to the caller.

Trade-offs, all documented in the code, the CHANGELOG and the --session-manager help:

  • output arrives when the command exits, not streamed;
  • SSM keeps only the first 24,000 chars of stdout and 8,000 of stderr; connect warns when the output reaches that size;
  • stdin is not forwarded, and --tty has no effect (a note says so);
  • the command is limited to AWS-RunShellScript's 1h default (the poll waits 61 min, so SSM's own TimedOut is reported).

User: RunCommand runs as root, so the script drops to the SSH login user, resolveSSHUser (--user > spawn:local-username > ec2-user). It runs sudo -H -u $u -- $loginshell -c "$cmd" from that user's home, which matches sshd. The command travels base64-encoded, so nothing needs quoting. An unknown user exits 255, ssh's code for "could not run anything".

Exit codes: a new remoteExitError is mapped by exitForError. The SSH path now passes the remote status through too (remoteExit around ssh.Run()), rather than exiting 1 for any failure. That is listed under Changed, so a script sees the same code whichever transport connect picked. Windows' SSM one-shot path is unchanged.

No new permission: the caller already needs ssm:SendCommand/GetCommandInvocation for key injection and status over SSM, and the instance role already has AmazonSSMManagedInstanceCore.

#1308: launch flags for spored's settings

--metrics, --metrics-port, --metrics-bind, --metrics-path, --tracing, --tracing-exporter, --tracing-sampling, --idle-cpu-threshold. They become LaunchConfig.Observability (new aws.SporedObservability) and LaunchConfig.IdleCPUPercent, and buildTags writes spawn:metrics-{enabled,port,bind,path}, spawn:tracing-{enabled,exporter,sampling} and spawn:idle-cpu.

They are validated by LaunchConfig.ValidateSporedSettings, both in runLaunch before any AWS call and inside buildTags for SDK callers:

  • a companion flag without its enabling flag is refused;
  • the port must be 1024-65535, which is spored's own accepted range;
  • the bind must be an IP address or localhost;
  • the path must start with /;
  • sampling must be between 0 and 1 (0 is kept);
  • the idle threshold must be above 0 and at most 100. 0 is refused because the idle stop could then never fire.

Tracing is limited to the stdout exporter. xray is refused with the reason: the spored role spawn creates has no xray:PutTraceSegments, and users must not add IAM by hand. spored's own default exporter is xray, so --tracing always writes spawn:tracing-exporter=stdout. No flag was added for spawn:tracing-endpoint, because nothing reads TracingConfig.Endpoint. Both are filed as #1335. spawn:tracing-endpoint stays on externallyWrittenTags, and the other eight entries are removed.

docs/self-hosted-monitoring.md now leads with spawn launch ... --metrics --metrics-bind 0.0.0.0. The create-tags + spored restart route is kept, under "An instance that is already running".

The flags are classified in the sweep manifest as honoured: every sweep row starts from buildLaunchConfig's struct, and the flag variables are read in launch_config.go. The Windows table marks them honoured too, since the Windows service runs the same runDaemon.

Tag budget, filed as #1336: maximalConfig (46 tags) plus every #1308 setting comes to 54, which is over EC2's 50. The documented monitoring setup fits at 49 (TestMonitoringSettingsFitOnAMaximalLaunch). buildTags has never refused over-limit launches itself (80 --tags already build 134 tags); #1336 proposes making it refuse them.

Gates, each watched failing against a revert that compiles

revert failing gate
connectOverSSM always opens a session (if false {) TestConnectOverSSMRunsTheOneShotCommand
runConnect's SSM verdict calls connectViaSessionManager directly TestRunConnectSendsEverySSMPathThroughConnectOverSSM (AST, discovers calls in runConnect), TestRunConnectUsesLinuxConnectTransport
key-injection fallback calls connectViaSessionManager directly TestRunConnectSendsEverySSMPathThroughConnectOverSSM
exitForError ignores the remote status TestRunOneShotOverSSMPropagatesStatusAndStreams, TestExitForErrorPassesTheRemoteStatus
Failed+code mapped to nil TestRunOneShotOverSSMPropagatesStatusAndStreams
script drops sudo -u (runs as root) TestOneShotSSMScriptRunsAsTheLoginUser
closed-port one-shot error restored TestConnectChecksTheSSHPortAndSendsOneShotToSSMWhenClosed
ssh.Run() not wrapped in remoteExit TestSSHToInstancePassesTheRemoteStatus (AST)
buildTags skips the metrics-bind write TestSporedSettingsSurviveTheTagHandOff (Launch on substrate, then spored's own loadConfigFromEC2Tags), TestMonitoringSettingsFitOnAMaximalLaunch
exporter tag not written TestSporedSettingsSurviveTheTagHandOff, TestSpawnTagContract
buildTags skips validation TestSporedSettingsAreValidated
xray accepted TestSporedSettingsAreValidated, TestSporedSettingFlagsAreValidated
buildLaunchConfig doesn't set the fields TestSporedSettingFlagsReachTheLaunchConfig
--idle-cpu-threshold 0 accepted TestSporedSettingFlagsAreValidated
doc's launch example loses --metrics / uses a nonexistent flag TestMonitoringDocLaunchesWithMetricsFlags

TestOneShotSSMScriptRunsTheCommandIntact runs the generated script under /bin/sh, with getent/sudo shims. It checks a command full of quotes, $HOME, and doubled spaces arrives intact, that the cwd is the user's home, that stderr stays separate, that exit 7 comes back as 7, and that an unknown user exits 255. It is skipped on Windows.

TestDocumentedLaunchTagsAreAccepted still passes.

Checks

  • make check: everything passes except check-build-tags, which fails identically on unmodified origin/main on this machine (the /private/tmp path issue); checked in a pristine worktree.
  • make lint: 0 issues in all 10 modules. semgrep scan --config=auto --error .: clean.
  • make api-snapshot committed (additions only). make gen-docs committed; make check-docs is clean.

Not verified

No live AWS run, so RunCommand against a real instance has not been tried: the sudo drop, getent on AL2023/Ubuntu, and whether a nohup ... & one-shot (the README example) survives AWS-RunShellScript's completion. The metrics/tracing flags have not been tried on a real spored boot either; the round trip is substrate tags into spored's own parser.

@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.21053% with 21 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/connect.go 47.05% 9 Missing ⚠️
pkg/aws/tags.go 61.11% 7 Missing ⚠️
cmd/connect_ssm_oneshot.go 91.89% 3 Missing ⚠️
cmd/launch_single.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

… spored metrics/tracing/idle-cpu

--session-manager, groups closed to SSH, failed key injection) now runs
the command with SSM RunCommand as the SSH login user and exits with its
status, instead of opening an interactive session that dropped it. The
SSH path passes the remote status through too.

--tracing, --tracing-exporter, --tracing-sampling and
--idle-cpu-threshold write the spawn:* tags spored reads at boot,
validated; docs/self-hosted-monitoring.md uses them and keeps the
create-tags route for running instances.

Fixes #1306
Fixes #1308
@scttfrdmn
scttfrdmn force-pushed the fix/1306-1308-connect-tags branch from 1c6ae96 to e9c44e7 Compare October 26, 2026 23:16
@scttfrdmn
scttfrdmn merged commit e6bac38 into main Oct 26, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1306-1308-connect-tags branch October 26, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant