diff --git a/accounts/cmd/accounts/main.go b/accounts/cmd/accounts/main.go index 35262ccad8..12ec59b8b3 100644 --- a/accounts/cmd/accounts/main.go +++ b/accounts/cmd/accounts/main.go @@ -4,11 +4,11 @@ import ( "os" "github.com/owncloud/ocis/accounts/pkg/command" - "github.com/owncloud/ocis/accounts/pkg/config" + defaults "github.com/owncloud/ocis/accounts/pkg/config/defaults" ) func main() { - if err := command.Execute(config.DefaultConfig()); err != nil { + if err := command.Execute(defaults.DefaultConfig()); err != nil { os.Exit(1) } } diff --git a/accounts/cmd/helper/defaultconfig/main.go b/accounts/cmd/helper/defaultconfig/main.go new file mode 100644 index 0000000000..e2b574505c --- /dev/null +++ b/accounts/cmd/helper/defaultconfig/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + + accountsdefaults "github.com/owncloud/ocis/accounts/pkg/config/defaults" + idpdefaults "github.com/owncloud/ocis/idp/pkg/config/defaults" + "gopkg.in/yaml.v2" +) + +func main() { + + fn1 := accountsdefaults.FullDefaultConfig + fn2 := idpdefaults.FullDefaultConfig + + b, err := yaml.Marshal(fn1()) + if err != nil { + return + } + fmt.Println(string(b)) + + b, err = yaml.Marshal(fn2()) + if err != nil { + return + } + fmt.Println(string(b)) +} diff --git a/accounts/pkg/config/defaultconfig.go b/accounts/pkg/config/defaultconfig.go deleted file mode 100644 index 394cf2155f..0000000000 --- a/accounts/pkg/config/defaultconfig.go +++ /dev/null @@ -1,68 +0,0 @@ -package config - -import ( - "path" - - "github.com/owncloud/ocis/ocis-pkg/config/defaults" -) - -func DefaultConfig() *Config { - return &Config{ - Debug: Debug{ - Addr: "127.0.0.1:9182", - Token: "", - Pprof: false, - Zpages: false, - }, - HTTP: HTTP{ - Addr: "127.0.0.1:9181", - Namespace: "com.owncloud.web", - Root: "/", - CacheTTL: 604800, // 7 days - CORS: CORS{ - AllowedOrigins: []string{"*"}, - AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, - AllowedHeaders: []string{"Authorization", "Origin", "Content-Type", "Accept", "X-Requested-With"}, - AllowCredentials: true, - }, - }, - GRPC: GRPC{ - Addr: "127.0.0.1:9180", - Namespace: "com.owncloud.api", - }, - Service: Service{ - Name: "accounts", - }, - Asset: Asset{}, - TokenManager: TokenManager{ - JWTSecret: "Pive-Fumkiu4", - }, - HashDifficulty: 11, - DemoUsersAndGroups: true, - Repo: Repo{ - Backend: "CS3", - Disk: Disk{ - Path: path.Join(defaults.BaseDataPath(), "accounts"), - }, - CS3: CS3{ - ProviderAddr: "localhost:9215", - }, - }, - Index: Index{ - UID: UIDBound{ - Lower: 0, - Upper: 1000, - }, - GID: GIDBound{ - Lower: 0, - Upper: 1000, - }, - }, - ServiceUser: ServiceUser{ - UUID: "95cb8724-03b2-11eb-a0a6-c33ef8ef53ad", - Username: "", - UID: 0, - GID: 0, - }, - } -} diff --git a/accounts/pkg/config/defaults/defaultconfig.go b/accounts/pkg/config/defaults/defaultconfig.go new file mode 100644 index 0000000000..93bf7557cf --- /dev/null +++ b/accounts/pkg/config/defaults/defaultconfig.go @@ -0,0 +1,115 @@ +package defaults + +import ( + "path" + "strings" + + "github.com/owncloud/ocis/accounts/pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/defaults" +) + +func FullDefaultConfig() *config.Config { + cfg := DefaultConfig() + + EnsureDefaults(cfg) + Sanitize(cfg) + + return cfg +} + +func DefaultConfig() *config.Config { + return &config.Config{ + Debug: config.Debug{ + Addr: "127.0.0.1:9182", + Token: "", + Pprof: false, + Zpages: false, + }, + HTTP: config.HTTP{ + Addr: "127.0.0.1:9181", + Namespace: "com.owncloud.web", + Root: "/", + CacheTTL: 604800, // 7 days + CORS: config.CORS{ + AllowedOrigins: []string{"*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Authorization", "Origin", "Content-Type", "Accept", "X-Requested-With"}, + AllowCredentials: true, + }, + }, + GRPC: config.GRPC{ + Addr: "127.0.0.1:9180", + Namespace: "com.owncloud.api", + }, + Service: config.Service{ + Name: "accounts", + }, + Asset: config.Asset{}, + TokenManager: config.TokenManager{ + JWTSecret: "Pive-Fumkiu4", + }, + HashDifficulty: 11, + DemoUsersAndGroups: true, + Repo: config.Repo{ + Backend: "CS3", + Disk: config.Disk{ + Path: path.Join(defaults.BaseDataPath(), "accounts"), + }, + CS3: config.CS3{ + ProviderAddr: "localhost:9215", + }, + }, + Index: config.Index{ + UID: config.UIDBound{ + Lower: 0, + Upper: 1000, + }, + GID: config.GIDBound{ + Lower: 0, + Upper: 1000, + }, + }, + ServiceUser: config.ServiceUser{ + UUID: "95cb8724-03b2-11eb-a0a6-c33ef8ef53ad", + Username: "", + UID: 0, + GID: 0, + }, + } +} + +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{ + 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.Log = &config.Log{} + } + // provide with defaults for shared tracing, since we need a valid destination address for BindEnv. + if cfg.Tracing == nil && cfg.Commons != nil && cfg.Commons.Tracing != nil { + cfg.Tracing = &config.Tracing{ + Enabled: cfg.Commons.Tracing.Enabled, + Type: cfg.Commons.Tracing.Type, + Endpoint: cfg.Commons.Tracing.Endpoint, + Collector: cfg.Commons.Tracing.Collector, + } + } else if cfg.Tracing == nil { + cfg.Tracing = &config.Tracing{} + } + + 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/parser/parse.go b/accounts/pkg/config/parser/parse.go index fb801aed28..a7103a65fb 100644 --- a/accounts/pkg/config/parser/parse.go +++ b/accounts/pkg/config/parser/parse.go @@ -2,11 +2,11 @@ package parser import ( "errors" - "strings" - - ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/accounts/pkg/config" + defaults "github.com/owncloud/ocis/accounts/pkg/config/defaults" + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/config/envdecode" ) @@ -17,27 +17,9 @@ func ParseConfig(cfg *config.Config) error { 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 = &config.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.Log = &config.Log{} - } - // provide with defaults for shared tracing, since we need a valid destination address for BindEnv. - if cfg.Tracing == nil && cfg.Commons != nil && cfg.Commons.Tracing != nil { - cfg.Tracing = &config.Tracing{ - Enabled: cfg.Commons.Tracing.Enabled, - Type: cfg.Commons.Tracing.Type, - Endpoint: cfg.Commons.Tracing.Endpoint, - Collector: cfg.Commons.Tracing.Collector, - } - } else if cfg.Tracing == nil { - cfg.Tracing = &config.Tracing{} + err = defaults.EnsureDefaults(cfg) + if err != nil { + return err } // load all env variables relevant to the config in the current context. @@ -48,11 +30,10 @@ func ParseConfig(cfg *config.Config) error { } } - // sanitize config - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + err = defaults.Sanitize(cfg) + if err != nil { + return err } - cfg.Repo.Backend = strings.ToLower(cfg.Repo.Backend) return nil } diff --git a/accounts/pkg/service/v0/accounts_permission_test.go b/accounts/pkg/service/v0/accounts_permission_test.go index 8c8dbd0786..e349ab6da9 100644 --- a/accounts/pkg/service/v0/accounts_permission_test.go +++ b/accounts/pkg/service/v0/accounts_permission_test.go @@ -13,7 +13,7 @@ import ( accountssvc "github.com/owncloud/ocis/protogen/gen/ocis/services/accounts/v0" "github.com/golang/protobuf/ptypes/empty" - "github.com/owncloud/ocis/accounts/pkg/config" + config "github.com/owncloud/ocis/accounts/pkg/config/defaults" olog "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/roles" diff --git a/idp/cmd/idp/main.go b/idp/cmd/idp/main.go index c60ac2f401..f2faebb068 100644 --- a/idp/cmd/idp/main.go +++ b/idp/cmd/idp/main.go @@ -4,11 +4,11 @@ import ( "os" "github.com/owncloud/ocis/idp/pkg/command" - "github.com/owncloud/ocis/idp/pkg/config" + "github.com/owncloud/ocis/idp/pkg/config/defaults" ) func main() { - if err := command.Execute(config.DefaultConfig()); err != nil { + if err := command.Execute(defaults.DefaultConfig()); err != nil { os.Exit(1) } } diff --git a/idp/pkg/config/defaultconfig.go b/idp/pkg/config/defaults/defaultconfig.go similarity index 61% rename from idp/pkg/config/defaultconfig.go rename to idp/pkg/config/defaults/defaultconfig.go index 823291596b..972f2238d5 100644 --- a/idp/pkg/config/defaultconfig.go +++ b/idp/pkg/config/defaults/defaultconfig.go @@ -1,17 +1,28 @@ -package config +package defaults import ( "path" + "strings" + "github.com/owncloud/ocis/idp/pkg/config" "github.com/owncloud/ocis/ocis-pkg/config/defaults" ) -func DefaultConfig() *Config { - return &Config{ - Debug: Debug{ +func FullDefaultConfig() *config.Config { + cfg := DefaultConfig() + + EnsureDefaults(cfg) + Sanitize(cfg) + + return cfg +} + +func DefaultConfig() *config.Config { + return &config.Config{ + Debug: config.Debug{ Addr: "127.0.0.1:9134", }, - HTTP: HTTP{ + HTTP: config.HTTP{ Addr: "127.0.0.1:9130", Root: "/", Namespace: "com.owncloud.web", @@ -19,11 +30,11 @@ func DefaultConfig() *Config { TLSKey: path.Join(defaults.BaseDataPath(), "idp", "server.key"), TLS: false, }, - Service: Service{ + Service: config.Service{ Name: "idp", }, - Asset: Asset{}, - IDP: Settings{ + Asset: config.Asset{}, + IDP: config.Settings{ Iss: "https://localhost:9200", IdentityManager: "ldap", URIBasePath: "", @@ -56,7 +67,7 @@ func DefaultConfig() *Config { RefreshTokenDurationSeconds: 60 * 60 * 24 * 365 * 3, // 1 year DyamicClientSecretDurationSeconds: 0, }, - Ldap: Ldap{ + Ldap: config.Ldap{ URI: "ldap://localhost:9125", BindDN: "cn=idp,ou=sysusers,dc=ocis,dc=test", BindPassword: "idp", @@ -71,3 +82,38 @@ func DefaultConfig() *Config { }, } } + +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{ + 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.Log = &config.Log{} + } + // provide with defaults for shared tracing, since we need a valid destination address for BindEnv. + if cfg.Tracing == nil && cfg.Commons != nil && cfg.Commons.Tracing != nil { + cfg.Tracing = &config.Tracing{ + Enabled: cfg.Commons.Tracing.Enabled, + Type: cfg.Commons.Tracing.Type, + Endpoint: cfg.Commons.Tracing.Endpoint, + Collector: cfg.Commons.Tracing.Collector, + } + } else if cfg.Tracing == nil { + cfg.Tracing = &config.Tracing{} + } + + return nil +} + +func Sanitize(cfg *config.Config) error { + // sanitize config + if cfg.HTTP.Root != "/" { + cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + } + return nil +} diff --git a/idp/pkg/config/parser/parse.go b/idp/pkg/config/parser/parse.go index d5561337e5..0207c21ed9 100644 --- a/idp/pkg/config/parser/parse.go +++ b/idp/pkg/config/parser/parse.go @@ -2,9 +2,9 @@ package parser import ( "errors" - "strings" "github.com/owncloud/ocis/idp/pkg/config" + "github.com/owncloud/ocis/idp/pkg/config/defaults" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/config/envdecode" @@ -17,27 +17,9 @@ func ParseConfig(cfg *config.Config) error { 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 = &config.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.Log = &config.Log{} - } - // provide with defaults for shared tracing, since we need a valid destination address for BindEnv. - if cfg.Tracing == nil && cfg.Commons != nil && cfg.Commons.Tracing != nil { - cfg.Tracing = &config.Tracing{ - Enabled: cfg.Commons.Tracing.Enabled, - Type: cfg.Commons.Tracing.Type, - Endpoint: cfg.Commons.Tracing.Endpoint, - Collector: cfg.Commons.Tracing.Collector, - } - } else if cfg.Tracing == nil { - cfg.Tracing = &config.Tracing{} + err = defaults.EnsureDefaults(cfg) + if err != nil { + return err } // load all env variables relevant to the config in the current context. @@ -48,9 +30,9 @@ func ParseConfig(cfg *config.Config) error { } } - // sanitize config - if cfg.HTTP.Root != "/" { - cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") + err = defaults.Sanitize(cfg) + if err != nil { + return err } return nil diff --git a/ocis-pkg/config/defaultconfig.go b/ocis-pkg/config/defaultconfig.go index 64e4df1829..a7777f6aee 100644 --- a/ocis-pkg/config/defaultconfig.go +++ b/ocis-pkg/config/defaultconfig.go @@ -1,13 +1,13 @@ package config import ( - accounts "github.com/owncloud/ocis/accounts/pkg/config" + accounts "github.com/owncloud/ocis/accounts/pkg/config/defaults" audit "github.com/owncloud/ocis/audit/pkg/config" glauth "github.com/owncloud/ocis/glauth/pkg/config" graphExplorer "github.com/owncloud/ocis/graph-explorer/pkg/config" graph "github.com/owncloud/ocis/graph/pkg/config" idm "github.com/owncloud/ocis/idm/pkg/config" - idp "github.com/owncloud/ocis/idp/pkg/config" + idp "github.com/owncloud/ocis/idp/pkg/config/defaults" nats "github.com/owncloud/ocis/nats/pkg/config" notifications "github.com/owncloud/ocis/notifications/pkg/config" ocs "github.com/owncloud/ocis/ocs/pkg/config"