From ff611633f597a5dc14ed993cfbe9c29a3eada5de Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Wed, 17 Nov 2021 13:19:31 +0100 Subject: [PATCH] Revert "fixed code smells: string literal repetition + cyclomatic complexity" This reverts commit aa4112e2e24f6875bd928e06317b632ea0d90af3. --- ocis-pkg/config/config.go | 2 +- ocis/pkg/runtime/service/service.go | 40 +++++++++---------- proxy/pkg/config/config.go | 51 ++++++++++++------------ proxy/pkg/server/debug/server.go | 6 +-- storage/pkg/config/config.go | 60 +++++++++++++---------------- store/pkg/command/server.go | 25 +++++++++++- web/pkg/config/config.go | 9 ++--- 7 files changed, 98 insertions(+), 95 deletions(-) diff --git a/ocis-pkg/config/config.go b/ocis-pkg/config/config.go index dbd3774be9..a334797fc3 100644 --- a/ocis-pkg/config/config.go +++ b/ocis-pkg/config/config.go @@ -72,7 +72,7 @@ type Runtime struct { type Config struct { *shared.Commons `mapstructure:"shared"` - Mode Mode + Mode Mode // DEPRECATED File string OcisURL string `mapstructure:"ocis_url"` diff --git a/ocis/pkg/runtime/service/service.go b/ocis/pkg/runtime/service/service.go index 10e66ae48c..a725b04bf8 100644 --- a/ocis/pkg/runtime/service/service.go +++ b/ocis/pkg/runtime/service/service.go @@ -124,6 +124,10 @@ func NewService(options ...Option) (*Service, error) { // Start an rpc service. By default the package scope Start will run all default extensions to provide with a working // oCIS instance. func Start(o ...Option) error { + // Start the runtime. Most likely this was called ONLY by the `ocis server` subcommand, but since we cannot protect + // from the caller, the previous statement holds truth. + + // prepare a new rpc Service struct. s, err := NewService(o...) if err != nil { return err @@ -167,7 +171,10 @@ func Start(o ...Option) error { s.cfg.Storage.Log = &shared.Log{} } - propagateLoggingCommonsToStorages(s) + s.cfg.Storage.Log.Color = s.cfg.Commons.Color + s.cfg.Storage.Log.Level = s.cfg.Commons.Level + s.cfg.Storage.Log.Pretty = s.cfg.Commons.Pretty + s.cfg.Storage.Log.File = s.cfg.Commons.File if err = rpc.Register(s); err != nil { if s != nil { @@ -181,7 +188,16 @@ func Start(o ...Option) error { s.Log.Fatal().Err(err) } - defer gracefulRecovery(s) + defer func() { + if r := recover(); r != nil { + reason := strings.Builder{} + if _, err := net.Dial("tcp", net.JoinHostPort(s.cfg.Runtime.Host, s.cfg.Runtime.Port)); err != nil { + reason.WriteString("runtime address already in use") + } + + fmt.Println(reason.String()) + } + }() // prepare the set of services to run s.generateRunSet(s.cfg) @@ -206,26 +222,6 @@ func Start(o ...Option) error { return http.Serve(l, nil) } -func propagateLoggingCommonsToStorages(s *Service) { - s.cfg.Storage.Log.Color = s.cfg.Commons.Color - s.cfg.Storage.Log.Level = s.cfg.Commons.Level - s.cfg.Storage.Log.Pretty = s.cfg.Commons.Pretty - s.cfg.Storage.Log.File = s.cfg.Commons.File -} - -func gracefulRecovery(s *Service) { - func() { - if r := recover(); r != nil { - reason := strings.Builder{} - if _, err := net.Dial("tcp", net.JoinHostPort(s.cfg.Runtime.Host, s.cfg.Runtime.Port)); err != nil { - reason.WriteString("runtime address already in use") - } - - fmt.Println(reason.String()) - } - }() -} - // scheduleServiceTokens adds service tokens to the service supervisor. func scheduleServiceTokens(s *Service, funcSet serviceFuncMap) { for _, name := range runset { diff --git a/proxy/pkg/config/config.go b/proxy/pkg/config/config.go index 1bc71c8e57..18a20ba1da 100644 --- a/proxy/pkg/config/config.go +++ b/proxy/pkg/config/config.go @@ -256,10 +256,6 @@ func DefaultConfig() *Config { } func defaultPolicies() []Policy { - const idpBackend = "http://localhost:9130" - const revaBackend = "http://localhost:9140" - const ingressBackendURL = "https://demo.owncloud.com" - return []Policy{ { Name: "ocis", @@ -270,19 +266,19 @@ func defaultPolicies() []Policy { }, { Endpoint: "/.well-known/", - Backend: idpBackend, + Backend: "http://localhost:9130", }, { Endpoint: "/konnect/", - Backend: idpBackend, + Backend: "http://localhost:9130", }, { Endpoint: "/signin/", - Backend: idpBackend, + Backend: "http://localhost:9130", }, { Endpoint: "/archiver", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Type: RegexRoute, @@ -291,7 +287,7 @@ func defaultPolicies() []Policy { }, { Endpoint: "/ocs/", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Type: QueryRoute, @@ -300,31 +296,31 @@ func defaultPolicies() []Policy { }, { Endpoint: "/remote.php/", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Endpoint: "/dav/", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Endpoint: "/webdav/", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Endpoint: "/status.php", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Endpoint: "/index.php/", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Endpoint: "/data", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Endpoint: "/app/", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Endpoint: "/graph/", @@ -339,6 +335,7 @@ func defaultPolicies() []Policy { Endpoint: "/api/v0/accounts", Backend: "http://localhost:9181", }, + // TODO the lookup needs a better mechanism { Endpoint: "/accounts.js", Backend: "http://localhost:9181", @@ -362,53 +359,53 @@ func defaultPolicies() []Policy { }, { Endpoint: "/.well-known/", - Backend: idpBackend, + Backend: "http://localhost:9130", }, { Endpoint: "/konnect/", - Backend: idpBackend, + Backend: "http://localhost:9130", }, { Endpoint: "/signin/", - Backend: revaBackend, + Backend: "http://localhost:9130", }, { Endpoint: "/archiver", - Backend: revaBackend, + Backend: "http://localhost:9140", }, { Endpoint: "/ocs/", - Backend: ingressBackendURL, + Backend: "https://demo.owncloud.com", ApacheVHost: true, }, { Endpoint: "/remote.php/", - Backend: ingressBackendURL, + Backend: "https://demo.owncloud.com", ApacheVHost: true, }, { Endpoint: "/dav/", - Backend: ingressBackendURL, + Backend: "https://demo.owncloud.com", ApacheVHost: true, }, { Endpoint: "/webdav/", - Backend: ingressBackendURL, + Backend: "https://demo.owncloud.com", ApacheVHost: true, }, { Endpoint: "/status.php", - Backend: ingressBackendURL, + Backend: "https://demo.owncloud.com", ApacheVHost: true, }, { Endpoint: "/index.php/", - Backend: ingressBackendURL, + Backend: "https://demo.owncloud.com", ApacheVHost: true, }, { Endpoint: "/data", - Backend: ingressBackendURL, + Backend: "https://demo.owncloud.com", ApacheVHost: true, }, }, diff --git a/proxy/pkg/server/debug/server.go b/proxy/pkg/server/debug/server.go index 05a0204fdc..b4106f2a8c 100644 --- a/proxy/pkg/server/debug/server.go +++ b/proxy/pkg/server/debug/server.go @@ -27,12 +27,10 @@ func Server(opts ...Option) (*http.Server, error) { ), nil } -const contentTypeHeader = "Content-Type" - // health implements the health check. func health(cfg *config.Config) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set(contentTypeHeader, "text/plain") + w.Header().Set("Content-Type", "text/plain") w.WriteHeader(http.StatusOK) // TODO(tboerger): check if services are up and running @@ -46,7 +44,7 @@ func health(cfg *config.Config) func(http.ResponseWriter, *http.Request) { // ready implements the ready check. func ready(cfg *config.Config) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set(contentTypeHeader, "text/plain") + w.Header().Set("Content-Type", "text/plain") w.WriteHeader(http.StatusOK) // TODO(tboerger): check if services are up and running diff --git a/storage/pkg/config/config.go b/storage/pkg/config/config.go index a03986edfd..3087a4897c 100644 --- a/storage/pkg/config/config.go +++ b/storage/pkg/config/config.go @@ -518,17 +518,9 @@ func New() *Config { return &Config{} } -const ( - defaultLocalIngressURL = "https://localhost:9200" - defaultSharesFolder = "/Shares" - defaultEOSMasterURL = "root://eos-mgm1.eoscluster.cern.ch:1094" - defaultGatewaySVCAddr = "127.0.0.1:9142" - defaultUserLayout = "{{.Id.OpaqueId}}" -) - func DefaultConfig() *Config { - return &Config{ + // log is inherited Debug: Debug{ Addr: "127.0.0.1:9109", }, @@ -538,7 +530,7 @@ func DefaultConfig() *Config { TransferSecret: "replace-me-with-a-transfer-secret", TransferExpires: 24 * 60 * 60, OIDC: OIDC{ - Issuer: defaultLocalIngressURL, + Issuer: "https://localhost:9200", Insecure: false, IDClaim: "preferred_username", }, @@ -559,7 +551,7 @@ func DefaultConfig() *Config { GroupMemberFilter: "(&(objectclass=posixAccount)(ownclouduuid={{.OpaqueId}}*))", BindDN: "cn=reva,ou=sysusers,dc=ocis,dc=test", BindPassword: "reva", - IDP: defaultLocalIngressURL, + IDP: "https://localhost:9200", UserSchema: LDAPUserSchema{ UID: "ownclouduuid", Mail: "mail", @@ -585,7 +577,7 @@ func DefaultConfig() *Config { DBHost: "mysql", DBPort: 3306, DBName: "owncloud", - Idp: defaultLocalIngressURL, + Idp: "https://localhost:9200", Nobody: 90, JoinUsername: false, JoinOwnCloudUUID: false, @@ -604,29 +596,29 @@ func DefaultConfig() *Config { EOS: DriverEOS{ DriverCommon: DriverCommon{ Root: "/eos/dockertest/reva", - ShareFolder: defaultSharesFolder, + ShareFolder: "/Shares", UserLayout: "{{substr 0 1 .Username}}/{{.Username}}", }, ShadowNamespace: "", // Defaults to path.Join(c.Namespace, ".shadow") UploadsNamespace: "", // Defaults to path.Join(c.Namespace, ".uploads") EosBinary: "/usr/bin/eos", XrdcopyBinary: "/usr/bin/xrdcopy", - MasterURL: defaultEOSMasterURL, - SlaveURL: defaultEOSMasterURL, + MasterURL: "root://eos-mgm1.eoscluster.cern.ch:1094", + SlaveURL: "root://eos-mgm1.eoscluster.cern.ch:1094", CacheDirectory: os.TempDir(), - GatewaySVC: defaultGatewaySVCAddr, + GatewaySVC: "127.0.0.1:9142", }, Local: DriverCommon{ Root: path.Join(defaults.BaseDataPath(), "storage", "local", "users"), - ShareFolder: defaultSharesFolder, + ShareFolder: "/Shares", UserLayout: "{{.Username}}", EnableHome: false, }, OwnCloud: DriverOwnCloud{ DriverCommon: DriverCommon{ Root: path.Join(defaults.BaseDataPath(), "storage", "owncloud"), - ShareFolder: defaultSharesFolder, - UserLayout: defaultUserLayout, + ShareFolder: "/Shares", + UserLayout: "{{.Id.OpaqueId}}", EnableHome: false, }, UploadInfoDir: path.Join(defaults.BaseDataPath(), "storage", "uploadinfo"), @@ -636,7 +628,7 @@ func DefaultConfig() *Config { OwnCloudSQL: DriverOwnCloudSQL{ DriverCommon: DriverCommon{ Root: path.Join(defaults.BaseDataPath(), "storage", "owncloud"), - ShareFolder: defaultSharesFolder, + ShareFolder: "/Shares", UserLayout: "{{.Username}}", EnableHome: false, }, @@ -658,8 +650,8 @@ func DefaultConfig() *Config { S3NG: DriverS3NG{ DriverCommon: DriverCommon{ Root: path.Join(defaults.BaseDataPath(), "storage", "users"), - ShareFolder: defaultSharesFolder, - UserLayout: defaultUserLayout, + ShareFolder: "/Shares", + UserLayout: "{{.Id.OpaqueId}}", EnableHome: false, }, Region: "default", @@ -671,8 +663,8 @@ func DefaultConfig() *Config { OCIS: DriverOCIS{ DriverCommon: DriverCommon{ Root: path.Join(defaults.BaseDataPath(), "storage", "users"), - ShareFolder: defaultSharesFolder, - UserLayout: defaultUserLayout, + ShareFolder: "/Shares", + UserLayout: "{{.Id.OpaqueId}}", }, ServiceUserUUID: "95cb8724-03b2-11eb-a0a6-c33ef8ef53ad", }, @@ -681,7 +673,7 @@ func DefaultConfig() *Config { EOS: DriverEOS{ DriverCommon: DriverCommon{ Root: "/eos/dockertest/reva", - ShareFolder: defaultSharesFolder, + ShareFolder: "/Shares", UserLayout: "{{substr 0 1 .Username}}/{{.Username}}", EnableHome: false, }, @@ -689,9 +681,9 @@ func DefaultConfig() *Config { UploadsNamespace: "", EosBinary: "/usr/bin/eos", XrdcopyBinary: "/usr/bin/xrdcopy", - MasterURL: defaultEOSMasterURL, + MasterURL: "root://eos-mgm1.eoscluster.cern.ch:1094", GrpcURI: "", - SlaveURL: defaultEOSMasterURL, + SlaveURL: "root://eos-mgm1.eoscluster.cern.ch:1094", CacheDirectory: os.TempDir(), EnableLogging: false, ShowHiddenSysFiles: false, @@ -700,7 +692,7 @@ func DefaultConfig() *Config { SecProtocol: "", Keytab: "", SingleUsername: "", - GatewaySVC: defaultGatewaySVCAddr, + GatewaySVC: "127.0.0.1:9142", }, Local: DriverCommon{ Root: path.Join(defaults.BaseDataPath(), "storage", "local", "metadata"), @@ -715,7 +707,7 @@ func DefaultConfig() *Config { DriverCommon: DriverCommon{ Root: path.Join(defaults.BaseDataPath(), "storage", "metadata"), ShareFolder: "", - UserLayout: defaultUserLayout, + UserLayout: "{{.Id.OpaqueId}}", EnableHome: false, }, Region: "default", @@ -724,7 +716,7 @@ func DefaultConfig() *Config { DriverCommon: DriverCommon{ Root: path.Join(defaults.BaseDataPath(), "storage", "metadata"), ShareFolder: "", - UserLayout: defaultUserLayout, + UserLayout: "{{.Id.OpaqueId}}", EnableHome: false, }, ServiceUserUUID: "95cb8724-03b2-11eb-a0a6-c33ef8ef53ad", @@ -755,9 +747,9 @@ func DefaultConfig() *Config { OCDavInsecure: false, OCDavPrefix: "", OCSPrefix: "ocs", - OCSSharePrefix: defaultSharesFolder, + OCSSharePrefix: "/Shares", OCSHomeNamespace: "/home", - PublicURL: defaultLocalIngressURL, + PublicURL: "https://localhost:9200", OCSCacheWarmupDriver: "", OCSAdditionalInfoAttribute: "{{.Mail}}", OCSResourceInfoCacheTTL: 0, @@ -769,10 +761,10 @@ func DefaultConfig() *Config { }, Gateway: Gateway{ Port: Port{ - Endpoint: defaultGatewaySVCAddr, + Endpoint: "127.0.0.1:9142", DebugAddr: "127.0.0.1:9143", GRPCNetwork: "tcp", - GRPCAddr: defaultGatewaySVCAddr, + GRPCAddr: "127.0.0.1:9142", }, CommitShareToStorageGrant: true, CommitShareToStorageRef: true, diff --git a/store/pkg/command/server.go b/store/pkg/command/server.go index a8677eb169..6227198742 100644 --- a/store/pkg/command/server.go +++ b/store/pkg/command/server.go @@ -3,6 +3,10 @@ package command import ( "context" + gofig "github.com/gookit/config/v2" + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/shared" + "github.com/owncloud/ocis/store/pkg/tracing" "github.com/owncloud/ocis/ocis-pkg/sync" @@ -21,7 +25,26 @@ func Server(cfg *config.Config) *cli.Command { Name: "server", Usage: "Start integrated server", Before: func(ctx *cli.Context) error { - return ParseConfig(ctx, cfg) + // remember shared logging info to prevent empty overwrites + inLog := cfg.Log + if err := ParseConfig(ctx, cfg); err != nil { + return err + } + + if (cfg.Log == shared.Log{}) && (inLog != shared.Log{}) { + // set the default to the parent config + cfg.Log = inLog + + // and parse the environment + conf := &gofig.Config{} + conf.LoadOSEnv(config.GetEnv(), false) + bindings := config.StructMappings(cfg) + if err := ociscfg.BindEnv(conf, bindings); err != nil { + return err + } + } + + return nil }, Action: func(c *cli.Context) error { logger := NewLogger(cfg) diff --git a/web/pkg/config/config.go b/web/pkg/config/config.go index 194b56366f..9859e91cac 100644 --- a/web/pkg/config/config.go +++ b/web/pkg/config/config.go @@ -6,8 +6,6 @@ import ( "github.com/owncloud/ocis/ocis-pkg/shared" ) -const defaultIngressURL = "https://localhost:9200" - // Debug defines the available debug configuration. type Debug struct { Addr string `mapstructure:"addr"` @@ -108,7 +106,6 @@ func New() *Config { } func DefaultConfig() *Config { - return &Config{ Debug: Debug{ Addr: "127.0.0.1:9104", @@ -134,15 +131,15 @@ func DefaultConfig() *Config { }, Web: Web{ Path: "", - ThemeServer: defaultIngressURL, + ThemeServer: "https://localhost:9200", ThemePath: "/themes/owncloud/theme.json", Config: WebConfig{ - Server: defaultIngressURL, + Server: "https://localhost:9200", Theme: "", Version: "0.1.0", OpenIDConnect: OIDC{ MetadataURL: "", - Authority: defaultIngressURL, + Authority: "https://localhost:9200", ClientID: "web", ResponseType: "code", Scope: "openid profile email",