@@ -2,11 +2,18 @@ package goriak
2
2
3
3
import (
4
4
riak "github.com/basho/riak-go-client"
5
+
6
+ "crypto/tls"
7
+ "crypto/x509"
8
+ "errors"
9
+ "io/ioutil"
10
+ "strconv"
11
+ "strings"
5
12
)
6
13
7
14
// Session holds the connection to Riak
8
15
type Session struct {
9
- riak * riak.Client
16
+ riak * riak.Cluster
10
17
opts ConnectOpts
11
18
}
12
19
@@ -15,6 +22,16 @@ type ConnectOpts struct {
15
22
// Both Address and Addresses should be on the form HOST|IP[:PORT]
16
23
Address string // Address to a single Riak host. Will be used in case Addresses is empty
17
24
Addresses []string // Addresses to all Riak hosts.
25
+
26
+ // Username and password for connection to servers with secirity enabled
27
+ User string
28
+ Password string
29
+
30
+ // Path to root CA certificate. Required if security is used
31
+ CARootCert string
32
+
33
+ // Option to override port. Is set to 8087 by default
34
+ Port uint32
18
35
}
19
36
20
37
// Connect creates a new Riak connection. See ConnectOpts for the available options.
@@ -37,10 +54,71 @@ func (c *Session) connect() error {
37
54
c .opts .Addresses = []string {c .opts .Address }
38
55
}
39
56
40
- con , err := riak .NewClient (& riak.NewClientOptions {
41
- RemoteAddresses : c .opts .Addresses ,
57
+ var authOptions * riak.AuthOptions
58
+
59
+ // Build auth options
60
+ if c .opts .User != "" {
61
+ rootCertPemData , err := ioutil .ReadFile (c .opts .CARootCert )
62
+ if err != nil {
63
+ return errors .New ("Opening CARootCert: " + err .Error ())
64
+ }
65
+
66
+ rootCertPool := x509 .NewCertPool ()
67
+ if ! rootCertPool .AppendCertsFromPEM (rootCertPemData ) {
68
+ return errors .New ("Invalid PEM certificate file" )
69
+ }
70
+
71
+ tlsConf := & tls.Config {
72
+ InsecureSkipVerify : true ,
73
+ RootCAs : rootCertPool ,
74
+ }
75
+
76
+ authOptions = & riak.AuthOptions {
77
+ User : c .opts .User ,
78
+ Password : c .opts .Password ,
79
+ TlsConfig : tlsConf ,
80
+ }
81
+ }
82
+
83
+ var nodes []* riak.Node
84
+
85
+ // Set to default port if not provided
86
+ port := c .opts .Port
87
+ if port == 0 {
88
+ port = 8087
89
+ }
90
+
91
+ for _ , address := range c .opts .Addresses {
92
+ if ! strings .Contains (address , ":" ) {
93
+ // Add port if not set in the user config
94
+ address = address + ":" + strconv .FormatUint (uint64 (port ), 10 )
95
+ }
96
+
97
+ // Set ServerName based on the address we're connecting to
98
+ if authOptions != nil {
99
+ addressWithoutPort := address [0 :strings .Index (address , ":" )]
100
+ authOptions .TlsConfig .ServerName = addressWithoutPort
101
+ }
102
+
103
+ node , err := riak .NewNode (& riak.NodeOptions {
104
+ RemoteAddress : address ,
105
+ AuthOptions : authOptions ,
106
+ })
107
+ if err != nil {
108
+ return err
109
+ }
110
+
111
+ nodes = append (nodes , node )
112
+ }
113
+
114
+ con , err := riak .NewCluster (& riak.ClusterOptions {
115
+ Nodes : nodes ,
42
116
})
117
+ if err != nil {
118
+ return err
119
+ }
43
120
121
+ err = con .Start ()
44
122
if err != nil {
45
123
return err
46
124
}
0 commit comments