This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaerospike_suite_test.go
283 lines (230 loc) · 7.3 KB
/
aerospike_suite_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
// Copyright 2014-2021 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aerospike_test
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"strings"
"testing"
"time"
as "github.com/aerospike/aerospike-client-go"
asl "github.com/aerospike/aerospike-client-go/logger"
ast "github.com/aerospike/aerospike-client-go/types"
gg "github.com/onsi/ginkgo"
gm "github.com/onsi/gomega"
)
var (
host = flag.String("h", "127.0.0.1", "Aerospike server seed hostnames or IP addresses")
port = flag.Int("p", 3000, "Aerospike server seed hostname or IP address port number.")
user = flag.String("U", "", "Username.")
password = flag.String("P", "", "Password.")
authMode = flag.String("A", "internal", "Authentication mode: internal | external")
useReplicas = flag.Bool("use-replicas", false, "Aerospike will use replicas as well as master partitions.")
debug = flag.Bool("debug", false, "Will set the logging level to DEBUG.")
namespace = flag.String("n", "test", "Namespace")
certFile = flag.String("cert_file", "", "Certificate file name.")
keyFile = flag.String("key_file", "", "Key file name.")
keyFilePassphrase = flag.String("key_file_passphrase", "", "Key file's pass phrase.")
nodeTLSName = flag.String("node_tls_name", "", "Node's TLS name.")
rootCA = flag.String("root_ca", "", "Root certificate.")
tlsConfig *tls.Config
clientPolicy *as.ClientPolicy
client *as.Client
)
func initTestVars() {
var buf bytes.Buffer
logger := log.New(&buf, "", log.LstdFlags|log.Lshortfile)
logger.SetOutput(os.Stdout)
asl.Logger.SetLogger(logger)
if *debug {
asl.Logger.SetLevel(asl.DEBUG)
}
clientPolicy = as.NewClientPolicy()
if *user != "" {
clientPolicy.User = *user
clientPolicy.Password = *password
}
*authMode = strings.ToLower(strings.TrimSpace(*authMode))
if *authMode != "internal" && *authMode != "external" {
log.Fatalln("Invalid auth mode: only `internal` and `external` values are accepted.")
}
if *authMode == "external" {
clientPolicy.AuthMode = as.AuthModeExternal
}
// setup TLS
tlsConfig = initTLS()
clientPolicy.TlsConfig = tlsConfig
dbHost := as.NewHost(*host, *port)
dbHost.TLSName = *nodeTLSName
client, err = as.NewClientWithPolicyAndHost(clientPolicy, dbHost)
if err != nil {
log.Fatal(err.Error())
}
// set default policies
if *useReplicas {
client.DefaultPolicy.ReplicaPolicy = as.MASTER_PROLES
}
}
func TestMain(m *testing.M) {
rand.Seed(time.Now().UnixNano())
flag.Parse()
// setup the client object
initTestVars()
os.Exit(m.Run())
}
func TestAerospike(t *testing.T) {
// TestMain will be called here, no need to do more
gm.RegisterFailHandler(gg.Fail)
gg.RunSpecs(t, "Aerospike Client Library Suite")
}
func featureEnabled(feature string) bool {
node := client.GetNodes()[0]
infoMap, err := node.RequestInfo(as.NewInfoPolicy(), "features")
if err != nil {
log.Fatal("Failed to connect to aerospike: err:", err)
}
return strings.Contains(infoMap["features"], feature)
}
func isEnterpriseEdition() bool {
node := client.GetNodes()[0]
infoMap, err := node.RequestInfo(as.NewInfoPolicy(), "edition")
if err != nil {
log.Fatal("Failed to connect to aerospike: err:", err)
}
return strings.Contains(infoMap["edition"], "Enterprise")
}
func securityEnabled() bool {
if !isEnterpriseEdition() {
return false
}
_, err := client.QueryRoles(nil)
return err == nil
}
func xdrEnabled() bool {
res := info(client, "get-config:context=xdr")
return len(res) > 0 && !strings.HasPrefix(res, "ERROR")
}
func nsInfo(ns string, feature string) string {
node := client.GetNodes()[0]
infoMap, err := node.RequestInfo(as.NewInfoPolicy(), "namespace/"+ns)
if err != nil {
log.Fatal("Failed to connect to aerospike: err:", err)
}
infoStr := infoMap["namespace/"+ns]
infoPairs := strings.Split(infoStr, ";")
for _, pairs := range infoPairs {
pair := strings.Split(pairs, "=")
if pair[0] == feature {
return pair[1]
}
}
return ""
}
func info(client *as.Client, feature string) string {
node := client.GetNodes()[0]
infoMap, err := node.RequestInfo(as.NewInfoPolicy(), feature)
if err != nil {
if ae, ok := err.(ast.AerospikeError); ok {
return ae.Error()
} else {
log.Fatal("Failed to connect to aerospike: err:", err)
}
}
return infoMap[feature]
}
func initTLS() *tls.Config {
if len(*rootCA) == 0 && len(*certFile) == 0 && len(*keyFile) == 0 {
return nil
}
// Try to load system CA certs, otherwise just make an empty pool
serverPool, err := x509.SystemCertPool()
if serverPool == nil || err != nil {
log.Printf("Adding system certificates to the cert pool failed: %s", err)
serverPool = x509.NewCertPool()
}
if len(*rootCA) > 0 {
// Try to load system CA certs and add them to the system cert pool
caCert, err := readFromFile(*rootCA)
if err != nil {
log.Fatal(err)
}
log.Printf("Adding CA certificate to the pool...")
serverPool.AppendCertsFromPEM(caCert)
}
var clientPool []tls.Certificate
if len(*certFile) > 0 || len(*keyFile) > 0 {
// Read cert file
certFileBytes, err := readFromFile(*certFile)
if err != nil {
log.Fatal(err)
}
// Read key file
keyFileBytes, err := readFromFile(*keyFile)
if err != nil {
log.Fatal(err)
}
// Decode PEM data
keyBlock, _ := pem.Decode(keyFileBytes)
certBlock, _ := pem.Decode(certFileBytes)
if keyBlock == nil || certBlock == nil {
log.Fatalf("Failed to decode PEM data for key or certificate")
}
// Check and Decrypt the the Key Block using passphrase
if x509.IsEncryptedPEMBlock(keyBlock) {
decryptedDERBytes, err := x509.DecryptPEMBlock(keyBlock, []byte(*keyFilePassphrase))
if err != nil {
log.Fatalf("Failed to decrypt PEM Block: `%s`", err)
}
keyBlock.Bytes = decryptedDERBytes
keyBlock.Headers = nil
}
// Encode PEM data
keyPEM := pem.EncodeToMemory(keyBlock)
certPEM := pem.EncodeToMemory(certBlock)
if keyPEM == nil || certPEM == nil {
log.Fatalf("Failed to encode PEM data for key or certificate")
}
cert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
log.Fatalf("Failed to add client certificate and key to the pool: `%s`", err)
}
log.Printf("Adding client certificate and key to the pool...")
clientPool = append(clientPool, cert)
}
tlsConfig := &tls.Config{
Certificates: clientPool,
RootCAs: serverPool,
InsecureSkipVerify: false,
PreferServerCipherSuites: true,
}
tlsConfig.BuildNameToCertificate()
return tlsConfig
}
// Read content from file
func readFromFile(filePath string) ([]byte, error) {
dataBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("Failed to read from file `%s`: `%v`", filePath, err)
}
data := bytes.TrimSuffix(dataBytes, []byte("\n"))
return data, nil
}