2016-10-25 15:20:06 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-01-11 00:51:25 +00:00
|
|
|
"encoding/base64"
|
2016-10-25 15:20:06 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// KapacitorProxy proxies requests to kapacitor using the path query parameter.
|
|
|
|
func (h *Service) KapacitorProxy(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
srcID, err := paramID("id", r)
|
|
|
|
if err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := paramID("kid", r)
|
|
|
|
if err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
path := r.URL.Query().Get("path")
|
|
|
|
if path == "" {
|
2016-11-19 17:41:06 +00:00
|
|
|
Error(w, http.StatusUnprocessableEntity, "path query parameter required", h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
srv, err := h.ServersStore.Get(ctx, id)
|
|
|
|
if err != nil || srv.SrcID != srcID {
|
2016-11-19 17:41:06 +00:00
|
|
|
notFound(w, id, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(srv.URL)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Error parsing kapacitor url: %v", err)
|
2016-11-19 17:41:06 +00:00
|
|
|
Error(w, http.StatusUnprocessableEntity, msg, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Path = path
|
|
|
|
|
|
|
|
director := func(req *http.Request) {
|
|
|
|
req.URL = u
|
2017-01-11 00:51:25 +00:00
|
|
|
// Because we are acting as a proxy, kapacitor needs to have the basic auth information set as
|
|
|
|
// a header directly
|
|
|
|
if srv.Username != "" && srv.Password != "" {
|
|
|
|
auth := "Basic " + srv.Username + ":" + srv.Password
|
|
|
|
header := base64.StdEncoding.EncodeToString([]byte(auth))
|
|
|
|
req.Header.Set("Authorization", header)
|
|
|
|
}
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
proxy := &httputil.ReverseProxy{
|
|
|
|
Director: director,
|
|
|
|
}
|
|
|
|
proxy.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// KapacitorProxyPost proxies POST to kapacitor
|
|
|
|
func (h *Service) KapacitorProxyPost(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
h.KapacitorProxy(w, r)
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// KapacitorProxyPatch proxies PATCH to kapacitor
|
|
|
|
func (h *Service) KapacitorProxyPatch(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
h.KapacitorProxy(w, r)
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// KapacitorProxyGet proxies GET to kapacitor
|
|
|
|
func (h *Service) KapacitorProxyGet(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
h.KapacitorProxy(w, r)
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// KapacitorProxyDelete proxies DELETE to kapacitor
|
|
|
|
func (h *Service) KapacitorProxyDelete(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
h.KapacitorProxy(w, r)
|
|
|
|
}
|