-
Notifications
You must be signed in to change notification settings - Fork 0
/
httphandler.go
110 lines (96 loc) · 2.64 KB
/
httphandler.go
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
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
var allowedMethods = map[string]bool{
"HEAD": true,
}
var allowedPostPaths = map[string]bool{
"/_mget": true,
"/_msearch": true,
"/.kibana/_search": true,
"/.kibana/_msearch": true,
"/.kibana/_mget": true,
}
var allowedPutPaths = map[string]bool{
"/_template/kibana_index_template:.kibana": true,
}
// Place the paths that Kibana frequently polls at the top.
var allowedGetPathPrefixes = []string{
"/_cluster/",
"/.kibana/",
"/_nodes",
"/_mget",
"/_msearch",
"/cloudstats/",
}
// ElasticProxy forwards received HTTP calls to another HTTP server.
type ElasticProxy struct {
url *url.URL
proxy *httputil.ReverseProxy
}
// CreateElasticProxy returns a new elasticProxy object.
func CreateElasticProxy(elasticURL *url.URL) *ElasticProxy {
return &ElasticProxy{
url: elasticURL,
proxy: httputil.NewSingleHostReverseProxy(elasticURL),
}
}
func allowedGetPrefix(path string) bool {
for _, prefix := range allowedGetPathPrefixes {
if strings.HasPrefix(path, prefix) {
return true
}
}
return false
}
// ServeHTTP only forwards allowed requests to the real ElasticSearch server.
func (ep *ElasticProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
status := 0
fields := log.Fields{
"remote_addr": r.RemoteAddr,
"method": r.Method,
"path": r.URL.Path,
}
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
fields["x_forwarded_for"] = xff
}
startTime := time.Now().UTC()
defer func() {
endTime := time.Now().UTC()
fields["duration"] = endTime.Sub(startTime)
if status == 0 {
log.WithFields(fields).Info("Request proxied")
} else {
fields["status"] = status
log.WithFields(fields).Warning("Request blocked")
}
}()
if r.Method == "GET" && allowedGetPrefix(r.URL.Path) {
log.WithFields(fields).Debug("Allowing GET request")
} else if r.Method == "POST" && allowedPostPaths[r.URL.Path] {
log.WithFields(fields).Debug("Allowing POST request")
} else if r.Method == "PUT" && allowedPutPaths[r.URL.Path] {
log.WithFields(fields).Debug("Allowing PUT request")
} else if !allowedMethods[r.Method] {
status = http.StatusMethodNotAllowed
w.WriteHeader(status)
return
}
if r.Header.Get("Upgrade") == "websocket" {
log.WithFields(fields).Warning("Upgrade to websocket blocked")
status = http.StatusNotImplemented
w.WriteHeader(status)
fmt.Fprintln(w, "Websockets not supported")
return
}
// All our checks were fine, so now we can defer to the ReverseProxy to do the actual work.
r.Header.Set("Host", ep.url.Host)
ep.proxy.ServeHTTP(w, r)
}