diff --git a/ocis-pkg/config/config.go b/ocis-pkg/config/config.go index 2aa26107cf2..f02ae564ccf 100644 --- a/ocis-pkg/config/config.go +++ b/ocis-pkg/config/config.go @@ -20,6 +20,7 @@ import ( notifications "github.com/owncloud/ocis/v2/services/notifications/pkg/config" ocdav "github.com/owncloud/ocis/v2/services/ocdav/pkg/config" ocs "github.com/owncloud/ocis/v2/services/ocs/pkg/config" + postprocessing "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" proxy "github.com/owncloud/ocis/v2/services/proxy/pkg/config" search "github.com/owncloud/ocis/v2/services/search/pkg/config" settings "github.com/owncloud/ocis/v2/services/settings/pkg/config" @@ -72,34 +73,35 @@ type Config struct { AdminUserID string `yaml:"admin_user_id" env:"OCIS_ADMIN_USER_ID" desc:"ID of a user, that should receive admin privileges."` Runtime Runtime `yaml:"runtime"` - AppProvider *appProvider.Config `yaml:"app_provider"` - AppRegistry *appRegistry.Config `yaml:"app_registry"` - Audit *audit.Config `yaml:"audit"` - AuthBasic *authbasic.Config `yaml:"auth_basic"` - AuthBearer *authbearer.Config `yaml:"auth_bearer"` - AuthMachine *authmachine.Config `yaml:"auth_machine"` - Frontend *frontend.Config `yaml:"frontend"` - Gateway *gateway.Config `yaml:"gateway"` - Graph *graph.Config `yaml:"graph"` - GraphExplorer *graphExplorer.Config `yaml:"graph_explorer"` - Groups *groups.Config `yaml:"groups"` - IDM *idm.Config `yaml:"idm"` - IDP *idp.Config `yaml:"idp"` - Nats *nats.Config `yaml:"nats"` - Notifications *notifications.Config `yaml:"notifications"` - OCDav *ocdav.Config `yaml:"ocdav"` - OCS *ocs.Config `yaml:"ocs"` - Proxy *proxy.Config `yaml:"proxy"` - Settings *settings.Config `yaml:"settings"` - Sharing *sharing.Config `yaml:"sharing"` - StorageSystem *storagesystem.Config `yaml:"storage_system"` - StoragePublicLink *storagepublic.Config `yaml:"storage_public"` - StorageShares *storageshares.Config `yaml:"storage_shares"` - StorageUsers *storageusers.Config `yaml:"storage_users"` - Store *store.Config `yaml:"store"` - Thumbnails *thumbnails.Config `yaml:"thumbnails"` - Users *users.Config `yaml:"users"` - Web *web.Config `yaml:"web"` - WebDAV *webdav.Config `yaml:"webdav"` - Search *search.Config `yaml:"search"` + AppProvider *appProvider.Config `yaml:"app_provider"` + AppRegistry *appRegistry.Config `yaml:"app_registry"` + Audit *audit.Config `yaml:"audit"` + AuthBasic *authbasic.Config `yaml:"auth_basic"` + AuthBearer *authbearer.Config `yaml:"auth_bearer"` + AuthMachine *authmachine.Config `yaml:"auth_machine"` + Frontend *frontend.Config `yaml:"frontend"` + Gateway *gateway.Config `yaml:"gateway"` + Graph *graph.Config `yaml:"graph"` + GraphExplorer *graphExplorer.Config `yaml:"graph_explorer"` + Groups *groups.Config `yaml:"groups"` + IDM *idm.Config `yaml:"idm"` + IDP *idp.Config `yaml:"idp"` + Nats *nats.Config `yaml:"nats"` + Notifications *notifications.Config `yaml:"notifications"` + OCDav *ocdav.Config `yaml:"ocdav"` + OCS *ocs.Config `yaml:"ocs"` + Postprocessing *postprocessing.Config `yaml:"postprocessing"` + Proxy *proxy.Config `yaml:"proxy"` + Settings *settings.Config `yaml:"settings"` + Sharing *sharing.Config `yaml:"sharing"` + StorageSystem *storagesystem.Config `yaml:"storage_system"` + StoragePublicLink *storagepublic.Config `yaml:"storage_public"` + StorageShares *storageshares.Config `yaml:"storage_shares"` + StorageUsers *storageusers.Config `yaml:"storage_users"` + Store *store.Config `yaml:"store"` + Thumbnails *thumbnails.Config `yaml:"thumbnails"` + Users *users.Config `yaml:"users"` + Web *web.Config `yaml:"web"` + WebDAV *webdav.Config `yaml:"webdav"` + Search *search.Config `yaml:"search"` } diff --git a/services/postprocessing/Makefile b/services/postprocessing/Makefile new file mode 100644 index 00000000000..0bdc1157223 --- /dev/null +++ b/services/postprocessing/Makefile @@ -0,0 +1,37 @@ +SHELL := bash +NAME := postprocessing + +include ../../.make/recursion.mk + +############ tooling ############ +ifneq (, $(shell command -v go 2> /dev/null)) # suppress `command not found warnings` for non go targets in CI +include ../../.bingo/Variables.mk +endif + +############ go tooling ############ +include ../../.make/go.mk + +############ release ############ +include ../../.make/release.mk + +############ docs generate ############ +include ../../.make/docs.mk + +.PHONY: docs-generate +docs-generate: config-docs-generate + +############ generate ############ +include ../../.make/generate.mk + +.PHONY: ci-go-generate +ci-go-generate: # CI runs ci-node-generate automatically before this target + +.PHONY: ci-node-generate +ci-node-generate: + +############ licenses ############ +.PHONY: ci-node-check-licenses +ci-node-check-licenses: + +.PHONY: ci-node-save-licenses +ci-node-save-licenses: diff --git a/services/postprocessing/cmd/postprocessing/blubb.txt b/services/postprocessing/cmd/postprocessing/blubb.txt new file mode 100644 index 00000000000..2fb06510d1b Binary files /dev/null and b/services/postprocessing/cmd/postprocessing/blubb.txt differ diff --git a/services/postprocessing/cmd/postprocessing/main.go b/services/postprocessing/cmd/postprocessing/main.go new file mode 100644 index 00000000000..ddc191e3a39 --- /dev/null +++ b/services/postprocessing/cmd/postprocessing/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "os" + + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/command" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config/defaults" +) + +func main() { + if err := command.Execute(defaults.DefaultConfig()); err != nil { + os.Exit(1) + } +} diff --git a/services/postprocessing/pkg/command/health.go b/services/postprocessing/pkg/command/health.go new file mode 100644 index 00000000000..0a9fe13bdf5 --- /dev/null +++ b/services/postprocessing/pkg/command/health.go @@ -0,0 +1,18 @@ +package command + +import ( + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" + "github.com/urfave/cli/v2" +) + +// Health is the entrypoint for the health command. +func Health(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "health", + Usage: "Check health status", + Action: func(c *cli.Context) error { + // Not implemented + return nil + }, + } +} diff --git a/services/postprocessing/pkg/command/root.go b/services/postprocessing/pkg/command/root.go new file mode 100644 index 00000000000..20106c6855f --- /dev/null +++ b/services/postprocessing/pkg/command/root.go @@ -0,0 +1,59 @@ +package command + +import ( + "context" + "os" + + "github.com/owncloud/ocis/v2/ocis-pkg/clihelper" + ociscfg "github.com/owncloud/ocis/v2/ocis-pkg/config" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" + "github.com/thejerf/suture/v4" + "github.com/urfave/cli/v2" +) + +// GetCommands provides all commands for this service +func GetCommands(cfg *config.Config) cli.Commands { + return []*cli.Command{ + // start this service + Server(cfg), + + // interaction with this service + + // infos about this service + Health(cfg), + Version(cfg), + } +} + +// Execute is the entry point for the postprocessing command. +func Execute(cfg *config.Config) error { + app := clihelper.DefaultApp(&cli.App{ + Name: "postprocessing", + Usage: "starts postprocessing service", + Commands: GetCommands(cfg), + }) + + return app.Run(os.Args) +} + +// SutureService allows for the postprocessing command to be embedded and supervised by a suture supervisor tree. +type SutureService struct { + cfg *config.Config +} + +// NewSutureService creates a new postprocessing.SutureService +func NewSutureService(cfg *ociscfg.Config) suture.Service { + cfg.Notifications.Commons = cfg.Commons + return SutureService{ + cfg: cfg.Postprocessing, + } +} + +func (s SutureService) Serve(ctx context.Context) error { + s.cfg.Context = ctx + if err := Execute(s.cfg); err != nil { + return err + } + + return nil +} diff --git a/services/postprocessing/pkg/command/server.go b/services/postprocessing/pkg/command/server.go new file mode 100644 index 00000000000..7c4067eccb2 --- /dev/null +++ b/services/postprocessing/pkg/command/server.go @@ -0,0 +1,48 @@ +package command + +import ( + "fmt" + "os" + + "github.com/cs3org/reva/v2/pkg/events/server" + "github.com/go-micro/plugins/v4/events/natsjs" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config/parser" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/logging" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/service" + "github.com/urfave/cli/v2" +) + +// Server is the entrypoint for the server command. +func Server(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "server", + Usage: fmt.Sprintf("start %s extension without runtime (unsupervised mode)", cfg.Service.Name), + Category: "server", + Before: func(c *cli.Context) error { + err := parser.ParseConfig(cfg) + if err != nil { + fmt.Printf("%v", err) + os.Exit(1) + } + return err + }, + Action: func(c *cli.Context) error { + logger := logging.Configure(cfg.Service.Name, cfg.Log) + + evtsCfg := cfg.Postprocessing.Events + client, err := server.NewNatsStream( + natsjs.Address(evtsCfg.Endpoint), + natsjs.ClusterID(evtsCfg.Cluster), + ) + if err != nil { + return err + } + svc, err := service.NewPostprocessingService(client, logger, cfg.Postprocessing) + if err != nil { + return err + } + return svc.Run() + }, + } +} diff --git a/services/postprocessing/pkg/command/version.go b/services/postprocessing/pkg/command/version.go new file mode 100644 index 00000000000..5d52c94a8ca --- /dev/null +++ b/services/postprocessing/pkg/command/version.go @@ -0,0 +1,19 @@ +package command + +import ( + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" + "github.com/urfave/cli/v2" +) + +// Version prints the service versions of all running instances. +func Version(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "version", + Usage: "print the version of this binary and the running extension instances", + Category: "info", + Action: func(c *cli.Context) error { + // not implemented + return nil + }, + } +} diff --git a/services/postprocessing/pkg/config/config.go b/services/postprocessing/pkg/config/config.go new file mode 100644 index 00000000000..bf2aa0d7261 --- /dev/null +++ b/services/postprocessing/pkg/config/config.go @@ -0,0 +1,48 @@ +package config + +import ( + "context" + + "github.com/owncloud/ocis/v2/ocis-pkg/shared" +) + +// Config combines all available configuration parts. +type Config struct { + Commons *shared.Commons `yaml:"-"` // don't use this directly as configuration for a service + + Service Service `yaml:"-"` + + Log *Log `yaml:"log"` + Debug Debug `yaml:"debug"` + + Postprocessing Postprocessing `yaml:"postprocessing"` + + Context context.Context `yaml:"-"` +} + +// Postprocessing definces the config options for the postprocessing service. +type Postprocessing struct { + Events Events `yaml:"events"` + + Steps []string + // TODO: optional steps? + + // TODO: still needed? + // RevaGateway string `yaml:"reva_gateway" env:"REVA_GATEWAY;NOTIFICATIONS_REVA_GATEWAY" desc:"CS3 gateway used to look up user metadata"` + // MachineAuthAPIKey string `yaml:"machine_auth_api_key" env:"OCIS_MACHINE_AUTH_API_KEY;NOTIFICATIONS_MACHINE_AUTH_API_KEY" desc:"Machine auth API key used to validate internal requests necessary for the access to resources from other services."` +} + +// SMTP combines the smtp configuration options. +type SMTP struct { + Host string `yaml:"smtp_host" env:"NOTIFICATIONS_SMTP_HOST" desc:"SMTP host, to connect to."` + Port string `yaml:"smtp_port" env:"NOTIFICATIONS_SMTP_PORT" desc:"Port of the SMTP host, to connect to."` + Sender string `yaml:"smtp_sender" env:"NOTIFICATIONS_SMTP_SENDER" desc:"Sender of emails, that will be sent."` + Password string `yaml:"smtp_password" env:"NOTIFICATIONS_SMTP_PASSWORD" desc:"Password of the SMTP host, to connect to."` +} + +// Events combines the configuration options for the event bus. +type Events struct { + Endpoint string `yaml:"endpoint" env:"NOTIFICATIONS_EVENTS_ENDPOINT" desc:"Endpoint of the event system."` + Cluster string `yaml:"cluster" env:"NOTIFICATIONS_EVENTS_CLUSTER" desc:"Cluster ID of the event system."` + ConsumerGroup string `yaml:"group" env:"NOTIFICATIONS_EVENTS_GROUP" desc:"Name of the event group / queue on the event system."` +} diff --git a/services/postprocessing/pkg/config/debug.go b/services/postprocessing/pkg/config/debug.go new file mode 100644 index 00000000000..8eac6d526e6 --- /dev/null +++ b/services/postprocessing/pkg/config/debug.go @@ -0,0 +1,9 @@ +package config + +// Debug defines the available debug configuration. +type Debug struct { + Addr string `yaml:"addr" env:"NOTIFICATIONS_DEBUG_ADDR" desc:"Bind address of the debug server, where metrics, health, config and debug endpoints will be exposed."` + Token string `yaml:"token" env:"NOTIFICATIONS_DEBUG_TOKEN" desc:"Token to secure the metrics endpoint"` + Pprof bool `yaml:"pprof" env:"NOTIFICATIONS_DEBUG_PPROF" desc:"Enables pprof, which can be used for profiling"` + Zpages bool `yaml:"zpages" env:"NOTIFICATIONS_DEBUG_ZPAGES" desc:"Enables zpages, which can be used for collecting and viewing in-memory traces."` +} diff --git a/services/postprocessing/pkg/config/defaults/defaultconfig.go b/services/postprocessing/pkg/config/defaults/defaultconfig.go new file mode 100644 index 00000000000..972c2018c4a --- /dev/null +++ b/services/postprocessing/pkg/config/defaults/defaultconfig.go @@ -0,0 +1,51 @@ +package defaults + +import ( + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" +) + +func FullDefaultConfig() *config.Config { + cfg := DefaultConfig() + EnsureDefaults(cfg) + Sanitize(cfg) + return cfg +} + +// NOTE: Most of this configuration is not needed to keep it as simple as possible +// TODO: Clean up unneeded configuration + +func DefaultConfig() *config.Config { + return &config.Config{ + Debug: config.Debug{ + Addr: "127.0.0.1:9174", + }, + Service: config.Service{ + Name: "postprocessing", + }, + Postprocessing: config.Postprocessing{ + Events: config.Events{ + Endpoint: "127.0.0.1:9233", + Cluster: "ocis-cluster", + ConsumerGroup: "postprocessing", + }, + }, + } +} + +func EnsureDefaults(cfg *config.Config) { + // provide with defaults for shared logging, since we need a valid destination address for BindEnv. + if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil { + cfg.Log = &config.Log{ + Level: cfg.Commons.Log.Level, + Pretty: cfg.Commons.Log.Pretty, + Color: cfg.Commons.Log.Color, + File: cfg.Commons.Log.File, + } + } else if cfg.Log == nil { + cfg.Log = &config.Log{} + } +} + +func Sanitize(cfg *config.Config) { + // nothing to sanitize here atm +} diff --git a/services/postprocessing/pkg/config/log.go b/services/postprocessing/pkg/config/log.go new file mode 100644 index 00000000000..5f13153ce99 --- /dev/null +++ b/services/postprocessing/pkg/config/log.go @@ -0,0 +1,9 @@ +package config + +// Log defines the available log configuration. +type Log struct { + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;NOTIFICATIONS_LOG_LEVEL" desc:"The log level. Valid values are: \"panic\", \"fatal\", \"error\", \"warn\", \"info\", \"debug\", \"trace\"."` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;NOTIFICATIONS_LOG_PRETTY" desc:"Activates pretty log output."` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;NOTIFICATIONS_LOG_COLOR" desc:"Activates colorized log output."` + File string `mapstructure:"file" env:"OCIS_LOG_FILE;NOTIFICATIONS_LOG_FILE" desc:"The path to the log file. Activates logging to this file if set."` +} diff --git a/services/postprocessing/pkg/config/parser/parse.go b/services/postprocessing/pkg/config/parser/parse.go new file mode 100644 index 00000000000..03441f9415f --- /dev/null +++ b/services/postprocessing/pkg/config/parser/parse.go @@ -0,0 +1,37 @@ +package parser + +import ( + "errors" + + ociscfg "github.com/owncloud/ocis/v2/ocis-pkg/config" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config/defaults" + + "github.com/owncloud/ocis/v2/ocis-pkg/config/envdecode" +) + +// ParseConfig loads configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + defaults.EnsureDefaults(cfg) + + // load all env variables relevant to the config in the current context. + if err := envdecode.Decode(cfg); err != nil { + // no environment variable set for this config is an expected "error" + if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) { + return err + } + } + + defaults.Sanitize(cfg) + + return Validate(cfg) +} + +func Validate(cfg *config.Config) error { + return nil +} diff --git a/services/postprocessing/pkg/config/service.go b/services/postprocessing/pkg/config/service.go new file mode 100644 index 00000000000..d1eac383f0b --- /dev/null +++ b/services/postprocessing/pkg/config/service.go @@ -0,0 +1,6 @@ +package config + +// Service defines the available service configuration. +type Service struct { + Name string `yaml:"-"` +} diff --git a/services/postprocessing/pkg/logging/logging.go b/services/postprocessing/pkg/logging/logging.go new file mode 100644 index 00000000000..ae5a674a0ea --- /dev/null +++ b/services/postprocessing/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/v2/ocis-pkg/log" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" +) + +// LoggerFromConfig initializes a service-specific logger instance. +func Configure(name string, cfg *config.Log) log.Logger { + return log.NewLogger( + log.Name(name), + log.Level(cfg.Level), + log.Pretty(cfg.Pretty), + log.Color(cfg.Color), + log.File(cfg.File), + ) +} diff --git a/services/postprocessing/pkg/postprocessing/postprocessing.go b/services/postprocessing/pkg/postprocessing/postprocessing.go new file mode 100644 index 00000000000..d224ceea8b1 --- /dev/null +++ b/services/postprocessing/pkg/postprocessing/postprocessing.go @@ -0,0 +1,57 @@ +package postprocessing + +import ( + "github.com/cs3org/reva/v2/pkg/events" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" +) + +// Postprocessing handles postprocessing of a file +type Postprocessing struct { + id string + url string + m map[events.Postprocessingstep]interface{} + c config.Postprocessing +} + +// New returns a new postprocessing instance +func New(uploadID string, uploadURL string, c config.Postprocessing) *Postprocessing { + return &Postprocessing{ + id: uploadID, + url: uploadURL, + m: make(map[events.Postprocessingstep]interface{}), + c: c, + } +} + +// Init is the first step of the postprocessing +func (pp *Postprocessing) Init(ev events.BytesReceived) interface{} { + pp.m["init"] = ev + return events.StartPostprocessingStep{ + UploadID: pp.id, + URL: pp.url, + StepToStart: events.PPStepAntivirus, // TODO: make order configurable + } + +} + +// Virusscan is the virusscanning step of the postprocessing +func (pp *Postprocessing) Virusscan(ev events.VirusscanFinished) interface{} { + pp.m["virusscan"] = ev + + var action string + switch { + case ev.Infected: + action = "delete" + case ev.Error != nil: + action = "abort" + default: + action = "continue" + } + + return events.PostprocessingFinished{ + UploadID: pp.id, + Result: pp.m, + Action: action, + } + +} diff --git a/services/postprocessing/pkg/service/service.go b/services/postprocessing/pkg/service/service.go new file mode 100644 index 00000000000..871be73540e --- /dev/null +++ b/services/postprocessing/pkg/service/service.go @@ -0,0 +1,64 @@ +package service + +import ( + "github.com/cs3org/reva/v2/pkg/events" + "github.com/owncloud/ocis/v2/ocis-pkg/log" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/config" + "github.com/owncloud/ocis/v2/services/postprocessing/pkg/postprocessing" +) + +// PostprocessingService is an instance of the service handling postprocessing of files +type PostprocessingService struct { + log log.Logger + events <-chan interface{} + pub events.Publisher + c config.Postprocessing +} + +// NewPostprocessingService returns a new instance of a postprocessing service +func NewPostprocessingService(stream events.Stream, logger log.Logger, c config.Postprocessing) (*PostprocessingService, error) { + evs, err := events.Consume(stream, "postprocessing", + events.BytesReceived{}, + events.VirusscanFinished{}, + events.UploadReady{}, + ) + if err != nil { + return nil, err + } + + return &PostprocessingService{ + log: logger, + events: evs, + pub: stream, + c: c, + }, nil +} + +// Run to fulfil Runner interface +func (pps *PostprocessingService) Run() error { + current := make(map[string]*postprocessing.Postprocessing) + for e := range pps.events { + var next interface{} + switch ev := e.(type) { + case events.BytesReceived: + pp := postprocessing.New(ev.UploadID, ev.URL, pps.c) + current[ev.UploadID] = pp + next = pp.Init(ev) + case events.VirusscanFinished: + pp := current[ev.UploadID] + next = pp.Virusscan(ev) + case events.UploadReady: + // the storage provider thinks the upload is done - so no need to keep it any more + delete(current, ev.UploadID) + } + + if next != nil { + if err := events.Publish(pps.pub, next); err != nil { + pps.log.Error().Err(err).Msg("unable to publish event") + return err // we can't publish -> we are screwed + } + } + + } + return nil +} diff --git a/services/storage-users/pkg/revaconfig/user.go b/services/storage-users/pkg/revaconfig/user.go index ca06607772f..c04961dbdc4 100644 --- a/services/storage-users/pkg/revaconfig/user.go +++ b/services/storage-users/pkg/revaconfig/user.go @@ -95,6 +95,11 @@ func UserDrivers(cfg *config.Config) map[string]interface{} { "treetime_accounting": true, "treesize_accounting": true, "permissionssvc": cfg.Drivers.OCIS.PermissionsEndpoint, + "asyncfileuploads": true, + "events": map[string]interface{}{ + "natsaddress": cfg.Events.Addr, + "natsclusterid": cfg.Events.ClusterID, + }, }, "s3": map[string]interface{}{ "enable_home": false,