forked from cashapp/pranadb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_test.go
342 lines (304 loc) · 9.95 KB
/
cli_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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package client
import (
log "github.com/sirupsen/logrus"
"github.com/squareup/pranadb/conf"
"github.com/squareup/pranadb/errors"
"github.com/squareup/pranadb/internal/testcerts"
"github.com/squareup/pranadb/server"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"strings"
"testing"
)
var (
caCertPath string
caSignedServerKeyPath string
caSignedServerCertPath string
caSignedClientKeyPath string
caSignedClientCertPath string
selfSignedServerKeyPath string
selfSignedServerCertPath string
selfSignedClientKeyPath string
selfSignedClientCertPath string
selfSignedClientKeyPath2 string
selfSignedClientCertPath2 string
)
func TestMain(m *testing.M) {
// Set up all the certs we need here. Creating certs is slow, we don't want to do it on each test
log.Info("creating certificates required for tests")
tmpDir, err := ioutil.TempDir("", "cli_test")
if err != nil {
log.Fatalf("failed to create tmp dir %v", err)
}
caCert, err := testcerts.CreateTestCACert("acme CA")
if err != nil {
log.Fatalf("failed to create ca cert %v", err)
}
caCertPath, err = testcerts.WritePemToTmpFile(tmpDir, caCert.Pem.Bytes())
if err != nil {
log.Fatalf("failed to write pem file %v", err)
}
caSignedServerCertPath, caSignedServerKeyPath, err = testcerts.CreateCertKeyPairToTmpFile(tmpDir, caCert, "acme badgers ltd.")
if err != nil {
log.Fatalf("failed to cert key pair %v", err)
}
caSignedClientCertPath, caSignedClientKeyPath, err = testcerts.CreateCertKeyPairToTmpFile(tmpDir, caCert, "acme squirrels ltd.")
if err != nil {
log.Fatalf("failed to cert key pair %v", err)
}
selfSignedServerCertPath, selfSignedServerKeyPath, err = testcerts.CreateCertKeyPairToTmpFile(tmpDir, nil, "acme antelopes ltd.")
if err != nil {
log.Fatalf("failed to cert key pair %v", err)
}
selfSignedClientCertPath, selfSignedClientKeyPath, err = testcerts.CreateCertKeyPairToTmpFile(tmpDir, nil, "acme aardvarks ltd.")
if err != nil {
log.Fatalf("failed to cert key pair %v", err)
}
selfSignedClientCertPath2, selfSignedClientKeyPath2, err = testcerts.CreateCertKeyPairToTmpFile(tmpDir, nil, "acme squirrels ltd.")
if err != nil {
log.Fatalf("failed to cert key pair %v", err)
}
defer func() {
defer func() {
err := os.RemoveAll(tmpDir)
if err != nil {
log.Fatalf("failed to remove test tmpdir %v", err)
}
}()
}()
m.Run()
}
func TestPlaintextGRPC(t *testing.T) {
err := testClientTransport(t, false, TLSConfig{}, conf.TLSConfig{})
require.NoError(t, err)
}
func TestGRPCWithTlsNoClientAuth(t *testing.T) {
testWithTLSNoClientAuth(t, false)
}
func TestHTTPWithTlsNoClientAuth(t *testing.T) {
testWithTLSNoClientAuth(t, true)
}
func testWithTLSNoClientAuth(t *testing.T, httpAPI bool) {
t.Helper()
clientTLSConfig := TLSConfig{
EnableTLS: true,
TrustedCertsPath: caSignedServerCertPath,
}
serverTLSConfig := conf.TLSConfig{
Enabled: true,
KeyPath: caSignedServerKeyPath,
CertPath: caSignedServerCertPath,
}
err := testClientTransport(t, httpAPI, clientTLSConfig, serverTLSConfig)
require.NoError(t, err)
}
func TestGRPCWithTLSNoClientAuthNoServerTrust(t *testing.T) {
testWithTLSNoClientAuthNoServerTrust(t, false)
}
func TestHTTPWithTLSNoClientAuthNoServerTrust(t *testing.T) {
testWithTLSNoClientAuthNoServerTrust(t, true)
}
func testWithTLSNoClientAuthNoServerTrust(t *testing.T, httpAPI bool) {
t.Helper()
clientTLSConfig := TLSConfig{
EnableTLS: true,
DisableServerVerification: true,
}
serverTLSConfig := conf.TLSConfig{
Enabled: true,
KeyPath: caSignedServerKeyPath,
CertPath: caSignedServerCertPath,
}
err := testClientTransport(t, httpAPI, clientTLSConfig, serverTLSConfig)
require.NoError(t, err)
}
func TestGRPCWithTLSClientAuthSingleCA(t *testing.T) {
testWithTLSClientAuthSingleCA(t, false)
}
func TestHTTPWithTLSClientAuthSingleCA(t *testing.T) {
testWithTLSClientAuthSingleCA(t, true)
}
func testWithTLSClientAuthSingleCA(t *testing.T, httpAPI bool) {
t.Helper()
// Use the same CA that signs both the server and client certificate
clientTLSConfig := TLSConfig{
EnableTLS: true,
TrustedCertsPath: caCertPath,
KeyPath: caSignedClientKeyPath,
CertPath: caSignedClientCertPath,
}
serverTLSConfig := conf.TLSConfig{
Enabled: true,
KeyPath: caSignedServerKeyPath,
CertPath: caSignedServerCertPath,
ClientCertsPath: caCertPath,
ClientAuth: "require-and-verify-client-cert",
}
err := testClientTransport(t, httpAPI, clientTLSConfig, serverTLSConfig)
require.NoError(t, err)
}
func TestGRPCWithTLSClientAuthSelfSignedCerts(t *testing.T) {
testWithTLSClientAuthSelfSignedCerts(t, false)
}
func TestHTTPWithTLSClientAuthSelfSignedCerts(t *testing.T) {
testWithTLSClientAuthSelfSignedCerts(t, true)
}
func testWithTLSClientAuthSelfSignedCerts(t *testing.T, httpAPI bool) {
t.Helper()
// Using self signed certs for server and client - no common CA
clientTLSConfig := TLSConfig{
EnableTLS: true,
TrustedCertsPath: selfSignedServerCertPath,
KeyPath: selfSignedClientKeyPath,
CertPath: selfSignedClientCertPath,
}
serverTLSConfig := conf.TLSConfig{
Enabled: true,
KeyPath: selfSignedServerKeyPath,
CertPath: selfSignedServerCertPath,
ClientCertsPath: selfSignedClientCertPath,
ClientAuth: "require-and-verify-client-cert",
}
err := testClientTransport(t, httpAPI, clientTLSConfig, serverTLSConfig)
require.NoError(t, err)
}
func TestGRPCWithTLSClientAuthFailNoClientCertProvided(t *testing.T) {
testWithTLSClientAuthFailNoClientCertProvided(t, false)
}
func TestHTTPWithTLSClientAuthFailNoClientCertProvided(t *testing.T) {
testWithTLSClientAuthFailNoClientCertProvided(t, true)
}
func testWithTLSClientAuthFailNoClientCertProvided(t *testing.T, httpAPI bool) {
t.Helper()
// Clients certs required but not provided
clientTLSConfig := TLSConfig{
EnableTLS: true,
TrustedCertsPath: selfSignedClientCertPath,
}
serverTLSConfig := conf.TLSConfig{
Enabled: true,
KeyPath: selfSignedServerKeyPath,
CertPath: selfSignedServerCertPath,
ClientCertsPath: selfSignedClientCertPath,
ClientAuth: "require-and-verify-client-cert",
}
err := testClientTransport(t, httpAPI, clientTLSConfig, serverTLSConfig)
require.Error(t, err)
}
func TestGRPCWithTLSClientAuthFailUntrustedClientCertProvided(t *testing.T) {
testWithTLSClientAuthFailUntrustedClientCertProvided(t, false)
}
func TestHTTPWithTLSClientAuthFailUntrustedClientCertProvided(t *testing.T) {
testWithTLSClientAuthFailUntrustedClientCertProvided(t, true)
}
func testWithTLSClientAuthFailUntrustedClientCertProvided(t *testing.T, httpAPI bool) {
t.Helper()
// Clients certs required but untrusted one provided
clientTLSConfig := TLSConfig{
EnableTLS: true,
TrustedCertsPath: selfSignedServerCertPath,
CertPath: selfSignedClientCertPath2,
KeyPath: selfSignedClientKeyPath2,
}
serverTLSConfig := conf.TLSConfig{
Enabled: true,
KeyPath: selfSignedServerKeyPath,
CertPath: selfSignedServerCertPath,
ClientCertsPath: selfSignedClientCertPath,
ClientAuth: "require-and-verify-client-cert",
}
err := testClientTransport(t, httpAPI, clientTLSConfig, serverTLSConfig)
require.Error(t, err)
}
func testClientTransport(t *testing.T, httpAPI bool, clientTLSConfig TLSConfig, serverTLSConfig conf.TLSConfig) error {
t.Helper()
cfg := conf.NewTestConfig(1)
serverAddress := "localhost:6584"
if httpAPI {
cfg.HTTPAPIServerEnabled = true
cfg.GRPCAPIServerEnabled = false
cfg.HTTPAPIServerListenAddresses = []string{serverAddress}
cfg.HTTPAPIServerTLSConfig = serverTLSConfig
} else {
cfg.GRPCAPIServerEnabled = true
cfg.HTTPAPIServerEnabled = false
cfg.GRPCAPIServerListenAddresses = []string{serverAddress}
cfg.GRPCAPIServerTLSConfig = serverTLSConfig
}
s, err := server.NewServer(*cfg)
require.NoError(t, err)
err = s.Start()
require.NoError(t, err)
defer func() {
err = s.Stop()
require.NoError(t, err)
}()
var cli *Client
if httpAPI {
cli = NewClientUsingHTTP(serverAddress, clientTLSConfig)
} else {
cli = NewClientUsingGRPC(serverAddress, clientTLSConfig)
}
cli.SetExitOnError(false)
err = cli.Start()
if err != nil {
return err
}
log.Println("started client")
defer func() {
err = cli.Stop()
require.NoError(t, err)
}()
ch, err := cli.ExecuteStatement("use sys", nil, nil)
if err != nil {
return err
}
for range ch {
}
log.Println("about to execute select *")
ch, err = cli.ExecuteStatement("select * from sys.tables", nil, nil)
if err != nil {
log.Printf("select * returned err %v", err)
return err
}
var out strings.Builder
log.Println("about to print results")
for line := range ch {
log.Println(line)
out.WriteString(line)
out.WriteRune('\n')
}
log.Println("ranged through results")
actual := out.String()
if actual == expectedOutput {
return nil
}
return errors.New(actual)
}
var expectedOutput = "+-----------------------------------------------------------------------------------------------------------------+\n" +
"| id | kind | schema_n.. | name | table_info | topic_info | query | mv_name |\n" +
"+-----------------------------------------------------------------------------------------------------------------+\n" +
"0 rows returned\n"
func TestClientNotExistentKeyFile(t *testing.T) {
tlsConfig := TLSConfig{
EnableTLS: true,
KeyPath: "nothing/here/client.key",
CertPath: caSignedClientCertPath,
}
cl := NewClientUsingGRPC("localhost:6584", tlsConfig)
err := cl.Start()
require.Error(t, err)
require.Equal(t, "open nothing/here/client.key: no such file or directory", err.Error())
}
func TestClientNotExistentCertFile(t *testing.T) {
tlsConfig := TLSConfig{
EnableTLS: true,
KeyPath: caSignedClientKeyPath,
CertPath: "nothing/here/client.crt",
}
cl := NewClientUsingGRPC("localhost:6584", tlsConfig)
err := cl.Start()
require.Error(t, err)
require.Equal(t, "open nothing/here/client.crt: no such file or directory", err.Error())
}