2021-11-02 13:44:37 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2022-08-08 19:00:45 +00:00
|
|
|
"bytes"
|
2021-11-02 13:44:37 +00:00
|
|
|
"encoding/json"
|
2022-08-08 19:00:45 +00:00
|
|
|
"io"
|
2021-11-02 13:44:37 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"goauthentik.io/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SentryRequest struct {
|
|
|
|
DSN string `json:"dsn"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WebServer) APISentryProxy(rw http.ResponseWriter, r *http.Request) {
|
2022-07-26 09:33:35 +00:00
|
|
|
if !config.Get().ErrorReporting.Enabled {
|
2021-11-02 13:44:37 +00:00
|
|
|
ws.log.Debug("error reporting disabled")
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-08-08 19:00:45 +00:00
|
|
|
fb := &bytes.Buffer{}
|
|
|
|
_, err := io.Copy(fb, r.Body)
|
2021-11-02 13:44:37 +00:00
|
|
|
if err != nil {
|
|
|
|
ws.log.Debug("failed to read body")
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-08-08 19:17:10 +00:00
|
|
|
lines := strings.Split(fb.String(), "\n")
|
2021-11-02 13:44:37 +00:00
|
|
|
if len(lines) < 1 {
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sd := SentryRequest{}
|
|
|
|
err = json.Unmarshal([]byte(lines[0]), &sd)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Warning("failed to parse sentry request")
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-07-26 09:33:35 +00:00
|
|
|
if sd.DSN != config.Get().ErrorReporting.DSN {
|
|
|
|
ws.log.WithField("have", sd.DSN).WithField("expected", config.Get().ErrorReporting.DSN).Debug("invalid DSN")
|
2021-11-02 13:44:37 +00:00
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-08-08 19:00:45 +00:00
|
|
|
res, err := http.DefaultClient.Post("https://sentry.beryju.org/api/8/envelope/", "application/octet-stream", fb)
|
2021-11-02 13:44:37 +00:00
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Warning("failed to proxy sentry")
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rw.WriteHeader(res.StatusCode)
|
|
|
|
}
|