Skip to content

Local-matching user login fails: spawn silently installs the wrong SSH key + DNS/connect used wrong user #349

Closed

Description

@scttfrdmn

Summary

Logging into a freshly-launched instance as the local-matching user (spawn's design: it creates a Linux user mirroring the controller's $USER and installs the SSH key for it) can silently fail with Permission denied (publickey). This also broke DNS registration (which SSHes in as that user) and produced a misleading ssh -i ~/.ssh/id_rsa … success hint.

Discovered during live testing: spawn launch then the printed connect path failed Permission denied, and DNS registration failed for the same reason before it ever reached the DNS API.

Root cause (the real bug)

spawnPublicKeyForUserData(plat, keyName) (cmd/launch_config.go) resolves the key pair's public key to install into $LOCAL_USERNAME's authorized_keys:

if keyName != "" {
    if priv, err := sshkey.Resolve(plat.HomeDir, keyName); err == nil {
        if pub, err := os.ReadFile(priv + ".pub"); err == nil {   // <-- needs a local .pub
            return pub, nil
        }
    }
}
pub, err := plat.ReadPublicKey()   // <-- SILENT fallback to ~/.ssh/id_rsa.pub

If the resolved private key has no adjacent .pub file (e.g. a key pair created via aws ec2 create-key-pair, which returns only the private key), it silently falls back to ~/.ssh/id_rsa.pub — a different key. The instance then trusts id_rsa for the local user, but the operator connects with the key pair they named → Permission denied. (ec2-user happens to work only because AWS installs the named EC2 key pair for the AMI's default user — masking the bug.)

Impact

  • The local-matching user — the whole point of spawn's user model — can be unusable via SSH, even though the launch "succeeded".
  • DNS registration SSHes in as that user and fails before calling the API.
  • The success connect hint was wrong (see launch: fix progress UX, connect hint, and DNS error reporting #348 — already changed to spawn connect <name>).

Fixes needed

  1. Don't silently substitute a key. In spawnPublicKeyForUserData, when a keyName is given but no .pub exists, derive the public key from the private key (parse it, marshal the authorized_keys line) instead of falling back to an unrelated id_rsa.pub. If it truly can't be resolved, fail loudly rather than install a mismatched key.
  2. Use the local-matching user everywhere, not a hardcoded default. registerDNS (cmd/launch_posthook.go) must SSH as $LOCAL_USERNAME (plat.GetUsername()), never a hardcoded ec2-user — the default login user differs across distros (ubuntu/rocky/admin/ec2-user) and contradicts the user model. (A prior stopgap that hardcoded ec2-user is being reverted.)

Repro

aws ec2 create-key-pair --key-name k --query KeyMaterial --output text > ~/.spawn/keys/k   # no .pub
spawn launch test --key-name k --instance-type t4g.small --region us-east-1 --yes
ssh -i ~/.spawn/keys/k <localuser>@<ip>   # Permission denied — instance trusts id_rsa, not k

Verification target

  • Deriving .pub from the private key makes $LOCAL_USERNAME login work with the named key pair.
  • DNS registration reaches the API as the local user (no Permission denied).
  • Confirm across at least one non-ec2-user distro (e.g. Ubuntu → ubuntu).

Activity

  1. added
    bug
    area:connect
    area:networking
    on Jul 13, 2026
  2. scttfrdmn commented on Jul 13, 2026

    @scttfrdmn
    ContributorAuthor

    Update: primary bug fixed + verified; a second (separate) DNS issue surfaced

    Fixed & live-verified: with public-key-derivation-from-private in place, SSH as the local-matching user now works with a .pub-less named key pair:

    $ ssh -i ~/.spawn/keys/plugin-e2e-test scttfrdmn@<ip>
    LOCAL_USER_LOGIN_OK
    uid=1001(scttfrdmn) gid=1001(scttfrdmn) groups=1001(scttfrdmn),10(wheel)
    

    (previously Permission denied). DNS registration now SSHes in as that local user successfully. registerDNS uses plat.GetUsername() (local-matching user), not a hardcoded default.

    Separate issue now visible (was masked before): DNS registration still fails, but now with a real error — could not reach DNS API (curl exit 6): 000. Root cause: the dns-updater Lambda URL (…lambda-url.us-east-1.on.aws) resolves to IPv6-only addresses, and the instance has no IPv6 route, so curl can't connect (exit 6 = couldn't resolve/route host). This is a reachability problem in the DNS-registration transport, independent of the SSH/user bug — likely wants curl -4, or the registration should go through spored/signed-request path rather than curl-from-instance. Filing/So tracking separately if desired; the error is now at least diagnosable thanks to the message fix (#348).

  3. scttfrdmn commented on Jul 14, 2026

    @scttfrdmn
    ContributorAuthor

    DNS-registration transport hardened (pushed to #348 branch); residual is environmental

    Committed (fix/launch-ux, f4ff2d3):

    • curl -4 — the dns-updater Lambda URL is dual-stack (A + AAAA); IPv4-only instances have no IPv6 route.
    • Retry the whole SSH exec (fresh session each attempt, bounded 4 min). The launch's shallow port-22 readiness returns before cloud-init finishes, so early attempts fail with SSH Permission denied (local user's authorized_keys not written yet) or curl exit 6. Re-establishing a new session recovers the Permission-denied case (verified live) where an in-session loop can't.
    • Real HTTP status/body in the error message (was a swallowed generic string).

    Residual (characterized across ~7 live instances; environmental, not a spawn bug)

    On t4g.small in the dev account (942542972736), early-boot outbound public DNS resolution stays unavailable for ~4-5 minutes — long after IMDS (link-local 169.254) and SSH (port 22) are up. registerDNS's sessions get curl exit 6 the whole time, while a manual session ~5 min post-boot resolves instantly. So registration can still miss even a 4-min window. Two independent facts make this not worth chasing further here:

    1. DNS registration is non-fatal — the instance is fully usable via its public IP / spawn connect.
    2. This account's dns-updater endpoint returns HTTP 404 regardless (it isn't wired to serve this account), so registration would not succeed even if resolution were instant.

    The original reported bug is fixed (silent SSH-key substitution + wrong login user — see the key-derivation + local-user commits, verified live). The remaining transport residual is best addressed by moving DNS registration into spored (runs after the instance is network-ready, sidestepping the controller-SSH-into-early-boot race entirely) — proposing that as the real fix, tracked here. Leaving #349 open for that follow-up.

  4. scttfrdmn commented on Jul 17, 2026

    @scttfrdmn
    ContributorAuthor

    Folding in the 2026-06 audit's DNS-over-SSH finding (from #175). That audit flagged registerDNS shelling out to ssh with host-key verification disabled (StrictHostKeyChecking=no, UserKnownHostsFile=/dev/null) — an MITM window on the controller→instance hop used to register DNS. The recommended fix is the same one this issue already tracks: move DNS registration into spored (runs in-instance after the network is up, over the signed dns-updater path), which sidesteps the controller-SSH hop and its host-key exposure entirely. So the #175 finding is a duplicate of this — capturing it here so it isn't lost when #175 closes.

  5. scttfrdmn commented on Jul 21, 2026

    @scttfrdmn
    ContributorAuthor

    Fixed and shipped in spawn v0.73.0 (issue was left open by oversight).

    Both fixes from this report are in main and released:

    1. No silent key substitution. spawnPublicKeyForUserData (cmd/launch_config.go) now calls sshkey.PublicKeyForName, which reads the adjacent .pub if present and otherwise derives the public key from the private key (PublicKeyFromPrivateFile → ssh.ParsePrivateKey + MarshalAuthorizedKey); passphrase-protected keys error clearly. It never falls back to ~/.ssh/id_rsa.pub, and fails loudly if the key can't be resolved. So a key pair created via aws ec2 create-key-pair (private-only, no .pub) now installs the correct public half for the local-matching user.
    2. DNS registers as the local-matching user. registerDNS (cmd/launch_posthook.go:124) uses plat.GetUsername() ($USER), never a hardcoded ec2-user.

    Covered by tests in pkg/sshkey/sshkey_test.go: TestPublicKeyForName_DerivesFromPrivateWhenNoPubFile, _PrefersPubFile, _MissingKeyErrors (all green). Changelog entry under [0.73.0].

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:connectarea:networkingbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions