diff --git a/accounts/pkg/command/health.go b/accounts/pkg/command/health.go index 5a4d79cef2..2fadae8ef7 100644 --- a/accounts/pkg/command/health.go +++ b/accounts/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/accounts/pkg/config" + "github.com/owncloud/ocis/accounts/pkg/config/parser" "github.com/owncloud/ocis/accounts/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/accounts/pkg/command/root.go b/accounts/pkg/command/root.go index 5122954234..a0d264fde4 100644 --- a/accounts/pkg/command/root.go +++ b/accounts/pkg/command/root.go @@ -2,15 +2,12 @@ package command import ( "context" - "errors" "os" - "strings" "github.com/owncloud/ocis/accounts/pkg/config" + "github.com/owncloud/ocis/accounts/pkg/config/parser" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -40,12 +37,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-accounts", Usage: "Provide accounts and groups for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -57,42 +51,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads accounts configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - // sanitize config - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") - } - cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend) - - return nil -} - // SutureService allows for the accounts command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/accounts/pkg/command/server.go b/accounts/pkg/command/server.go index d0cd905a05..906dd75b64 100644 --- a/accounts/pkg/command/server.go +++ b/accounts/pkg/command/server.go @@ -5,6 +5,7 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/accounts/pkg/config" + "github.com/owncloud/ocis/accounts/pkg/config/parser" "github.com/owncloud/ocis/accounts/pkg/logging" "github.com/owncloud/ocis/accounts/pkg/metrics" "github.com/owncloud/ocis/accounts/pkg/server/debug" @@ -22,7 +23,7 @@ func Server(cfg *config.Config) *cli.Command { Usage: "Start ocis accounts service", Description: "uses an LDAP server as the storage backend", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/accounts/pkg/config/parser/parse.go b/accounts/pkg/config/parser/parse.go new file mode 100644 index 0000000000..dee2dc13b7 --- /dev/null +++ b/accounts/pkg/config/parser/parse.go @@ -0,0 +1,47 @@ +package parser + +import ( + "errors" + "strings" + + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + + "github.com/owncloud/ocis/accounts/pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend) + + return nil +} diff --git a/glauth/pkg/command/health.go b/glauth/pkg/command/health.go index 1cd0eac23c..215018b291 100644 --- a/glauth/pkg/command/health.go +++ b/glauth/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/glauth/pkg/config" + "github.com/owncloud/ocis/glauth/pkg/config/parser" "github.com/owncloud/ocis/glauth/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/glauth/pkg/command/root.go b/glauth/pkg/command/root.go index 120791317a..4ba8e95be3 100644 --- a/glauth/pkg/command/root.go +++ b/glauth/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/glauth/pkg/config" + "github.com/owncloud/ocis/glauth/pkg/config/parser" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-glauth", Usage: "Serve GLAuth API for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,36 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads glauth configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the glauth command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/glauth/pkg/command/server.go b/glauth/pkg/command/server.go index e5f88b3948..db5222db91 100644 --- a/glauth/pkg/command/server.go +++ b/glauth/pkg/command/server.go @@ -7,6 +7,7 @@ import ( "github.com/oklog/run" accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0" "github.com/owncloud/ocis/glauth/pkg/config" + "github.com/owncloud/ocis/glauth/pkg/config/parser" "github.com/owncloud/ocis/glauth/pkg/logging" "github.com/owncloud/ocis/glauth/pkg/metrics" "github.com/owncloud/ocis/glauth/pkg/server/debug" @@ -24,7 +25,7 @@ func Server(cfg *config.Config) *cli.Command { Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/glauth/pkg/config/parser/parse.go b/glauth/pkg/config/parser/parse.go new file mode 100644 index 0000000000..b2a18b2a90 --- /dev/null +++ b/glauth/pkg/config/parser/parse.go @@ -0,0 +1,42 @@ +package parser + +import ( + "errors" + + "github.com/owncloud/ocis/glauth/pkg/config" + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + + return nil +} diff --git a/graph-explorer/pkg/command/health.go b/graph-explorer/pkg/command/health.go index 54ad47fe99..6719e960af 100644 --- a/graph-explorer/pkg/command/health.go +++ b/graph-explorer/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/graph-explorer/pkg/config" + "github.com/owncloud/ocis/graph-explorer/pkg/config/parser" "github.com/owncloud/ocis/graph-explorer/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig( cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/graph-explorer/pkg/command/root.go b/graph-explorer/pkg/command/root.go index 4e3fecc58b..92097b2799 100644 --- a/graph-explorer/pkg/command/root.go +++ b/graph-explorer/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/graph-explorer/pkg/config" + "github.com/owncloud/ocis/graph-explorer/pkg/config/parser" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "graph-explorer", Usage: "Serve Graph-Explorer for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,36 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads graph configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the graph-explorer command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/graph-explorer/pkg/command/server.go b/graph-explorer/pkg/command/server.go index 6b9f80129b..b16ec1150b 100644 --- a/graph-explorer/pkg/command/server.go +++ b/graph-explorer/pkg/command/server.go @@ -2,10 +2,10 @@ package command import ( "context" - "strings" "github.com/oklog/run" "github.com/owncloud/ocis/graph-explorer/pkg/config" + "github.com/owncloud/ocis/graph-explorer/pkg/config/parser" "github.com/owncloud/ocis/graph-explorer/pkg/logging" "github.com/owncloud/ocis/graph-explorer/pkg/metrics" "github.com/owncloud/ocis/graph-explorer/pkg/server/debug" @@ -20,11 +20,7 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") - } - - return ParseConfig(ctx, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/graph-explorer/pkg/config/parser/parse.go b/graph-explorer/pkg/config/parser/parse.go new file mode 100644 index 0000000000..be113e4598 --- /dev/null +++ b/graph-explorer/pkg/config/parser/parse.go @@ -0,0 +1,46 @@ +package parser + +import ( + "errors" + "strings" + + "github.com/owncloud/ocis/graph-explorer/pkg/config" + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + + return nil +} diff --git a/graph/pkg/command/health.go b/graph/pkg/command/health.go index 18cc12baef..5e2a31ecbe 100644 --- a/graph/pkg/command/health.go +++ b/graph/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/graph/pkg/config" + "github.com/owncloud/ocis/graph/pkg/config/parser" "github.com/owncloud/ocis/graph/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/graph/pkg/command/root.go b/graph/pkg/command/root.go index af4f264f17..2318c610b7 100644 --- a/graph/pkg/command/root.go +++ b/graph/pkg/command/root.go @@ -2,16 +2,14 @@ package command import ( "context" - "errors" "os" + "github.com/owncloud/ocis/graph/pkg/config/parser" "github.com/owncloud/ocis/ocis-pkg/clihelper" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" "github.com/thejerf/suture/v4" "github.com/owncloud/ocis/graph/pkg/config" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/urfave/cli/v2" ) @@ -34,12 +32,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-graph", Usage: "Serve Graph API for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) cli.HelpFlag = &cli.BoolFlag{ @@ -50,36 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads graph configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the graph command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/graph/pkg/command/server.go b/graph/pkg/command/server.go index 6a62b1dace..27bb58b13d 100644 --- a/graph/pkg/command/server.go +++ b/graph/pkg/command/server.go @@ -2,10 +2,10 @@ package command import ( "context" - "strings" "github.com/oklog/run" "github.com/owncloud/ocis/graph/pkg/config" + "github.com/owncloud/ocis/graph/pkg/config/parser" "github.com/owncloud/ocis/graph/pkg/logging" "github.com/owncloud/ocis/graph/pkg/metrics" "github.com/owncloud/ocis/graph/pkg/server/debug" @@ -20,11 +20,8 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") - } - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/graph/pkg/config/parser/parse.go b/graph/pkg/config/parser/parse.go new file mode 100644 index 0000000000..8856821543 --- /dev/null +++ b/graph/pkg/config/parser/parse.go @@ -0,0 +1,46 @@ +package parser + +import ( + "errors" + "strings" + + "github.com/owncloud/ocis/graph/pkg/config" + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + + return nil +} diff --git a/idp/pkg/command/health.go b/idp/pkg/command/health.go index fcb337253b..a668eaf4cd 100644 --- a/idp/pkg/command/health.go +++ b/idp/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/idp/pkg/config" + "github.com/owncloud/ocis/idp/pkg/config/parser" "github.com/owncloud/ocis/idp/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/idp/pkg/command/root.go b/idp/pkg/command/root.go index bcb8e4ea1f..9bb2df16e0 100644 --- a/idp/pkg/command/root.go +++ b/idp/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/idp/pkg/config" + "github.com/owncloud/ocis/idp/pkg/config/parser" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-idp", Usage: "Serve IDP API for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,36 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads idp configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the idp command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/idp/pkg/command/server.go b/idp/pkg/command/server.go index 0c91b4b5b2..e955731c2b 100644 --- a/idp/pkg/command/server.go +++ b/idp/pkg/command/server.go @@ -2,10 +2,10 @@ package command import ( "context" - "strings" "github.com/oklog/run" "github.com/owncloud/ocis/idp/pkg/config" + "github.com/owncloud/ocis/idp/pkg/config/parser" "github.com/owncloud/ocis/idp/pkg/logging" "github.com/owncloud/ocis/idp/pkg/metrics" "github.com/owncloud/ocis/idp/pkg/server/debug" @@ -20,14 +20,10 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") - } - return nil }, Action: func(c *cli.Context) error { diff --git a/idp/pkg/config/parser/parse.go b/idp/pkg/config/parser/parse.go new file mode 100644 index 0000000000..de98f42d68 --- /dev/null +++ b/idp/pkg/config/parser/parse.go @@ -0,0 +1,46 @@ +package parser + +import ( + "errors" + "strings" + + "github.com/owncloud/ocis/idp/pkg/config" + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + + return nil +} diff --git a/ocis-pkg/config/parser/parse.go b/ocis-pkg/config/parser/parse.go new file mode 100644 index 0000000000..a0870d17c7 --- /dev/null +++ b/ocis-pkg/config/parser/parse.go @@ -0,0 +1,39 @@ +package parser + +import ( + "errors" + + "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" + "github.com/owncloud/ocis/ocis-pkg/shared" +) + +// ParseConfig loads ocis configuration. +func ParseConfig(cfg *config.Config) error { + _, err := config.BindSourcesToStructs("ocis", cfg) + if err != nil { + return err + } + + // 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 = &shared.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.Commons == nil { + cfg.Log = &shared.Log{} + } + + // 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 + } + } + + return nil +} diff --git a/ocis/pkg/command/accounts.go b/ocis/pkg/command/accounts.go index 62efe82eaa..719b08062c 100644 --- a/ocis/pkg/command/accounts.go +++ b/ocis/pkg/command/accounts.go @@ -3,6 +3,7 @@ package command import ( "github.com/owncloud/ocis/accounts/pkg/command" "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/urfave/cli/v2" ) @@ -14,7 +15,7 @@ func AccountsCommand(cfg *config.Config) *cli.Command { Usage: "Start accounts server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/glauth.go b/ocis/pkg/command/glauth.go index 1f9da764d0..23b45f9dbf 100644 --- a/ocis/pkg/command/glauth.go +++ b/ocis/pkg/command/glauth.go @@ -3,6 +3,7 @@ package command import ( "github.com/owncloud/ocis/glauth/pkg/command" "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/urfave/cli/v2" ) @@ -14,7 +15,7 @@ func GLAuthCommand(cfg *config.Config) *cli.Command { Usage: "Start glauth server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/graph.go b/ocis/pkg/command/graph.go index 92e5b532c2..0e1d4e2955 100644 --- a/ocis/pkg/command/graph.go +++ b/ocis/pkg/command/graph.go @@ -3,6 +3,7 @@ package command import ( "github.com/owncloud/ocis/graph/pkg/command" "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/urfave/cli/v2" ) @@ -14,7 +15,7 @@ func GraphCommand(cfg *config.Config) *cli.Command { Usage: "Start graph server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/graphexplorer.go b/ocis/pkg/command/graphexplorer.go index f02ad25b75..db65c6b1d6 100644 --- a/ocis/pkg/command/graphexplorer.go +++ b/ocis/pkg/command/graphexplorer.go @@ -3,6 +3,7 @@ package command import ( "github.com/owncloud/ocis/graph-explorer/pkg/command" "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/urfave/cli/v2" ) @@ -14,7 +15,7 @@ func GraphExplorerCommand(cfg *config.Config) *cli.Command { Usage: "Start graph-explorer server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/idp.go b/ocis/pkg/command/idp.go index 62d929cbe1..8f996f493e 100644 --- a/ocis/pkg/command/idp.go +++ b/ocis/pkg/command/idp.go @@ -3,6 +3,7 @@ package command import ( "github.com/owncloud/ocis/idp/pkg/command" "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/urfave/cli/v2" ) @@ -14,7 +15,7 @@ func IDPCommand(cfg *config.Config) *cli.Command { Usage: "Start idp server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/ocs.go b/ocis/pkg/command/ocs.go index dd010e71f3..eb29b3d25f 100644 --- a/ocis/pkg/command/ocs.go +++ b/ocis/pkg/command/ocs.go @@ -2,6 +2,7 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/ocs/pkg/command" "github.com/urfave/cli/v2" @@ -14,7 +15,7 @@ func OCSCommand(cfg *config.Config) *cli.Command { Usage: "Start ocs server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/proxy.go b/ocis/pkg/command/proxy.go index 2554573a28..423794d91a 100644 --- a/ocis/pkg/command/proxy.go +++ b/ocis/pkg/command/proxy.go @@ -2,6 +2,7 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/proxy/pkg/command" "github.com/urfave/cli/v2" @@ -14,7 +15,7 @@ func ProxyCommand(cfg *config.Config) *cli.Command { Usage: "Start proxy server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/root.go b/ocis/pkg/command/root.go index 163adebc80..e0c77c0f69 100644 --- a/ocis/pkg/command/root.go +++ b/ocis/pkg/command/root.go @@ -1,13 +1,11 @@ package command import ( - "errors" "os" "github.com/owncloud/ocis/ocis-pkg/clihelper" "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/shared" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/urfave/cli/v2" ) @@ -19,11 +17,8 @@ func Execute() error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis", Usage: "ownCloud Infinite Scale Stack", - Before: func(c *cli.Context) error { - // TODO: what do do? - //cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, }) @@ -41,33 +36,3 @@ func Execute() error { return app.Run(os.Args) } - -// ParseConfig loads ocis configuration. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := config.BindSourcesToStructs("ocis", cfg) - if err != nil { - return err - } - - // 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 = &shared.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.Commons == nil { - cfg.Log = &shared.Log{} - } - - // 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 - } - } - - return nil -} diff --git a/ocis/pkg/command/server.go b/ocis/pkg/command/server.go index 67cde41954..ac24c1c648 100644 --- a/ocis/pkg/command/server.go +++ b/ocis/pkg/command/server.go @@ -2,6 +2,8 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" + "github.com/owncloud/ocis/ocis-pkg/shared" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/ocis/pkg/runtime" "github.com/urfave/cli/v2" @@ -14,14 +16,13 @@ func Server(cfg *config.Config) *cli.Command { Usage: "Start fullstack server", Category: "Fullstack", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { - // what to do - //cfg.Commons = &shared.Commons{ - // Log: &cfg.Log, - //} + cfg.Commons = &shared.Commons{ + Log: cfg.Log, + } r := runtime.New(cfg) return r.Start() diff --git a/ocis/pkg/command/settings.go b/ocis/pkg/command/settings.go index ae1fec6c60..0cf950c4cb 100644 --- a/ocis/pkg/command/settings.go +++ b/ocis/pkg/command/settings.go @@ -2,6 +2,7 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/settings/pkg/command" "github.com/urfave/cli/v2" @@ -14,7 +15,7 @@ func SettingsCommand(cfg *config.Config) *cli.Command { Usage: "Start settings server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/store.go b/ocis/pkg/command/store.go index 6fda33d5eb..71ecb35d94 100644 --- a/ocis/pkg/command/store.go +++ b/ocis/pkg/command/store.go @@ -2,6 +2,7 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/store/pkg/command" "github.com/urfave/cli/v2" @@ -15,7 +16,7 @@ func StoreCommand(cfg *config.Config) *cli.Command { Usage: "Start a go-micro store", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/thumbnails.go b/ocis/pkg/command/thumbnails.go index add5b66845..171b781431 100644 --- a/ocis/pkg/command/thumbnails.go +++ b/ocis/pkg/command/thumbnails.go @@ -2,6 +2,7 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/thumbnails/pkg/command" "github.com/urfave/cli/v2" @@ -14,7 +15,7 @@ func ThumbnailsCommand(cfg *config.Config) *cli.Command { Usage: "Start thumbnails server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/util.go b/ocis/pkg/command/util.go index a2e9b1abc0..914c5a37c1 100644 --- a/ocis/pkg/command/util.go +++ b/ocis/pkg/command/util.go @@ -2,12 +2,13 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis-pkg/shared" "github.com/urfave/cli/v2" ) func ParseStorageCommon(ctx *cli.Context, cfg *config.Config) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/web.go b/ocis/pkg/command/web.go index e1f66f8eef..a95cc94aec 100644 --- a/ocis/pkg/command/web.go +++ b/ocis/pkg/command/web.go @@ -2,6 +2,7 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/web/pkg/command" "github.com/urfave/cli/v2" @@ -14,7 +15,7 @@ func WebCommand(cfg *config.Config) *cli.Command { Usage: "Start web server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocis/pkg/command/webdav.go b/ocis/pkg/command/webdav.go index ea7f518225..2b7ed2dea4 100644 --- a/ocis/pkg/command/webdav.go +++ b/ocis/pkg/command/webdav.go @@ -2,6 +2,7 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/parser" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/webdav/pkg/command" "github.com/urfave/cli/v2" @@ -15,7 +16,7 @@ func WebDAVCommand(cfg *config.Config) *cli.Command { Usage: "Start webdav server", Category: "Extensions", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocs/pkg/command/health.go b/ocs/pkg/command/health.go index b1d2249e26..834390b9e1 100644 --- a/ocs/pkg/command/health.go +++ b/ocs/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/ocs/pkg/config" + "github.com/owncloud/ocis/ocs/pkg/config/parser" "github.com/owncloud/ocis/ocs/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/ocs/pkg/command/root.go b/ocs/pkg/command/root.go index 2b143d547a..1d10a9847f 100644 --- a/ocs/pkg/command/root.go +++ b/ocs/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/ocs/pkg/config" + "github.com/owncloud/ocis/ocs/pkg/config/parser" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-ocs", Usage: "Serve OCS API for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,36 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads idp configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the ocs command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/ocs/pkg/command/server.go b/ocs/pkg/command/server.go index 406e0221cc..807a7786a4 100644 --- a/ocs/pkg/command/server.go +++ b/ocs/pkg/command/server.go @@ -2,8 +2,8 @@ package command import ( "context" - "strings" + "github.com/owncloud/ocis/ocs/pkg/config/parser" "github.com/owncloud/ocis/ocs/pkg/logging" "github.com/owncloud/ocis/ocs/pkg/tracing" @@ -21,11 +21,7 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") - } - - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/ocs/pkg/config/parser/parse.go b/ocs/pkg/config/parser/parse.go new file mode 100644 index 0000000000..9a50e535b3 --- /dev/null +++ b/ocs/pkg/config/parser/parse.go @@ -0,0 +1,46 @@ +package parser + +import ( + "errors" + "strings" + + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocs/pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + + return nil +} diff --git a/proxy/pkg/command/root.go b/proxy/pkg/command/root.go index 283e85246a..b2c82bf0a7 100644 --- a/proxy/pkg/command/root.go +++ b/proxy/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/proxy/pkg/config" + "github.com/owncloud/ocis/proxy/pkg/config/parser" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-proxy", Usage: "proxy for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,36 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads accounts configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the proxy command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/proxy/pkg/command/server.go b/proxy/pkg/command/server.go index a99060d8b2..501305bcef 100644 --- a/proxy/pkg/command/server.go +++ b/proxy/pkg/command/server.go @@ -3,9 +3,7 @@ package command import ( "context" "crypto/tls" - "fmt" "net/http" - "strings" "time" "github.com/coreos/go-oidc/v3/oidc" @@ -14,11 +12,11 @@ import ( "github.com/justinas/alice" "github.com/oklog/run" acc "github.com/owncloud/ocis/accounts/pkg/proto/v0" - "github.com/owncloud/ocis/ocis-pkg/conversions" "github.com/owncloud/ocis/ocis-pkg/log" pkgmiddleware "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/service/grpc" "github.com/owncloud/ocis/proxy/pkg/config" + "github.com/owncloud/ocis/proxy/pkg/config/parser" "github.com/owncloud/ocis/proxy/pkg/cs3" "github.com/owncloud/ocis/proxy/pkg/logging" "github.com/owncloud/ocis/proxy/pkg/metrics" @@ -40,26 +38,9 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } - - if cfg.Policies == nil { - cfg.Policies = config.DefaultPolicies() - } - - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") - } - - if len(ctx.StringSlice("presignedurl-allow-method")) > 0 { - cfg.PreSignedURL.AllowedHTTPMethods = ctx.StringSlice("presignedurl-allow-method") - } - - if err := loadUserAgent(ctx, cfg); err != nil { - return err - } - return nil }, Action: func(c *cli.Context) error { @@ -218,7 +199,7 @@ func loadMiddlewares(ctx context.Context, logger log.Logger, cfg *config.Config) middleware.OIDCIss(cfg.OIDC.Issuer), middleware.UserOIDCClaim(cfg.UserOIDCClaim), middleware.UserCS3Claim(cfg.UserCS3Claim), - middleware.CredentialsByUserAgent(cfg.Reva.Middleware.Auth.CredentialsByUserAgent), + middleware.CredentialsByUserAgent(cfg.AuthMiddleware.CredentialsByUserAgent), ), middleware.SignedURLAuth( middleware.Logger(logger), @@ -253,28 +234,3 @@ func loadMiddlewares(ctx context.Context, logger log.Logger, cfg *config.Config) ), ) } - -// loadUserAgent reads the proxy-user-agent-lock-in, since it is a string flag, and attempts to construct a map of -// "user-agent":"challenge" locks in for Reva. -// Modifies cfg. Spaces don't need to be trimmed as urfavecli takes care of it. User agents with spaces are valid. i.e: -// Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:83.0) Gecko/20100101 Firefox/83.0 -// This function works by relying in our format of specifying [user-agent:challenge] and the fact that the user agent -// might contain ":" (colon), so the original string is reversed, split in two parts, by the time it is split we -// have the indexes reversed and the tuple is in the format of [challenge:user-agent], then the same process is applied -// in reverse for each individual part -func loadUserAgent(c *cli.Context, cfg *config.Config) error { - cfg.Reva.Middleware.Auth.CredentialsByUserAgent = make(map[string]string) - locks := c.StringSlice("proxy-user-agent-lock-in") - - for _, v := range locks { - vv := conversions.Reverse(v) - parts := strings.SplitN(vv, ":", 2) - if len(parts) != 2 { - return fmt.Errorf("unexpected config value for user-agent lock-in: %v, expected format is user-agent:challenge", v) - } - - cfg.Reva.Middleware.Auth.CredentialsByUserAgent[conversions.Reverse(parts[1])] = conversions.Reverse(parts[0]) - } - - return nil -} diff --git a/proxy/pkg/config/config.go b/proxy/pkg/config/config.go index 5ac9ee8410..c525421e80 100644 --- a/proxy/pkg/config/config.go +++ b/proxy/pkg/config/config.go @@ -18,11 +18,12 @@ type Config struct { HTTP HTTP `ocisConfig:"http"` + Reva Reva `ocisConfig:"reva"` + Policies []Policy `ocisConfig:"policies"` OIDC OIDC `ocisConfig:"oidc"` TokenManager TokenManager `ocisConfig:"token_manager"` PolicySelector *PolicySelector `ocisConfig:"policy_selector"` - Reva Reva `ocisConfig:"reva"` PreSignedURL PreSignedURL `ocisConfig:"pre_signed_url"` AccountBackend string `ocisConfig:"account_backend" env:"PROXY_ACCOUNT_BACKEND_TYPE"` UserOIDCClaim string `ocisConfig:"user_oidc_claim" env:"PROXY_USER_OIDC_CLAIM"` @@ -31,6 +32,7 @@ type Config struct { AutoprovisionAccounts bool `ocisConfig:"auto_provision_accounts" env:"PROXY_AUTOPROVISION_ACCOUNTS"` EnableBasicAuth bool `ocisConfig:"enable_basic_auth" env:"PROXY_ENABLE_BASIC_AUTH"` InsecureBackends bool `ocisConfig:"insecure_backends" env:"PROXY_INSECURE_BACKENDS"` + AuthMiddleware AuthMiddleware `ocisConfig:"auth_middleware"` Context context.Context } @@ -68,21 +70,9 @@ var ( RouteTypes = []RouteType{QueryRoute, RegexRoute, PrefixRoute} ) -// TODO: use reva config here -// Reva defines all available REVA configuration. -type Reva struct { - Address string `ocisConfig:"address" env:"REVA_GATEWAY"` - Middleware Middleware `ocisConfig:"middleware"` -} - -// Middleware configures proxy middlewares. -type Middleware struct { - Auth Auth `ocisConfig:"middleware"` -} - -// Auth configures proxy http auth middleware. -type Auth struct { - CredentialsByUserAgent map[string]string `ocisConfig:""` +// AuthMiddleware configures the proxy http auth middleware. +type AuthMiddleware struct { + CredentialsByUserAgent map[string]string `ocisConfig:"credentials_by_user_agent"` } // OIDC is the config for the OpenID-Connect middleware. If set the proxy will try to authenticate every request diff --git a/proxy/pkg/config/parser/parse.go b/proxy/pkg/config/parser/parse.go new file mode 100644 index 0000000000..65921b31cf --- /dev/null +++ b/proxy/pkg/config/parser/parse.go @@ -0,0 +1,51 @@ +package parser + +import ( + "errors" + "strings" + + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/proxy/pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + + if cfg.Policies == nil { + cfg.Policies = config.DefaultPolicies() + } + + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + + return nil +} diff --git a/proxy/pkg/config/reva.go b/proxy/pkg/config/reva.go new file mode 100644 index 0000000000..2b299a0f65 --- /dev/null +++ b/proxy/pkg/config/reva.go @@ -0,0 +1,6 @@ +package config + +// Reva defines all available REVA configuration. +type Reva struct { + Address string `ocisConfig:"address" env:"REVA_GATEWAY"` +} diff --git a/settings/pkg/command/health.go b/settings/pkg/command/health.go index e561b84e08..a8e15f8cfd 100644 --- a/settings/pkg/command/health.go +++ b/settings/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/settings/pkg/config" + "github.com/owncloud/ocis/settings/pkg/config/parser" "github.com/owncloud/ocis/settings/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/settings/pkg/command/root.go b/settings/pkg/command/root.go index 03340bb1af..938a3f26be 100644 --- a/settings/pkg/command/root.go +++ b/settings/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/settings/pkg/config" + "github.com/owncloud/ocis/settings/pkg/config/parser" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-settings", Usage: "Provide settings and permissions for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,36 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads idp configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the settings command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/settings/pkg/command/server.go b/settings/pkg/command/server.go index a6e51c9304..c26623a86d 100644 --- a/settings/pkg/command/server.go +++ b/settings/pkg/command/server.go @@ -2,10 +2,10 @@ package command import ( "context" - "strings" "github.com/oklog/run" "github.com/owncloud/ocis/settings/pkg/config" + "github.com/owncloud/ocis/settings/pkg/config/parser" "github.com/owncloud/ocis/settings/pkg/logging" "github.com/owncloud/ocis/settings/pkg/metrics" "github.com/owncloud/ocis/settings/pkg/server/debug" @@ -21,11 +21,7 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") - } - - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/settings/pkg/config/parser/parse.go b/settings/pkg/config/parser/parse.go new file mode 100644 index 0000000000..551d44108c --- /dev/null +++ b/settings/pkg/config/parser/parse.go @@ -0,0 +1,46 @@ +package parser + +import ( + "errors" + "strings" + + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/settings/pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + + return nil +} diff --git a/store/pkg/command/health.go b/store/pkg/command/health.go index b280d8b0e4..bc0b77b344 100644 --- a/store/pkg/command/health.go +++ b/store/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/store/pkg/config" + "github.com/owncloud/ocis/store/pkg/config/parser" "github.com/owncloud/ocis/store/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/store/pkg/command/root.go b/store/pkg/command/root.go index c32c0c45f3..6586d0947c 100644 --- a/store/pkg/command/root.go +++ b/store/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/store/pkg/config" + "github.com/owncloud/ocis/store/pkg/config/parser" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-store", Usage: "Service to store values for ocis extensions", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,36 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads accounts configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the store command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/store/pkg/command/server.go b/store/pkg/command/server.go index add4beaef6..c4ebd5e9ab 100644 --- a/store/pkg/command/server.go +++ b/store/pkg/command/server.go @@ -3,14 +3,15 @@ package command import ( "context" - "github.com/owncloud/ocis/store/pkg/logging" - "github.com/owncloud/ocis/store/pkg/tracing" - "github.com/oklog/run" + "github.com/owncloud/ocis/store/pkg/config" + "github.com/owncloud/ocis/store/pkg/config/parser" + "github.com/owncloud/ocis/store/pkg/logging" "github.com/owncloud/ocis/store/pkg/metrics" "github.com/owncloud/ocis/store/pkg/server/debug" "github.com/owncloud/ocis/store/pkg/server/grpc" + "github.com/owncloud/ocis/store/pkg/tracing" "github.com/urfave/cli/v2" ) @@ -20,7 +21,7 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/store/pkg/config/parser/parse.go b/store/pkg/config/parser/parse.go new file mode 100644 index 0000000000..f3789eecbf --- /dev/null +++ b/store/pkg/config/parser/parse.go @@ -0,0 +1,42 @@ +package parser + +import ( + "errors" + + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/store/pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + + return nil +} diff --git a/thumbnails/pkg/command/health.go b/thumbnails/pkg/command/health.go index 1ba961d62c..5b232dc9e1 100644 --- a/thumbnails/pkg/command/health.go +++ b/thumbnails/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/thumbnails/pkg/config" + "github.com/owncloud/ocis/thumbnails/pkg/config/parser" "github.com/owncloud/ocis/thumbnails/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/thumbnails/pkg/command/root.go b/thumbnails/pkg/command/root.go index ad18155e75..1cdb5c4944 100644 --- a/thumbnails/pkg/command/root.go +++ b/thumbnails/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/thumbnails/pkg/config" + "github.com/owncloud/ocis/thumbnails/pkg/config/parser" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "ocis-thumbnails", Usage: "Example usage", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,35 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads accounts configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - return nil -} - // SutureService allows for the thumbnails command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/thumbnails/pkg/command/server.go b/thumbnails/pkg/command/server.go index 42531f4d86..990c549e35 100644 --- a/thumbnails/pkg/command/server.go +++ b/thumbnails/pkg/command/server.go @@ -6,6 +6,7 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/thumbnails/pkg/config" + "github.com/owncloud/ocis/thumbnails/pkg/config/parser" "github.com/owncloud/ocis/thumbnails/pkg/logging" "github.com/owncloud/ocis/thumbnails/pkg/metrics" "github.com/owncloud/ocis/thumbnails/pkg/server/debug" @@ -20,7 +21,7 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } return nil diff --git a/thumbnails/pkg/config/parser/parse.go b/thumbnails/pkg/config/parser/parse.go new file mode 100644 index 0000000000..3591aea8a5 --- /dev/null +++ b/thumbnails/pkg/config/parser/parse.go @@ -0,0 +1,42 @@ +package parser + +import ( + "errors" + + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/thumbnails/pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + + return nil +} diff --git a/web/pkg/command/health.go b/web/pkg/command/health.go index d780d4058e..5ab5887ea0 100644 --- a/web/pkg/command/health.go +++ b/web/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/web/pkg/config" + "github.com/owncloud/ocis/web/pkg/config/parser" "github.com/owncloud/ocis/web/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/web/pkg/command/root.go b/web/pkg/command/root.go index 38c8ea330d..2c22ed570d 100644 --- a/web/pkg/command/root.go +++ b/web/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/web/pkg/config" + "github.com/owncloud/ocis/web/pkg/config/parser" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "web", Usage: "Serve ownCloud Web for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,37 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads accounts configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - // TODO: remove cli.Context - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the web command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/web/pkg/command/server.go b/web/pkg/command/server.go index 6b3b1f9ad2..fbacd1cccb 100644 --- a/web/pkg/command/server.go +++ b/web/pkg/command/server.go @@ -8,6 +8,7 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/web/pkg/config" + "github.com/owncloud/ocis/web/pkg/config/parser" "github.com/owncloud/ocis/web/pkg/logging" "github.com/owncloud/ocis/web/pkg/metrics" "github.com/owncloud/ocis/web/pkg/server/debug" @@ -26,7 +27,7 @@ func Server(cfg *config.Config) *cli.Command { cfg.HTTP.Root = strings.TrimRight(cfg.HTTP.Root, "/") } - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/web/pkg/config/defaultconfig.go b/web/pkg/config/defaultconfig.go index 42f22beda3..cf518a9def 100644 --- a/web/pkg/config/defaultconfig.go +++ b/web/pkg/config/defaultconfig.go @@ -1,5 +1,7 @@ package config +import "github.com/owncloud/ocis/ocis-pkg/version" + func DefaultConfig() *Config { return &Config{ Debug: Debug{ @@ -15,7 +17,8 @@ func DefaultConfig() *Config { CacheTTL: 604800, // 7 days }, Service: Service{ - Name: "web", + Name: "web", + Version: version.String, // TODO: ensure everywhere or remove }, Tracing: Tracing{ Enabled: false, diff --git a/web/pkg/config/parser/parse.go b/web/pkg/config/parser/parse.go new file mode 100644 index 0000000000..ed22078282 --- /dev/null +++ b/web/pkg/config/parser/parse.go @@ -0,0 +1,42 @@ +package parser + +import ( + "errors" + + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/web/pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + + return nil +} diff --git a/webdav/pkg/command/health.go b/webdav/pkg/command/health.go index e1f2be33e6..0a8e62486b 100644 --- a/webdav/pkg/command/health.go +++ b/webdav/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/webdav/pkg/config" + "github.com/owncloud/ocis/webdav/pkg/config/parser" "github.com/owncloud/ocis/webdav/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Name: "health", Usage: "Check health status", Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) diff --git a/webdav/pkg/command/root.go b/webdav/pkg/command/root.go index 2846715b32..45e2564921 100644 --- a/webdav/pkg/command/root.go +++ b/webdav/pkg/command/root.go @@ -2,14 +2,12 @@ package command import ( "context" - "errors" "os" "github.com/owncloud/ocis/ocis-pkg/clihelper" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/config/envdecode" - "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/webdav/pkg/config" + "github.com/owncloud/ocis/webdav/pkg/config/parser" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -33,12 +31,9 @@ func Execute(cfg *config.Config) error { app := clihelper.DefaultApp(&cli.App{ Name: "webdav", Usage: "Serve WebDAV API for oCIS", - Before: func(c *cli.Context) error { - cfg.Service.Version = version.String - return ParseConfig(c, cfg) + return parser.ParseConfig(cfg) }, - Commands: GetCommands(cfg), }) @@ -50,36 +45,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// ParseConfig loads graph configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) - if err != nil { - return err - } - - // 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.Commons == nil { - cfg.Log = &config.Log{} - } - - // 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 - } - } - - return nil -} - // SutureService allows for the webdav command to be embedded and supervised by a suture supervisor tree. type SutureService struct { cfg *config.Config diff --git a/webdav/pkg/command/server.go b/webdav/pkg/command/server.go index 7732f04bb7..073a8eb805 100644 --- a/webdav/pkg/command/server.go +++ b/webdav/pkg/command/server.go @@ -2,10 +2,10 @@ package command import ( "context" - "strings" "github.com/oklog/run" "github.com/owncloud/ocis/webdav/pkg/config" + "github.com/owncloud/ocis/webdav/pkg/config/parser" "github.com/owncloud/ocis/webdav/pkg/logging" "github.com/owncloud/ocis/webdav/pkg/metrics" "github.com/owncloud/ocis/webdav/pkg/server/debug" @@ -20,11 +20,8 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") - } - if err := ParseConfig(ctx, cfg); err != nil { + if err := parser.ParseConfig(cfg); err != nil { return err } diff --git a/webdav/pkg/config/parser/parse.go b/webdav/pkg/config/parser/parse.go new file mode 100644 index 0000000000..66341b0992 --- /dev/null +++ b/webdav/pkg/config/parser/parse.go @@ -0,0 +1,46 @@ +package parser + +import ( + "errors" + "strings" + + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/webdav/pkg/config" + + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" +) + +// ParseConfig loads accounts configuration from known paths. +func ParseConfig(cfg *config.Config) error { + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + if err != nil { + return err + } + + // 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.Commons == nil { + cfg.Log = &config.Log{} + } + + // 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 + } + } + + // sanitize config + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + + return nil +}