2017-05-23 18:43:05 +02:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-04-15 18:48:40 +02:00
|
|
|
"io"
|
2017-07-17 18:20:57 +02:00
|
|
|
"net/http"
|
2020-04-15 18:48:40 +02:00
|
|
|
"net/http/httptest"
|
|
|
|
"net/http/httputil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2017-09-04 14:14:01 +02:00
|
|
|
"time"
|
2017-07-17 18:20:57 +02:00
|
|
|
|
2017-05-23 18:43:05 +02:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2020-04-14 16:54:35 +02:00
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-09-04 14:14:01 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-05-23 18:43:05 +02:00
|
|
|
"github.com/matrix-org/util"
|
2017-09-28 15:50:40 +02:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
2019-08-14 19:34:49 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-12-17 17:47:45 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2018-05-23 16:42:08 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2020-04-14 16:54:35 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-23 18:43:05 +02:00
|
|
|
)
|
|
|
|
|
2020-04-14 16:54:35 +02:00
|
|
|
// BasicAuth is used for authorization on /metrics handlers
|
|
|
|
type BasicAuth struct {
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:17:03 +02:00
|
|
|
// MakeAuthAPI turns a util.JSONRequestHandler function into an http.Handler which authenticates the request.
|
|
|
|
func MakeAuthAPI(
|
|
|
|
metricsName string, data auth.Data,
|
|
|
|
f func(*http.Request, *authtypes.Device) util.JSONResponse,
|
|
|
|
) http.Handler {
|
2017-09-04 14:14:01 +02:00
|
|
|
h := func(req *http.Request) util.JSONResponse {
|
2018-07-17 00:17:03 +02:00
|
|
|
device, err := auth.VerifyUserFromRequest(req, data)
|
|
|
|
if err != nil {
|
|
|
|
return *err
|
2017-05-23 18:43:05 +02:00
|
|
|
}
|
2020-03-09 15:37:51 +01:00
|
|
|
// add the user ID to the logger
|
|
|
|
logger := util.GetLogger((req.Context()))
|
|
|
|
logger = logger.WithField("user_id", device.UserID)
|
|
|
|
req = req.WithContext(util.ContextWithLogger(req.Context(), logger))
|
2018-07-17 00:17:03 +02:00
|
|
|
|
2017-05-23 18:43:05 +02:00
|
|
|
return f(req, device)
|
2017-09-04 14:14:01 +02:00
|
|
|
}
|
2017-09-28 15:50:40 +02:00
|
|
|
return MakeExternalAPI(metricsName, h)
|
2017-05-23 18:43:05 +02:00
|
|
|
}
|
|
|
|
|
2017-09-28 15:50:40 +02:00
|
|
|
// MakeExternalAPI turns a util.JSONRequestHandler function into an http.Handler.
|
|
|
|
// This is used for APIs that are called from the internet.
|
|
|
|
func MakeExternalAPI(metricsName string, f func(*http.Request) util.JSONResponse) http.Handler {
|
2020-04-15 18:48:40 +02:00
|
|
|
// TODO: We shouldn't be directly reading env vars here, inject it in instead.
|
|
|
|
// Refactor this when we split out config structs.
|
|
|
|
verbose := false
|
|
|
|
if os.Getenv("DENDRITE_TRACE_HTTP") == "1" {
|
|
|
|
verbose = true
|
|
|
|
}
|
2017-09-28 15:50:40 +02:00
|
|
|
h := util.MakeJSONAPI(util.NewJSONRequestHandler(f))
|
|
|
|
withSpan := func(w http.ResponseWriter, req *http.Request) {
|
2020-04-15 18:48:40 +02:00
|
|
|
nextWriter := w
|
|
|
|
if verbose {
|
|
|
|
logger := logrus.NewEntry(logrus.StandardLogger())
|
|
|
|
// Log outgoing response
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
nextWriter = rec
|
|
|
|
defer func() {
|
|
|
|
resp := rec.Result()
|
|
|
|
dump, err := httputil.DumpResponse(resp, true)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debugf("Failed to dump outgoing response: %s", err)
|
|
|
|
} else {
|
|
|
|
strSlice := strings.Split(string(dump), "\n")
|
|
|
|
for _, s := range strSlice {
|
|
|
|
logger.Debug(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// copy the response to the client
|
|
|
|
for hdr, vals := range resp.Header {
|
|
|
|
for _, val := range vals {
|
|
|
|
w.Header().Add(hdr, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
// discard errors as this is for debugging
|
|
|
|
_, _ = io.Copy(w, resp.Body)
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Log incoming request
|
|
|
|
dump, err := httputil.DumpRequest(req, true)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debugf("Failed to dump incoming request: %s", err)
|
|
|
|
} else {
|
|
|
|
strSlice := strings.Split(string(dump), "\n")
|
|
|
|
for _, s := range strSlice {
|
|
|
|
logger.Debug(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 15:50:40 +02:00
|
|
|
span := opentracing.StartSpan(metricsName)
|
|
|
|
defer span.Finish()
|
|
|
|
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))
|
2020-04-15 18:48:40 +02:00
|
|
|
h.ServeHTTP(nextWriter, req)
|
|
|
|
|
2017-09-28 15:50:40 +02:00
|
|
|
}
|
|
|
|
|
2018-05-23 16:42:08 +02:00
|
|
|
return http.HandlerFunc(withSpan)
|
2017-09-28 15:50:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 19:34:49 +02:00
|
|
|
// MakeHTMLAPI adds Span metrics to the HTML Handler function
|
|
|
|
// This is used to serve HTML alongside JSON error messages
|
|
|
|
func MakeHTMLAPI(metricsName string, f func(http.ResponseWriter, *http.Request) *util.JSONResponse) http.Handler {
|
|
|
|
withSpan := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
span := opentracing.StartSpan(metricsName)
|
|
|
|
defer span.Finish()
|
|
|
|
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))
|
|
|
|
if err := f(w, req); err != nil {
|
|
|
|
h := util.MakeJSONAPI(util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
|
|
|
|
return *err
|
|
|
|
}))
|
|
|
|
h.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 17:47:45 +01:00
|
|
|
return promhttp.InstrumentHandlerCounter(
|
|
|
|
promauto.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: metricsName,
|
|
|
|
Help: "Total number of http requests for HTML resources",
|
|
|
|
},
|
|
|
|
[]string{"code"},
|
|
|
|
),
|
|
|
|
http.HandlerFunc(withSpan),
|
|
|
|
)
|
2019-08-14 19:34:49 +02:00
|
|
|
}
|
|
|
|
|
2017-09-28 15:50:40 +02:00
|
|
|
// MakeInternalAPI turns a util.JSONRequestHandler function into an http.Handler.
|
|
|
|
// This is used for APIs that are internal to dendrite.
|
|
|
|
// If we are passed a tracing context in the request headers then we use that
|
|
|
|
// as the parent of any tracing spans we create.
|
|
|
|
func MakeInternalAPI(metricsName string, f func(*http.Request) util.JSONResponse) http.Handler {
|
|
|
|
h := util.MakeJSONAPI(util.NewJSONRequestHandler(f))
|
|
|
|
withSpan := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
carrier := opentracing.HTTPHeadersCarrier(req.Header)
|
|
|
|
tracer := opentracing.GlobalTracer()
|
|
|
|
clientContext, err := tracer.Extract(opentracing.HTTPHeaders, carrier)
|
|
|
|
var span opentracing.Span
|
|
|
|
if err == nil {
|
|
|
|
// Default to a span without RPC context.
|
|
|
|
span = tracer.StartSpan(metricsName)
|
|
|
|
} else {
|
|
|
|
// Set the RPC context.
|
|
|
|
span = tracer.StartSpan(metricsName, ext.RPCServerOption(clientContext))
|
|
|
|
}
|
|
|
|
defer span.Finish()
|
|
|
|
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))
|
|
|
|
h.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
2018-05-23 16:42:08 +02:00
|
|
|
return http.HandlerFunc(withSpan)
|
2017-05-23 18:43:05 +02:00
|
|
|
}
|
2017-08-03 16:10:39 +02:00
|
|
|
|
2017-09-04 14:14:01 +02:00
|
|
|
// MakeFedAPI makes an http.Handler that checks matrix federation authentication.
|
|
|
|
func MakeFedAPI(
|
|
|
|
metricsName string,
|
|
|
|
serverName gomatrixserverlib.ServerName,
|
|
|
|
keyRing gomatrixserverlib.KeyRing,
|
|
|
|
f func(*http.Request, *gomatrixserverlib.FederationRequest) util.JSONResponse,
|
|
|
|
) http.Handler {
|
|
|
|
h := func(req *http.Request) util.JSONResponse {
|
|
|
|
fedReq, errResp := gomatrixserverlib.VerifyHTTPRequest(
|
|
|
|
req, time.Now(), serverName, keyRing,
|
|
|
|
)
|
|
|
|
if fedReq == nil {
|
|
|
|
return errResp
|
|
|
|
}
|
|
|
|
return f(req, fedReq)
|
|
|
|
}
|
2017-09-28 15:50:40 +02:00
|
|
|
return MakeExternalAPI(metricsName, h)
|
2017-09-04 14:14:01 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 16:10:39 +02:00
|
|
|
// SetupHTTPAPI registers an HTTP API mux under /api and sets up a metrics
|
|
|
|
// listener.
|
2020-04-14 16:54:35 +02:00
|
|
|
func SetupHTTPAPI(servMux *http.ServeMux, apiMux http.Handler, cfg *config.Dendrite) {
|
|
|
|
if cfg.Metrics.Enabled {
|
|
|
|
servMux.Handle("/metrics", WrapHandlerInBasicAuth(promhttp.Handler(), cfg.Metrics.BasicAuth))
|
|
|
|
}
|
2017-08-03 16:10:39 +02:00
|
|
|
servMux.Handle("/api/", http.StripPrefix("/api", apiMux))
|
|
|
|
}
|
2017-12-06 10:36:50 +01:00
|
|
|
|
2020-04-14 16:54:35 +02:00
|
|
|
// WrapHandlerInBasicAuth adds basic auth to a handler. Only used for /metrics
|
|
|
|
func WrapHandlerInBasicAuth(h http.Handler, b BasicAuth) http.HandlerFunc {
|
|
|
|
if b.Username == "" || b.Password == "" {
|
|
|
|
logrus.Warn("Metrics are exposed without protection. Make sure you set up protection at proxy level.")
|
|
|
|
}
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Serve without authorization if either Username or Password is unset
|
|
|
|
if b.Username == "" || b.Password == "" {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, pass, ok := r.BasicAuth()
|
|
|
|
|
|
|
|
if !ok || user != b.Username || pass != b.Password {
|
|
|
|
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 10:36:50 +01:00
|
|
|
// WrapHandlerInCORS adds CORS headers to all responses, including all error
|
|
|
|
// responses.
|
|
|
|
// Handles OPTIONS requests directly.
|
|
|
|
func WrapHandlerInCORS(h http.Handler) http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
|
|
|
|
|
2018-03-13 16:55:45 +01:00
|
|
|
if r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" {
|
2017-12-06 10:36:50 +01:00
|
|
|
// Its easiest just to always return a 200 OK for everything. Whether
|
|
|
|
// this is technically correct or not is a question, but in the end this
|
|
|
|
// is what a lot of other people do (including synapse) and the clients
|
|
|
|
// are perfectly happy with it.
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
} else {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|