Skip to content

Commit 7e463cd

Browse files
authored
feat(gocql): add authentication support (#1939)
1 parent 19e32e0 commit 7e463cd

File tree

8 files changed

+87
-8
lines changed

8 files changed

+87
-8
lines changed

cmd/migrate-lid/migrate_lid.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ var migrateYugabyteDBCmd = &cli.Command{
122122
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
123123
Required: true,
124124
},
125+
&cli.StringFlag{
126+
Name: "username",
127+
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
128+
},
129+
&cli.StringFlag{
130+
Name: "password",
131+
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
132+
},
125133
&cli.StringFlag{
126134
Name: "connect-string",
127135
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
@@ -154,6 +162,8 @@ var migrateYugabyteDBCmd = &cli.Command{
154162
// Create a connection to the yugabyte local index directory
155163
settings := yugabyte.DBSettings{
156164
Hosts: cctx.StringSlice("hosts"),
165+
Username: cctx.String("username"),
166+
Password: cctx.String("password"),
157167
ConnectString: cctx.String("connect-string"),
158168
PayloadPiecesParallelism: cctx.Int("insert-parallelism"),
159169
CQLTimeout: cctx.Int("CQLTimeout"),
@@ -646,6 +656,14 @@ var migrateReverseYugabyteCmd = &cli.Command{
646656
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
647657
Required: true,
648658
},
659+
&cli.StringFlag{
660+
Name: "username",
661+
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
662+
},
663+
&cli.StringFlag{
664+
Name: "password",
665+
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
666+
},
649667
&cli.StringFlag{
650668
Name: "connect-string",
651669
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
@@ -691,6 +709,8 @@ func migrateReverse(cctx *cli.Context, dbType string) error {
691709
settings := yugabyte.DBSettings{
692710
ConnectString: cctx.String("connect-string"),
693711
Hosts: cctx.StringSlice("hosts"),
712+
Username: cctx.String("username"),
713+
Password: cctx.String("password"),
694714
PayloadPiecesParallelism: cctx.Int("insert-parallelism"),
695715
}
696716
migrator := yugabyte.NewMigrator(settings, migrations.DisabledMinerAddr)

extern/boostd-data/cmd/run.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ var yugabyteCmd = &cli.Command{
103103
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
104104
Required: true,
105105
},
106+
&cli.StringFlag{
107+
Name: "username",
108+
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
109+
},
110+
&cli.StringFlag{
111+
Name: "password",
112+
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
113+
},
106114
&cli.StringFlag{
107115
Name: "connect-string",
108116
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
@@ -125,6 +133,8 @@ var yugabyteCmd = &cli.Command{
125133
// Create a yugabyte data service
126134
settings := yugabyte.DBSettings{
127135
Hosts: cctx.StringSlice("hosts"),
136+
Username: cctx.String("username"),
137+
Password: cctx.String("password"),
128138
ConnectString: cctx.String("connect-string"),
129139
CQLTimeout: cctx.Int("CQLTimeout"),
130140
InsertConcurrency: cctx.Int("insert-concurrency"),
@@ -224,6 +234,14 @@ var yugabyteMigrateCmd = &cli.Command{
224234
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
225235
Required: true,
226236
},
237+
&cli.StringFlag{
238+
Name: "username",
239+
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
240+
},
241+
&cli.StringFlag{
242+
Name: "password",
243+
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
244+
},
227245
&cli.StringFlag{
228246
Name: "connect-string",
229247
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
@@ -252,6 +270,8 @@ var yugabyteMigrateCmd = &cli.Command{
252270
// Create a yugabyte data service
253271
settings := yugabyte.DBSettings{
254272
Hosts: cctx.StringSlice("hosts"),
273+
Username: cctx.String("username"),
274+
Password: cctx.String("password"),
255275
ConnectString: cctx.String("connect-string"),
256276
CQLTimeout: cctx.Int("CQLTimeout"),
257277
InsertConcurrency: cctx.Int("insert-concurrency"),
@@ -286,6 +306,14 @@ var yugabyteAddIndexCmd = &cli.Command{
286306
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
287307
Required: true,
288308
},
309+
&cli.StringFlag{
310+
Name: "username",
311+
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
312+
},
313+
&cli.StringFlag{
314+
Name: "password",
315+
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
316+
},
289317
&cli.StringFlag{
290318
Name: "connect-string",
291319
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
@@ -314,6 +342,8 @@ var yugabyteAddIndexCmd = &cli.Command{
314342
// Create a yugabyte data service
315343
settings := yugabyte.DBSettings{
316344
Hosts: cctx.StringSlice("hosts"),
345+
Username: cctx.String("username"),
346+
Password: cctx.String("password"),
317347
ConnectString: cctx.String("connect-string"),
318348
CQLTimeout: cctx.Int("CQLTimeout"),
319349
InsertConcurrency: cctx.Int("insert-concurrency"),

extern/boostd-data/yugabyte/migrator.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"database/sql"
66
"fmt"
77
"net/url"
8-
"time"
98

109
"github.com/filecoin-project/boost/extern/boostd-data/yugabyte/cassmigrate"
1110
"github.com/filecoin-project/boost/extern/boostd-data/yugabyte/migrations"
1211
"github.com/filecoin-project/go-address"
1312
_ "github.com/lib/pq"
14-
"github.com/yugabyte/gocql"
1513
)
1614

1715
type Migrator struct {
@@ -55,8 +53,7 @@ func (m *Migrator) Migrate(ctx context.Context) error {
5553
}
5654

5755
// Create a cassandra connection to be used only for running migrations.
58-
cluster := gocql.NewCluster(m.settings.Hosts...)
59-
cluster.Timeout = time.Duration(m.settings.CQLTimeout) * time.Second
56+
cluster := NewCluster(m.settings)
6057
cluster.Keyspace = m.CassandraKeyspace
6158
session, err := cluster.CreateSession()
6259
if err != nil {

extern/boostd-data/yugabyte/service.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ const InsertConcurrency = 4
4242
type DBSettings struct {
4343
// The cassandra hosts to connect to
4444
Hosts []string
45+
// The cassandra password to use
46+
Username string
47+
// The cassandra password to use
48+
Password string
4549
// The postgres connect string
4650
ConnectString string
4751
// The number of threads to use when inserting into the PayloadToPieces index
@@ -89,8 +93,7 @@ func NewStore(settings DBSettings, migrator *Migrator, opts ...StoreOpt) *Store
8993
settings.InsertConcurrency = InsertConcurrency
9094
}
9195

92-
cluster := gocql.NewCluster(settings.Hosts...)
93-
cluster.Timeout = time.Duration(settings.CQLTimeout) * time.Second
96+
cluster := NewCluster(settings)
9497
cluster.Keyspace = defaultKeyspace
9598
s := &Store{
9699
settings: settings,

extern/boostd-data/yugabyte/setup.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ func (s *Store) CreateKeyspace(ctx context.Context) error {
2020
// Create a new session using the default keyspace, then use that to create
2121
// the new keyspace
2222
log.Infow("creating cassandra keyspace " + s.cluster.Keyspace)
23-
cluster := gocql.NewCluster(s.settings.Hosts...)
24-
cluster.Timeout = time.Duration(s.settings.CQLTimeout) * time.Second
23+
cluster := NewCluster(s.settings)
2524
session, err := cluster.CreateSession()
2625
if err != nil {
2726
return fmt.Errorf("creating yugabyte cluster: %w", err)
@@ -90,3 +89,15 @@ func (s *Store) execSQL(ctx context.Context, query string) error {
9089
_, err := s.db.Exec(ctx, query)
9190
return err
9291
}
92+
93+
func NewCluster(settings DBSettings) *gocql.ClusterConfig {
94+
cluster := gocql.NewCluster(settings.Hosts...)
95+
cluster.Timeout = time.Duration(settings.CQLTimeout) * time.Second
96+
if settings.Username != "" {
97+
cluster.Authenticator = gocql.PasswordAuthenticator{
98+
Username: settings.Username,
99+
Password: settings.Password,
100+
}
101+
}
102+
return cluster
103+
}

node/config/doc_gen.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/config/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ type LocalIndexDirectoryYugabyteConfig struct {
265265
ConnectString string
266266
// The yugabyte cassandra hosts eg ["127.0.0.1"]
267267
Hosts []string
268+
// The yugabyte cassandra username eg "cassandra"
269+
Username string
270+
// The yugabyte cassandra password eg "cassandra"
271+
Password string
268272
}
269273

270274
type LocalIndexDirectoryConfig struct {

node/modules/piecedirectory.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func NewPieceDirectoryStore(cfg *config.Boost) func(lc fx.Lifecycle, r lotus_rep
5656
// Set up a local index directory service that connects to the yugabyte db
5757
settings := yugabyte.DBSettings{
5858
Hosts: cfg.LocalIndexDirectory.Yugabyte.Hosts,
59+
Username: cfg.LocalIndexDirectory.Yugabyte.Username,
60+
Password: cfg.LocalIndexDirectory.Yugabyte.Password,
5961
ConnectString: cfg.LocalIndexDirectory.Yugabyte.ConnectString,
6062
}
6163
migrator := yugabyte.NewMigrator(settings, address.Address(maddr))

0 commit comments

Comments
 (0)