diff --git a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go index 1a7abd6a7..261334412 100644 --- a/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/dendrite-federation-api-server/main.go @@ -15,16 +15,14 @@ package main import ( - "encoding/base64" + "flag" "net/http" "os" - "strings" - "time" "github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/common" + "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/common/keydb" - "github.com/matrix-org/dendrite/federationapi/config" "github.com/matrix-org/dendrite/federationapi/routing" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" @@ -33,73 +31,28 @@ import ( ) var ( - bindAddr = os.Getenv("BIND_ADDRESS") logDir = os.Getenv("LOG_DIR") - serverName = gomatrixserverlib.ServerName(os.Getenv("SERVER_NAME")) - serverKey = os.Getenv("SERVER_KEY") - // Base64 encoded SHA256 TLS fingerprint of the X509 certificate used by - // the public federation listener for this server. - // Can be generated from a PEM certificate called "server.crt" using: - // - // openssl x509 -noout -fingerprint -sha256 -inform pem -in server.crt |\ - // python -c 'print raw_input()[19:].replace(":","").decode("hex").encode("base64").rstrip("=\n")' - // - tlsFingerprint = os.Getenv("TLS_FINGERPRINT") - kafkaURIs = strings.Split(os.Getenv("KAFKA_URIS"), ",") - roomserverURL = os.Getenv("ROOMSERVER_URL") - roomserverInputTopic = os.Getenv("TOPIC_INPUT_ROOM_EVENT") - keyDataSource = os.Getenv("KEY_DATABASE") + configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") ) func main() { common.SetupLogging(logDir) - if bindAddr == "" { - log.Panic("No BIND_ADDRESS environment variable found.") - } - if serverName == "" { - serverName = "localhost" + if *configPath == "" { + log.Fatal("--config must be supplied") } - - if tlsFingerprint == "" { - log.Panic("No TLS_FINGERPRINT environment variable found.") - } - - if len(kafkaURIs) == 0 { - // the kafka default is :9092 - kafkaURIs = []string{"localhost:9092"} - } - - if roomserverURL == "" { - log.Panic("No ROOMSERVER_URL environment variable found.") - } - - if roomserverInputTopic == "" { - log.Panic("No TOPIC_INPUT_ROOM_EVENT environment variable found. This should match the roomserver input topic.") - } - cfg := config.FederationAPI{ - ServerName: serverName, - // TODO: make the validity period configurable. - ValidityPeriod: 24 * time.Hour, - } - - var err error - cfg.KeyID, cfg.PrivateKey, err = common.ReadKey(serverKey) + cfg, err := config.Load(*configPath) if err != nil { - log.Panicf("Failed to load private key: %s", err) + log.Fatalf("Invalid config file: %s", err) } - var fingerprintSHA256 []byte - if fingerprintSHA256, err = base64.RawStdEncoding.DecodeString(tlsFingerprint); err != nil { - log.Panicf("Failed to load TLS fingerprint: %s", err) - } - cfg.TLSFingerPrints = []gomatrixserverlib.TLSFingerprint{{fingerprintSHA256}} + federation := gomatrixserverlib.NewFederationClient( + cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey, + ) - federation := gomatrixserverlib.NewFederationClient(cfg.ServerName, cfg.KeyID, cfg.PrivateKey) - - keyDB, err := keydb.NewDatabase(keyDataSource) + keyDB, err := keydb.NewDatabase(string(cfg.Database.ServerKey)) if err != nil { - log.Panicf("Failed to setup key database(%q): %s", keyDataSource, err.Error()) + log.Panicf("Failed to setup key database(%q): %s", cfg.Database.ServerKey, err.Error()) } keyRing := gomatrixserverlib.KeyRing{ @@ -109,13 +62,18 @@ func main() { }, KeyDatabase: keyDB, } - queryAPI := api.NewRoomserverQueryAPIHTTP(roomserverURL, nil) + queryAPI := api.NewRoomserverQueryAPIHTTP("http://"+string(cfg.Listen.RoomServer), nil) + + roomserverProducer, err := producers.NewRoomserverProducer( + cfg.Kafka.Addresses, string(cfg.Kafka.Topics.InputRoomEvent), + ) - roomserverProducer, err := producers.NewRoomserverProducer(kafkaURIs, roomserverInputTopic) if err != nil { - log.Panicf("Failed to setup kafka producers(%s): %s", kafkaURIs, err) + log.Panicf("Failed to setup kafka producers(%s): %s", cfg.Kafka.Addresses, err) } - routing.Setup(http.DefaultServeMux, cfg, queryAPI, roomserverProducer, keyRing, federation) - log.Fatal(http.ListenAndServe(bindAddr, nil)) + log.Info("Starting federation API server on ", cfg.Listen.FederationAPI) + + routing.Setup(http.DefaultServeMux, *cfg, queryAPI, roomserverProducer, keyRing, federation) + log.Fatal(http.ListenAndServe(string(cfg.Listen.FederationAPI), nil)) } diff --git a/src/github.com/matrix-org/dendrite/federationapi/config/config.go b/src/github.com/matrix-org/dendrite/federationapi/config/config.go deleted file mode 100644 index 0e2012ca6..000000000 --- a/src/github.com/matrix-org/dendrite/federationapi/config/config.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2017 Vector Creations Ltd -// -// 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. - -package config - -import ( - "github.com/matrix-org/gomatrixserverlib" - "golang.org/x/crypto/ed25519" - "time" -) - -// FederationAPI contains the config information necessary to spin up a federationapi process. -type FederationAPI struct { - // The name of the server. This is usually the domain name, e.g 'matrix.org', 'localhost'. - ServerName gomatrixserverlib.ServerName - // The private key which will be used to sign requests. - PrivateKey ed25519.PrivateKey - // An arbitrary string used to uniquely identify the PrivateKey. Must start with the - // prefix "ed25519:". - KeyID gomatrixserverlib.KeyID - // A list of SHA256 TLS fingerprints for this server. - TLSFingerPrints []gomatrixserverlib.TLSFingerprint - // How long a remote server can cache our server key for before requesting it again. - // Increasing this number will reduce the number of requests made by remote servers - // for our key, but increases the period a compromised key will be considered valid - // by remote servers. - ValidityPeriod time.Duration -} diff --git a/src/github.com/matrix-org/dendrite/federationapi/readers/keys.go b/src/github.com/matrix-org/dendrite/federationapi/readers/keys.go index 572cfb52b..87c9f79bc 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/readers/keys.go +++ b/src/github.com/matrix-org/dendrite/federationapi/readers/keys.go @@ -16,7 +16,7 @@ package readers import ( "encoding/json" - "github.com/matrix-org/dendrite/federationapi/config" + "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" "golang.org/x/crypto/ed25519" @@ -26,29 +26,29 @@ import ( // LocalKeys returns the local keys for the server. // See https://matrix.org/docs/spec/server_server/unstable.html#publishing-keys -func LocalKeys(req *http.Request, cfg config.FederationAPI) util.JSONResponse { - keys, err := localKeys(cfg, time.Now().Add(cfg.ValidityPeriod)) +func LocalKeys(req *http.Request, cfg config.Dendrite) util.JSONResponse { + keys, err := localKeys(cfg, time.Now().Add(cfg.Matrix.KeyValidityPeriod)) if err != nil { return util.ErrorResponse(err) } return util.JSONResponse{Code: 200, JSON: keys} } -func localKeys(cfg config.FederationAPI, validUntil time.Time) (*gomatrixserverlib.ServerKeys, error) { +func localKeys(cfg config.Dendrite, validUntil time.Time) (*gomatrixserverlib.ServerKeys, error) { var keys gomatrixserverlib.ServerKeys - keys.ServerName = cfg.ServerName - keys.FromServer = cfg.ServerName + keys.ServerName = cfg.Matrix.ServerName + keys.FromServer = cfg.Matrix.ServerName - publicKey := cfg.PrivateKey.Public().(ed25519.PublicKey) + publicKey := cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey) keys.VerifyKeys = map[gomatrixserverlib.KeyID]gomatrixserverlib.VerifyKey{ - cfg.KeyID: { + cfg.Matrix.KeyID: { gomatrixserverlib.Base64String(publicKey), }, } - keys.TLSFingerprints = cfg.TLSFingerPrints + keys.TLSFingerprints = cfg.Matrix.TLSFingerPrints keys.OldVerifyKeys = map[gomatrixserverlib.KeyID]gomatrixserverlib.OldVerifyKey{} keys.ValidUntilTS = gomatrixserverlib.AsTimestamp(validUntil) @@ -57,7 +57,9 @@ func localKeys(cfg config.FederationAPI, validUntil time.Time) (*gomatrixserverl return nil, err } - keys.Raw, err = gomatrixserverlib.SignJSON(string(cfg.ServerName), cfg.KeyID, cfg.PrivateKey, toSign) + keys.Raw, err = gomatrixserverlib.SignJSON( + string(cfg.Matrix.ServerName), cfg.Matrix.KeyID, cfg.Matrix.PrivateKey, toSign, + ) if err != nil { return nil, err } diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go index 0f20d0115..d09858ca9 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go @@ -17,7 +17,7 @@ package routing import ( "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/producers" - "github.com/matrix-org/dendrite/federationapi/config" + "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/federationapi/readers" "github.com/matrix-org/dendrite/federationapi/writers" "github.com/matrix-org/dendrite/roomserver/api" @@ -36,7 +36,7 @@ const ( // Setup registers HTTP handlers with the given ServeMux. func Setup( servMux *http.ServeMux, - cfg config.FederationAPI, + cfg config.Dendrite, query api.RoomserverQueryAPI, producer *producers.RoomserverProducer, keys gomatrixserverlib.KeyRing, diff --git a/src/github.com/matrix-org/dendrite/federationapi/writers/send.go b/src/github.com/matrix-org/dendrite/federationapi/writers/send.go index d799c94c9..cdaabb811 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/writers/send.go +++ b/src/github.com/matrix-org/dendrite/federationapi/writers/send.go @@ -6,7 +6,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/producers" - "github.com/matrix-org/dendrite/federationapi/config" + "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" @@ -19,13 +19,13 @@ func Send( req *http.Request, txnID gomatrixserverlib.TransactionID, now time.Time, - cfg config.FederationAPI, + cfg config.Dendrite, query api.RoomserverQueryAPI, producer *producers.RoomserverProducer, keys gomatrixserverlib.KeyRing, federation *gomatrixserverlib.FederationClient, ) util.JSONResponse { - request, errResp := gomatrixserverlib.VerifyHTTPRequest(req, now, cfg.ServerName, keys) + request, errResp := gomatrixserverlib.VerifyHTTPRequest(req, now, cfg.Matrix.ServerName, keys) if request == nil { return errResp } @@ -45,7 +45,7 @@ func Send( t.Origin = request.Origin() t.TransactionID = txnID - t.Destination = cfg.ServerName + t.Destination = cfg.Matrix.ServerName resp, err := t.processTransaction() if err != nil {