0
0
Fork 0
mirror of https://github.com/matrix-org/dendrite synced 2024-06-01 03:59:03 +02:00

Configuration tweaks (#2567)

This makes the following changes:

* The various `Defaults` functions are now responsible for setting sane defaults if `generate` is specified, rather than hiding them in `generate-config`
* Some configuration options have been marked as `omitempty` so that they don't appear in generated configs unnecessarily (monolith-specific vs. polylith-specific options)
* A new option `-polylith` has been added to `generate-config` to create a config that makes sense for polylith deployments (i.e. including the internal/external API listeners and per-component database sections)
* A new option `-normalise` has been added to `generate-config` to take an existing file and add any missing options and/or defaults
This commit is contained in:
Neil Alexander 2022-09-01 14:15:41 +01:00 committed by GitHub
parent ad6b902b84
commit 51d229b025
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 322 additions and 212 deletions

View file

@ -243,7 +243,10 @@ func (m *DendriteMonolith) Start() {
prefix := hex.EncodeToString(pk) prefix := hex.EncodeToString(pk)
cfg := &config.Dendrite{} cfg := &config.Dendrite{}
cfg.Defaults(true) cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk)) cfg.Global.ServerName = gomatrixserverlib.ServerName(hex.EncodeToString(pk))
cfg.Global.PrivateKey = sk cfg.Global.PrivateKey = sk
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID) cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)

View file

@ -82,7 +82,10 @@ func (m *DendriteMonolith) Start() {
m.YggdrasilNode = ygg m.YggdrasilNode = ygg
cfg := &config.Dendrite{} cfg := &config.Dendrite{}
cfg.Defaults(true) cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = gomatrixserverlib.ServerName(ygg.DerivedServerName()) cfg.Global.ServerName = gomatrixserverlib.ServerName(ygg.DerivedServerName())
cfg.Global.PrivateKey = ygg.PrivateKey() cfg.Global.PrivateKey = ygg.PrivateKey()
cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID) cfg.Global.KeyID = gomatrixserverlib.KeyID(signing.KeyID)

View file

@ -46,9 +46,8 @@ EXPOSE 8008 8448
# At runtime, generate TLS cert based on the CA now mounted at /ca # At runtime, generate TLS cert based on the CA now mounted at /ca
# At runtime, replace the SERVER_NAME with what we are told # At runtime, replace the SERVER_NAME with what we are told
CMD /build/run_postgres.sh && ./generate-keys --keysize 1024 --server $SERVER_NAME --tls-cert server.crt --tls-key server.key --tls-authority-cert /complement/ca/ca.crt --tls-authority-key /complement/ca/ca.key && \ CMD /build/run_postgres.sh && ./generate-keys --keysize 1024 --server $SERVER_NAME --tls-cert server.crt --tls-key server.key --tls-authority-cert /complement/ca/ca.crt --tls-authority-key /complement/ca/ca.key && \
./generate-config -server $SERVER_NAME --ci > dendrite.yaml && \ ./generate-config -server $SERVER_NAME --ci --db postgresql://postgres@localhost/postgres?sslmode=disable > dendrite.yaml && \
# Replace the connection string with a single postgres DB, using user/db = 'postgres' and no password, bump max_conns # Bump max_open_conns up here in the global database config
sed -i "s%connection_string:.*$%connection_string: postgresql://postgres@localhost/postgres?sslmode=disable%g" dendrite.yaml && \ sed -i 's/max_open_conns:.*$/max_open_conns: 1990/g' dendrite.yaml && \
sed -i 's/max_open_conns:.*$/max_open_conns: 100/g' dendrite.yaml && \
cp /complement/ca/ca.crt /usr/local/share/ca-certificates/ && update-ca-certificates && \ cp /complement/ca/ca.crt /usr/local/share/ca-certificates/ && update-ca-certificates && \
exec ./dendrite-monolith-server --really-enable-open-registration --tls-cert server.crt --tls-key server.key --config dendrite.yaml -api=${API:-0} exec ./dendrite-monolith-server --really-enable-open-registration --tls-cert server.crt --tls-key server.key --config dendrite.yaml -api=${API:-0}

View file

@ -181,7 +181,10 @@ func TestValidationOfApplicationServices(t *testing.T) {
// Set up a config // Set up a config
fakeConfig := &config.Dendrite{} fakeConfig := &config.Dendrite{}
fakeConfig.Defaults(true) fakeConfig.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
fakeConfig.Global.ServerName = "localhost" fakeConfig.Global.ServerName = "localhost"
fakeConfig.ClientAPI.Derived.ApplicationServices = []config.ApplicationService{fakeApplicationService} fakeConfig.ClientAPI.Derived.ApplicationServices = []config.ApplicationService{fakeApplicationService}

View file

@ -115,7 +115,10 @@ func main() {
panic("failed to load PEM key: " + err.Error()) panic("failed to load PEM key: " + err.Error())
} }
} }
cfg.Defaults(true) cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.PrivateKey = sk cfg.Global.PrivateKey = sk
cfg.Global.JetStream.StoragePath = config.Path(fmt.Sprintf("%s/", *instanceName)) cfg.Global.JetStream.StoragePath = config.Path(fmt.Sprintf("%s/", *instanceName))
cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-account.db", *instanceName)) cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-account.db", *instanceName))

View file

