2016-10-25 15:20:06 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2017-02-09 20:35:38 +00:00
|
|
|
"github.com/NYTimes/gziphandler"
|
2016-10-25 15:20:06 +00:00
|
|
|
"github.com/bouk/httprouter"
|
2016-11-19 17:41:06 +00:00
|
|
|
"github.com/influxdata/chronograf" // When julienschmidt/httprouter v2 w/ context is out, switch
|
2017-02-14 21:14:24 +00:00
|
|
|
"github.com/influxdata/chronograf/oauth2"
|
2016-10-25 15:20:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// JSONType the mimetype for a json request
|
|
|
|
JSONType = "application/json"
|
|
|
|
)
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// MuxOpts are the options for the router. Mostly related to auth.
|
2016-10-25 15:20:06 +00:00
|
|
|
type MuxOpts struct {
|
|
|
|
Logger chronograf.Logger
|
2017-01-05 17:29:26 +00:00
|
|
|
Develop bool // Develop loads assets from filesystem instead of bindata
|
|
|
|
UseAuth bool // UseAuth turns on Github OAuth and JWT
|
|
|
|
TokenSecret string // TokenSecret is the JWT secret
|
|
|
|
GithubClientID string // GithubClientID is the GH OAuth id
|
|
|
|
GithubClientSecret string // GithubClientSecret is the GH OAuth secret
|
|
|
|
GithubOrgs []string // GithubOrgs is the list of organizations a user my be a member of
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// NewMux attaches all the route handlers; handler returned servers chronograf.
|
|
|
|
func NewMux(opts MuxOpts, service Service) http.Handler {
|
2016-10-25 15:20:06 +00:00
|
|
|
router := httprouter.New()
|
|
|
|
|
|
|
|
/* React Application */
|
|
|
|
assets := Assets(AssetsOpts{
|
|
|
|
Develop: opts.Develop,
|
|
|
|
Logger: opts.Logger,
|
|
|
|
})
|
2017-01-28 00:14:21 +00:00
|
|
|
|
|
|
|
// Prefix any URLs found in the React assets with any configured basepath
|
2017-01-28 00:24:51 +00:00
|
|
|
prefixedAssets := NewDefaultURLPrefixer(basepath, assets, opts.Logger)
|
2017-01-28 00:14:21 +00:00
|
|
|
|
2017-02-10 19:48:42 +00:00
|
|
|
// Compress the assets with gzip if an accepted encoding
|
|
|
|
compressed := gziphandler.GzipHandler(prefixedAssets)
|
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
// The react application handles all the routing if the server does not
|
|
|
|
// know about the route. This means that we never have unknown
|
|
|
|
// routes on the server.
|
2017-02-10 19:48:42 +00:00
|
|
|
router.NotFound = compressed
|
2016-10-25 15:20:06 +00:00
|
|
|
|
|
|
|
/* Documentation */
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/swagger.json", Spec())
|
|
|
|
router.GET("/docs", Redoc("/swagger.json"))
|
2016-10-25 15:20:06 +00:00
|
|
|
|
|
|
|
/* API */
|
|
|
|
// Root Routes returns all top-level routes in the API
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/chronograf/v1/", AllRoutes(opts.Logger))
|
2017-02-14 00:02:43 +00:00
|
|
|
router.GET("/chronograf/v1", AllRoutes(opts.Logger))
|
2016-10-25 15:20:06 +00:00
|
|
|
|
|
|
|
// Sources
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/chronograf/v1/sources", service.Sources)
|
|
|
|
router.POST("/chronograf/v1/sources", service.NewSource)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/chronograf/v1/sources/:id", service.SourcesID)
|
|
|
|
router.PATCH("/chronograf/v1/sources/:id", service.UpdateSource)
|
|
|
|
router.DELETE("/chronograf/v1/sources/:id", service.RemoveSource)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
|
|
|
// Source Proxy
|
2016-10-28 16:27:06 +00:00
|
|
|
router.POST("/chronograf/v1/sources/:id/proxy", service.Proxy)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
|
|
|
// Kapacitor
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/chronograf/v1/sources/:id/kapacitors", service.Kapacitors)
|
|
|
|
router.POST("/chronograf/v1/sources/:id/kapacitors", service.NewKapacitor)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/chronograf/v1/sources/:id/kapacitors/:kid", service.KapacitorsID)
|
|
|
|
router.PATCH("/chronograf/v1/sources/:id/kapacitors/:kid", service.UpdateKapacitor)
|
|
|
|
router.DELETE("/chronograf/v1/sources/:id/kapacitors/:kid", service.RemoveKapacitor)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
2016-11-04 06:53:54 +00:00
|
|
|
// Kapacitor rules
|
|
|
|
router.GET("/chronograf/v1/sources/:id/kapacitors/:kid/rules", service.KapacitorRulesGet)
|
|
|
|
router.POST("/chronograf/v1/sources/:id/kapacitors/:kid/rules", service.KapacitorRulesPost)
|
2016-11-03 06:42:52 +00:00
|
|
|
|
2016-11-04 06:53:54 +00:00
|
|
|
router.GET("/chronograf/v1/sources/:id/kapacitors/:kid/rules/:tid", service.KapacitorRulesID)
|
|
|
|
router.PUT("/chronograf/v1/sources/:id/kapacitors/:kid/rules/:tid", service.KapacitorRulesPut)
|
2017-02-10 19:48:42 +00:00
|
|
|
router.PATCH("/chronograf/v1/sources/:id/kapacitors/:kid/rules/:tid", service.KapacitorRulesStatus)
|
2016-11-04 06:53:54 +00:00
|
|
|
router.DELETE("/chronograf/v1/sources/:id/kapacitors/:kid/rules/:tid", service.KapacitorRulesDelete)
|
2016-10-31 23:11:05 +00:00
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
// Kapacitor Proxy
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/chronograf/v1/sources/:id/kapacitors/:kid/proxy", service.KapacitorProxyGet)
|
|
|
|
router.POST("/chronograf/v1/sources/:id/kapacitors/:kid/proxy", service.KapacitorProxyPost)
|
|
|
|
router.PATCH("/chronograf/v1/sources/:id/kapacitors/:kid/proxy", service.KapacitorProxyPatch)
|
|
|
|
router.DELETE("/chronograf/v1/sources/:id/kapacitors/:kid/proxy", service.KapacitorProxyDelete)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
|
|
|
// Mappings
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/chronograf/v1/mappings", service.GetMappings)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
|
|
|
// Layouts
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/chronograf/v1/layouts", service.Layouts)
|
|
|
|
router.POST("/chronograf/v1/layouts", service.NewLayout)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
router.GET("/chronograf/v1/layouts/:id", service.LayoutsID)
|
|
|
|
router.PUT("/chronograf/v1/layouts/:id", service.UpdateLayout)
|
|
|
|
router.DELETE("/chronograf/v1/layouts/:id", service.RemoveLayout)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
|
|
|
// Users
|
2016-11-17 23:57:46 +00:00
|
|
|
router.GET("/chronograf/v1/me", service.Me)
|
|
|
|
router.POST("/chronograf/v1/users", service.NewUser)
|
|
|
|
|
|
|
|
router.GET("/chronograf/v1/users/:id", service.UserID)
|
|
|
|
router.PATCH("/chronograf/v1/users/:id", service.UpdateUser)
|
|
|
|
router.DELETE("/chronograf/v1/users/:id", service.RemoveUser)
|
|
|
|
|
2016-12-07 23:18:04 +00:00
|
|
|
// Dashboards
|
|
|
|
router.GET("/chronograf/v1/dashboards", service.Dashboards)
|
|
|
|
router.POST("/chronograf/v1/dashboards", service.NewDashboard)
|
|
|
|
|
|
|
|
router.GET("/chronograf/v1/dashboards/:id", service.DashboardID)
|
2017-01-27 12:59:13 +00:00
|
|
|
router.DELETE("/chronograf/v1/dashboards/:id", service.RemoveDashboard)
|
|
|
|
router.PUT("/chronograf/v1/dashboards/:id", service.UpdateDashboard)
|
2016-12-07 23:18:04 +00:00
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
/* Authentication */
|
|
|
|
if opts.UseAuth {
|
|
|
|
auth := AuthAPI(opts, router)
|
|
|
|
return Logger(opts.Logger, auth)
|
|
|
|
}
|
2017-02-09 20:35:38 +00:00
|
|
|
|
2017-02-10 19:48:42 +00:00
|
|
|
logged := Logger(opts.Logger, router)
|
2017-02-09 20:35:38 +00:00
|
|
|
return logged
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// AuthAPI adds the OAuth routes if auth is enabled.
|
2016-10-25 15:20:06 +00:00
|
|
|
func AuthAPI(opts MuxOpts, router *httprouter.Router) http.Handler {
|
2017-02-14 21:14:24 +00:00
|
|
|
auth := oauth2.NewJWT(opts.TokenSecret)
|
2016-10-25 15:20:06 +00:00
|
|
|
|
2017-02-14 21:14:24 +00:00
|
|
|
gh := oauth2.NewGithub(
|
2016-10-25 15:20:06 +00:00
|
|
|
opts.GithubClientID,
|
|
|
|
opts.GithubClientSecret,
|
2017-01-05 17:29:26 +00:00
|
|
|
opts.GithubOrgs,
|
2016-10-25 15:20:06 +00:00
|
|
|
&auth,
|
|
|
|
opts.Logger,
|
|
|
|
)
|
|
|
|
|
2017-02-14 21:14:24 +00:00
|
|
|
ghMux := oauth2.NewJWTMux(&gh, &auth, opts.Logger)
|
|
|
|
router.Handler("GET", "/oauth/github/login", ghMux.Login())
|
|
|
|
router.Handler("GET", "/oauth/github/logout", ghMux.Logout())
|
|
|
|
router.Handler("GET", "/oauth/github/callback", ghMux.Callback())
|
2016-10-25 15:20:06 +00:00
|
|
|
|
2017-02-14 21:14:24 +00:00
|
|
|
tokenMiddleware := oauth2.AuthorizedToken(&auth, &oauth2.CookieExtractor{Name: "session"}, opts.Logger, router)
|
2016-10-25 15:20:06 +00:00
|
|
|
// Wrap the API with token validation middleware.
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-10-28 16:27:06 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/chronograf/v1/") {
|
2016-10-25 15:20:06 +00:00
|
|
|
tokenMiddleware.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2016-10-28 16:27:06 +00:00
|
|
|
router.ServeHTTP(w, r)
|
2016-10-25 15:20:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeJSON(w http.ResponseWriter, status int, v interface{}, logger chronograf.Logger) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
unknownErrorWithMessage(w, err, logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// Error writes an JSON message
|
2016-11-19 17:41:06 +00:00
|
|
|
func Error(w http.ResponseWriter, code int, msg string, logger chronograf.Logger) {
|
|
|
|
e := ErrorMessage{
|
2016-10-25 15:20:06 +00:00
|
|
|
Code: code,
|
|
|
|
Message: msg,
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
code = http.StatusInternalServerError
|
|
|
|
b = []byte(`{"code": 500, "message":"server_error"}`)
|
|
|
|
}
|
2016-11-19 17:41:06 +00:00
|
|
|
|
|
|
|
logger.
|
|
|
|
WithField("component", "server").
|
|
|
|
WithField("http_status ", code).
|
|
|
|
Error("Error message ", msg)
|
2016-10-25 15:20:06 +00:00
|
|
|
w.Header().Set("Content-Type", JSONType)
|
|
|
|
w.WriteHeader(code)
|
2016-12-20 20:59:56 +00:00
|
|
|
_, _ = w.Write(b)
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 17:41:06 +00:00
|
|
|
func invalidData(w http.ResponseWriter, err error, logger chronograf.Logger) {
|
|
|
|
Error(w, http.StatusUnprocessableEntity, fmt.Sprintf("%v", err), logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 17:41:06 +00:00
|
|
|
func invalidJSON(w http.ResponseWriter, logger chronograf.Logger) {
|
|
|
|
Error(w, http.StatusBadRequest, "Unparsable JSON", logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 17:41:06 +00:00
|
|
|
func unknownErrorWithMessage(w http.ResponseWriter, err error, logger chronograf.Logger) {
|
|
|
|
Error(w, http.StatusInternalServerError, fmt.Sprintf("Unknown error: %v", err), logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 17:41:06 +00:00
|
|
|
func notFound(w http.ResponseWriter, id int, logger chronograf.Logger) {
|
|
|
|
Error(w, http.StatusNotFound, fmt.Sprintf("ID %d not found", id), logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func paramID(key string, r *http.Request) (int, error) {
|
|
|
|
ctx := r.Context()
|
|
|
|
param := httprouter.GetParamFromContext(ctx, key)
|
|
|
|
id, err := strconv.Atoi(param)
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("Error converting ID %s", param)
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|