From a515a97c8fa3c4cf4961031d7899f7331689c44a Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 8 Nov 2021 12:17:23 +0100 Subject: [PATCH] normalize idp --- accounts/pkg/command/root.go | 7 +-- accounts/pkg/config/{env.go => mappings.go} | 22 +++++-- glauth/pkg/config/config.go | 38 ------------ graph-explorer/pkg/config/config.go | 38 ------------ graph/pkg/config/config.go | 38 ------------ idp/pkg/command/root.go | 23 +------ idp/pkg/command/server.go | 29 ++++----- idp/pkg/config/config.go | 69 ++++----------------- idp/pkg/config/{env.go => mappings.go} | 14 +++-- ocs/pkg/server/http/svc_test.go | 4 +- 10 files changed, 58 insertions(+), 224 deletions(-) rename accounts/pkg/config/{env.go => mappings.go} (82%) rename idp/pkg/config/{env.go => mappings.go} (91%) diff --git a/accounts/pkg/command/root.go b/accounts/pkg/command/root.go index af45789771..153a01545f 100644 --- a/accounts/pkg/command/root.go +++ b/accounts/pkg/command/root.go @@ -76,11 +76,8 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { // load all env variables relevant to the config in the current context. conf.LoadOSEnv(config.GetEnv(), false) - if err = cfg.UnmapEnv(conf); err != nil { - return err - } - - return nil + bindings := config.StructMappings(cfg) + return ociscfg.BindEnv(conf, bindings) } // SutureService allows for the accounts command to be embedded and supervised by a suture supervisor tree. diff --git a/accounts/pkg/config/env.go b/accounts/pkg/config/mappings.go similarity index 82% rename from accounts/pkg/config/env.go rename to accounts/pkg/config/mappings.go index d2193575b4..cb710237f6 100644 --- a/accounts/pkg/config/env.go +++ b/accounts/pkg/config/mappings.go @@ -1,17 +1,29 @@ package config -type mapping struct { - EnvVars []string // name of the EnvVars var. - Destination interface{} // memory address of the original config value to modify. +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) []mapping { - return []mapping{ +func structMappings(cfg *Config) []shared.EnvBinding { + return []shared.EnvBinding{ { EnvVars: []string{"ACCOUNTS_LOG_FILE", "OCIS_LOG_FILE"}, Destination: &cfg.Log.File, }, + { + EnvVars: []string{"ACCOUNTS_LOG_COLOR", "OCIS_LOG_COLOR"}, + Destination: &cfg.Log.Color, + }, + { + EnvVars: []string{"ACCOUNTS_LOG_PRETTY", "OCIS_LOG_PRETTY"}, + Destination: &cfg.Log.Pretty, + }, { EnvVars: []string{"ACCOUNTS_TRACING_ENABLED", "OCIS_TRACING_ENABLED"}, Destination: &cfg.Tracing.Enabled, diff --git a/glauth/pkg/config/config.go b/glauth/pkg/config/config.go index 89339a5fe4..00843b8845 100644 --- a/glauth/pkg/config/config.go +++ b/glauth/pkg/config/config.go @@ -2,13 +2,10 @@ package config import ( "context" - "fmt" "path" - "reflect" "github.com/owncloud/ocis/ocis-pkg/shared" - gofig "github.com/gookit/config/v2" "github.com/owncloud/ocis/ocis-pkg/config/defaults" ) @@ -140,38 +137,3 @@ func GetEnv() []string { 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/graph-explorer/pkg/config/config.go b/graph-explorer/pkg/config/config.go index 558273a683..07d55dd484 100644 --- a/graph-explorer/pkg/config/config.go +++ b/graph-explorer/pkg/config/config.go @@ -2,10 +2,7 @@ package config import ( "context" - "fmt" - "reflect" - gofig "github.com/gookit/config/v2" "github.com/owncloud/ocis/ocis-pkg/shared" ) @@ -107,38 +104,3 @@ func GetEnv() []string { 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/graph/pkg/config/config.go b/graph/pkg/config/config.go index 9bfb29167a..c29fcfb2eb 100644 --- a/graph/pkg/config/config.go +++ b/graph/pkg/config/config.go @@ -2,10 +2,7 @@ package config import ( "context" - "fmt" - "reflect" - gofig "github.com/gookit/config/v2" "github.com/owncloud/ocis/ocis-pkg/shared" ) @@ -118,38 +115,3 @@ func GetEnv() []string { 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/idp/pkg/command/root.go b/idp/pkg/command/root.go index 60db566552..1f78830a5d 100644 --- a/idp/pkg/command/root.go +++ b/idp/pkg/command/root.go @@ -71,11 +71,8 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { // load all env variables relevant to the config in the current context. conf.LoadOSEnv(config.GetEnv(), false) - if err = cfg.UnmapEnv(conf); err != nil { - return err - } - - return nil + bindings := config.StructMappings(cfg) + return ociscfg.BindEnv(conf, bindings) } // SutureService allows for the idp command to be embedded and supervised by a suture supervisor tree. @@ -85,11 +82,7 @@ type SutureService struct { // NewSutureService creates a new idp.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - inheritLogging(cfg) - if cfg.Mode == 0 { - cfg.IDP.Supervised = true - } - cfg.IDP.Log.File = cfg.Log.File + cfg.IDP.Log = cfg.Log return SutureService{ cfg: cfg.IDP, } @@ -103,13 +96,3 @@ func (s SutureService) Serve(ctx context.Context) error { return nil } - -// inheritLogging is a poor man's global logging state tip-toeing around circular dependencies. It sets the logging -// of the service to whatever is in the higher config (in this case coming from ocis.yaml) and sets them as defaults, -// being overwritten when the extension parses its config file / env variables. -func inheritLogging(cfg *ociscfg.Config) { - cfg.IDP.Log.File = cfg.Log.File - cfg.IDP.Log.Color = cfg.Log.Color - cfg.IDP.Log.Pretty = cfg.Log.Pretty - cfg.IDP.Log.Level = cfg.Log.Level -} diff --git a/idp/pkg/command/server.go b/idp/pkg/command/server.go index 97120aa07f..1036f6d6ad 100644 --- a/idp/pkg/command/server.go +++ b/idp/pkg/command/server.go @@ -24,24 +24,19 @@ func Server(cfg *config.Config) *cli.Command { cfg.HTTP.Root = strings.TrimSuffix(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("trusted-proxy")) > 0 { - cfg.IDP.TrustedProxy = ctx.StringSlice("trusted-proxy") - } + //if len(ctx.StringSlice("trusted-proxy")) > 0 { + // cfg.IDP.TrustedProxy = ctx.StringSlice("trusted-proxy") + //} + // + //if len(ctx.StringSlice("allow-scope")) > 0 { + // cfg.IDP.AllowScope = ctx.StringSlice("allow-scope") + //} + // + //if len(ctx.StringSlice("signing-private-key")) > 0 { + // cfg.IDP.SigningPrivateKeyFiles = ctx.StringSlice("signing-private-key") + //} - if len(ctx.StringSlice("allow-scope")) > 0 { - cfg.IDP.AllowScope = ctx.StringSlice("allow-scope") - } - - if len(ctx.StringSlice("signing-private-key")) > 0 { - cfg.IDP.SigningPrivateKeyFiles = ctx.StringSlice("signing-private-key") - } - - if err := ParseConfig(ctx, cfg); err != nil { - return err - } - return nil + return ParseConfig(ctx, cfg) }, Action: func(c *cli.Context) error { logger := NewLogger(cfg) diff --git a/idp/pkg/config/config.go b/idp/pkg/config/config.go index 7d0c2771f4..9ee63acfc2 100644 --- a/idp/pkg/config/config.go +++ b/idp/pkg/config/config.go @@ -2,23 +2,13 @@ package config import ( "context" - "fmt" "path" - "reflect" + + "github.com/owncloud/ocis/ocis-pkg/shared" "github.com/owncloud/ocis/ocis-pkg/config/defaults" - - gofig "github.com/gookit/config/v2" ) -// Log defines the available logging configuration. -type Log struct { - Level string `mapstructure:"level"` - Pretty bool `mapstructure:"pretty"` - Color bool `mapstructure:"color"` - File string `mapstructure:"file"` -} - // Debug defines the available debug configuration. type Debug struct { Addr string `mapstructure:"addr"` @@ -108,15 +98,15 @@ type Settings struct { // Config combines all available configuration parts. type Config struct { - File string `mapstructure:"file"` - Log Log `mapstructure:"log"` - Debug Debug `mapstructure:"debug"` - HTTP HTTP `mapstructure:"http"` - Tracing Tracing `mapstructure:"tracing"` - Asset Asset `mapstructure:"asset"` - IDP Settings `mapstructure:"idp"` - Ldap Ldap `mapstructure:"ldap"` - Service Service `mapstructure:"service"` + File string `mapstructure:"file"` + Log shared.Log `mapstructure:"log"` + Debug Debug `mapstructure:"debug"` + HTTP HTTP `mapstructure:"http"` + Tracing Tracing `mapstructure:"tracing"` + Asset Asset `mapstructure:"asset"` + IDP Settings `mapstructure:"idp"` + Ldap Ldap `mapstructure:"ldap"` + Service Service `mapstructure:"service"` Context context.Context Supervised bool @@ -129,7 +119,7 @@ func New() *Config { func DefaultConfig() *Config { return &Config{ - Log: Log{}, + Log: shared.Log{}, Debug: Debug{ Addr: "127.0.0.1:9134", }, @@ -210,38 +200,3 @@ func GetEnv() []string { 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/idp/pkg/config/env.go b/idp/pkg/config/mappings.go similarity index 91% rename from idp/pkg/config/env.go rename to idp/pkg/config/mappings.go index 71f4639796..60467e6de2 100644 --- a/idp/pkg/config/env.go +++ b/idp/pkg/config/mappings.go @@ -1,13 +1,17 @@ package config -type mapping struct { - EnvVars []string // name of the EnvVars var. - Destination interface{} // memory address of the original config value to modify. +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) []mapping { - return []mapping{ +func structMappings(cfg *Config) []shared.EnvBinding { + return []shared.EnvBinding{ { EnvVars: []string{"IDP_LOG_LEVEL", "OCIS_LOG_LEVEL"}, Destination: &cfg.Log.Level, diff --git a/ocs/pkg/server/http/svc_test.go b/ocs/pkg/server/http/svc_test.go index cdcb2eee9a..3016ee705c 100644 --- a/ocs/pkg/server/http/svc_test.go +++ b/ocs/pkg/server/http/svc_test.go @@ -14,6 +14,8 @@ import ( "strings" "testing" + "github.com/owncloud/ocis/ocis-pkg/shared" + user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" "github.com/cs3org/reva/pkg/auth/scope" @@ -549,7 +551,7 @@ func init() { Path: dataPath, }, }, - Log: accountsCfg.Log{ + Log: shared.Log{ Level: "info", Pretty: true, Color: true,