better configuration for pp service

Signed-off-by: jkoberg <jkoberg@owncloud.com>
This commit is contained in:
jkoberg
2023-01-25 12:31:47 +01:00
parent 85b84bdeaf
commit 22f20b2b2e
5 changed files with 79 additions and 27 deletions

View File

@@ -0,0 +1,5 @@
Enhancement: Better config for postprocessing service
We want postprocessing service to be individually configurable. We achieve this by allowing to define a list of postprocessing steps with the `POSTPROCESSING_STEPS` envvar
https://github.com/owncloud/ocis/pull/5457

View File

@@ -20,12 +20,25 @@ When postprocessing has been enabled, configuring any postprocessing step will r
## Postprocessing Steps
As of now, `ocis` allows two different postprocessing steps to be enabled via an environment variable.
`ocis` allows setting the order and amount of postprocessing steps via the `POSTPROCESSING_STEPS` envvar. It expects a comma seperated list of steps that should be executed. Known to the system are `virusscan` and `delay`. It is allowed to set custom steps.
### Virus Scanning
To enable virus scanning as a postprocessing step after uploading a file, the environment variable `POSTPROCESSING_VIRUSSCAN` needs to be set to ` true`. As a result, each uploaded file gets virus scanned as part of the postprocessing steps. Note that the `antivirus` service is required to be enabled and configured for this to work.
To enable virus scanning as a postprocessing step after uploading a file, the environment variable `POSTPROCESSING_STEPS` needs to contain the word `virusscan`. As a result, each uploaded file gets virus scanned as part of the postprocessing steps. Note that the `antivirus` service is required to be enabled and configured for this to work.
### Delay
Though this is for development purposes only and NOT RECOMMENDED on production systems, setting the environment variable `POSTPROCESSING_DELAY` to a duration not equal to zero will add a delay step with the configured amount of time. ocis will continue postprocessing the file after the configured delay.
Though this is for development purposes only and NOT RECOMMENDED on production systems, setting the environment variable `POSTPROCESSING_DELAY` to a duration not equal to zero will add a delay step with the configured amount of time. ocis will continue postprocessing the file after the configured delay. Use the enviroment variable `POSTPROCESSING_STEPS` and the keyword `delay` if you have multiple postprocessing steps and want to define their order. If `POSTPROCESSING_DELAY` is set but `delay` is not contained in `POSTPROCESSING_STEPS` it will be added as last postprocessing step.
### Custom Postprocessing Steps
Using the envvar `POSTPROCESSING_STEPS` custom postprocessing steps can be added. Any word can be used as step name but be careful not to clash with exising steps `virusscan` and `delay`.
#### Prerequisites
For using custom postprocessing steps you need a custom service listening to the configured event system (see `General Prerequisites`)
#### Workflow
When setting a custom postprocessing step (eg. `"customstep"`) the postprocessing service will eventually sent an event during postprocessing. The event will be of type `StartPostprocessingStep` with its field `StepToStart` set to `"customstep"`. When the custom service receives this event it can savely execute its actions, postprocessing service will wait until it has finished its work. The event contains further information (filename, executing user, size, ...) and also required tokens and urls to download the file in case byte inspection is necessary.
Once the custom service has finished its work it should sent an event of type `PostprocessingFinished` via the configured events system. This event needs to contain a `FinishedStep` field set to `"customstep"`. It also must contain the outcome of the step, which can be one of "delete" (abort postprocessing, delete the file), "abort" (abort postprocessing, keep the file) and "continue" (continue postprocessing, this is the success case).
See https://github.com/cs3org/reva/blob/edge/pkg/events/postprocessing.go for up-to-date information of reserved step names and event definitons.

View File

