From ca997e5bfcbc4755d0ef79e29d1105849b54df6f Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Fri, 5 Nov 2021 13:33:03 +0100 Subject: [PATCH] migrate web to the new config scheme --- accounts/pkg/config/config.go | 2 +- ocis-pkg/config/config.go | 2 +- ocis/pkg/command/web.go | 2 - settings/pkg/config/config.go | 2 +- web/cmd/web/main.go | 2 +- web/pkg/command/root.go | 48 +++---------- web/pkg/command/server.go | 15 +--- web/pkg/config/config.go | 101 ++++++++++++++++++++++++++- web/pkg/config/env.go | 128 ++++++++++++++++++++++++++++++++++ 9 files changed, 242 insertions(+), 60 deletions(-) create mode 100644 web/pkg/config/env.go diff --git a/accounts/pkg/config/config.go b/accounts/pkg/config/config.go index 2ca48d58f..340f07f04 100644 --- a/accounts/pkg/config/config.go +++ b/accounts/pkg/config/config.go @@ -163,7 +163,7 @@ func DefaultConfig() *Config { Addr: "127.0.0.1:9181", Namespace: "com.owncloud.web", Root: "/", - CacheTTL: 604800, + CacheTTL: 604800, // 7 days CORS: CORS{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, diff --git a/ocis-pkg/config/config.go b/ocis-pkg/config/config.go index f445a9991..74d92dc2f 100644 --- a/ocis-pkg/config/config.go +++ b/ocis-pkg/config/config.go @@ -122,7 +122,7 @@ func New() *Config { GraphExplorer: graphExplorer.DefaultConfig(), OCS: ocs.DefaultConfig(), Settings: settings.DefaultConfig(), - Web: web.New(), + Web: web.DefaultConfig(), Storage: storage.New(), Store: store.New(), Thumbnails: thumbnails.DefaultConfig(), diff --git a/ocis/pkg/command/web.go b/ocis/pkg/command/web.go index 431443cad..736d32060 100644 --- a/ocis/pkg/command/web.go +++ b/ocis/pkg/command/web.go @@ -5,7 +5,6 @@ import ( "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/web/pkg/command" svcconfig "github.com/owncloud/ocis/web/pkg/config" - "github.com/owncloud/ocis/web/pkg/flagset" "github.com/urfave/cli/v2" ) @@ -15,7 +14,6 @@ func WebCommand(cfg *config.Config) *cli.Command { Name: "web", Usage: "Start web server", Category: "Extensions", - Flags: flagset.ServerWithConfig(cfg.Web), Before: func(ctx *cli.Context) error { return ParseConfig(ctx, cfg) }, diff --git a/settings/pkg/config/config.go b/settings/pkg/config/config.go index d8f7302c4..b6d3f29e7 100644 --- a/settings/pkg/config/config.go +++ b/settings/pkg/config/config.go @@ -115,7 +115,7 @@ func DefaultConfig() *Config { Addr: "127.0.0.1:9190", Namespace: "com.owncloud.web", Root: "/", - CacheTTL: 604800, // 10 days + CacheTTL: 604800, // 7 days CORS: CORS{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, diff --git a/web/cmd/web/main.go b/web/cmd/web/main.go index 50ca749a7..7334862fe 100644 --- a/web/cmd/web/main.go +++ b/web/cmd/web/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/web/pkg/command/root.go b/web/pkg/command/root.go index 0114791d3..bef81771b 100644 --- a/web/pkg/command/root.go +++ b/web/pkg/command/root.go @@ -3,15 +3,11 @@ package command import ( "context" "os" - "strings" - - "github.com/owncloud/ocis/ocis-pkg/sync" 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/spf13/viper" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -59,46 +55,18 @@ func NewLogger(cfg *config.Config) log.Logger { ) } -// ParseConfig loads proxy configuration from Viper known paths. +// ParseConfig loads graph configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - sync.ParsingViperConfig.Lock() - defer sync.ParsingViperConfig.Unlock() - logger := NewLogger(cfg) - - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - viper.SetEnvPrefix("WEB") - viper.AutomaticEnv() - - if c.IsSet("config-file") { - viper.SetConfigFile(c.String("config-file")) - } else { - viper.SetConfigName("web") - - viper.AddConfigPath("/etc/ocis") - viper.AddConfigPath("$HOME/.ocis") - viper.AddConfigPath("./config") + conf, err := ociscfg.BindSourcesToStructs("web", cfg) + if err != nil { + return err } - if err := viper.ReadInConfig(); err != nil { - switch err.(type) { - case viper.ConfigFileNotFoundError: - logger.Debug(). - Msg("no config found on preconfigured location") - case viper.UnsupportedConfigError: - logger.Fatal(). - Err(err). - Msg("unsupported config type") - default: - logger.Fatal(). - Err(err). - Msg("failed to read config") - } - } + // load all env variables relevant to the config in the current context. + conf.LoadOSEnv(config.GetEnv(), false) - if err := viper.Unmarshal(&cfg); err != nil { - logger.Fatal(). - Err(err). - Msg("failed to parse config") + if err = cfg.UnmapEnv(conf); err != nil { + return err } return nil diff --git a/web/pkg/command/server.go b/web/pkg/command/server.go index 77ee5dce3..ff4865651 100644 --- a/web/pkg/command/server.go +++ b/web/pkg/command/server.go @@ -9,7 +9,6 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/web/pkg/config" - "github.com/owncloud/ocis/web/pkg/flagset" "github.com/owncloud/ocis/web/pkg/metrics" "github.com/owncloud/ocis/web/pkg/server/debug" "github.com/owncloud/ocis/web/pkg/server/http" @@ -22,24 +21,16 @@ func Server(cfg *config.Config) *cli.Command { return &cli.Command{ Name: "server", Usage: "Start integrated server", - Flags: flagset.ServerWithConfig(cfg), Before: func(ctx *cli.Context) error { - logger := NewLogger(cfg) if cfg.HTTP.Root != "/" { cfg.HTTP.Root = strings.TrimRight(cfg.HTTP.Root, "/") } - // StringSliceFlag doesn't support Destination - // UPDATE Destination on string flags supported. Wait for https://github.com/urfave/cli/pull/1078 to get to micro/cli - if len(ctx.StringSlice("web-config-app")) > 0 { - cfg.Web.Config.Apps = ctx.StringSlice("web-config-app") + if err := ParseConfig(ctx, cfg); err != nil { + return err } - if !cfg.Supervised { - if err := ParseConfig(ctx, cfg); err != nil { - return err - } - } + logger := NewLogger(cfg) logger.Debug().Str("service", "web").Msg("ignoring config file parsing when running supervised") // build well known openid-configuration endpoint if it is not set diff --git a/web/pkg/config/config.go b/web/pkg/config/config.go index 64a03838b..7e0f795a7 100644 --- a/web/pkg/config/config.go +++ b/web/pkg/config/config.go @@ -1,6 +1,12 @@ package config -import "context" +import ( + "context" + "fmt" + "reflect" + + gofig "github.com/gookit/config/v2" +) // Log defines the available logging configuration. type Log struct { @@ -96,7 +102,6 @@ type Config struct { HTTP HTTP Tracing Tracing Asset Asset - OIDC OIDC Web Web Context context.Context @@ -107,3 +112,95 @@ type Config struct { func New() *Config { return &Config{} } + +func DefaultConfig() *Config { + return &Config{ + Log: Log{}, + Debug: Debug{ + Addr: "127.0.0.1:9104", + Token: "", + Pprof: false, + Zpages: false, + }, + HTTP: HTTP{ + Addr: "127.0.0.1:9100", + Root: "/", + Namespace: "com.owncloud.web", + CacheTTL: 604800, // 7 days + }, + Tracing: Tracing{ + Enabled: false, + Type: "jaeger", + Endpoint: "", + Collector: "", + Service: "web", + }, + Asset: Asset{ + Path: "", + }, + Web: Web{ + Path: "", + ThemeServer: "https://localhost:9200", + ThemePath: "/themes/owncloud/theme.json", + Config: WebConfig{ + Server: "https://localhost:9200", + Theme: "", + Version: "0.1.0", + OpenIDConnect: OIDC{ + MetadataURL: "", + Authority: "https://localhost:9200", + ClientID: "web", + ResponseType: "code", + Scope: "openid profile email", + }, + Apps: []string{"files", "search", "media-viewer", "external"}, + }, + }, + } +} + +// 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 +} + +// UnmapEnv loads values from the gooconf.Config argument and sets them in the expected destination. +func (c *Config) UnmapEnv(gooconf *gofig.Config) error { + vals := structMappings(c) + for i := range vals { + for j := range vals[i].EnvVars { + // we need to guard against v != "" because this is the condition that checks that the value is set from the environment. + // the `ok` guard is not enough, apparently. + if v, ok := gooconf.GetValue(vals[i].EnvVars[j]); ok && v != "" { + + // get the destination type from destination + switch reflect.ValueOf(vals[i].Destination).Type().String() { + case "*bool": + r := gooconf.Bool(vals[i].EnvVars[j]) + *vals[i].Destination.(*bool) = r + case "*string": + r := gooconf.String(vals[i].EnvVars[j]) + *vals[i].Destination.(*string) = r + case "*int": + r := gooconf.Int(vals[i].EnvVars[j]) + *vals[i].Destination.(*int) = r + case "*float64": + // defaults to float64 + r := gooconf.Float(vals[i].EnvVars[j]) + *vals[i].Destination.(*float64) = r + default: + // it is unlikely we will ever get here. Let this serve more as a runtime check for when debugging. + return fmt.Errorf("invalid type for env var: `%v`", vals[i].EnvVars[j]) + } + } + } + } + + return nil +} diff --git a/web/pkg/config/env.go b/web/pkg/config/env.go new file mode 100644 index 000000000..771fd4c80 --- /dev/null +++ b/web/pkg/config/env.go @@ -0,0 +1,128 @@ +package config + +type mapping struct { + EnvVars []string // name of the EnvVars var. + Destination interface{} // memory address of the original config value to modify. +} + +// structMappings binds a set of environment variables to a destination on cfg. +func structMappings(cfg *Config) []mapping { + return []mapping{ + { + EnvVars: []string{"WEB_LOG_LEVEL", "OCIS_LOG_LEVEL"}, + Destination: &cfg.Log.Level, + }, + { + EnvVars: []string{"WEB_LOG_PRETTY", "OCIS_LOG_PRETTY"}, + Destination: &cfg.Log.Pretty, + }, + { + EnvVars: []string{"WEB_LOG_COLOR", "OCIS_LOG_COLOR"}, + Destination: &cfg.Log.Color, + }, + { + EnvVars: []string{"WEB_LOG_FILE", "OCIS_LOG_FILE"}, + Destination: &cfg.Log.File, + }, + { + EnvVars: []string{"WEB_CONFIG_FILE"}, + Destination: &cfg.File, + }, + { + EnvVars: []string{"WEB_TRACING_ENABLED", "OCIS_TRACING_ENABLED"}, + Destination: &cfg.Tracing.Enabled, + }, + { + EnvVars: []string{"WEB_TRACING_TYPE", "OCIS_TRACING_TYPE"}, + Destination: &cfg.Tracing.Type, + }, + { + EnvVars: []string{"WEB_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"}, + Destination: &cfg.Tracing.Endpoint, + }, + { + EnvVars: []string{"WEB_TRACING_COLLECTOR", "OCIS_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{"WEB_UI_CONFIG_SERVER", "OCIS_URL"}, // WEB_UI_CONFIG_SERVER takes precedence over OCIS_URL + Destination: &cfg.Web.Config.Server, + }, + { + EnvVars: []string{"WEB_UI_THEME_SERVER", "OCIS_URL"}, // 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{"WEB_OIDC_AUTHORITY", "OCIS_URL"}, // 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, + }, + } +}