Debian Codegraph @

e2e/sshd_test.go

raw
1 //go:build e2e_testing
2 // +build e2e_testing
3
4 package e2e
5
6 import (
7 "crypto/ed25519"
8 "crypto/rand"
9 "encoding/pem"
10 "net"
11 "strings"
12 "testing"
13 "time"
14
15 "github.com/slackhq/nebula/cert"
16 "github.com/slackhq/nebula/cert_test"
17 "github.com/stretchr/testify/assert"
18 "github.com/stretchr/testify/require"
19 "golang.org/x/crypto/ssh"
20 )
21
22 func TestSSHDLifecycle(t *testing.T) {
23 // TestSSHDLifecycle exercises the in-process sshd through several config reloads and a Control.Stop.
24 ca, _, caKey, _ := cert_test.NewTestCaCert(
25 cert.Version1, cert.Curve_CURVE25519,
26 time.Now(), time.Now().Add(10*time.Minute),
27 nil, nil, []string{},
28 )
29
30 hostKeyPEM := generateSSHHostKey(t)
31 clientSigner, clientAuthKey := generateSSHClientKey(t)
32 sshdAddr := allocLoopbackPort(t)
33
34 overrides := m{
35 "sshd": m{
36 "enabled": true,
37 "listen": sshdAddr,
38 "host_key": hostKeyPEM,
39 "authorized_users": []m{{
40 "user": "tester",
41 "keys": []string{clientAuthKey},
42 }},
43 },
44 }
45 control, _, _, _ := newSimpleServer(cert.Version1, ca, caKey, "sshd-test", "10.222.0.1/24", overrides)
46 control.Start()
47 t.Cleanup(func() { control.Stop() })
48
49 // sshd binds in a goroutine after Start returns; wait for it.
50 require.Eventually(t, func() bool { return canDial(sshdAddr) }, 2*time.Second, 25*time.Millisecond,
51 "sshd never started listening")
52
53 for i := 1; i <= 3; i++ {
54 out := sshExecReload(t, sshdAddr, clientSigner)
55 assert.Contains(t, out, "Reloading config", "reload cycle %d", i)
56 require.Eventually(t, func() bool { return canDial(sshdAddr) }, 2*time.Second, 25*time.Millisecond,
57 "sshd not listening after reload cycle %d", i)
58 }
59
60 control.Stop()
61 require.Eventually(t, func() bool { return !canDial(sshdAddr) }, 2*time.Second, 25*time.Millisecond,
62 "sshd still listening after Control.Stop")
63 }
64
65 func canDial(addr string) bool {
66 c, err := net.DialTimeout("tcp", addr, 100*time.Millisecond)
67 if err != nil {
68 return false
69 }
70 _ = c.Close()
71 return true
72 }
73
74 // allocLoopbackPort grabs an unused TCP port on 127.0.0.1, closes it, and returns the address. There
75 // is a small race between releasing the port and the sshd reclaiming it; in practice the OS keeps the
76 // port available long enough for the test to bind it.
77 func allocLoopbackPort(t *testing.T) string {
78 t.Helper()
79 l, err := net.Listen("tcp", "127.0.0.1:0")
80 require.NoError(t, err)
81 addr := l.Addr().String()
82 require.NoError(t, l.Close())
83 return addr
84 }
85
86 func generateSSHHostKey(t *testing.T) string {
87 t.Helper()
88 _, priv, err := ed25519.GenerateKey(rand.Reader)
89 require.NoError(t, err)
90 block, err := ssh.MarshalPrivateKey(priv, "nebula-e2e-host")
91 require.NoError(t, err)
92 return string(pem.EncodeToMemory(block))
93 }
94
95 func generateSSHClientKey(t *testing.T) (ssh.Signer, string) {
96 t.Helper()
97 _, priv, err := ed25519.GenerateKey(rand.Reader)
98 require.NoError(t, err)
99 signer, err := ssh.NewSignerFromKey(priv)
100 require.NoError(t, err)
101 auth := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(signer.PublicKey())))
102 return signer, auth
103 }
104
105 func sshExecReload(t *testing.T, addr string, signer ssh.Signer) string {
106 t.Helper()
107 cfg := &ssh.ClientConfig{
108 User: "tester",
109 Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
110 HostKeyCallback: ssh.InsecureIgnoreHostKey(),
111 Timeout: 2 * time.Second,
112 }
113 client, err := ssh.Dial("tcp", addr, cfg)
114 require.NoError(t, err)
115 defer client.Close()
116
117 sess, err := client.NewSession()
118 require.NoError(t, err)
119 defer sess.Close()
120
121 // reload tears the channel down before sending exit-status, so Output returns an error on the
122 // channel close. The output buffer still has whatever the reload callback wrote before that.
123 out, _ := sess.Output("reload")
124 return string(out)
125 }