1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
package integrationtests
import (
"bufio"
"context"
"encoding/base64"
"fmt"
"io"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/mimecast/dtail/internal/protocol"
"github.com/mimecast/dtail/internal/user"
gossh "golang.org/x/crypto/ssh"
)
func TestDServerProtocolVersionMismatchReportsCompatibilityError(t *testing.T) {
skipIfNotIntegrationTest(t)
testLogger := NewTestLogger("TestDServerProtocolVersionMismatchReportsCompatibilityError")
defer testLogger.WriteLogFile()
cleanupTmpFiles(t)
ctx, cancel := createTestContextWithTimeout(t)
ctx = WithTestLogger(ctx, testLogger)
defer cancel()
port := getUniquePortNumber()
serverStdout, serverStderr, _, err := startCommand(ctx, t, "", "../dserver",
"--cfg", "none",
"--logger", "stdout",
"--logLevel", "error",
"--bindAddress", "localhost",
"--port", fmt.Sprintf("%d", port),
)
if err != nil {
t.Fatalf("start dserver: %v", err)
}
_ = startProcessOutputCollector(ctx, serverStdout, serverStderr)
if err := waitForServerReady(ctx, "localhost", port); err != nil {
t.Fatalf("wait for dserver: %v", err)
}
tests := []struct {
name string
protocolVersion string
expectedUpdate string
}{
{
name: "major-only-client-guidance",
protocolVersion: "4",
expectedUpdate: "client",
},
{
name: "older-client-guidance",
protocolVersion: "0",
expectedUpdate: "client",
},
{
name: "newer-client-guidance",
protocolVersion: "5",
expectedUpdate: "server",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := runProtocolMismatchSession(ctx, t, fmt.Sprintf("localhost:%d", port), test.protocolVersion)
if !strings.Contains(output, "the DTail server protocol version '"+protocol.ProtocolCompat+"' does not match") {
t.Fatalf("expected protocol mismatch error in SSH output:\n%s", output)
}
if !strings.Contains(output, "please update DTail "+test.expectedUpdate) {
t.Fatalf("expected mixed-version compatibility guidance in output:\n%s", output)
}
})
}
}
// This wire-level probe is the strongest compatibility coverage available here:
// ProtocolCompat is compiled into both client and server binaries, and the repo
// does not ship historical release artifacts that would let us launch a real
// old/new binary pair in integration tests.
func runProtocolMismatchSession(ctx context.Context, t *testing.T, address string, protocolVersion string) string {
t.Helper()
client, session, stdin, lines, err := openSSHSession(ctx, t, address)
if err != nil {
t.Fatalf("open ssh session: %v", err)
}
defer client.Close()
defer session.Close()
rawCommand := "protocol " + protocolVersion + " base64 " + base64.StdEncoding.EncodeToString([]byte("tail: . /tmp/ignored .")) + ";"
if _, err := io.WriteString(stdin, rawCommand); err != nil {
t.Fatalf("write protocol mismatch command: %v", err)
}
output, ok := waitForSSHOutputContains(ctx, session, lines,
"the DTail server protocol version '"+protocol.ProtocolCompat+"' does not match")
if !ok {
t.Fatalf("expected protocol mismatch error in SSH output:\n%s", output)
}
return output
}
func openSSHSession(ctx context.Context, t *testing.T, address string) (*gossh.Client, *gossh.Session, io.WriteCloser, <-chan string, error) {
t.Helper()
signer, err := loadTestSigner()
if err != nil {
return nil, nil, nil, nil, err
}
clientConfig := &gossh.ClientConfig{
User: user.Name(),
Auth: []gossh.AuthMethod{gossh.PublicKeys(signer)},
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
Timeout: 5 * time.Second,
}
conn, err := gossh.Dial("tcp", address, clientConfig)
if err != nil {
return nil, nil, nil, nil, err
}
session, err := conn.NewSession()
if err != nil {
conn.Close()
return nil, nil, nil, nil, err
}
stdin, err := session.StdinPipe()
if err != nil {
session.Close()
conn.Close()
return nil, nil, nil, nil, err
}
stdout, err := session.StdoutPipe()
if err != nil {
session.Close()
conn.Close()
return nil, nil, nil, nil, err
}
stderr, err := session.StderrPipe()
if err != nil {
session.Close()
conn.Close()
return nil, nil, nil, nil, err
}
if err := session.Shell(); err != nil {
session.Close()
conn.Close()
return nil, nil, nil, nil, err
}
lines := make(chan string, 32)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
select {
case lines <- scanner.Text():
case <-ctx.Done():
return
}
}
}()
go func() {
defer wg.Done()
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
select {
case lines <- scanner.Text():
case <-ctx.Done():
return
}
}
}()
go func() {
wg.Wait()
close(lines)
}()
return conn, session, stdin, lines, nil
}
func loadTestSigner() (gossh.Signer, error) {
for _, path := range []string{"id_rsa", "../id_rsa"} {
keyBytes, err := os.ReadFile(path)
if err != nil {
continue
}
signer, err := gossh.ParsePrivateKey(keyBytes)
if err == nil {
return signer, nil
}
}
return nil, fmt.Errorf("unable to load test ssh private key from id_rsa")
}
func waitForSSHOutputContains(ctx context.Context, session *gossh.Session, lines <-chan string, needle string) (string, bool) {
var output strings.Builder
timeout := time.NewTimer(5 * time.Second)
defer timeout.Stop()
drainRemaining := func() {
drainTimeout := time.NewTimer(2 * time.Second)
defer drainTimeout.Stop()
for {
select {
case line, ok := <-lines:
if !ok {
return
}
if output.Len() > 0 {
output.WriteByte('\n')
}
output.WriteString(line)
case <-drainTimeout.C:
return
case <-ctx.Done():
return
}
}
}
for {
select {
case line, ok := <-lines:
if !ok {
return output.String(), strings.Contains(output.String(), needle)
}
if output.Len() > 0 {
output.WriteByte('\n')
}
output.WriteString(line)
if strings.Contains(output.String(), needle) {
_ = session.Close()
return output.String(), true
}
case <-timeout.C:
_ = session.Close()
drainRemaining()
return output.String(), strings.Contains(output.String(), needle)
case <-ctx.Done():
_ = session.Close()
drainRemaining()
return output.String(), strings.Contains(output.String(), needle)
}
}
}
|