From d0beaa4a8762bb20cbc8709c4521c6d5dce39539 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Wed, 28 Oct 2020 16:01:41 +0200 Subject: [PATCH] pkg/revocation: pass ctx into opening the database Opening a databases requires ctx, this is first step to passing ctx to the appropriate level. Change-Id: I12700f39a320206d8a2a4e054452319f8585b44b --- cmd/certificates/main.go | 2 +- cmd/identity/certificate_authority.go | 2 +- cmd/identity/main.go | 2 +- cmd/identity/revocations.go | 2 +- cmd/satellite/api.go | 2 +- cmd/satellite/gc.go | 2 +- cmd/satellite/main.go | 2 +- cmd/satellite/repairer.go | 2 +- cmd/storagenode/main.go | 2 +- pkg/revocation/common.go | 24 +++++++++++--------- pkg/revocation/tlsopts_test.go | 2 +- private/testplanet/satellite.go | 32 ++++++++++++++------------- private/testplanet/storagenode.go | 6 +++-- private/testplanet/uplink_test.go | 2 +- private/testrevocation/db.go | 4 ++-- 15 files changed, 47 insertions(+), 41 deletions(-) diff --git a/cmd/certificates/main.go b/cmd/certificates/main.go index 2ada309df..a2f7484e4 100644 --- a/cmd/certificates/main.go +++ b/cmd/certificates/main.go @@ -75,7 +75,7 @@ func cmdRun(cmd *cobra.Command, args []string) error { return errs.New("error opening authorizations database: %+v", err) } - revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config) if err != nil { return errs.New("error creating revocation database: %+v", err) } diff --git a/cmd/identity/certificate_authority.go b/cmd/identity/certificate_authority.go index 5d356f80c..57afa0ee8 100644 --- a/cmd/identity/certificate_authority.go +++ b/cmd/identity/certificate_authority.go @@ -193,7 +193,7 @@ func cmdRevokePeerCA(cmd *cobra.Command, args []string) (err error) { return err } - revDB, err := revocation.NewDB(revokePeerCACfg.RevocationDBURL) + revDB, err := revocation.OpenDB(ctx, revokePeerCACfg.RevocationDBURL) if err != nil { return err } diff --git a/cmd/identity/main.go b/cmd/identity/main.go index e955e3111..e48e5e30f 100644 --- a/cmd/identity/main.go +++ b/cmd/identity/main.go @@ -189,7 +189,7 @@ func cmdAuthorize(cmd *cobra.Command, args []string) (err error) { // Ensure we dont enforce a signed Peer Identity config.Signer.TLS.UsePeerCAWhitelist = false - revocationDB, err := revocation.NewDBFromCfg(config.Signer.TLS) + revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Signer.TLS) if err != nil { return errs.New("error creating revocation database: %+v", err) } diff --git a/cmd/identity/revocations.go b/cmd/identity/revocations.go index 1f9b3fac3..88522bf62 100644 --- a/cmd/identity/revocations.go +++ b/cmd/identity/revocations.go @@ -42,7 +42,7 @@ func cmdRevocations(cmd *cobra.Command, args []string) error { if len(args) > 0 { revCfg.RevocationDBURL = "bolt://" + filepath.Join(configDir, args[0], "revocations.db") } - revDB, err := revocation.NewDB(revCfg.RevocationDBURL) + revDB, err := revocation.OpenDB(ctx, revCfg.RevocationDBURL) if err != nil { return err } diff --git a/cmd/satellite/api.go b/cmd/satellite/api.go index 9377c53dd..fed9f02e8 100644 --- a/cmd/satellite/api.go +++ b/cmd/satellite/api.go @@ -50,7 +50,7 @@ func cmdAPIRun(cmd *cobra.Command, args []string) (err error) { err = errs.Combine(err, pointerDB.Close()) }() - revocationDB, err := revocation.NewDBFromCfg(runCfg.Config.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Config.Server.Config) if err != nil { return errs.New("Error creating revocation database on satellite api: %+v", err) } diff --git a/cmd/satellite/gc.go b/cmd/satellite/gc.go index ad163db9f..be16f3a7b 100644 --- a/cmd/satellite/gc.go +++ b/cmd/satellite/gc.go @@ -44,7 +44,7 @@ func cmdGCRun(cmd *cobra.Command, args []string) (err error) { err = errs.Combine(err, pointerDB.Close()) }() - revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config) if err != nil { return errs.New("Error creating revocation database GC: %+v", err) } diff --git a/cmd/satellite/main.go b/cmd/satellite/main.go index 752e0fe7d..2ccaf191d 100644 --- a/cmd/satellite/main.go +++ b/cmd/satellite/main.go @@ -343,7 +343,7 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) { err = errs.Combine(err, pointerDB.Close()) }() - revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config) if err != nil { return errs.New("Error creating revocation database: %+v", err) } diff --git a/cmd/satellite/repairer.go b/cmd/satellite/repairer.go index af180dbea..2f192de4b 100644 --- a/cmd/satellite/repairer.go +++ b/cmd/satellite/repairer.go @@ -46,7 +46,7 @@ func cmdRepairerRun(cmd *cobra.Command, args []string) (err error) { err = errs.Combine(err, pointerDB.Close()) }() - revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config) if err != nil { return errs.New("Error creating revocation database: %+v", err) } diff --git a/cmd/storagenode/main.go b/cmd/storagenode/main.go index 32493c63d..70bee48ee 100644 --- a/cmd/storagenode/main.go +++ b/cmd/storagenode/main.go @@ -158,7 +158,7 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) { err = errs.Combine(err, db.Close()) }() - revocationDB, err := revocation.NewDBFromCfg(runCfg.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, runCfg.Server.Config) if err != nil { return errs.New("Error creating revocation database: %+v", err) } diff --git a/pkg/revocation/common.go b/pkg/revocation/common.go index 145b5ec58..81af780d9 100644 --- a/pkg/revocation/common.go +++ b/pkg/revocation/common.go @@ -4,6 +4,8 @@ package revocation import ( + "context" + "storj.io/common/peertls/extensions" "storj.io/common/peertls/tlsopts" "storj.io/storj/private/dbutil" @@ -11,18 +13,18 @@ import ( "storj.io/storj/storage/redis" ) -// NewDBFromCfg is a convenience method to create a revocation DB +// OpenDBFromCfg is a convenience method to create a revocation DB // directly from a config. If the revocation extension option is not set, it // returns a nil db with no error. -func NewDBFromCfg(cfg tlsopts.Config) (*DB, error) { +func OpenDBFromCfg(ctx context.Context, cfg tlsopts.Config) (*DB, error) { if !cfg.Extensions.Revocation { return &DB{}, nil } - return NewDB(cfg.RevocationDBURL) + return OpenDB(ctx, cfg.RevocationDBURL) } -// NewDB returns a new revocation database given the URL. -func NewDB(dbURL string) (*DB, error) { +// OpenDB returns a new revocation database given the URL. +func OpenDB(ctx context.Context, dbURL string) (*DB, error) { driver, source, _, err := dbutil.SplitConnStr(dbURL) if err != nil { return nil, extensions.ErrRevocationDB.Wrap(err) @@ -30,12 +32,12 @@ func NewDB(dbURL string) (*DB, error) { var db *DB switch driver { case "bolt": - db, err = newDBBolt(source) + db, err = openDBBolt(ctx, source) if err != nil { return nil, extensions.ErrRevocationDB.Wrap(err) } case "redis": - db, err = newDBRedis(dbURL) + db, err = openDBRedis(ctx, dbURL) if err != nil { return nil, extensions.ErrRevocationDB.Wrap(err) } @@ -45,8 +47,8 @@ func NewDB(dbURL string) (*DB, error) { return db, nil } -// newDBBolt creates a bolt-backed DB. -func newDBBolt(path string) (*DB, error) { +// openDBBolt creates a bolt-backed DB. +func openDBBolt(ctx context.Context, path string) (*DB, error) { client, err := boltdb.New(path, extensions.RevocationBucket) if err != nil { return nil, err @@ -56,8 +58,8 @@ func newDBBolt(path string) (*DB, error) { }, nil } -// newDBRedis creates a redis-backed DB. -func newDBRedis(address string) (*DB, error) { +// openDBRedis creates a redis-backed DB. +func openDBRedis(ctx context.Context, address string) (*DB, error) { client, err := redis.NewClientFrom(address) if err != nil { return nil, err diff --git a/pkg/revocation/tlsopts_test.go b/pkg/revocation/tlsopts_test.go index 3a8b004d6..eedf2ac37 100644 --- a/pkg/revocation/tlsopts_test.go +++ b/pkg/revocation/tlsopts_test.go @@ -110,7 +110,7 @@ func TestNewOptions(t *testing.T) { for _, c := range cases { t.Log(c.testID) - revocationDB, err := revocation.NewDBFromCfg(c.config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, c.config) require.NoError(t, err) tlsOptions, err := tlsopts.NewOptions(fi, c.config, revocationDB) diff --git a/private/testplanet/satellite.go b/private/testplanet/satellite.go index fffb0a8da..a0fa7b423 100644 --- a/private/testplanet/satellite.go +++ b/private/testplanet/satellite.go @@ -323,6 +323,8 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes }() for i := 0; i < count; i++ { + ctx := context.TODO() + prefix := "satellite" + strconv.Itoa(i) log := planet.log.Named(prefix) @@ -336,7 +338,7 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes return nil, err } - db, err := satellitedbtest.CreateMasterDB(context.TODO(), log.Named("db"), planet.config.Name, "S", i, satelliteDatabases.MasterDB) + db, err := satellitedbtest.CreateMasterDB(ctx, log.Named("db"), planet.config.Name, "S", i, satelliteDatabases.MasterDB) if err != nil { return nil, err } @@ -351,7 +353,7 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes } planet.databases = append(planet.databases, db) - pointerDB, err := satellitedbtest.CreatePointerDB(context.TODO(), log.Named("pointerdb"), planet.config.Name, "P", i, satelliteDatabases.PointerDB) + pointerDB, err := satellitedbtest.CreatePointerDB(ctx, log.Named("pointerdb"), planet.config.Name, "P", i, satelliteDatabases.PointerDB) if err != nil { return nil, err } @@ -620,7 +622,7 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes versionInfo := planet.NewVersionInfo() - revocationDB, err := revocation.NewDBFromCfg(config.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config) if err != nil { return xs, errs.Wrap(err) } @@ -641,27 +643,27 @@ func (planet *Planet) newSatellites(count int, satelliteDatabases satellitedbtes return xs, err } - err = db.TestingMigrateToLatest(context.TODO()) + err = db.TestingMigrateToLatest(ctx) if err != nil { return nil, err } - api, err := planet.newAPI(i, identity, db, pointerDB, config, versionInfo) + api, err := planet.newAPI(ctx, i, identity, db, pointerDB, config, versionInfo) if err != nil { return xs, err } - adminPeer, err := planet.newAdmin(i, identity, db, config, versionInfo) + adminPeer, err := planet.newAdmin(ctx, i, identity, db, config, versionInfo) if err != nil { return xs, err } - repairerPeer, err := planet.newRepairer(i, identity, db, pointerDB, config, versionInfo) + repairerPeer, err := planet.newRepairer(ctx, i, identity, db, pointerDB, config, versionInfo) if err != nil { return xs, err } - gcPeer, err := planet.newGarbageCollection(i, identity, db, pointerDB, config, versionInfo) + gcPeer, err := planet.newGarbageCollection(ctx, i, identity, db, pointerDB, config, versionInfo) if err != nil { return xs, err } @@ -753,12 +755,12 @@ func createNewSystem(log *zap.Logger, config satellite.Config, peer *satellite.C return system } -func (planet *Planet) newAPI(count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.API, error) { +func (planet *Planet) newAPI(ctx context.Context, count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.API, error) { prefix := "satellite-api" + strconv.Itoa(count) log := planet.log.Named(prefix) var err error - revocationDB, err := revocation.NewDBFromCfg(config.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config) if err != nil { return nil, errs.Wrap(err) } @@ -776,18 +778,18 @@ func (planet *Planet) newAPI(count int, identity *identity.FullIdentity, db sate return satellite.NewAPI(log, identity, db, pointerDB, revocationDB, liveAccounting, rollupsWriteCache, &config, versionInfo, nil) } -func (planet *Planet) newAdmin(count int, identity *identity.FullIdentity, db satellite.DB, config satellite.Config, versionInfo version.Info) (*satellite.Admin, error) { +func (planet *Planet) newAdmin(ctx context.Context, count int, identity *identity.FullIdentity, db satellite.DB, config satellite.Config, versionInfo version.Info) (*satellite.Admin, error) { prefix := "satellite-admin" + strconv.Itoa(count) log := planet.log.Named(prefix) return satellite.NewAdmin(log, identity, db, versionInfo, &config, nil) } -func (planet *Planet) newRepairer(count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.Repairer, error) { +func (planet *Planet) newRepairer(ctx context.Context, count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.Repairer, error) { prefix := "satellite-repairer" + strconv.Itoa(count) log := planet.log.Named(prefix) - revocationDB, err := revocation.NewDBFromCfg(config.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config) if err != nil { return nil, errs.Wrap(err) } @@ -807,11 +809,11 @@ func (cache rollupsWriteCacheCloser) Close() error { return cache.RollupsWriteCache.CloseAndFlush(context.TODO()) } -func (planet *Planet) newGarbageCollection(count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.GarbageCollection, error) { +func (planet *Planet) newGarbageCollection(ctx context.Context, count int, identity *identity.FullIdentity, db satellite.DB, pointerDB metainfo.PointerDB, config satellite.Config, versionInfo version.Info) (*satellite.GarbageCollection, error) { prefix := "satellite-gc" + strconv.Itoa(count) log := planet.log.Named(prefix) - revocationDB, err := revocation.NewDBFromCfg(config.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config) if err != nil { return nil, errs.Wrap(err) } diff --git a/private/testplanet/storagenode.go b/private/testplanet/storagenode.go index d66f4d888..776c41d97 100644 --- a/private/testplanet/storagenode.go +++ b/private/testplanet/storagenode.go @@ -72,6 +72,8 @@ func (planet *Planet) newStorageNodes(count int, whitelistedSatellites storj.Nod } for i := 0; i < count; i++ { + ctx := context.TODO() + prefix := "storage" + strconv.Itoa(i) log := planet.log.Named(prefix) storageDir := filepath.Join(planet.directory, prefix) @@ -207,7 +209,7 @@ func (planet *Planet) newStorageNodes(count int, whitelistedSatellites storj.Nod } } - revocationDB, err := revocation.NewDBFromCfg(config.Server.Config) + revocationDB, err := revocation.OpenDBFromCfg(ctx, config.Server.Config) if err != nil { return xs, errs.Wrap(err) } @@ -221,7 +223,7 @@ func (planet *Planet) newStorageNodes(count int, whitelistedSatellites storj.Nod // Mark the peer's PieceDeleter as in testing mode, so it is easy to wait on the deleter peer.Storage2.PieceDeleter.SetupTest() - err = db.MigrateToLatest(context.TODO()) + err = db.MigrateToLatest(ctx) if err != nil { return nil, err } diff --git a/private/testplanet/uplink_test.go b/private/testplanet/uplink_test.go index f4d6cb6a1..4f2a36d00 100644 --- a/private/testplanet/uplink_test.go +++ b/private/testplanet/uplink_test.go @@ -209,7 +209,7 @@ func TestDownloadFromUnresponsiveNode(t *testing.T) { }, } - revocationDB, err := revocation.NewDBFromCfg(tlscfg) + revocationDB, err := revocation.OpenDBFromCfg(ctx, tlscfg) require.NoError(t, err) tlsOptions, err := tlsopts.NewOptions(storageNode.Identity, tlscfg, revocationDB) diff --git a/private/testrevocation/db.go b/private/testrevocation/db.go index 90895b405..0820acae0 100644 --- a/private/testrevocation/db.go +++ b/private/testrevocation/db.go @@ -27,7 +27,7 @@ func RunDBs(t *testing.T, test func(*testing.T, extensions.RevocationDB, storage // Test using redis-backed revocation DB dbURL := "redis://" + redis.Addr() + "?db=0" - db, err := revocation.NewDB(dbURL) + db, err := revocation.OpenDB(ctx, dbURL) require.NoError(t, err) defer ctx.Check(db.Close) @@ -39,7 +39,7 @@ func RunDBs(t *testing.T, test func(*testing.T, extensions.RevocationDB, storage defer ctx.Cleanup() // Test using bolt-backed revocation DB - db, err := revocation.NewDB("bolt://" + ctx.File("revocations.db")) + db, err := revocation.OpenDB(ctx, "bolt://"+ctx.File("revocations.db")) require.NoError(t, err) defer ctx.Check(db.Close)