From b1ace48c723521c7651c27b55f76ef1b2e675f97 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Tue, 12 Jul 2022 12:27:58 +0100 Subject: [PATCH] Update `-normalise` text, add `config.DefaultOpts` for clearer call sites (@kegsay review comments) --- build/gobind-pinecone/monolith.go | 5 +++- build/gobind-yggdrasil/monolith.go | 5 +++- clientapi/routing/register_test.go | 5 +++- cmd/dendrite-demo-pinecone/main.go | 5 +++- cmd/dendrite-demo-yggdrasil/main.go | 5 +++- cmd/generate-config/main.go | 7 +++-- federationapi/federationapi_keys_test.go | 5 +++- federationapi/federationapi_test.go | 5 +++- setup/config/config.go | 35 ++++++++++++++---------- setup/config/config_appservice.go | 8 +++--- setup/config/config_clientapi.go | 4 +-- setup/config/config_federationapi.go | 8 +++--- setup/config/config_global.go | 24 ++++++++-------- setup/config/config_jetstream.go | 4 +-- setup/config/config_keyserver.go | 8 +++--- setup/config/config_mediaapi.go | 8 +++--- setup/config/config_mscs.go | 8 +++--- setup/config/config_roomserver.go | 8 +++--- setup/config/config_syncapi.go | 8 +++--- setup/config/config_userapi.go | 8 +++--- setup/mscs/msc2836/msc2836_test.go | 5 +++- test/testrig/base.go | 25 +++++++++++++---- 22 files changed, 126 insertions(+), 77 deletions(-) diff --git a/build/gobind-pinecone/monolith.go b/build/gobind-pinecone/monolith.go index 02440c01b..f28fd1e07 100644 --- a/build/gobind-pinecone/monolith.go +++ b/build/gobind-pinecone/monolith.go @@ -243,7 +243,10 @@ func (m *DendriteMonolith) Start() { prefix := hex.EncodeToString(pk) cfg := &config.Dendrite{} - cfg.Defaults(true, true) + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk)) cfg.Global.PrivateKey = sk cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID) diff --git a/build/gobind-yggdrasil/monolith.go b/build/gobind-yggdrasil/monolith.go index e17de3160..7af606a4a 100644 --- a/build/gobind-yggdrasil/monolith.go +++ b/build/gobind-yggdrasil/monolith.go @@ -82,7 +82,10 @@ func (m *DendriteMonolith) Start() { m.YggdrasilNode = ygg cfg := &config.Dendrite{} - cfg.Defaults(true, true) + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) cfg.Global.ServerName = gomatrixserverlib.ServerName(ygg.DerivedServerName()) cfg.Global.PrivateKey = ygg.PrivateKey() cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID) diff --git a/clientapi/routing/register_test.go b/clientapi/routing/register_test.go index 8e3113036..85846c7d6 100644 --- a/clientapi/routing/register_test.go +++ b/clientapi/routing/register_test.go @@ -181,7 +181,10 @@ func TestValidationOfApplicationServices(t *testing.T) { // Set up a config fakeConfig := &config.Dendrite{} - fakeConfig.Defaults(true, true) + fakeConfig.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) fakeConfig.Global.ServerName = "localhost" fakeConfig.ClientAPI.Derived.ApplicationServices = []config.ApplicationService{fakeApplicationService} diff --git a/cmd/dendrite-demo-pinecone/main.go b/cmd/dendrite-demo-pinecone/main.go index 3ccefd74a..17fc829e3 100644 --- a/cmd/dendrite-demo-pinecone/main.go +++ b/cmd/dendrite-demo-pinecone/main.go @@ -127,7 +127,10 @@ func main() { }() cfg := &config.Dendrite{} - cfg.Defaults(true, true) + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk)) cfg.Global.PrivateKey = sk cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID) diff --git a/cmd/dendrite-demo-yggdrasil/main.go b/cmd/dendrite-demo-yggdrasil/main.go index 86a853920..ad66572b7 100644 --- a/cmd/dendrite-demo-yggdrasil/main.go +++ b/cmd/dendrite-demo-yggdrasil/main.go @@ -78,7 +78,10 @@ func main() { if configFlagSet { cfg = setup.ParseFlags(true) } else { - cfg.Defaults(true, true) + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) cfg.Global.JetStream.StoragePath = config.Path(fmt.Sprintf("%s/", *instanceName)) cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-account.db", *instanceName)) cfg.MediaAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-mediaapi.db", *instanceName)) diff --git a/cmd/generate-config/main.go b/cmd/generate-config/main.go index eda6356df..9f6ed18f0 100644 --- a/cmd/generate-config/main.go +++ b/cmd/generate-config/main.go @@ -14,7 +14,7 @@ func main() { defaultsForCI := flag.Bool("ci", false, "Populate the configuration with sane defaults for use in CI") serverName := flag.String("server", "", "The domain name of the server if not 'localhost'") dbURI := flag.String("db", "", "The DB URI to use for all components if not SQLite files") - normalise := flag.String("normalise", "", "Normalise a given configuration file") + normalise := flag.String("normalise", "", "Normalise an existing configuration file by adding new/missing options and defaults") polylith := flag.Bool("polylith", false, "Generate a config that makes sense for polylith deployments") flag.Parse() @@ -23,7 +23,10 @@ func main() { cfg = &config.Dendrite{ Version: config.Version, } - cfg.Defaults(true, !*polylith) + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: !*polylith, + }) } else { var err error if cfg, err = config.Load(*normalise, !*polylith); err != nil { diff --git a/federationapi/federationapi_keys_test.go b/federationapi/federationapi_keys_test.go index 9288a7edb..3b1dee890 100644 --- a/federationapi/federationapi_keys_test.go +++ b/federationapi/federationapi_keys_test.go @@ -75,7 +75,10 @@ func TestMain(m *testing.M) { // Draw up just enough Dendrite config for the server key // API to work. cfg := &config.Dendrite{} - cfg.Defaults(true, true) + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) cfg.Global.ServerName = gomatrixserverlib.ServerName(s.name) cfg.Global.PrivateKey = testPriv cfg.Global.JetStream.InMemory = true diff --git a/federationapi/federationapi_test.go b/federationapi/federationapi_test.go index 979aab423..7ce66feb6 100644 --- a/federationapi/federationapi_test.go +++ b/federationapi/federationapi_test.go @@ -252,7 +252,10 @@ func testFederationAPIJoinThenKeyUpdate(t *testing.T, dbType test.DBType) { func TestRoomsV3URLEscapeDoNot404(t *testing.T) { _, privKey, _ := ed25519.GenerateKey(nil) cfg := &config.Dendrite{} - cfg.Defaults(true, true) + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) cfg.Global.KeyID = gomatrixserverlib.KeyID("ed25519:auto") cfg.Global.ServerName = gomatrixserverlib.ServerName("localhost") cfg.Global.PrivateKey = privKey diff --git a/setup/config/config.go b/setup/config/config.go index cfcafc0e5..08b1c9f62 100644 --- a/setup/config/config.go +++ b/setup/config/config.go @@ -211,7 +211,10 @@ func loadConfig( monolithic bool, ) (*Dendrite, error) { var c Dendrite - c.Defaults(false, monolithic) + c.Defaults(DefaultOpts{ + Generate: false, + Monolithic: monolithic, + }) c.IsMonolith = monolithic var err error @@ -292,11 +295,16 @@ func (config *Dendrite) Derive() error { return nil } +type DefaultOpts struct { + Generate bool + Monolithic bool +} + // SetDefaults sets default config values if they are not explicitly set. -func (c *Dendrite) Defaults(generate bool, isMonolith bool) { +func (c *Dendrite) Defaults(opts DefaultOpts) { c.Version = Version - if generate { + if opts.Generate { c.Logging = []LogrusHook{ { Type: "file", @@ -308,17 +316,16 @@ func (c *Dendrite) Defaults(generate bool, isMonolith bool) { } } - c.Global.Defaults(generate, isMonolith) - c.ClientAPI.Defaults(generate, isMonolith) - c.FederationAPI.Defaults(generate, isMonolith) - c.KeyServer.Defaults(generate, isMonolith) - c.MediaAPI.Defaults(generate, isMonolith) - c.RoomServer.Defaults(generate, isMonolith) - c.SyncAPI.Defaults(generate, isMonolith) - c.UserAPI.Defaults(generate, isMonolith) - c.AppServiceAPI.Defaults(generate, isMonolith) - c.MSCs.Defaults(generate, isMonolith) - + c.Global.Defaults(opts) + c.ClientAPI.Defaults(opts) + c.FederationAPI.Defaults(opts) + c.KeyServer.Defaults(opts) + c.MediaAPI.Defaults(opts) + c.RoomServer.Defaults(opts) + c.SyncAPI.Defaults(opts) + c.UserAPI.Defaults(opts) + c.AppServiceAPI.Defaults(opts) + c.MSCs.Defaults(opts) c.Wiring() } diff --git a/setup/config/config_appservice.go b/setup/config/config_appservice.go index cab859e70..52c9f934e 100644 --- a/setup/config/config_appservice.go +++ b/setup/config/config_appservice.go @@ -40,14 +40,14 @@ type AppServiceAPI struct { ConfigFiles []string `yaml:"config_files"` } -func (c *AppServiceAPI) Defaults(generate bool, isMonolith bool) { - if !isMonolith { +func (c *AppServiceAPI) Defaults(opts DefaultOpts) { + if !opts.Monolithic { c.InternalAPI.Listen = "http://localhost:7777" c.InternalAPI.Connect = "http://localhost:7777" c.Database.Defaults(5) } - if generate { - if !isMonolith { + if opts.Generate { + if !opts.Monolithic { c.Database.ConnectionString = "file:appservice.db" } } diff --git a/setup/config/config_clientapi.go b/setup/config/config_clientapi.go index 007dab6bf..56f4b3f92 100644 --- a/setup/config/config_clientapi.go +++ b/setup/config/config_clientapi.go @@ -51,8 +51,8 @@ type ClientAPI struct { MSCs *MSCs `yaml:"-"` } -func (c *ClientAPI) Defaults(generate bool, isMonolith bool) { - if !isMonolith { +func (c *ClientAPI) Defaults(opts DefaultOpts) { + if !opts.Monolithic { c.InternalAPI.Listen = "http://localhost:7771" c.InternalAPI.Connect = "http://localhost:7771" c.ExternalAPI.Listen = "http://[::]:8071" diff --git a/setup/config/config_federationapi.go b/setup/config/config_federationapi.go index 0eab00319..d3a4b2b67 100644 --- a/setup/config/config_federationapi.go +++ b/setup/config/config_federationapi.go @@ -30,8 +30,8 @@ type FederationAPI struct { PreferDirectFetch bool `yaml:"prefer_direct_fetch"` } -func (c *FederationAPI) Defaults(generate bool, isMonolith bool) { - if !isMonolith { +func (c *FederationAPI) Defaults(opts DefaultOpts) { + if !opts.Monolithic { c.InternalAPI.Listen = "http://localhost:7772" c.InternalAPI.Connect = "http://localhost:7772" c.ExternalAPI.Listen = "http://[::]:8072" @@ -39,7 +39,7 @@ func (c *FederationAPI) Defaults(generate bool, isMonolith bool) { } c.FederationMaxRetries = 16 c.DisableTLSValidation = false - if generate { + if opts.Generate { c.KeyPerspectives = KeyPerspectives{ { ServerName: "matrix.org", @@ -55,7 +55,7 @@ func (c *FederationAPI) Defaults(generate bool, isMonolith bool) { }, }, } - if !isMonolith { + if !opts.Monolithic { c.Database.ConnectionString = "file:federationapi.db" } } diff --git a/setup/config/config_global.go b/setup/config/config_global.go index fa2008375..da7ea3dcc 100644 --- a/setup/config/config_global.go +++ b/setup/config/config_global.go @@ -80,8 +80,8 @@ type Global struct { Cache Cache `yaml:"cache"` } -func (c *Global) Defaults(generate bool, isMonolith bool) { - if generate { +func (c *Global) Defaults(opts DefaultOpts) { + if opts.Generate { c.ServerName = "localhost" c.PrivateKeyPath = "matrix_key.pem" _, c.PrivateKey, _ = ed25519.GenerateKey(rand.New(rand.NewSource(0))) @@ -92,16 +92,16 @@ func (c *Global) Defaults(generate bool, isMonolith bool) { } } c.KeyValidityPeriod = time.Hour * 24 * 7 - if isMonolith { + if opts.Monolithic { c.DatabaseOptions.Defaults(90) } - c.JetStream.Defaults(generate) - c.Metrics.Defaults(generate) + c.JetStream.Defaults(opts) + c.Metrics.Defaults(opts) c.DNSCache.Defaults() c.Sentry.Defaults() - c.ServerNotices.Defaults(generate) + c.ServerNotices.Defaults(opts) c.ReportStats.Defaults() - c.Cache.Defaults(generate) + c.Cache.Defaults() } func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) { @@ -145,9 +145,9 @@ type Metrics struct { } `yaml:"basic_auth"` } -func (c *Metrics) Defaults(generate bool) { +func (c *Metrics) Defaults(opts DefaultOpts) { c.Enabled = false - if generate { + if opts.Generate { c.BasicAuth.Username = "metrics" c.BasicAuth.Password = "metrics" } @@ -169,8 +169,8 @@ type ServerNotices struct { RoomName string `yaml:"room_name"` } -func (c *ServerNotices) Defaults(generate bool) { - if generate { +func (c *ServerNotices) Defaults(opts DefaultOpts) { + if opts.Generate { c.Enabled = true c.LocalPart = "_server" c.DisplayName = "Server Alert" @@ -186,7 +186,7 @@ type Cache struct { MaxAge time.Duration `yaml:"max_age"` } -func (c *Cache) Defaults(generate bool) { +func (c *Cache) Defaults() { c.EstimatedMaxSize = 1024 * 1024 * 1024 // 1GB c.MaxAge = time.Hour } diff --git a/setup/config/config_jetstream.go b/setup/config/config_jetstream.go index e4cfd4d3b..adb9b4857 100644 --- a/setup/config/config_jetstream.go +++ b/setup/config/config_jetstream.go @@ -27,10 +27,10 @@ func (c *JetStream) Durable(name string) string { return c.Prefixed(name) } -func (c *JetStream) Defaults(generate bool) { +func (c *JetStream) Defaults(opts DefaultOpts) { c.Addresses = []string{} c.TopicPrefix = "Dendrite" - if generate { + if opts.Generate { c.StoragePath = Path("./") } } diff --git a/setup/config/config_keyserver.go b/setup/config/config_keyserver.go index 2a18950c9..dca9ca9f5 100644 --- a/setup/config/config_keyserver.go +++ b/setup/config/config_keyserver.go @@ -8,14 +8,14 @@ type KeyServer struct { Database DatabaseOptions `yaml:"database,omitempty"` } -func (c *KeyServer) Defaults(generate bool, isMonolith bool) { - if !isMonolith { +func (c *KeyServer) Defaults(opts DefaultOpts) { + if !opts.Monolithic { c.InternalAPI.Listen = "http://localhost:7779" c.InternalAPI.Connect = "http://localhost:7779" c.Database.Defaults(10) } - if generate { - if !isMonolith { + if opts.Generate { + if !opts.Monolithic { c.Database.ConnectionString = "file:keyserver.db" } } diff --git a/setup/config/config_mediaapi.go b/setup/config/config_mediaapi.go index 37c551546..53a8219eb 100644 --- a/setup/config/config_mediaapi.go +++ b/setup/config/config_mediaapi.go @@ -38,8 +38,8 @@ type MediaAPI struct { // DefaultMaxFileSizeBytes defines the default file size allowed in transfers var DefaultMaxFileSizeBytes = FileSizeBytes(10485760) -func (c *MediaAPI) Defaults(generate bool, isMonolith bool) { - if !isMonolith { +func (c *MediaAPI) Defaults(opts DefaultOpts) { + if !opts.Monolithic { c.InternalAPI.Listen = "http://localhost:7774" c.InternalAPI.Connect = "http://localhost:7774" c.ExternalAPI.Listen = "http://[::]:8074" @@ -47,7 +47,7 @@ func (c *MediaAPI) Defaults(generate bool, isMonolith bool) { } c.MaxFileSizeBytes = DefaultMaxFileSizeBytes c.MaxThumbnailGenerators = 10 - if generate { + if opts.Generate { c.ThumbnailSizes = []ThumbnailSize{ { Width: 32, @@ -65,7 +65,7 @@ func (c *MediaAPI) Defaults(generate bool, isMonolith bool) { ResizeMethod: "scale", }, } - if !isMonolith { + if !opts.Monolithic { c.Database.ConnectionString = "file:mediaapi.db" } c.BasePath = "./media_store" diff --git a/setup/config/config_mscs.go b/setup/config/config_mscs.go index 6a2f77a21..6d5ff39a5 100644 --- a/setup/config/config_mscs.go +++ b/setup/config/config_mscs.go @@ -13,12 +13,12 @@ type MSCs struct { Database DatabaseOptions `yaml:"database,omitempty"` } -func (c *MSCs) Defaults(generate bool, isMonolith bool) { - if !isMonolith { +func (c *MSCs) Defaults(opts DefaultOpts) { + if !opts.Monolithic { c.Database.Defaults(5) } - if generate { - if !isMonolith { + if opts.Generate { + if !opts.Monolithic { c.Database.ConnectionString = "file:mscs.db" } } diff --git a/setup/config/config_roomserver.go b/setup/config/config_roomserver.go index c3f9a910d..7da574172 100644 --- a/setup/config/config_roomserver.go +++ b/setup/config/config_roomserver.go @@ -8,14 +8,14 @@ type RoomServer struct { Database DatabaseOptions `yaml:"database,omitempty"` } -func (c *RoomServer) Defaults(generate bool, isMonolith bool) { - if !isMonolith { +func (c *RoomServer) Defaults(opts DefaultOpts) { + if !opts.Monolithic { c.InternalAPI.Listen = "http://localhost:7770" c.InternalAPI.Connect = "http://localhost:7770" c.Database.Defaults(10) } - if generate { - if !isMonolith { + if opts.Generate { + if !opts.Monolithic { c.Database.ConnectionString = "file:roomserver.db" } } diff --git a/setup/config/config_syncapi.go b/setup/config/config_syncapi.go index 91baa341a..342421cc1 100644 --- a/setup/config/config_syncapi.go +++ b/setup/config/config_syncapi.go @@ -11,15 +11,15 @@ type SyncAPI struct { RealIPHeader string `yaml:"real_ip_header"` } -func (c *SyncAPI) Defaults(generate bool, isMonolith bool) { - if !isMonolith { +func (c *SyncAPI) Defaults(opts DefaultOpts) { + if !opts.Monolithic { c.InternalAPI.Listen = "http://localhost:7773" c.InternalAPI.Connect = "http://localhost:7773" c.ExternalAPI.Listen = "http://localhost:8073" c.Database.Defaults(10) } - if generate { - if !isMonolith { + if opts.Generate { + if !opts.Monolithic { c.Database.ConnectionString = "file:syncapi.db" } } diff --git a/setup/config/config_userapi.go b/setup/config/config_userapi.go index ff33571a8..97a6d738b 100644 --- a/setup/config/config_userapi.go +++ b/setup/config/config_userapi.go @@ -23,16 +23,16 @@ type UserAPI struct { const DefaultOpenIDTokenLifetimeMS = 3600000 // 60 minutes -func (c *UserAPI) Defaults(generate bool, isMonolith bool) { - if !isMonolith { +func (c *UserAPI) Defaults(opts DefaultOpts) { + if !opts.Monolithic { c.InternalAPI.Listen = "http://localhost:7781" c.InternalAPI.Connect = "http://localhost:7781" c.AccountDatabase.Defaults(10) } c.BCryptCost = bcrypt.DefaultCost c.OpenIDTokenLifetimeMS = DefaultOpenIDTokenLifetimeMS - if generate { - if !isMonolith { + if opts.Generate { + if !opts.Monolithic { c.AccountDatabase.ConnectionString = "file:userapi_accounts.db" } } diff --git a/setup/mscs/msc2836/msc2836_test.go b/setup/mscs/msc2836/msc2836_test.go index ef4327f5d..52522ccfa 100644 --- a/setup/mscs/msc2836/msc2836_test.go +++ b/setup/mscs/msc2836/msc2836_test.go @@ -544,7 +544,10 @@ func (r *testRoomserverAPI) QueryMembershipForUser(ctx context.Context, req *roo func injectEvents(t *testing.T, userAPI userapi.UserInternalAPI, rsAPI roomserver.RoomserverInternalAPI, events []*gomatrixserverlib.HeaderedEvent) *mux.Router { t.Helper() cfg := &config.Dendrite{} - cfg.Defaults(true, true) + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) cfg.Global.ServerName = "localhost" cfg.MSCs.Database.ConnectionString = "file:msc2836_test.db" cfg.MSCs.MSCs = []string{"msc2836"} diff --git a/test/testrig/base.go b/test/testrig/base.go index 33db1a174..4ab1033c1 100644 --- a/test/testrig/base.go +++ b/test/testrig/base.go @@ -30,13 +30,22 @@ import ( func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, func()) { var cfg config.Dendrite - cfg.Defaults(false, true) + cfg.Defaults(config.DefaultOpts{ + Generate: false, + Monolithic: true, + }) cfg.Global.JetStream.InMemory = true switch dbType { case test.DBTypePostgres: - cfg.Global.Defaults(true, true) // autogen a signing key - cfg.MediaAPI.Defaults(true, true) // autogen a media path + cfg.Global.Defaults(config.DefaultOpts{ // autogen a signing key + Generate: true, + Monolithic: true, + }) + cfg.MediaAPI.Defaults(config.DefaultOpts{ // autogen a media path + Generate: true, + Monolithic: true, + }) // use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use // the file system event with InMemory=true :( cfg.Global.JetStream.TopicPrefix = fmt.Sprintf("Test_%d_", dbType) @@ -49,7 +58,10 @@ func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, f } return base.NewBaseDendrite(&cfg, "Test", base.DisableMetrics), close case test.DBTypeSQLite: - cfg.Defaults(true, true) // sets a sqlite db per component + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) // sets a sqlite db per component // use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use // the file system event with InMemory=true :( cfg.Global.JetStream.TopicPrefix = fmt.Sprintf("Test_%d_", dbType) @@ -82,7 +94,10 @@ func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, f func Base(cfg *config.Dendrite) (*base.BaseDendrite, nats.JetStreamContext, *nats.Conn) { if cfg == nil { cfg = &config.Dendrite{} - cfg.Defaults(true, true) + cfg.Defaults(config.DefaultOpts{ + Generate: true, + Monolithic: true, + }) } cfg.Global.JetStream.InMemory = true base := base.NewBaseDendrite(cfg, "Tests")