Skip to content

Commit

Permalink
Enable TLS client connections to Cassandra
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Collins <robertc@vmware.com>
  • Loading branch information
Robert Collins committed Nov 24, 2017
1 parent 3136216 commit e0b79be
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cmd/flags/cassandra/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const (
suffixSocketKeepAlive = ".socket-keep-alive"
suffixUsername = ".username"
suffixPassword = ".password"
suffixTLS = ".tls"
suffixCert = ".cert"
suffixKey = ".key"
suffixCA = ".ca"
suffixServerName = ".servername"
suffixVerifyHost = ".verify-host"
)

// TODO this should be moved next to config.Configuration struct (maybe ./flags package)
Expand Down Expand Up @@ -62,6 +68,10 @@ func NewOptions(primaryNamespace string, otherNamespaces ...string) *Options {
options := &Options{
primary: &namespaceConfig{
Configuration: config.Configuration{
TLS: config.TLS{
Enabled: false,
EnableHostVerification: true,
},
MaxRetryAttempts: 3,
Keyspace: "jaeger_v1_local",
ProtoVersion: 4,
Expand Down Expand Up @@ -129,6 +139,30 @@ func addFlags(flagSet *flag.FlagSet, nsConfig *namespaceConfig) {
nsConfig.namespace+suffixPassword,
nsConfig.Authenticator.Basic.Password,
"Password for password authentication for Cassandra")
flagSet.Bool(
nsConfig.namespace+suffixTLS,
nsConfig.TLS.Enabled,
"Enable TLS")
flagSet.String(
nsConfig.namespace+suffixCert,
nsConfig.TLS.CertPath,
"Path to TLS certificate file")
flagSet.String(
nsConfig.namespace+suffixKey,
nsConfig.TLS.KeyPath,
"Path to TLS key file")
flagSet.String(
nsConfig.namespace+suffixCA,
nsConfig.TLS.CaPath,
"Path to TLS CA file")
flagSet.String(
nsConfig.namespace+suffixServerName,
nsConfig.TLS.ServerName,
"Override the TLS server name")
flagSet.Bool(
nsConfig.namespace+suffixVerifyHost,
nsConfig.TLS.EnableHostVerification,
"Enable (or disable) host key verification")
}

// InitFromViper initializes Options with properties from viper
Expand All @@ -150,6 +184,12 @@ func initFromViper(cfg *namespaceConfig, v *viper.Viper) {
cfg.SocketKeepAlive = v.GetDuration(cfg.namespace + suffixSocketKeepAlive)
cfg.Authenticator.Basic.Username = v.GetString(cfg.namespace + suffixUsername)
cfg.Authenticator.Basic.Password = v.GetString(cfg.namespace + suffixPassword)
cfg.TLS.Enabled = v.GetBool(cfg.namespace + suffixTLS)
cfg.TLS.CertPath = v.GetString(cfg.namespace + suffixCert)
cfg.TLS.KeyPath = v.GetString(cfg.namespace + suffixKey)
cfg.TLS.CaPath = v.GetString(cfg.namespace + suffixCA)
cfg.TLS.ServerName = v.GetString(cfg.namespace + suffixServerName)
cfg.TLS.EnableHostVerification = v.GetBool(cfg.namespace + suffixVerifyHost)
}

// GetPrimary returns primary configuration.
Expand Down
23 changes: 23 additions & 0 deletions pkg/cassandra/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package config

import (
"crypto/tls"
"fmt"
"time"

Expand All @@ -36,6 +37,7 @@ type Configuration struct {
Consistency string `yaml:"consistency"`
Port int `yaml:"port"`
Authenticator Authenticator `yaml:"authenticator"`
TLS TLS
}

// Authenticator holds the authentication properties needed to connect to a Cassandra cluster
Expand All @@ -50,6 +52,16 @@ type BasicAuthenticator struct {
Password string `yaml:"password"`
}

// TLS Config
type TLS struct {
Enabled bool
ServerName string
CertPath string
KeyPath string
CaPath string
EnableHostVerification bool
}

// ApplyDefaults copies settings from source unless its own value is non-zero.
func (c *Configuration) ApplyDefaults(source *Configuration) {
if c.ConnectionsPerHost == 0 {
Expand Down Expand Up @@ -119,6 +131,17 @@ func (c *Configuration) NewCluster() *gocql.ClusterConfig {
Password: c.Authenticator.Basic.Password,
}
}
if c.TLS.Enabled {
cluster.SslOpts = &gocql.SslOptions{
Config: tls.Config{
ServerName: c.TLS.ServerName,
},
CertPath: c.TLS.CertPath,
KeyPath: c.TLS.KeyPath,
CaPath: c.TLS.CaPath,
EnableHostVerification: c.TLS.EnableHostVerification,
}
}
return cluster
}

Expand Down

0 comments on commit e0b79be

Please sign in to comment.