2015-06-19 17:10:19 +00:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-07-01 19:18:11 +00:00
|
|
|
"time"
|
2015-06-19 17:10:19 +00:00
|
|
|
|
|
|
|
"github.com/gocql/gocql"
|
2021-07-16 00:17:31 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/tlsutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/certutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-06-19 17:10:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Query templates a query for us.
|
|
|
|
func substQuery(tpl string, data map[string]string) string {
|
|
|
|
for k, v := range data {
|
|
|
|
tpl = strings.Replace(tpl, fmt.Sprintf("{{%s}}", k), v, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tpl
|
|
|
|
}
|
|
|
|
|
2016-07-01 20:24:48 +00:00
|
|
|
func createSession(cfg *sessionConfig, s logical.Storage) (*gocql.Session, error) {
|
2015-06-19 17:10:19 +00:00
|
|
|
clusterConfig := gocql.NewCluster(strings.Split(cfg.Hosts, ",")...)
|
|
|
|
clusterConfig.Authenticator = gocql.PasswordAuthenticator{
|
|
|
|
Username: cfg.Username,
|
|
|
|
Password: cfg.Password,
|
|
|
|
}
|
|
|
|
|
2016-02-01 15:27:26 +00:00
|
|
|
clusterConfig.ProtoVersion = cfg.ProtocolVersion
|
|
|
|
if clusterConfig.ProtoVersion == 0 {
|
|
|
|
clusterConfig.ProtoVersion = 2
|
|
|
|
}
|
|
|
|
|
2016-07-01 20:24:48 +00:00
|
|
|
clusterConfig.Timeout = time.Duration(cfg.ConnectTimeout) * time.Second
|
2016-07-01 19:18:11 +00:00
|
|
|
|
2015-06-19 17:10:19 +00:00
|
|
|
if cfg.TLS {
|
2016-07-12 23:32:47 +00:00
|
|
|
var tlsConfig *tls.Config
|
2015-06-19 17:10:19 +00:00
|
|
|
if len(cfg.Certificate) > 0 || len(cfg.IssuingCA) > 0 {
|
|
|
|
if len(cfg.Certificate) > 0 && len(cfg.PrivateKey) == 0 {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("found certificate for TLS authentication but no private key")
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
certBundle := &certutil.CertBundle{}
|
|
|
|
if len(cfg.Certificate) > 0 {
|
|
|
|
certBundle.Certificate = cfg.Certificate
|
|
|
|
certBundle.PrivateKey = cfg.PrivateKey
|
|
|
|
}
|
|
|
|
if len(cfg.IssuingCA) > 0 {
|
|
|
|
certBundle.IssuingCA = cfg.IssuingCA
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedCertBundle, err := certBundle.ToParsedCertBundle()
|
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse certificate bundle: %w", err)
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig, err = parsedCertBundle.GetTLSConfig(certutil.TLSClient)
|
2016-07-13 15:52:26 +00:00
|
|
|
if err != nil || tlsConfig == nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to get TLS configuration: tlsConfig: %#v; %w", tlsConfig, err)
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
2016-07-12 23:32:47 +00:00
|
|
|
tlsConfig.InsecureSkipVerify = cfg.InsecureTLS
|
2016-07-13 15:52:26 +00:00
|
|
|
|
2016-07-13 16:32:31 +00:00
|
|
|
if cfg.TLSMinVersion != "" {
|
|
|
|
var ok bool
|
|
|
|
tlsConfig.MinVersion, ok = tlsutil.TLSLookup[cfg.TLSMinVersion]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid 'tls_min_version' in config")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// MinVersion was not being set earlier. Reset it to
|
|
|
|
// zero to gracefully handle upgrades.
|
|
|
|
tlsConfig.MinVersion = 0
|
2016-07-13 15:52:26 +00:00
|
|
|
}
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clusterConfig.SslOpts = &gocql.SslOptions{
|
2017-04-17 15:51:42 +00:00
|
|
|
Config: tlsConfig,
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := clusterConfig.CreateSession()
|
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("error creating session: %w", err)
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the info
|
|
|
|
err = session.Query(`LIST USERS`).Exec()
|
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("error validating connection info: %w", err)
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
}
|