@ -78,7 +78,10 @@ func main() {
if configFlagSet { if configFlagSet {
cfg = setup.ParseFlags(true) cfg = setup.ParseFlags(true)
} else { } else {
cfg.Defaults(true) cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.JetStream.StoragePath = config.Path(fmt.Sprintf("%s/", *instanceName)) cfg.Global.JetStream.StoragePath = config.Path(fmt.Sprintf("%s/", *instanceName))
cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-account.db", *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)) cfg.MediaAPI.Database.ConnectionString = config.DataSource(fmt.Sprintf("file:%s-mediaapi.db", *instanceName))

View file

@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"path/filepath"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -11,90 +12,81 @@ import (
) )
func main() { func main() {
defaultsForCI := flag.Bool("ci", false, "sane defaults for CI testing") 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'") 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") dbURI := flag.String("db", "", "The DB URI to use for all components (PostgreSQL only)")
dirPath := flag.String("dir", "./", "The folder to use for paths (like SQLite databases, media storage)")
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() flag.Parse()
cfg := &config.Dendrite{ var cfg *config.Dendrite
Version: config.Version, if *normalise == "" {
} cfg = &config.Dendrite{
cfg.Defaults(true) Version: config.Version,
if *serverName != "" { }
cfg.Global.ServerName = gomatrixserverlib.ServerName(*serverName) cfg.Defaults(config.DefaultOpts{
} Generate: true,
if *dbURI != "" { Monolithic: !*polylith,
cfg.FederationAPI.Database.ConnectionString = config.DataSource(*dbURI) })
cfg.KeyServer.Database.ConnectionString = config.DataSource(*dbURI) if *serverName != "" {
cfg.MSCs.Database.ConnectionString = config.DataSource(*dbURI) cfg.Global.ServerName = gomatrixserverlib.ServerName(*serverName)
cfg.MediaAPI.Database.ConnectionString = config.DataSource(*dbURI) }
cfg.RoomServer.Database.ConnectionString = config.DataSource(*dbURI) uri := config.DataSource(*dbURI)
cfg.SyncAPI.Database.ConnectionString = config.DataSource(*dbURI) if *polylith || uri.IsSQLite() || uri == "" {
cfg.UserAPI.AccountDatabase.ConnectionString = config.DataSource(*dbURI) for name, db := range map[string]*config.DatabaseOptions{
} "federationapi": &cfg.FederationAPI.Database,
cfg.Global.TrustedIDServers = []string{ "keyserver": &cfg.KeyServer.Database,
"matrix.org", "mscs": &cfg.MSCs.Database,
"vector.im", "mediaapi": &cfg.MediaAPI.Database,
} "roomserver": &cfg.RoomServer.Database,
cfg.Logging = []config.LogrusHook{ "syncapi": &cfg.SyncAPI.Database,
{ "userapi": &cfg.UserAPI.AccountDatabase,
Type: "file", } {
Level: "info", if uri == "" {
Params: map[string]interface{}{ path := filepath.Join(*dirPath, fmt.Sprintf("dendrite_%s.db", name))
"path": "/var/log/dendrite", db.ConnectionString = config.DataSource(fmt.Sprintf("file:%s", path))
}, } else {
}, db.ConnectionString = uri
} }
cfg.FederationAPI.KeyPerspectives = config.KeyPerspectives{ }
{ } else {
ServerName: "matrix.org", cfg.Global.DatabaseOptions.ConnectionString = uri
Keys: []config.KeyPerspectiveTrustKey{ }
{ cfg.Logging = []config.LogrusHook{
KeyID: "ed25519:auto", {
PublicKey: "Noi6WqcDj0QmPxCNQqgezwTlBKrfqehY1u2FyWP9uYw", Type: "file",
}, Level: "info",
{ Params: map[string]interface{}{
KeyID: "ed25519:a_RXGa", "path": filepath.Join(*dirPath, "log"),
PublicKey: "l8Hft5qXKn1vfHrg3p4+W8gELQVo8N13JkluMfmn2sQ",
}, },
}, },
}, }
} if *defaultsForCI {
cfg.MediaAPI.ThumbnailSizes = []config.ThumbnailSize{ cfg.AppServiceAPI.DisableTLSValidation = true
{ cfg.ClientAPI.RateLimiting.Enabled = false
Width: 32, cfg.FederationAPI.DisableTLSValidation = false
Height: 32, // don't hit matrix.org when running tests!!!
ResizeMethod: "crop", cfg.FederationAPI.KeyPerspectives = config.KeyPerspectives{}
}, cfg.MediaAPI.BasePath = config.Path(filepath.Join(*dirPath, "media"))
{ cfg.MSCs.MSCs = []string{"msc2836", "msc2946", "msc2444", "msc2753"}
Width: 96, cfg.Logging[0].Level = "trace"
Height: 96, cfg.Logging[0].Type = "std"
ResizeMethod: "crop", cfg.UserAPI.BCryptCost = bcrypt.MinCost
}, cfg.Global.JetStream.InMemory = true
{ cfg.Global.JetStream.StoragePath = config.Path(*dirPath)
Width: 640, cfg.ClientAPI.RegistrationDisabled = false
Height: 480, cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled = true
ResizeMethod: "scale", cfg.ClientAPI.RegistrationSharedSecret = "complement"
}, cfg.Global.Presence = config.PresenceOptions{
} EnableInbound: true,
EnableOutbound: true,
if *defaultsForCI { }
cfg.AppServiceAPI.DisableTLSValidation = true }
cfg.ClientAPI.RateLimiting.Enabled = false } else {
cfg.FederationAPI.DisableTLSValidation = false var err error
// don't hit matrix.org when running tests!!! if cfg, err = config.Load(*normalise, !*polylith); err != nil {
cfg.FederationAPI.KeyPerspectives = config.KeyPerspectives{} panic(err)
cfg.MSCs.MSCs = []string{"msc2836", "msc2946", "msc2444", "msc2753"}
cfg.Logging[0].Level = "trace"
cfg.Logging[0].Type = "std"
cfg.UserAPI.BCryptCost = bcrypt.MinCost
cfg.Global.JetStream.InMemory = true
cfg.ClientAPI.RegistrationDisabled = false
cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled = true
cfg.ClientAPI.RegistrationSharedSecret = "complement"
cfg.Global.Presence = config.PresenceOptions{
EnableInbound: true,
EnableOutbound: true,
} }
} }

View file

@ -75,7 +75,10 @@ func TestMain(m *testing.M) {
// Draw up just enough Dendrite config for the server key // Draw up just enough Dendrite config for the server key
// API to work. // API to work.
cfg := &config.Dendrite{} cfg := &config.Dendrite{}
cfg.Defaults(true) cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = gomatrixserverlib.ServerName(s.name) cfg.Global.ServerName = gomatrixserverlib.ServerName(s.name)
cfg.Global.PrivateKey = testPriv cfg.Global.PrivateKey = testPriv
cfg.Global.JetStream.InMemory = true cfg.Global.JetStream.InMemory = true

View file

@ -263,7 +263,10 @@ func testFederationAPIJoinThenKeyUpdate(t *testing.T, dbType test.DBType) {
func TestRoomsV3URLEscapeDoNot404(t *testing.T) { func TestRoomsV3URLEscapeDoNot404(t *testing.T) {
_, privKey, _ := ed25519.GenerateKey(nil) _, privKey, _ := ed25519.GenerateKey(nil)
cfg := &config.Dendrite{} cfg := &config.Dendrite{}
cfg.Defaults(true) cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.KeyID = gomatrixserverlib.KeyID("ed25519:auto") cfg.Global.KeyID = gomatrixserverlib.KeyID("ed25519:auto")
cfg.Global.ServerName = gomatrixserverlib.ServerName("localhost") cfg.Global.ServerName = gomatrixserverlib.ServerName("localhost")
cfg.Global.PrivateKey = privKey cfg.Global.PrivateKey = privKey

View file

@ -211,7 +211,10 @@ func loadConfig(
monolithic bool, monolithic bool,
) (*Dendrite, error) { ) (*Dendrite, error) {
var c Dendrite var c Dendrite
c.Defaults(false) c.Defaults(DefaultOpts{
Generate: false,
Monolithic: monolithic,
})
c.IsMonolith = monolithic c.IsMonolith = monolithic
var err error var err error
@ -295,21 +298,25 @@ func (config *Dendrite) Derive() error {
return nil return nil
} }
type DefaultOpts struct {
Generate bool
Monolithic bool
}
// SetDefaults sets default config values if they are not explicitly set. // SetDefaults sets default config values if they are not explicitly set.
func (c *Dendrite) Defaults(generate bool) { func (c *Dendrite) Defaults(opts DefaultOpts) {
c.Version = Version c.Version = Version
c.Global.Defaults(generate) c.Global.Defaults(opts)
c.ClientAPI.Defaults(generate) c.ClientAPI.Defaults(opts)
c.FederationAPI.Defaults(generate) c.FederationAPI.Defaults(opts)
c.KeyServer.Defaults(generate) c.KeyServer.Defaults(opts)
c.MediaAPI.Defaults(generate) c.MediaAPI.Defaults(opts)
c.RoomServer.Defaults(generate) c.RoomServer.Defaults(opts)
c.SyncAPI.Defaults(generate) c.SyncAPI.Defaults(opts)
c.UserAPI.Defaults(generate) c.UserAPI.Defaults(opts)
c.AppServiceAPI.Defaults(generate) c.AppServiceAPI.Defaults(opts)
c.MSCs.Defaults(generate) c.MSCs.Defaults(opts)
c.Wiring() c.Wiring()
} }

View file

@ -29,7 +29,7 @@ type AppServiceAPI struct {
Matrix *Global `yaml:"-"` Matrix *Global `yaml:"-"`
Derived *Derived `yaml:"-"` // TODO: Nuke Derived from orbit Derived *Derived `yaml:"-"` // TODO: Nuke Derived from orbit
InternalAPI InternalAPIOptions `yaml:"internal_api"` InternalAPI InternalAPIOptions `yaml:"internal_api,omitempty"`
// DisableTLSValidation disables the validation of X.509 TLS certs // DisableTLSValidation disables the validation of X.509 TLS certs
// on appservice endpoints. This is not recommended in production! // on appservice endpoints. This is not recommended in production!
@ -38,9 +38,11 @@ type AppServiceAPI struct {
ConfigFiles []string `yaml:"config_files"` ConfigFiles []string `yaml:"config_files"`
} }
func (c *AppServiceAPI) Defaults(generate bool) { func (c *AppServiceAPI) Defaults(opts DefaultOpts) {
c.InternalAPI.Listen = "http://localhost:7777" if !opts.Monolithic {
c.InternalAPI.Connect = "http://localhost:7777" c.InternalAPI.Listen = "http://localhost:7777"
c.InternalAPI.Connect = "http://localhost:7777"
}
} }
func (c *AppServiceAPI) Verify(configErrs *ConfigErrors, isMonolith bool) { func (c *AppServiceAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {

View file

@ -9,8 +9,8 @@ type ClientAPI struct {
Matrix *Global `yaml:"-"` Matrix *Global `yaml:"-"`
Derived *Derived `yaml:"-"` // TODO: Nuke Derived from orbit Derived *Derived `yaml:"-"` // TODO: Nuke Derived from orbit
InternalAPI InternalAPIOptions `yaml:"internal_api"` InternalAPI InternalAPIOptions `yaml:"internal_api,omitempty"`
ExternalAPI ExternalAPIOptions `yaml:"external_api"` ExternalAPI ExternalAPIOptions `yaml:"external_api,omitempty"`
// If set disables new users from registering (except via shared // If set disables new users from registering (except via shared
// secrets) // secrets)
@ -48,13 +48,15 @@ type ClientAPI struct {
// Rate-limiting options // Rate-limiting options
RateLimiting RateLimiting `yaml:"rate_limiting"` RateLimiting RateLimiting `yaml:"rate_limiting"`
MSCs *MSCs `yaml:"mscs"` MSCs *MSCs `yaml:"-"`
} }
func (c *ClientAPI) Defaults(generate bool) { func (c *ClientAPI) Defaults(opts DefaultOpts) {
c.InternalAPI.Listen = "http://localhost:7771" if !opts.Monolithic {
c.InternalAPI.Connect = "http://localhost:7771" c.InternalAPI.Listen = "http://localhost:7771"
c.ExternalAPI.Listen = "http://[::]:8071" c.InternalAPI.Connect = "http://localhost:7771"
c.ExternalAPI.Listen = "http://[::]:8071"
}
c.RegistrationSharedSecret = "" c.RegistrationSharedSecret = ""
c.RecaptchaPublicKey = "" c.RecaptchaPublicKey = ""
c.RecaptchaPrivateKey = "" c.RecaptchaPrivateKey = ""

View file

@ -5,12 +5,12 @@ import "github.com/matrix-org/gomatrixserverlib"
type FederationAPI struct { type FederationAPI struct {
Matrix *Global `yaml:"-"` Matrix *Global `yaml:"-"`
InternalAPI InternalAPIOptions `yaml:"internal_api"` InternalAPI InternalAPIOptions `yaml:"internal_api,omitempty"`
ExternalAPI ExternalAPIOptions `yaml:"external_api"` ExternalAPI ExternalAPIOptions `yaml:"external_api,omitempty"`
// The database stores information used by the federation destination queues to // The database stores information used by the federation destination queues to
// send transactions to remote servers. // send transactions to remote servers.
Database DatabaseOptions `yaml:"database"` Database DatabaseOptions `yaml:"database,omitempty"`
// Federation failure threshold. How many consecutive failures that we should // Federation failure threshold. How many consecutive failures that we should
// tolerate when sending federation requests to a specific server. The backoff // tolerate when sending federation requests to a specific server. The backoff
@ -30,25 +30,44 @@ type FederationAPI struct {
PreferDirectFetch bool `yaml:"prefer_direct_fetch"` PreferDirectFetch bool `yaml:"prefer_direct_fetch"`
} }
func (c *FederationAPI) Defaults(generate bool) { func (c *FederationAPI) Defaults(opts DefaultOpts) {
c.InternalAPI.Listen = "http://localhost:7772" if !opts.Monolithic {
c.InternalAPI.Connect = "http://localhost:7772" c.InternalAPI.Listen = "http://localhost:7772"
c.ExternalAPI.Listen = "http://[::]:8072" c.InternalAPI.Connect = "http://localhost:7772"
c.ExternalAPI.Listen = "http://[::]:8072"
c.Database.Defaults(10)
}
c.FederationMaxRetries = 16 c.FederationMaxRetries = 16
c.DisableTLSValidation = false c.DisableTLSValidation = false
c.Database.Defaults(10) if opts.Generate {
if generate { c.KeyPerspectives = KeyPerspectives{
c.Database.ConnectionString = "file:federationapi.db" {
ServerName: "matrix.org",
Keys: []KeyPerspectiveTrustKey{
{
KeyID: "ed25519:auto",
PublicKey: "Noi6WqcDj0QmPxCNQqgezwTlBKrfqehY1u2FyWP9uYw",
},
{
KeyID: "ed25519:a_RXGa",
PublicKey: "l8Hft5qXKn1vfHrg3p4+W8gELQVo8N13JkluMfmn2sQ",
},
},
},
}
if !opts.Monolithic {
c.Database.ConnectionString = "file:federationapi.db"
}
} }
} }
func (c *FederationAPI) Verify(configErrs *ConfigErrors, isMonolith bool) { func (c *FederationAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "federation_api.database.connection_string", string(c.Database.ConnectionString))
}
if isMonolith { // polylith required configs below if isMonolith { // polylith required configs below
return return
} }
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "federation_api.database.connection_string", string(c.Database.ConnectionString))
}
checkURL(configErrs, "federation_api.external_api.listen", string(c.ExternalAPI.Listen)) checkURL(configErrs, "federation_api.external_api.listen", string(c.ExternalAPI.Listen))
checkURL(configErrs, "federation_api.internal_api.listen", string(c.InternalAPI.Listen)) checkURL(configErrs, "federation_api.internal_api.listen", string(c.InternalAPI.Listen))
checkURL(configErrs, "federation_api.internal_api.connect", string(c.InternalAPI.Connect)) checkURL(configErrs, "federation_api.internal_api.connect", string(c.InternalAPI.Connect))

View file

@ -41,7 +41,7 @@ type Global struct {
// connections will be used instead. This way we don't have to manage connection // connections will be used instead. This way we don't have to manage connection
// counts on a per-component basis, but can instead do it for the entire monolith. // counts on a per-component basis, but can instead do it for the entire monolith.
// In a polylith deployment, this will be ignored. // In a polylith deployment, this will be ignored.
DatabaseOptions DatabaseOptions `yaml:"database"` DatabaseOptions DatabaseOptions `yaml:"database,omitempty"`
// The server name to delegate server-server communications to, with optional port // The server name to delegate server-server communications to, with optional port
WellKnownServerName string `yaml:"well_known_server_name"` WellKnownServerName string `yaml:"well_known_server_name"`
@ -83,22 +83,28 @@ type Global struct {
Cache Cache `yaml:"cache"` Cache Cache `yaml:"cache"`
} }
func (c *Global) Defaults(generate bool) { func (c *Global) Defaults(opts DefaultOpts) {
if generate { if opts.Generate {
c.ServerName = "localhost" c.ServerName = "localhost"
c.PrivateKeyPath = "matrix_key.pem" c.PrivateKeyPath = "matrix_key.pem"
_, c.PrivateKey, _ = ed25519.GenerateKey(rand.New(rand.NewSource(0))) _, c.PrivateKey, _ = ed25519.GenerateKey(rand.New(rand.NewSource(0)))
c.KeyID = "ed25519:auto" c.KeyID = "ed25519:auto"
c.TrustedIDServers = []string{
"matrix.org",
"vector.im",
}
} }
c.KeyValidityPeriod = time.Hour * 24 * 7 c.KeyValidityPeriod = time.Hour * 24 * 7
if opts.Monolithic {
c.JetStream.Defaults(generate) c.DatabaseOptions.Defaults(90)
c.Metrics.Defaults(generate) }
c.JetStream.Defaults(opts)
c.Metrics.Defaults(opts)
c.DNSCache.Defaults() c.DNSCache.Defaults()
c.Sentry.Defaults() c.Sentry.Defaults()
c.ServerNotices.Defaults(generate) c.ServerNotices.Defaults(opts)
c.ReportStats.Defaults() c.ReportStats.Defaults()
c.Cache.Defaults(generate) c.Cache.Defaults()
} }
func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) { func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) {
@ -142,9 +148,9 @@ type Metrics struct {
} `yaml:"basic_auth"` } `yaml:"basic_auth"`
} }
func (c *Metrics) Defaults(generate bool) { func (c *Metrics) Defaults(opts DefaultOpts) {
c.Enabled = false c.Enabled = false
if generate { if opts.Generate {
c.BasicAuth.Username = "metrics" c.BasicAuth.Username = "metrics"
c.BasicAuth.Password = "metrics" c.BasicAuth.Password = "metrics"
} }
@ -166,8 +172,8 @@ type ServerNotices struct {
RoomName string `yaml:"room_name"` RoomName string `yaml:"room_name"`
} }
func (c *ServerNotices) Defaults(generate bool) { func (c *ServerNotices) Defaults(opts DefaultOpts) {
if generate { if opts.Generate {
c.Enabled = true c.Enabled = true
c.LocalPart = "_server" c.LocalPart = "_server"
c.DisplayName = "Server Alert" c.DisplayName = "Server Alert"
@ -183,7 +189,7 @@ type Cache struct {
MaxAge time.Duration `yaml:"max_age"` MaxAge time.Duration `yaml:"max_age"`
} }
func (c *Cache) Defaults(generate bool) { func (c *Cache) Defaults() {
c.EstimatedMaxSize = 1024 * 1024 * 1024 // 1GB c.EstimatedMaxSize = 1024 * 1024 * 1024 // 1GB
c.MaxAge = time.Hour c.MaxAge = time.Hour
} }

View file

@ -31,10 +31,10 @@ func (c *JetStream) Durable(name string) string {
return c.Prefixed(name) return c.Prefixed(name)
} }
func (c *JetStream) Defaults(generate bool) { func (c *JetStream) Defaults(opts DefaultOpts) {
c.Addresses = []string{} c.Addresses = []string{}
c.TopicPrefix = "Dendrite" c.TopicPrefix = "Dendrite"
if generate { if opts.Generate {
c.StoragePath = Path("./") c.StoragePath = Path("./")
c.NoLog = true c.NoLog = true
c.DisableTLSValidation = true c.DisableTLSValidation = true

View file

@ -3,27 +3,31 @@ package config
type KeyServer struct { type KeyServer struct {
Matrix *Global `yaml:"-"` Matrix *Global `yaml:"-"`
InternalAPI InternalAPIOptions `yaml:"internal_api"` InternalAPI InternalAPIOptions `yaml:"internal_api,omitempty"`
Database DatabaseOptions `yaml:"database"` Database DatabaseOptions `yaml:"database,omitempty"`
} }
func (c *KeyServer) Defaults(generate bool) { func (c *KeyServer) Defaults(opts DefaultOpts) {
c.InternalAPI.Listen = "http://localhost:7779" if !opts.Monolithic {
c.InternalAPI.Connect = "http://localhost:7779" c.InternalAPI.Listen = "http://localhost:7779"
c.Database.Defaults(10) c.InternalAPI.Connect = "http://localhost:7779"
if generate { c.Database.Defaults(10)
c.Database.ConnectionString = "file:keyserver.db" }
if opts.Generate {
if !opts.Monolithic {
c.Database.ConnectionString = "file:keyserver.db"
}
} }
} }
func (c *KeyServer) Verify(configErrs *ConfigErrors, isMonolith bool) { func (c *KeyServer) Verify(configErrs *ConfigErrors, isMonolith bool) {
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "key_server.database.connection_string", string(c.Database.ConnectionString))
}
if isMonolith { // polylith required configs below if isMonolith { // polylith required configs below
return return
} }
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "key_server.database.connection_string", string(c.Database.ConnectionString))
}
checkURL(configErrs, "key_server.internal_api.listen", string(c.InternalAPI.Listen)) checkURL(configErrs, "key_server.internal_api.listen", string(c.InternalAPI.Listen))
checkURL(configErrs, "key_server.internal_api.connect", string(c.InternalAPI.Connect)) checkURL(configErrs, "key_server.internal_api.connect", string(c.InternalAPI.Connect))
} }

View file

@ -7,12 +7,12 @@ import (
type MediaAPI struct { type MediaAPI struct {
Matrix *Global `yaml:"-"` Matrix *Global `yaml:"-"`
InternalAPI InternalAPIOptions `yaml:"internal_api"` InternalAPI InternalAPIOptions `yaml:"internal_api,omitempty"`
ExternalAPI ExternalAPIOptions `yaml:"external_api"` ExternalAPI ExternalAPIOptions `yaml:"external_api,omitempty"`
// The MediaAPI database stores information about files uploaded and downloaded // The MediaAPI database stores information about files uploaded and downloaded
// by local users. It is only accessed by the MediaAPI. // by local users. It is only accessed by the MediaAPI.
Database DatabaseOptions `yaml:"database"` Database DatabaseOptions `yaml:"database,omitempty"`
// The base path to where the media files will be stored. May be relative or absolute. // The base path to where the media files will be stored. May be relative or absolute.
BasePath Path `yaml:"base_path"` BasePath Path `yaml:"base_path"`
@ -38,23 +38,41 @@ type MediaAPI struct {
// DefaultMaxFileSizeBytes defines the default file size allowed in transfers // DefaultMaxFileSizeBytes defines the default file size allowed in transfers
var DefaultMaxFileSizeBytes = FileSizeBytes(10485760) var DefaultMaxFileSizeBytes = FileSizeBytes(10485760)
func (c *MediaAPI) Defaults(generate bool) { func (c *MediaAPI) Defaults(opts DefaultOpts) {
c.InternalAPI.Listen = "http://localhost:7774" if !opts.Monolithic {
c.InternalAPI.Connect = "http://localhost:7774" c.InternalAPI.Listen = "http://localhost:7774"
c.ExternalAPI.Listen = "http://[::]:8074" c.InternalAPI.Connect = "http://localhost:7774"
c.ExternalAPI.Listen = "http://[::]:8074"
c.Database.Defaults(5)
}
c.MaxFileSizeBytes = DefaultMaxFileSizeBytes c.MaxFileSizeBytes = DefaultMaxFileSizeBytes
c.MaxThumbnailGenerators = 10 c.MaxThumbnailGenerators = 10
c.Database.Defaults(5) if opts.Generate {
if generate { c.ThumbnailSizes = []ThumbnailSize{
c.Database.ConnectionString = "file:mediaapi.db" {
Width: 32,
Height: 32,
ResizeMethod: "crop",
},
{
Width: 96,
Height: 96,
ResizeMethod: "crop",
},
{
Width: 640,
Height: 480,
ResizeMethod: "scale",
},
}
if !opts.Monolithic {
c.Database.ConnectionString = "file:mediaapi.db"
}
c.BasePath = "./media_store" c.BasePath = "./media_store"
} }
} }
func (c *MediaAPI) Verify(configErrs *ConfigErrors, isMonolith bool) { func (c *MediaAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "media_api.database.connection_string", string(c.Database.ConnectionString))
}
checkNotEmpty(configErrs, "media_api.base_path", string(c.BasePath)) checkNotEmpty(configErrs, "media_api.base_path", string(c.BasePath))
checkPositive(configErrs, "media_api.max_file_size_bytes", int64(c.MaxFileSizeBytes)) checkPositive(configErrs, "media_api.max_file_size_bytes", int64(c.MaxFileSizeBytes))
checkPositive(configErrs, "media_api.max_thumbnail_generators", int64(c.MaxThumbnailGenerators)) checkPositive(configErrs, "media_api.max_thumbnail_generators", int64(c.MaxThumbnailGenerators))
@ -66,6 +84,9 @@ func (c *MediaAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
if isMonolith { // polylith required configs below if isMonolith { // polylith required configs below
return return
} }
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "media_api.database.connection_string", string(c.Database.ConnectionString))
}
checkURL(configErrs, "media_api.internal_api.listen", string(c.InternalAPI.Listen)) checkURL(configErrs, "media_api.internal_api.listen", string(c.InternalAPI.Listen))
checkURL(configErrs, "media_api.internal_api.connect", string(c.InternalAPI.Connect)) checkURL(configErrs, "media_api.internal_api.connect", string(c.InternalAPI.Connect))
checkURL(configErrs, "media_api.external_api.listen", string(c.ExternalAPI.Listen)) checkURL(configErrs, "media_api.external_api.listen", string(c.ExternalAPI.Listen))

View file

@ -10,13 +10,17 @@ type MSCs struct {
// 'msc2946': Spaces Summary - https://github.com/matrix-org/matrix-doc/pull/2946 // 'msc2946': Spaces Summary - https://github.com/matrix-org/matrix-doc/pull/2946
MSCs []string `yaml:"mscs"` MSCs []string `yaml:"mscs"`
Database DatabaseOptions `yaml:"database"` Database DatabaseOptions `yaml:"database,omitempty"`
} }
func (c *MSCs) Defaults(generate bool) { func (c *MSCs) Defaults(opts DefaultOpts) {
c.Database.Defaults(5) if !opts.Monolithic {
if generate { c.Database.Defaults(5)
c.Database.ConnectionString = "file:mscs.db" }
if opts.Generate {
if !opts.Monolithic {
c.Database.ConnectionString = "file:mscs.db"
}
} }
} }
@ -31,6 +35,9 @@ func (c *MSCs) Enabled(msc string) bool {
} }
func (c *MSCs) Verify(configErrs *ConfigErrors, isMonolith bool) { func (c *MSCs) Verify(configErrs *ConfigErrors, isMonolith bool) {
if isMonolith { // polylith required configs below
return
}
if c.Matrix.DatabaseOptions.ConnectionString == "" { if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "mscs.database.connection_string", string(c.Database.ConnectionString)) checkNotEmpty(configErrs, "mscs.database.connection_string", string(c.Database.ConnectionString))
} }

View file

@ -3,27 +3,31 @@ package config
type RoomServer struct { type RoomServer struct {
Matrix *Global `yaml:"-"` Matrix *Global `yaml:"-"`
InternalAPI InternalAPIOptions `yaml:"internal_api"` InternalAPI InternalAPIOptions `yaml:"internal_api,omitempty"`
Database DatabaseOptions `yaml:"database"` Database DatabaseOptions `yaml:"database,omitempty"`
} }
func (c *RoomServer) Defaults(generate bool) { func (c *RoomServer) Defaults(opts DefaultOpts) {
c.InternalAPI.Listen = "http://localhost:7770" if !opts.Monolithic {
c.InternalAPI.Connect = "http://localhost:7770" c.InternalAPI.Listen = "http://localhost:7770"
c.Database.Defaults(10) c.InternalAPI.Connect = "http://localhost:7770"
if generate { c.Database.Defaults(20)
c.Database.ConnectionString = "file:roomserver.db" }
if opts.Generate {
if !opts.Monolithic {
c.Database.ConnectionString = "file:roomserver.db"
}
} }
} }
func (c *RoomServer) Verify(configErrs *ConfigErrors, isMonolith bool) { func (c *RoomServer) Verify(configErrs *ConfigErrors, isMonolith bool) {
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "room_server.database.connection_string", string(c.Database.ConnectionString))
}
if isMonolith { // polylith required configs below if isMonolith { // polylith required configs below
return return
} }
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "room_server.database.connection_string", string(c.Database.ConnectionString))
}
checkURL(configErrs, "room_server.internal_api.listen", string(c.InternalAPI.Listen)) checkURL(configErrs, "room_server.internal_api.listen", string(c.InternalAPI.Listen))
checkURL(configErrs, "room_server.internal_ap.connect", string(c.InternalAPI.Connect)) checkURL(configErrs, "room_server.internal_ap.connect", string(c.InternalAPI.Connect))
} }

View file

@ -3,31 +3,35 @@ package config
type SyncAPI struct { type SyncAPI struct {
Matrix *Global `yaml:"-"` Matrix *Global `yaml:"-"`
InternalAPI InternalAPIOptions `yaml:"internal_api"` InternalAPI InternalAPIOptions `yaml:"internal_api,omitempty"`
ExternalAPI ExternalAPIOptions `yaml:"external_api"` ExternalAPI ExternalAPIOptions `yaml:"external_api,omitempty"`
Database DatabaseOptions `yaml:"database"` Database DatabaseOptions `yaml:"database,omitempty"`
RealIPHeader string `yaml:"real_ip_header"` RealIPHeader string `yaml:"real_ip_header"`
} }
func (c *SyncAPI) Defaults(generate bool) { func (c *SyncAPI) Defaults(opts DefaultOpts) {
c.InternalAPI.Listen = "http://localhost:7773" if !opts.Monolithic {
c.InternalAPI.Connect = "http://localhost:7773" c.InternalAPI.Listen = "http://localhost:7773"
c.ExternalAPI.Listen = "http://localhost:8073" c.InternalAPI.Connect = "http://localhost:7773"
c.Database.Defaults(10) c.ExternalAPI.Listen = "http://localhost:8073"
if generate { c.Database.Defaults(20)
c.Database.ConnectionString = "file:syncapi.db" }
if opts.Generate {
if !opts.Monolithic {
c.Database.ConnectionString = "file:syncapi.db"
}
} }
} }
func (c *SyncAPI) Verify(configErrs *ConfigErrors, isMonolith bool) { func (c *SyncAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "sync_api.database", string(c.Database.ConnectionString))
}
if isMonolith { // polylith required configs below if isMonolith { // polylith required configs below
return return
} }
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "sync_api.database", string(c.Database.ConnectionString))
}
checkURL(configErrs, "sync_api.internal_api.listen", string(c.InternalAPI.Listen)) checkURL(configErrs, "sync_api.internal_api.listen", string(c.InternalAPI.Listen))
checkURL(configErrs, "sync_api.internal_api.connect", string(c.InternalAPI.Connect)) checkURL(configErrs, "sync_api.internal_api.connect", string(c.InternalAPI.Connect))
checkURL(configErrs, "sync_api.external_api.listen", string(c.ExternalAPI.Listen)) checkURL(configErrs, "sync_api.external_api.listen", string(c.ExternalAPI.Listen))

View file

@ -5,7 +5,7 @@ import "golang.org/x/crypto/bcrypt"
type UserAPI struct { type UserAPI struct {
Matrix *Global `yaml:"-"` Matrix *Global `yaml:"-"`
InternalAPI InternalAPIOptions `yaml:"internal_api"` InternalAPI InternalAPIOptions `yaml:"internal_api,omitempty"`
// The cost when hashing passwords. // The cost when hashing passwords.
BCryptCost int `yaml:"bcrypt_cost"` BCryptCost int `yaml:"bcrypt_cost"`
@ -18,30 +18,34 @@ type UserAPI struct {
// The Account database stores the login details and account information // The Account database stores the login details and account information
// for local users. It is accessed by the UserAPI. // for local users. It is accessed by the UserAPI.
AccountDatabase DatabaseOptions `yaml:"account_database"` AccountDatabase DatabaseOptions `yaml:"account_database,omitempty"`
} }
const DefaultOpenIDTokenLifetimeMS = 3600000 // 60 minutes const DefaultOpenIDTokenLifetimeMS = 3600000 // 60 minutes
func (c *UserAPI) Defaults(generate bool) { func (c *UserAPI) Defaults(opts DefaultOpts) {
c.InternalAPI.Listen = "http://localhost:7781" if !opts.Monolithic {
c.InternalAPI.Connect = "http://localhost:7781" c.InternalAPI.Listen = "http://localhost:7781"
c.InternalAPI.Connect = "http://localhost:7781"
c.AccountDatabase.Defaults(10)
}
c.BCryptCost = bcrypt.DefaultCost c.BCryptCost = bcrypt.DefaultCost
c.OpenIDTokenLifetimeMS = DefaultOpenIDTokenLifetimeMS c.OpenIDTokenLifetimeMS = DefaultOpenIDTokenLifetimeMS
c.AccountDatabase.Defaults(10) if opts.Generate {
if generate { if !opts.Monolithic {
c.AccountDatabase.ConnectionString = "file:userapi_accounts.db" c.AccountDatabase.ConnectionString = "file:userapi_accounts.db"
}
} }
} }
func (c *UserAPI) Verify(configErrs *ConfigErrors, isMonolith bool) { func (c *UserAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
checkPositive(configErrs, "user_api.openid_token_lifetime_ms", c.OpenIDTokenLifetimeMS) checkPositive(configErrs, "user_api.openid_token_lifetime_ms", c.OpenIDTokenLifetimeMS)
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "user_api.account_database.connection_string", string(c.AccountDatabase.ConnectionString))
}
if isMonolith { // polylith required configs below if isMonolith { // polylith required configs below
return return
} }
if c.Matrix.DatabaseOptions.ConnectionString == "" {
checkNotEmpty(configErrs, "user_api.account_database.connection_string", string(c.AccountDatabase.ConnectionString))
}
checkURL(configErrs, "user_api.internal_api.listen", string(c.InternalAPI.Listen)) checkURL(configErrs, "user_api.internal_api.listen", string(c.InternalAPI.Listen))
checkURL(configErrs, "user_api.internal_api.connect", string(c.InternalAPI.Connect)) checkURL(configErrs, "user_api.internal_api.connect", string(c.InternalAPI.Connect))
} }

