2019-08-20 16:04:17 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package revocation
|
|
|
|
|
|
|
|
import (
|
2020-10-28 14:01:41 +00:00
|
|
|
"context"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/peertls/extensions"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil"
|
2023-04-06 14:54:41 +01:00
|
|
|
"storj.io/storj/private/kvstore/boltdb"
|
|
|
|
"storj.io/storj/private/kvstore/redis"
|
2019-08-20 16:04:17 +01:00
|
|
|
)
|
|
|
|
|
2020-10-28 14:01:41 +00:00
|
|
|
// OpenDBFromCfg is a convenience method to create a revocation DB
|
2019-08-20 16:04:17 +01:00
|
|
|
// directly from a config. If the revocation extension option is not set, it
|
|
|
|
// returns a nil db with no error.
|
2020-10-28 14:01:41 +00:00
|
|
|
func OpenDBFromCfg(ctx context.Context, cfg tlsopts.Config) (*DB, error) {
|
2019-08-20 16:04:17 +01:00
|
|
|
if !cfg.Extensions.Revocation {
|
|
|
|
return &DB{}, nil
|
|
|
|
}
|
2020-10-28 14:01:41 +00:00
|
|
|
return OpenDB(ctx, cfg.RevocationDBURL)
|
2019-08-20 16:04:17 +01:00
|
|
|
}
|
|
|
|
|
2020-10-28 14:01:41 +00:00
|
|
|
// OpenDB returns a new revocation database given the URL.
|
|
|
|
func OpenDB(ctx context.Context, dbURL string) (*DB, error) {
|
2019-12-02 21:05:21 +00:00
|
|
|
driver, source, _, err := dbutil.SplitConnStr(dbURL)
|
2019-08-20 16:04:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, extensions.ErrRevocationDB.Wrap(err)
|
|
|
|
}
|
|
|
|
var db *DB
|
|
|
|
switch driver {
|
|
|
|
case "bolt":
|
2020-10-28 14:01:41 +00:00
|
|
|
db, err = openDBBolt(ctx, source)
|
2019-08-20 16:04:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, extensions.ErrRevocationDB.Wrap(err)
|
|
|
|
}
|
|
|
|
case "redis":
|
2020-10-28 14:01:41 +00:00
|
|
|
db, err = openDBRedis(ctx, dbURL)
|
2019-08-20 16:04:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, extensions.ErrRevocationDB.Wrap(err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, extensions.ErrRevocationDB.New("database scheme not supported: %s", driver)
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:01:41 +00:00
|
|
|
// openDBBolt creates a bolt-backed DB.
|
|
|
|
func openDBBolt(ctx context.Context, path string) (*DB, error) {
|
2019-08-20 16:04:17 +01:00
|
|
|
client, err := boltdb.New(path, extensions.RevocationBucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &DB{
|
|
|
|
store: client,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:01:41 +00:00
|
|
|
// openDBRedis creates a redis-backed DB.
|
|
|
|
func openDBRedis(ctx context.Context, address string) (*DB, error) {
|
2021-03-24 19:22:50 +00:00
|
|
|
client, err := redis.OpenClientFrom(ctx, address)
|
2019-08-20 16:04:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &DB{
|
|
|
|
store: client,
|
|
|
|
}, nil
|
|
|
|
}
|