From 6990e7d6602eb89ffc9c7ae6af0cd27527e22b17 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Fri, 17 Dec 2021 10:44:57 +0100 Subject: [PATCH] switch all other services to struct tag based env config --- accounts/pkg/command/root.go | 1 + accounts/pkg/command/server.go | 4 +- accounts/pkg/config/config.go | 43 ++-- .../pkg/proto/v0/accounts.pb.micro_test.go | 2 +- .../service/v0/accounts_permission_test.go | 3 +- glauth/cmd/glauth/main.go | 2 +- glauth/pkg/command/root.go | 6 +- glauth/pkg/config/config.go | 27 +- graph-explorer/pkg/command/health.go | 3 +- graph-explorer/pkg/command/root.go | 47 ++-- graph-explorer/pkg/command/server.go | 9 +- graph-explorer/pkg/config/config.go | 76 +++--- graph-explorer/pkg/config/mappings.go | 96 ------- graph-explorer/pkg/logging/logging.go | 17 ++ graph/cmd/graph/main.go | 2 +- graph/pkg/command/health.go | 3 +- graph/pkg/command/root.go | 16 +- graph/pkg/config/config.go | 23 +- idp/pkg/command/health.go | 3 +- idp/pkg/command/root.go | 56 ++-- idp/pkg/command/server.go | 10 +- idp/pkg/config/config.go | 172 +++++++------ idp/pkg/config/mappings.go | 239 ------------------ idp/pkg/logging/logging.go | 17 ++ idp/pkg/{log => logging}/logrus_wrapper.go | 5 +- idp/pkg/server/http/server.go | 2 +- idp/pkg/service/v0/logging.go | 8 +- idp/pkg/service/v0/service.go | 4 +- ocis-pkg/config/config.go | 50 ++-- ocis/pkg/command/accounts.go | 3 - ocis/pkg/command/graph.go | 3 - ocis/pkg/command/graphexplorer.go | 3 - ocis/pkg/command/ocs.go | 3 - ocis/pkg/command/proxy.go | 3 - ocis/pkg/command/root.go | 17 +- ocis/pkg/command/server.go | 11 +- ocis/pkg/command/settings.go | 3 - ocis/pkg/command/storageappprovider.go | 3 - ocis/pkg/command/storageauthbasic.go | 3 - ocis/pkg/command/storageauthbearer.go | 3 - ocis/pkg/command/storageauthmachine.go | 3 - ocis/pkg/command/storagefrontend.go | 3 - ocis/pkg/command/storagegateway.go | 3 - ocis/pkg/command/storagegroupprovider.go | 3 - ocis/pkg/command/storagepubliclink.go | 3 - ocis/pkg/command/storageshares.go | 3 - ocis/pkg/command/storagesharing.go | 3 - ocis/pkg/command/storageuserprovider.go | 3 - ocis/pkg/command/storageusers.go | 3 - ocis/pkg/command/store.go | 16 +- ocis/pkg/command/thumbnails.go | 9 +- ocis/pkg/command/util.go | 21 +- ocis/pkg/command/web.go | 6 +- ocis/pkg/command/webdav.go | 3 - ocis/pkg/runtime/service/service.go | 6 +- ocs/pkg/command/health.go | 3 +- ocs/pkg/command/root.go | 16 +- ocs/pkg/config/config.go | 30 +-- proxy/pkg/command/health.go | 3 +- proxy/pkg/command/root.go | 63 ++--- proxy/pkg/command/server.go | 7 +- proxy/pkg/config/config.go | 118 +++++---- proxy/pkg/config/mappings.go | 182 ------------- proxy/pkg/cs3/client.go | 3 +- proxy/pkg/logging/logging.go | 17 ++ settings/pkg/command/health.go | 3 +- settings/pkg/command/root.go | 55 ++-- settings/pkg/command/server.go | 4 +- settings/pkg/config/config.go | 72 +++--- settings/pkg/config/mappings.go | 115 --------- settings/pkg/logging/logging.go | 17 ++ .../pkg/proto/v0/settings.pb.micro_test.go | 4 +- settings/pkg/store/filesystem/store.go | 10 +- storage/pkg/command/appprovider.go | 3 +- storage/pkg/command/authbasic.go | 3 +- storage/pkg/command/authbearer.go | 3 +- storage/pkg/command/authmachine.go | 3 +- storage/pkg/command/frontend.go | 3 +- storage/pkg/command/gateway.go | 24 +- storage/pkg/command/groups.go | 3 +- storage/pkg/command/health.go | 3 +- storage/pkg/command/root.go | 16 +- storage/pkg/command/sharing.go | 3 +- storage/pkg/command/storagemetadata.go | 3 +- storage/pkg/command/storagepubliclink.go | 3 +- storage/pkg/command/storageshares.go | 3 +- storage/pkg/command/storageusers.go | 3 +- storage/pkg/command/users.go | 3 +- storage/pkg/config/config.go | 23 +- storage/pkg/logging/logging.go | 17 ++ store/pkg/command/health.go | 3 +- store/pkg/command/root.go | 45 ++-- store/pkg/command/server.go | 26 +- store/pkg/config/config.go | 66 +++-- store/pkg/config/mappings.go | 80 ------ store/pkg/logging/logging.go | 17 ++ thumbnails/pkg/command/health.go | 3 +- thumbnails/pkg/command/root.go | 58 ++--- thumbnails/pkg/command/server.go | 6 +- thumbnails/pkg/config/config.go | 68 ++--- thumbnails/pkg/config/mappings.go | 111 -------- thumbnails/pkg/logging/logging.go | 17 ++ .../pkg/proto/v0/thumbnails.pb.micro_test.go | 14 +- thumbnails/pkg/service/v0/service.go | 3 +- web/pkg/command/health.go | 3 +- web/pkg/command/root.go | 61 ++--- web/pkg/command/server.go | 7 +- web/pkg/config/config.go | 87 ++++--- web/pkg/config/mappings.go | 143 ----------- web/pkg/logging/logging.go | 17 ++ webdav/pkg/command/health.go | 3 +- webdav/pkg/command/root.go | 55 ++-- webdav/pkg/command/server.go | 7 +- webdav/pkg/config/config.go | 61 +++-- webdav/pkg/config/mappings.go | 107 -------- webdav/pkg/logging/logging.go | 17 ++ 116 files changed, 1024 insertions(+), 1991 deletions(-) delete mode 100644 graph-explorer/pkg/config/mappings.go create mode 100644 graph-explorer/pkg/logging/logging.go delete mode 100644 idp/pkg/config/mappings.go create mode 100644 idp/pkg/logging/logging.go rename idp/pkg/{log => logging}/logrus_wrapper.go (98%) delete mode 100644 proxy/pkg/config/mappings.go create mode 100644 proxy/pkg/logging/logging.go delete mode 100644 settings/pkg/config/mappings.go create mode 100644 settings/pkg/logging/logging.go create mode 100644 storage/pkg/logging/logging.go delete mode 100644 store/pkg/config/mappings.go create mode 100644 store/pkg/logging/logging.go delete mode 100644 thumbnails/pkg/config/mappings.go create mode 100644 thumbnails/pkg/logging/logging.go delete mode 100644 web/pkg/config/mappings.go create mode 100644 web/pkg/logging/logging.go delete mode 100644 webdav/pkg/config/mappings.go create mode 100644 webdav/pkg/logging/logging.go diff --git a/accounts/pkg/command/root.go b/accounts/pkg/command/root.go index 4b0eb6a43..97088ea20 100644 --- a/accounts/pkg/command/root.go +++ b/accounts/pkg/command/root.go @@ -26,6 +26,7 @@ func Execute(cfg *config.Config) error { Email: "support@owncloud.com", }, }, + Before: func(c *cli.Context) error { cfg.Service.Version = version.String return ParseConfig(c, cfg) diff --git a/accounts/pkg/command/server.go b/accounts/pkg/command/server.go index 2f68df37c..aa2c1b0c3 100644 --- a/accounts/pkg/command/server.go +++ b/accounts/pkg/command/server.go @@ -26,12 +26,12 @@ func Server(cfg *config.Config) *cli.Command { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } - cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend) - if err := ParseConfig(ctx, cfg); err != nil { return err } + cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend) + return nil }, Action: func(c *cli.Context) error { diff --git a/accounts/pkg/config/config.go b/accounts/pkg/config/config.go index bc4996081..5c138ad78 100644 --- a/accounts/pkg/config/config.go +++ b/accounts/pkg/config/config.go @@ -8,6 +8,15 @@ import ( "github.com/owncloud/ocis/ocis-pkg/config/defaults" ) +//TODO: use debug config +// Debug defines the available debug configuration. +type Debug struct { + Addr string `ocisConfig:"addr" env:"ACCOUNTS_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"ACCOUNTS_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"ACCOUNTS_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"ACCOUNTS_DEBUG_ZPAGES"` +} + // CORS defines the available cors configuration. type CORS struct { AllowedOrigins []string `ocisConfig:"allowed_origins"` @@ -112,28 +121,28 @@ type Log struct { type Config struct { //*shared.Commons - HTTP HTTP `ocisConfig:"http"` - GRPC GRPC `ocisConfig:"grpc"` - Service Service `ocisConfig:"service"` - Asset Asset `ocisConfig:"asset"` - Log Log `ocisConfig:"log"` - TokenManager TokenManager `ocisConfig:"token_manager"` - Repo Repo `ocisConfig:"repo"` - Index Index `ocisConfig:"index"` - ServiceUser ServiceUser `ocisConfig:"service_user"` - HashDifficulty int `ocisConfig:"hash_difficulty" env:"ACCOUNTS_HASH_DIFFICULTY"` - DemoUsersAndGroups bool `ocisConfig:"demo_users_and_groups" env:"ACCOUNTS_DEMO_USERS_AND_GROUPS"` - Tracing Tracing `ocisConfig:"tracing"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + HTTP HTTP `ocisConfig:"http"` + GRPC GRPC `ocisConfig:"grpc"` + + TokenManager TokenManager `ocisConfig:"token_manager"` + + Asset Asset `ocisConfig:"asset"` + Repo Repo `ocisConfig:"repo"` + Index Index `ocisConfig:"index"` + ServiceUser ServiceUser `ocisConfig:"service_user"` + HashDifficulty int `ocisConfig:"hash_difficulty" env:"ACCOUNTS_HASH_DIFFICULTY"` + DemoUsersAndGroups bool `ocisConfig:"demo_users_and_groups" env:"ACCOUNTS_DEMO_USERS_AND_GROUPS"` Context context.Context Supervised bool } -// New returns a new config. -func New() *Config { - return &Config{} -} - func DefaultConfig() *Config { return &Config{ diff --git a/accounts/pkg/proto/v0/accounts.pb.micro_test.go b/accounts/pkg/proto/v0/accounts.pb.micro_test.go index d15b0ab2d..b8e3eeb4d 100644 --- a/accounts/pkg/proto/v0/accounts.pb.micro_test.go +++ b/accounts/pkg/proto/v0/accounts.pb.micro_test.go @@ -79,7 +79,7 @@ func init() { grpc.Address("localhost:9180"), ) - cfg := config.New() + cfg := config.DefaultConfig() cfg.Repo.Backend = "disk" cfg.Repo.Disk.Path = dataPath cfg.DemoUsersAndGroups = true diff --git a/accounts/pkg/service/v0/accounts_permission_test.go b/accounts/pkg/service/v0/accounts_permission_test.go index f339855f3..0cb477468 100644 --- a/accounts/pkg/service/v0/accounts_permission_test.go +++ b/accounts/pkg/service/v0/accounts_permission_test.go @@ -31,8 +31,7 @@ var ( ) func init() { - cfg := config.New() - cfg.Service.Name = "accounts" + cfg := config.DefaultConfig() cfg.Repo.Backend = "disk" cfg.Repo.Disk.Path = dataPath logger := olog.NewLogger(olog.Color(true), olog.Pretty(true)) diff --git a/glauth/cmd/glauth/main.go b/glauth/cmd/glauth/main.go index f2abd5e20..6a2a83249 100644 --- a/glauth/cmd/glauth/main.go +++ b/glauth/cmd/glauth/main.go @@ -8,7 +8,7 @@ import ( ) func main() { - if err := command.Execute(config.New()); err != nil { + if err := command.Execute(config.DefaultConfig()); err != nil { os.Exit(1) } } diff --git a/glauth/pkg/command/root.go b/glauth/pkg/command/root.go index c90a6c850..b7b5fef7c 100644 --- a/glauth/pkg/command/root.go +++ b/glauth/pkg/command/root.go @@ -26,10 +26,12 @@ func Execute(cfg *config.Config) error { Email: "support@owncloud.com", }, }, + Before: func(c *cli.Context) error { cfg.Service.Version = version.String - return nil + return ParseConfig(c, cfg) }, + Commands: []*cli.Command{ Server(cfg), Health(cfg), @@ -89,7 +91,7 @@ type SutureService struct { // NewSutureService creates a new glauth.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.GLAuth.Commons = cfg.Commons + //cfg.GLAuth.Commons = cfg.Commons return SutureService{ cfg: cfg.GLAuth, } diff --git a/glauth/pkg/config/config.go b/glauth/pkg/config/config.go index 19850fdc3..4876413f6 100644 --- a/glauth/pkg/config/config.go +++ b/glauth/pkg/config/config.go @@ -84,25 +84,24 @@ type FallbackBackend struct { type Config struct { *shared.Commons - Log Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - Service Service `ocisConfig:"service"` - Tracing Tracing `ocisConfig:"tracing"` - Ldap Ldap `ocisConfig:"ldap"` - Ldaps Ldaps `ocisConfig:"ldaps"` - Backend Backend `ocisConfig:"backend"` - Fallback FallbackBackend `ocisConfig:"fallback"` - RoleBundleUUID string `ocisConfig:"role_bundle_uuid" env:"GLAUTH_ROLE_BUNDLE_ID"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + Ldap Ldap `ocisConfig:"ldap"` + Ldaps Ldaps `ocisConfig:"ldaps"` + + Backend Backend `ocisConfig:"backend"` + Fallback FallbackBackend `ocisConfig:"fallback"` + + RoleBundleUUID string `ocisConfig:"role_bundle_uuid" env:"GLAUTH_ROLE_BUNDLE_ID"` Context context.Context Supervised bool } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} -} - func DefaultConfig() *Config { return &Config{ Debug: Debug{ diff --git a/graph-explorer/pkg/command/health.go b/graph-explorer/pkg/command/health.go index 60f4cc925..54ad47fe9 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/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/graph-explorer/pkg/command/root.go b/graph-explorer/pkg/command/root.go index d1c0e0ff6..621958d00 100644 --- a/graph-explorer/pkg/command/root.go +++ b/graph-explorer/pkg/command/root.go @@ -4,12 +4,13 @@ import ( "context" "os" + "github.com/imdario/mergo" "github.com/owncloud/ocis/graph-explorer/pkg/config" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" + "github.com/wkloucek/envdecode" ) // Execute is the entry point for the graph-explorer command. @@ -25,10 +26,12 @@ func Execute(cfg *config.Config) error { Email: "support@owncloud.com", }, }, + Before: func(c *cli.Context) error { cfg.Service.Version = version.String return ParseConfig(c, cfg) }, + Commands: []*cli.Command{ Server(cfg), Health(cfg), @@ -46,27 +49,37 @@ 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("graph-explorer"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - // ParseConfig loads graph configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - conf, err := ociscfg.BindSourcesToStructs("graph-explorer", cfg) + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) if err != nil { return err } - conf.LoadOSEnv(config.GetEnv(), false) - bindings := config.StructMappings(cfg) - return ociscfg.BindEnv(conf, bindings) + // 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. + envCfg := config.Config{} + if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" { + return err + } + + // merge environment variable config on top of the current config + if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil { + return err + } + + return nil } // SutureService allows for the graph-explorer command to be embedded and supervised by a suture supervisor tree. @@ -76,7 +89,7 @@ type SutureService struct { // NewSutureService creates a new graph-explorer.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.GraphExplorer.Log = cfg.Log + //cfg.GraphExplorer.Log = cfg.Log return SutureService{ cfg: cfg.GraphExplorer, } diff --git a/graph-explorer/pkg/command/server.go b/graph-explorer/pkg/command/server.go index c606f1d7c..6b9f80129 100644 --- a/graph-explorer/pkg/command/server.go +++ b/graph-explorer/pkg/command/server.go @@ -6,6 +6,7 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/graph-explorer/pkg/config" + "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" "github.com/owncloud/ocis/graph-explorer/pkg/server/http" @@ -26,9 +27,11 @@ func Server(cfg *config.Config) *cli.Command { return ParseConfig(ctx, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) - tracing.Configure(cfg) - + logger := logging.Configure(cfg.Service.Name, cfg.Log) + err := tracing.Configure(cfg) + if err != nil { + return err + } var ( gr = run.Group{} ctx, cancel = func() (context.Context, context.CancelFunc) { diff --git a/graph-explorer/pkg/config/config.go b/graph-explorer/pkg/config/config.go index f03e4f83e..9a76da101 100644 --- a/graph-explorer/pkg/config/config.go +++ b/graph-explorer/pkg/config/config.go @@ -2,70 +2,73 @@ package config import ( "context" - - "github.com/owncloud/ocis/ocis-pkg/shared" ) // Debug defines the available debug configuration. type Debug struct { - Addr string `ocisConfig:"addr"` - Token string `ocisConfig:"token"` - Pprof bool `ocisConfig:"pprof"` - Zpages bool `ocisConfig:"zpages"` + Addr string `ocisConfig:"addr" env:"GRAPH_EXPLORER_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"GRAPH_EXPLORER_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"GRAPH_EXPLORER_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"GRAPH_EXPLORER_DEBUG_ZPAGES"` } // HTTP defines the available http configuration. type HTTP struct { - Addr string `ocisConfig:"addr"` - Root string `ocisConfig:"root"` + Addr string `ocisConfig:"addr" env:"GRAPH_EXPLORER_HTTP_ADDR"` + Root string `ocisConfig:"root" env:"GRAPH_EXPLORER_HTTP_ROOT"` Namespace string } // Service defines the available service configuration. type Service struct { - Name string `ocisConfig:"name"` - Version string `ocisConfig:"version"` + Name string + Version string } // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` - Service string `ocisConfig:"service"` + Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;GRAPH_EXPLORER_TRACING_ENABLED"` + Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;GRAPH_EXPLORER_TRACING_TYPE"` + Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;GRAPH_EXPLORER_TRACING_ENDPOINT"` + Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;GRAPH_EXPLORER_TRACING_COLLECTOR"` + Service string `ocisConfig:"service" env:"GRAPH_EXPLORER_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name? +} + +// Log defines the available log configuration. +type Log struct { + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;GRAPH_EXPLORER_LOG_LEVEL"` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;GRAPH_EXPLORER_LOG_PRETTY"` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;GRAPH_EXPLORER_LOG_COLOR"` + File string `mapstructure:"file" env:"OCIS_LOG_FILE;GRAPH_EXPLORER_LOG_FILE"` } // GraphExplorer defines the available graph-explorer configuration. type GraphExplorer struct { - ClientID string `ocisConfig:"client_id"` - Issuer string `ocisConfig:"issuer"` - GraphURLBase string `ocisConfig:"graph_url_base"` - GraphURLPath string `ocisConfig:"graph_url_path"` + ClientID string `ocisConfig:"client_id" env:"GRAPH_EXPLORER_CLIENT_ID"` + Issuer string `ocisConfig:"issuer" env:"OCIS_URL;GRAPH_EXPLORER_ISSUER"` + GraphURLBase string `ocisConfig:"graph_url_base" env:"OCIS_URL;GRAPH_EXPLORER_GRAPH_URL_BASE"` + GraphURLPath string `ocisConfig:"graph_url_path" env:"GRAPH_EXPLORER_GRAPH_URL_PATH"` } // Config combines all available configuration parts. type Config struct { - Log shared.Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - HTTP HTTP `ocisConfig:"http"` - Service Service `ocisConfig:"service"` - Tracing Tracing `ocisConfig:"tracing"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + HTTP HTTP `ocisConfig:"http"` + GraphExplorer GraphExplorer `ocisConfig:"graph_explorer"` Context context.Context Supervised bool } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} -} - // DefaultConfig provides with a working version of a config. func DefaultConfig() *Config { return &Config{ - Log: shared.Log{}, Debug: Debug{ Addr: "127.0.0.1:9136", Token: "", @@ -78,7 +81,7 @@ func DefaultConfig() *Config { Namespace: "com.owncloud.web", }, Service: Service{ - Name: "graph", + Name: "graph-explorer", }, Tracing: Tracing{ Type: "jaeger", @@ -94,14 +97,3 @@ 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...) - } - - return r -} diff --git a/graph-explorer/pkg/config/mappings.go b/graph-explorer/pkg/config/mappings.go deleted file mode 100644 index 85014e61b..000000000 --- a/graph-explorer/pkg/config/mappings.go +++ /dev/null @@ -1,96 +0,0 @@ -package config - -import "github.com/owncloud/ocis/ocis-pkg/shared" - -// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the -// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets -// us propagate changes easier. -func StructMappings(cfg *Config) []shared.EnvBinding { - return structMappings(cfg) -} - -// structMappings binds a set of environment variables to a destination on cfg. -func structMappings(cfg *Config) []shared.EnvBinding { - return []shared.EnvBinding{ - { - EnvVars: []string{"OCIS_LOG_LEVEL", "GRAPH_EXPLORER_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - { - EnvVars: []string{"OCIS_LOG_PRETTY", "GRAPH_EXPLORER_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - { - EnvVars: []string{"OCIS_LOG_COLOR", "GRAPH_EXPLORER_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - { - EnvVars: []string{"OCIS_LOG_FILE", "GRAPH_EXPLORER_LOG_FILE"}, - Destination: &cfg.Log.File, - }, - { - EnvVars: []string{"OCIS_TRACING_ENABLED", "GRAPH_EXPLORER_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - { - EnvVars: []string{"OCIS_TRACING_TYPE", "GRAPH_EXPLORER_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - { - EnvVars: []string{"OCIS_TRACING_ENDPOINT", "GRAPH_EXPLORER_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - { - EnvVars: []string{"OCIS_TRACING_COLLECTOR", "GRAPH_EXPLORER_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_TRACING_SERVICE"}, - Destination: &cfg.Tracing.Service, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_DEBUG_TOKEN"}, - Destination: &cfg.Debug.Token, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_DEBUG_PPROF"}, - Destination: &cfg.Debug.Pprof, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_DEBUG_ZPAGES"}, - Destination: &cfg.Debug.Zpages, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_HTTP_ADDR"}, - Destination: &cfg.HTTP.Addr, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_HTTP_ROOT"}, - Destination: &cfg.HTTP.Root, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_NAMESPACE"}, - Destination: &cfg.HTTP.Namespace, - }, - { - EnvVars: []string{"OCIS_URL", "GRAPH_EXPLORER_ISSUER"}, - Destination: &cfg.GraphExplorer.Issuer, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_CLIENT_ID"}, - Destination: &cfg.GraphExplorer.ClientID, - }, - { - EnvVars: []string{"OCIS_URL", "GRAPH_EXPLORER_GRAPH_URL_BASE"}, - Destination: &cfg.GraphExplorer.GraphURLBase, - }, - { - EnvVars: []string{"GRAPH_EXPLORER_GRAPH_URL_PATH"}, - Destination: &cfg.GraphExplorer.GraphURLPath, - }, - } -} diff --git a/graph-explorer/pkg/logging/logging.go b/graph-explorer/pkg/logging/logging.go new file mode 100644 index 000000000..8d3175cbf --- /dev/null +++ b/graph-explorer/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/graph-explorer/pkg/config" + "github.com/owncloud/ocis/ocis-pkg/log" +) + +// 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/graph/cmd/graph/main.go b/graph/cmd/graph/main.go index c43db0993..ec85f2371 100644 --- a/graph/cmd/graph/main.go +++ b/graph/cmd/graph/main.go @@ -8,7 +8,7 @@ import ( ) func main() { - if err := command.Execute(config.New()); err != nil { + if err := command.Execute(config.DefaultConfig()); err != nil { os.Exit(1) } } diff --git a/graph/pkg/command/health.go b/graph/pkg/command/health.go index 509146e3f..18cc12bae 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/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/graph/pkg/command/root.go b/graph/pkg/command/root.go index 04906877a..7168eb16a 100644 --- a/graph/pkg/command/root.go +++ b/graph/pkg/command/root.go @@ -10,7 +10,6 @@ import ( "github.com/owncloud/ocis/graph/pkg/config" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/urfave/cli/v2" ) @@ -28,10 +27,12 @@ func Execute(cfg *config.Config) error { Email: "support@owncloud.com", }, }, + Before: func(c *cli.Context) error { cfg.Service.Version = version.String return ParseConfig(c, cfg) }, + Commands: []*cli.Command{ Server(cfg), Health(cfg), @@ -49,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("graph"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - // ParseConfig loads graph configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) @@ -100,7 +90,7 @@ type SutureService struct { // NewSutureService creates a new graph.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.Graph.Commons = cfg.Commons + //cfg.Graph.Commons = cfg.Commons return SutureService{ cfg: cfg.Graph, } diff --git a/graph/pkg/config/config.go b/graph/pkg/config/config.go index 5185a6fed..c3d22789a 100644 --- a/graph/pkg/config/config.go +++ b/graph/pkg/config/config.go @@ -90,25 +90,24 @@ type Identity struct { type Config struct { *shared.Commons - Log Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - HTTP HTTP `ocisConfig:"http"` - Service Service `ocisConfig:"service"` - Tracing Tracing `ocisConfig:"tracing"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + HTTP HTTP `ocisConfig:"http"` + Reva Reva `ocisConfig:"reva"` TokenManager TokenManager `ocisConfig:"token_manager"` - Spaces Spaces `ocisConfig:"spaces"` - Identity Identity `ocisConfig:"identity"` + + Spaces Spaces `ocisConfig:"spaces"` + Identity Identity `ocisConfig:"identity"` Context context.Context Supervised bool } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} -} - func DefaultConfig() *Config { return &Config{ Debug: Debug{ diff --git a/idp/pkg/command/health.go b/idp/pkg/command/health.go index 813470bd4..fcb337253 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/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/idp/pkg/command/root.go b/idp/pkg/command/root.go index e0d5ba775..77c740592 100644 --- a/idp/pkg/command/root.go +++ b/idp/pkg/command/root.go @@ -4,14 +4,13 @@ import ( "context" "os" - "github.com/owncloud/ocis/ocis-pkg/shared" - + "github.com/imdario/mergo" "github.com/owncloud/ocis/idp/pkg/config" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" + "github.com/wkloucek/envdecode" ) // Execute is the entry point for the ocis-idp command. @@ -27,9 +26,10 @@ func Execute(cfg *config.Config) error { Email: "support@owncloud.com", }, }, + Before: func(c *cli.Context) error { cfg.Service.Version = version.String - return nil + return ParseConfig(c, cfg) }, Commands: []*cli.Command{ @@ -52,41 +52,37 @@ 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("idp"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - // ParseConfig loads idp configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - conf, err := ociscfg.BindSourcesToStructs("idp", cfg) + _, 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 = &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{} - } + //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(cfg), false) + envCfg := config.Config{} + if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" { + return err + } - bindings := config.StructMappings(cfg) - return ociscfg.BindEnv(conf, bindings) + // merge environment variable config on top of the current config + if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil { + return err + } + + return nil } // SutureService allows for the idp command to be embedded and supervised by a suture supervisor tree. @@ -96,7 +92,7 @@ type SutureService struct { // NewSutureService creates a new idp.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.IDP.Commons = cfg.Commons + //cfg.IDP.Commons = cfg.Commons return SutureService{ cfg: cfg.IDP, } diff --git a/idp/pkg/command/server.go b/idp/pkg/command/server.go index d3b62034f..0c91b4b5b 100644 --- a/idp/pkg/command/server.go +++ b/idp/pkg/command/server.go @@ -6,6 +6,7 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/idp/pkg/config" + "github.com/owncloud/ocis/idp/pkg/logging" "github.com/owncloud/ocis/idp/pkg/metrics" "github.com/owncloud/ocis/idp/pkg/server/debug" "github.com/owncloud/ocis/idp/pkg/server/http" @@ -30,10 +31,11 @@ func Server(cfg *config.Config) *cli.Command { return nil }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) - - tracing.Configure(cfg) - + logger := logging.Configure(cfg.Service.Name, cfg.Log) + err := tracing.Configure(cfg) + if err != nil { + return err + } var ( gr = run.Group{} ctx, cancel = func() (context.Context, context.CancelFunc) { diff --git a/idp/pkg/config/config.go b/idp/pkg/config/config.go index 510f06e6b..6793294e6 100644 --- a/idp/pkg/config/config.go +++ b/idp/pkg/config/config.go @@ -11,113 +11,139 @@ import ( // Debug defines the available debug configuration. type Debug struct { - Addr string `ocisConfig:"addr"` - Token string `ocisConfig:"token"` - Pprof bool `ocisConfig:"pprof"` - Zpages bool `ocisConfig:"zpages"` + Addr string `ocisConfig:"addr" env:"IDP_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"IDP_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"IDP_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"IDP_DEBUG_ZPAGES"` } // HTTP defines the available http configuration. type HTTP struct { - Addr string `ocisConfig:"addr"` - Root string `ocisConfig:"root"` - Namespace string `ocisConfig:"namespace"` - TLSCert string `ocisConfig:"tls_cert"` - TLSKey string `ocisConfig:"tls_key"` - TLS bool `ocisConfig:"tls"` + Addr string `ocisConfig:"addr" env:"IDP_HTTP_ADDR"` + Root string `ocisConfig:"root" env:"IDP_HTTP_ROOT"` + Namespace string + TLSCert string `ocisConfig:"tls_cert" env:"IDP_TRANSPORT_TLS_CERT"` + TLSKey string `ocisConfig:"tls_key" env:"IDP_TRANSPORT_TLS_KEY"` + TLS bool `ocisConfig:"tls" env:"IDP_TLS"` } // Service defines the available service configuration. type Service struct { - Name string `ocisConfig:"name"` - Version string `ocisConfig:"version"` + Name string + Version string } // Ldap defines the available LDAP configuration. type Ldap struct { - URI string `ocisConfig:"uri"` - BindDN string `ocisConfig:"bind_dn"` - BindPassword string `ocisConfig:"bind_password"` - BaseDN string `ocisConfig:"base_dn"` - Scope string `ocisConfig:"scope"` - LoginAttribute string `ocisConfig:"login_attribute"` - EmailAttribute string `ocisConfig:"email_attribute"` - NameAttribute string `ocisConfig:"name_attribute"` - UUIDAttribute string `ocisConfig:"uuid_attribute"` - UUIDAttributeType string `ocisConfig:"uuid_attribute_type"` - Filter string `ocisConfig:"filter"` + URI string `ocisConfig:"uri" env:"IDP_LDAP_URI"` + + BindDN string `ocisConfig:"bind_dn" env:"IDP_LDAP_BIND_DN"` + BindPassword string `ocisConfig:"bind_password" env:"IDP_LDAP_BIND_PASSWORD"` + + BaseDN string `ocisConfig:"base_dn" env:"IDP_LDAP_BASE_DN"` + Scope string `ocisConfig:"scope" env:"IDP_LDAP_SCOPE"` + + LoginAttribute string `ocisConfig:"login_attribute" env:"IDP_LDAP_LOGIN_ATTRIBUTE"` + EmailAttribute string `ocisConfig:"email_attribute" env:"IDP_LDAP_EMAIL_ATTRIBUTE"` + NameAttribute string `ocisConfig:"name_attribute" env:"IDP_LDAP_NAME_ATTRIBUTE"` + UUIDAttribute string `ocisConfig:"uuid_attribute" env:"IDP_LDAP_UUID_ATTRIBUTE"` + UUIDAttributeType string `ocisConfig:"uuid_attribute_type" env:"IDP_LDAP_UUID_ATTRIBUTE_TYPE"` + + Filter string `ocisConfig:"filter" env:"IDP_LDAP_FILTER"` } // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` - Service string `ocisConfig:"service"` + Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;IDP_TRACING_ENABLED"` + Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;IDP_TRACING_TYPE"` + Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;IDP_TRACING_ENDPOINT"` + Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;IDP_TRACING_COLLECTOR"` + Service string `ocisConfig:"service" env:"IDP_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name? +} + +// Log defines the available log configuration. +type Log struct { + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;IDP_LOG_LEVEL"` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;IDP_LOG_PRETTY"` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;IDP_LOG_COLOR"` + File string `mapstructure:"file" env:"OCIS_LOG_FILE;IDP_LOG_FILE"` } // Asset defines the available asset configuration. type Asset struct { - Path string `ocisConfig:"asset"` + Path string `ocisConfig:"asset" env:"IDP_ASSET_PATH"` } type Settings struct { - Iss string `ocisConfig:"iss"` - IdentityManager string `ocisConfig:"identity_manager"` - URIBasePath string `ocisConfig:"uri_base_path"` - SignInURI string `ocisConfig:"sign_in_uri"` - SignedOutURI string `ocisConfig:"signed_out_uri"` - AuthorizationEndpointURI string `ocisConfig:"authorization_endpoint_uri"` - EndsessionEndpointURI string `ocisConfig:"end_session_endpoint_uri"` - Insecure bool `ocisConfig:"insecure"` - TrustedProxy []string `ocisConfig:"trusted_proxy"` - AllowScope []string `ocisConfig:"allow_scope"` - AllowClientGuests bool `ocisConfig:"allow_client_guests"` - AllowDynamicClientRegistration bool `ocisConfig:"allow_dynamic_client_registration"` - EncryptionSecretFile string `ocisConfig:"encrypt_secret_file"` - Listen string `ocisConfig:"listen"` - IdentifierClientDisabled bool `ocisConfig:"identifier_client_disabled"` - IdentifierClientPath string `ocisConfig:"identifier_client_path"` - IdentifierRegistrationConf string `ocisConfig:"identifier_registration_conf"` - IdentifierScopesConf string `ocisConfig:"identifier_scopes_conf"` - IdentifierDefaultBannerLogo string `ocisConfig:"identifier_default_banner_logo"` - IdentifierDefaultSignInPageText string `ocisConfig:"identifier_default_sign_in_page_text"` - IdentifierDefaultUsernameHintText string `ocisConfig:"identifier_default_username_hint_text"` - SigningKid string `ocisConfig:"sign_in_kid"` - SigningMethod string `ocisConfig:"sign_in_method"` - SigningPrivateKeyFiles []string `ocisConfig:"sign_in_private_key_files"` - ValidationKeysPath string `ocisConfig:"validation_keys_path"` - CookieBackendURI string `ocisConfig:"cookie_backend_uri"` - CookieNames []string `ocisConfig:"cookie_names"` - AccessTokenDurationSeconds uint64 `ocisConfig:"access_token_duration_seconds"` - IDTokenDurationSeconds uint64 `ocisConfig:"id_token_duration_seconds"` - RefreshTokenDurationSeconds uint64 `ocisConfig:"refresh_token_duration_seconds"` - DyamicClientSecretDurationSeconds uint64 `ocisConfig:"dynamic_client_secret_duration_seconds"` + // don't change the order of elements in this struct + // it needs to match github.com/libregraph/lico/bootstrap.Settings + + Iss string `ocisConfig:"iss" env:"OCIS_URL;IDP_ISS"` + + IdentityManager string `ocisConfig:"identity_manager" env:"IDP_IDENTITY_MANAGER"` + + URIBasePath string `ocisConfig:"uri_base_path" env:"IDP_URI_BASE_PATH"` + + SignInURI string `ocisConfig:"sign_in_uri" env:"IDP_SIGN_IN_URI"` + SignedOutURI string `ocisConfig:"signed_out_uri" env:"IDP_SIGN_OUT_URI"` + + AuthorizationEndpointURI string `ocisConfig:"authorization_endpoint_uri" env:"IDP_ENDPOINT_URI"` + EndsessionEndpointURI string `ocisConfig:"end_session_endpoint_uri" env:"IDP_ENDSESSION_ENDPOINT_URI"` + + Insecure bool `ocisConfig:"insecure" env:"IDP_INSECURE"` + + TrustedProxy []string `ocisConfig:"trusted_proxy"` //TODO: how to configure this via env? + + AllowScope []string `ocisConfig:"allow_scope"` // TODO: is this even needed? + AllowClientGuests bool `ocisConfig:"allow_client_guests" env:"IDP_ALLOW_CLIENT_GUESTS"` + AllowDynamicClientRegistration bool `ocisConfig:"allow_dynamic_client_registration" env:"IDP_ALLOW_DYNAMIC_CLIENT_REGISTRATION"` + + EncryptionSecretFile string `ocisConfig:"encrypt_secret_file" env:"IDP_ENCRYPTION_SECRET"` + + Listen string `ocisConfig:"listen"` //TODO: is this even needed? + + IdentifierClientDisabled bool `ocisConfig:"identifier_client_disabled" env:"IDP_DISABLE_IDENTIFIER_WEBAPP"` + IdentifierClientPath string `ocisConfig:"identifier_client_path" env:"IDP_IDENTIFIER_CLIENT_PATH"` + IdentifierRegistrationConf string `ocisConfig:"identifier_registration_conf" env:"IDP_IDENTIFIER_REGISTRATION_CONF"` + IdentifierScopesConf string `ocisConfig:"identifier_scopes_conf" env:"IDP_IDENTIFIER_SCOPES_CONF"` + IdentifierDefaultBannerLogo string `ocisConfig:"identifier_default_banner_logo"` // TODO: is this even needed? + IdentifierDefaultSignInPageText string `ocisConfig:"identifier_default_sign_in_page_text"` // TODO: is this even needed? + IdentifierDefaultUsernameHintText string `ocisConfig:"identifier_default_username_hint_text"` // TODO: is this even needed? + + SigningKid string `ocisConfig:"sign_in_kid" env:"IDP_SIGNING_KID"` + SigningMethod string `ocisConfig:"sign_in_method" env:"IDP_SIGNING_METHOD"` + SigningPrivateKeyFiles []string `ocisConfig:"sign_in_private_key_files"` // TODO: is this even needed? + ValidationKeysPath string `ocisConfig:"validation_keys_path" env:"IDP_VALIDATION_KEYS_PATH"` + + CookieBackendURI string `ocisConfig:"cookie_backend_uri"` // TODO: is this even needed? + CookieNames []string `ocisConfig:"cookie_names"` // TODO: is this even needed? + + AccessTokenDurationSeconds uint64 `ocisConfig:"access_token_duration_seconds" env:"IDP_ACCESS_TOKEN_EXPIRATION"` + IDTokenDurationSeconds uint64 `ocisConfig:"id_token_duration_seconds" env:"IDP_ID_TOKEN_EXPIRATION"` + RefreshTokenDurationSeconds uint64 `ocisConfig:"refresh_token_duration_seconds" env:"IDP_REFRESH_TOKEN_EXPIRATION"` + DyamicClientSecretDurationSeconds uint64 `ocisConfig:"dynamic_client_secret_duration_seconds" env:""` } // Config combines all available configuration parts. type Config struct { *shared.Commons - Log *shared.Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - HTTP HTTP `ocisConfig:"http"` - Tracing Tracing `ocisConfig:"tracing"` - Asset Asset `ocisConfig:"asset"` - IDP Settings `ocisConfig:"idp"` - Ldap Ldap `ocisConfig:"ldap"` - Service Service `ocisConfig:"service"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + HTTP HTTP `ocisConfig:"http"` + + Asset Asset `ocisConfig:"asset"` + IDP Settings `ocisConfig:"idp"` + Ldap Ldap `ocisConfig:"ldap"` Context context.Context Supervised bool } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} -} - func DefaultConfig() *Config { return &Config{ Debug: Debug{ diff --git a/idp/pkg/config/mappings.go b/idp/pkg/config/mappings.go deleted file mode 100644 index c5b01cf74..000000000 --- a/idp/pkg/config/mappings.go +++ /dev/null @@ -1,239 +0,0 @@ -package config - -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(cfg *Config) []string { - var r = make([]string, len(structMappings(cfg))) - for i := range structMappings(cfg) { - r = append(r, structMappings(cfg)[i].EnvVars...) - } - - return r -} - -// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the -// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets -// us propagate changes easier. -func StructMappings(cfg *Config) []shared.EnvBinding { - return structMappings(cfg) -} - -// structMappings binds a set of environment variables to a destination on cfg. -func structMappings(cfg *Config) []shared.EnvBinding { - return []shared.EnvBinding{ - { - EnvVars: []string{"OCIS_LOG_LEVEL", "IDP_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - { - EnvVars: []string{"OCIS_LOG_PRETTY", "IDP_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - { - EnvVars: []string{"OCIS_LOG_COLOR", "IDP_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - { - EnvVars: []string{"OCIS_LOG_FILE", "IDP_LOG_FILE"}, - Destination: &cfg.Log.File, - }, - { - EnvVars: []string{"IDP_CONFIG_FILE"}, - Destination: &cfg.File, - }, - { - EnvVars: []string{"OCIS_TRACING_ENABLED", "IDP_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - { - EnvVars: []string{"OCIS_TRACING_TYPE", "IDP_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - { - EnvVars: []string{"OCIS_TRACING_ENDPOINT", "IDP_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - { - EnvVars: []string{"OCIS_TRACING_COLLECTOR", "IDP_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - { - EnvVars: []string{"IDP_TRACING_SERVICE"}, - Destination: &cfg.Tracing.Service, - }, - { - EnvVars: []string{"IDP_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - { - EnvVars: []string{"IDP_DEBUG_TOKEN"}, - Destination: &cfg.Debug.Token, - }, - { - EnvVars: []string{"IDP_DEBUG_PPROF"}, - Destination: &cfg.Debug.Pprof, - }, - { - EnvVars: []string{"IDP_DEBUG_ZPAGES"}, - Destination: &cfg.Debug.Zpages, - }, - { - EnvVars: []string{"IDP_HTTP_ADDR"}, - Destination: &cfg.HTTP.Addr, - }, - { - EnvVars: []string{"IDP_HTTP_ROOT"}, - Destination: &cfg.HTTP.Root, - }, - { - EnvVars: []string{"IDP_HTTP_NAMESPACE"}, - Destination: &cfg.HTTP.Namespace, - }, - { - EnvVars: []string{"IDP_IDENTITY_MANAGER"}, - Destination: &cfg.IDP.IdentityManager, - }, - { - EnvVars: []string{"IDP_LDAP_URI"}, - Destination: &cfg.Ldap.URI, - }, - { - EnvVars: []string{"IDP_LDAP_BIND_DN"}, - Destination: &cfg.Ldap.BindDN, - }, - { - EnvVars: []string{"IDP_LDAP_BIND_PASSWORD"}, - Destination: &cfg.Ldap.BindPassword, - }, - { - EnvVars: []string{"IDP_LDAP_BASE_DN"}, - Destination: &cfg.Ldap.BaseDN, - }, - { - EnvVars: []string{"IDP_LDAP_SCOPE"}, - Destination: &cfg.Ldap.Scope, - }, - { - EnvVars: []string{"IDP_LDAP_LOGIN_ATTRIBUTE"}, - Destination: &cfg.Ldap.LoginAttribute, - }, - { - EnvVars: []string{"IDP_LDAP_EMAIL_ATTRIBUTE"}, - Destination: &cfg.Ldap.EmailAttribute, - }, - { - EnvVars: []string{"IDP_LDAP_NAME_ATTRIBUTE"}, - Destination: &cfg.Ldap.NameAttribute, - }, - { - EnvVars: []string{"IDP_LDAP_UUID_ATTRIBUTE"}, - Destination: &cfg.Ldap.UUIDAttribute, - }, - { - EnvVars: []string{"IDP_LDAP_UUID_ATTRIBUTE_TYPE"}, - Destination: &cfg.Ldap.UUIDAttributeType, - }, - { - EnvVars: []string{"IDP_LDAP_FILTER"}, - Destination: &cfg.Ldap.Filter, - }, - { - EnvVars: []string{"IDP_TRANSPORT_TLS_CERT"}, - Destination: &cfg.HTTP.TLSCert, - }, - { - EnvVars: []string{"IDP_TRANSPORT_TLS_KEY"}, - Destination: &cfg.HTTP.TLSKey, - }, - { - EnvVars: []string{"OCIS_URL", "IDP_ISS"}, // IDP_ISS takes precedence over OCIS_URL - Destination: &cfg.IDP.Iss, - }, - { - EnvVars: []string{"IDP_SIGNING_KID"}, - Destination: &cfg.IDP.SigningKid, - }, - { - EnvVars: []string{"IDP_VALIDATION_KEYS_PATH"}, - Destination: &cfg.IDP.ValidationKeysPath, - }, - { - EnvVars: []string{"IDP_ENCRYPTION_SECRET"}, - Destination: &cfg.IDP.EncryptionSecretFile, - }, - { - EnvVars: []string{"IDP_SIGNING_METHOD"}, - Destination: &cfg.IDP.SigningMethod, - }, - { - EnvVars: []string{"IDP_URI_BASE_PATH"}, - Destination: &cfg.IDP.URIBasePath, - }, - { - EnvVars: []string{"IDP_SIGN_IN_URI"}, - Destination: &cfg.IDP.SignInURI, - }, - { - EnvVars: []string{"IDP_SIGN_OUT_URI"}, - Destination: &cfg.IDP.SignedOutURI, - }, - { - EnvVars: []string{"IDP_ENDPOINT_URI"}, - Destination: &cfg.IDP.AuthorizationEndpointURI, - }, - { - EnvVars: []string{"IDP_ENDSESSION_ENDPOINT_URI"}, - Destination: &cfg.IDP.EndsessionEndpointURI, - }, - { - EnvVars: []string{"IDP_ASSET_PATH"}, - Destination: &cfg.Asset.Path, - }, - { - EnvVars: []string{"IDP_IDENTIFIER_CLIENT_PATH"}, - Destination: &cfg.IDP.IdentifierClientPath, - }, - { - EnvVars: []string{"IDP_IDENTIFIER_REGISTRATION_CONF"}, - Destination: &cfg.IDP.IdentifierRegistrationConf, - }, - { - EnvVars: []string{"IDP_IDENTIFIER_SCOPES_CONF"}, - Destination: &cfg.IDP.IdentifierScopesConf, - }, - { - EnvVars: []string{"IDP_INSECURE"}, - Destination: &cfg.IDP.Insecure, - }, - { - EnvVars: []string{"IDP_TLS"}, - Destination: &cfg.HTTP.TLS, - }, - { - EnvVars: []string{"IDP_ALLOW_CLIENT_GUESTS"}, - Destination: &cfg.IDP.AllowClientGuests, - }, - { - EnvVars: []string{"IDP_ALLOW_DYNAMIC_CLIENT_REGISTRATION"}, - Destination: &cfg.IDP.AllowDynamicClientRegistration, - }, - { - EnvVars: []string{"IDP_DISABLE_IDENTIFIER_WEBAPP"}, - Destination: &cfg.IDP.IdentifierClientDisabled, - }, - { - EnvVars: []string{"IDP_ACCESS_TOKEN_EXPIRATION"}, - Destination: &cfg.IDP.AccessTokenDurationSeconds, - }, - { - EnvVars: []string{"IDP_ID_TOKEN_EXPIRATION"}, - Destination: &cfg.IDP.IDTokenDurationSeconds, - }, - { - EnvVars: []string{"IDP_REFRESH_TOKEN_EXPIRATION"}, - Destination: &cfg.IDP.RefreshTokenDurationSeconds, - }, - } -} diff --git a/idp/pkg/logging/logging.go b/idp/pkg/logging/logging.go new file mode 100644 index 000000000..cbcc64833 --- /dev/null +++ b/idp/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/idp/pkg/config" + "github.com/owncloud/ocis/ocis-pkg/log" +) + +// 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/idp/pkg/log/logrus_wrapper.go b/idp/pkg/logging/logrus_wrapper.go similarity index 98% rename from idp/pkg/log/logrus_wrapper.go rename to idp/pkg/logging/logrus_wrapper.go index 71f29be2c..38e302005 100644 --- a/idp/pkg/log/logrus_wrapper.go +++ b/idp/pkg/logging/logrus_wrapper.go @@ -1,9 +1,10 @@ -package log +package logging import ( + "io/ioutil" + "github.com/rs/zerolog" "github.com/sirupsen/logrus" - "io/ioutil" ) type levelMap map[logrus.Level]zerolog.Level diff --git a/idp/pkg/server/http/server.go b/idp/pkg/server/http/server.go index 107643e33..0d9cf8b2f 100644 --- a/idp/pkg/server/http/server.go +++ b/idp/pkg/server/http/server.go @@ -70,7 +70,7 @@ func Server(opts ...Option) (http.Service, error) { { handle = svc.NewInstrument(handle, options.Metrics) - handle = svc.NewLogging(handle, options.Logger) + handle = svc.NewLoggingHandler(handle, options.Logger) } if err := micro.RegisterHandler(service.Server(), handle); err != nil { diff --git a/idp/pkg/service/v0/logging.go b/idp/pkg/service/v0/logging.go index d5098e2fe..f3dfab793 100644 --- a/idp/pkg/service/v0/logging.go +++ b/idp/pkg/service/v0/logging.go @@ -7,19 +7,19 @@ import ( ) // NewLogging returns a service that logs messages. -func NewLogging(next Service, logger log.Logger) Service { - return logging{ +func NewLoggingHandler(next Service, logger log.Logger) Service { + return loggingHandler{ next: next, logger: logger, } } -type logging struct { +type loggingHandler struct { next Service logger log.Logger } // ServeHTTP implements the Service interface. -func (l logging) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (l loggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { l.next.ServeHTTP(w, r) } diff --git a/idp/pkg/service/v0/service.go b/idp/pkg/service/v0/service.go index 0f949a91a..24fb3132d 100644 --- a/idp/pkg/service/v0/service.go +++ b/idp/pkg/service/v0/service.go @@ -21,7 +21,7 @@ import ( "github.com/libregraph/lico/server" "github.com/owncloud/ocis/idp/pkg/assets" "github.com/owncloud/ocis/idp/pkg/config" - logw "github.com/owncloud/ocis/idp/pkg/log" + "github.com/owncloud/ocis/idp/pkg/logging" "github.com/owncloud/ocis/idp/pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/log" "stash.kopano.io/kgol/rndm" @@ -59,7 +59,7 @@ func NewService(opts ...Option) Service { idpSettings := bootstrap.Settings(options.Config.IDP) bs, err := bootstrap.Boot(ctx, &idpSettings, &licoconfig.Config{ - Logger: logw.Wrap(logger), + Logger: logging.Wrap(logger), }) if err != nil { diff --git a/ocis-pkg/config/config.go b/ocis-pkg/config/config.go index e73c9d07e..40436f6fa 100644 --- a/ocis-pkg/config/config.go +++ b/ocis-pkg/config/config.go @@ -42,6 +42,28 @@ const ( type Mode int +// Service defines the available service configuration. +type Service struct { + Name string + Version string +} + +// Debug defines the available debug configuration. +type Debug struct { + Addr string `ocisConfig:"addr" env:"ACCOUNTS_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"ACCOUNTS_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"ACCOUNTS_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"ACCOUNTS_DEBUG_ZPAGES"` +} + +// Log defines the available log configuration. +type Log struct { + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL"` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY"` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR"` + File string `mapstructure:"file" env:"OCIS_LOG_FILE"` +} + // Runtime configures the oCIS runtime when running in supervised mode. type Runtime struct { Port string `ocisConfig:"port"` @@ -53,13 +75,17 @@ type Runtime struct { type Config struct { *shared.Commons `ocisConfig:"shared"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + Mode Mode // DEPRECATED File string OcisURL string `ocisConfig:"ocis_url"` Registry string `ocisConfig:"registry"` - Log shared.Log `ocisConfig:"log"` - Tracing Tracing `ocisConfig:"tracing"` TokenManager TokenManager `ocisConfig:"token_manager"` Runtime Runtime `ocisConfig:"runtime"` @@ -78,25 +104,6 @@ type Config struct { WebDAV *webdav.Config `ocisConfig:"webdav"` } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{ - Accounts: accounts.DefaultConfig(), - GLAuth: glauth.DefaultConfig(), - Graph: graph.DefaultConfig(), - IDP: idp.DefaultConfig(), - Proxy: proxy.DefaultConfig(), - GraphExplorer: graphExplorer.DefaultConfig(), - OCS: ocs.DefaultConfig(), - Settings: settings.DefaultConfig(), - Web: web.DefaultConfig(), - Store: store.DefaultConfig(), - Thumbnails: thumbnails.DefaultConfig(), - WebDAV: webdav.DefaultConfig(), - Storage: storage.DefaultConfig(), - } -} - func DefaultConfig() *Config { return &Config{ Tracing: Tracing{ @@ -149,6 +156,7 @@ func StructMappings(cfg *Config) []shared.EnvBinding { func structMappings(cfg *Config) []shared.EnvBinding { return []shared.EnvBinding{ + // TODO: transform this too { EnvVars: []string{"OCIS_LOG_LEVEL"}, Destination: &cfg.Log.Level, diff --git a/ocis/pkg/command/accounts.go b/ocis/pkg/command/accounts.go index 40acb44ef..cb3bdc620 100644 --- a/ocis/pkg/command/accounts.go +++ b/ocis/pkg/command/accounts.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/graph.go b/ocis/pkg/command/graph.go index 1b1ae5553..ecc94826c 100644 --- a/ocis/pkg/command/graph.go +++ b/ocis/pkg/command/graph.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/graphexplorer.go b/ocis/pkg/command/graphexplorer.go index e4dc82e02..7833206b9 100644 --- a/ocis/pkg/command/graphexplorer.go +++ b/ocis/pkg/command/graphexplorer.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/ocs.go b/ocis/pkg/command/ocs.go index 17f2d927e..243c27696 100644 --- a/ocis/pkg/command/ocs.go +++ b/ocis/pkg/command/ocs.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/proxy.go b/ocis/pkg/command/proxy.go index 321298689..7458a80d6 100644 --- a/ocis/pkg/command/proxy.go +++ b/ocis/pkg/command/proxy.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/root.go b/ocis/pkg/command/root.go index 7272c357f..ffcbba1b6 100644 --- a/ocis/pkg/command/root.go +++ b/ocis/pkg/command/root.go @@ -5,7 +5,6 @@ import ( "github.com/owncloud/ocis/ocis-pkg/config" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/urfave/cli/v2" @@ -20,9 +19,12 @@ func Execute() error { Version: version.String, Usage: "ownCloud Infinite Scale Stack", Compiled: version.Compiled(), + Before: func(c *cli.Context) error { + //cfg.Service.Version = version.String return ParseConfig(c, cfg) }, + Authors: []*cli.Author{ { Name: "ownCloud GmbH", @@ -51,17 +53,6 @@ func Execute() 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("ocis"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - // ParseConfig loads ocis configuration. func ParseConfig(c *cli.Context, cfg *config.Config) error { conf, err := ociscfg.BindSourcesToStructs("ocis", cfg) @@ -69,6 +60,8 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { return err } + // TODO: use envconfig here too + conf.LoadOSEnv(config.GetEnv(), false) bindings := config.StructMappings(cfg) diff --git a/ocis/pkg/command/server.go b/ocis/pkg/command/server.go index c750340e8..c9262144d 100644 --- a/ocis/pkg/command/server.go +++ b/ocis/pkg/command/server.go @@ -1,11 +1,6 @@ -//go:build !simple -// +build !simple - package command import ( - "github.com/owncloud/ocis/ocis-pkg/shared" - "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/ocis/pkg/runtime" @@ -23,9 +18,9 @@ func Server(cfg *config.Config) *cli.Command { }, Action: func(c *cli.Context) error { - 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 adb37be5b..90bdafc30 100644 --- a/ocis/pkg/command/settings.go +++ b/ocis/pkg/command/settings.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storageappprovider.go b/ocis/pkg/command/storageappprovider.go index 520295965..03d886f3b 100644 --- a/ocis/pkg/command/storageappprovider.go +++ b/ocis/pkg/command/storageappprovider.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storageauthbasic.go b/ocis/pkg/command/storageauthbasic.go index 62cddc5c8..1bfe109db 100644 --- a/ocis/pkg/command/storageauthbasic.go +++ b/ocis/pkg/command/storageauthbasic.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storageauthbearer.go b/ocis/pkg/command/storageauthbearer.go index 329e64467..8b87968e0 100644 --- a/ocis/pkg/command/storageauthbearer.go +++ b/ocis/pkg/command/storageauthbearer.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storageauthmachine.go b/ocis/pkg/command/storageauthmachine.go index 9498c7a01..18b9d8a18 100644 --- a/ocis/pkg/command/storageauthmachine.go +++ b/ocis/pkg/command/storageauthmachine.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storagefrontend.go b/ocis/pkg/command/storagefrontend.go index 29fb13ee2..5fe8ef78c 100644 --- a/ocis/pkg/command/storagefrontend.go +++ b/ocis/pkg/command/storagefrontend.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storagegateway.go b/ocis/pkg/command/storagegateway.go index 4f24f3e49..937305d81 100644 --- a/ocis/pkg/command/storagegateway.go +++ b/ocis/pkg/command/storagegateway.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storagegroupprovider.go b/ocis/pkg/command/storagegroupprovider.go index 530e8148c..6519772a1 100644 --- a/ocis/pkg/command/storagegroupprovider.go +++ b/ocis/pkg/command/storagegroupprovider.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storagepubliclink.go b/ocis/pkg/command/storagepubliclink.go index e4d9dc750..97057ed00 100644 --- a/ocis/pkg/command/storagepubliclink.go +++ b/ocis/pkg/command/storagepubliclink.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storageshares.go b/ocis/pkg/command/storageshares.go index fe38a4bc3..7e94fd9e3 100644 --- a/ocis/pkg/command/storageshares.go +++ b/ocis/pkg/command/storageshares.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storagesharing.go b/ocis/pkg/command/storagesharing.go index 3a15d90ee..d5d479c5c 100644 --- a/ocis/pkg/command/storagesharing.go +++ b/ocis/pkg/command/storagesharing.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storageuserprovider.go b/ocis/pkg/command/storageuserprovider.go index fc423737a..984d7e86a 100644 --- a/ocis/pkg/command/storageuserprovider.go +++ b/ocis/pkg/command/storageuserprovider.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/storageusers.go b/ocis/pkg/command/storageusers.go index a7978fb69..fbcd1d40a 100644 --- a/ocis/pkg/command/storageusers.go +++ b/ocis/pkg/command/storageusers.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/command/store.go b/ocis/pkg/command/store.go index 639462117..5629abac3 100644 --- a/ocis/pkg/command/store.go +++ b/ocis/pkg/command/store.go @@ -1,11 +1,7 @@ -//go:build !simple -// +build !simple - 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/store/pkg/command" "github.com/urfave/cli/v2" @@ -13,7 +9,7 @@ import ( // StoreCommand is the entrypoint for the ocs command. func StoreCommand(cfg *config.Config) *cli.Command { - var globalLog shared.Log + //var globalLog shared.Log return &cli.Command{ Name: "store", @@ -27,16 +23,16 @@ func StoreCommand(cfg *config.Config) *cli.Command { return err } - globalLog = cfg.Log + //globalLog = cfg.Log return nil }, Action: func(c *cli.Context) error { // if accounts logging is empty in ocis.yaml - if (cfg.Store.Log == shared.Log{}) && (globalLog != shared.Log{}) { - // we can safely inherit the global logging values. - cfg.Store.Log = globalLog - } + //if (cfg.Store.Log == shared.Log{}) && (globalLog != shared.Log{}) { + // // we can safely inherit the global logging values. + // cfg.Store.Log = globalLog + //} origCmd := command.Server(cfg.Store) return handleOriginalAction(c, origCmd) diff --git a/ocis/pkg/command/thumbnails.go b/ocis/pkg/command/thumbnails.go index 0562b5d61..01cdae6fc 100644 --- a/ocis/pkg/command/thumbnails.go +++ b/ocis/pkg/command/thumbnails.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( @@ -24,9 +21,9 @@ func ThumbnailsCommand(cfg *config.Config) *cli.Command { return err } - if cfg.Commons != nil { - cfg.Thumbnails.Commons = cfg.Commons - } + //if cfg.Commons != nil { + // cfg.Thumbnails.Commons = cfg.Commons + //} return nil }, diff --git a/ocis/pkg/command/util.go b/ocis/pkg/command/util.go index a2e9b1abc..c49fb2c12 100644 --- a/ocis/pkg/command/util.go +++ b/ocis/pkg/command/util.go @@ -2,7 +2,6 @@ package command import ( "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/shared" "github.com/urfave/cli/v2" ) @@ -11,16 +10,16 @@ func ParseStorageCommon(ctx *cli.Context, cfg *config.Config) error { return err } - if cfg.Storage.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil { - cfg.Storage.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.Storage.Log == nil && cfg.Commons == nil { - cfg.Storage.Log = &shared.Log{} - } + //if cfg.Storage.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil { + // cfg.Storage.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.Storage.Log == nil && cfg.Commons == nil { + // cfg.Storage.Log = &shared.Log{} + //} return nil } diff --git a/ocis/pkg/command/web.go b/ocis/pkg/command/web.go index c3f2df2ea..5d63587c2 100644 --- a/ocis/pkg/command/web.go +++ b/ocis/pkg/command/web.go @@ -18,9 +18,9 @@ func WebCommand(cfg *config.Config) *cli.Command { return err } - if cfg.Commons != nil { - cfg.Web.Commons = cfg.Commons - } + //if cfg.Commons != nil { + // cfg.Web.Commons = cfg.Commons + //} return nil }, diff --git a/ocis/pkg/command/webdav.go b/ocis/pkg/command/webdav.go index 0502642bb..31cb2c528 100644 --- a/ocis/pkg/command/webdav.go +++ b/ocis/pkg/command/webdav.go @@ -1,6 +1,3 @@ -//go:build !simple -// +build !simple - package command import ( diff --git a/ocis/pkg/runtime/service/service.go b/ocis/pkg/runtime/service/service.go index c66b07c3f..7c586838e 100644 --- a/ocis/pkg/runtime/service/service.go +++ b/ocis/pkg/runtime/service/service.go @@ -164,9 +164,9 @@ func Start(o ...Option) error { } } - if s.cfg.Storage.Log == nil { - s.cfg.Storage.Log = &shared.Log{} - } + //if s.cfg.Storage.Log == nil { + // s.cfg.Storage.Log = &shared.Log{} + //} s.cfg.Storage.Log.Color = s.cfg.Commons.Color s.cfg.Storage.Log.Level = s.cfg.Commons.Level diff --git a/ocs/pkg/command/health.go b/ocs/pkg/command/health.go index 51474a3da..b1d2249e2 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/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/ocs/pkg/command/root.go b/ocs/pkg/command/root.go index 26c27151b..01d18eb58 100644 --- a/ocs/pkg/command/root.go +++ b/ocs/pkg/command/root.go @@ -6,7 +6,6 @@ import ( "github.com/imdario/mergo" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/ocs/pkg/config" "github.com/thejerf/suture/v4" @@ -31,7 +30,7 @@ func Execute(cfg *config.Config) error { Before: func(c *cli.Context) error { cfg.Service.Version = version.String - return nil + return ParseConfig(c, cfg) }, Commands: []*cli.Command{ @@ -54,17 +53,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("ocs"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - // ParseConfig loads idp configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) @@ -105,7 +93,7 @@ type SutureService struct { // NewSutureService creates a new ocs.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.OCS.Commons = cfg.Commons + //cfg.OCS.Commons = cfg.Commons return SutureService{ cfg: cfg.OCS, } diff --git a/ocs/pkg/config/config.go b/ocs/pkg/config/config.go index 6c25c3017..c1d7145ad 100644 --- a/ocs/pkg/config/config.go +++ b/ocs/pkg/config/config.go @@ -74,27 +74,27 @@ type IdentityManagement struct { type Config struct { *shared.Commons - Log Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - HTTP HTTP `ocisConfig:"http"` - Tracing Tracing `ocisConfig:"tracing"` - TokenManager TokenManager `ocisConfig:"token_manager"` - Service Service `ocisConfig:"service"` - Reva Reva `ocisConfig:"reva"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + HTTP HTTP `ocisConfig:"http"` + + TokenManager TokenManager `ocisConfig:"token_manager"` + Reva Reva `ocisConfig:"reva"` + IdentityManagement IdentityManagement `ocisConfig:"identity_management"` - AccountBackend string `ocisConfig:"account_backend" env:"OCS_ACCOUNT_BACKEND_TYPE"` - StorageUsersDriver string `ocisConfig:"storage_users_driver" env:"STORAGE_USERS_DRIVER;OCS_STORAGE_USERS_DRIVER"` - MachineAuthAPIKey string `ocisConfig:"machine_auth_api_key" env:"OCIS_MACHINE_AUTH_API_KEY;OCS_MACHINE_AUTH_API_KEY"` + + AccountBackend string `ocisConfig:"account_backend" env:"OCS_ACCOUNT_BACKEND_TYPE"` + StorageUsersDriver string `ocisConfig:"storage_users_driver" env:"STORAGE_USERS_DRIVER;OCS_STORAGE_USERS_DRIVER"` + MachineAuthAPIKey string `ocisConfig:"machine_auth_api_key" env:"OCIS_MACHINE_AUTH_API_KEY;OCS_MACHINE_AUTH_API_KEY"` Context context.Context Supervised bool } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} -} - // DefaultConfig provides default values for a config struct. func DefaultConfig() *Config { return &Config{ diff --git a/proxy/pkg/command/health.go b/proxy/pkg/command/health.go index adb3f0a77..3f904bda1 100644 --- a/proxy/pkg/command/health.go +++ b/proxy/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/proxy/pkg/config" + "github.com/owncloud/ocis/proxy/pkg/logging" "github.com/urfave/cli/v2" ) @@ -15,7 +16,7 @@ func Health(cfg *config.Config) *cli.Command { Usage: "Check health status", //Flags: flagset.HealthWithConfig(cfg), Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/proxy/pkg/command/root.go b/proxy/pkg/command/root.go index 13ea25e99..9801fe65a 100644 --- a/proxy/pkg/command/root.go +++ b/proxy/pkg/command/root.go @@ -4,13 +4,13 @@ import ( "context" "os" + "github.com/imdario/mergo" 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" "github.com/urfave/cli/v2" + "github.com/wkloucek/envdecode" ) // Execute is the entry point for the ocis-proxy command. @@ -26,10 +26,12 @@ func Execute(cfg *config.Config) error { Email: "support@owncloud.com", }, }, + Before: func(c *cli.Context) error { cfg.Service.Version = version.String - return nil + return ParseConfig(c, cfg) }, + Commands: []*cli.Command{ Server(cfg), Health(cfg), @@ -50,35 +52,37 @@ func Execute(cfg *config.Config) error { return app.Run(os.Args) } -// 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. -// If this extension is run as a subcommand (i.e: ocis proxy) then there are 2 levels of config parsing: -// 1. ocis.yaml (if any) -// 2. proxy.yaml (if any) -// 3. environment variables. +// ParseConfig loads accounts configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - conf, err := ociscfg.BindSourcesToStructs("proxy", cfg) + _, 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 = &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{} + //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. + envCfg := config.Config{} + if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" { + return err } - conf.LoadOSEnv(config.GetEnv(cfg), false) + // merge environment variable config on top of the current config + if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil { + return err + } - bindings := config.StructMappings(cfg) - return ociscfg.BindEnv(conf, bindings) + return nil } // SutureService allows for the proxy command to be embedded and supervised by a suture supervisor tree. @@ -88,7 +92,7 @@ type SutureService struct { // NewSutureService creates a new proxy.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.Proxy.Commons = cfg.Commons + //cfg.Proxy.Commons = cfg.Commons return SutureService{ cfg: cfg.Proxy, } @@ -102,14 +106,3 @@ 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 ed8e2d9c0..a99060d8b 100644 --- a/proxy/pkg/command/server.go +++ b/proxy/pkg/command/server.go @@ -20,6 +20,7 @@ import ( "github.com/owncloud/ocis/ocis-pkg/service/grpc" "github.com/owncloud/ocis/proxy/pkg/config" "github.com/owncloud/ocis/proxy/pkg/cs3" + "github.com/owncloud/ocis/proxy/pkg/logging" "github.com/owncloud/ocis/proxy/pkg/metrics" "github.com/owncloud/ocis/proxy/pkg/middleware" "github.com/owncloud/ocis/proxy/pkg/proxy" @@ -62,9 +63,9 @@ func Server(cfg *config.Config) *cli.Command { return nil }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) - - if err := tracing.Configure(cfg); err != nil { + logger := logging.Configure(cfg.Service.Name, cfg.Log) + err := tracing.Configure(cfg) + if err != nil { return err } diff --git a/proxy/pkg/config/config.go b/proxy/pkg/config/config.go index f9f93cc02..9afd1cc72 100644 --- a/proxy/pkg/config/config.go +++ b/proxy/pkg/config/config.go @@ -8,45 +8,45 @@ import ( "github.com/owncloud/ocis/ocis-pkg/shared" ) -// Log defines the available logging configuration. +// Log defines the available log configuration. type Log struct { - Level string `ocisConfig:"level"` - Pretty bool `ocisConfig:"pretty"` - Color bool `ocisConfig:"color"` - File string `ocisConfig:"file"` + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;PROXY_LOG_LEVEL"` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;PROXY_LOG_PRETTY"` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;PROXY_LOG_COLOR"` + File string `mapstructure:"file" env:"OCIS_LOG_FILE;PROXY_LOG_FILE"` } // Debug defines the available debug configuration. type Debug struct { - Addr string `ocisConfig:"addr"` - Token string `ocisConfig:"token"` - Pprof bool `ocisConfig:"pprof"` - Zpages bool `ocisConfig:"zpages"` + Addr string `ocisConfig:"addr" env:"PROXY_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"PROXY_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"PROXY_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"PROXY_DEBUG_ZPAGES"` } // HTTP defines the available http configuration. type HTTP struct { - Addr string `ocisConfig:"addr"` - Root string `ocisConfig:"root"` + Addr string `ocisConfig:"addr" env:"PROXY_HTTP_ADDR"` + Root string `ocisConfig:"root" env:"PROXY_HTTP_ROOT"` Namespace string - TLSCert string `ocisConfig:"tls_cert"` - TLSKey string `ocisConfig:"tls_key"` - TLS bool `ocisConfig:"tls"` + TLSCert string `ocisConfig:"tls_cert" env:"PROXY_TRANSPORT_TLS_CERT"` + TLSKey string `ocisConfig:"tls_key" env:"PROXY_TRANSPORT_TLS_KEY"` + TLS bool `ocisConfig:"tls" env:"PROXY_TLS"` } // Service defines the available service configuration. type Service struct { - Name string `ocisConfig:"name"` - Version string `ocisConfig:"version"` + Name string + Version string } // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` - Service string `ocisConfig:"service"` + Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;PROXY_TRACING_ENABLED"` + Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;PROXY_TRACING_TYPE"` + Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;PROXY_TRACING_ENDPOINT"` + Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;PROXY_TRACING_COLLECTOR"` + Service string `ocisConfig:"service" env:"PROXY_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name? } // Policy enables us to use multiple directors. @@ -84,7 +84,7 @@ var ( // Reva defines all available REVA configuration. type Reva struct { - Address string `ocisConfig:"address"` + Address string `ocisConfig:"address" env:"REVA_GATEWAY"` Middleware Middleware `ocisConfig:"middleware"` } @@ -98,34 +98,31 @@ type Auth struct { CredentialsByUserAgent map[string]string `ocisConfig:""` } -// Cache is a TTL cache configuration. -type Cache struct { - Size int `ocisConfig:"size"` - TTL int `ocisConfig:"ttl"` -} - // Config combines all available configuration parts. type Config struct { *shared.Commons - Log *shared.Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - HTTP HTTP `ocisConfig:"http"` - Service Service `ocisConfig:"service"` - Tracing Tracing `ocisConfig:"tracing"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + HTTP HTTP `ocisConfig:"http"` + 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"` - UserOIDCClaim string `ocisConfig:"user_oidc_claim"` - UserCS3Claim string `ocisConfig:"user_cs3_claim"` - MachineAuthAPIKey string `ocisConfig:"machine_auth_api_key"` - AutoprovisionAccounts bool `ocisConfig:"auto_provision_accounts"` - EnableBasicAuth bool `ocisConfig:"enable_basic_auth"` - InsecureBackends bool `ocisConfig:"insecure_backends"` + AccountBackend string `ocisConfig:"account_backend" env:"PROXY_ACCOUNT_BACKEND_TYPE"` + UserOIDCClaim string `ocisConfig:"user_oidc_claim" env:"PROXY_USER_OIDC_CLAIM"` + UserCS3Claim string `ocisConfig:"user_cs3_claim" env:"PROXY_USER_CS3_CLAIM"` + MachineAuthAPIKey string `ocisConfig:"machine_auth_api_key" env:"OCIS_MACHINE_AUTH_API_KEY;PROXY_MACHINE_AUTH_API_KEY"` + 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"` Context context.Context Supervised bool @@ -134,9 +131,15 @@ type Config struct { // OIDC is the config for the OpenID-Connect middleware. If set the proxy will try to authenticate every request // with the configured oidc-provider type OIDC struct { - Issuer string `ocisConfig:"issuer"` - Insecure bool `ocisConfig:"insecure"` - UserinfoCache Cache `ocisConfig:"user_info_cache"` + Issuer string `ocisConfig:"issuer" env:"OCIS_URL;PROXY_OIDC_ISSUER"` + Insecure bool `ocisConfig:"insecure" env:"OCIS_INSECURE;PROXY_OIDC_INSECURE"` + UserinfoCache UserinfoCache `ocisConfig:"user_info_cache"` +} + +// UserinfoCache is a TTL cache configuration. +type UserinfoCache struct { + Size int `ocisConfig:"size" env:"PROXY_OIDC_USERINFO_CACHE_SIZE"` + TTL int `ocisConfig:"ttl" env:"PROXY_OIDC_USERINFO_CACHE_TTL"` } // PolicySelector is the toplevel-configuration for different selectors @@ -154,13 +157,13 @@ type StaticSelectorConf struct { // TokenManager is the config for using the reva token manager type TokenManager struct { - JWTSecret string `ocisConfig:"jwt_secret"` + JWTSecret string `ocisConfig:"jwt_secret" env:"OCIS_JWT_SECRET;PROXY_JWT_SECRET"` } // PreSignedURL is the config for the presigned url middleware type PreSignedURL struct { AllowedHTTPMethods []string `ocisConfig:"allowed_http_methods"` - Enabled bool `ocisConfig:"enabled"` + Enabled bool `ocisConfig:"enabled" env:"PROXY_ENABLE_PRESIGNEDURLS"` } // MigrationSelectorConf is the config for the migration-selector @@ -192,13 +195,6 @@ type RegexRuleConf struct { Policy string `ocisConfig:"policy"` } -// New initializes a new configuration -func New() *Config { - return &Config{ - HTTP: HTTP{}, - } -} - // DefaultConfig provides with a working local configuration for a proxy service. func DefaultConfig() *Config { return &Config{ @@ -227,7 +223,7 @@ func DefaultConfig() *Config { Issuer: "https://localhost:9200", Insecure: true, //Insecure: true, - UserinfoCache: Cache{ + UserinfoCache: UserinfoCache{ Size: 1024, TTL: 10, }, @@ -243,14 +239,14 @@ func DefaultConfig() *Config { AllowedHTTPMethods: []string{"GET"}, Enabled: true, }, - AccountBackend: "accounts", - UserOIDCClaim: "email", - UserCS3Claim: "mail", - MachineAuthAPIKey: "change-me-please", - //AutoprovisionAccounts: false, - //EnableBasicAuth: false, - //InsecureBackends: false, - Context: nil, + AccountBackend: "accounts", + UserOIDCClaim: "email", + UserCS3Claim: "mail", + MachineAuthAPIKey: "change-me-please", + AutoprovisionAccounts: false, + EnableBasicAuth: false, + InsecureBackends: false, + // TODO: enable //Policies: defaultPolicies(), } } diff --git a/proxy/pkg/config/mappings.go b/proxy/pkg/config/mappings.go deleted file mode 100644 index 101d9a70d..000000000 --- a/proxy/pkg/config/mappings.go +++ /dev/null @@ -1,182 +0,0 @@ -package config - -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(cfg *Config) []string { - var r = make([]string, len(structMappings(cfg))) - for i := range structMappings(cfg) { - r = append(r, structMappings(cfg)[i].EnvVars...) - } - - return r -} - -// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the -// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets -// us propagate changes easier. -func StructMappings(cfg *Config) []shared.EnvBinding { - return structMappings(cfg) -} - -func structMappings(cfg *Config) []shared.EnvBinding { - return []shared.EnvBinding{ - // Logging - { - EnvVars: []string{"OCIS_LOG_LEVEL", "PROXY_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - { - EnvVars: []string{"OCIS_LOG_COLOR", "PROXY_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - { - EnvVars: []string{"OCIS_LOG_PRETTY", "PROXY_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - { - EnvVars: []string{"OCIS_LOG_FILE", "PROXY_LOG_FILE"}, - Destination: &cfg.Log.File, - }, - - // Basic auth - { - EnvVars: []string{"PROXY_ENABLE_BASIC_AUTH"}, - Destination: &cfg.EnableBasicAuth, - }, - - // Debug (health) - { - EnvVars: []string{"PROXY_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - - // Tracing - { - EnvVars: []string{"OCIS_TRACING_ENABLED", "PROXY_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - { - EnvVars: []string{"OCIS_TRACING_TYPE", "PROXY_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - { - EnvVars: []string{"OCIS_TRACING_ENDPOINT", "PROXY_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - { - EnvVars: []string{"OCIS_TRACING_COLLECTOR", "PROXY_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - { - EnvVars: []string{"PROXY_TRACING_SERVICE"}, - Destination: &cfg.Tracing.Service, - }, - - // Debug - { - EnvVars: []string{"PROXY_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - { - EnvVars: []string{"PROXY_DEBUG_TOKEN"}, - Destination: &cfg.Debug.Token, - }, - { - EnvVars: []string{"PROXY_DEBUG_PPROF"}, - Destination: &cfg.Debug.Pprof, - }, - { - EnvVars: []string{"PROXY_DEBUG_ZPAGES"}, - Destination: &cfg.Debug.Zpages, - }, - - // HTTP - { - EnvVars: []string{"PROXY_HTTP_ADDR"}, - Destination: &cfg.HTTP.Addr, - }, - { - EnvVars: []string{"PROXY_HTTP_ROOT"}, - Destination: &cfg.HTTP.Root, - }, - { - EnvVars: []string{"PROXY_HTTP_NAMESPACE"}, - Destination: &cfg.HTTP.Namespace, - }, - { - EnvVars: []string{"PROXY_TRANSPORT_TLS_CERT"}, - Destination: &cfg.HTTP.TLSCert, - }, - { - EnvVars: []string{"PROXY_TRANSPORT_TLS_KEY"}, - Destination: &cfg.HTTP.TLSKey, - }, - { - EnvVars: []string{"PROXY_TLS"}, - Destination: &cfg.HTTP.TLS, - }, - - // Other - { - EnvVars: []string{"OCIS_JWT_SECRET", "PROXY_JWT_SECRET"}, - Destination: &cfg.TokenManager.JWTSecret, - }, - - { - EnvVars: []string{"REVA_GATEWAY"}, - Destination: &cfg.Reva.Address, - }, - { - EnvVars: []string{"PROXY_INSECURE_BACKENDS"}, - Destination: &cfg.InsecureBackends, - }, - { - EnvVars: []string{"OCIS_URL", "PROXY_OIDC_ISSUER"}, - Destination: &cfg.OIDC.Issuer, - }, - { - EnvVars: []string{"OCIS_INSECURE", "PROXY_OIDC_INSECURE"}, - Destination: &cfg.OIDC.Insecure, - }, - { - EnvVars: []string{"PROXY_OIDC_USERINFO_CACHE_TTL"}, - Destination: &cfg.OIDC.UserinfoCache.TTL, - }, - { - EnvVars: []string{"PROXY_OIDC_USERINFO_CACHE_SIZE"}, - Destination: &cfg.OIDC.UserinfoCache.Size, - }, - { - EnvVars: []string{"PROXY_AUTOPROVISION_ACCOUNTS"}, - Destination: &cfg.AutoprovisionAccounts, - }, - { - EnvVars: []string{"PROXY_USER_OIDC_CLAIM"}, - Destination: &cfg.UserOIDCClaim, - }, - { - EnvVars: []string{"PROXY_USER_CS3_CLAIM"}, - Destination: &cfg.UserCS3Claim, - }, - { - EnvVars: []string{"PROXY_ENABLE_PRESIGNEDURLS"}, - Destination: &cfg.PreSignedURL.Enabled, - }, - { - EnvVars: []string{"PROXY_ACCOUNT_BACKEND_TYPE"}, - Destination: &cfg.AccountBackend, - }, - { - EnvVars: []string{"OCIS_MACHINE_AUTH_API_KEY", "PROXY_MACHINE_AUTH_API_KEY"}, - Destination: &cfg.MachineAuthAPIKey, - }, - // there are 2 missing bindings: - // EnvVars: []string{"PROXY_MIDDLEWARE_AUTH_CREDENTIALS_BY_USER_AGENT"}, - // EnvVars: []string{"PRESIGNEDURL_ALLOWED_METHODS"}, - // since they both have no destination - // see https://github.com/owncloud/ocis/blob/52e5effa4fa05a1626d46f7d4cb574dde3a54593/proxy/pkg/flagset/flagset.go#L256-L261 - // and https://github.com/owncloud/ocis/blob/52e5effa4fa05a1626d46f7d4cb574dde3a54593/proxy/pkg/flagset/flagset.go#L295-L300 - } -} diff --git a/proxy/pkg/cs3/client.go b/proxy/pkg/cs3/client.go index 68f52d2d7..dbaaa03ae 100644 --- a/proxy/pkg/cs3/client.go +++ b/proxy/pkg/cs3/client.go @@ -10,7 +10,7 @@ import ( func newConn(endpoint string) (*grpc.ClientConn, error) { conn, err := grpc.Dial( endpoint, - grpc.WithInsecure(), + grpc.WithInsecure(), //TODO: depreciated grpc.WithUnaryInterceptor( otelgrpc.UnaryClientInterceptor( otelgrpc.WithTracerProvider( @@ -28,6 +28,7 @@ func newConn(endpoint string) (*grpc.ClientConn, error) { // GetGatewayServiceClient returns a new cs3 gateway client func GetGatewayServiceClient(endpoint string) (gateway.GatewayAPIClient, error) { + // TODO: check connection pooling conn, err := newConn(endpoint) if err != nil { return nil, err diff --git a/proxy/pkg/logging/logging.go b/proxy/pkg/logging/logging.go new file mode 100644 index 000000000..b2626eb74 --- /dev/null +++ b/proxy/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/proxy/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/settings/pkg/command/health.go b/settings/pkg/command/health.go index 9b475f5e7..e561b84e0 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/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/settings/pkg/command/root.go b/settings/pkg/command/root.go index 74c09626e..24d5b3619 100644 --- a/settings/pkg/command/root.go +++ b/settings/pkg/command/root.go @@ -4,14 +4,13 @@ import ( "context" "os" - "github.com/owncloud/ocis/ocis-pkg/shared" - + "github.com/imdario/mergo" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/settings/pkg/config" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" + "github.com/wkloucek/envdecode" ) // Execute is the entry point for the ocis-settings command. @@ -31,7 +30,7 @@ func Execute(cfg *config.Config) error { Before: func(c *cli.Context) error { cfg.Service.Version = version.String - return nil + return ParseConfig(c, cfg) }, Commands: []*cli.Command{ @@ -54,39 +53,37 @@ 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("settings"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - // ParseConfig loads idp configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - conf, err := ociscfg.BindSourcesToStructs("settings", cfg) + _, 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 = &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{} + //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. + envCfg := config.Config{} + if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" { + return err } - conf.LoadOSEnv(config.GetEnv(cfg), false) - bindings := config.StructMappings(cfg) - return ociscfg.BindEnv(conf, bindings) + // merge environment variable config on top of the current config + if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil { + return err + } + + return nil } // SutureService allows for the settings command to be embedded and supervised by a suture supervisor tree. @@ -96,7 +93,7 @@ type SutureService struct { // NewSutureService creates a new settings.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.Settings.Commons = cfg.Commons + //cfg.Settings.Commons = cfg.Commons return SutureService{ cfg: cfg.Settings, } diff --git a/settings/pkg/command/server.go b/settings/pkg/command/server.go index bd430f263..a6e51c930 100644 --- a/settings/pkg/command/server.go +++ b/settings/pkg/command/server.go @@ -6,6 +6,7 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/settings/pkg/config" + "github.com/owncloud/ocis/settings/pkg/logging" "github.com/owncloud/ocis/settings/pkg/metrics" "github.com/owncloud/ocis/settings/pkg/server/debug" "github.com/owncloud/ocis/settings/pkg/server/grpc" @@ -31,8 +32,7 @@ func Server(cfg *config.Config) *cli.Command { return nil }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) - + logger := logging.Configure(cfg.Service.Name, cfg.Log) err := tracing.Configure(cfg) if err != nil { return err diff --git a/settings/pkg/config/config.go b/settings/pkg/config/config.go index d07229723..fadd6722c 100644 --- a/settings/pkg/config/config.go +++ b/settings/pkg/config/config.go @@ -11,10 +11,10 @@ import ( // Debug defines the available debug configuration. type Debug struct { - Addr string `ocisConfig:"addr"` - Token string `ocisConfig:"token"` - Pprof bool `ocisConfig:"pprof"` - Zpages bool `ocisConfig:"zpages"` + Addr string `ocisConfig:"addr" env:"SETTINGS_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"SETTINGS_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"SETTINGS_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"SETTINGS_DEBUG_ZPAGES"` } // CORS defines the available cors configuration. @@ -27,55 +27,66 @@ type CORS struct { // HTTP defines the available http configuration. type HTTP struct { - Addr string `ocisConfig:"addr"` + Addr string `ocisConfig:"addr" env:"SETTINGS_HTTP_ADDR"` Namespace string - Root string `ocisConfig:"root"` - CacheTTL int `ocisConfig:"cache_ttl"` + Root string `ocisConfig:"root" env:"SETTINGS_HTTP_ROOT"` + CacheTTL int `ocisConfig:"cache_ttl" env:"SETTINGS_CACHE_TTL"` CORS CORS `ocisConfig:"cors"` } // GRPC defines the available grpc configuration. type GRPC struct { - Addr string `ocisConfig:"addr"` + Addr string `ocisConfig:"addr" env:"SETTINGS_GRPC_ADDR"` Namespace string } -// Service provides configuration options for the service +// Service defines the available service configuration. type Service struct { - Name string `ocisConfig:"name"` - Version string `ocisConfig:"version"` - DataPath string `ocisConfig:"data_path"` + Name string + Version string } // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` - Service string `ocisConfig:"service"` + Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;SETTINGS_TRACING_ENABLED"` + Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;SETTINGS_TRACING_TYPE"` + Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;SETTINGS_TRACING_ENDPOINT"` + Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;SETTINGS_TRACING_COLLECTOR"` + Service string `ocisConfig:"service" env:"SETTINGS_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name? } -// Asset undocumented +// Log defines the available log configuration. +type Log struct { + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;SETTINGS_LOG_LEVEL"` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;SETTINGS_LOG_PRETTY"` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;SETTINGS_LOG_COLOR"` + File string `mapstructure:"file" env:"OCIS_LOG_FILE;SETTINGS_LOG_FILE"` +} + +// Asset defines the available asset configuration. type Asset struct { - Path string `ocisConfig:"asset"` + Path string `ocisConfig:"path" env:"SETTINGS_ASSET_PATH"` } // TokenManager is the config for using the reva token manager type TokenManager struct { - JWTSecret string `ocisConfig:"jwt_secret"` + JWTSecret string `ocisConfig:"jwt_secret" env:"OCIS_JWT_SECRET;SETTINGS_JWT_SECRET"` } // Config combines all available configuration parts. type Config struct { *shared.Commons - Service Service `ocisConfig:"service"` - Log *shared.Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - HTTP HTTP `ocisConfig:"http"` - GRPC GRPC `ocisConfig:"grpc"` - Tracing Tracing `ocisConfig:"tracing"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + HTTP HTTP `ocisConfig:"http"` + GRPC GRPC `ocisConfig:"grpc"` + + DataPath string `ocisConfig:"data_path" env:"SETTINGS_DATA_PATH"` Asset Asset `ocisConfig:"asset"` TokenManager TokenManager `ocisConfig:"token_manager"` @@ -83,17 +94,11 @@ type Config struct { Supervised bool } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} -} - // DefaultConfig provides sane bootstrapping defaults. func DefaultConfig() *Config { return &Config{ Service: Service{ - Name: "settings", - DataPath: path.Join(defaults.BaseDataPath(), "settings"), + Name: "settings", }, Debug: Debug{ Addr: "127.0.0.1:9194", @@ -124,6 +129,7 @@ func DefaultConfig() *Config { Collector: "", Service: "settings", }, + DataPath: path.Join(defaults.BaseDataPath(), "settings"), Asset: Asset{ Path: "", }, diff --git a/settings/pkg/config/mappings.go b/settings/pkg/config/mappings.go deleted file mode 100644 index d4910269c..000000000 --- a/settings/pkg/config/mappings.go +++ /dev/null @@ -1,115 +0,0 @@ -package config - -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(cfg *Config) []string { - var r = make([]string, len(structMappings(cfg))) - for i := range structMappings(cfg) { - r = append(r, structMappings(cfg)[i].EnvVars...) - } - - return r -} - -// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the -// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets -// us propagate changes easier. -func StructMappings(cfg *Config) []shared.EnvBinding { - return structMappings(cfg) -} - -// structMappings binds a set of environment variables to a destination on cfg. -func structMappings(cfg *Config) []shared.EnvBinding { - return []shared.EnvBinding{ - { - EnvVars: []string{"OCIS_LOG_LEVEL", "SETTINGS_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - { - EnvVars: []string{"OCIS_LOG_PRETTY", "SETTINGS_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - { - EnvVars: []string{"OCIS_LOG_COLOR", "SETTINGS_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - { - EnvVars: []string{"SETTINGS_CONFIG_FILE"}, - Destination: &cfg.File, - }, - { - EnvVars: []string{"OCIS_TRACING_ENABLED", "SETTINGS_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - { - EnvVars: []string{"OCIS_TRACING_TYPE", "SETTINGS_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - { - EnvVars: []string{"OCIS_TRACING_ENDPOINT", "SETTINGS_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - { - EnvVars: []string{"OCIS_TRACING_COLLECTOR", "SETTINGS_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - { - EnvVars: []string{"SETTINGS_TRACING_SERVICE"}, - Destination: &cfg.Tracing.Service, - }, - { - EnvVars: []string{"SETTINGS_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - { - EnvVars: []string{"SETTINGS_DEBUG_TOKEN"}, - Destination: &cfg.Debug.Token, - }, - { - EnvVars: []string{"SETTINGS_DEBUG_PPROF"}, - Destination: &cfg.Debug.Pprof, - }, - { - EnvVars: []string{"SETTINGS_DEBUG_ZPAGES"}, - Destination: &cfg.Debug.Zpages, - }, - { - EnvVars: []string{"SETTINGS_HTTP_ADDR"}, - Destination: &cfg.HTTP.Addr, - }, - { - EnvVars: []string{"SETTINGS_HTTP_NAMESPACE"}, - Destination: &cfg.HTTP.Namespace, - }, - { - EnvVars: []string{"SETTINGS_HTTP_ROOT"}, - Destination: &cfg.HTTP.Root, - }, - { - EnvVars: []string{"SETTINGS_CACHE_TTL"}, - Destination: &cfg.HTTP.CacheTTL, - }, - { - EnvVars: []string{"SETTINGS_GRPC_ADDR"}, - Destination: &cfg.GRPC.Addr, - }, - { - EnvVars: []string{"SETTINGS_ASSET_PATH"}, - Destination: &cfg.Asset.Path, - }, - { - EnvVars: []string{"SETTINGS_GRPC_NAMESPACE"}, - Destination: &cfg.GRPC.Namespace, - }, - { - EnvVars: []string{"SETTINGS_DATA_PATH"}, - Destination: &cfg.Service.DataPath, - }, - { - EnvVars: []string{"OCIS_JWT_SECRET", "SETTINGS_JWT_SECRET"}, - Destination: &cfg.TokenManager.JWTSecret, - }, - } -} diff --git a/settings/pkg/logging/logging.go b/settings/pkg/logging/logging.go new file mode 100644 index 000000000..147d07e7c --- /dev/null +++ b/settings/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/settings/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/settings/pkg/proto/v0/settings.pb.micro_test.go b/settings/pkg/proto/v0/settings.pb.micro_test.go index 786b6ffaf..ec15d60a7 100644 --- a/settings/pkg/proto/v0/settings.pb.micro_test.go +++ b/settings/pkg/proto/v0/settings.pb.micro_test.go @@ -176,8 +176,8 @@ func init() { grpc.Address("localhost:9992"), ) - cfg := config.New() - cfg.Service.DataPath = dataPath + cfg := config.DefaultConfig() + cfg.DataPath = dataPath handler = svc.NewService(cfg, ocislog.NewLogger(ocislog.Color(true), ocislog.Pretty(true))) err := proto.RegisterBundleServiceHandler(service.Server(), handler) if err != nil { diff --git a/settings/pkg/store/filesystem/store.go b/settings/pkg/store/filesystem/store.go index cf81638b9..ccd0fe011 100644 --- a/settings/pkg/store/filesystem/store.go +++ b/settings/pkg/store/filesystem/store.go @@ -32,16 +32,16 @@ func New(cfg *config.Config) settings.Manager { //), } - if _, err := os.Stat(cfg.Service.DataPath); err != nil { - s.Logger.Info().Msgf("creating container on %v", cfg.Service.DataPath) - err = os.MkdirAll(cfg.Service.DataPath, 0700) + if _, err := os.Stat(cfg.DataPath); err != nil { + s.Logger.Info().Msgf("creating container on %v", cfg.DataPath) + err = os.MkdirAll(cfg.DataPath, 0700) if err != nil { - s.Logger.Err(err).Msgf("providing container on %v", cfg.Service.DataPath) + s.Logger.Err(err).Msgf("providing container on %v", cfg.DataPath) } } - s.dataPath = cfg.Service.DataPath + s.dataPath = cfg.DataPath return &s } diff --git a/storage/pkg/command/appprovider.go b/storage/pkg/command/appprovider.go index 80c8c523b..3ac4f71d4 100644 --- a/storage/pkg/command/appprovider.go +++ b/storage/pkg/command/appprovider.go @@ -12,6 +12,7 @@ import ( ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/thejerf/suture/v4" @@ -27,7 +28,7 @@ func AppProvider(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-app-provider") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) gr := run.Group{} ctx, cancel := context.WithCancel(context.Background()) diff --git a/storage/pkg/command/authbasic.go b/storage/pkg/command/authbasic.go index 10268754a..40bd71ed1 100644 --- a/storage/pkg/command/authbasic.go +++ b/storage/pkg/command/authbasic.go @@ -13,6 +13,7 @@ import ( ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/thejerf/suture/v4" @@ -28,7 +29,7 @@ func AuthBasic(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-auth-basic") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) gr := run.Group{} ctx, cancel := context.WithCancel(context.Background()) diff --git a/storage/pkg/command/authbearer.go b/storage/pkg/command/authbearer.go index af60ccbda..4424a6ef2 100644 --- a/storage/pkg/command/authbearer.go +++ b/storage/pkg/command/authbearer.go @@ -12,6 +12,7 @@ import ( ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/thejerf/suture/v4" @@ -27,7 +28,7 @@ func AuthBearer(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-auth-bearer") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) gr := run.Group{} ctx, cancel := context.WithCancel(context.Background()) diff --git a/storage/pkg/command/authmachine.go b/storage/pkg/command/authmachine.go index eaaa97cbf..80c70f832 100644 --- a/storage/pkg/command/authmachine.go +++ b/storage/pkg/command/authmachine.go @@ -12,6 +12,7 @@ import ( ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/thejerf/suture/v4" @@ -27,7 +28,7 @@ func AuthMachine(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-auth-machine") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) gr := run.Group{} ctx, cancel := context.WithCancel(context.Background()) diff --git a/storage/pkg/command/frontend.go b/storage/pkg/command/frontend.go index c3e92788d..8847a50f9 100644 --- a/storage/pkg/command/frontend.go +++ b/storage/pkg/command/frontend.go @@ -16,6 +16,7 @@ import ( "github.com/owncloud/ocis/ocis-pkg/conversions" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/thejerf/suture/v4" @@ -34,7 +35,7 @@ func Frontend(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-frontend") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) diff --git a/storage/pkg/command/gateway.go b/storage/pkg/command/gateway.go index 0dff38512..431ef5027 100644 --- a/storage/pkg/command/gateway.go +++ b/storage/pkg/command/gateway.go @@ -15,10 +15,10 @@ import ( "github.com/oklog/run" 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/sync" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/service/external" "github.com/owncloud/ocis/storage/pkg/tracing" @@ -43,7 +43,7 @@ func Gateway(cfg *config.Config) *cli.Command { return nil }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) gr := run.Group{} ctx, cancel := context.WithCancel(context.Background()) @@ -425,16 +425,16 @@ func ParseConfig(c *cli.Context, cfg *config.Config, storageExtension string) er } // 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{} - } + //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(cfg), false) diff --git a/storage/pkg/command/groups.go b/storage/pkg/command/groups.go index 043c96fdc..625a23177 100644 --- a/storage/pkg/command/groups.go +++ b/storage/pkg/command/groups.go @@ -13,6 +13,7 @@ import ( ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/thejerf/suture/v4" @@ -28,7 +29,7 @@ func Groups(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-groups") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) gr := run.Group{} ctx, cancel := context.WithCancel(context.Background()) diff --git a/storage/pkg/command/health.go b/storage/pkg/command/health.go index a3c3791a9..244ce713d 100644 --- a/storage/pkg/command/health.go +++ b/storage/pkg/command/health.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/storage/pkg/command/root.go b/storage/pkg/command/root.go index 80e16d071..6170aa7cf 100644 --- a/storage/pkg/command/root.go +++ b/storage/pkg/command/root.go @@ -3,7 +3,6 @@ package command import ( "os" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/storage/pkg/config" "github.com/urfave/cli/v2" @@ -23,8 +22,10 @@ func Execute(cfg *config.Config) error { Email: "support@owncloud.com", }, }, + Before: func(c *cli.Context) error { - return ParseConfig(c, cfg, "storage") + cfg.Service.Version = version.String + return ParseConfig(c, cfg, "_") }, Commands: []*cli.Command{ @@ -57,14 +58,3 @@ 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("storage"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} diff --git a/storage/pkg/command/sharing.go b/storage/pkg/command/sharing.go index abace4d50..1d8c032c8 100644 --- a/storage/pkg/command/sharing.go +++ b/storage/pkg/command/sharing.go @@ -7,6 +7,7 @@ import ( "path" "path/filepath" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/owncloud/ocis/ocis-pkg/sync" @@ -30,7 +31,7 @@ func Sharing(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-sharing") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) diff --git a/storage/pkg/command/storagemetadata.go b/storage/pkg/command/storagemetadata.go index f77d1eca3..34cf47c65 100644 --- a/storage/pkg/command/storagemetadata.go +++ b/storage/pkg/command/storagemetadata.go @@ -7,6 +7,7 @@ import ( "path" "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/cs3org/reva/cmd/revad/runtime" "github.com/gofrs/uuid" @@ -34,7 +35,7 @@ func StorageMetadata(cfg *config.Config) *cli.Command { }, Category: "Extensions", Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) gr := run.Group{} diff --git a/storage/pkg/command/storagepubliclink.go b/storage/pkg/command/storagepubliclink.go index 0e3966b7d..92f4d3794 100644 --- a/storage/pkg/command/storagepubliclink.go +++ b/storage/pkg/command/storagepubliclink.go @@ -12,6 +12,7 @@ import ( ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/thejerf/suture/v4" @@ -28,7 +29,7 @@ func StoragePublicLink(cfg *config.Config) *cli.Command { }, Category: "Extensions", Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) gr := run.Group{} ctx, cancel := context.WithCancel(context.Background()) diff --git a/storage/pkg/command/storageshares.go b/storage/pkg/command/storageshares.go index a239170a2..35e44e9d6 100644 --- a/storage/pkg/command/storageshares.go +++ b/storage/pkg/command/storageshares.go @@ -7,6 +7,7 @@ import ( "path" "github.com/owncloud/ocis/ocis-pkg/sync" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/cs3org/reva/cmd/revad/runtime" "github.com/gofrs/uuid" @@ -28,7 +29,7 @@ func StorageShares(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-shares") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) diff --git a/storage/pkg/command/storageusers.go b/storage/pkg/command/storageusers.go index 9d671949a..15cf235d4 100644 --- a/storage/pkg/command/storageusers.go +++ b/storage/pkg/command/storageusers.go @@ -13,6 +13,7 @@ import ( "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/storage/pkg/command/storagedrivers" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/thejerf/suture/v4" @@ -28,7 +29,7 @@ func StorageUsers(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-userprovider") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) diff --git a/storage/pkg/command/users.go b/storage/pkg/command/users.go index 34aee1d7c..b7205a542 100644 --- a/storage/pkg/command/users.go +++ b/storage/pkg/command/users.go @@ -13,6 +13,7 @@ import ( ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/storage/pkg/config" + "github.com/owncloud/ocis/storage/pkg/logging" "github.com/owncloud/ocis/storage/pkg/server/debug" "github.com/owncloud/ocis/storage/pkg/tracing" "github.com/thejerf/suture/v4" @@ -28,7 +29,7 @@ func Users(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg, "storage-users") }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) tracing.Configure(cfg, logger) diff --git a/storage/pkg/config/config.go b/storage/pkg/config/config.go index 09adc2e98..c46c0a61d 100644 --- a/storage/pkg/config/config.go +++ b/storage/pkg/config/config.go @@ -10,6 +10,12 @@ import ( "github.com/owncloud/ocis/ocis-pkg/shared" ) +// Service defines the available service configuration. +type Service struct { + Name string + Version string +} + // Log defines the available logging configuration. type Log struct { Level string `ocisConfig:"level"` @@ -507,16 +513,15 @@ type Asset struct { type Config struct { *shared.Commons - Log *shared.Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - Reva Reva `ocisConfig:"reva"` - Tracing Tracing `ocisConfig:"tracing"` - Asset Asset `ocisConfig:"asset"` -} + Service Service `ocisConfig:"service"` -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + Reva Reva `ocisConfig:"reva"` + + Asset Asset `ocisConfig:"asset"` } func DefaultConfig() *Config { diff --git a/storage/pkg/logging/logging.go b/storage/pkg/logging/logging.go new file mode 100644 index 000000000..c9d443321 --- /dev/null +++ b/storage/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/storage/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/store/pkg/command/health.go b/store/pkg/command/health.go index 38461c9ac..b280d8b0e 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/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/store/pkg/command/root.go b/store/pkg/command/root.go index 49584e63c..5c63f8a3e 100644 --- a/store/pkg/command/root.go +++ b/store/pkg/command/root.go @@ -4,12 +4,13 @@ import ( "context" "os" + "github.com/imdario/mergo" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/store/pkg/config" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" + "github.com/wkloucek/envdecode" ) // Execute is the entry point for the ocis-store command. @@ -52,29 +53,37 @@ 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("store"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - -// ParseConfig loads idp configuration from known paths. +// ParseConfig loads accounts configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - conf, err := ociscfg.BindSourcesToStructs("store", cfg) + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) if err != nil { return err } - // load all env variables relevant to the config in the current context. - 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{} + //} - bindings := config.StructMappings(cfg) - return ociscfg.BindEnv(conf, bindings) + // load all env variables relevant to the config in the current context. + envCfg := config.Config{} + if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" { + return err + } + + // merge environment variable config on top of the current config + if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil { + return err + } + + return nil } // SutureService allows for the store command to be embedded and supervised by a suture supervisor tree. diff --git a/store/pkg/command/server.go b/store/pkg/command/server.go index 4bc9b35b9..add4beaef 100644 --- a/store/pkg/command/server.go +++ b/store/pkg/command/server.go @@ -3,10 +3,7 @@ package command import ( "context" - 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/store/pkg/logging" "github.com/owncloud/ocis/store/pkg/tracing" "github.com/oklog/run" @@ -23,31 +20,16 @@ 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 - } - } - return nil }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) - - if err := tracing.Configure(cfg); err != nil { + logger := logging.Configure(cfg.Service.Name, cfg.Log) + err := tracing.Configure(cfg) + if err != nil { return err } diff --git a/store/pkg/config/config.go b/store/pkg/config/config.go index a804e7faf..c96271fbd 100644 --- a/store/pkg/config/config.go +++ b/store/pkg/config/config.go @@ -5,60 +5,63 @@ import ( "path" "github.com/owncloud/ocis/ocis-pkg/config/defaults" - "github.com/owncloud/ocis/ocis-pkg/shared" ) // Debug defines the available debug configuration. type Debug struct { - Addr string `ocisConfig:"addr"` - Token string `ocisConfig:"token"` - Pprof bool `ocisConfig:"pprof"` - Zpages bool `ocisConfig:"zpages"` + Addr string `ocisConfig:"addr" env:"STORE_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"STORE_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"STORE_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"STORE_DEBUG_ZPAGES"` } // GRPC defines the available grpc configuration. type GRPC struct { - Addr string `ocisConfig:"addr"` - Root string `ocisConfig:"root"` + Addr string `ocisConfig:"addr" env:"STORE_GRPC_ADDR"` Namespace string } // Service defines the available service configuration. type Service struct { - Name string `ocisConfig:"name"` - Version string `ocisConfig:"version"` + Name string + Version string } // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` - Service string `ocisConfig:"service"` + Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;STORE_TRACING_ENABLED"` + Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;STORE_TRACING_TYPE"` + Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;STORE_TRACING_ENDPOINT"` + Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;STORE_TRACING_COLLECTOR"` + Service string `ocisConfig:"service" env:"STORE_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name? +} + +// Log defines the available log configuration. +type Log struct { + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;STORE_LOG_LEVEL"` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;STORE_LOG_PRETTY"` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;STORE_LOG_COLOR"` + File string `mapstructure:"file" env:"OCIS_LOG_FILE;STORE_LOG_FILE"` } // Config combines all available configuration parts. type Config struct { - Log shared.Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - GRPC GRPC `ocisConfig:"grpc"` - Tracing Tracing `ocisConfig:"tracing"` - Datapath string `ocisConfig:"data_path"` - Service Service `ocisConfig:"service"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + GRPC GRPC `ocisConfig:"grpc"` + + Datapath string `ocisConfig:"data_path" env:"STORE_DATA_PATH"` Context context.Context Supervised bool } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} -} - func DefaultConfig() *Config { return &Config{ - Log: shared.Log{}, Debug: Debug{ Addr: "127.0.0.1:9464", Token: "", @@ -82,14 +85,3 @@ func DefaultConfig() *Config { Datapath: path.Join(defaults.BaseDataPath(), "store"), } } - -// 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...) - } - - return r -} diff --git a/store/pkg/config/mappings.go b/store/pkg/config/mappings.go deleted file mode 100644 index 9d883ca23..000000000 --- a/store/pkg/config/mappings.go +++ /dev/null @@ -1,80 +0,0 @@ -package config - -import "github.com/owncloud/ocis/ocis-pkg/shared" - -// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the -// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets -// us propagate changes easier. -func StructMappings(cfg *Config) []shared.EnvBinding { - return structMappings(cfg) -} - -// structMappings binds a set of environment variables to a destination on cfg. -func structMappings(cfg *Config) []shared.EnvBinding { - return []shared.EnvBinding{ - { - EnvVars: []string{"OCIS_LOG_LEVEL", "STORE_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - { - EnvVars: []string{"OCIS_LOG_PRETTY", "STORE_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - { - EnvVars: []string{"OCIS_LOG_COLOR", "STORE_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - { - EnvVars: []string{"OCIS_LOG_FILE", "STORE_LOG_FILE"}, - Destination: &cfg.Log.File, - }, - { - EnvVars: []string{"OCIS_TRACING_ENABLED", "STORE_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - { - EnvVars: []string{"OCIS_TRACING_TYPE", "STORE_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - { - EnvVars: []string{"OCIS_TRACING_ENDPOINT", "STORE_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - { - EnvVars: []string{"OCIS_TRACING_COLLECTOR", "STORE_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - { - EnvVars: []string{"STORE_TRACING_SERVICE"}, - Destination: &cfg.Tracing.Service, - }, - { - EnvVars: []string{"STORE_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - { - EnvVars: []string{"STORE_DEBUG_TOKEN"}, - Destination: &cfg.Debug.Token, - }, - { - EnvVars: []string{"STORE_DEBUG_PPROF"}, - Destination: &cfg.Debug.Pprof, - }, - { - EnvVars: []string{"STORE_DEBUG_ZPAGES"}, - Destination: &cfg.Debug.Zpages, - }, - { - EnvVars: []string{"STORE_GRPC_NAMESPACE"}, - Destination: &cfg.GRPC.Namespace, - }, - { - EnvVars: []string{"STORE_GRPC_ADDR"}, - Destination: &cfg.GRPC.Addr, - }, - { - EnvVars: []string{"STORE_DATA_PATH"}, - Destination: &cfg.Datapath, - }, - } -} diff --git a/store/pkg/logging/logging.go b/store/pkg/logging/logging.go new file mode 100644 index 000000000..e6183eb18 --- /dev/null +++ b/store/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/store/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/thumbnails/pkg/command/health.go b/thumbnails/pkg/command/health.go index 6b5c84939..1ba961d62 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/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/thumbnails/pkg/command/root.go b/thumbnails/pkg/command/root.go index 30dc5c53b..57428daac 100644 --- a/thumbnails/pkg/command/root.go +++ b/thumbnails/pkg/command/root.go @@ -4,14 +4,13 @@ import ( "context" "os" - "github.com/owncloud/ocis/ocis-pkg/shared" - + "github.com/imdario/mergo" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/thumbnails/pkg/config" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" + "github.com/wkloucek/envdecode" ) // Execute is the entry point for the ocis-thumbnails command. @@ -31,7 +30,7 @@ func Execute(cfg *config.Config) error { Before: func(c *cli.Context) error { cfg.Service.Version = version.String - return nil + return ParseConfig(c, cfg) }, Commands: []*cli.Command{ @@ -54,40 +53,37 @@ 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("thumbnails"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - -// ParseConfig loads configuration from Viper known paths. -// ParseConfig loads glauth configuration from known paths. +// ParseConfig loads accounts configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - conf, err := ociscfg.BindSourcesToStructs("thumbnails", cfg) + _, 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 = &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{} + //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. + envCfg := config.Config{} + if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" { + return err } - conf.LoadOSEnv(config.GetEnv(cfg), false) - bindings := config.StructMappings(cfg) - return ociscfg.BindEnv(conf, bindings) + // merge environment variable config on top of the current config + if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil { + return err + } + + return nil } // SutureService allows for the thumbnails command to be embedded and supervised by a suture supervisor tree. @@ -97,7 +93,7 @@ type SutureService struct { // NewSutureService creates a new thumbnails.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.Thumbnails.Commons = cfg.Commons + //cfg.Thumbnails.Commons = cfg.Commons return SutureService{ cfg: cfg.Thumbnails, } diff --git a/thumbnails/pkg/command/server.go b/thumbnails/pkg/command/server.go index f76645ba8..42531f4d8 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/logging" "github.com/owncloud/ocis/thumbnails/pkg/metrics" "github.com/owncloud/ocis/thumbnails/pkg/server/debug" "github.com/owncloud/ocis/thumbnails/pkg/server/grpc" @@ -25,8 +26,9 @@ func Server(cfg *config.Config) *cli.Command { return nil }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) - if err := tracing.Configure(cfg); err != nil { + logger := logging.Configure(cfg.Service.Name, cfg.Log) + err := tracing.Configure(cfg) + if err != nil { return err } diff --git a/thumbnails/pkg/config/config.go b/thumbnails/pkg/config/config.go index 466c0085c..f9f1f3d7e 100644 --- a/thumbnails/pkg/config/config.go +++ b/thumbnails/pkg/config/config.go @@ -5,49 +5,56 @@ import ( "path" "github.com/owncloud/ocis/ocis-pkg/config/defaults" - "github.com/owncloud/ocis/ocis-pkg/shared" ) // Debug defines the available debug configuration. type Debug struct { - Addr string `ocisConfig:"addr"` - Token string `ocisConfig:"token"` - Pprof bool `ocisConfig:"pprof"` - Zpages bool `ocisConfig:"zpages"` + Addr string `ocisConfig:"addr" env:"THUMBNAILS_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"THUMBNAILS_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"THUMBNAILS_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"THUMBNAILS_DEBUG_ZPAGES"` } // GRPC defines the available grpc configuration. type GRPC struct { - Addr string `ocisConfig:"addr"` + Addr string `ocisConfig:"addr" env:"THUMBNAILS_GRPC_ADDR"` Namespace string } -// Service provides configuration options for the service +// Service defines the available service configuration. type Service struct { - Name string `ocisConfig:"name"` - Version string `ocisConfig:"version"` + Name string + Version string } // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` - Service string `ocisConfig:"service"` + Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;THUMBNAILS_TRACING_ENABLED"` + Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;THUMBNAILS_TRACING_TYPE"` + Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;THUMBNAILS_TRACING_ENDPOINT"` + Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;THUMBNAILS_TRACING_COLLECTOR"` + Service string `ocisConfig:"service" env:"THUMBNAILS_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name? +} + +// Log defines the available log configuration. +type Log struct { + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;THUMBNAILS_LOG_LEVEL"` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;THUMBNAILS_LOG_PRETTY"` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;THUMBNAILS_LOG_COLOR"` + File string `mapstructure:"file" env:"OCIS_LOG_FILE;THUMBNAILS_LOG_FILE"` } // Config combines all available configuration parts. type Config struct { - *shared.Commons + Service Service `ocisConfig:"service"` - File string `ocisConfig:"file"` - Log *shared.Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - GRPC GRPC `ocisConfig:"grpc"` - Service Service `ocisConfig:"service"` - Tracing Tracing `ocisConfig:"tracing"` - Thumbnail Thumbnail `ocisConfig:"thumbnail"` + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + GRPC GRPC `ocisConfig:"grpc"` + + Thumbnail Thumbnail `ocisConfig:"thumbnail"` Context context.Context Supervised bool @@ -55,7 +62,7 @@ type Config struct { // FileSystemStorage defines the available filesystem storage configuration. type FileSystemStorage struct { - RootDirectory string `ocisConfig:"root_directory"` + RootDirectory string `ocisConfig:"root_directory" env:"THUMBNAILS_FILESYSTEMSTORAGE_ROOT"` } // FileSystemSource defines the available filesystem source configuration. @@ -65,17 +72,12 @@ type FileSystemSource struct { // Thumbnail defines the available thumbnail related configuration. type Thumbnail struct { - Resolutions []string `ocisConfig:"resolutions"` + Resolutions []string `ocisConfig:"resolutions"` // TODO: how to configure FileSystemStorage FileSystemStorage `ocisConfig:"filesystem_storage"` - WebdavAllowInsecure bool `ocisConfig:"webdav_allow_insecure"` - CS3AllowInsecure bool `ocisConfig:"cs3_allow_insecure"` - RevaGateway string `ocisConfig:"reva_gateway"` - FontMapFile string `ocisConfig:"font_map_file"` -} - -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} + WebdavAllowInsecure bool `ocisConfig:"webdav_allow_insecure" env:"OCIS_INSECURE;THUMBNAILS_WEBDAVSOURCE_INSECURE"` + CS3AllowInsecure bool `ocisConfig:"cs3_allow_insecure" env:"OCIS_INSECURE;THUMBNAILS_CS3SOURCE_INSECURE"` + RevaGateway string `ocisConfig:"reva_gateway" env:"REVA_GATEWAY"` + FontMapFile string `ocisConfig:"font_map_file" env:"THUMBNAILS_TXT_FONTMAP_FILE"` } func DefaultConfig() *Config { diff --git a/thumbnails/pkg/config/mappings.go b/thumbnails/pkg/config/mappings.go deleted file mode 100644 index fdd30b501..000000000 --- a/thumbnails/pkg/config/mappings.go +++ /dev/null @@ -1,111 +0,0 @@ -package config - -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(cfg *Config) []string { - var r = make([]string, len(structMappings(cfg))) - for i := range structMappings(cfg) { - r = append(r, structMappings(cfg)[i].EnvVars...) - } - - return r -} - -// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the -// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets -// us propagate changes easier. -func StructMappings(cfg *Config) []shared.EnvBinding { - return structMappings(cfg) -} - -// structMappings binds a set of environment variables to a destination on cfg. -func structMappings(cfg *Config) []shared.EnvBinding { - return []shared.EnvBinding{ - { - EnvVars: []string{"OCIS_LOG_FILE", "THUMBNAILS_LOG_FILE"}, - Destination: &cfg.Log.File, - }, - { - EnvVars: []string{"OCIS_LOG_LEVEL", "THUMBNAILS_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - { - EnvVars: []string{"OCIS_LOG_PRETTY", "THUMBNAILS_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - { - EnvVars: []string{"OCIS_LOG_COLOR", "THUMBNAILS_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - { - EnvVars: []string{"THUMBNAILS_CONFIG_FILE"}, - Destination: &cfg.File, - }, - { - EnvVars: []string{"OCIS_TRACING_ENABLED", "THUMBNAILS_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - { - EnvVars: []string{"OCIS_TRACING_TYPE", "THUMBNAILS_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - { - EnvVars: []string{"OCIS_TRACING_ENDPOINT", "THUMBNAILS_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - { - EnvVars: []string{"OCIS_TRACING_COLLECTOR", "THUMBNAILS_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - { - EnvVars: []string{"THUMBNAILS_TRACING_SERVICE"}, - Destination: &cfg.Tracing.Service, - }, - { - EnvVars: []string{"THUMBNAILS_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - { - EnvVars: []string{"THUMBNAILS_DEBUG_TOKEN"}, - Destination: &cfg.Debug.Token, - }, - { - EnvVars: []string{"THUMBNAILS_DEBUG_PPROF"}, - Destination: &cfg.Debug.Pprof, - }, - { - EnvVars: []string{"THUMBNAILS_DEBUG_ZPAGES"}, - Destination: &cfg.Debug.Zpages, - }, - { - EnvVars: []string{"THUMBNAILS_GRPC_ADDR"}, - Destination: &cfg.GRPC.Addr, - }, - { - EnvVars: []string{"THUMBNAILS_GRPC_NAMESPACE"}, - Destination: &cfg.GRPC.Namespace, - }, - { - EnvVars: []string{"THUMBNAILS_TXT_FONTMAP_FILE"}, - Destination: &cfg.Thumbnail.FontMapFile, - }, - { - EnvVars: []string{"THUMBNAILS_FILESYSTEMSTORAGE_ROOT"}, - Destination: &cfg.Thumbnail.FileSystemStorage.RootDirectory, - }, - { - EnvVars: []string{"REVA_GATEWAY"}, - Destination: &cfg.Thumbnail.RevaGateway, - }, - { - EnvVars: []string{"OCIS_INSECURE", "THUMBNAILS_WEBDAVSOURCE_INSECURE"}, - Destination: &cfg.Thumbnail.WebdavAllowInsecure, - }, - { - EnvVars: []string{"OCIS_INSECURE", "THUMBNAILS_CS3SOURCE_INSECURE"}, - Destination: &cfg.Thumbnail.CS3AllowInsecure, - }, - } -} diff --git a/thumbnails/pkg/logging/logging.go b/thumbnails/pkg/logging/logging.go new file mode 100644 index 000000000..e097814b2 --- /dev/null +++ b/thumbnails/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/thumbnails/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/thumbnails/pkg/proto/v0/thumbnails.pb.micro_test.go b/thumbnails/pkg/proto/v0/thumbnails.pb.micro_test.go index d3298d62c..a435cdf36 100644 --- a/thumbnails/pkg/proto/v0/thumbnails.pb.micro_test.go +++ b/thumbnails/pkg/proto/v0/thumbnails.pb.micro_test.go @@ -26,7 +26,7 @@ func init() { grpc.Address("localhost:9992"), ) - cfg := config.New() + cfg := config.DefaultConfig() cfg.Thumbnail.Resolutions = []string{"16x16", "32x32", "64x64", "128x128"} wd, _ := os.Getwd() @@ -44,17 +44,17 @@ func init() { if err != nil { log.Fatalf("could not register ThumbnailHandler: %v", err) } - if err := service.Server().Start(); err != nil { - log.Fatalf("could not start server: %v", err) - } + if err := service.Server().Start(); err != nil { + log.Fatalf("could not start server: %v", err) + } } func TestGetThumbnailInvalidImage(t *testing.T) { req := proto.GetThumbnailRequest{ - Filepath: "invalid.png", + Filepath: "invalid.png", ThumbnailType: proto.GetThumbnailRequest_PNG, - Height: 32, - Width: 32, + Height: 32, + Width: 32, } client := service.Client() cl := proto.NewThumbnailService("com.owncloud.api.thumbnails", client) diff --git a/thumbnails/pkg/service/v0/service.go b/thumbnails/pkg/service/v0/service.go index 5d9fac804..82ca91390 100644 --- a/thumbnails/pkg/service/v0/service.go +++ b/thumbnails/pkg/service/v0/service.go @@ -31,8 +31,7 @@ func NewService(opts ...Option) v0proto.ThumbnailServiceHandler { logger.Fatal().Err(err).Msg("resolutions not configured correctly") } svc := Thumbnail{ - serviceID: options.Config.GRPC.Namespace + "." + options.Config.Service.Name, - webdavNamespace: options.Config.Thumbnail.WebdavNamespace, + serviceID: options.Config.GRPC.Namespace + "." + options.Config.Service.Name, manager: thumbnail.NewSimpleManager( resolutions, options.ThumbnailStorage, diff --git a/web/pkg/command/health.go b/web/pkg/command/health.go index 97206879b..d780d4058 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/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/web/pkg/command/root.go b/web/pkg/command/root.go index 8c99ecad6..0a7e731c6 100644 --- a/web/pkg/command/root.go +++ b/web/pkg/command/root.go @@ -4,14 +4,13 @@ import ( "context" "os" - "github.com/owncloud/ocis/ocis-pkg/shared" - + "github.com/imdario/mergo" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/web/pkg/config" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" + "github.com/wkloucek/envdecode" ) // Execute is the entry point for the web command. @@ -27,6 +26,12 @@ func Execute(cfg *config.Config) error { Email: "support@owncloud.com", }, }, + + Before: func(c *cli.Context) error { + cfg.Service.Version = version.String + return ParseConfig(c, cfg) + }, + Commands: []*cli.Command{ Server(cfg), Health(cfg), @@ -46,39 +51,37 @@ 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("web"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - -// ParseConfig loads graph configuration from known paths. +// ParseConfig loads accounts configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - conf, err := ociscfg.BindSourcesToStructs("web", cfg) + _, 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 = &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{} + //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. + envCfg := config.Config{} + if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" { + return err } - conf.LoadOSEnv(config.GetEnv(cfg), false) - bindings := config.StructMappings(cfg) - return ociscfg.BindEnv(conf, bindings) + // merge environment variable config on top of the current config + if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil { + return err + } + + return nil } // SutureService allows for the web command to be embedded and supervised by a suture supervisor tree. @@ -88,7 +91,7 @@ type SutureService struct { // NewSutureService creates a new web.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.Web.Commons = cfg.Commons + //cfg.Web.Commons = cfg.Commons return SutureService{ cfg: cfg.Web, } diff --git a/web/pkg/command/server.go b/web/pkg/command/server.go index c8b8fd107..6b3b1f9ad 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/logging" "github.com/owncloud/ocis/web/pkg/metrics" "github.com/owncloud/ocis/web/pkg/server/debug" "github.com/owncloud/ocis/web/pkg/server/http" @@ -37,9 +38,9 @@ func Server(cfg *config.Config) *cli.Command { return nil }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) - - if err := tracing.Configure(cfg); err != nil { + logger := logging.Configure(cfg.Service.Name, cfg.Log) + err := tracing.Configure(cfg) + if err != nil { return err } diff --git a/web/pkg/config/config.go b/web/pkg/config/config.go index 0cfd317fc..345ae8aec 100644 --- a/web/pkg/config/config.go +++ b/web/pkg/config/config.go @@ -2,51 +2,57 @@ package config import ( "context" - - "github.com/owncloud/ocis/ocis-pkg/shared" ) // Debug defines the available debug configuration. type Debug struct { - Addr string `ocisConfig:"addr"` - Token string `ocisConfig:"token"` - Pprof bool `ocisConfig:"pprof"` - Zpages bool `ocisConfig:"zpages"` + Addr string `ocisConfig:"addr" env:"WEB_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"WEB_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"WEB_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"WEB_DEBUG_ZPAGES"` } // HTTP defines the available http configuration. type HTTP struct { - Addr string `ocisConfig:"addr"` - Root string `ocisConfig:"root"` + Addr string `ocisConfig:"addr" env:"WEB_HTTP_ADDR"` Namespace string - CacheTTL int `ocisConfig:"cache_ttl"` + Root string `ocisConfig:"root" env:"WEB_HTTP_ROOT"` + CacheTTL int `ocisConfig:"cache_ttl" env:"WEB_CACHE_TTL"` } // Service defines the available service configuration. type Service struct { - Name string `ocisConfig:"name"` - Version string `ocisConfig:"version"` + Name string + Version string } // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` - Service string `ocisConfig:"service"` + Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;WEB_TRACING_ENABLED"` + Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;WEB_TRACING_TYPE"` + Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;WEB_TRACING_ENDPOINT"` + Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;WEB_TRACING_COLLECTOR"` + Service string `ocisConfig:"service" env:"WEB_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name? +} + +// Log defines the available log configuration. +type Log struct { + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;WEB_LOG_LEVEL"` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;WEB_LOG_PRETTY"` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;WEB_LOG_COLOR"` + File string `mapstructure:"file" env:"OCIS_LOG_FILE;WEB_LOG_FILE"` } // Asset defines the available asset configuration. type Asset struct { - Path string `ocisConfig:"path"` + Path string `ocisConfig:"path" env:"WEB_ASSET_PATH"` } // WebConfig defines the available web configuration for a dynamically rendered config.json. type WebConfig struct { - Server string `json:"server,omitempty" ocisConfig:"server"` - Theme string `json:"theme,omitempty" ocisConfig:"theme"` - Version string `json:"version,omitempty" ocisConfig:"version"` + Server string `json:"server,omitempty" ocisConfig:"server" env:"OCIS_URL;WEB_UI_CONFIG_SERVER"` + Theme string `json:"theme,omitempty" ocisConfig:"theme" env:""` + Version string `json:"version,omitempty" ocisConfig:"version" env:"WEB_UI_CONFIG_VERSION"` OpenIDConnect OIDC `json:"openIdConnect,omitempty" ocisConfig:"oids"` Apps []string `json:"apps" ocisConfig:"apps"` ExternalApps []ExternalApp `json:"external_apps,omitempty" ocisConfig:"external_apps"` @@ -55,11 +61,11 @@ type WebConfig struct { // OIDC defines the available oidc configuration type OIDC struct { - MetadataURL string `json:"metadata_url,omitempty" ocisConfig:"metadata_url"` - Authority string `json:"authority,omitempty" ocisConfig:"authority"` - ClientID string `json:"client_id,omitempty" ocisConfig:"client_id"` - ResponseType string `json:"response_type,omitempty" ocisConfig:"response_type"` - Scope string `json:"scope,omitempty" ocisConfig:"scope"` + MetadataURL string `json:"metadata_url,omitempty" ocisConfig:"metadata_url" env:"WEB_OIDC_METADATA_URL"` + Authority string `json:"authority,omitempty" ocisConfig:"authority" env:"OCIS_URL;WEB_OIDC_AUTHORITY"` + ClientID string `json:"client_id,omitempty" ocisConfig:"client_id" env:"WEB_OIDC_CLIENT_ID"` + ResponseType string `json:"response_type,omitempty" ocisConfig:"response_type" env:"WEB_OIDC_RESPONSE_TYPE"` + Scope string `json:"scope,omitempty" ocisConfig:"scope" env:"WEB_OIDC_SCOPE"` } // ExternalApp defines an external web app. @@ -79,38 +85,35 @@ type ExternalApp struct { // ExternalAppConfig defines an external web app configuration. type ExternalAppConfig struct { - URL string `json:"url,omitempty" ocisConfig:"url"` + URL string `json:"url,omitempty" ocisConfig:"url" env:""` } // Web defines the available web configuration. type Web struct { - Path string `ocisConfig:"path"` - ThemeServer string `ocisConfig:"theme_server"` // used to build Theme in WebConfig - ThemePath string `ocisConfig:"theme_path"` // used to build Theme in WebConfig + Path string `ocisConfig:"path" env:"WEB_UI_PATH"` + ThemeServer string `ocisConfig:"theme_server" env:"OCIS_URL;WEB_UI_THEME_SERVER"` // used to build Theme in WebConfig + ThemePath string `ocisConfig:"theme_path" env:"WEB_UI_THEME_PATH"` // used to build Theme in WebConfig Config WebConfig `ocisConfig:"config"` } // Config combines all available configuration parts. type Config struct { - *shared.Commons + Service Service `ocisConfig:"service"` - Log *shared.Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - HTTP HTTP `ocisConfig:"http"` - Service Service `ocisConfig:"service"` - Tracing Tracing `ocisConfig:"tracing"` - Asset Asset `ocisConfig:"asset"` - Web Web `ocisConfig:"web"` + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + HTTP HTTP `ocisConfig:"http"` + + Asset Asset `ocisConfig:"asset"` + File string `ocisConfig:"file" env:"WEB_UI_CONFIG"` // TODO: rename this to a more self explaining string + Web Web `ocisConfig:"web"` Context context.Context Supervised bool } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} -} - func DefaultConfig() *Config { return &Config{ Debug: Debug{ diff --git a/web/pkg/config/mappings.go b/web/pkg/config/mappings.go deleted file mode 100644 index 3957c225e..000000000 --- a/web/pkg/config/mappings.go +++ /dev/null @@ -1,143 +0,0 @@ -package config - -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(cfg *Config) []string { - var r = make([]string, len(structMappings(cfg))) - for i := range structMappings(cfg) { - r = append(r, structMappings(cfg)[i].EnvVars...) - } - - return r -} - -// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the -// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets -// us propagate changes easier. -func StructMappings(cfg *Config) []shared.EnvBinding { - return structMappings(cfg) -} - -// structMappings binds a set of environment variables to a destination on cfg. -func structMappings(cfg *Config) []shared.EnvBinding { - return []shared.EnvBinding{ - { - EnvVars: []string{"OCIS_LOG_LEVEL", "WEB_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - { - EnvVars: []string{"OCIS_LOG_PRETTY", "WEB_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - { - EnvVars: []string{"OCIS_LOG_COLOR", "WEB_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - { - EnvVars: []string{"OCIS_LOG_FILE", "WEB_LOG_FILE"}, - Destination: &cfg.Log.File, - }, - { - EnvVars: []string{"WEB_CONFIG_FILE"}, - Destination: &cfg.File, - }, - { - EnvVars: []string{"OCIS_TRACING_ENABLED", "WEB_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - { - EnvVars: []string{"OCIS_TRACING_TYPE", "WEB_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - { - EnvVars: []string{"OCIS_TRACING_ENDPOINT", "WEB_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - { - EnvVars: []string{"OCIS_TRACING_COLLECTOR", "WEB_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - { - EnvVars: []string{"WEB_TRACING_SERVICE"}, - Destination: &cfg.Tracing.Service, - }, - { - EnvVars: []string{"WEB_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - { - EnvVars: []string{"WEB_DEBUG_TOKEN"}, - Destination: &cfg.Debug.Token, - }, - { - EnvVars: []string{"WEB_DEBUG_PPROF"}, - Destination: &cfg.Debug.Pprof, - }, - { - EnvVars: []string{"WEB_DEBUG_ZPAGES"}, - Destination: &cfg.Debug.Zpages, - }, - { - EnvVars: []string{"WEB_HTTP_ADDR"}, - Destination: &cfg.HTTP.Addr, - }, - { - EnvVars: []string{"WEB_HTTP_ROOT"}, - Destination: &cfg.HTTP.Root, - }, - { - EnvVars: []string{"WEB_NAMESPACE"}, - Destination: &cfg.HTTP.Namespace, - }, - { - EnvVars: []string{"WEB_CACHE_TTL"}, - Destination: &cfg.HTTP.CacheTTL, - }, - { - EnvVars: []string{"WEB_ASSET_PATH"}, - Destination: &cfg.Asset.Path, - }, - { - EnvVars: []string{"WEB_UI_CONFIG"}, - Destination: &cfg.Web.Path, - }, - { - EnvVars: []string{"OCIS_URL", "WEB_UI_CONFIG_SERVER"}, // WEB_UI_CONFIG_SERVER takes precedence over OCIS_URL - Destination: &cfg.Web.Config.Server, - }, - { - EnvVars: []string{"OCIS_URL", "WEB_UI_THEME_SERVER"}, // WEB_UI_THEME_SERVER takes precedence over OCIS_URL - Destination: &cfg.Web.ThemeServer, - }, - { - EnvVars: []string{"WEB_UI_THEME_PATH"}, - Destination: &cfg.Web.ThemePath, - }, - { - EnvVars: []string{"WEB_UI_CONFIG_VERSION"}, - Destination: &cfg.Web.Config.Version, - }, - { - EnvVars: []string{"WEB_OIDC_METADATA_URL"}, - Destination: &cfg.Web.Config.OpenIDConnect.MetadataURL, - }, - { - EnvVars: []string{"OCIS_URL", "WEB_OIDC_AUTHORITY"}, // WEB_OIDC_AUTHORITY takes precedence over OCIS_URL - Destination: &cfg.Web.Config.OpenIDConnect.Authority, - }, - { - EnvVars: []string{"WEB_OIDC_CLIENT_ID"}, - Destination: &cfg.Web.Config.OpenIDConnect.ClientID, - }, - { - EnvVars: []string{"WEB_OIDC_RESPONSE_TYPE"}, - Destination: &cfg.Web.Config.OpenIDConnect.ResponseType, - }, - { - EnvVars: []string{"WEB_OIDC_SCOPE"}, - Destination: &cfg.Web.Config.OpenIDConnect.Scope, - }, - } -} diff --git a/web/pkg/logging/logging.go b/web/pkg/logging/logging.go new file mode 100644 index 000000000..6510b8c21 --- /dev/null +++ b/web/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/web/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/webdav/pkg/command/health.go b/webdav/pkg/command/health.go index 58aca1e1d..e1f2be33e 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/logging" "github.com/urfave/cli/v2" ) @@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command { return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) + logger := logging.Configure(cfg.Service.Name, cfg.Log) resp, err := http.Get( fmt.Sprintf( diff --git a/webdav/pkg/command/root.go b/webdav/pkg/command/root.go index f221de832..15d0db369 100644 --- a/webdav/pkg/command/root.go +++ b/webdav/pkg/command/root.go @@ -4,14 +4,13 @@ import ( "context" "os" - "github.com/owncloud/ocis/ocis-pkg/shared" - + "github.com/imdario/mergo" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" - "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/webdav/pkg/config" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" + "github.com/wkloucek/envdecode" ) // Execute is the entry point for the ocis-webdav command. @@ -30,7 +29,7 @@ func Execute(cfg *config.Config) error { }, Before: func(c *cli.Context) error { cfg.Service.Version = version.String - return nil + return ParseConfig(c, cfg) }, Commands: []*cli.Command{ @@ -52,41 +51,37 @@ 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("webdav"), - log.Level(cfg.Log.Level), - log.Pretty(cfg.Log.Pretty), - log.Color(cfg.Log.Color), - log.File(cfg.Log.File), - ) -} - // ParseConfig loads graph configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - conf, err := ociscfg.BindSourcesToStructs("webdav", cfg) + _, 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 = &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{} - } + //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(cfg), false) + envCfg := config.Config{} + if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" { + return err + } - bindings := config.StructMappings(cfg) - return ociscfg.BindEnv(conf, bindings) + // merge environment variable config on top of the current config + if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil { + return err + } + + return nil } // SutureService allows for the webdav command to be embedded and supervised by a suture supervisor tree. @@ -96,7 +91,7 @@ type SutureService struct { // NewSutureService creates a new webdav.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - cfg.Proxy.Commons = cfg.Commons + //cfg.Proxy.Commons = cfg.Commons return SutureService{ cfg: cfg.WebDAV, } diff --git a/webdav/pkg/command/server.go b/webdav/pkg/command/server.go index d3f68efcf..7732f04bb 100644 --- a/webdav/pkg/command/server.go +++ b/webdav/pkg/command/server.go @@ -6,6 +6,7 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/webdav/pkg/config" + "github.com/owncloud/ocis/webdav/pkg/logging" "github.com/owncloud/ocis/webdav/pkg/metrics" "github.com/owncloud/ocis/webdav/pkg/server/debug" "github.com/owncloud/ocis/webdav/pkg/server/http" @@ -30,9 +31,9 @@ func Server(cfg *config.Config) *cli.Command { return nil }, Action: func(c *cli.Context) error { - logger := NewLogger(cfg) - - if err := tracing.Configure(cfg); err != nil { + logger := logging.Configure(cfg.Service.Name, cfg.Log) + err := tracing.Configure(cfg) + if err != nil { return err } diff --git a/webdav/pkg/config/config.go b/webdav/pkg/config/config.go index 22964b3f9..076c33177 100644 --- a/webdav/pkg/config/config.go +++ b/webdav/pkg/config/config.go @@ -8,10 +8,10 @@ import ( // Debug defines the available debug configuration. type Debug struct { - Addr string `ocisConfig:"addr"` - Token string `ocisConfig:"token"` - Pprof bool `ocisConfig:"pprof"` - Zpages bool `ocisConfig:"zpages"` + Addr string `ocisConfig:"addr" env:"WEBDAV_DEBUG_ADDR"` + Token string `ocisConfig:"token" env:"WEBDAV_DEBUG_TOKEN"` + Pprof bool `ocisConfig:"pprof" env:"WEBDAV_DEBUG_PPROF"` + Zpages bool `ocisConfig:"zpages" env:"WEBDAV_DEBUG_ZPAGES"` } // CORS defines the available cors configuration. @@ -24,50 +24,55 @@ type CORS struct { // HTTP defines the available http configuration. type HTTP struct { - Addr string `ocisConfig:"addr"` - Root string `ocisConfig:"root"` + Addr string `ocisConfig:"addr" env:"WEBDAV_HTTP_ADDR"` Namespace string - CORS CORS `ocisConfig:"cors"` + Root string `ocisConfig:"root" env:"WEBDAV_HTTP_ROOT"` + CORS CORS `ocisConfig:"cors"` } // Service defines the available service configuration. type Service struct { - Name string `ocisConfig:"name"` - Version string `ocisConfig:"version"` + Name string + Version string } // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` - Service string `ocisConfig:"service"` + Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;WEBDAV_TRACING_ENABLED"` + Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;WEBDAV_TRACING_TYPE"` + Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;WEBDAV_TRACING_ENDPOINT"` + Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;WEBDAV_TRACING_COLLECTOR"` + Service string `ocisConfig:"service" env:"WEBDAV_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name? +} + +// Log defines the available log configuration. +type Log struct { + Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;WEBDAV_LOG_LEVEL"` + Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;WEBDAV_LOG_PRETTY"` + Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;WEBDAV_LOG_COLOR"` + File string `mapstructure:"file" env:"OCIS_LOG_FILE;WEBDAV_LOG_FILE"` } // Config combines all available configuration parts. type Config struct { *shared.Commons - File string `ocisConfig:"file"` - Log *shared.Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` - HTTP HTTP `ocisConfig:"http"` - Tracing Tracing `ocisConfig:"tracing"` - Service Service `ocisConfig:"service"` - OcisPublicURL string `ocisConfig:"ocis_public_url"` - WebdavNamespace string `ocisConfig:"webdav_namespace"` - RevaGateway string `ocisConfig:"reva_gateway"` + Service Service `ocisConfig:"service"` + + Tracing Tracing `ocisConfig:"tracing"` + Log Log `ocisConfig:"log"` + Debug Debug `ocisConfig:"debug"` + + HTTP HTTP `ocisConfig:"http"` + + OcisPublicURL string `ocisConfig:"ocis_public_url" env:"OCIS_URL;OCIS_PUBLIC_URL"` + WebdavNamespace string `ocisConfig:"webdav_namespace" env:"STORAGE_WEBDAV_NAMESPACE"` //TODO: prevent this cross config + RevaGateway string `ocisConfig:"reva_gateway" env:"REVA_GATEWAY"` Context context.Context Supervised bool } -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{} -} - func DefaultConfig() *Config { return &Config{ Debug: Debug{ diff --git a/webdav/pkg/config/mappings.go b/webdav/pkg/config/mappings.go deleted file mode 100644 index 350ab25fd..000000000 --- a/webdav/pkg/config/mappings.go +++ /dev/null @@ -1,107 +0,0 @@ -package config - -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(cfg *Config) []string { - var r = make([]string, len(structMappings(cfg))) - for i := range structMappings(cfg) { - r = append(r, structMappings(cfg)[i].EnvVars...) - } - - return r -} - -// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the -// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets -// us propagate changes easier. -func StructMappings(cfg *Config) []shared.EnvBinding { - return structMappings(cfg) -} - -// structMappings binds a set of environment variables to a destination on cfg. -func structMappings(cfg *Config) []shared.EnvBinding { - return []shared.EnvBinding{ - { - EnvVars: []string{"OCIS_LOG_FILE", "WEBDAV_LOG_FILE"}, - Destination: &cfg.Log.File, - }, - { - EnvVars: []string{"OCIS_LOG_LEVEL", "WEBDAV_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - { - EnvVars: []string{"OCIS_LOG_PRETTY", "WEBDAV_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - { - EnvVars: []string{"OCIS_LOG_COLOR", "WEBDAV_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - { - EnvVars: []string{"WEBDAV_CONFIG_FILE"}, - Destination: &cfg.File, - }, - { - EnvVars: []string{"OCIS_TRACING_ENABLED", "WEBDAV_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - { - EnvVars: []string{"OCIS_TRACING_TYPE", "WEBDAV_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - { - EnvVars: []string{"OCIS_TRACING_ENDPOINT", "WEBDAV_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - { - EnvVars: []string{"OCIS_TRACING_COLLECTOR", "WEBDAV_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - { - EnvVars: []string{"WEBDAV_TRACING_SERVICE"}, - Destination: &cfg.Tracing.Service, - }, - { - EnvVars: []string{"WEBDAV_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - { - EnvVars: []string{"WEBDAV_DEBUG_TOKEN"}, - Destination: &cfg.Debug.Token, - }, - { - EnvVars: []string{"WEBDAV_DEBUG_PPROF"}, - Destination: &cfg.Debug.Pprof, - }, - { - EnvVars: []string{"WEBDAV_DEBUG_ZPAGES"}, - Destination: &cfg.Debug.Zpages, - }, - { - EnvVars: []string{"WEBDAV_HTTP_ADDR"}, - Destination: &cfg.HTTP.Addr, - }, - { - EnvVars: []string{"WEBDAV_HTTP_NAMESPACE"}, - Destination: &cfg.HTTP.Namespace, - }, - { - EnvVars: []string{"WEBDAV_HTTP_ROOT"}, - Destination: &cfg.HTTP.Root, - }, - { - EnvVars: []string{"OCIS_URL", "OCIS_PUBLIC_URL"}, - Destination: &cfg.OcisPublicURL, - }, - { - EnvVars: []string{"STORAGE_WEBDAV_NAMESPACE"}, - Destination: &cfg.WebdavNamespace, - }, - { - EnvVars: []string{"REVA_GATEWAY"}, - Destination: &cfg.RevaGateway, - }, - } -} diff --git a/webdav/pkg/logging/logging.go b/webdav/pkg/logging/logging.go new file mode 100644 index 000000000..11c8f85aa --- /dev/null +++ b/webdav/pkg/logging/logging.go @@ -0,0 +1,17 @@ +package logging + +import ( + "github.com/owncloud/ocis/ocis-pkg/log" + "github.com/owncloud/ocis/webdav/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), + ) +}