hook accounts to env variables

This commit is contained in:
A.Unger
2021-11-04 13:16:16 +01:00
parent e858fa0188
commit b61887aca1
5 changed files with 221 additions and 255 deletions
+9 -2
View File
@@ -66,13 +66,20 @@ func NewLogger(cfg *config.Config) log.Logger {
)
}
// ParseConfig loads accounts configuration from known paths.
// ParseConfig loads proxy configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
_, err := ociscfg.BindSourcesToStructs("accounts", cfg)
conf, err := ociscfg.BindSourcesToStructs("accounts", cfg)
if err != nil {
return err
}
// load all env variables relevant to the config in the current context.
conf.LoadOSEnv(config.GetEnv(), false)
if err = cfg.UnmapEnv(conf); err != nil {
return err
}
return nil
}
-2
View File
@@ -8,7 +8,6 @@ import (
"github.com/oklog/run"
"github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
"github.com/owncloud/ocis/accounts/pkg/metrics"
"github.com/owncloud/ocis/accounts/pkg/server/grpc"
"github.com/owncloud/ocis/accounts/pkg/server/http"
@@ -23,7 +22,6 @@ func Server(cfg *config.Config) *cli.Command {
Name: "server",
Usage: "Start ocis accounts service",
Description: "uses an LDAP server as the storage backend",
Flags: flagset.ServerWithConfig(cfg),
Before: func(ctx *cli.Context) error {
logger := NewLogger(cfg)
if cfg.HTTP.Root != "/" {
+211
View File
@@ -3,7 +3,11 @@ package config
import (
"context"
"fmt"
"path"
"reflect"
gofig "github.com/gookit/config/v2"
"github.com/owncloud/ocis/ocis-pkg/config/defaults"
)
@@ -215,3 +219,210 @@ func DefaultConfig() *Config {
},
}
}
// 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
}
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{"ACCOUNTS_LOG_FILE", "OCIS_LOG_FILE"},
Destination: &cfg.Log.File,
},
{
EnvVars: []string{"ACCOUNTS_TRACING_ENABLED", "OCIS_TRACING_ENABLED"},
Destination: &cfg.Tracing.Enabled,
},
{
EnvVars: []string{"ACCOUNTS_TRACING_TYPE", "OCIS_TRACING_TYPE"},
Destination: &cfg.Tracing.Type,
},
{
EnvVars: []string{"ACCOUNTS_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"},
Destination: &cfg.Tracing.Endpoint,
},
{
EnvVars: []string{"ACCOUNTS_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"},
Destination: &cfg.Tracing.Collector,
},
{
EnvVars: []string{"ACCOUNTS_TRACING_SERVICE"},
Destination: &cfg.Tracing.Service,
},
{
EnvVars: []string{"ACCOUNTS_HTTP_NAMESPACE"},
Destination: &cfg.HTTP.Namespace,
},
{
EnvVars: []string{"ACCOUNTS_HTTP_ADDR"},
Destination: &cfg.HTTP.Addr,
},
{
EnvVars: []string{"ACCOUNTS_HTTP_ROOT"},
Destination: &cfg.HTTP.Root,
},
{
EnvVars: []string{"ACCOUNTS_CACHE_TTL"},
Destination: &cfg.HTTP.CacheTTL,
},
{
EnvVars: []string{"ACCOUNTS_GRPC_NAMESPACE"},
Destination: &cfg.GRPC.Namespace,
},
{
EnvVars: []string{"ACCOUNTS_GRPC_ADDR"},
Destination: &cfg.GRPC.Addr,
},
{
EnvVars: []string{"ACCOUNTS_NAME"},
Destination: &cfg.Server.Name,
},
{
EnvVars: []string{"ACCOUNTS_HASH_DIFFICULTY"},
Destination: &cfg.Server.HashDifficulty,
},
{
EnvVars: []string{"ACCOUNTS_DEMO_USERS_AND_GROUPS"},
Destination: &cfg.Server.DemoUsersAndGroups,
},
{
EnvVars: []string{"ACCOUNTS_ASSET_PATH"},
Destination: &cfg.Asset.Path,
},
{
EnvVars: []string{"ACCOUNTS_JWT_SECRET", "OCIS_JWT_SECRET"},
Destination: &cfg.TokenManager.JWTSecret,
},
{
EnvVars: []string{"ACCOUNTS_STORAGE_BACKEND"},
Destination: &cfg.Repo.Backend,
},
{
EnvVars: []string{"ACCOUNTS_STORAGE_DISK_PATH"},
Destination: &cfg.Repo.Disk.Path,
},
{
EnvVars: []string{"ACCOUNTS_STORAGE_CS3_PROVIDER_ADDR"},
Destination: &cfg.Repo.CS3.ProviderAddr,
},
{
EnvVars: []string{"ACCOUNTS_STORAGE_CS3_DATA_URL"},
Destination: &cfg.Repo.CS3.DataURL,
},
{
EnvVars: []string{"ACCOUNTS_STORAGE_CS3_DATA_PREFIX"},
Destination: &cfg.Repo.CS3.DataPrefix,
},
{
EnvVars: []string{"ACCOUNTS_STORAGE_CS3_JWT_SECRET", "OCIS_JWT_SECRET"},
Destination: &cfg.Repo.CS3.JWTSecret,
},
{
EnvVars: []string{"ACCOUNTS_SERVICE_USER_UUID"},
Destination: &cfg.ServiceUser.UUID,
},
{
EnvVars: []string{"ACCOUNTS_SERVICE_USER_USERNAME"},
Destination: &cfg.ServiceUser.Username,
},
{
EnvVars: []string{"ACCOUNTS_SERVICE_USER_UID"},
Destination: &cfg.ServiceUser.UID,
},
{
EnvVars: []string{"ACCOUNTS_SERVICE_USER_GID"},
Destination: &cfg.ServiceUser.GID,
},
{
EnvVars: []string{"ACCOUNTS_UID_INDEX_LOWER_BOUND"},
Destination: &cfg.Index.UID.Lower,
},
{
EnvVars: []string{"ACCOUNTS_GID_INDEX_LOWER_BOUND"},
Destination: &cfg.Index.GID.Lower,
},
{
EnvVars: []string{"ACCOUNTS_UID_INDEX_UPPER_BOUND"},
Destination: &cfg.Index.UID.Upper,
},
{
EnvVars: []string{"ACCOUNTS_GID_INDEX_UPPER_BOUND"},
Destination: &cfg.Index.GID.Upper,
},
}
}
// TODO(refs) What is with the variables with no destination defined?
//&cli.StringSliceFlag{
//Name: "cors-allowed-origins",
//Value: cli.NewStringSlice("*"),
//Usage: "Set the allowed CORS origins",
//EnvVars: []string{"ACCOUNTS_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{"ACCOUNTS_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{"ACCOUNTS_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{"ACCOUNTS_CORS_ALLOW_CREDENTIALS", "OCIS_CORS_ALLOW_CREDENTIALS"},
//},
-249
View File
@@ -35,255 +35,6 @@ func RootWithConfig(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{"ACCOUNTS_LOG_FILE", "OCIS_LOG_FILE"},
// Destination: &cfg.Log.File,
//},
//&cli.BoolFlag{
// Name: "tracing-enabled",
// Usage: "Enable sending traces",
// EnvVars: []string{"ACCOUNTS_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{"ACCOUNTS_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{"ACCOUNTS_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{"ACCOUNTS_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"},
// Destination: &cfg.Tracing.Collector,
//},
//&cli.StringFlag{
// Name: "tracing-service",
// Value: flags.OverrideDefaultString(cfg.Tracing.Service, "accounts"),
// Usage: "Service name for tracing",
// EnvVars: []string{"ACCOUNTS_TRACING_SERVICE"},
// Destination: &cfg.Tracing.Service,
//},
//&cli.StringFlag{
// Name: "http-namespace",
// Value: flags.OverrideDefaultString(cfg.HTTP.Namespace, "com.owncloud.web"),
// Usage: "Set the base namespace for the http namespace",
// EnvVars: []string{"ACCOUNTS_HTTP_NAMESPACE"},
// Destination: &cfg.HTTP.Namespace,
//},
//&cli.StringFlag{
// Name: "http-addr",
// Value: flags.OverrideDefaultString(cfg.HTTP.Addr, "127.0.0.1:9181"),
// Usage: "Address to bind http server",
// EnvVars: []string{"ACCOUNTS_HTTP_ADDR"},
// Destination: &cfg.HTTP.Addr,
//},
//&cli.StringFlag{
// Name: "http-root",
// Value: flags.OverrideDefaultString(cfg.HTTP.Root, "/"),
// Usage: "Root path of http server",
// EnvVars: []string{"ACCOUNTS_HTTP_ROOT"},
// Destination: &cfg.HTTP.Root,
//},
//&cli.IntFlag{
// Name: "http-cache-ttl",
// Value: flags.OverrideDefaultInt(cfg.HTTP.CacheTTL, 604800),
// Usage: "Set the static assets caching duration in seconds",
// EnvVars: []string{"ACCOUNTS_CACHE_TTL"},
// Destination: &cfg.HTTP.CacheTTL,
//},
//&cli.StringSliceFlag{
// Name: "cors-allowed-origins",
// Value: cli.NewStringSlice("*"),
// Usage: "Set the allowed CORS origins",
// EnvVars: []string{"ACCOUNTS_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{"ACCOUNTS_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{"ACCOUNTS_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{"ACCOUNTS_CORS_ALLOW_CREDENTIALS", "OCIS_CORS_ALLOW_CREDENTIALS"},
//},
//&cli.StringFlag{
// Name: "grpc-namespace",
// Value: flags.OverrideDefaultString(cfg.GRPC.Namespace, "com.owncloud.api"),
// Usage: "Set the base namespace for the grpc namespace",
// EnvVars: []string{"ACCOUNTS_GRPC_NAMESPACE"},
// Destination: &cfg.GRPC.Namespace,
//},
//&cli.StringFlag{
// Name: "grpc-addr",
// Value: flags.OverrideDefaultString(cfg.GRPC.Addr, "127.0.0.1:9180"),
// Usage: "Address to bind grpc server",
// EnvVars: []string{"ACCOUNTS_GRPC_ADDR"},
// Destination: &cfg.GRPC.Addr,
//},
//&cli.StringFlag{
// Name: "name",
// Value: flags.OverrideDefaultString(cfg.Server.Name, "accounts"),
// Usage: "service name",
// EnvVars: []string{"ACCOUNTS_NAME"},
// Destination: &cfg.Server.Name,
//},
//&cli.IntFlag{
// Name: "accounts-hash-difficulty",
// Value: flags.OverrideDefaultInt(cfg.Server.HashDifficulty, 11),
// Usage: "accounts password hash difficulty",
// EnvVars: []string{"ACCOUNTS_HASH_DIFFICULTY"},
// Destination: &cfg.Server.HashDifficulty,
//},
//&cli.BoolFlag{
// Name: "demo-users-and-groups",
// Value: flags.OverrideDefaultBool(cfg.Server.DemoUsersAndGroups, true),
// Usage: "Enable demo users and groups",
// EnvVars: []string{"ACCOUNTS_DEMO_USERS_AND_GROUPS"},
// Destination: &cfg.Server.DemoUsersAndGroups,
//},
//&cli.StringFlag{
// Name: "asset-path",
// Value: flags.OverrideDefaultString(cfg.Asset.Path, ""),
// Usage: "Path to custom assets",
// EnvVars: []string{"ACCOUNTS_ASSET_PATH"},
// Destination: &cfg.Asset.Path,
//},
//&cli.StringFlag{
// Name: "jwt-secret",
// Value: flags.OverrideDefaultString(cfg.TokenManager.JWTSecret, "Pive-Fumkiu4"),
// Usage: "Used to create JWT to talk to reva, should equal reva's jwt-secret",
// EnvVars: []string{"ACCOUNTS_JWT_SECRET", "OCIS_JWT_SECRET"},
// Destination: &cfg.TokenManager.JWTSecret,
//},
//&cli.StringFlag{
// Name: "storage-backend",
// Value: flags.OverrideDefaultString(cfg.Repo.Backend, "CS3"),
// Usage: "Which backend to use to store accounts data (CS3 or disk)",
// EnvVars: []string{"ACCOUNTS_STORAGE_BACKEND"},
// Destination: &cfg.Repo.Backend,
//},
//&cli.StringFlag{
// Name: "storage-disk-path",
// Value: flags.OverrideDefaultString(cfg.Repo.Disk.Path, path.Join(defaults.BaseDataPath(), "accounts")),
// Usage: "Path on the local disk to store accounts data when backend is set to disk",
// EnvVars: []string{"ACCOUNTS_STORAGE_DISK_PATH"},
// Destination: &cfg.Repo.Disk.Path,
//},
//&cli.StringFlag{
// Name: "storage-cs3-provider-addr",
// Value: flags.OverrideDefaultString(cfg.Repo.CS3.ProviderAddr, "localhost:9215"),
// Usage: "bind address for the metadata storage provider",
// EnvVars: []string{"ACCOUNTS_STORAGE_CS3_PROVIDER_ADDR"},
// Destination: &cfg.Repo.CS3.ProviderAddr,
//},
//&cli.StringFlag{
// Name: "storage-cs3-data-url",
// Value: flags.OverrideDefaultString(cfg.Repo.CS3.DataURL, "http://localhost:9216"),
// Usage: "http endpoint of the metadata storage",
// EnvVars: []string{"ACCOUNTS_STORAGE_CS3_DATA_URL"},
// Destination: &cfg.Repo.CS3.DataURL,
//},
//&cli.StringFlag{
// Name: "storage-cs3-data-prefix",
// Value: flags.OverrideDefaultString(cfg.Repo.CS3.DataPrefix, "data"),
// Usage: "path prefix for the http endpoint of the metadata storage, without leading slash",
// EnvVars: []string{"ACCOUNTS_STORAGE_CS3_DATA_PREFIX"},
// Destination: &cfg.Repo.CS3.DataPrefix,
//},
//&cli.StringFlag{
// Name: "storage-cs3-jwt-secret",
// Value: flags.OverrideDefaultString(cfg.Repo.CS3.JWTSecret, "Pive-Fumkiu4"),
// Usage: "Used to create JWT to talk to reva, should equal reva's jwt-secret",
// EnvVars: []string{"ACCOUNTS_STORAGE_CS3_JWT_SECRET", "OCIS_JWT_SECRET"},
// Destination: &cfg.Repo.CS3.JWTSecret,
//},
//&cli.StringFlag{
// Name: "service-user-uuid",
// Value: flags.OverrideDefaultString(cfg.ServiceUser.UUID, "95cb8724-03b2-11eb-a0a6-c33ef8ef53ad"),
// Usage: "uuid of the internal service user (required on EOS)",
// EnvVars: []string{"ACCOUNTS_SERVICE_USER_UUID"},
// Destination: &cfg.ServiceUser.UUID,
//},
//&cli.StringFlag{
// Name: "service-user-username",
// Value: flags.OverrideDefaultString(cfg.ServiceUser.Username, ""),
// Usage: "username of the internal service user (required on EOS)",
// EnvVars: []string{"ACCOUNTS_SERVICE_USER_USERNAME"},
// Destination: &cfg.ServiceUser.Username,
//},
//&cli.Int64Flag{
// Name: "service-user-uid",
// Value: flags.OverrideDefaultInt64(cfg.ServiceUser.UID, 0),
// Usage: "uid of the internal service user (required on EOS)",
// EnvVars: []string{"ACCOUNTS_SERVICE_USER_UID"},
// Destination: &cfg.ServiceUser.UID,
//},
//&cli.Int64Flag{
// Name: "service-user-gid",
// Value: flags.OverrideDefaultInt64(cfg.ServiceUser.GID, 0),
// Usage: "gid of the internal service user (required on EOS)",
// EnvVars: []string{"ACCOUNTS_SERVICE_USER_GID"},
// Destination: &cfg.ServiceUser.GID,
//},
//&cli.Int64Flag{
// Name: "uid-index-lower-bound",
// Value: flags.OverrideDefaultInt64(cfg.Index.UID.Lower, 0),
// Usage: "define a starting point for the account UID",
// EnvVars: []string{"ACCOUNTS_UID_INDEX_LOWER_BOUND"},
// Destination: &cfg.Index.UID.Lower,
//},
//&cli.Int64Flag{
// Name: "gid-index-lower-bound",
// Value: flags.OverrideDefaultInt64(cfg.Index.GID.Lower, 1000),
// Usage: "define a starting point for the account GID",
// EnvVars: []string{"ACCOUNTS_GID_INDEX_LOWER_BOUND"},
// Destination: &cfg.Index.GID.Lower,
//},
//&cli.Int64Flag{
// Name: "uid-index-upper-bound",
// Value: flags.OverrideDefaultInt64(cfg.Index.UID.Upper, 0),
// Usage: "define an ending point for the account UID",
// EnvVars: []string{"ACCOUNTS_UID_INDEX_UPPER_BOUND"},
// Destination: &cfg.Index.UID.Upper,
//},
//&cli.Int64Flag{
// Name: "gid-index-upper-bound",
// Value: flags.OverrideDefaultInt64(cfg.Index.GID.Upper, 1000),
// Usage: "define an ending point for the account GID",
// EnvVars: []string{"ACCOUNTS_GID_INDEX_UPPER_BOUND"},
// Destination: &cfg.Index.GID.Upper,
//},
//&cli.StringFlag{
// Name: "extensions",
// Usage: "Run specific extensions during supervised mode",
//},
}
}
// UpdateAccountWithConfig applies update command flags to cfg
func UpdateAccountWithConfig(cfg *config.Config, a *accounts.Account) []cli.Flag {
if a.PasswordProfile == nil {
+1 -2
View File
@@ -6,7 +6,6 @@ package command
import (
"github.com/owncloud/ocis/accounts/pkg/command"
svcconfig "github.com/owncloud/ocis/accounts/pkg/config"
"github.com/owncloud/ocis/accounts/pkg/flagset"
"github.com/owncloud/ocis/ocis-pkg/config"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/owncloud/ocis/ocis/pkg/register"
@@ -19,7 +18,7 @@ func AccountsCommand(cfg *config.Config) *cli.Command {
Name: "accounts",
Usage: "Start accounts server",
Category: "Extensions",
Flags: flagset.ServerWithConfig(cfg.Accounts),
//Flags: flagset.ServerWithConfig(cfg.Accounts),
Subcommands: []*cli.Command{
command.ListAccounts(cfg.Accounts),
command.AddAccount(cfg.Accounts),