forked from vesoft-inc/nebula-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssl_connection_test.go
167 lines (145 loc) · 4.26 KB
/
ssl_connection_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
//go:build integration
// +build integration
/*
*
* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*
*/
package nebula_go
import (
"os"
"testing"
"time"
)
func TestSslConnection(t *testing.T) {
// skip test when ssl_test is not set to true
skipSsl(t)
hostAdress := HostAddress{Host: address, Port: port}
hostList := []HostAddress{}
hostList = append(hostList, hostAdress)
testPoolConfig = PoolConfig{
TimeOut: 0 * time.Millisecond,
IdleTime: 0 * time.Millisecond,
MaxConnPoolSize: 10,
MinConnPoolSize: 1,
}
sslConfig, err := GetDefaultSSLConfig(
"./nebula-docker-compose/secrets/test.ca.pem",
"./nebula-docker-compose/secrets/test.client.crt",
"./nebula-docker-compose/secrets/test.client.key",
)
if err != nil {
t.Fatal(err)
}
sslConfig.InsecureSkipVerify = true // This is only used for testing
// Initialize connection pool
pool, err := NewSslConnectionPool(hostList, testPoolConfig, sslConfig, nebulaLog)
if err != nil {
t.Fatalf("fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error())
}
// Close all connections in the pool
defer pool.Close()
// Create session
session, err := pool.GetSession(username, password)
if err != nil {
t.Fatalf("fail to create a new session from connection pool, username: %s, password: %s, %s",
username, password, err.Error())
}
defer session.Release()
// Excute a query
resp, err := tryToExecute(session, "SHOW HOSTS;")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResultSet(t, "show hosts", resp)
// Create a new space
resp, err = tryToExecute(session, "CREATE SPACE client_test(partition_num=1024, replica_factor=1, vid_type = FIXED_STRING(30));")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResultSet(t, "create space", resp)
resp, err = tryToExecute(session, "DROP SPACE client_test;")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResultSet(t, "drop space", resp)
}
// TODO: generate certificate with hostName info and disable InsecureSkipVerify
func TestSslConnectionSelfSigned(t *testing.T) {
// skip test when ssl_test is not set to true
skipSslSelfSigned(t)
hostAdress := HostAddress{Host: address, Port: port}
hostList := []HostAddress{}
hostList = append(hostList, hostAdress)
testPoolConfig = PoolConfig{
TimeOut: 0 * time.Millisecond,
IdleTime: 0 * time.Millisecond,
MaxConnPoolSize: 10,
MinConnPoolSize: 1,
}
sslConfig, err := GetDefaultSSLConfig(
"./nebula-docker-compose/secrets/test.self-signed.pem",
"./nebula-docker-compose/secrets/test.self-signed.pem",
"./nebula-docker-compose/secrets/test.self-signed.key",
)
if err != nil {
t.Fatal(err)
}
sslConfig.InsecureSkipVerify = true // This is only used for testing
// Initialize connection pool
pool, err := NewSslConnectionPool(hostList, testPoolConfig, sslConfig, nebulaLog)
if err != nil {
t.Fatalf("fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error())
}
// Close all connections in the pool
defer pool.Close()
// Create session
session, err := pool.GetSession(username, password)
if err != nil {
t.Fatalf("fail to create a new session from connection pool, username: %s, password: %s, %s",
username, password, err.Error())
}
defer session.Release()
// Excute a query
resp, err := tryToExecute(session, "SHOW HOSTS;")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResultSet(t, "show hosts", resp)
// Create a new space
resp, err = tryToExecute(session, "CREATE SPACE client_test(partition_num=1024, replica_factor=1, vid_type = FIXED_STRING(30));")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResultSet(t, "create space", resp)
resp, err = tryToExecute(session, "DROP SPACE client_test;")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResultSet(t, "drop space", resp)
}
func openAndReadFileTest(t *testing.T, path string) []byte {
b, err := openAndReadFile(path)
if err != nil {
t.Fatal(err)
}
return b
}
func skipSsl(t *testing.T) {
if os.Getenv("ssl_test") != "true" {
t.Skip("Skipping SSL testing in CI environment")
}
}
func skipSslSelfSigned(t *testing.T) {
if os.Getenv("self_signed") != "true" {
t.Skip("Skipping SSL testing in CI environment")
}
}