Skip to content

Commit

Permalink
feat(gocql): add authentication support (filecoin-project#1939)
Browse files Browse the repository at this point in the history
  • Loading branch information
strahe authored Aug 14, 2024
1 parent 19e32e0 commit 7e463cd
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 8 deletions.
20 changes: 20 additions & 0 deletions cmd/migrate-lid/migrate_lid.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ var migrateYugabyteDBCmd = &cli.Command{
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
Required: true,
},
&cli.StringFlag{
Name: "username",
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "password",
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "connect-string",
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
Expand Down Expand Up @@ -154,6 +162,8 @@ var migrateYugabyteDBCmd = &cli.Command{
// Create a connection to the yugabyte local index directory
settings := yugabyte.DBSettings{
Hosts: cctx.StringSlice("hosts"),
Username: cctx.String("username"),
Password: cctx.String("password"),
ConnectString: cctx.String("connect-string"),
PayloadPiecesParallelism: cctx.Int("insert-parallelism"),
CQLTimeout: cctx.Int("CQLTimeout"),
Expand Down Expand Up @@ -646,6 +656,14 @@ var migrateReverseYugabyteCmd = &cli.Command{
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
Required: true,
},
&cli.StringFlag{
Name: "username",
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "password",
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "connect-string",
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
Expand Down Expand Up @@ -691,6 +709,8 @@ func migrateReverse(cctx *cli.Context, dbType string) error {
settings := yugabyte.DBSettings{
ConnectString: cctx.String("connect-string"),
Hosts: cctx.StringSlice("hosts"),
Username: cctx.String("username"),
Password: cctx.String("password"),
PayloadPiecesParallelism: cctx.Int("insert-parallelism"),
}
migrator := yugabyte.NewMigrator(settings, migrations.DisabledMinerAddr)
Expand Down
30 changes: 30 additions & 0 deletions extern/boostd-data/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ var yugabyteCmd = &cli.Command{
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
Required: true,
},
&cli.StringFlag{
Name: "username",
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "password",
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "connect-string",
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
Expand All @@ -125,6 +133,8 @@ var yugabyteCmd = &cli.Command{
// Create a yugabyte data service
settings := yugabyte.DBSettings{
Hosts: cctx.StringSlice("hosts"),
Username: cctx.String("username"),
Password: cctx.String("password"),
ConnectString: cctx.String("connect-string"),
CQLTimeout: cctx.Int("CQLTimeout"),
InsertConcurrency: cctx.Int("insert-concurrency"),
Expand Down Expand Up @@ -224,6 +234,14 @@ var yugabyteMigrateCmd = &cli.Command{
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
Required: true,
},
&cli.StringFlag{
Name: "username",
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "password",
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "connect-string",
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
Expand Down Expand Up @@ -252,6 +270,8 @@ var yugabyteMigrateCmd = &cli.Command{
// Create a yugabyte data service
settings := yugabyte.DBSettings{
Hosts: cctx.StringSlice("hosts"),
Username: cctx.String("username"),
Password: cctx.String("password"),
ConnectString: cctx.String("connect-string"),
CQLTimeout: cctx.Int("CQLTimeout"),
InsertConcurrency: cctx.Int("insert-concurrency"),
Expand Down Expand Up @@ -286,6 +306,14 @@ var yugabyteAddIndexCmd = &cli.Command{
Usage: "yugabyte hosts to connect to over cassandra interface eg '127.0.0.1'",
Required: true,
},
&cli.StringFlag{
Name: "username",
Usage: "yugabyte username to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "password",
Usage: "yugabyte password to connect to over cassandra interface eg 'cassandra'",
},
&cli.StringFlag{
Name: "connect-string",
Usage: "postgres connect string eg 'postgresql://postgres:postgres@localhost'",
Expand Down Expand Up @@ -314,6 +342,8 @@ var yugabyteAddIndexCmd = &cli.Command{
// Create a yugabyte data service
settings := yugabyte.DBSettings{
Hosts: cctx.StringSlice("hosts"),
Username: cctx.String("username"),
Password: cctx.String("password"),
ConnectString: cctx.String("connect-string"),
CQLTimeout: cctx.Int("CQLTimeout"),
InsertConcurrency: cctx.Int("insert-concurrency"),
Expand Down
5 changes: 1 addition & 4 deletions extern/boostd-data/yugabyte/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"database/sql"
"fmt"
"net/url"
"time"

"github.com/filecoin-project/boost/extern/boostd-data/yugabyte/cassmigrate"
"github.com/filecoin-project/boost/extern/boostd-data/yugabyte/migrations"
"github.com/filecoin-project/go-address"
_ "github.com/lib/pq"
"github.com/yugabyte/gocql"
)

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

// Create a cassandra connection to be used only for running migrations.
cluster := gocql.NewCluster(m.settings.Hosts...)
cluster.Timeout = time.Duration(m.settings.CQLTimeout) * time.Second
cluster := NewCluster(m.settings)
cluster.Keyspace = m.CassandraKeyspace
session, err := cluster.CreateSession()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions extern/boostd-data/yugabyte/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const InsertConcurrency = 4
type DBSettings struct {
// The cassandra hosts to connect to
Hosts []string
// The cassandra password to use
Username string
// The cassandra password to use
Password string
// The postgres connect string
ConnectString string
// The number of threads to use when inserting into the PayloadToPieces index
Expand Down Expand Up @@ -89,8 +93,7 @@ func NewStore(settings DBSettings, migrator *Migrator, opts ...StoreOpt) *Store
settings.InsertConcurrency = InsertConcurrency
}

cluster := gocql.NewCluster(settings.Hosts...)
cluster.Timeout = time.Duration(settings.CQLTimeout) * time.Second
cluster := NewCluster(settings)
cluster.Keyspace = defaultKeyspace
s := &Store{
settings: settings,
Expand Down
15 changes: 13 additions & 2 deletions extern/boostd-data/yugabyte/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func (s *Store) CreateKeyspace(ctx context.Context) error {
// Create a new session using the default keyspace, then use that to create
// the new keyspace
log.Infow("creating cassandra keyspace " + s.cluster.Keyspace)
cluster := gocql.NewCluster(s.settings.Hosts...)
cluster.Timeout = time.Duration(s.settings.CQLTimeout) * time.Second
cluster := NewCluster(s.settings)
session, err := cluster.CreateSession()
if err != nil {
return fmt.Errorf("creating yugabyte cluster: %w", err)
Expand Down Expand Up @@ -90,3 +89,15 @@ func (s *Store) execSQL(ctx context.Context, query string) error {
_, err := s.db.Exec(ctx, query)
return err
}

func NewCluster(settings DBSettings) *gocql.ClusterConfig {
cluster := gocql.NewCluster(settings.Hosts...)
cluster.Timeout = time.Duration(settings.CQLTimeout) * time.Second
if settings.Username != "" {
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: settings.Username,
Password: settings.Password,
}
}
return cluster
}
12 changes: 12 additions & 0 deletions node/config/doc_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions node/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ type LocalIndexDirectoryYugabyteConfig struct {
ConnectString string
// The yugabyte cassandra hosts eg ["127.0.0.1"]
Hosts []string
// The yugabyte cassandra username eg "cassandra"
Username string
// The yugabyte cassandra password eg "cassandra"
Password string
}

type LocalIndexDirectoryConfig struct {
Expand Down
2 changes: 2 additions & 0 deletions node/modules/piecedirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func NewPieceDirectoryStore(cfg *config.Boost) func(lc fx.Lifecycle, r lotus_rep
// Set up a local index directory service that connects to the yugabyte db
settings := yugabyte.DBSettings{
Hosts: cfg.LocalIndexDirectory.Yugabyte.Hosts,
Username: cfg.LocalIndexDirectory.Yugabyte.Username,
Password: cfg.LocalIndexDirectory.Yugabyte.Password,
ConnectString: cfg.LocalIndexDirectory.Yugabyte.ConnectString,
}
migrator := yugabyte.NewMigrator(settings, address.Address(maddr))
Expand Down

0 comments on commit 7e463cd

Please sign in to comment.