switch graph to struct tag based env config

This commit is contained in:
Willy Kloucek
2021-12-16 19:40:23 +01:00
committed by Jörn Friedrich Dreyer
parent 788a390016
commit 288d6c469e
8 changed files with 92 additions and 235 deletions

View File

@@ -58,7 +58,7 @@ func Execute(cfg *config.Config) error {
// ParseConfig loads accounts configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
_, err := ociscfg.BindSourcesToStructs("accounts", cfg)
_, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg)
if err != nil {
return err
}

View File

@@ -51,7 +51,7 @@ func Execute(cfg *config.Config) error {
// ParseConfig loads glauth configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
_, err := ociscfg.BindSourcesToStructs("accounts", cfg)
_, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg)
if err != nil {
return err
}

View File

@@ -4,9 +4,9 @@ import (
"context"
"os"
"github.com/owncloud/ocis/ocis-pkg/shared"
"github.com/imdario/mergo"
"github.com/thejerf/suture/v4"
"github.com/wkloucek/envdecode"
"github.com/owncloud/ocis/graph/pkg/config"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
@@ -62,26 +62,35 @@ func NewLogger(cfg *config.Config) log.Logger {
// ParseConfig loads graph configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
conf, err := ociscfg.BindSourcesToStructs("graph", cfg)
_, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg)
if err != nil {
return err
}
// 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 = &shared.Log{
Level: cfg.Commons.Log.Level,
Pretty: cfg.Commons.Log.Pretty,
Color: cfg.Commons.Log.Color,
File: cfg.Commons.Log.File,
}
} else if cfg.Log == nil && cfg.Commons == nil {
cfg.Log = &shared.Log{}
//if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
// cfg.Log = &shared.Log{
// Level: cfg.Commons.Log.Level,
// Pretty: cfg.Commons.Log.Pretty,
// Color: cfg.Commons.Log.Color,
// File: cfg.Commons.Log.File,
// }
//} else if cfg.Log == nil && cfg.Commons == nil {
// cfg.Log = &shared.Log{}
//}
// load all env variables relevant to the config in the current context.
envCfg := config.Config{}
if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" {
return err
}
conf.LoadOSEnv(config.GetEnv(cfg), false)
bindings := config.StructMappings(cfg)
return ociscfg.BindEnv(conf, bindings)
// merge environment variable config on top of the current config
if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil {
return err
}
return nil
}
// SutureService allows for the graph command to be embedded and supervised by a suture supervisor tree.

View File

