Skip to content

Commit 17ab1a2

Browse files
authored
add cassandra tool support for user and password connection to cluster (#298)
* add cassandra tool support for user and password connection to cluster
1 parent 5c4df61 commit 17ab1a2

File tree

9 files changed

+70
-16
lines changed

9 files changed

+70
-16
lines changed

tools/cassandra/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ type (
3131
// with cassandra
3232
BaseConfig struct {
3333
CassHosts string
34+
CassPort int
35+
CassUser string
36+
CassPassword string
3437
CassKeyspace string
3538
}
3639

@@ -70,6 +73,9 @@ type (
7073

7174
const (
7275
cliOptEndpoint = "endpoint"
76+
cliOptPort = "port"
77+
cliOptUser = "user"
78+
cliOptPassword = "password"
7379
cliOptKeyspace = "keyspace"
7480
cliOptVersion = "version"
7581
cliOptSchemaFile = "schema-file"
@@ -82,6 +88,9 @@ const (
8288
cliOptQuiet = "quiet"
8389

8490
cliFlagEndpoint = cliOptEndpoint + ", ep"
91+
cliFlagPort = cliOptPort + ", p"
92+
cliFlagUser = cliOptUser + ", u"
93+
cliFlagPassword = cliOptPassword + ", pw"
8594
cliFlagKeyspace = cliOptKeyspace + ", k"
8695
cliFlagVersion = cliOptVersion + ", v"
8796
cliFlagSchemaFile = cliOptSchemaFile + ", f"

tools/cassandra/cqlclient.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ var errNoHosts = errors.New("Cassandra hosts list is empty or malformed")
7373
var errGetSchemaVersion = errors.New("Failed to get current schema version from cassandra")
7474

7575
const (
76-
newLineDelim = '\n'
77-
defaultTimeout = 30 * time.Second
78-
cqlProtoVersion = 4 // default CQL protocol version
79-
defaultConsistency = "QUORUM" // schema updates must always be QUORUM
76+
newLineDelim = '\n'
77+
defaultTimeout = 30 * time.Second
78+
cqlProtoVersion = 4 // default CQL protocol version
79+
defaultConsistency = "QUORUM" // schema updates must always be QUORUM
80+
defaultCassandraPort = 9042
8081
)
8182

8283
const (
@@ -106,12 +107,19 @@ const (
106107
)
107108

108109
// newCQLClient returns a new instance of CQLClient
109-
func newCQLClient(hostsCsv string, keyspace string) (CQLClient, error) {
110+
func newCQLClient(hostsCsv string, port int, user, password, keyspace string) (CQLClient, error) {
110111
hosts := parseHosts(hostsCsv)
111112
if len(hosts) == 0 {
112113
return nil, errNoHosts
113114
}
114115
clusterCfg := gocql.NewCluster(hosts...)
116+
clusterCfg.Port = port
117+
if user != "" && password != "" {
118+
clusterCfg.Authenticator = gocql.PasswordAuthenticator{
119+
Username: user,
120+
Password: password,
121+
}
122+
}
115123
clusterCfg.Keyspace = keyspace
116124
clusterCfg.Timeout = defaultTimeout
117125
clusterCfg.ProtoVersion = cqlProtoVersion

tools/cassandra/cqlclient_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (s *CQLClientTestSuite) SetupSuite() {
5959
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
6060
s.keyspace = fmt.Sprintf("cql_client_test_%v", rand.Int63())
6161

62-
client, err := newCQLClient("127.0.0.1", "system")
62+
client, err := newCQLClient("127.0.0.1", defaultCassandraPort, "", "", "system")
6363
if err != nil {
6464
log.Fatalf("error creating CQLClient, err=%v", err)
6565
}
@@ -191,7 +191,7 @@ func (s *CQLClientTestSuite) testCreate(client CQLClient) {
191191
}
192192

193193
func (s *CQLClientTestSuite) TestCQLClient() {
194-
client, err := newCQLClient("127.0.0.1", s.keyspace)
194+
client, err := newCQLClient("127.0.0.1", defaultCassandraPort, "", "", s.keyspace)
195195
s.Nil(err)
196196
s.testCreate(client)
197197
s.testUpdate(client)

tools/cassandra/handler.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func createKeyspace(cli *cli.Context) error {
5959
if err != nil {
6060
return handleErr(err)
6161
}
62-
client, err := newCQLClient(config.CassHosts, "system")
62+
client, err := newCQLClient(config.CassHosts, config.CassPort, config.CassUser, config.CassPassword, "system")
6363
if err != nil {
6464
return handleErr(fmt.Errorf("error creating cql client:%v", err))
6565
}
@@ -96,6 +96,9 @@ func validateSetupSchemaConfig(config *SetupSchemaConfig) error {
9696
if len(config.CassHosts) == 0 {
9797
return newConfigError("missing cassandra endpoint argument " + flag(cliOptEndpoint))
9898
}
99+
if config.CassPort == 0 {
100+
config.CassPort = defaultCassandraPort
101+
}
99102
if len(config.CassKeyspace) == 0 {
100103
return newConfigError("missing " + flag(cliOptKeyspace) + " argument ")
101104
}
@@ -121,6 +124,9 @@ func newSetupSchemaConfig(cli *cli.Context) (*SetupSchemaConfig, error) {
121124

122125
config := new(SetupSchemaConfig)
123126
config.CassHosts = cli.GlobalString(cliOptEndpoint)
127+
config.CassPort = cli.GlobalInt(cliOptPort)
128+
config.CassUser = cli.GlobalString(cliOptUser)
129+
config.CassPassword = cli.GlobalString(cliOptPassword)
124130
config.CassKeyspace = cli.GlobalString(cliOptKeyspace)
125131
config.SchemaFilePath = cli.String(cliOptSchemaFile)
126132
config.InitialVersion = cli.String(cliOptVersion)
@@ -139,6 +145,9 @@ func validateUpdateSchemaConfig(config *UpdateSchemaConfig) error {
139145
if len(config.CassHosts) == 0 {
140146
return newConfigError("missing cassandra endpoint argument " + flag(cliOptEndpoint))
141147
}
148+
if config.CassPort == 0 {
149+
config.CassPort = defaultCassandraPort
150+
}
142151
if len(config.CassKeyspace) == 0 {
143152
return newConfigError("missing " + flag(cliOptKeyspace) + " argument ")
144153
}
@@ -159,6 +168,9 @@ func newUpdateSchemaConfig(cli *cli.Context) (*UpdateSchemaConfig, error) {
159168

160169
config := new(UpdateSchemaConfig)
161170
config.CassHosts = cli.GlobalString(cliOptEndpoint)
171+
config.CassPort = cli.GlobalInt(cliOptPort)
172+
config.CassUser = cli.GlobalString(cliOptUser)
173+
config.CassPassword = cli.GlobalString(cliOptPassword)
162174
config.CassKeyspace = cli.GlobalString(cliOptKeyspace)
163175
config.SchemaDir = cli.String(cliOptSchemaDir)
164176
config.IsDryRun = cli.Bool(cliOptDryrun)
@@ -174,6 +186,9 @@ func newUpdateSchemaConfig(cli *cli.Context) (*UpdateSchemaConfig, error) {
174186
func newCreateKeyspaceConfig(cli *cli.Context) (*CreateKeyspaceConfig, error) {
175187
config := new(CreateKeyspaceConfig)
176188
config.CassHosts = cli.GlobalString(cliOptEndpoint)
189+
config.CassPort = cli.GlobalInt(cliOptPort)
190+
config.CassUser = cli.GlobalString(cliOptUser)
191+
config.CassPassword = cli.GlobalString(cliOptPassword)
177192
config.CassKeyspace = cli.String(cliOptKeyspace)
178193
config.ReplicationFactor = cli.Int(cliOptReplicationFactor)
179194

@@ -187,6 +202,9 @@ func validateCreateKeyspaceConfig(config *CreateKeyspaceConfig) error {
187202
if len(config.CassHosts) == 0 {
188203
return newConfigError("missing cassandra endpoint argument " + flag(cliOptEndpoint))
189204
}
205+
if config.CassPort == 0 {
206+
config.CassPort = defaultCassandraPort
207+
}
190208
if len(config.CassKeyspace) == 0 {
191209
return newConfigError("missing " + flag(cliOptKeyspace) + " argument ")
192210
}

tools/cassandra/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ func buildCLIOptions() *cli.App {
6262
Usage: "hostname or ip address of cassandra host to connect to",
6363
EnvVar: "CASSANDRA_HOST",
6464
},
65+
cli.IntFlag{
66+
Name: cliFlagPort,
67+
Value: defaultCassandraPort,
68+
Usage: "port of cassandra host to connect to",
69+
EnvVar: "CASSANDRA_PORT",
70+
},
71+
cli.StringFlag{
72+
Name: cliFlagUser,
73+
Value: "",
74+
Usage: "user name used for authentication for connecting to cassandra host",
75+
EnvVar: "CASSANDRA_USER",
76+
},
77+
cli.StringFlag{
78+
Name: cliFlagPassword,
79+
Value: "",
80+
Usage: "password used for authentication for connecting to cassandra host",
81+
EnvVar: "CASSANDRA_PASSWORD",
82+
},
6583
cli.StringFlag{
6684
Name: cliFlagKeyspace,
6785
Value: "cadence",

tools/cassandra/setupTask.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ type SetupSchemaTask struct {
3333
}
3434

3535
func newSetupSchemaTask(config *SetupSchemaConfig) (*SetupSchemaTask, error) {
36-
client, err := newCQLClient(config.CassHosts, config.CassKeyspace)
36+
client, err := newCQLClient(config.CassHosts, config.CassPort, config.CassUser, config.CassPassword,
37+
config.CassKeyspace)
3738
if err != nil {
3839
return nil, err
3940
}

tools/cassandra/setupTask_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (s *SetupSchemaTestSuite) SetupSuite() {
6161
s.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
6262
s.keyspace = fmt.Sprintf("setup_schema_test_%v", s.rand.Int63())
6363

64-
client, err := newCQLClient("127.0.0.1", "system")
64+
client, err := newCQLClient("127.0.0.1", defaultCassandraPort, "", "", "system")
6565
if err != nil {
6666
s.log.Fatal("Error creating CQLClient")
6767
}
@@ -87,7 +87,7 @@ func (s *SetupSchemaTestSuite) TestCreateKeyspace() {
8787

8888
func (s *SetupSchemaTestSuite) TestSetupSchema() {
8989

90-
client, err := newCQLClient("127.0.0.1", s.keyspace)
90+
client, err := newCQLClient("127.0.0.1", defaultCassandraPort, "", "", s.keyspace)
9191
s.Nil(err)
9292

9393
// test command fails without required arguments

tools/cassandra/updateTask.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func NewUpdateSchemaTask(config *UpdateSchemaConfig) (*UpdateSchemaTask, error)
8686
}
8787
}
8888

89-
client, err := newCQLClient(config.CassHosts, keyspace)
89+
client, err := newCQLClient(config.CassHosts, config.CassPort, config.CassUser, config.CassPassword, keyspace)
9090
if err != nil {
9191
return nil, err
9292
}
@@ -358,7 +358,7 @@ func readSchemaDir(dir string, startVer string, endVer string) ([]string, error)
358358
// executing the cassandra schema update
359359
func setupDryrunKeyspace(config *UpdateSchemaConfig) error {
360360

361-
client, err := newCQLClient(config.CassHosts, systemKeyspace)
361+
client, err := newCQLClient(config.CassHosts, config.CassPort, config.CassUser, config.CassPassword, systemKeyspace)
362362
if err != nil {
363363
return err
364364
}

tools/cassandra/updateTask_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (s *UpdateSchemaTestSuite) SetupSuite() {
6161
s.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
6262
s.keyspace = fmt.Sprintf("update_schema_test_%v", s.rand.Int63())
6363

64-
client, err := newCQLClient("127.0.0.1", "system")
64+
client, err := newCQLClient("127.0.0.1", defaultCassandraPort, "", "", "system")
6565
if err != nil {
6666
s.log.Fatal("Error creating CQLClient")
6767
}
@@ -81,7 +81,7 @@ func (s *UpdateSchemaTestSuite) TearDownSuite() {
8181

8282
func (s *UpdateSchemaTestSuite) TestUpdateSchema() {
8383

84-
client, err := newCQLClient("127.0.0.1", s.keyspace)
84+
client, err := newCQLClient("127.0.0.1", defaultCassandraPort, "", "", s.keyspace)
8585
s.Nil(err)
8686
defer client.Close()
8787

@@ -118,7 +118,7 @@ func (s *UpdateSchemaTestSuite) TestUpdateSchema() {
118118

119119
func (s *UpdateSchemaTestSuite) TestDryrun() {
120120

121-
client, err := newCQLClient("127.0.0.1", s.keyspace)
121+
client, err := newCQLClient("127.0.0.1", defaultCassandraPort, "", "", s.keyspace)
122122
s.Nil(err)
123123
defer client.Close()
124124

0 commit comments

Comments
 (0)