Skip to content

Commit

Permalink
service: remove KeyManager and KeyStore
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jul 15, 2021
1 parent 79089a4 commit d1ca564
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 59 deletions.
11 changes: 1 addition & 10 deletions notifier/service/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"

"github.com/google/uuid"

"github.com/quay/clair/v4/notifier"
"github.com/quay/clair/v4/notifier/keymanager"
)

var _ Service = (*Mock)(nil)
Expand All @@ -14,8 +14,6 @@ var _ Service = (*Mock)(nil)
type Mock struct {
Notifications_ func(ctx context.Context, id uuid.UUID, page *notifier.Page) ([]notifier.Notification, notifier.Page, error)
DeleteNotifications_ func(ctx context.Context, id uuid.UUID) error
KeyStore_ func(ctx context.Context) notifier.KeyStore
KeyManager_ func(ctx context.Context) *keymanager.Manager
}

func (m *Mock) Notifications(ctx context.Context, id uuid.UUID, page *notifier.Page) ([]notifier.Notification, notifier.Page, error) {
Expand All @@ -25,10 +23,3 @@ func (m *Mock) Notifications(ctx context.Context, id uuid.UUID, page *notifier.P
func (m *Mock) DeleteNotifications(ctx context.Context, id uuid.UUID) error {
return m.DeleteNotifications_(ctx, id)
}

func (m *Mock) KeyStore(ctx context.Context) notifier.KeyStore {
return m.KeyStore_(ctx)
}
func (m *Mock) KeyManager(ctx context.Context) *keymanager.Manager {
return m.KeyManager_(ctx)
}
61 changes: 12 additions & 49 deletions notifier/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/quay/clair/v4/matcher"
"github.com/quay/clair/v4/notifier"
namqp "github.com/quay/clair/v4/notifier/amqp"
"github.com/quay/clair/v4/notifier/keymanager"
"github.com/quay/clair/v4/notifier/migrations"
"github.com/quay/clair/v4/notifier/postgres"
"github.com/quay/clair/v4/notifier/stomp"
Expand All @@ -41,19 +40,13 @@ type Service interface {
Notifications(ctx context.Context, id uuid.UUID, page *notifier.Page) ([]notifier.Notification, notifier.Page, error)
// Deletes the provided notification id
DeleteNotifications(ctx context.Context, id uuid.UUID) error
// KeyStore returns the notifier's KeyStore.
KeyStore(ctx context.Context) notifier.KeyStore
// KeyManager returns the notifier's KeyManager.
KeyManager(ctx context.Context) *keymanager.Manager
}

var _ Service = (*service)(nil)

// service is a local implementation of a notifier service.
type service struct {
store notifier.Store
keystore notifier.KeyStore
keymanager *keymanager.Manager
store notifier.Store
}

func (s *service) Notifications(ctx context.Context, id uuid.UUID, page *notifier.Page) ([]notifier.Notification, notifier.Page, error) {
Expand All @@ -64,14 +57,6 @@ func (s *service) DeleteNotifications(ctx context.Context, id uuid.UUID) error {
return s.store.SetDeleted(ctx, id)
}

func (s *service) KeyStore(_ context.Context) notifier.KeyStore {
return s.keystore
}

func (s *service) KeyManager(_ context.Context) *keymanager.Manager {
return s.keymanager
}

// Opts configures the notifier service
type Opts struct {
PollInterval time.Duration
Expand All @@ -97,17 +82,11 @@ func New(ctx context.Context, opts Opts) (*service, error) {
)

// initialize store and dist lock pool
store, keystore, lockPool, err := storeInit(ctx, opts)
store, lockPool, err := storeInit(ctx, opts)
if err != nil {
return nil, fmt.Errorf("failed to initialize store and lockpool: %v", err)
}

// kick off key manager
kmgr, err := keyManagerInit(ctx, keystore)
if err != nil {
return nil, fmt.Errorf("failed to initialize key manager: %v", err)
}

// check for test mode
if tm := os.Getenv("NOTIFIER_TEST_MODE"); tm != "" {
zlog.Warn(ctx).
Expand Down Expand Up @@ -144,7 +123,7 @@ func New(ctx context.Context, opts Opts) (*service, error) {
// kick off configured deliverer type
switch {
case opts.Webhook != nil:
if err := webhookDeliveries(ctx, opts, lockPool, store, kmgr); err != nil {
if err := webhookDeliveries(ctx, opts, lockPool, store); err != nil {
return nil, err
}
case opts.AMQP != nil:
Expand All @@ -158,9 +137,7 @@ func New(ctx context.Context, opts Opts) (*service, error) {
}

return &service{
store: store,
keymanager: kmgr,
keystore: keystore,
store: store,
}, nil
}

Expand All @@ -176,24 +153,24 @@ func testModeInit(ctx context.Context, opts *Opts) error {
return nil
}

func storeInit(ctx context.Context, opts Opts) (*postgres.Store, *postgres.KeyStore, *pgxpool.Pool, error) {
func storeInit(ctx context.Context, opts Opts) (*postgres.Store, *pgxpool.Pool, error) {
ctx = baggage.ContextWithValues(ctx,
label.String("component", "notifier/service/storeInit"),
)

cfg, err := pgxpool.ParseConfig(opts.ConnString)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to parse ConnString: %v", err)
return nil, nil, fmt.Errorf("failed to parse ConnString: %v", err)
}
cfg.MaxConns = 30
pool, err := pgxpool.ConnectConfig(ctx, cfg)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create ConnPool: %v", err)
return nil, nil, fmt.Errorf("failed to create ConnPool: %v", err)
}

db, err := sql.Open("pgx", opts.ConnString)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to open db: %v", err)
return nil, nil, fmt.Errorf("failed to open db: %v", err)
}
defer db.Close()

Expand All @@ -204,30 +181,16 @@ func storeInit(ctx context.Context, opts Opts) (*postgres.Store, *postgres.KeySt
migrator.Table = migrations.MigrationTable
err := migrator.Exec(migrate.Up, migrations.Migrations...)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to perform migrations: %w", err)
return nil, nil, fmt.Errorf("failed to perform migrations: %w", err)
}
}

zlog.Info(ctx).Msg("initializing notifier store")
store := postgres.NewStore(pool)
keystore := postgres.NewKeyStore(pool)
return store, keystore, pool, nil
}

func keyManagerInit(ctx context.Context, keystore notifier.KeyStore) (*keymanager.Manager, error) {
ctx = baggage.ContextWithValues(ctx,
label.String("component", "notifier/service/keyManagerInit"),
)

zlog.Debug(ctx).Msg("initializing keymanager")
mgr, err := keymanager.NewManager(ctx, keystore)
if err != nil {
return nil, err
}
return mgr, nil
return store, pool, nil
}

func webhookDeliveries(ctx context.Context, opts Opts, lockPool *pgxpool.Pool, store notifier.Store, keymanager *keymanager.Manager) error {
func webhookDeliveries(ctx context.Context, opts Opts, lockPool *pgxpool.Pool, store notifier.Store) error {
ctx = baggage.ContextWithValues(ctx,
label.String("component", "notifier/service/webhookDeliveries"),
)
Expand All @@ -243,7 +206,7 @@ func webhookDeliveries(ctx context.Context, opts Opts, lockPool *pgxpool.Pool, s
ds := make([]*notifier.Delivery, 0, deliveries)
for i := 0; i < deliveries; i++ {
distLock := pgdl.NewPool(lockPool, 0)
wh, err := webhook.New(conf, opts.Client, keymanager)
wh, err := webhook.New(conf, opts.Client)
if err != nil {
return fmt.Errorf("failed to create webhook deliverer: %v", err)
}
Expand Down

0 comments on commit d1ca564

Please sign in to comment.