forked from scylladb/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_cluster_test.go
304 lines (255 loc) · 7.32 KB
/
cloud_cluster_test.go
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//go:build integration && scylla
// +build integration,scylla
package gocql_test
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/gocql/gocql"
"github.com/gocql/gocql/scyllacloud"
"sigs.k8s.io/yaml"
)
func TestCloudConnection(t *testing.T) {
if !*gocql.FlagRunSslTest {
t.Skip("Skipping because SSL is not enabled on cluster")
}
const (
sslPort = 9142
datacenterName = "datacenter1"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := map[string]string{}
cluster := gocql.CreateCluster(func(config *gocql.ClusterConfig) {
config.Port = sslPort
})
session, err := cluster.CreateSession()
if err != nil {
t.Fatal(err)
}
var localAddress string
var localHostID gocql.UUID
scanner := session.Query("SELECT broadcast_address, host_id FROM system.local").Iter().Scanner()
if scanner.Next() {
if err := scanner.Scan(&localAddress, &localHostID); err != nil {
t.Fatal(err)
}
hosts[localHostID.String()] = net.JoinHostPort(localAddress, fmt.Sprintf("%d", sslPort))
}
var peerAddress string
var peerHostID gocql.UUID
scanner = session.Query("SELECT peer, host_id FROM system.peers").Iter().Scanner()
for scanner.Next() {
if err := scanner.Scan(&peerAddress, &peerHostID); err != nil {
t.Fatal(err)
}
hosts[peerHostID.String()] = net.JoinHostPort(peerAddress, fmt.Sprintf("%d", sslPort))
}
session.Close()
logger := gocql.TestLogger
defer func() {
if t.Failed() {
os.Stdout.WriteString(logger.String())
}
}()
proxy := &sniProxy{
hosts: hosts,
defaultBackend: net.JoinHostPort(localAddress, fmt.Sprintf("%d", sslPort)),
logger: logger,
}
proxyAddress, err := proxy.Run(ctx)
if err != nil {
t.Fatal(err)
}
defer proxy.Close()
cc := &scyllacloud.ConnectionConfig{
Datacenters: map[string]*scyllacloud.Datacenter{
datacenterName: {
CertificateAuthorityPath: "testdata/pki/ca.crt",
Server: proxyAddress,
TLSServerName: "any",
NodeDomain: "cloud.scylladb.com",
InsecureSkipTLSVerify: true,
},
},
AuthInfos: map[string]*scyllacloud.AuthInfo{
"ai-1": {
Username: "username",
Password: "password",
ClientKeyPath: "testdata/pki/gocql.key",
ClientCertificatePath: "testdata/pki/gocql.crt",
},
},
Contexts: map[string]*scyllacloud.Context{
"default-context": {
AuthInfoName: "ai-1",
DatacenterName: datacenterName,
},
},
CurrentContext: "default-context",
}
configPath, err := writeYamlToTempFile(cc)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(configPath)
cluster, err = scyllacloud.NewCloudCluster(configPath)
if err != nil {
t.Fatal(err)
}
// Forward connections directed to node domain to our test sni proxy.
cluster.Dialer = dialerContextFunc(func(ctx context.Context, network, addr string) (net.Conn, error) {
if strings.Contains(addr, cc.Datacenters[datacenterName].NodeDomain) {
addr = cc.Datacenters[datacenterName].Server
}
return net.Dial(network, addr)
})
session, err = cluster.CreateSession()
if err != nil {
t.Fatal(err)
}
if err := gocql.WaitUntilPoolsStopFilling(ctx, session, 10*time.Second); err != nil {
t.Fatal(err)
}
ringHosts := gocql.GetRingAllHosts(session)
if len(ringHosts) != len(hosts) {
t.Errorf("expected %d hosts in ring, got %d", len(hosts), len(ringHosts))
}
snisCount := map[string]int{}
events := proxy.GetEvents()
for _, event := range events {
snisCount[event]++
}
for hostID := range hosts {
sni := fmt.Sprintf("%s.%s", hostID, cc.Datacenters[datacenterName].NodeDomain)
count, ok := snisCount[sni]
if !ok {
t.Errorf("not found connection to host %q", hostID)
}
if count != cluster.NumConns {
t.Errorf("expected %d connections to host %q, got %d", cluster.NumConns, sni, count)
}
}
}
func writeYamlToTempFile(obj interface{}) (string, error) {
f, err := os.CreateTemp(os.TempDir(), "gocql-cloud")
if err != nil {
return "", fmt.Errorf("create temp file: %w", err)
}
if err := f.Close(); err != nil {
return "", fmt.Errorf("close temp file: %w", err)
}
buf, err := yaml.Marshal(obj)
if err != nil {
return "", fmt.Errorf("marshal yaml: %w", err)
}
if err := os.WriteFile(f.Name(), buf, 0600); err != nil {
return "", fmt.Errorf("write to file %q: %w", f.Name(), err)
}
return f.Name(), nil
}
type dialerContextFunc func(ctx context.Context, network, addr string) (net.Conn, error)
func (d dialerContextFunc) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
return d(ctx, network, addr)
}
type sniProxy struct {
hosts map[string]string
defaultBackend string
logger gocql.StdLogger
listener net.Listener
events []string
mu sync.Mutex
}
func (p *sniProxy) Run(ctx context.Context) (string, error) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return "", fmt.Errorf("failed to listen: %w", err)
}
p.listener = listener
go func() {
for {
conn, err := p.listener.Accept()
if err != nil {
p.logger.Println("failed to accept connection", err)
return
}
go p.handleConnection(conn)
}
}()
return listener.Addr().String(), nil
}
func (p *sniProxy) handleConnection(conn net.Conn) {
defer conn.Close()
var hello *tls.ClientHelloInfo
peekedBytes := &bytes.Buffer{}
// Ignore error because TLS library returns it when nil TLSConfig is returned.
_ = tls.Server(readOnlyConn{reader: io.TeeReader(conn, peekedBytes)}, &tls.Config{
GetConfigForClient: func(argHello *tls.ClientHelloInfo) (*tls.Config, error) {
hello = &tls.ClientHelloInfo{}
*hello = *argHello
return nil, nil
},
}).Handshake()
if hello == nil {
p.logger.Println("client hello not sent")
return
}
p.mu.Lock()
p.events = append(p.events, hello.ServerName)
p.mu.Unlock()
backend, ok := p.hosts[hello.ServerName]
if !ok {
backend = p.defaultBackend
}
p.logger.Println("Dialing backend", backend, "SNI", hello.ServerName)
backendConn, err := net.Dial("tcp", backend)
if err != nil {
p.logger.Println("failed to dial backend", backend, err)
return
}
defer backendConn.Close()
var wg sync.WaitGroup
wg.Add(2)
go func() {
_, _ = io.Copy(conn, backendConn)
wg.Done()
}()
go func() {
_, _ = io.Copy(backendConn, peekedBytes)
_, _ = io.Copy(backendConn, conn)
wg.Done()
}()
wg.Wait()
}
func (p *sniProxy) Close() error {
return p.listener.Close()
}
func (p *sniProxy) GetEvents() []string {
p.mu.Lock()
defer p.mu.Unlock()
events := make([]string, 0, len(p.events))
for _, e := range p.events {
events = append(events, e)
}
return events
}
type readOnlyConn struct {
reader io.Reader
}
var _ net.Conn = readOnlyConn{}
func (conn readOnlyConn) Read(p []byte) (int, error) { return conn.reader.Read(p) }
func (conn readOnlyConn) Write(p []byte) (int, error) { return 0, io.ErrClosedPipe }
func (conn readOnlyConn) Close() error { return nil }
func (conn readOnlyConn) LocalAddr() net.Addr { return nil }
func (conn readOnlyConn) RemoteAddr() net.Addr { return nil }
func (conn readOnlyConn) SetDeadline(t time.Time) error { return nil }
func (conn readOnlyConn) SetReadDeadline(t time.Time) error { return nil }
func (conn readOnlyConn) SetWriteDeadline(t time.Time) error { return nil }