Skip to content

Commit 1e5eeee

Browse files
authored
Merge pull request #48 from zegl/riak-security
#47 - Added support for password based authentication
2 parents ccac7f1 + e5a9184 commit 1e5eeee

File tree

1 file changed

+81
-3
lines changed

1 file changed

+81
-3
lines changed

client.go

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ package goriak
22

33
import (
44
riak "github.com/basho/riak-go-client"
5+
6+
"crypto/tls"
7+
"crypto/x509"
8+
"errors"
9+
"io/ioutil"
10+
"strconv"
11+
"strings"
512
)
613

714
// Session holds the connection to Riak
815
type Session struct {
9-
riak *riak.Client
16+
riak *riak.Cluster
1017
opts ConnectOpts
1118
}
1219

@@ -15,6 +22,16 @@ type ConnectOpts struct {
1522
// Both Address and Addresses should be on the form HOST|IP[:PORT]
1623
Address string // Address to a single Riak host. Will be used in case Addresses is empty
1724
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
1835
}
1936

2037
// Connect creates a new Riak connection. See ConnectOpts for the available options.
@@ -37,10 +54,71 @@ func (c *Session) connect() error {
3754
c.opts.Addresses = []string{c.opts.Address}
3855
}
3956

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,
42116
})
117+
if err != nil {
118+
return err
119+
}
43120

121+
err = con.Start()
44122
if err != nil {
45123
return err
46124
}

0 commit comments

Comments
 (0)