switch glauth to struct tag based env config

This commit is contained in:
Willy Kloucek
2021-12-16 19:24:07 +01:00
parent d0030ab555
commit 4067ae9493
16 changed files with 120 additions and 247 deletions

View File

@@ -77,12 +77,12 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error {
// load all env variables relevant to the config in the current context.
envCfg := config.Config{}
if err := envdecode.Decode(&envCfg); err != nil {
if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" {
return err
}
// merge environment variable config on top of the current config
if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil && err.Error() != "none of the target fields were set from environment variables" {
if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil {
return err
}

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"github.com/owncloud/ocis/glauth/pkg/config"
"github.com/owncloud/ocis/glauth/pkg/logging"
"github.com/urfave/cli/v2"
)
@@ -17,7 +18,7 @@ func Health(cfg *config.Config) *cli.Command {
return ParseConfig(c, cfg)
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
logger := logging.Configure(cfg.Service.Name, cfg.Log)
resp, err := http.Get(
fmt.Sprintf(

View File

@@ -4,14 +4,13 @@ import (
"context"
"os"
"github.com/imdario/mergo"
"github.com/owncloud/ocis/glauth/pkg/config"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
oclog "github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/shared"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
"github.com/wkloucek/envdecode"
)
// Execute is the entry point for the ocis-glauth command.
@@ -50,34 +49,37 @@ func Execute(cfg *config.Config) error {
return app.Run(os.Args)
}
// NewLogger initializes a service-specific logger instance.
func NewLogger(cfg *config.Config) log.Logger {
return oclog.LoggerFromConfig("glauth", *cfg.Log)
}
// ParseConfig loads glauth configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
conf, err := ociscfg.BindSourcesToStructs("glauth", cfg)
_, err := ociscfg.BindSourcesToStructs("accounts", 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.
conf.LoadOSEnv(config.GetEnv(cfg), false)
bindings := config.StructMappings(cfg)
return ociscfg.BindEnv(conf, bindings)
envCfg := config.Config{}
if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" {
return err
}
// 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 glauth command to be embedded and supervised by a suture supervisor tree.

View File

@@ -7,6 +7,7 @@ import (
"github.com/oklog/run"
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/owncloud/ocis/glauth/pkg/config"
"github.com/owncloud/ocis/glauth/pkg/logging"
"github.com/owncloud/ocis/glauth/pkg/metrics"
"github.com/owncloud/ocis/glauth/pkg/server/debug"
"github.com/owncloud/ocis/glauth/pkg/server/glauth"
@@ -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
}
@@ -47,7 +48,7 @@ func Server(cfg *config.Config) *cli.Command {
defer cancel()
metrics.BuildInfo.WithLabelValues(cfg.Version).Set(1)
metrics.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1)
{

View File

@@ -11,69 +11,88 @@ 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:"GLAUTH_DEBUG_ADDR"`
Token string `ocisConfig:"token" env:"GLAUTH_DEBUG_TOKEN"`
Pprof bool `ocisConfig:"pprof" env:"GLAUTH_DEBUG_PPROF"`
Zpages bool `ocisConfig:"zpages" env:"GLAUTH_DEBUG_ZPAGES"`
}
// 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;GLAUTH_TRACING_ENABLED"`
Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE;GLAUTH_TRACING_TYPE"`
Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT;GLAUTH_TRACING_ENDPOINT"`
Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR;GLAUTH_TRACING_COLLECTOR"`
Service string `ocisConfig:"service" env:"GLAUTH_TRACING_SERVICE"` // TODO:
}
// Log defines the available log configuration.
type Log struct {
Level string `mapstructure:"level" env:"OCIS_LOG_LEVEL;GLAUTH_LOG_LEVEL"`
Pretty bool `mapstructure:"pretty" env:"OCIS_LOG_PRETTY;GLAUTH_LOG_PRETTY"`
Color bool `mapstructure:"color" env:"OCIS_LOG_COLOR;GLAUTH_LOG_COLOR"`
File string `mapstructure:"file" env:"OCIS_LOG_FILE;GLAUTH_LOG_FILE"`
}
// Ldap defined the available LDAP configuration.
type Ldap struct {
Enabled bool `ocisConfig:"enabled"`
Addr string `ocisConfig:"addr"`
Namespace string `ocisConfig:"namespace"`
Enabled bool `ocisConfig:"enabled" env:"GLAUTH_LDAP_ENABLED"`
Addr string `ocisConfig:"addr" env:"GLAUTH_LDAP_ADDR"`
Namespace string
}
// Ldaps defined the available LDAPS configuration.
type Ldaps struct {
Enabled bool `ocisConfig:"enabled"`
Addr string `ocisConfig:"addr"`
Namespace string `ocisConfig:"namespace"`
Cert string `ocisConfig:"cert"`
Key string `ocisConfig:"key"`
Enabled bool `ocisConfig:"enabled" env:"GLAUTH_LDAPS_ENABLED"`
Addr string `ocisConfig:"addr" env:"GLAUTH_LDAPS_ADDR"`
Namespace string
Cert string `ocisConfig:"cert" env:"GLAUTH_LDAPS_CERT"`
Key string `ocisConfig:"key" env:"GLAUTH_LDAPS_KEY"`
}
// Backend defined the available backend configuration.
type Backend struct {
Datastore string `ocisConfig:"datastore"`
BaseDN string `ocisConfig:"base_dn"`
Insecure bool `ocisConfig:"insecure"`
NameFormat string `ocisConfig:"name_format"`
GroupFormat string `ocisConfig:"group_format"`
Servers []string `ocisConfig:"servers"`
SSHKeyAttr string `ocisConfig:"ssh_key_attr"`
UseGraphAPI bool `ocisConfig:"use_graph_api"`
Datastore string `ocisConfig:"datastore" env:"GLAUTH_BACKEND_DATASTORE"`
BaseDN string `ocisConfig:"base_dn" env:"GLAUTH_BACKEND_BASEDN"`
Insecure bool `ocisConfig:"insecure" env:"GLAUTH_BACKEND_INSECURE"`
NameFormat string `ocisConfig:"name_format" env:"GLAUTH_BACKEND_NAME_FORMAT"`
GroupFormat string `ocisConfig:"group_format" env:"GLAUTH_BACKEND_GROUP_FORMAT"`
Servers []string `ocisConfig:"servers"` //TODO: how to configure this via env?
SSHKeyAttr string `ocisConfig:"ssh_key_attr" env:"GLAUTH_BACKEND_SSH_KEY_ATTR"`
UseGraphAPI bool `ocisConfig:"use_graph_api" env:"GLAUTH_BACKEND_USE_GRAPHAPI"`
}
// FallbackBackend defined the available fallback backend configuration.
type FallbackBackend struct {
Datastore string `ocisConfig:"datastore" env:"GLAUTH_FALLBACK_DATASTORE"`
BaseDN string `ocisConfig:"base_dn" env:"GLAUTH_FALLBACK_BASEDN"`
Insecure bool `ocisConfig:"insecure" env:"GLAUTH_FALLBACK_INSECURE"`
NameFormat string `ocisConfig:"name_format" env:"GLAUTH_FALLBACK_NAME_FORMAT"`
GroupFormat string `ocisConfig:"group_format" env:"GLAUTH_FALLBACK_GROUP_FORMAT"`
Servers []string `ocisConfig:"servers"` //TODO: how to configure this via env?
SSHKeyAttr string `ocisConfig:"ssh_key_attr" env:"GLAUTH_FALLBACK_SSH_KEY_ATTR"`
UseGraphAPI bool `ocisConfig:"use_graph_api" env:"GLAUTH_FALLBACK_USE_GRAPHAPI"`
}
// Config combines all available configuration parts.
type Config struct {
*shared.Commons
Log *shared.Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"`
Service Service `ocisConfig:"service"`
Tracing Tracing `ocisConfig:"tracing"`
Ldap Ldap `ocisConfig:"ldap"`
Ldaps Ldaps `ocisConfig:"ldaps"`
Backend Backend `ocisConfig:"backend"`
Fallback Backend `ocisConfig:"fallback"`
Version string `ocisConfig:"version"`
RoleBundleUUID string `ocisConfig:"role_bundle_uuid"`
Log Log `ocisConfig:"log"`
Debug Debug `ocisConfig:"debug"`
Service Service `ocisConfig:"service"`
Tracing Tracing `ocisConfig:"tracing"`
Ldap Ldap `ocisConfig:"ldap"`
Ldaps Ldaps `ocisConfig:"ldaps"`
Backend Backend `ocisConfig:"backend"`
Fallback FallbackBackend `ocisConfig:"fallback"`
RoleBundleUUID string `ocisConfig:"role_bundle_uuid" env:"GLAUTH_ROLE_BUNDLE_ID"`
Context context.Context
Supervised bool
@@ -118,7 +137,7 @@ func DefaultConfig() *Config {
SSHKeyAttr: "sshPublicKey",
UseGraphAPI: true,
},
Fallback: Backend{
Fallback: FallbackBackend{
Datastore: "",
BaseDN: "dc=ocis,dc=test",
Insecure: false,

View File

@@ -1,167 +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{"OCIS_LOG_LEVEL", "GLAUTH_LOG_LEVEL"},
Destination: &cfg.Log.Level,
},
{
EnvVars: []string{"OCIS_LOG_PRETTY", "GLAUTH_LOG_PRETTY"},
Destination: &cfg.Log.Pretty,
},
{
EnvVars: []string{"OCIS_LOG_COLOR", "GLAUTH_LOG_COLOR"},
Destination: &cfg.Log.Color,
},
{
EnvVars: []string{"OCIS_LOG_FILE", "GLAUTH_LOG_FILE"},
Destination: &cfg.Log.File,
},
{
EnvVars: []string{"GLAUTH_CONFIG_FILE"},
Destination: &cfg.File,
},
{
EnvVars: []string{"OCIS_TRACING_ENABLED", "GLAUTH_TRACING_ENABLED"},
Destination: &cfg.Tracing.Enabled,
},
{
EnvVars: []string{"OCIS_TRACING_TYPE", "GLAUTH_TRACING_TYPE"},
Destination: &cfg.Tracing.Type,
},
{
EnvVars: []string{"OCIS_TRACING_ENDPOINT", "GLAUTH_TRACING_ENDPOINT"},
Destination: &cfg.Tracing.Endpoint,
},
{
EnvVars: []string{"OCIS_TRACING_COLLECTOR", "GLAUTH_TRACING_COLLECTOR"},
Destination: &cfg.Tracing.Collector,
},
{
EnvVars: []string{"GLAUTH_TRACING_SERVICE"},
Destination: &cfg.Tracing.Service,
},
{
EnvVars: []string{"GLAUTH_DEBUG_ADDR"},
Destination: &cfg.Debug.Addr,
},
{
EnvVars: []string{"GLAUTH_DEBUG_TOKEN"},
Destination: &cfg.Debug.Token,
},
{
EnvVars: []string{"GLAUTH_DEBUG_PPROF"},
Destination: &cfg.Debug.Pprof,
},
{
EnvVars: []string{"GLAUTH_DEBUG_ZPAGES"},
Destination: &cfg.Debug.Zpages,
},
{
EnvVars: []string{"GLAUTH_ROLE_BUNDLE_ID"},
Destination: &cfg.RoleBundleUUID,
},
{
EnvVars: []string{"GLAUTH_LDAP_ADDR"},
Destination: &cfg.Ldap.Addr,
},
{
EnvVars: []string{"GLAUTH_LDAP_ENABLED"},
Destination: &cfg.Ldap.Enabled,
},
{
EnvVars: []string{"GLAUTH_LDAPS_ADDR"},
Destination: &cfg.Ldaps.Addr,
},
{
EnvVars: []string{"GLAUTH_LDAPS_ENABLED"},
Destination: &cfg.Ldaps.Enabled,
},
{
EnvVars: []string{"GLAUTH_LDAPS_CERT"},
Destination: &cfg.Ldaps.Cert,
},
{
EnvVars: []string{"GLAUTH_LDAPS_KEY"},
Destination: &cfg.Ldaps.Key,
},
{
EnvVars: []string{"GLAUTH_BACKEND_BASEDN"},
Destination: &cfg.Backend.BaseDN,
},
{
EnvVars: []string{"GLAUTH_BACKEND_NAME_FORMAT"},
Destination: &cfg.Backend.NameFormat,
},
{
EnvVars: []string{"GLAUTH_BACKEND_GROUP_FORMAT"},
Destination: &cfg.Backend.GroupFormat,
},
{
EnvVars: []string{"GLAUTH_BACKEND_SSH_KEY_ATTR"},
Destination: &cfg.Backend.SSHKeyAttr,
},
{
EnvVars: []string{"GLAUTH_BACKEND_DATASTORE"},
Destination: &cfg.Backend.Datastore,
},
{
EnvVars: []string{"GLAUTH_BACKEND_INSECURE"},
Destination: &cfg.Backend.Insecure,
},
{
EnvVars: []string{"GLAUTH_BACKEND_USE_GRAPHAPI"},
Destination: &cfg.Backend.UseGraphAPI,
},
{
EnvVars: []string{"GLAUTH_FALLBACK_BASEDN"},
Destination: &cfg.Fallback.BaseDN,
},
{
EnvVars: []string{"GLAUTH_FALLBACK_NAME_FORMAT"},
Destination: &cfg.Fallback.NameFormat,
},
{
EnvVars: []string{"GLAUTH_FALLBACK_GROUP_FORMAT"},
Destination: &cfg.Fallback.GroupFormat,
},
{
EnvVars: []string{"GLAUTH_FALLBACK_SSH_KEY_ATTR"},
Destination: &cfg.Fallback.SSHKeyAttr,
},
{
EnvVars: []string{"GLAUTH_FALLBACK_DATASTORE"},
Destination: &cfg.Fallback.Datastore,
},
{
EnvVars: []string{"GLAUTH_FALLBACK_INSECURE"},
Destination: &cfg.Fallback.Insecure,
},
{
EnvVars: []string{"GLAUTH_FALLBACK_USE_GRAPHAPI"},
Destination: &cfg.Fallback.UseGraphAPI,
},
}
}

View File

@@ -0,0 +1,17 @@
package logging
import (
"github.com/owncloud/ocis/glauth/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

@@ -18,7 +18,7 @@ type Debug struct {
type HTTP struct {
Addr string `ocisConfig:"addr"`
Root string `ocisConfig:"root"`
Namespace string `ocisConfig:"namespace"`
Namespace string
}
// Service defines the available service configuration.

View File

@@ -17,7 +17,7 @@ type Debug struct {
// HTTP defines the available http configuration.
type HTTP struct {
Addr string `ocisConfig:"addr"`
Namespace string `ocisConfig:"namespace"`
Namespace string
Root string `ocisConfig:"root"`
}

View File

@@ -86,12 +86,12 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error {
// load all env variables relevant to the config in the current context.
envCfg := config.Config{}
if err := envdecode.Decode(&envCfg); err != nil {
if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" {
return err
}
// merge environment variable config on top of the current config
if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil && err.Error() != "none of the target fields were set from environment variables" {
if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil {
return err
}

View File

@@ -28,7 +28,7 @@ type Debug struct {
type HTTP struct {
Addr string `ocisConfig:"addr"`
Root string `ocisConfig:"root"`
Namespace string `ocisConfig:"namespace"`
Namespace string
TLSCert string `ocisConfig:"tls_cert"`
TLSKey string `ocisConfig:"tls_key"`
TLS bool `ocisConfig:"tls"`

View File

@@ -28,7 +28,7 @@ type CORS struct {
// HTTP defines the available http configuration.
type HTTP struct {
Addr string `ocisConfig:"addr"`
Namespace string `ocisConfig:"namespace"`
Namespace string
Root string `ocisConfig:"root"`
CacheTTL int `ocisConfig:"cache_ttl"`
CORS CORS `ocisConfig:"cors"`
@@ -37,7 +37,7 @@ type HTTP struct {
// GRPC defines the available grpc configuration.
type GRPC struct {
Addr string `ocisConfig:"addr"`
Namespace string `ocisConfig:"namespace"`
Namespace string
}
// Service provides configuration options for the service

View File

@@ -20,7 +20,7 @@ type Debug struct {
type GRPC struct {
Addr string `ocisConfig:"addr"`
Root string `ocisConfig:"root"`
Namespace string `ocisConfig:"namespace"`
Namespace string
}
// Service defines the available service configuration.

View File

@@ -19,7 +19,7 @@ type Debug struct {
// GRPC defines the available grpc configuration.
type GRPC struct {
Addr string `ocisConfig:"addr"`
Namespace string `ocisConfig:"namespace"`
Namespace string
}
// Service provides configuration options for the service

View File

@@ -18,8 +18,8 @@ type Debug struct {
type HTTP struct {
Addr string `ocisConfig:"addr"`
Root string `ocisConfig:"root"`
Namespace string `ocisConfig:"namespace"`
CacheTTL int `ocisConfig:"cache_ttl"`
Namespace string
CacheTTL int `ocisConfig:"cache_ttl"`
}
// Service defines the available service configuration.

View File

@@ -26,8 +26,8 @@ type CORS struct {
type HTTP struct {
Addr string `ocisConfig:"addr"`
Root string `ocisConfig:"root"`
Namespace string `ocisConfig:"namespace"`
CORS CORS `ocisConfig:"cors"`
Namespace string
CORS CORS `ocisConfig:"cors"`
}
// Service defines the available service configuration.