vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
| 1 | package ssh |
| 2 | |
| 3 | // A front-door instance has no SSH listener, so it has no host key of its own. |
| 4 | // What a job still needs is the key it will see when it connects: sshd's. These |
| 5 | // tests cover reading it, refusing to start without it, and refusing to accept |
| 6 | // connections when there is nothing to present. |
| 7 | |
| 8 | import ( |
| 9 | "net" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | xssh "golang.org/x/crypto/ssh" |
| 16 | ) |
| 17 | |
| 18 | // canonicalKey is an authorized_keys line without its comment, which is the form |
| 19 | // both host-key paths produce. |
| 20 | func canonicalKey(t *testing.T, line string) string { |
| 21 | t.Helper() |
| 22 | k, _, _, _, err := xssh.ParseAuthorizedKey([]byte(line)) |
| 23 | if err != nil { |
| 24 | t.Fatalf("parse key: %v", err) |
| 25 | } |
| 26 | return strings.TrimSpace(string(xssh.MarshalAuthorizedKey(k))) |
| 27 | } |
| 28 | |
| 29 | func TestFrontDoorCarriesSshdsHostKeyInALease(t *testing.T) { |
| 30 | t.Parallel() |
| 31 | dir := t.TempDir() |
| 32 | _, pubLine := keypair(t, filepath.Join(dir, "sshd")) |
| 33 | pubPath := filepath.Join(dir, "sshd.pub") |
| 34 | privatePath := filepath.Join(dir, "data_host_key") |
| 35 | |
| 36 | srv, err := New(Config{ |
| 37 | Log: discardLogger(), |
| 38 | PublicHostKeyPath: pubPath, |
| 39 | HostKeyPath: privatePath, |
| 40 | }) |
| 41 | if err != nil { |
| 42 | t.Fatalf("new: %v", err) |
| 43 | } |
| 44 | t.Cleanup(func() { srv.Close() }) |
| 45 | |
| 46 | // The comment is not part of the key, and the built-in path's line has none |
| 47 | // either; what has to match is the key itself. |
| 48 | if got, want := srv.hostKey, canonicalKey(t, pubLine); got != want { |
| 49 | t.Errorf("lease host key is %q, want sshd's %q", got, want) |
| 50 | } |
| 51 | // Creating a private key nobody presents would be a secret with no purpose. |
| 52 | if _, err := os.Stat(privatePath); !os.IsNotExist(err) { |
| 53 | t.Errorf("a private host key was created at %s", privatePath) |
| 54 | } |
| 55 | |
| 56 | // There is no signer, so there is no SSH listener this server can accept on. |
| 57 | ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | defer ln.Close() |
| 62 | if err := srv.Serve(ln); err == nil { |
| 63 | t.Fatal("a front-door server accepted SSH connections") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestFrontDoorRefusesToStartWithoutTheHostKey(t *testing.T) { |
| 68 | t.Parallel() |
| 69 | _, err := New(Config{ |
| 70 | Log: discardLogger(), |
| 71 | PublicHostKeyPath: filepath.Join(t.TempDir(), "missing.pub"), |
| 72 | }) |
| 73 | if err == nil { |
| 74 | t.Fatal("a front-door server started with no sshd host key") |
| 75 | } |
| 76 | if !strings.Contains(err.Error(), "missing.pub") { |
| 77 | t.Errorf("the error does not name the file: %v", err) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestFrontDoorRefusesAFileThatIsNotAKey(t *testing.T) { |
| 82 | t.Parallel() |
| 83 | path := filepath.Join(t.TempDir(), "sshd.pub") |
| 84 | if err := os.WriteFile(path, []byte("not a key\n"), 0o644); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | if _, err := New(Config{Log: discardLogger(), PublicHostKeyPath: path}); err == nil { |
| 88 | t.Fatal("a file that is not a key was accepted as the host key") |
| 89 | } |
| 90 | } |
| 91 |