Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Add password authentication for Cassandra (#249)
Browse files Browse the repository at this point in the history
* Add password authentication for Cassandra
  • Loading branch information
kobeyang authored Jul 19, 2017
1 parent 901e092 commit c6f3b6f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ install:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
- go get github.com/golang/lint/golint
- ccm create test -v 2.2.8 -n 1 -s
- ccm create test -v 2.2.8 -n 1 -s --pwd-auth
- sudo ln -sf /home/travis/.local/bin/cqlsh /usr/local/bin/cqlsh
- wget https://github.com/uber/cherami-server/releases/download/rocksdb-5.0.2-trusty/librocksdb.so.5.0.2
- ln -s librocksdb.so.5.0.2 librocksdb.so.5.0
Expand Down
8 changes: 8 additions & 0 deletions clients/metadata/metadata_cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ func NewCassandraMetadataService(cfg configure.CommonMetadataConfig) (*Cassandra
cluster.Keyspace = cfg.GetKeyspace()
cluster.ProtoVersion = cassandraProtoVersion

auth := cfg.GetAuthentication()
if auth.Enabled {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: auth.Username,
Password: auth.Password,
}
}

cms := new(CassandraMetadataService)
cms.lowConsLevel = gocql.Two
cms.midConsLevel = gocql.Two
Expand Down
32 changes: 30 additions & 2 deletions clients/metadata/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ func LoadSchema(cqlshpath string, fileName string, keyspace string) (err error)
// Using cqlsh as I couldn't find a way to execute multiple commands through gocql.Session
var out bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command(cqlshpath, fmt.Sprintf("--keyspace=%v", keyspace), fmt.Sprintf("--file=%v", fileName), `127.0.0.1`)
cmd := exec.Command(cqlshpath,
"--username=cassandra",
"--password=cassandra",
fmt.Sprintf("--keyspace=%v", keyspace),
fmt.Sprintf("--file=%v", fileName),
`127.0.0.1`)
cmd.Stdout = &out
cmd.Stderr = &stderr
err = cmd.Run()
Expand All @@ -97,14 +102,26 @@ func newCluster(clusterHosts string) *gocql.ClusterConfig {
}

// CreateKeyspaceNoSession is used to create a keyspace when we don't have a session
func CreateKeyspaceNoSession(clusterHosts string, keyspace string, replicas int, overwrite bool) error {
func CreateKeyspaceNoSession(
clusterHosts string,
keyspace string,
replicas int,
overwrite bool,
auth configure.Authentication,
) error {
// open a session to the "system" keyspace just to create the new keyspace
// TODO: Find out if we can do this "outside" of a session (cqlsh?)
cluster := newCluster(clusterHosts)
cluster.Consistency = gocql.One
cluster.Keyspace = "system"
cluster.Timeout = 40 * time.Second
cluster.ProtoVersion = cassandraProtoVersion
if auth.Enabled {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: auth.Username,
Password: auth.Password,
}
}
session, err := cluster.CreateSession()
if err != nil {
log.WithField(common.TagErr, err).Error(`CreateKeyspaceNoSession: unable to create session`)
Expand Down Expand Up @@ -146,11 +163,18 @@ func (s *TestCluster) SetupTestCluster() {
s.createKeyspace(1)
s.loadSchema("schema/metadata.cql")

auth := configure.Authentication{
Enabled: true,
Username: "cassandra",
Password: "cassandra",
}

var err error
s.client, err = NewCassandraMetadataService(&configure.MetadataConfig{
CassandraHosts: ip,
Keyspace: s.keyspace,
Consistency: "One",
Authentication: auth,
})
if err != nil {
log.Fatal(err)
Expand All @@ -169,6 +193,10 @@ func (s *TestCluster) createCluster(clusterHosts string, cons gocql.Consistency,
s.cluster.Keyspace = "system"
s.cluster.Timeout = 40 * time.Second
s.cluster.ProtoVersion = cassandraProtoVersion
s.cluster.Authenticator = gocql.PasswordAuthenticator{
Username: "cassandra",
Password: "cassandra",
}
var err error
s.session, err = s.cluster.CreateSession()
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions common/configure/commonmetadataconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@

package configure

// Authentication holds the authentication info to our metadata
type Authentication struct {
Enabled bool `yaml:"Enabled"`
Username string `yaml:"Username"`
Password string `yaml:"Password"`
}

// MetadataConfig holds the config info related to our metadata
type MetadataConfig struct {
CassandraHosts string `yaml:"CassandraHosts"`
Keyspace string `yaml:"Keyspace"`
Authentication Authentication `yaml:"Authentication"`
Consistency string `yaml:"Consistency"`
ClusterName string `yaml:"ClusterName"`
NumConns int `yaml:"NumConns"`
Expand All @@ -47,6 +55,11 @@ func (r *MetadataConfig) GetKeyspace() string {
return r.Keyspace
}

// GetAuthentication returns the authentication info to be used for cherami cluster
func (r *MetadataConfig) GetAuthentication() Authentication {
return r.Authentication
}

// GetConsistency returns the consistency level to be used for cherami cluster
func (r *MetadataConfig) GetConsistency() string {
return r.Consistency
Expand Down
2 changes: 2 additions & 0 deletions common/configure/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ type (
GetCassandraHosts() string
// GetKeyspace returns the keyspace for our cassandra cluster
GetKeyspace() string
// GetAuthentication returns the authentication info for our cassandra cluster
GetAuthentication() Authentication
// GetConsistency returns the configured consistency level
GetConsistency() string
// GetDcFilter returns the dc filter map for the cassandra cluster
Expand Down
4 changes: 4 additions & 0 deletions config/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ MetadataConfig:
Consistency: "one"
ClusterName: "base"
NumConns: 1
Authentication:
Enabled: true
Username: cassandra
Password: cassandra

# ReplicatorConfig specifies
ReplicatorConfig:
Expand Down
6 changes: 5 additions & 1 deletion config/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ DefaultDestinationConfig:

MetadataConfig:
CassandraHosts: "127.0.0.1"
Authentication:
Enabled: false
Username:
Password:

StorageConfig:
BaseDir: /tmp/cherami-store
HostUUID: "11111111-1111-1111-1111-111111111111"

logging:
level: debug
stdout: true
stdout: true
9 changes: 8 additions & 1 deletion test/integration/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,21 @@ func (tb *testBase) setupSuiteImpl(t *testing.T) {
tb.keyspace = "integration_test"
tb.Assertions = require.New(tb.T())

auth := configure.Authentication{
Enabled: true,
Username: "cassandra",
Password: "cassandra",
}

// create the keyspace first
err := metadata.CreateKeyspaceNoSession("127.0.0.1", tb.keyspace, 1, true)
err := metadata.CreateKeyspaceNoSession("127.0.0.1", tb.keyspace, 1, true, auth)
tb.NoError(err)

tb.mClient, _ = metadata.NewCassandraMetadataService(&configure.MetadataConfig{
CassandraHosts: "127.0.0.1",
Keyspace: tb.keyspace,
Consistency: "One",
Authentication: auth,
})
tb.NotNil(tb.mClient)

Expand Down

0 comments on commit c6f3b6f

Please sign in to comment.