certificate/authorization: add ctx to OpenDB

Database opening usually dial and hence we should pass ctx to them.

Change-Id: I1362783568f66383c46f07be7549327bb1aaa39e
This commit is contained in:
Egon Elbre 2020-10-29 09:46:23 +02:00
parent d0beaa4a87
commit 096445bc1c
7 changed files with 20 additions and 18 deletions

View File

@ -47,13 +47,13 @@ type DBConfig struct {
Overwrite bool `default:"false" help:"if true, overwrites config AND authorization db is truncated" setup:"true"`
}
// NewDBFromCfg creates and/or opens the authorization database specified by the config.
func NewDBFromCfg(config DBConfig) (*DB, error) {
return NewDB(config.URL, config.Overwrite)
// OpenDBFromCfg creates and/or opens the authorization database specified by the config.
func OpenDBFromCfg(ctx context.Context, config DBConfig) (*DB, error) {
return OpenDB(ctx, config.URL, config.Overwrite)
}
// NewDB creates and/or opens the authorization database.
func NewDB(dbURL string, overwrite bool) (*DB, error) {
// OpenDB creates and/or opens the authorization database.
func OpenDB(ctx context.Context, dbURL string, overwrite bool) (*DB, error) {
driver, source, _, err := dbutil.SplitConnStr(dbURL)
if err != nil {
return nil, extensions.ErrRevocationDB.Wrap(err)

View File

@ -28,7 +28,7 @@ func TestNewDB(t *testing.T) {
defer ctx.Cleanup()
dbURL := "bolt://" + ctx.File("authorizations.db")
db, err := NewDB(dbURL, false)
db, err := OpenDB(ctx, dbURL, false)
require.NoError(t, err)
defer ctx.Check(db.Close)
@ -40,7 +40,7 @@ func TestNewDBFromCfg(t *testing.T) {
ctx := testcontext.New(t)
defer ctx.Cleanup()
db, err := NewDBFromCfg(DBConfig{
db, err := OpenDBFromCfg(ctx, DBConfig{
URL: "bolt://" + ctx.File("authorizations.db"),
Overwrite: false,
})
@ -410,7 +410,7 @@ func TestAuthorizationDB_Emails(t *testing.T) {
func newTestAuthDB(t *testing.T, ctx *testcontext.Context) *DB {
dbURL := "bolt://" + ctx.File("authorizations.db")
db, err := NewDB(dbURL, false)
db, err := OpenDB(ctx, dbURL, false)
require.NoError(t, err)
return db
}

View File

@ -51,7 +51,7 @@ func TestCertificateSigner_Sign_E2E(t *testing.T) {
URL: "bolt://" + ctx.File("authorizations.db"),
}
authDB, err := authorization.NewDBFromCfg(authorizationsCfg)
authDB, err := authorization.OpenDBFromCfg(ctx, authorizationsCfg)
require.NoError(t, err)
require.NotNil(t, authDB)
@ -113,7 +113,7 @@ func TestCertificateSigner_Sign_E2E(t *testing.T) {
assert.NoError(t, err)
// NB: re-open after closing for server
authDB, err = authorization.NewDBFromCfg(authorizationsCfg)
authDB, err = authorization.OpenDBFromCfg(ctx, authorizationsCfg)
require.NoError(t, err)
defer ctx.Check(authDB.Close)
require.NotNil(t, authDB)
@ -150,7 +150,7 @@ func TestCertificateSigner_Sign(t *testing.T) {
userID := "user@mail.test"
// TODO: test with all types of authorization DBs (bolt, redis, etc.)
authDB, err := authorization.NewDB("bolt://"+ctx.File("authorizations.db"), false)
authDB, err := authorization.OpenDB(ctx, "bolt://"+ctx.File("authorizations.db"), false)
require.NoError(t, err)
defer ctx.Check(authDB.Close)
require.NotNil(t, authDB)

View File

@ -70,7 +70,7 @@ func cmdCreateAuth(cmd *cobra.Command, args []string) error {
if err != nil {
return errs.New("Count couldn't be parsed: %s", args[0])
}
authDB, err := authorization.NewDBFromCfg(authCfg.Config.AuthorizationDB)
authDB, err := authorization.OpenDBFromCfg(ctx, authCfg.Config.AuthorizationDB)
if err != nil {
return err
}
@ -99,7 +99,7 @@ func cmdCreateAuth(cmd *cobra.Command, args []string) error {
func cmdInfoAuth(cmd *cobra.Command, args []string) error {
ctx, _ := process.Ctx(cmd)
authDB, err := authorization.NewDBFromCfg(authCfg.Config.AuthorizationDB)
authDB, err := authorization.OpenDBFromCfg(ctx, authCfg.Config.AuthorizationDB)
if err != nil {
return err
}
@ -194,7 +194,7 @@ func writeTokenInfo(claimed, open authorization.Group, w io.Writer) error {
func cmdExportAuth(cmd *cobra.Command, args []string) error {
ctx, _ := process.Ctx(cmd)
authDB, err := authorization.NewDBFromCfg(authCfg.Config.AuthorizationDB)
authDB, err := authorization.OpenDBFromCfg(ctx, authCfg.Config.AuthorizationDB)
if err != nil {
return err
}

View File

@ -37,7 +37,7 @@ var (
func cmdExportClaims(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)
authDB, err := authorization.NewDBFromCfg(claimsExportCfg.Config.AuthorizationDB)
authDB, err := authorization.OpenDBFromCfg(ctx, claimsExportCfg.Config.AuthorizationDB)
if err != nil {
return err
}
@ -76,7 +76,7 @@ func cmdExportClaims(cmd *cobra.Command, args []string) (err error) {
func cmdDeleteClaim(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)
authDB, err := authorization.NewDBFromCfg(claimsDeleteCfg.AuthorizationDB)
authDB, err := authorization.OpenDBFromCfg(ctx, claimsDeleteCfg.AuthorizationDB)
if err != nil {
return err
}

View File

@ -70,7 +70,7 @@ func cmdRun(cmd *cobra.Command, args []string) error {
return err
}
authorizationDB, err := authorization.NewDBFromCfg(runCfg.AuthorizationDB)
authorizationDB, err := authorization.OpenDBFromCfg(ctx, runCfg.AuthorizationDB)
if err != nil {
return errs.New("error opening authorizations database: %+v", err)
}

View File

@ -25,6 +25,8 @@ var (
)
func cmdSetup(cmd *cobra.Command, args []string) error {
ctx, _ := process.Ctx(cmd)
setupDir, err := filepath.Abs(confDir)
if err != nil {
return err
@ -44,7 +46,7 @@ func cmdSetup(cmd *cobra.Command, args []string) error {
return nil
}
authorizationDB, err := authorization.NewDBFromCfg(setupCfg.Config.AuthorizationDB)
authorizationDB, err := authorization.OpenDBFromCfg(ctx, setupCfg.Config.AuthorizationDB)
if err != nil {
return err
}