diff --git a/accounts/pkg/command/root.go b/accounts/pkg/command/root.go index 910a9240c1..19eae93b2f 100644 --- a/accounts/pkg/command/root.go +++ b/accounts/pkg/command/root.go @@ -63,8 +63,20 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { 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. - conf.LoadOSEnv(config.GetEnv(), false) + conf.LoadOSEnv(config.GetEnv(cfg), false) bindings := config.StructMappings(cfg) return ociscfg.BindEnv(conf, bindings) @@ -77,9 +89,7 @@ type SutureService struct { // NewSutureService creates a new accounts.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - if (cfg.Accounts.Log == shared.Log{}) { - cfg.Accounts.Log = cfg.Log - } + cfg.Accounts.Commons = cfg.Commons return SutureService{ cfg: cfg.Accounts, } diff --git a/accounts/pkg/command/server.go b/accounts/pkg/command/server.go index bb820b921f..45298d2980 100644 --- a/accounts/pkg/command/server.go +++ b/accounts/pkg/command/server.go @@ -6,10 +6,6 @@ import ( "github.com/owncloud/ocis/ocis-pkg/log" - gofig "github.com/gookit/config/v2" - ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/shared" - "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/oklog/run" @@ -29,8 +25,6 @@ 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 { - // remember shared logging info to prevent empty overwrites - inLog := cfg.Log if cfg.HTTP.Root != "/" { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } @@ -41,23 +35,10 @@ func Server(cfg *config.Config) *cli.Command { return err } - if (cfg.Log == shared.Log{}) && (inLog != shared.Log{}) { - // set the default to the parent config - cfg.Log = inLog - - // and parse the environment - conf := &gofig.Config{} - conf.LoadOSEnv(config.GetEnv(), false) - bindings := config.StructMappings(cfg) - if err := ociscfg.BindEnv(conf, bindings); err != nil { - return err - } - } - return nil }, Action: func(c *cli.Context) error { - logger := log.LoggerFromConfig("accounts", cfg.Log) + logger := log.LoggerFromConfig("accounts", *cfg.Log) err := tracing.Configure(cfg) if err != nil { return err diff --git a/accounts/pkg/config/config.go b/accounts/pkg/config/config.go index e382cc9651..fbb299a632 100644 --- a/accounts/pkg/config/config.go +++ b/accounts/pkg/config/config.go @@ -125,12 +125,14 @@ type Tracing struct { // Config merges all Account config parameters. type Config struct { + *shared.Commons + LDAP LDAP `mapstructure:"ldap"` HTTP HTTP `mapstructure:"http"` GRPC GRPC `mapstructure:"grpc"` Server Server `mapstructure:"server"` Asset Asset `mapstructure:"asset"` - Log shared.Log `mapstructure:"log"` + Log *shared.Log `mapstructure:"log"` TokenManager TokenManager `mapstructure:"token_manager"` Repo Repo `mapstructure:"repo"` Index Index `mapstructure:"index"` @@ -171,7 +173,6 @@ func DefaultConfig() *Config { DemoUsersAndGroups: true, }, Asset: Asset{}, - Log: shared.Log{}, TokenManager: TokenManager{ JWTSecret: "Pive-Fumkiu4", }, @@ -212,10 +213,10 @@ func DefaultConfig() *Config { // GetEnv fetches a list of known env variables for this extension. It is to be used by gookit, as it provides a list // with all the environment variables an extension supports. -func GetEnv() []string { - var r = make([]string, len(structMappings(&Config{}))) - for i := range structMappings(&Config{}) { - r = append(r, structMappings(&Config{})[i].EnvVars...) +func GetEnv(cfg *Config) []string { + var r = make([]string, len(structMappings(cfg))) + for i := range structMappings(cfg) { + r = append(r, structMappings(cfg)[i].EnvVars...) } return r diff --git a/go.mod b/go.mod index 8edb1d288a..d821d7072b 100644 --- a/go.mod +++ b/go.mod @@ -70,6 +70,7 @@ require ( google.golang.org/genproto v0.0.0-20211020151524-b7c3a969101a google.golang.org/grpc v1.42.0 google.golang.org/protobuf v1.27.1 + gopkg.in/yaml.v2 v2.4.0 gotest.tools/v3 v3.0.3 stash.kopano.io/kgol/rndm v1.1.1 ) @@ -243,7 +244,6 @@ require ( gopkg.in/square/go-jose.v2 v2.6.0 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect stash.kopano.io/kgol/kcc-go/v5 v5.0.1 // indirect stash.kopano.io/kgol/oidc-go v0.3.2 // indirect diff --git a/ocis-pkg/config/config.go b/ocis-pkg/config/config.go index dc08ec50ff..cbb79bcb33 100644 --- a/ocis-pkg/config/config.go +++ b/ocis-pkg/config/config.go @@ -70,6 +70,8 @@ type Runtime struct { // Config combines all available configuration parts. type Config struct { + *shared.Commons `mapstructure:"shared"` + Mode Mode // DEPRECATED File string OcisURL string `mapstructure:"ocis_url"` @@ -119,9 +121,6 @@ func New() *Config { func DefaultConfig() *Config { return &Config{ - Log: shared.Log{ - Level: "info", - }, Debug: Debug{ Addr: "127.0.0.1:9010", Token: "", diff --git a/ocis-pkg/shared/shared_types.go b/ocis-pkg/shared/shared_types.go index 9a938d5533..fc655e73df 100644 --- a/ocis-pkg/shared/shared_types.go +++ b/ocis-pkg/shared/shared_types.go @@ -15,3 +15,10 @@ type Log struct { Color bool `mapstructure:"color"` File string `mapstructure:"file"` } + +// Commons holds configuration that are common to all extensions. Each extension can then decide whether +// to overwrite its values. +type Commons struct { + *Log `mapstructure:"log"` + OcisURL string `mapstructure:"ocis_url"` +} diff --git a/ocis/pkg/command/proxy.go b/ocis/pkg/command/proxy.go index 3f327d8169..321298689b 100644 --- a/ocis/pkg/command/proxy.go +++ b/ocis/pkg/command/proxy.go @@ -5,7 +5,6 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/shared" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/proxy/pkg/command" "github.com/urfave/cli/v2" @@ -13,8 +12,6 @@ import ( // ProxyCommand is the entry point for the proxy command. func ProxyCommand(cfg *config.Config) *cli.Command { - var globalLog shared.Log - return &cli.Command{ Name: "proxy", Usage: "Start proxy server", @@ -26,15 +23,14 @@ func ProxyCommand(cfg *config.Config) *cli.Command { if err := ParseConfig(ctx, cfg); err != nil { return err } - globalLog = cfg.Log + + if cfg.Commons != nil { + cfg.Proxy.Commons = cfg.Commons + } + return nil }, Action: func(c *cli.Context) error { - // if proxy logging is empty in ocis.yaml - if (cfg.Proxy.Log == shared.Log{}) && (globalLog != shared.Log{}) { - // we can safely inherit the global logging values. - cfg.Proxy.Log = globalLog - } origCmd := command.Server(cfg.Proxy) return handleOriginalAction(c, origCmd) }, diff --git a/proxy/pkg/command/root.go b/proxy/pkg/command/root.go index 924461ea6c..caad7d3477 100644 --- a/proxy/pkg/command/root.go +++ b/proxy/pkg/command/root.go @@ -4,10 +4,9 @@ import ( "context" "os" - "github.com/owncloud/ocis/ocis-pkg/shared" - ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/ocis-pkg/shared" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/proxy/pkg/config" "github.com/thejerf/suture/v4" @@ -51,17 +50,6 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// NewLogger initializes a service-specific logger instance. -func NewLogger(cfg *config.Config) log.Logger { - return log.NewLogger( - log.Name("proxy"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - // ParseConfig loads proxy configuration. Loading will first attempt to parse config files in the expected locations // and then parses environment variables. In the context of oCIS env variables will always overwrite values set // in a config file. @@ -75,7 +63,20 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { return err } - conf.LoadOSEnv(config.GetEnv(), false) + // 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{} + } + + conf.LoadOSEnv(config.GetEnv(cfg), false) + bindings := config.StructMappings(cfg) return ociscfg.BindEnv(conf, bindings) } @@ -87,9 +88,8 @@ type SutureService struct { // NewSutureService creates a new proxy.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - if (cfg.Proxy.Log == shared.Log{}) { - cfg.Proxy.Log = cfg.Log - } + // inherit common configuration from ocis config parsing. + cfg.Proxy.Commons = cfg.Commons return SutureService{ cfg: cfg.Proxy, } @@ -103,3 +103,14 @@ func (s SutureService) Serve(ctx context.Context) error { return nil } + +// NewLogger initializes a service-specific logger instance. +func NewLogger(cfg *config.Config) log.Logger { + return log.NewLogger( + log.Name("proxy"), + log.Level(cfg.Log.Level), + log.Pretty(cfg.Log.Pretty), + log.Color(cfg.Log.Color), + log.File(cfg.Log.File), + ) +} diff --git a/proxy/pkg/command/server.go b/proxy/pkg/command/server.go index 4853149589..521d2f07db 100644 --- a/proxy/pkg/command/server.go +++ b/proxy/pkg/command/server.go @@ -8,11 +8,6 @@ import ( "strings" "time" - gofig "github.com/gookit/config/v2" - ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - - "github.com/owncloud/ocis/ocis-pkg/shared" - "github.com/coreos/go-oidc/v3/oidc" "github.com/cs3org/reva/pkg/token/manager/jwt" chimiddleware "github.com/go-chi/chi/v5/middleware" @@ -45,26 +40,10 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - // remember shared logging info to prevent empty overwrites - inLog := cfg.Log - if err := ParseConfig(ctx, cfg); err != nil { return err } - if (cfg.Log == shared.Log{}) && (inLog != shared.Log{}) { - // set the default to the parent config - cfg.Log = inLog - - // and parse the environment - conf := &gofig.Config{} - conf.LoadOSEnv(config.GetEnv(), false) - bindings := config.StructMappings(cfg) - if err := ociscfg.BindEnv(conf, bindings); err != nil { - return err - } - } - if cfg.HTTP.Root != "/" { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } @@ -73,14 +52,6 @@ func Server(cfg *config.Config) *cli.Command { cfg.PreSignedURL.AllowedHTTPMethods = ctx.StringSlice("presignedurl-allow-method") } - // we need a starting point to compare the default config values to determine if the outcome of ParseConfig - // modified a value that should be shared just because it is present in the ocis.yaml file. - defaultConfig := config.DefaultConfig() - - if cfg.OcisURL != "" && cfg.OIDC.Issuer == defaultConfig.OIDC.Issuer { - cfg.OIDC.Issuer = cfg.OcisURL - } - if err := loadUserAgent(ctx, cfg); err != nil { return err } diff --git a/proxy/pkg/config/config.go b/proxy/pkg/config/config.go index 9e858a0eed..18a20ba1da 100644 --- a/proxy/pkg/config/config.go +++ b/proxy/pkg/config/config.go @@ -106,9 +106,9 @@ type Cache struct { // Config combines all available configuration parts. type Config struct { - OcisURL string + *shared.Commons - Log shared.Log `mapstructure:"log"` + Log *shared.Log `mapstructure:"log"` Debug Debug `mapstructure:"debug"` HTTP HTTP `mapstructure:"http"` Service Service `mapstructure:"service"` @@ -199,10 +199,9 @@ func New() *Config { } } -// DefaultConfig are values stored in the flag set, but moved to a struct. +// DefaultConfig provides with a working local configuration for a proxy service. func DefaultConfig() *Config { return &Config{ - Log: shared.Log{}, // logging config is inherited. Debug: Debug{ Addr: "0.0.0.0:9205", Token: "", diff --git a/proxy/pkg/config/mappings.go b/proxy/pkg/config/mappings.go index a13fedf37d..106f16d6bd 100644 --- a/proxy/pkg/config/mappings.go +++ b/proxy/pkg/config/mappings.go @@ -4,10 +4,10 @@ import "github.com/owncloud/ocis/ocis-pkg/shared" // GetEnv fetches a list of known env variables for this extension. It is to be used by gookit, as it provides a list // with all the environment variables an extension supports. -func GetEnv() []string { - var r = make([]string, len(structMappings(&Config{}))) - for i := range structMappings(&Config{}) { - r = append(r, structMappings(&Config{})[i].EnvVars...) +func GetEnv(cfg *Config) []string { + var r = make([]string, len(structMappings(cfg))) + for i := range structMappings(cfg) { + r = append(r, structMappings(cfg)[i].EnvVars...) } return r