@@ -6,6 +6,7 @@ import (
"github.com/oklog/run"
"github.com/owncloud/ocis/graph/pkg/config"
"github.com/owncloud/ocis/graph/pkg/logging"
"github.com/owncloud/ocis/graph/pkg/metrics"
"github.com/owncloud/ocis/graph/pkg/server/debug"
"github.com/owncloud/ocis/graph/pkg/server/http"
@@ -30,9 +31,9 @@ func Server(cfg *config.Config) *cli.Command {
return nil
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
if err := tracing.Configure(cfg); err != nil {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
}

View File

@@ -8,72 +8,81 @@ import (
// Debug defines the available debug configuration.
type Debug struct {
Addr string `ocisConfig:"addr"`
Token string `ocisConfig:"token"`
Pprof bool `ocisConfig:"pprof"`
Zpages bool `ocisConfig:"zpages"`
Addr string `ocisConfig:"addr" env:"GRAPH_DEBUG_ADDR"`
Token string `ocisConfig:"token" env:"GRAPH_DEBUG_TOKEN"`
Pprof bool `ocisConfig:"pprof" env:"GRAPH_DEBUG_PPROF"`
Zpages bool `ocisConfig:"zpages" env:"GRAPH_DEBUG_ZPAGES"`
}
// HTTP defines the available http configuration.
type HTTP struct {
Addr string `ocisConfig:"addr"`
Addr string `ocisConfig:"addr" env:"GRAPH_HTTP_ADDR"`
Namespace string
Root string `ocisConfig:"root"`
Root string `ocisConfig:"root" env:"GRAPH_HTTP_ROOT"`
}
// Service defines the available service configuration.
type Service struct {
Name string `ocisConfig:"name"`
Version string `ocisConfig:"version"`
Name string
Version string
}
// Tracing defines the available tracing configuration.
type Tracing struct {
Enabled bool `ocisConfig:"enabled"`
Type string `ocisConfig:"type"`
Endpoint string `ocisConfig:"endpoint"`
Collector string `ocisConfig:"collector"`
Service string `ocisConfig:"service"`
Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED;GRAPH_TRACING_ENABLED"`
Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;GRAPH_TRACING_TYPE"`
Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;GRAPH_TRACING_ENDPOINT"`
Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;GRAPH_TRACING_COLLECTOR"`
Service string `ocisConfig:"service" env:"GRAPH_TRACING_SERVICE"` //TODO: should this be an ID? or the same as Service.Name?
}
// Log defines the available log configuration.
type Log struct {
Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;GRAPH_LOG_LEVEL"`
Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;GRAPH_LOG_PRETTY"`
Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;GRAPH_LOG_COLOR"`
File string `mapstructure:"file" env:"OCIS_LOG_FILE;GRAPH_LOG_FILE"`
}
// Reva defines all available REVA configuration.
type Reva struct {
Address string `ocisConfig:"address"`
Address string `ocisConfig:"address" env:"REVA_GATEWAY"`
}
// TokenManager is the config for using the reva token manager
type TokenManager struct {
JWTSecret string `ocisConfig:"jwt_secret"`
JWTSecret string `ocisConfig:"jwt_secret" env:"OCIS_JWT_SECRET;GRAPH_JWT_SECRET"`
}
type Spaces struct {
WebDavBase string `ocisConfig:"webdav_base"`
WebDavPath string `ocisConfig:"webdav_path"`
DefaultQuota string `ocisConfig:"default_quota"`
WebDavBase string `ocisConfig:"webdav_base" env:"GRAPH_SPACES_WEBDAV_BASE"`
WebDavPath string `ocisConfig:"webdav_path" env:"GRAPH_SPACES_WEBDAV_PATH"`
DefaultQuota string `ocisConfig:"default_quota" env:"GRAPH_SPACES_DEFAULT_QUOTA"`
}
// TODO: do we really need a ldap backend if CS3 also does LDAP!?
type LDAP struct {
URI string `ocisConfig:"uri"`
BindDN string `ocisConfig:"bind_dn"`
BindPassword string `ocisConfig:"bind_password"`
URI string `ocisConfig:"uri" env:"GRAPH_LDAP_URI"`
BindDN string `ocisConfig:"bind_dn" env:"GRAPH_LDAP_BIND_DN"`
BindPassword string `ocisConfig:"bind_password" env:"GRAPH_LDAP_BIND_PASSWORD"`
UserBaseDN string `ocisConfig:"user_base_dn"`
UserSearchScope string `ocisConfig:"user_search_scope"`
UserFilter string `ocisConfig:"user_filter"`
UserEmailAttribute string `ocisConfig:"user_mail_attribute"`
UserDisplayNameAttribute string `ocisConfig:"user_displayname_attribute"`
UserNameAttribute string `ocisConfig:"user_name_attribute"`
UserIDAttribute string `ocisConfig:"user_id_attribute"`
UserBaseDN string `ocisConfig:"user_base_dn" env:"GRAPH_LDAP_USER_BASE_DN"`
UserSearchScope string `ocisConfig:"user_search_scope" env:"GRAPH_LDAP_USER_SCOPE"`
UserFilter string `ocisConfig:"user_filter" env:"GRAPH_LDAP_USER_FILTER"`
UserEmailAttribute string `ocisConfig:"user_mail_attribute" env:"GRAPH_LDAP_USER_EMAIL_ATTRIBUTE"`
UserDisplayNameAttribute string `ocisConfig:"user_displayname_attribute" env:"GRAPH_LDAP_USER_DISPLAYNAME_ATTRIBUTE"`
UserNameAttribute string `ocisConfig:"user_name_attribute" env:"GRAPH_LDAP_USER_NAME_ATTRIBUTE"`
UserIDAttribute string `ocisConfig:"user_id_attribute" env:"GRAPH_LDAP_USER_UID_ATTRIBUTE"`
GroupBaseDN string `ocisConfig:"group_base_dn"`
GroupSearchScope string `ocisConfig:"group_search_scope"`
GroupFilter string `ocisConfig:"group_filter"`
GroupNameAttribute string `ocisConfig:"group_name_attribute"`
GroupIDAttribute string `ocisConfig:"group_id_attribute"`
GroupBaseDN string `ocisConfig:"group_base_dn" env:"GRAPH_LDAP_GROUP_BASE_DN"`
GroupSearchScope string `ocisConfig:"group_search_scope" env:"GRAPH_LDAP_GROUP_SEARCH_SCOPE"`
GroupFilter string `ocisConfig:"group_filter" env:"GRAPH_LDAP_GROUP_FILTER"`
GroupNameAttribute string `ocisConfig:"group_name_attribute" env:"GRAPH_LDAP_GROUP_NAME_ATTRIBUTE"`
GroupIDAttribute string `ocisConfig:"group_id_attribute" env:"GRAPH_LDAP_GROUP_ID_ATTRIBUTE"`
}
type Identity struct {
Backend string `ocisConfig:"backend"`
Backend string `ocisConfig:"backend" env:"GRAPH_IDENTITY_BACKEND"`
LDAP LDAP `ocisConfig:"ldap"`
}
@@ -81,7 +90,7 @@ type Identity struct {
type Config struct {
*shared.Commons
Log *shared.Log `ocisConfig:"log"`
Log Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"`
HTTP HTTP `ocisConfig:"http"`
Service Service `ocisConfig:"service"`

View File

@@ -1,179 +0,0 @@
package config
import "github.com/owncloud/ocis/ocis-pkg/shared"
// 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(cfg *Config) []string {
var r = make([]string, len(structMappings(cfg)))
for i := range structMappings(cfg) {
r = append(r, structMappings(cfg)[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)
}
// structMappings binds a set of environment variables to a destination on cfg.
func structMappings(cfg *Config) []shared.EnvBinding {
return []shared.EnvBinding{
{
EnvVars: []string{"GRAPH_CONFIG_FILE"},
Destination: &cfg.File,
},
{
EnvVars: []string{"OCIS_LOG_LEVEL", "GRAPH_LOG_LEVEL"},
Destination: &cfg.Log.Level,
},
{
EnvVars: []string{"OCIS_LOG_PRETTY", "GRAPH_LOG_PRETTY"},
Destination: &cfg.Log.Pretty,
},
{
EnvVars: []string{"OCIS_LOG_COLOR", "GRAPH_LOG_COLOR"},
Destination: &cfg.Log.Color,
},
{
EnvVars: []string{"OCIS_LOG_FILE", "GRAPH_LOG_FILE"},
Destination: &cfg.Log.File,
},
{
EnvVars: []string{"OCIS_TRACING_ENABLED", "GRAPH_TRACING_ENABLED"},
Destination: &cfg.Tracing.Enabled,
},
{
EnvVars: []string{"OCIS_TRACING_TYPE", "GRAPH_TRACING_TYPE"},
Destination: &cfg.Tracing.Type,
},
{
EnvVars: []string{"OCIS_TRACING_ENDPOINT", "GRAPH_TRACING_ENDPOINT"},
Destination: &cfg.Tracing.Endpoint,
},
{
EnvVars: []string{"OCIS_TRACING_COLLECTOR", "GRAPH_TRACING_COLLECTOR"},
Destination: &cfg.Tracing.Collector,
},
{
EnvVars: []string{"GRAPH_TRACING_SERVICE"},
Destination: &cfg.Tracing.Service,
},
{
EnvVars: []string{"GRAPH_DEBUG_ADDR"},
Destination: &cfg.Debug.Addr,
},
{
EnvVars: []string{"GRAPH_DEBUG_TOKEN"},
Destination: &cfg.Debug.Token,
},
{
EnvVars: []string{"GRAPH_DEBUG_PPROF"},
Destination: &cfg.Debug.Pprof,
},
{
EnvVars: []string{"GRAPH_DEBUG_ZPAGES"},
Destination: &cfg.Debug.Zpages,
},
{
EnvVars: []string{"GRAPH_HTTP_ADDR"},
Destination: &cfg.HTTP.Addr,
},
{
EnvVars: []string{"GRAPH_HTTP_ROOT"},
Destination: &cfg.HTTP.Root,
},
{
EnvVars: []string{"GRAPH_HTTP_NAMESPACE"},
Destination: &cfg.HTTP.Namespace,
},
{
EnvVars: []string{"OCIS_URL", "GRAPH_SPACES_WEBDAV_BASE"},
Destination: &cfg.Spaces.WebDavBase,
},
{
EnvVars: []string{"GRAPH_SPACES_WEBDAV_PATH"},
Destination: &cfg.Spaces.WebDavPath,
},
{
EnvVars: []string{"GRAPH_SPACES_DEFAULT_QUOTA"},
Destination: &cfg.Spaces.DefaultQuota,
},
{
EnvVars: []string{"OCIS_JWT_SECRET", "GRAPH_JWT_SECRET"},
Destination: &cfg.TokenManager.JWTSecret,
},
{
EnvVars: []string{"REVA_GATEWAY"},
Destination: &cfg.Reva.Address,
},
{
EnvVars: []string{"GRAPH_IDENTITY_BACKEND"},
Destination: &cfg.Identity.Backend,
},
{
EnvVars: []string{"GRAPH_LDAP_URI"},
Destination: &cfg.Identity.LDAP.URI,
},
{
EnvVars: []string{"GRAPH_LDAP_BIND_DN"},
Destination: &cfg.Identity.LDAP.BindDN,
},
{
EnvVars: []string{"GRAPH_LDAP_BIND_PASSWORD"},
Destination: &cfg.Identity.LDAP.BindPassword,
},
{
EnvVars: []string{"GRAPH_LDAP_USER_BASE_DN"},
Destination: &cfg.Identity.LDAP.UserBaseDN,
},
{
EnvVars: []string{"GRAPH_LDAP_USER_EMAIL_ATTRIBUTE"},
Destination: &cfg.Identity.LDAP.UserEmailAttribute,
},
{
EnvVars: []string{"GRAPH_LDAP_USER_DISPLAYNAME_ATTRIBUTE"},
Destination: &cfg.Identity.LDAP.UserDisplayNameAttribute,
},
{
EnvVars: []string{"GRAPH_LDAP_USER_NAME_ATTRIBUTE"},
Destination: &cfg.Identity.LDAP.UserNameAttribute,
},
{
EnvVars: []string{"GRAPH_LDAP_USER_UID_ATTRIBUTE"},
Destination: &cfg.Identity.LDAP.UserIDAttribute,
},
{
EnvVars: []string{"GRAPH_LDAP_USER_FILTER"},
Destination: &cfg.Identity.LDAP.UserFilter,
},
{
EnvVars: []string{"GRAPH_LDAP_USER_SCOPE"},
Destination: &cfg.Identity.LDAP.UserSearchScope,
},
{
EnvVars: []string{"GRAPH_LDAP_GROUP_BASE_DN"},
Destination: &cfg.Identity.LDAP.GroupBaseDN,
},
{
EnvVars: []string{"GRAPH_LDAP_GROUP_SEARCH_SCOPE"},
Destination: &cfg.Identity.LDAP.GroupSearchScope,
},
{
EnvVars: []string{"GRAPH_LDAP_GROUP_FILTER"},
Destination: &cfg.Identity.LDAP.GroupFilter,
},
{
EnvVars: []string{"GRAPH_LDAP_GROUP_NAME_ATTRIBUTE"},
Destination: &cfg.Identity.LDAP.GroupNameAttribute,
},
{
EnvVars: []string{"GRAPH_LDAP_GROUP_ID_ATTRIBUTE"},
Destination: &cfg.Identity.LDAP.GroupIDAttribute,
},
}
}

View File

@@ -0,0 +1,17 @@
package logging
import (
"github.com/owncloud/ocis/graph/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
)
// LoggerFromConfig initializes a service-specific logger instance.
func Configure(name string, cfg config.Log) log.Logger {
return log.NewLogger(
log.Name(name),
log.Level(cfg.Level),
log.Pretty(cfg.Pretty),
log.Color(cfg.Color),
log.File(cfg.File),
)
}

View File

@@ -67,7 +67,7 @@ func NewLogger(cfg *config.Config) log.Logger {
// ParseConfig loads idp configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
_, err := ociscfg.BindSourcesToStructs("ocs", cfg)
_, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg)
if err != nil {
return err
}