2020-06-12 08:55:57 -05:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
2018-01-02 04:26:56 -06:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-06-12 08:55:57 -05:00
|
|
|
package setup
|
2018-01-02 04:26:56 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-06-02 15:02:24 -05:00
|
|
|
"fmt"
|
2018-01-02 04:26:56 -06:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-02-13 11:27:33 -06:00
|
|
|
"net/url"
|
2020-04-03 05:40:50 -05:00
|
|
|
"time"
|
2018-01-02 04:26:56 -06:00
|
|
|
|
2020-05-21 08:40:13 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2020-06-12 08:55:57 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2020-04-16 04:06:55 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2018-01-02 04:26:56 -06:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/naffka"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
|
2020-05-21 08:40:13 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2018-01-02 04:26:56 -06:00
|
|
|
|
2020-04-22 09:26:56 -05:00
|
|
|
"github.com/Shopify/sarama"
|
2018-01-02 04:26:56 -06:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2018-07-17 09:36:04 -05:00
|
|
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
2020-06-04 09:43:07 -05:00
|
|
|
asinthttp "github.com/matrix-org/dendrite/appservice/inthttp"
|
2020-03-30 09:02:20 -05:00
|
|
|
eduServerAPI "github.com/matrix-org/dendrite/eduserver/api"
|
2020-06-04 09:43:07 -05:00
|
|
|
eduinthttp "github.com/matrix-org/dendrite/eduserver/inthttp"
|
2019-10-01 11:09:47 -05:00
|
|
|
federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api"
|
2020-06-04 09:43:07 -05:00
|
|
|
fsinthttp "github.com/matrix-org/dendrite/federationsender/inthttp"
|
2020-05-21 08:40:13 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/config"
|
2018-07-17 09:36:04 -05:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-06-04 09:43:07 -05:00
|
|
|
rsinthttp "github.com/matrix-org/dendrite/roomserver/inthttp"
|
2020-05-27 04:19:24 -05:00
|
|
|
serverKeyAPI "github.com/matrix-org/dendrite/serverkeyapi/api"
|
2020-06-04 10:26:35 -05:00
|
|
|
skinthttp "github.com/matrix-org/dendrite/serverkeyapi/inthttp"
|
2018-01-02 04:26:56 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-05-11 12:21:39 -05:00
|
|
|
|
|
|
|
_ "net/http/pprof"
|
2018-01-02 04:26:56 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// BaseDendrite is a base for creating new instances of dendrite. It parses
|
|
|
|
// command line flags and config, and exposes methods for creating various
|
|
|
|
// resources. All errors are handled by logging then exiting, so all methods
|
|
|
|
// should only be used during start up.
|
|
|
|
// Must be closed when shutting down.
|
|
|
|
type BaseDendrite struct {
|
|
|
|
componentName string
|
|
|
|
tracerCloser io.Closer
|
|
|
|
|
2020-05-22 05:43:17 -05:00
|
|
|
// PublicAPIMux should be used to register new public matrix api endpoints
|
|
|
|
PublicAPIMux *mux.Router
|
|
|
|
InternalAPIMux *mux.Router
|
2020-06-04 08:27:10 -05:00
|
|
|
UseHTTPAPIs bool
|
2020-04-22 07:00:05 -05:00
|
|
|
httpClient *http.Client
|
|
|
|
Cfg *config.Dendrite
|
2020-06-05 10:42:01 -05:00
|
|
|
Caches *caching.Caches
|
2020-04-22 07:00:05 -05:00
|
|
|
KafkaConsumer sarama.Consumer
|
|
|
|
KafkaProducer sarama.SyncProducer
|
2018-01-02 04:26:56 -06:00
|
|
|
}
|
|
|
|
|
2020-04-20 11:42:34 -05:00
|
|
|
const HTTPServerTimeout = time.Minute * 5
|
|
|
|
const HTTPClientTimeout = time.Second * 30
|
|
|
|
|
2018-01-02 04:26:56 -06:00
|
|
|
// NewBaseDendrite creates a new instance to be used by a component.
|
|
|
|
// The componentName is used for logging purposes, and should be a friendly name
|
|
|
|
// of the compontent running, e.g. "SyncAPI"
|
2020-06-04 08:27:10 -05:00
|
|
|
func NewBaseDendrite(cfg *config.Dendrite, componentName string, useHTTPAPIs bool) *BaseDendrite {
|
2020-05-21 08:40:13 -05:00
|
|
|
internal.SetupStdLogging()
|
|
|
|
internal.SetupHookLogging(cfg.Logging, componentName)
|
|
|
|
internal.SetupPprof()
|
2018-01-02 04:26:56 -06:00
|
|
|
|
|
|
|
closer, err := cfg.SetupTracing("Dendrite" + componentName)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to start opentracing")
|
|
|
|
}
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
var kafkaConsumer sarama.Consumer
|
|
|
|
var kafkaProducer sarama.SyncProducer
|
|
|
|
if cfg.Kafka.UseNaffka {
|
|
|
|
kafkaConsumer, kafkaProducer = setupNaffka(cfg)
|
|
|
|
} else {
|
|
|
|
kafkaConsumer, kafkaProducer = setupKafka(cfg)
|
|
|
|
}
|
2018-01-02 04:26:56 -06:00
|
|
|
|
2020-06-05 10:42:01 -05:00
|
|
|
cache, err := caching.NewInMemoryLRUCache()
|
2020-04-22 07:00:05 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Warnf("Failed to create cache")
|
|
|
|
}
|
|
|
|
|
2020-06-02 15:02:24 -05:00
|
|
|
client := http.Client{Timeout: HTTPClientTimeout}
|
|
|
|
if cfg.Proxy != nil {
|
|
|
|
client.Transport = &http.Transport{Proxy: http.ProxyURL(&url.URL{
|
|
|
|
Scheme: cfg.Proxy.Protocol,
|
|
|
|
Host: fmt.Sprintf("%s:%d", cfg.Proxy.Host, cfg.Proxy.Port),
|
|
|
|
})}
|
|
|
|
}
|
|
|
|
|
2020-06-04 05:14:08 -05:00
|
|
|
// Ideally we would only use SkipClean on routes which we know can allow '/' but due to
|
|
|
|
// https://github.com/gorilla/mux/issues/460 we have to attach this at the top router.
|
|
|
|
// When used in conjunction with UseEncodedPath() we get the behaviour we want when parsing
|
|
|
|
// path parameters:
|
|
|
|
// /foo/bar%2Fbaz == [foo, bar%2Fbaz] (from UseEncodedPath)
|
|
|
|
// /foo/bar%2F%2Fbaz == [foo, bar%2F%2Fbaz] (from SkipClean)
|
|
|
|
// In particular, rooms v3 event IDs are not urlsafe and can include '/' and because they
|
|
|
|
// are randomly generated it results in flakey tests.
|
|
|
|
// We need to be careful with media APIs if they read from a filesystem to make sure they
|
|
|
|
// are not inadvertently reading paths without cleaning, else this could introduce a
|
|
|
|
// directory traversal attack e.g /../../../etc/passwd
|
|
|
|
httpmux := mux.NewRouter().SkipClean(true)
|
2020-06-02 15:02:24 -05:00
|
|
|
|
2018-01-02 04:26:56 -06:00
|
|
|
return &BaseDendrite{
|
2020-04-22 07:00:05 -05:00
|
|
|
componentName: componentName,
|
2020-06-04 08:27:10 -05:00
|
|
|
UseHTTPAPIs: useHTTPAPIs,
|
2020-04-22 07:00:05 -05:00
|
|
|
tracerCloser: closer,
|
|
|
|
Cfg: cfg,
|
2020-06-05 10:42:01 -05:00
|
|
|
Caches: cache,
|
2020-06-12 08:55:57 -05:00
|
|
|
PublicAPIMux: httpmux.PathPrefix(httputil.PublicPathPrefix).Subrouter().UseEncodedPath(),
|
|
|
|
InternalAPIMux: httpmux.PathPrefix(httputil.InternalPathPrefix).Subrouter().UseEncodedPath(),
|
2020-06-02 15:02:24 -05:00
|
|
|
httpClient: &client,
|
2020-04-22 07:00:05 -05:00
|
|
|
KafkaConsumer: kafkaConsumer,
|
|
|
|
KafkaProducer: kafkaProducer,
|
2018-01-02 04:26:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements io.Closer
|
|
|
|
func (b *BaseDendrite) Close() error {
|
|
|
|
return b.tracerCloser.Close()
|
|
|
|
}
|
|
|
|
|
2020-06-04 09:43:07 -05:00
|
|
|
// AppserviceHTTPClient returns the AppServiceQueryAPI for hitting the appservice component over HTTP.
|
|
|
|
func (b *BaseDendrite) AppserviceHTTPClient() appserviceAPI.AppServiceQueryAPI {
|
|
|
|
a, err := asinthttp.NewAppserviceClient(b.Cfg.AppServiceURL(), b.httpClient)
|
2020-04-03 05:40:50 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("CreateHTTPAppServiceAPIs failed")
|
|
|
|
}
|
|
|
|
return a
|
2018-07-17 09:36:04 -05:00
|
|
|
}
|
|
|
|
|
2020-06-04 09:43:07 -05:00
|
|
|
// RoomserverHTTPClient returns RoomserverInternalAPI for hitting the roomserver over HTTP.
|
|
|
|
func (b *BaseDendrite) RoomserverHTTPClient() roomserverAPI.RoomserverInternalAPI {
|
2020-06-05 10:42:01 -05:00
|
|
|
rsAPI, err := rsinthttp.NewRoomserverClient(b.Cfg.RoomServerURL(), b.httpClient, b.Caches)
|
2020-04-03 05:40:50 -05:00
|
|
|
if err != nil {
|
2020-06-04 09:43:07 -05:00
|
|
|
logrus.WithError(err).Panic("RoomserverHTTPClient failed", b.httpClient)
|
2020-04-03 05:40:50 -05:00
|
|
|
}
|
2020-05-01 04:48:17 -05:00
|
|
|
return rsAPI
|
2018-01-02 04:26:56 -06:00
|
|
|
}
|
|
|
|
|
2020-06-04 09:43:07 -05:00
|
|
|
// EDUServerClient returns EDUServerInputAPI for hitting the EDU server over HTTP
|
|
|
|
func (b *BaseDendrite) EDUServerClient() eduServerAPI.EDUServerInputAPI {
|
|
|
|
e, err := eduinthttp.NewEDUServerClient(b.Cfg.EDUServerURL(), b.httpClient)
|
2020-04-03 05:40:50 -05:00
|
|
|
if err != nil {
|
2020-06-04 09:43:07 -05:00
|
|
|
logrus.WithError(err).Panic("EDUServerClient failed", b.httpClient)
|
2020-04-03 05:40:50 -05:00
|
|
|
}
|
|
|
|
return e
|
2018-07-24 09:49:49 -05:00
|
|
|
}
|
|
|
|
|
2020-06-04 08:27:10 -05:00
|
|
|
// FederationSenderHTTPClient returns FederationSenderInternalAPI for hitting
|
2019-10-01 11:09:47 -05:00
|
|
|
// the federation sender over HTTP
|
2020-06-04 08:27:10 -05:00
|
|
|
func (b *BaseDendrite) FederationSenderHTTPClient() federationSenderAPI.FederationSenderInternalAPI {
|
2020-06-04 09:43:07 -05:00
|
|
|
f, err := fsinthttp.NewFederationSenderClient(b.Cfg.FederationSenderURL(), b.httpClient)
|
2020-04-03 05:40:50 -05:00
|
|
|
if err != nil {
|
2020-06-04 09:43:07 -05:00
|
|
|
logrus.WithError(err).Panic("FederationSenderHTTPClient failed", b.httpClient)
|
2020-04-03 05:40:50 -05:00
|
|
|
}
|
|
|
|
return f
|
2019-10-01 11:09:47 -05:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:26:35 -05:00
|
|
|
// ServerKeyAPIClient returns ServerKeyInternalAPI for hitting the server key API over HTTP
|
|
|
|
func (b *BaseDendrite) ServerKeyAPIClient() serverKeyAPI.ServerKeyInternalAPI {
|
|
|
|
f, err := skinthttp.NewServerKeyClient(
|
2020-05-27 04:19:24 -05:00
|
|
|
b.Cfg.ServerKeyAPIURL(),
|
|
|
|
b.httpClient,
|
2020-06-05 10:42:01 -05:00
|
|
|
b.Caches,
|
2020-05-27 04:19:24 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("NewServerKeyInternalAPIHTTP failed", b.httpClient)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-01-02 04:26:56 -06:00
|
|
|
// CreateDeviceDB creates a new instance of the device database. Should only be
|
|
|
|
// called once per component.
|
2020-02-13 11:27:33 -06:00
|
|
|
func (b *BaseDendrite) CreateDeviceDB() devices.Database {
|
2020-05-01 07:34:53 -05:00
|
|
|
db, err := devices.NewDatabase(string(b.Cfg.Database.Device), b.Cfg.DbProperties(), b.Cfg.Matrix.ServerName)
|
2018-01-02 04:26:56 -06:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to connect to devices db")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAccountsDB creates a new instance of the accounts database. Should only
|
|
|
|
// be called once per component.
|
2020-02-13 11:27:33 -06:00
|
|
|
func (b *BaseDendrite) CreateAccountsDB() accounts.Database {
|
2020-05-01 07:34:53 -05:00
|
|
|
db, err := accounts.NewDatabase(string(b.Cfg.Database.Account), b.Cfg.DbProperties(), b.Cfg.Matrix.ServerName)
|
2018-01-02 04:26:56 -06:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to connect to accounts db")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFederationClient creates a new federation client. Should only be called
|
|
|
|
// once per component.
|
|
|
|
func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationClient {
|
|
|
|
return gomatrixserverlib.NewFederationClient(
|
|
|
|
b.Cfg.Matrix.ServerName, b.Cfg.Matrix.KeyID, b.Cfg.Matrix.PrivateKey,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on
|
|
|
|
// ApiMux under /api/ and adds a prometheus handler under /metrics.
|
2019-10-02 04:29:27 -05:00
|
|
|
func (b *BaseDendrite) SetupAndServeHTTP(bindaddr string, listenaddr string) {
|
|
|
|
// If a separate bind address is defined, listen on that. Otherwise use
|
|
|
|
// the listen address
|
|
|
|
var addr string
|
|
|
|
if bindaddr != "" {
|
|
|
|
addr = bindaddr
|
|
|
|
} else {
|
|
|
|
addr = listenaddr
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:42:34 -05:00
|
|
|
serv := http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
WriteTimeout: HTTPServerTimeout,
|
|
|
|
}
|
2018-01-02 04:26:56 -06:00
|
|
|
|
2020-06-12 08:55:57 -05:00
|
|
|
httputil.SetupHTTPAPI(
|
2020-05-22 05:43:17 -05:00
|
|
|
http.DefaultServeMux,
|
|
|
|
b.PublicAPIMux,
|
|
|
|
b.InternalAPIMux,
|
|
|
|
b.Cfg,
|
2020-06-04 08:27:10 -05:00
|
|
|
b.UseHTTPAPIs,
|
2020-05-22 05:43:17 -05:00
|
|
|
)
|
2020-04-20 11:42:34 -05:00
|
|
|
logrus.Infof("Starting %s server on %s", b.componentName, serv.Addr)
|
2018-01-02 04:26:56 -06:00
|
|
|
|
2020-04-20 11:42:34 -05:00
|
|
|
err := serv.ListenAndServe()
|
2018-01-02 04:26:56 -06:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Fatal("failed to serve http")
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:42:34 -05:00
|
|
|
logrus.Infof("Stopped %s server on %s", b.componentName, serv.Addr)
|
2018-01-02 04:26:56 -06:00
|
|
|
}
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
// setupKafka creates kafka consumer/producer pair from the config.
|
2018-01-02 04:26:56 -06:00
|
|
|
func setupKafka(cfg *config.Dendrite) (sarama.Consumer, sarama.SyncProducer) {
|
2020-02-13 11:27:33 -06:00
|
|
|
consumer, err := sarama.NewConsumer(cfg.Kafka.Addresses, nil)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("failed to start kafka consumer")
|
|
|
|
}
|
|
|
|
|
|
|
|
producer, err := sarama.NewSyncProducer(cfg.Kafka.Addresses, nil)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("failed to setup kafka producers")
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumer, producer
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupNaffka creates kafka consumer/producer pair from the config.
|
|
|
|
func setupNaffka(cfg *config.Dendrite) (sarama.Consumer, sarama.SyncProducer) {
|
|
|
|
var err error
|
|
|
|
var db *sql.DB
|
|
|
|
var naffkaDB *naffka.DatabaseImpl
|
|
|
|
|
|
|
|
uri, err := url.Parse(string(cfg.Database.Naffka))
|
|
|
|
if err != nil || uri.Scheme == "file" {
|
2020-05-25 09:29:49 -05:00
|
|
|
var cs string
|
2020-06-04 05:18:08 -05:00
|
|
|
cs, err = sqlutil.ParseFileURI(string(cfg.Database.Naffka))
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("Failed to parse naffka database file URI")
|
2020-05-25 09:29:49 -05:00
|
|
|
}
|
2020-06-12 08:55:57 -05:00
|
|
|
db, err = sqlutil.Open(sqlutil.SQLiteDriverName(), cs, nil)
|
2018-01-02 04:26:56 -06:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("Failed to open naffka database")
|
|
|
|
}
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
naffkaDB, err = naffka.NewSqliteDatabase(db)
|
2018-01-02 04:26:56 -06:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("Failed to setup naffka database")
|
|
|
|
}
|
2020-02-13 11:27:33 -06:00
|
|
|
} else {
|
2020-05-01 07:34:53 -05:00
|
|
|
db, err = sqlutil.Open("postgres", string(cfg.Database.Naffka), nil)
|
2018-01-02 04:26:56 -06:00
|
|
|
if err != nil {
|
2020-02-13 11:27:33 -06:00
|
|
|
logrus.WithError(err).Panic("Failed to open naffka database")
|
2018-01-02 04:26:56 -06:00
|
|
|
}
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
naffkaDB, err = naffka.NewPostgresqlDatabase(db)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panic("Failed to setup naffka database")
|
|
|
|
}
|
2018-01-02 04:26:56 -06:00
|
|
|
}
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
if naffkaDB == nil {
|
|
|
|
panic("naffka connection string not understood")
|
2018-01-02 04:26:56 -06:00
|
|
|
}
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
naff, err := naffka.New(naffkaDB)
|
2018-01-02 04:26:56 -06:00
|
|
|
if err != nil {
|
2020-02-13 11:27:33 -06:00
|
|
|
logrus.WithError(err).Panic("Failed to setup naffka")
|
2018-01-02 04:26:56 -06:00
|
|
|
}
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
return naff, naff
|
2018-01-02 04:26:56 -06:00
|
|
|
}
|