diff --git a/accounts/cmd/helper/defaultconfig/main.go b/accounts/cmd/helper/defaultconfig/main.go new file mode 100644 index 0000000000..1092497cb7 --- /dev/null +++ b/accounts/cmd/helper/defaultconfig/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + + "github.com/owncloud/ocis/accounts/pkg/config" + "github.com/owncloud/ocis/accounts/pkg/config/parser" + "gopkg.in/yaml.v2" +) + +func main() { + + cfg := config.DefaultConfig() + + parser.EnsureDefaults(cfg) + parser.Sanitize(cfg) + + b, err := yaml.Marshal(cfg) + if err != nil { + return + } + fmt.Println(string(b)) +} diff --git a/accounts/pkg/config/config.go b/accounts/pkg/config/config.go index 79024993be..0152e49911 100644 --- a/accounts/pkg/config/config.go +++ b/accounts/pkg/config/config.go @@ -8,78 +8,79 @@ import ( // Config combines all available configuration parts. type Config struct { - *shared.Commons + *shared.Commons `yaml:"-"` - Service Service + Service Service `yaml:"-"` - Tracing *Tracing `ocisConfig:"tracing"` - Log *Log `ocisConfig:"log"` - Debug Debug `ocisConfig:"debug"` + Tracing *Tracing + Log *Log + Debug Debug - HTTP HTTP `ocisConfig:"http"` - GRPC GRPC `ocisConfig:"grpc"` + HTTP HTTP + GRPC GRPC - TokenManager TokenManager `ocisConfig:"token_manager"` + TokenManager TokenManager - 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"` + Asset Asset + Repo Repo + Index Index + ServiceUser ServiceUser + HashDifficulty int `env:"ACCOUNTS_HASH_DIFFICULTY"` + DemoUsersAndGroups bool `env:"ACCOUNTS_DEMO_USERS_AND_GROUPS"` - Context context.Context + Context context.Context `yaml:"-"` } // Asset defines the available asset configuration. type Asset struct { - Path string `ocisConfig:"path" env:"ACCOUNTS_ASSET_PATH"` + Path string `env:"ACCOUNTS_ASSET_PATH"` } // TokenManager is the config for using the reva token manager type TokenManager struct { - JWTSecret string `ocisConfig:"jwt_secret" env:"OCIS_JWT_SECRET;ACCOUNTS_JWT_SECRET"` + JWTSecret string `env:"OCIS_JWT_SECRET;ACCOUNTS_JWT_SECRET"` } // Repo defines which storage implementation is to be used. type Repo struct { - Backend string `ocisConfig:"backend" env:"ACCOUNTS_STORAGE_BACKEND"` - Disk Disk `ocisConfig:"disk"` - CS3 CS3 `ocisConfig:"cs3"` + Backend string `env:"ACCOUNTS_STORAGE_BACKEND"` + Disk Disk + CS3 CS3 } // Disk is the local disk implementation of the storage. type Disk struct { - Path string `ocisConfig:"path" env:"ACCOUNTS_STORAGE_DISK_PATH"` + Path string `env:"ACCOUNTS_STORAGE_DISK_PATH"` } // CS3 is the cs3 implementation of the storage. type CS3 struct { - ProviderAddr string `ocisConfig:"provider_addr" env:"ACCOUNTS_STORAGE_CS3_PROVIDER_ADDR"` + ProviderAddr string `env:"ACCOUNTS_STORAGE_CS3_PROVIDER_ADDR"` + JWTSecret string `env:"ACCOUNTS_STORAGE_CS3_JWT_SECRET"` } // ServiceUser defines the user required for EOS. type ServiceUser struct { - UUID string `ocisConfig:"uuid" env:"ACCOUNTS_SERVICE_USER_UUID"` - Username string `ocisConfig:"username" env:"ACCOUNTS_SERVICE_USER_USERNAME"` - UID int64 `ocisConfig:"uid" env:"ACCOUNTS_SERVICE_USER_UID"` - GID int64 `ocisConfig:"gid" env:"ACCOUNTS_SERVICE_USER_GID"` + UUID string `env:"ACCOUNTS_SERVICE_USER_UUID"` + Username string `env:"ACCOUNTS_SERVICE_USER_USERNAME"` + UID int64 `env:"ACCOUNTS_SERVICE_USER_UID"` + GID int64 `env:"ACCOUNTS_SERVICE_USER_GID"` } // Index defines config for indexes. type Index struct { - UID UIDBound `ocisConfig:"uid"` - GID GIDBound `ocisConfig:"gid"` + UID UIDBound + GID GIDBound } // GIDBound defines a lower and upper bound. type GIDBound struct { - Lower int64 `ocisConfig:"lower" env:"ACCOUNTS_GID_INDEX_LOWER_BOUND"` - Upper int64 `ocisConfig:"upper" env:"ACCOUNTS_GID_INDEX_UPPER_BOUND"` + Lower int64 `env:"ACCOUNTS_GID_INDEX_LOWER_BOUND"` + Upper int64 `env:"ACCOUNTS_GID_INDEX_UPPER_BOUND"` } // UIDBound defines a lower and upper bound. type UIDBound struct { - Lower int64 `ocisConfig:"lower" env:"ACCOUNTS_UID_INDEX_LOWER_BOUND"` - Upper int64 `ocisConfig:"upper" env:"ACCOUNTS_UID_INDEX_UPPER_BOUND"` + Lower int64 `env:"ACCOUNTS_UID_INDEX_LOWER_BOUND"` + Upper int64 `env:"ACCOUNTS_UID_INDEX_UPPER_BOUND"` } diff --git a/accounts/pkg/config/debug.go b/accounts/pkg/config/debug.go index 539b8fabab..fd90aad7ab 100644 --- a/accounts/pkg/config/debug.go +++ b/accounts/pkg/config/debug.go @@ -2,8 +2,8 @@ package 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"` + Addr string `env:"ACCOUNTS_DEBUG_ADDR"` + Token string `env:"ACCOUNTS_DEBUG_TOKEN"` + Pprof bool `env:"ACCOUNTS_DEBUG_PPROF"` + Zpages bool `env:"ACCOUNTS_DEBUG_ZPAGES"` } diff --git a/accounts/pkg/config/grpc.go b/accounts/pkg/config/grpc.go index f16de42f2b..55e66da118 100644 --- a/accounts/pkg/config/grpc.go +++ b/accounts/pkg/config/grpc.go @@ -2,6 +2,6 @@ package config // GRPC defines the available grpc configuration. type GRPC struct { - Addr string `ocisConfig:"addr" env:"ACCOUNTS_GRPC_ADDR"` + Addr string `env:"ACCOUNTS_GRPC_ADDR"` Namespace string } diff --git a/accounts/pkg/config/http.go b/accounts/pkg/config/http.go index c8c7ab628e..c5c2e4975b 100644 --- a/accounts/pkg/config/http.go +++ b/accounts/pkg/config/http.go @@ -2,17 +2,17 @@ package config // HTTP defines the available http configuration. type HTTP struct { - Addr string `ocisConfig:"addr" env:"ACCOUNTS_HTTP_ADDR"` + Addr string `env:"ACCOUNTS_HTTP_ADDR"` Namespace string - Root string `ocisConfig:"root" env:"ACCOUNTS_HTTP_ROOT"` - CacheTTL int `ocisConfig:"cache_ttl" env:"ACCOUNTS_CACHE_TTL"` - CORS CORS `ocisConfig:"cors"` + Root string `env:"ACCOUNTS_HTTP_ROOT"` + CacheTTL int `env:"ACCOUNTS_CACHE_TTL"` + CORS CORS } // CORS defines the available cors configuration. type CORS struct { - AllowedOrigins []string `ocisConfig:"allowed_origins"` - AllowedMethods []string `ocisConfig:"allowed_methods"` - AllowedHeaders []string `ocisConfig:"allowed_headers"` - AllowCredentials bool `ocisConfig:"allowed_credentials"` + AllowedOrigins []string + AllowedMethods []string + AllowedHeaders []string + AllowCredentials bool } diff --git a/accounts/pkg/config/log.go b/accounts/pkg/config/log.go index 6ada8a7dd6..2d20d3005b 100644 --- a/accounts/pkg/config/log.go +++ b/accounts/pkg/config/log.go @@ -2,8 +2,8 @@ package config // Log defines the available log configuration. type Log struct { - Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;ACCOUNTS_LOG_LEVEL"` - Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;ACCOUNTS_LOG_PRETTY"` - Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;ACCOUNTS_LOG_COLOR"` - File string `mapstructure:"file" env:"OCIS_LOG_FILE;ACCOUNTS_LOG_FILE"` + Level string `env:"OCIS_LOG_LEVEL;ACCOUNTS_LOG_LEVEL"` + Pretty bool `env:"OCIS_LOG_PRETTY;ACCOUNTS_LOG_PRETTY"` + Color bool `env:"OCIS_LOG_COLOR;ACCOUNTS_LOG_COLOR"` + File string `env:"OCIS_LOG_FILE;ACCOUNTS_LOG_FILE"` } diff --git a/accounts/pkg/config/parser/parse.go b/accounts/pkg/config/parser/parse.go index fb801aed28..e437fa6a07 100644 --- a/accounts/pkg/config/parser/parse.go +++ b/accounts/pkg/config/parser/parse.go @@ -17,6 +17,28 @@ func ParseConfig(cfg *config.Config) error { return err } + err = EnsureDefaults(cfg) + if err != nil { + return err + } + + // load all env variables relevant to the config in the current context. + if err := envdecode.Decode(cfg); err != nil { + // no environment variable set for this config is an expected "error" + if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) { + return err + } + } + + err = Sanitize(cfg) + if err != nil { + return err + } + + return nil +} + +func EnsureDefaults(cfg *config.Config) error { // 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 = &config.Log{ @@ -40,19 +62,14 @@ func ParseConfig(cfg *config.Config) error { cfg.Tracing = &config.Tracing{} } - // load all env variables relevant to the config in the current context. - if err := envdecode.Decode(cfg); err != nil { - // no environment variable set for this config is an expected "error" - if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) { - return err - } - } + return nil +} +func Sanitize(cfg *config.Config) error { // sanitize config if cfg.HTTP.Root != "/" { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend) - return nil } diff --git a/accounts/pkg/config/tracing.go b/accounts/pkg/config/tracing.go index fc673f8246..9a72990858 100644 --- a/accounts/pkg/config/tracing.go +++ b/accounts/pkg/config/tracing.go @@ -2,8 +2,8 @@ package config // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;ACCOUNTS_TRACING_ENABLED"` - Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;ACCOUNTS_TRACING_TYPE"` - Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;ACCOUNTS_TRACING_ENDPOINT"` - Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;ACCOUNTS_TRACING_COLLECTOR"` + Enabled bool `env:"OCIS_TRACING_ENABLED;ACCOUNTS_TRACING_ENABLED"` + Type string `env:"OCIS_TRACING_TYPE;ACCOUNTS_TRACING_TYPE"` + Endpoint string `env:"OCIS_TRACING_ENDPOINT;ACCOUNTS_TRACING_ENDPOINT"` + Collector string `env:"OCIS_TRACING_COLLECTOR;ACCOUNTS_TRACING_COLLECTOR"` }