certificates/authority/mgmt/db.go

137 lines
4.3 KiB
Go
Raw Normal View History

2021-05-03 19:48:20 +00:00
package mgmt
import (
"context"
"github.com/pkg/errors"
2021-05-24 20:38:24 +00:00
"github.com/smallstep/certificates/linkedca"
2021-05-03 19:48:20 +00:00
)
// ErrNotFound is an error that should be used by the authority.DB interface to
// indicate that an entity does not exist.
var ErrNotFound = errors.New("not found")
// DB is the DB interface expected by the step-ca ACME API.
type DB interface {
2021-05-24 20:38:24 +00:00
CreateProvisioner(ctx context.Context, prov *linkedca.Provisioner) error
GetProvisioner(ctx context.Context, id string) (*linkedca.Provisioner, error)
GetProvisioners(ctx context.Context) ([]*linkedca.Provisioner, error)
UpdateProvisioner(ctx context.Context, prov *linkedca.Provisioner) error
2021-05-25 23:52:06 +00:00
DeleteProvisioner(ctx context.Context, id string) error
2021-05-24 20:38:24 +00:00
CreateAdmin(ctx context.Context, admin *linkedca.Admin) error
GetAdmin(ctx context.Context, id string) (*linkedca.Admin, error)
GetAdmins(ctx context.Context) ([]*linkedca.Admin, error)
UpdateAdmin(ctx context.Context, admin *linkedca.Admin) error
2021-05-25 23:52:06 +00:00
DeleteAdmin(ctx context.Context, id string) error
2021-05-03 19:48:20 +00:00
}
// MockDB is an implementation of the DB interface that should only be used as
// a mock in tests.
type MockDB struct {
2021-05-24 20:38:24 +00:00
MockCreateProvisioner func(ctx context.Context, prov *linkedca.Provisioner) error
MockGetProvisioner func(ctx context.Context, id string) (*linkedca.Provisioner, error)
MockGetProvisioners func(ctx context.Context) ([]*linkedca.Provisioner, error)
MockUpdateProvisioner func(ctx context.Context, prov *linkedca.Provisioner) error
2021-05-25 23:52:06 +00:00
MockDeleteProvisioner func(ctx context.Context, id string) error
2021-05-03 19:48:20 +00:00
2021-05-24 20:38:24 +00:00
MockCreateAdmin func(ctx context.Context, adm *linkedca.Admin) error
MockGetAdmin func(ctx context.Context, id string) (*linkedca.Admin, error)
MockGetAdmins func(ctx context.Context) ([]*linkedca.Admin, error)
MockUpdateAdmin func(ctx context.Context, adm *linkedca.Admin) error
2021-05-25 23:52:06 +00:00
MockDeleteAdmin func(ctx context.Context, id string) error
2021-05-03 19:48:20 +00:00
MockError error
MockRet1 interface{}
}
// CreateProvisioner mock.
2021-05-24 20:38:24 +00:00
func (m *MockDB) CreateProvisioner(ctx context.Context, prov *linkedca.Provisioner) error {
2021-05-03 19:48:20 +00:00
if m.MockCreateProvisioner != nil {
return m.MockCreateProvisioner(ctx, prov)
} else if m.MockError != nil {
return m.MockError
}
return m.MockError
}
// GetProvisioner mock.
2021-05-24 20:38:24 +00:00
func (m *MockDB) GetProvisioner(ctx context.Context, id string) (*linkedca.Provisioner, error) {
2021-05-03 19:48:20 +00:00
if m.MockGetProvisioner != nil {
return m.MockGetProvisioner(ctx, id)
} else if m.MockError != nil {
return nil, m.MockError
}
2021-05-24 20:38:24 +00:00
return m.MockRet1.(*linkedca.Provisioner), m.MockError
2021-05-03 19:48:20 +00:00
}
// GetProvisioners mock
2021-05-24 20:38:24 +00:00
func (m *MockDB) GetProvisioners(ctx context.Context) ([]*linkedca.Provisioner, error) {
2021-05-03 19:48:20 +00:00
if m.MockGetProvisioners != nil {
return m.MockGetProvisioners(ctx)
} else if m.MockError != nil {
return nil, m.MockError
}
2021-05-24 20:38:24 +00:00
return m.MockRet1.([]*linkedca.Provisioner), m.MockError
2021-05-03 19:48:20 +00:00
}
// UpdateProvisioner mock
2021-05-24 20:38:24 +00:00
func (m *MockDB) UpdateProvisioner(ctx context.Context, prov *linkedca.Provisioner) error {
2021-05-03 19:48:20 +00:00
if m.MockUpdateProvisioner != nil {
2021-05-18 23:50:54 +00:00
return m.MockUpdateProvisioner(ctx, prov)
2021-05-03 19:48:20 +00:00
}
return m.MockError
}
2021-05-25 23:52:06 +00:00
// DeleteProvisioner mock
func (m *MockDB) DeleteProvisioner(ctx context.Context, id string) error {
if m.MockDeleteProvisioner != nil {
return m.MockDeleteProvisioner(ctx, id)
}
return m.MockError
}
2021-05-03 19:48:20 +00:00
// CreateAdmin mock
2021-05-24 20:38:24 +00:00
func (m *MockDB) CreateAdmin(ctx context.Context, admin *linkedca.Admin) error {
2021-05-03 19:48:20 +00:00
if m.MockCreateAdmin != nil {
return m.MockCreateAdmin(ctx, admin)
}
return m.MockError
}
// GetAdmin mock.
2021-05-24 20:38:24 +00:00
func (m *MockDB) GetAdmin(ctx context.Context, id string) (*linkedca.Admin, error) {
2021-05-03 19:48:20 +00:00
if m.MockGetAdmin != nil {
return m.MockGetAdmin(ctx, id)
} else if m.MockError != nil {
return nil, m.MockError
}
2021-05-24 20:38:24 +00:00
return m.MockRet1.(*linkedca.Admin), m.MockError
2021-05-03 19:48:20 +00:00
}
// GetAdmins mock
2021-05-24 20:38:24 +00:00
func (m *MockDB) GetAdmins(ctx context.Context) ([]*linkedca.Admin, error) {
2021-05-03 19:48:20 +00:00
if m.MockGetAdmins != nil {
return m.MockGetAdmins(ctx)
} else if m.MockError != nil {
return nil, m.MockError
}
2021-05-24 20:38:24 +00:00
return m.MockRet1.([]*linkedca.Admin), m.MockError
2021-05-03 19:48:20 +00:00
}
// UpdateAdmin mock
2021-05-24 20:38:24 +00:00
func (m *MockDB) UpdateAdmin(ctx context.Context, adm *linkedca.Admin) error {
2021-05-07 00:03:12 +00:00
if m.MockUpdateAdmin != nil {
2021-05-03 19:48:20 +00:00
return m.MockUpdateAdmin(ctx, adm)
}
return m.MockError
}
2021-05-25 23:52:06 +00:00
// DeleteAdmin mock
func (m *MockDB) DeleteAdmin(ctx context.Context, id string) error {
if m.MockDeleteAdmin != nil {
return m.MockDeleteAdmin(ctx, id)
}
return m.MockError
}