From 0f0be2377b4d4b01ed696b58d4cca3f495cc04ae Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 22 Dec 2020 03:26:10 +0300 Subject: [PATCH] [#255] services/audit: Implement audit executor without checks Signed-off-by: Leonard Lyubich --- pkg/services/audit/auditor/context.go | 80 +++++++++++++++++++++++++++ pkg/services/audit/auditor/exec.go | 41 ++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 pkg/services/audit/auditor/context.go create mode 100644 pkg/services/audit/auditor/exec.go diff --git a/pkg/services/audit/auditor/context.go b/pkg/services/audit/auditor/context.go new file mode 100644 index 00000000..3a236199 --- /dev/null +++ b/pkg/services/audit/auditor/context.go @@ -0,0 +1,80 @@ +package auditor + +import ( + "github.com/nspcc-dev/neofs-api-go/pkg/container" + "github.com/nspcc-dev/neofs-node/pkg/services/audit" + "github.com/nspcc-dev/neofs-node/pkg/util/logger" + "go.uber.org/zap" +) + +// Context represents container data audit execution context. +type Context struct { + ContextPrm + + task *audit.Task + + report *audit.Report +} + +// ContextPrm groups components required to conduct data audit checks. +type ContextPrm struct { + log *logger.Logger +} + +// NewContext creates, initializes and returns Context. +func NewContext(prm ContextPrm) *Context { + return &Context{ + ContextPrm: prm, + } +} + +// SetLogger sets logging component. +func (p *ContextPrm) SetLogger(l *logger.Logger) { + if p != nil { + p.log = l + } +} + +// WithTask sets container audit parameters. +func (c *Context) WithTask(t *audit.Task) *Context { + if c != nil { + c.task = t + } + + return c +} + +func (c *Context) containerID() *container.ID { + return c.task.ContainerID() +} + +func (c *Context) init() { + c.report = audit.NewReport(c.containerID()) + + c.log = c.log.With( + zap.Stringer("container ID", c.task.ContainerID()), + ) +} + +func (c *Context) expired() bool { + ctx := c.task.AuditContext() + + select { + case <-ctx.Done(): + c.log.Debug("audit context is done", + zap.String("error", ctx.Err().Error()), + ) + + return true + default: + return false + } +} + +func (c *Context) writeReport() { + c.log.Debug("writing audit report...") + + if err := c.task.Reporter().WriteReport(c.report); err != nil { + c.log.Error("could not write audit report") + } +} diff --git a/pkg/services/audit/auditor/exec.go b/pkg/services/audit/auditor/exec.go new file mode 100644 index 00000000..90d6d76f --- /dev/null +++ b/pkg/services/audit/auditor/exec.go @@ -0,0 +1,41 @@ +package auditor + +import ( + "fmt" +) + +// Execute audits container data. +func (c *Context) Execute() { + c.init() + + for _, check := range []struct { + name string + exec func() + }{ + {name: "PoR", exec: c.executePoR}, + {name: "PoP", exec: c.executePoP}, + {name: "PDP", exec: c.executePDP}, + } { + c.log.Debug(fmt.Sprintf("executing %s check...", check.name)) + + if c.expired() { + break + } + + check.exec() + } + + c.writeReport() +} + +func (c *Context) executePoR() { + // TODO: implement me +} + +func (c *Context) executePoP() { + // TODO: implement me +} + +func (c *Context) executePDP() { + // TODO: implement me +}