@@ -23,6 +23,7 @@ type Config struct {
// Postprocessing definces the config options for the postprocessing service.
type Postprocessing struct {
Events Events `yaml:"events"`
Steps []string `yaml:"steps" env:"POSTPROCESSING_STEPS" desc:"A comma seperated list of postprocessing steps. Known to the system are virusscan and delay. Custom steps are allowed. See README.md for instructions."`
Virusscan bool `yaml:"virusscan" env:"POSTPROCESSING_VIRUSSCAN" desc:"After uploading a file but before making it available for download, virus scanning the file can be enabled. Needs as prerequisite the antivirus service to be enabled and configured."`
Delayprocessing time.Duration `yaml:"delayprocessing" env:"POSTPROCESSING_DELAY" desc:"After uploading a file but before making it available for download, a delay step can be added. Intended for developing purposes only. The duration can be set as number followed by a unit identifier like s, m or h."`
}

View File

@@ -6,7 +6,6 @@ import (
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/owncloud/ocis/v2/services/postprocessing/pkg/config"
)
// Postprocessing handles postprocessing of a file
@@ -18,22 +17,22 @@ type Postprocessing struct {
filename string
filesize uint64
resourceID *provider.ResourceId
c config.Postprocessing
steps []events.Postprocessingstep
delay time.Duration
}
// New returns a new postprocessing instance
func New(uploadID string, uploadURL string, user *user.User, filename string, filesize uint64, resourceID *provider.ResourceId, c config.Postprocessing) *Postprocessing {
func New(uploadID string, uploadURL string, user *user.User, filename string, filesize uint64, resourceID *provider.ResourceId, steps []events.Postprocessingstep, delay time.Duration) *Postprocessing {
return &Postprocessing{
id: uploadID,
url: uploadURL,
u: user,
m: make(map[events.Postprocessingstep]interface{}),
c: c,
filename: filename,
filesize: filesize,
resourceID: resourceID,
steps: getSteps(c),
steps: steps,
delay: delay,
}
}
@@ -64,7 +63,7 @@ func (pp *Postprocessing) Virusscan(ev events.VirusscanFinished) interface{} {
// Delay will sleep the configured time then continue
func (pp *Postprocessing) Delay(ev events.StartPostprocessingStep) interface{} {
pp.m[events.PPStepDelay] = ev
time.Sleep(pp.c.Delayprocessing)
time.Sleep(pp.delay)
return pp.next(events.PPStepDelay)
}
@@ -99,20 +98,3 @@ func (pp *Postprocessing) finished(outcome events.PostprocessingOutcome) events.
Outcome: outcome,
}
}
func getSteps(c config.Postprocessing) []events.Postprocessingstep {
// NOTE: first version only contains very basic configuration options
// But we aim for a system where postprocessing steps and their order can be configured per space
// ideally by the spaceadmin itself
// We need to iterate over configuring PP service when we see fit
var steps []events.Postprocessingstep
if c.Delayprocessing != 0 {
steps = append(steps, events.PPStepDelay)
}
if c.Virusscan {
steps = append(steps, events.PPStepAntivirus)
}
return steps
}

View File

@@ -1,6 +1,9 @@
package service
import (
"fmt"
"strings"
"github.com/cs3org/reva/v2/pkg/events"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/postprocessing/pkg/config"
@@ -12,6 +15,7 @@ type PostprocessingService struct {
log log.Logger
events <-chan interface{}
pub events.Publisher
steps []events.Postprocessingstep
c config.Postprocessing
}
@@ -31,6 +35,7 @@ func NewPostprocessingService(stream events.Stream, logger log.Logger, c config.
log: logger,
events: evs,
pub: stream,
steps: getSteps(c),
c: c,
}, nil
}
@@ -42,7 +47,7 @@ func (pps *PostprocessingService) Run() error {
var next interface{}
switch ev := e.(type) {
case events.BytesReceived:
pp := postprocessing.New(ev.UploadID, ev.URL, ev.ExecutingUser, ev.Filename, ev.Filesize, ev.ResourceID, pps.c)
pp := postprocessing.New(ev.UploadID, ev.URL, ev.ExecutingUser, ev.Filename, ev.Filesize, ev.ResourceID, pps.steps, pps.c.Delayprocessing)
current[ev.UploadID] = pp
next = pp.Init(ev)
case events.VirusscanFinished:
@@ -73,3 +78,49 @@ func (pps *PostprocessingService) Run() error {
}
return nil
}
func getSteps(c config.Postprocessing) []events.Postprocessingstep {
// NOTE: improved version only allows configuring order of postprocessing steps
// But we aim for a system where postprocessing steps can be configured per space, ideally by the spaceadmin itself
// We need to iterate over configuring PP service when we see fit
var steps []events.Postprocessingstep
for _, s := range c.Steps {
steps = append(steps, events.Postprocessingstep(s))
}
if c.Virusscan {
if !contains(steps, events.PPStepAntivirus) {
steps = append(steps, events.PPStepAntivirus)
fmt.Printf("ATTENTION: POSTPROCESSING_VIRUSSCAN is deprecated. Use `POSTPROCESSING_STEPS=%v` in the future\n", join(steps))
}
}
if c.Delayprocessing != 0 {
if !contains(steps, events.PPStepDelay) {
if len(steps) > 0 {
fmt.Printf("Added delay step to the list of postprocessing steps. NOTE: Use envvar `POSTPROCESSING_STEPS=%v` to suppress this message and choose the order of postprocessing steps.\n", join(append(steps, events.PPStepDelay)))
}
steps = append(steps, events.PPStepDelay)
}
}
return steps
}
func contains(all []events.Postprocessingstep, candidate events.Postprocessingstep) bool {
for _, s := range all {
if s == candidate {
return true
}
}
return false
}
func join(all []events.Postprocessingstep) string {
var slice []string
for _, s := range all {
slice = append(slice, string(s))
}
return strings.Join(slice, ",")
}