add ocis-pkg/config default config + fix logging inheritance

This commit is contained in:
A.Unger
2021-11-08 11:14:26 +01:00
parent 835928b29b
commit 74dae6dad9
11 changed files with 152 additions and 249 deletions

View File

@@ -63,17 +63,22 @@ func NewLogger(cfg *config.Config) log.Logger {
)
}
// ParseConfig loads proxy configuration from known paths.
// 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.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
conf, err := ociscfg.BindSourcesToStructs("proxy", cfg)
if err != nil {
return err
}
// load all env variables relevant to the config in the current context.
conf.LoadOSEnv(config.GetEnv(), false)
bindings := config.StructMappings(cfg)
return ociscfg.UnbindEnv(conf, bindings)
return ociscfg.BindEnv(conf, bindings)
}
// SutureService allows for the proxy command to be embedded and supervised by a suture supervisor tree.
@@ -83,10 +88,6 @@ type SutureService struct {
// NewSutureService creates a new proxy.SutureService
func NewSutureService(cfg *ociscfg.Config) suture.Service {
inheritLogging(cfg)
if cfg.Mode == 0 {
cfg.Proxy.Supervised = true
}
return SutureService{
cfg: cfg.Proxy,
}
@@ -100,13 +101,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.Proxy.Log.File = cfg.Log.File
cfg.Proxy.Log.Color = cfg.Log.Color
cfg.Proxy.Log.Pretty = cfg.Log.Pretty
cfg.Proxy.Log.Level = cfg.Log.Level
}

View File

@@ -141,10 +141,10 @@ type OIDC struct {
// PolicySelector is the toplevel-configuration for different selectors
type PolicySelector struct {
Static *StaticSelectorConf
Migration *MigrationSelectorConf
Claims *ClaimsSelectorConf
Regex *RegexSelectorConf
Static *StaticSelectorConf `mapstructure:"static"`
Migration *MigrationSelectorConf `mapstructure:"migration"`
Claims *ClaimsSelectorConf `mapstructure:"claims"`
Regex *RegexSelectorConf `mapstructure:"regex"`
}
// StaticSelectorConf is the config for the static-policy-selector

View File

@@ -1,17 +0,0 @@
package config
type mapping struct {
EnvVars []string // name of the EnvVars var.
Destination interface{} // memory address of the original config value to modify.
}
// 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
}

View File

@@ -2,7 +2,20 @@ package config
import "github.com/owncloud/ocis/ocis-pkg/shared"
// StructMappings binds a set of environment variables to a destination on cfg.
// 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
}
// 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)
}