Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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.

Blame · internal/sshdconfig/sshdconfig_test.go

Line attribution on main · 173 line(s)

09610c0f
Keenan Nemetz
16d ago
1 package sshdconfig
2
883ffcc9
Keenan Nemetz
12d ago
3 // The shipped drop-ins are rendered results, not hand-written files: the test
4 // below compares each one with the renderer, so what the package installs and
5 // what the binary expects cannot drift. `go test ./internal/sshdconfig -update`
6 // rewrites both, which is the only way they should ever change.
09610c0f
Keenan Nemetz
16d ago
7
8 import (
9 "flag"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "strings"
14 "testing"
15 )
16
883ffcc9
Keenan Nemetz
12d ago
17 var update = flag.Bool("update", false, "rewrite the shipped drop-ins from the renderer")
09610c0f
Keenan Nemetz
16d ago
18
883ffcc9
Keenan Nemetz
12d ago
19 // shippedDropIn is one committed file and the defaults it is rendered from.
20 type shippedDropIn struct {
21 platform string
22 path string
23 options Options
24 }
09610c0f
Keenan Nemetz
16d ago
25
883ffcc9
Keenan Nemetz
12d ago
26 func shippedDropIns() []shippedDropIn {
27 return []shippedDropIn{
28 {"linux", "../../packaging/sshd/vole-linux.conf", LinuxDefaults()},
29 {"darwin", "../../packaging/sshd/vole-darwin.conf", DarwinDefaults()},
09610c0f
Keenan Nemetz
16d ago
30 }
883ffcc9
Keenan Nemetz
12d ago
31 }
32
33 func TestShippedDropInsMatchTheRenderer(t *testing.T) {
34 for _, dropIn := range shippedDropIns() {
35 got := Render(dropIn.options)
36 if *update {
37 if err := os.WriteFile(dropIn.path, []byte(got), 0o644); err != nil {
38 t.Fatal(err)
39 }
40 }
41 want, err := os.ReadFile(dropIn.path)
42 if err != nil {
43 t.Fatalf("read %s: %v (run: go test ./internal/sshdconfig -update)", dropIn.path, err)
44 }
45 if string(want) != got {
46 t.Errorf("%s (%s) is not what the renderer produces (run: go test ./internal/sshdconfig -update)",
47 dropIn.platform, dropIn.path)
48 }
09610c0f
Keenan Nemetz
16d ago
49 }
50 }
51
52 func TestRenderNamesTheConfiguredAccountAndPaths(t *testing.T) {
53 t.Parallel()
54 got := Render(Options{User: "vole-git", Vole: "/opt/vole", Config: "/srv/vole.toml"})
55 for _, want := range []string{
56 "Match User vole-git",
57 `AuthorizedKeysCommand /opt/vole ssh-authorized-keys --config /srv/vole.toml --fingerprint %f --type %t --key %k --connection %C`,
58 "AuthorizedKeysCommandUser vole",
59 "AuthorizedKeysFile none",
60 "DisableForwarding yes",
61 "PermitTTY no",
62 } {
63 if !strings.Contains(got, want) {
64 t.Errorf("the rendered drop-in is missing %q:\n%s", want, got)
65 }
66 }
67 }
68
883ffcc9
Keenan Nemetz
12d ago
69 // TestDarwinRenderNamesTheMacCommands is the whole reason the renderer knows a
70 // platform: the Match block is the same, and every command around it is not.
71 func TestDarwinRenderNamesTheMacCommands(t *testing.T) {
72 t.Parallel()
73 got := Render(Options{
74 Platform: "darwin",
75 User: "you",
76 Vole: "/opt/homebrew/bin/vole",
77 Config: "/opt/homebrew/etc/vole/vole.toml",
78 Socket: "/opt/homebrew/var/vole/ssh.sock",
79 })
80 for _, want := range []string{
81 "sudo sshd -t && sudo launchctl kickstart -k system/com.openssh.sshd",
82 "systemsetup -setremotelogin on",
83 "brew services restart vole",
84 "sudo sysadminctl -addUser you -shell /usr/local/lib/vole/ssh-shell ...",
85 `ssh_socket = "/opt/homebrew/var/vole/ssh.sock"`,
86 "Include /etc/ssh/vole.conf",
87 "AuthorizedKeysCommand /opt/homebrew/bin/vole ssh-authorized-keys --config /opt/homebrew/etc/vole/vole.toml",
88 } {
89 if !strings.Contains(got, want) {
90 t.Errorf("the macOS render is missing %q:\n%s", want, got)
91 }
92 }
93 for _, unwanted := range []string{"systemctl", "useradd"} {
94 if strings.Contains(got, unwanted) {
95 t.Errorf("the macOS render names %q, which does not exist there", unwanted)
96 }
97 }
98 }
99
09610c0f
Keenan Nemetz
16d ago
100 func TestDropInDisablesEverythingButTheForcedCommand(t *testing.T) {
101 t.Parallel()
883ffcc9
Keenan Nemetz
12d ago
102 got := Render(LinuxDefaults())
09610c0f
Keenan Nemetz
16d ago
103 // Every one of these is a way in that must not exist for this account. A
104 // dropped line is silent, so each is checked by name.
105 for _, line := range []string{
106 "AuthorizedKeysFile none",
107 "PasswordAuthentication no",
108 "KbdInteractiveAuthentication no",
109 "DisableForwarding yes",
110 "PermitTTY no",
111 "PermitUserRC no",
112 } {
113 if !strings.Contains(got, "\n "+line+"\n") {
114 t.Errorf("the drop-in does not set %q as a Match directive", line)
115 }
116 }
117 }
118
119 // TestSshdAcceptsTheDropIn runs the real sshd over the rendered file when sshd
120 // and ssh-keygen are installed. The verify image has neither, so it skips there;
121 // on a host that can check it, this is the answer that matters.
122 func TestSshdAcceptsTheDropIn(t *testing.T) {
123 t.Parallel()
124 sshd, err := exec.LookPath("sshd")
125 if err != nil {
126 t.Skip("sshd is not installed")
127 }
128 if _, err := exec.LookPath("ssh-keygen"); err != nil {
129 t.Skip("ssh-keygen is not installed")
130 }
131
132 dir := t.TempDir()
133 key := filepath.Join(dir, "host_ed25519")
134 if out, err := exec.Command("ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-f", key).CombinedOutput(); err != nil {
135 t.Fatalf("ssh-keygen: %v\n%s", err, out)
136 }
883ffcc9
Keenan Nemetz
12d ago
137 // Both files carry the same directives; only the comments around them differ
138 // by platform, and sshd ignores those. So Linux sshd checks the macOS file too.
139 for _, dropIn := range shippedDropIns() {
140 // A host key is the minimum sshd needs to consider a file a whole config; the
141 // drop-in itself is only a Match block.
142 config := filepath.Join(dir, dropIn.platform+"_sshd_config")
143 body := "HostKey " + key + "\nPidFile none\n" + Render(dropIn.options)
144 if err := os.WriteFile(config, []byte(body), 0o600); err != nil {
145 t.Fatal(err)
146 }
09610c0f
Keenan Nemetz
16d ago
147
883ffcc9
Keenan Nemetz
12d ago
148 // -T parses the file and prints the effective configuration. -C supplies a
149 // connection spec, without which the dump is the global context and the Match
150 // block would not be applied at all: a drop-in that sshd ignored would pass.
151 out, err := exec.Command(sshd, "-T", "-f", config,
152 "-C", "user="+dropIn.options.User+",host=example.test,addr=127.0.0.1").CombinedOutput()
153 if err != nil {
154 if strings.Contains(string(out), "privilege separation") {
155 t.Skipf("sshd cannot validate a config in this environment: %s", strings.TrimSpace(string(out)))
156 }
157 t.Fatalf("sshd rejected the %s drop-in: %v\n%s", dropIn.platform, err, out)
09610c0f
Keenan Nemetz
16d ago
158 }
883ffcc9
Keenan Nemetz
12d ago
159 effective := strings.ToLower(string(out))
160 for _, want := range []string{
161 "authorizedkeysfile none",
162 "authorizedkeyscommand " + strings.ToLower(dropIn.options.Vole) + " ssh-authorized-keys",
163 "authorizedkeyscommanduser " + strings.ToLower(dropIn.options.ServiceUser),
164 "permittty no",
165 "permituserrc no",
166 "disableforwarding yes",
167 } {
168 if !strings.Contains(effective, want) {
169 t.Errorf("sshd did not apply %q to the %s drop-in:\n%s", want, dropIn.platform, effective)
170 }
09610c0f
Keenan Nemetz
16d ago
171 }
172 }
173 }