2020-12-18 09:27:19 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
2020-12-22 00:28:42 +00:00
|
|
|
"context"
|
2021-03-13 15:22:21 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-12-23 15:07:10 +00:00
|
|
|
"time"
|
2020-12-22 00:28:42 +00:00
|
|
|
|
2020-12-18 13:30:13 +00:00
|
|
|
SDKClient "github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2020-12-21 08:40:30 +00:00
|
|
|
wrapContainer "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
|
|
|
|
wrapNetmap "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper"
|
2020-12-18 09:27:19 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2021-05-20 15:17:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-12-22 00:28:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/audit"
|
2020-12-18 09:27:19 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Indexer is a callback interface for inner ring global state.
|
|
|
|
Indexer interface {
|
2021-03-23 14:14:28 +00:00
|
|
|
InnerRingIndex() int
|
2021-02-21 06:28:53 +00:00
|
|
|
InnerRingSize() int
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 13:30:13 +00:00
|
|
|
// NeoFSClientCache is an interface for cache of neofs RPC clients
|
|
|
|
NeoFSClientCache interface {
|
2021-06-22 15:34:44 +00:00
|
|
|
Get(network.AddressGroup) (SDKClient.Client, error)
|
2020-12-18 13:30:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
TaskManager interface {
|
|
|
|
PushTask(*audit.Task) error
|
2020-12-22 12:34:38 +00:00
|
|
|
|
|
|
|
// Must skip all tasks planned for execution and
|
|
|
|
// return their number.
|
|
|
|
Reset() int
|
2020-12-22 00:28:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 09:27:19 +00:00
|
|
|
// Processor of events related with data audit.
|
|
|
|
Processor struct {
|
2021-05-31 11:50:11 +00:00
|
|
|
log *zap.Logger
|
|
|
|
pool *ants.Pool
|
|
|
|
irList Indexer
|
|
|
|
clientCache NeoFSClientCache
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
searchTimeout time.Duration
|
2020-12-21 08:40:30 +00:00
|
|
|
|
|
|
|
containerClient *wrapContainer.Wrapper
|
|
|
|
netmapClient *wrapNetmap.Wrapper
|
2020-12-22 00:28:42 +00:00
|
|
|
|
|
|
|
taskManager TaskManager
|
|
|
|
reporter audit.Reporter
|
|
|
|
prevAuditCanceler context.CancelFunc
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params of the processor constructor.
|
|
|
|
Params struct {
|
2021-05-31 11:50:11 +00:00
|
|
|
Log *zap.Logger
|
|
|
|
NetmapClient *wrapNetmap.Wrapper
|
|
|
|
ContainerClient *wrapContainer.Wrapper
|
|
|
|
IRList Indexer
|
|
|
|
ClientCache NeoFSClientCache
|
|
|
|
RPCSearchTimeout time.Duration
|
|
|
|
TaskManager TaskManager
|
|
|
|
Reporter audit.Reporter
|
|
|
|
Key *ecdsa.PrivateKey
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-12-22 00:28:42 +00:00
|
|
|
type epochAuditReporter struct {
|
|
|
|
epoch uint64
|
|
|
|
|
|
|
|
rep audit.Reporter
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// ProcessorPoolSize limits pool size for audit Processor. Processor manages
|
|
|
|
// audit tasks and fills queue for next epoch. This process must not be interrupted
|
|
|
|
// by new audit epoch, so we limit pool size for processor to one.
|
2020-12-18 09:27:19 +00:00
|
|
|
const ProcessorPoolSize = 1
|
|
|
|
|
|
|
|
// New creates audit processor instance.
|
|
|
|
func New(p *Params) (*Processor, error) {
|
|
|
|
switch {
|
|
|
|
case p.Log == nil:
|
|
|
|
return nil, errors.New("ir/audit: logger is not set")
|
|
|
|
case p.IRList == nil:
|
|
|
|
return nil, errors.New("ir/audit: global state is not set")
|
2020-12-18 13:30:13 +00:00
|
|
|
case p.ClientCache == nil:
|
|
|
|
return nil, errors.New("ir/audit: neofs RPC client cache is not set")
|
2020-12-23 15:07:10 +00:00
|
|
|
case p.TaskManager == nil:
|
|
|
|
return nil, errors.New("ir/audit: audit task manager is not set")
|
|
|
|
case p.Reporter == nil:
|
|
|
|
return nil, errors.New("ir/audit: audit result reporter is not set")
|
2021-03-13 15:22:21 +00:00
|
|
|
case p.Key == nil:
|
|
|
|
return nil, errors.New("ir/audit: signing key is not set")
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := ants.NewPool(ProcessorPoolSize, ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("ir/audit: can't create worker pool: %w", err)
|
2020-12-18 09:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Processor{
|
|
|
|
log: p.Log,
|
|
|
|
pool: pool,
|
2021-05-31 11:50:11 +00:00
|
|
|
containerClient: p.ContainerClient,
|
2020-12-18 09:27:19 +00:00
|
|
|
irList: p.IRList,
|
2020-12-18 13:30:13 +00:00
|
|
|
clientCache: p.ClientCache,
|
2021-03-13 15:22:21 +00:00
|
|
|
key: p.Key,
|
2020-12-23 15:07:10 +00:00
|
|
|
searchTimeout: p.RPCSearchTimeout,
|
2021-05-31 11:50:11 +00:00
|
|
|
netmapClient: p.NetmapClient,
|
2020-12-22 00:28:42 +00:00
|
|
|
taskManager: p.TaskManager,
|
|
|
|
reporter: p.Reporter,
|
|
|
|
prevAuditCanceler: func() {},
|
2020-12-18 09:27:19 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationParsers for the 'event.Listener' event producer.
|
|
|
|
func (ap *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
|
2020-12-18 09:27:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
// ListenerNotificationHandlers for the 'event.Listener' event producer.
|
|
|
|
func (ap *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
|
2020-12-18 09:27:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimersHandlers for the 'Timers' event producer.
|
2021-08-12 15:24:17 +00:00
|
|
|
func (ap *Processor) TimersHandlers() []event.NotificationHandlerInfo {
|
2020-12-18 09:27:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartAuditHandler for the internal event producer.
|
|
|
|
func (ap *Processor) StartAuditHandler() event.Handler {
|
|
|
|
return ap.handleNewAuditRound
|
|
|
|
}
|
2020-12-22 00:28:42 +00:00
|
|
|
|
|
|
|
func (r *epochAuditReporter) WriteReport(rep *audit.Report) error {
|
|
|
|
res := rep.Result()
|
|
|
|
res.SetAuditEpoch(r.epoch)
|
|
|
|
|
|
|
|
return r.rep.WriteReport(rep)
|
|
|
|
}
|