View file

@ -547,7 +547,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 { func injectEvents(t *testing.T, userAPI userapi.UserInternalAPI, rsAPI roomserver.RoomserverInternalAPI, events []*gomatrixserverlib.HeaderedEvent) *mux.Router {
t.Helper() t.Helper()
cfg := &config.Dendrite{} cfg := &config.Dendrite{}
cfg.Defaults(true) cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = "localhost" cfg.Global.ServerName = "localhost"
cfg.MSCs.Database.ConnectionString = "file:msc2836_test.db" cfg.MSCs.Database.ConnectionString = "file:msc2836_test.db"
cfg.MSCs.MSCs = []string{"msc2836"} cfg.MSCs.MSCs = []string{"msc2836"}

View file

@ -30,12 +30,21 @@ import (
func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, func()) { func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, func()) {
var cfg config.Dendrite var cfg config.Dendrite
cfg.Defaults(false) cfg.Defaults(config.DefaultOpts{
Generate: false,
Monolithic: true,
})
cfg.Global.JetStream.InMemory = true cfg.Global.JetStream.InMemory = true
switch dbType { switch dbType {
case test.DBTypePostgres: case test.DBTypePostgres:
cfg.Global.Defaults(true) // autogen a signing key cfg.Global.Defaults(config.DefaultOpts{ // autogen a signing key
cfg.MediaAPI.Defaults(true) // autogen a media path Generate: true,
Monolithic: true,
})
cfg.MediaAPI.Defaults(config.DefaultOpts{ // autogen a media path
Generate: true,
Monolithic: true,
})
cfg.Global.ServerName = "test" cfg.Global.ServerName = "test"
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use // use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
// the file system event with InMemory=true :( // the file system event with InMemory=true :(
@ -49,7 +58,10 @@ func CreateBaseDendrite(t *testing.T, dbType test.DBType) (*base.BaseDendrite, f
} }
return base.NewBaseDendrite(&cfg, "Test", base.DisableMetrics), close return base.NewBaseDendrite(&cfg, "Test", base.DisableMetrics), close
case test.DBTypeSQLite: case test.DBTypeSQLite:
cfg.Defaults(true) // sets a sqlite db per component cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: false, // because we need a database per component
})
cfg.Global.ServerName = "test" cfg.Global.ServerName = "test"
// use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use // use a distinct prefix else concurrent postgres/sqlite runs will clash since NATS will use
// the file system event with InMemory=true :( // the file system event with InMemory=true :(
@ -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) { func Base(cfg *config.Dendrite) (*base.BaseDendrite, nats.JetStreamContext, *nats.Conn) {
if cfg == nil { if cfg == nil {
cfg = &config.Dendrite{} cfg = &config.Dendrite{}
cfg.Defaults(true) cfg.Defaults(config.DefaultOpts{
Generate: true,
Monolithic: true,
})
} }
cfg.Global.JetStream.InMemory = true cfg.Global.JetStream.InMemory = true
base := base.NewBaseDendrite(cfg, "Tests") base := base.NewBaseDendrite(cfg, "Tests")