File size: 10,017 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
package app
import (
"context"
"net/http"
"net/url"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/target/goalert/config"
"github.com/target/goalert/expflag"
"github.com/target/goalert/genericapi"
"github.com/target/goalert/grafana"
"github.com/target/goalert/mailgun"
"github.com/target/goalert/notification/twilio"
"github.com/target/goalert/permission"
prometheus "github.com/target/goalert/prometheusalertmanager"
"github.com/target/goalert/site24x7"
"github.com/target/goalert/util/errutil"
"github.com/target/goalert/util/log"
"github.com/target/goalert/web"
)
func (app *App) initHTTP(ctx context.Context) error {
middleware := []func(http.Handler) http.Handler{
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
next.ServeHTTP(w, req.WithContext(app.Context(req.Context())))
})
},
withSecureHeaders(app.cfg.EnableSecureHeaders, strings.HasPrefix(app.cfg.PublicURL, "https://")),
config.ShortURLMiddleware,
// redirect http to https if public URL is https
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fwdProto := req.Header.Get("x-forwarded-proto")
if fwdProto != "" {
req.URL.Scheme = fwdProto
} else if req.URL.Scheme == "" {
if req.TLS == nil {
req.URL.Scheme = "http"
} else {
req.URL.Scheme = "https"
}
}
req.URL.Host = req.Host
cfg := config.FromContext(req.Context())
if app.cfg.DisableHTTPSRedirect || cfg.ValidReferer(req.URL.String(), req.URL.String()) {
next.ServeHTTP(w, req)
return
}
u, err := url.ParseRequestURI(req.RequestURI)
if errutil.HTTPError(req.Context(), w, err) {
return
}
u.Scheme = "https"
u.Host = req.Host
if cfg.ValidReferer(req.URL.String(), u.String()) {
http.Redirect(w, req, u.String(), http.StatusTemporaryRedirect)
return
}
next.ServeHTTP(w, req)
})
},
// limit auth check counts (fail-safe for loops or DB access)
authCheckLimit(100),
// request logging
logRequest(app.cfg.LogRequests),
// max request time
timeout(2 * time.Minute),
func(next http.Handler) http.Handler {
return http.StripPrefix(app.cfg.HTTPPrefix, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "" {
req.URL.Path = "/"
}
next.ServeHTTP(w, req)
}))
},
// limit max request size
maxBodySizeMiddleware(app.cfg.MaxReqBodyBytes),
// authenticate requests
app.AuthHandler.WrapHandler,
// add auth info to request logs
logRequestAuth,
LimitConcurrencyByAuthSource,
wrapGzip,
}
if app.cfg.Verbose {
middleware = append(middleware, func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
next.ServeHTTP(w, req.WithContext(log.WithDebug(req.Context())))
})
})
}
mux := http.NewServeMux()
generic := genericapi.NewHandler(genericapi.Config{
AlertStore: app.AlertStore,
IntegrationKeyStore: app.IntegrationKeyStore,
HeartbeatStore: app.HeartbeatStore,
UserStore: app.UserStore,
})
mux.Handle("POST /api/graphql", app.graphql2.Handler())
mux.HandleFunc("GET /api/v2/config", app.ConfigStore.ServeConfig)
mux.HandleFunc("PUT /api/v2/config", app.ConfigStore.ServeConfig)
mux.HandleFunc("GET /api/v2/identity/providers", app.AuthHandler.ServeProviders)
mux.HandleFunc("POST /api/v2/identity/logout", app.AuthHandler.ServeLogout)
basicAuth := app.AuthHandler.IdentityProviderHandler("basic")
mux.HandleFunc("POST /api/v2/identity/providers/basic", basicAuth)
githubAuth := app.AuthHandler.IdentityProviderHandler("github")
mux.HandleFunc("POST /api/v2/identity/providers/github", githubAuth)
mux.HandleFunc("GET /api/v2/identity/providers/github/callback", githubAuth)
oidcAuth := app.AuthHandler.IdentityProviderHandler("oidc")
mux.HandleFunc("POST /api/v2/identity/providers/oidc", oidcAuth)
mux.HandleFunc("GET /api/v2/identity/providers/oidc/callback", oidcAuth)
if expflag.ContextHas(ctx, expflag.UnivKeys) {
mux.HandleFunc("POST /api/v2/uik", app.UIKHandler.ServeHTTP)
}
mux.HandleFunc("POST /api/v2/mailgun/incoming", mailgun.IngressWebhooks(app.AlertStore, app.IntegrationKeyStore))
mux.HandleFunc("POST /api/v2/grafana/incoming", grafana.GrafanaToEventsAPI(app.AlertStore, app.IntegrationKeyStore))
mux.HandleFunc("POST /api/v2/site24x7/incoming", site24x7.Site24x7ToEventsAPI(app.AlertStore, app.IntegrationKeyStore))
mux.HandleFunc("POST /api/v2/prometheusalertmanager/incoming", prometheus.PrometheusAlertmanagerEventsAPI(app.AlertStore, app.IntegrationKeyStore))
mux.HandleFunc("POST /api/v2/generic/incoming", generic.ServeCreateAlert)
mux.HandleFunc("POST /api/v2/heartbeat/{heartbeatID}", generic.ServeHeartbeatCheck)
mux.HandleFunc("GET /api/v2/user-avatar/{userID}", generic.ServeUserAvatar)
mux.HandleFunc("GET /api/v2/calendar", app.CalSubStore.ServeICalData)
mux.HandleFunc("POST /api/v2/twilio/message", app.twilioSMS.ServeMessage)
mux.HandleFunc("POST /api/v2/twilio/message/status", app.twilioSMS.ServeStatusCallback)
mux.HandleFunc("POST /api/v2/twilio/call", app.twilioVoice.ServeCall)
mux.HandleFunc("POST /api/v2/twilio/call/status", app.twilioVoice.ServeStatusCallback)
mux.HandleFunc("POST /api/v2/slack/message-action", app.slackChan.ServeMessageAction)
middleware = append(middleware,
httpRewrite(app.cfg.HTTPPrefix, "/v1/graphql2", "/api/graphql"),
httpRedirect(app.cfg.HTTPPrefix, "/v1/graphql2/explore", "/api/graphql/explore"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/config", "/api/v2/config"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/identity/providers", "/api/v2/identity/providers"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/identity/providers/", "/api/v2/identity/providers/"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/identity/logout", "/api/v2/identity/logout"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/webhooks/mailgun", "/api/v2/mailgun/incoming"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/webhooks/grafana", "/api/v2/grafana/incoming"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/api/alerts", "/api/v2/generic/incoming"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/api/heartbeat/", "/api/v2/heartbeat/"),
httpRewriteWith(app.cfg.HTTPPrefix, "/v1/api/users/", func(req *http.Request) *http.Request {
parts := strings.Split(strings.TrimSuffix(req.URL.Path, "/avatar"), "/")
req.URL.Path = "/api/v2/user-avatar/" + parts[len(parts)-1]
return req
}),
httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/sms/messages", "/api/v2/twilio/message"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/sms/status", "/api/v2/twilio/message/status"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/call", "/api/v2/twilio/call?type=alert"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/alert-status", "/api/v2/twilio/call?type=alert-status"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/test", "/api/v2/twilio/call?type=test"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/stop", "/api/v2/twilio/call?type=stop"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/verify", "/api/v2/twilio/call?type=verify"),
httpRewrite(app.cfg.HTTPPrefix, "/v1/twilio/voice/status", "/api/v2/twilio/call/status"),
func(next http.Handler) http.Handler {
twilioHandler := twilio.WrapValidation(
// go back to the regular mux after validation
twilio.WrapHeaderHack(next),
*app.twilioConfig,
)
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if strings.HasPrefix(req.URL.Path, "/api/v2/twilio/") {
twilioHandler.ServeHTTP(w, req)
return
}
next.ServeHTTP(w, req)
})
},
)
mux.HandleFunc("GET /health", app.healthCheck)
mux.HandleFunc("GET /health/engine", app.engineStatus)
mux.HandleFunc("GET /health/engine/cycle", app.engineCycle)
mux.Handle("GET /health/", http.NotFoundHandler())
webH, err := web.NewHandler(app.cfg.UIDir, app.cfg.HTTPPrefix)
if err != nil {
return err
}
// This is necessary so that we can return 404 for invalid/unknown API routes, otherwise it will get caught by the UI handler and incorrectly return the index.html or a 405 (Method Not Allowed) error.
mux.Handle("GET /api/", http.NotFoundHandler())
mux.Handle("POST /api/", http.NotFoundHandler())
mux.Handle("GET /v1/", http.NotFoundHandler())
mux.Handle("POST /v1/", http.NotFoundHandler())
// non-API/404s go to UI handler and return index.html
mux.Handle("GET /", webH)
mux.Handle("GET /api/graphql/explore", webH)
mux.Handle("GET /api/graphql/explore/", webH)
mux.HandleFunc("GET /admin/riverui/", func(w http.ResponseWriter, r *http.Request) {
err := permission.LimitCheckAny(r.Context(), permission.Admin)
if permission.IsUnauthorized(err) {
// render login since we're on a UI route
webH.ServeHTTP(w, r)
return
}
if errutil.HTTPError(r.Context(), w, err) {
return
}
app.RiverUI.ServeHTTP(w, r)
})
mux.HandleFunc("POST /admin/riverui/api/", func(w http.ResponseWriter, r *http.Request) {
err := permission.LimitCheckAny(r.Context(), permission.Admin)
if errutil.HTTPError(r.Context(), w, err) {
return
}
app.RiverUI.ServeHTTP(w, r)
})
app.srv = &http.Server{
Handler: applyMiddleware(mux, middleware...),
ReadHeaderTimeout: time.Second * 30,
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
IdleTimeout: time.Minute * 2,
MaxHeaderBytes: app.cfg.MaxReqHeaderBytes,
}
app.srv.Handler = promhttp.InstrumentHandlerInFlight(metricReqInFlight, app.srv.Handler)
app.srv.Handler = promhttp.InstrumentHandlerCounter(metricReqTotal, app.srv.Handler)
// Ingress/load balancer/proxy can do a keep-alive, backend doesn't need it.
// It also makes zero downtime deploys nearly impossible; an idle connection
// could have an in-flight request when the server closes it.
app.srv.SetKeepAlivesEnabled(false)
return nil
}
|