first examples of config parsing

This commit is contained in:
A.Unger
2021-10-29 16:15:35 +02:00
parent 34253fcae2
commit 94aeefb7fd
6 changed files with 89 additions and 70 deletions
@@ -0,0 +1,6 @@
log:
level: info
color: true
pretty: true
http:
addr: "${PROXY_HTTP_ADDR|localhost:2222}"
+14 -38
View File
@@ -3,15 +3,13 @@ package command
import (
"context"
"os"
"strings"
"github.com/owncloud/ocis/ocis-pkg/sync"
gofig "github.com/gookit/config/v2"
gooyaml "github.com/gookit/config/v2/yaml"
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/proxy/pkg/config"
"github.com/spf13/viper"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)
@@ -71,45 +69,23 @@ func NewLogger(cfg *config.Config) log.Logger {
// ParseConfig loads proxy configuration from Viper known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
sync.ParsingViperConfig.Lock()
defer sync.ParsingViperConfig.Unlock()
logger := NewLogger(cfg)
// create a new config and load files and env values onto it since this needs to be thread-safe.
cnf := gofig.NewWithOptions("proxy", gofig.ParseEnv)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("PROXY")
viper.AutomaticEnv()
// TODO(refs) add ENV + toml + json
cnf.AddDriver(gooyaml.Driver)
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("proxy")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
// TODO(refs) load from expected locations with the expected name
err := cnf.LoadFiles("/Users/aunger/code/owncloud/ocis/proxy/pkg/command/proxy_example_config.yaml")
if err != nil {
panic(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")
}
}
// bind all keys to cfg, as we expect an entire proxy.[yaml, toml...] to define all keys and not only sub values.
err = cnf.BindStruct("", cfg)
if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
}
// step 2: overwrite the config values with those from ENV variables. Sadly the library only parses config files and does
// not support tags for env variables.
return nil
}
+5 -2
View File
@@ -55,9 +55,12 @@ func Server(cfg *config.Config) *cli.Command {
return err
}
if !cfg.Supervised {
return ParseConfig(ctx, cfg)
if err := ParseConfig(ctx, cfg); err != nil {
panic(err)
}
//if !cfg.Supervised {
// return ParseConfig(ctx, cfg)
//}
logger.Debug().Str("service", "ocs").Msg("ignoring config file parsing when running supervised")
return nil
},
+30 -30
View File
@@ -25,11 +25,11 @@ type Debug struct {
// HTTP defines the available http configuration.
type HTTP struct {
Addr string
Root string
TLSCert string
TLSKey string
TLS bool
Addr string `mapstructure:"addr"`
Root string `mapstructure:"http_root"`
TLSCert string `mapstructure:"http_tls_cert"`
TLSKey string `mapstructure:"http_tls_key"`
TLS bool `mapstructure:"http_tls"`
}
// Service defines the available service configuration.
@@ -105,25 +105,25 @@ type Cache struct {
// Config combines all available configuration parts.
type Config struct {
File string
Log Log
Debug Debug
HTTP HTTP
Service Service
Tracing Tracing
Policies []Policy
OIDC OIDC
TokenManager TokenManager
File string `mapstructure:"file"`
Log Log `mapstructure:"log"`
Debug Debug `mapstructure:"debug"`
HTTP HTTP `mapstructure:"http"`
Service Service `mapstructure:"service"`
Tracing Tracing `mapstructure:"tracing"`
Policies []Policy `mapstructure:"policies"`
OIDC OIDC `mapstructure:"oidc"`
TokenManager TokenManager `mapstructure:"token_manager"`
PolicySelector *PolicySelector `mapstructure:"policy_selector"`
Reva Reva
PreSignedURL PreSignedURL
AccountBackend string
UserOIDCClaim string
UserCS3Claim string
MachineAuthAPIKey string
AutoprovisionAccounts bool
EnableBasicAuth bool
InsecureBackends bool
Reva Reva `mapstructure:"reva"`
PreSignedURL PreSignedURL `mapstructure:"pre_signed_url"`
AccountBackend string `mapstructure:"account_backend"`
UserOIDCClaim string `mapstructure:"user_oidc_claim"`
UserCS3Claim string `mapstructure:"user_cs3_claim"`
MachineAuthAPIKey string `mapstructure:"machine_auth_api_key"`
AutoprovisionAccounts bool `mapstructure:"auto_provision_accounts"`
EnableBasicAuth bool `mapstructure:"enable_basic_auth"`
InsecureBackends bool `mapstructure:"insecure_backends"`
Context context.Context
Supervised bool
@@ -132,9 +132,9 @@ 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
Insecure bool
UserinfoCache Cache
Issuer string `mapstructure:"issuer"`
Insecure bool `mapstructure:"insecure"`
UserinfoCache Cache `mapstructure:"user_info_cache"`
}
// PolicySelector is the toplevel-configuration for different selectors
@@ -147,18 +147,18 @@ type PolicySelector struct {
// StaticSelectorConf is the config for the static-policy-selector
type StaticSelectorConf struct {
Policy string
Policy string `mapstructure:"policy"`
}
// TokenManager is the config for using the reva token manager
type TokenManager struct {
JWTSecret string
JWTSecret string `mapstructure:"jwt_secret"`
}
// PreSignedURL is the config for the presigned url middleware
type PreSignedURL struct {
AllowedHTTPMethods []string
Enabled bool
AllowedHTTPMethods []string `mapstructure:"allowed_http_methods"`
Enabled bool `mapstructure:"enabled"`
}
// MigrationSelectorConf is the config for the migration-selector