Skip to content

feat(connect): inject SSH key over SSM when we don't hold the launch key - #114

Merged
scttfrdmn merged 2 commits into
mainfrom
feat/connect-ssm-key-injection
Jun 12, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
feat/connect-ssm-key-injection

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Why

spawn connect to an instance whose launch key isn't on this machine — or that has no key at all — dropped straight to a Session Manager shell. Instances launched headlessly by lagotto/cohort are keyless by design (the launcher had no SSH key on disk, lagotto#19), so interactive SSH was impossible. Per the design discussion: if connect can't find a key, it should inject one over SSM; if the spore never needs interactive connect, no harm done.

What

When findSSHKey can't locate a local key for the instance, connect now:

  1. Ensures spawn's managed ED25519 keypair exists locally (idempotent).
  2. Appends its public key to the login user's authorized_keys over SSM RunCommand.
  3. SSHes with the matching private key.

If injection isn't possible (no SSM agent / no SSM permissions / command fails), it falls back to the Session Manager shell exactly as before — no regression for instances that genuinely can't be reached.

  • pkg/aws/ssm.go: RunShellScript — Linux sibling of the existing RunPowerShell (AWS-RunShellScript, polls to completion).
  • cmd/connect.go: injectSSHKeyViaSSM + authorizedKeyInjectionScript (pure, unit-tested). The script is idempotent (grep-guarded append), sets correct perms (700/.ssh, 600/authorized_keys) and ownership, resolves the home dir via getent (not hardcoded), and rejects unsafe user / key content so it can't break out of the single-quoted shell literal.

Tests

  • Script generation: well-formed output, idempotent-append guard, rejection of unsafe usernames and keys (quote/newline injection).
  • make check green.

Chain

Completes the lagotto#19 work: lagotto launches keyless spores via launcher.Provision (#113); spawn connect can now reach them interactively when a human wants in.

`spawn connect` to an instance whose launch key isn't on this machine — or that
has no key at all — used to drop straight to a Session Manager shell. Instances
launched headlessly by lagotto/cohort are keyless by design (the launcher had no
SSH key on disk), so interactive SSH was impossible.

Now, when findSSHKey can't locate a local key for the instance, connect ensures
spawn's managed ED25519 keypair exists and appends its public key to the login
user's authorized_keys over SSM RunCommand, then SSHes with the matching private
key. If injection isn't possible (no SSM agent / no SSM permissions / command
fails) it falls back to the Session Manager shell as before.

- pkg/aws/ssm.go: RunShellScript — Linux sibling of RunPowerShell
  (AWS-RunShellScript, polls to completion).
- cmd/connect.go: injectSSHKeyViaSSM + authorizedKeyInjectionScript (pure,
  unit-tested). The script is idempotent (grep-guarded append), sets correct
  perms/ownership, resolves the home dir via getent, and rejects unsafe user /
  key content so it can't break out of the single-quoted literal.

Completes the lagotto#19 chain: lagotto launches keyless spores; connect can now
reach them interactively when needed.
@codecov

codecov Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 46.15385% with 56 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/connect.go 29.23% 46 Missing ⚠️
pkg/aws/ssm.go 74.35% 9 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Adds substrate-backed tests for the Linux SSM RunCommand helper that connect's
key-injection uses, plus a timeout-doesn't-hang guard. Closes the patch-coverage
gap on the SSM plumbing.
@scttfrdmn
scttfrdmn merged commit b269ae3 into main Jun 12, 2026
3 checks passed
@scttfrdmn
scttfrdmn deleted the feat/connect-ssm-key-injection branch June 12, 2026 04:36
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