diff --git a/ocis-pkg/config/config.go b/ocis-pkg/config/config.go index 74771ce4d..2b18352a6 100644 --- a/ocis-pkg/config/config.go +++ b/ocis-pkg/config/config.go @@ -120,7 +120,7 @@ func New() *Config { IDP: idp.DefaultConfig(), Proxy: proxy.DefaultConfig(), GraphExplorer: graphExplorer.DefaultConfig(), - OCS: ocs.New(), + OCS: ocs.DefaultConfig(), Web: web.New(), Settings: settings.New(), Storage: storage.New(), diff --git a/ocis/pkg/command/ocs.go b/ocis/pkg/command/ocs.go index 12d39be92..9a08c49d9 100644 --- a/ocis/pkg/command/ocs.go +++ b/ocis/pkg/command/ocs.go @@ -9,7 +9,6 @@ import ( "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/ocs/pkg/command" svcconfig "github.com/owncloud/ocis/ocs/pkg/config" - "github.com/owncloud/ocis/ocs/pkg/flagset" "github.com/urfave/cli/v2" ) @@ -19,7 +18,6 @@ func OCSCommand(cfg *config.Config) *cli.Command { Name: "ocs", Usage: "Start ocs server", Category: "Extensions", - Flags: flagset.ServerWithConfig(cfg.OCS), Subcommands: []*cli.Command{ command.PrintVersion(cfg.OCS), }, diff --git a/ocs/cmd/ocs/main.go b/ocs/cmd/ocs/main.go index aac75b441..ca3ea14b3 100644 --- a/ocs/cmd/ocs/main.go +++ b/ocs/cmd/ocs/main.go @@ -8,7 +8,7 @@ import ( ) func main() { - if err := command.Execute(config.New()); err != nil { + if err := command.Execute(config.DefaultConfig()); err != nil { os.Exit(1) } } diff --git a/ocs/pkg/command/server.go b/ocs/pkg/command/server.go index 0e66aa9aa..722673cd1 100644 --- a/ocs/pkg/command/server.go +++ b/ocs/pkg/command/server.go @@ -10,7 +10,6 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/ocs/pkg/config" - "github.com/owncloud/ocis/ocs/pkg/flagset" "github.com/owncloud/ocis/ocs/pkg/metrics" "github.com/owncloud/ocis/ocs/pkg/server/debug" "github.com/owncloud/ocis/ocs/pkg/server/http" @@ -22,26 +21,15 @@ func Server(cfg *config.Config) *cli.Command { return &cli.Command{ Name: "server", Usage: "Start integrated server", - Flags: flagset.ServerWithConfig(cfg), Before: func(ctx *cli.Context) error { - logger := NewLogger(cfg) if cfg.HTTP.Root != "/" { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } - if !cfg.Supervised { - return ParseConfig(ctx, cfg) + if err := ParseConfig(ctx, cfg); err != nil { + return err } - if origins := ctx.StringSlice("cors-allowed-origins"); len(origins) != 0 { - cfg.HTTP.CORS.AllowedOrigins = origins - } - if methods := ctx.StringSlice("cors-allowed-methods"); len(methods) != 0 { - cfg.HTTP.CORS.AllowedMethods = methods - } - if headers := ctx.StringSlice("cors-allowed-headers"); len(headers) != 0 { - cfg.HTTP.CORS.AllowedOrigins = headers - } - logger.Debug().Str("service", "ocs").Msg("ignoring config file parsing when running supervised") + return nil }, Action: func(c *cli.Context) error { diff --git a/ocs/pkg/config/config.go b/ocs/pkg/config/config.go index 6ee0377a4..5098589b5 100644 --- a/ocs/pkg/config/config.go +++ b/ocs/pkg/config/config.go @@ -1,6 +1,12 @@ package config -import "context" +import ( + "context" + "fmt" + "reflect" + + gofig "github.com/gookit/config/v2" +) // Log defines the available logging configuration. type Log struct { @@ -84,3 +90,93 @@ type Config struct { func New() *Config { return &Config{} } + +// DefaultConfig provides default values for a config struct. +func DefaultConfig() *Config { + return &Config{ + Log: Log{}, + Debug: Debug{ + Addr: "127.0.0.1:9114", + Token: "", + Pprof: false, + Zpages: false, + }, + HTTP: HTTP{ + Addr: "127.0.0.1:9110", + Root: "/ocs", + CORS: CORS{ + AllowedOrigins: []string{"*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Authorization", "Origin", "Content-Type", "Accept", "X-Requested-With"}, + AllowCredentials: true, + }, + }, + Tracing: Tracing{ + Enabled: false, + Type: "jaeger", + Endpoint: "", + Collector: "", + Service: "ocs", + }, + TokenManager: TokenManager{ + JWTSecret: "Pive-Fumkiu4", + }, + Service: Service{ + Name: "ocs", + Namespace: "com.owncloud.web", + }, + AccountBackend: "accounts", + RevaAddress: "127.0.0.1:9142", + StorageUsersDriver: "ocis", + MachineAuthAPIKey: "change-me-please", + IdentityManagement: IdentityManagement{ + Address: "https://localhost:9200", + }, + } +} + +// 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 +} + +// UnmapEnv loads values from the gooconf.Config argument and sets them in the expected destination. +func (c *Config) UnmapEnv(gooconf *gofig.Config) error { + vals := structMappings(c) + for i := range vals { + for j := range vals[i].EnvVars { + // we need to guard against v != "" because this is the condition that checks that the value is set from the environment. + // the `ok` guard is not enough, apparently. + if v, ok := gooconf.GetValue(vals[i].EnvVars[j]); ok && v != "" { + + // get the destination type from destination + switch reflect.ValueOf(vals[i].Destination).Type().String() { + case "*bool": + r := gooconf.Bool(vals[i].EnvVars[j]) + *vals[i].Destination.(*bool) = r + case "*string": + r := gooconf.String(vals[i].EnvVars[j]) + *vals[i].Destination.(*string) = r + case "*int": + r := gooconf.Int(vals[i].EnvVars[j]) + *vals[i].Destination.(*int) = r + case "*float64": + // defaults to float64 + r := gooconf.Float(vals[i].EnvVars[j]) + *vals[i].Destination.(*float64) = r + default: + // it is unlikely we will ever get here. Let this serve more as a runtime check for when debugging. + return fmt.Errorf("invalid type for env var: `%v`", vals[i].EnvVars[j]) + } + } + } + } + + return nil +} diff --git a/ocs/pkg/config/env.go b/ocs/pkg/config/env.go new file mode 100644 index 000000000..5ef5ac450 --- /dev/null +++ b/ocs/pkg/config/env.go @@ -0,0 +1,108 @@ +package config + +type mapping struct { + EnvVars []string // name of the EnvVars var. + Destination interface{} // memory address of the original config value to modify. +} + +// structMappings binds a set of environment variables to a destination on cfg. +func structMappings(cfg *Config) []mapping { + return []mapping{ + { + EnvVars: []string{"OCS_LOG_FILE", "OCIS_LOG_FILE"}, + Destination: &cfg.Log.File, + }, + { + EnvVars: []string{"OCS_LOG_LEVEL", "OCIS_LOG_LEVEL"}, + Destination: &cfg.Log.Level, + }, + { + EnvVars: []string{"OCS_LOG_PRETTY", "OCIS_LOG_PRETTY"}, + Destination: &cfg.Log.Pretty, + }, + { + EnvVars: []string{"OCS_LOG_COLOR", "OCIS_LOG_COLOR"}, + Destination: &cfg.Log.Color, + }, + { + EnvVars: []string{"OCS_CONFIG_FILE"}, + Destination: &cfg.File, + }, + { + EnvVars: []string{"OCS_TRACING_ENABLED", "OCIS_TRACING_ENABLED"}, + Destination: &cfg.Tracing.Enabled, + }, + { + EnvVars: []string{"OCS_TRACING_TYPE", "OCIS_TRACING_TYPE"}, + Destination: &cfg.Tracing.Type, + }, + { + EnvVars: []string{"OCS_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"}, + Destination: &cfg.Tracing.Endpoint, + }, + { + EnvVars: []string{"OCS_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"}, + Destination: &cfg.Tracing.Collector, + }, + { + EnvVars: []string{"OCS_TRACING_SERVICE"}, + Destination: &cfg.Tracing.Service, + }, + { + EnvVars: []string{"OCS_DEBUG_ADDR"}, + Destination: &cfg.Debug.Addr, + }, + { + EnvVars: []string{"OCS_DEBUG_TOKEN"}, + Destination: &cfg.Debug.Token, + }, + { + EnvVars: []string{"OCS_DEBUG_PPROF"}, + Destination: &cfg.Debug.Pprof, + }, + { + EnvVars: []string{"OCS_DEBUG_ZPAGES"}, + Destination: &cfg.Debug.Zpages, + }, + { + EnvVars: []string{"OCS_HTTP_ADDR"}, + Destination: &cfg.HTTP.Addr, + }, + { + EnvVars: []string{"OCS_NAMESPACE"}, + Destination: &cfg.Service.Namespace, + }, + { + EnvVars: []string{"OCS_NAME"}, + Destination: &cfg.Service.Name, + }, + { + EnvVars: []string{"OCS_HTTP_ROOT"}, + Destination: &cfg.HTTP.Root, + }, + { + EnvVars: []string{"OCS_JWT_SECRET", "OCIS_JWT_SECRET"}, + Destination: &cfg.TokenManager.JWTSecret, + }, + { + EnvVars: []string{"OCS_ACCOUNT_BACKEND_TYPE"}, + Destination: &cfg.AccountBackend, + }, + { + EnvVars: []string{"REVA_GATEWAY"}, + Destination: &cfg.RevaAddress, + }, + { + EnvVars: []string{"OCS_MACHINE_AUTH_API_KEY", "OCIS_MACHINE_AUTH_API_KEY"}, + Destination: &cfg.MachineAuthAPIKey, + }, + { + EnvVars: []string{"OCS_IDM_ADDRESS", "OCIS_URL"}, + Destination: &cfg.IdentityManagement.Address, + }, + { + EnvVars: []string{"OCS_STORAGE_USERS_DRIVER", "STORAGE_USERS_DRIVER"}, + Destination: &cfg.StorageUsersDriver, + }, + } +} diff --git a/ocs/pkg/flagset/flagset.go b/ocs/pkg/flagset/flagset.go index 378d07eaf..f210b31bc 100644 --- a/ocs/pkg/flagset/flagset.go +++ b/ocs/pkg/flagset/flagset.go @@ -19,204 +19,6 @@ func HealthWithConfig(cfg *config.Config) []cli.Flag { } } -// ServerWithConfig applies cfg to the root flagset -func ServerWithConfig(cfg *config.Config) []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: "log-file", - Usage: "Enable log to file", - EnvVars: []string{"OCS_LOG_FILE", "OCIS_LOG_FILE"}, - Destination: &cfg.Log.File, - }, - &cli.StringFlag{ - Name: "log-level", - Usage: "Set logging level", - EnvVars: []string{"OCS_LOG_LEVEL", "OCIS_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - &cli.BoolFlag{ - Name: "log-pretty", - Usage: "Enable pretty logging", - EnvVars: []string{"OCS_LOG_PRETTY", "OCIS_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - &cli.BoolFlag{ - Name: "log-color", - Usage: "Enable colored logging", - EnvVars: []string{"OCS_LOG_COLOR", "OCIS_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - &cli.StringFlag{ - Name: "config-file", - Value: "", - Usage: "Path to config file", - EnvVars: []string{"OCS_CONFIG_FILE"}, - Destination: &cfg.File, - }, - &cli.BoolFlag{ - Name: "tracing-enabled", - Value: flags.OverrideDefaultBool(cfg.Tracing.Enabled, false), - Usage: "Enable sending traces", - EnvVars: []string{"OCS_TRACING_ENABLED", "OCIS_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - &cli.StringFlag{ - Name: "tracing-type", - Value: flags.OverrideDefaultString(cfg.Tracing.Type, "jaeger"), - Usage: "Tracing backend type", - EnvVars: []string{"OCS_TRACING_TYPE", "OCIS_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - &cli.StringFlag{ - Name: "tracing-endpoint", - Value: flags.OverrideDefaultString(cfg.Tracing.Endpoint, ""), - Usage: "Endpoint for the agent", - EnvVars: []string{"OCS_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - &cli.StringFlag{ - Name: "tracing-collector", - Value: flags.OverrideDefaultString(cfg.Tracing.Collector, ""), - Usage: "Endpoint for the collector", - EnvVars: []string{"OCS_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - &cli.StringFlag{ - Name: "tracing-service", - Value: flags.OverrideDefaultString(cfg.Tracing.Service, "ocs"), - Usage: "Service name for tracing", - EnvVars: []string{"OCS_TRACING_SERVICE"}, - Destination: &cfg.Tracing.Service, - }, - &cli.StringFlag{ - Name: "debug-addr", - Value: flags.OverrideDefaultString(cfg.Debug.Addr, "127.0.0.1:9114"), - Usage: "Address to bind debug server", - EnvVars: []string{"OCS_DEBUG_ADDR"}, - Destination: &cfg.Debug.Addr, - }, - &cli.StringFlag{ - Name: "debug-token", - Value: flags.OverrideDefaultString(cfg.Debug.Token, ""), - Usage: "Token to grant metrics access", - EnvVars: []string{"OCS_DEBUG_TOKEN"}, - Destination: &cfg.Debug.Token, - }, - &cli.BoolFlag{ - Name: "debug-pprof", - Usage: "Enable pprof debugging", - EnvVars: []string{"OCS_DEBUG_PPROF"}, - Destination: &cfg.Debug.Pprof, - }, - &cli.BoolFlag{ - Name: "debug-zpages", - Usage: "Enable zpages debugging", - EnvVars: []string{"OCS_DEBUG_ZPAGES"}, - Destination: &cfg.Debug.Zpages, - }, - &cli.StringFlag{ - Name: "http-addr", - Value: flags.OverrideDefaultString(cfg.HTTP.Addr, "127.0.0.1:9110"), - Usage: "Address to bind http server", - EnvVars: []string{"OCS_HTTP_ADDR"}, - Destination: &cfg.HTTP.Addr, - }, - &cli.StringFlag{ - Name: "http-namespace", - Value: flags.OverrideDefaultString(cfg.Service.Namespace, "com.owncloud.web"), - Usage: "Set the base namespace for the http namespace", - EnvVars: []string{"OCS_NAMESPACE"}, - Destination: &cfg.Service.Namespace, - }, - &cli.StringSliceFlag{ - Name: "cors-allowed-origins", - Value: cli.NewStringSlice("*"), - Usage: "Set the allowed CORS origins", - EnvVars: []string{"OCS_CORS_ALLOW_ORIGINS", "OCIS_CORS_ALLOW_ORIGINS"}, - }, - &cli.StringSliceFlag{ - Name: "cors-allowed-methods", - Value: cli.NewStringSlice("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"), - Usage: "Set the allowed CORS origins", - EnvVars: []string{"OCS_CORS_ALLOW_METHODS", "OCIS_CORS_ALLOW_METHODS"}, - }, - &cli.StringSliceFlag{ - Name: "cors-allowed-headers", - Value: cli.NewStringSlice("Authorization", "Origin", "Content-Type", "Accept", "X-Requested-With"), - Usage: "Set the allowed CORS origins", - EnvVars: []string{"OCS_CORS_ALLOW_HEADERS", "OCIS_CORS_ALLOW_HEADERS"}, - }, - &cli.BoolFlag{ - Name: "cors-allow-credentials", - Value: flags.OverrideDefaultBool(cfg.HTTP.CORS.AllowCredentials, true), - Usage: "Allow credentials for CORS", - EnvVars: []string{"OCS_CORS_ALLOW_CREDENTIALS", "OCIS_CORS_ALLOW_CREDENTIALS"}, - }, - &cli.StringFlag{ - Name: "name", - Value: flags.OverrideDefaultString(cfg.Service.Name, "ocs"), - Usage: "Service name", - EnvVars: []string{"OCS_NAME"}, - Destination: &cfg.Service.Name, - }, - &cli.StringFlag{ - Name: "http-root", - Value: flags.OverrideDefaultString(cfg.HTTP.Root, "/ocs"), - Usage: "Root path of http server", - EnvVars: []string{"OCS_HTTP_ROOT"}, - Destination: &cfg.HTTP.Root, - }, - - &cli.StringFlag{ - Name: "jwt-secret", - Value: flags.OverrideDefaultString(cfg.TokenManager.JWTSecret, "Pive-Fumkiu4"), - Usage: "Used to dismantle the access token, should equal reva's jwt-secret", - EnvVars: []string{"OCS_JWT_SECRET", "OCIS_JWT_SECRET"}, - Destination: &cfg.TokenManager.JWTSecret, - }, - - &cli.StringFlag{ - Name: "account-backend-type", - Value: flags.OverrideDefaultString(cfg.AccountBackend, "accounts"), - Usage: "account-backend-type", - EnvVars: []string{"OCS_ACCOUNT_BACKEND_TYPE"}, - Destination: &cfg.AccountBackend, - }, - &cli.StringFlag{ - Name: "reva-gateway-addr", - Value: flags.OverrideDefaultString(cfg.RevaAddress, "127.0.0.1:9142"), - Usage: "Address of REVA gateway endpoint", - EnvVars: []string{"REVA_GATEWAY"}, - Destination: &cfg.RevaAddress, - }, - &cli.StringFlag{ - Name: "machine-auth-api-key", - Value: flags.OverrideDefaultString(cfg.MachineAuthAPIKey, "change-me-please"), - Usage: "the API key to be used for the machine auth driver in reva", - EnvVars: []string{"OCS_MACHINE_AUTH_API_KEY", "OCIS_MACHINE_AUTH_API_KEY"}, - Destination: &cfg.MachineAuthAPIKey, - }, - &cli.StringFlag{ - Name: "idm-address", - Value: flags.OverrideDefaultString(cfg.IdentityManagement.Address, "https://localhost:9200"), - EnvVars: []string{"OCS_IDM_ADDRESS", "OCIS_URL"}, - Usage: "keeps track of the IDM Address. Needed because of Reva requisite of uniqueness for users", - Destination: &cfg.IdentityManagement.Address, - }, - &cli.StringFlag{ - Name: "users-driver", - Value: flags.OverrideDefaultString(cfg.StorageUsersDriver, "ocis"), - Usage: "storage driver for users mount: eg. local, eos, owncloud, ocis or s3", - EnvVars: []string{"OCS_STORAGE_USERS_DRIVER", "STORAGE_USERS_DRIVER"}, - Destination: &cfg.StorageUsersDriver, - }, - &cli.StringFlag{ - Name: "extensions", - Usage: "Run specific extensions during supervised mode", - }, - } -} - // ListOcsWithConfig applies the config to the list commands flagset. func ListOcsWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{