package web
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"goauthentik.io/internal/config"
)
type SentryRequest struct {
DSN string `json:"dsn"`
}
func (ws *WebServer) APISentryProxy(rw http.ResponseWriter, r *http.Request) {
if !config.Get().ErrorReporting.Enabled {
ws.log.Debug("error reporting disabled")
rw.WriteHeader(http.StatusBadRequest)
return
fullBody, err := ioutil.ReadAll(r.Body)
if err != nil {
ws.log.Debug("failed to read body")
lines := strings.Split(string(fullBody), "\n")
if len(lines) < 1 {
sd := SentryRequest{}
err = json.Unmarshal([]byte(lines[0]), &sd)
ws.log.WithError(err).Warning("failed to parse sentry request")
if sd.DSN != config.Get().ErrorReporting.DSN {
ws.log.WithField("have", sd.DSN).WithField("expected", config.Get().ErrorReporting.DSN).Debug("invalid DSN")
res, err := http.DefaultClient.Post("https://sentry.beryju.org/api/8/envelope/", "application/octet-stream", strings.NewReader(string(fullBody)))
ws.log.WithError(err).Warning("failed to proxy sentry")
rw.WriteHeader(res.StatusCode)