forked from abutaha/aws-es-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-es-proxy.go
213 lines (176 loc) · 4.89 KB
/
aws-es-proxy.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
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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/signer/v4"
)
type proxy struct {
Scheme string
Host string
Region string
Service string
Verbose bool
Prettify bool
Signer *v4.Signer
}
func copyHeaders(dst, src http.Header) {
for k, vals := range src {
for _, v := range vals {
dst.Add(k, v)
}
}
}
func replaceBody(req *http.Request) []byte {
if req.Body == nil {
return []byte{}
}
payload, _ := ioutil.ReadAll(req.Body)
req.Body = ioutil.NopCloser(bytes.NewReader(payload))
return payload
}
func parseEndpoint(endpoint string, p *proxy) {
link, err := url.Parse(endpoint)
if err != nil {
log.Fatalf("ERROR: Failed parsing endpoint: %s\n", endpoint)
}
// Only http/https are supported schemes
scheme := func(x string) string {
switch x {
case "http", "https":
return x
}
return "https"
}
link.Scheme = scheme(link.Scheme)
// Unkown schemes sometimes result in empty host value
if link.Host == "" {
log.Fatalf("ERROR: Empty host information in submitted endpoint (%s)\n", endpoint)
}
// Extract region and service from link
parts := strings.Split(link.Host, ".")
var region, service string
if len(parts) == 5 {
region, service = parts[1], parts[2]
} else {
log.Fatalln("ERROR: Submitted endpoint is not a valid Amazon ElasticSearch Endpoint")
}
// Build proxy struct
p.Scheme = link.Scheme
p.Host = link.Host
p.Region = region
p.Service = service
}
func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requestStarted := time.Now()
dump, err := httputil.DumpRequest(r, true)
defer r.Body.Close()
respondError := func(err error) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
}
endpoint := *r.URL
endpoint.Host = p.Host
endpoint.Scheme = p.Scheme
req, err := http.NewRequest(r.Method, endpoint.String(), r.Body)
if err != nil {
respondError(err)
return
}
// Workaround for ES 5.1 and Kibana 5.1.1
if val, ok := r.Header["Kbn-Version"]; ok {
req.Header.Set("Kbn-Version", val[0])
}
// Sign the request with AWSv4
payload := bytes.NewReader(replaceBody(req))
p.Signer.Sign(req, payload, p.Service, p.Region, time.Now())
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
respondError(err)
return
}
defer resp.Body.Close()
// Write back received headers
copyHeaders(w.Header(), resp.Header)
buf := bytes.Buffer{}
if _, err := io.Copy(&buf, resp.Body); err != nil {
log.Fatal(err)
}
// Send response back
w.WriteHeader(resp.StatusCode)
w.Write(buf.Bytes())
// Log everything
remoteAddr := r.RemoteAddr
rawQuery := string(dump)
rawQuery = strings.Replace(rawQuery, "\n", " ", -1)
regex, _ := regexp.Compile("{.*}")
regEx, _ := regexp.Compile("_msearch|_bulk")
queryEx := regEx.FindString(rawQuery)
var query string
if len(queryEx) == 0 {
query = regex.FindString(rawQuery)
} else {
query = ""
}
if p.Verbose {
requestEnded := time.Since(requestStarted)
if p.Prettify {
var prettyBody bytes.Buffer
json.Indent(&prettyBody, []byte(query), "", " ")
t := time.Now()
fmt.Println()
fmt.Println("========================")
fmt.Println(t.Format("2006/01/02 15:04:05"))
fmt.Println("Remote Address: ", remoteAddr)
fmt.Println("Request URI: ", endpoint.RequestURI())
fmt.Println("Method: ", r.Method)
fmt.Println("Status: ", resp.StatusCode)
fmt.Printf("Took: %.3fs\n", requestEnded.Seconds())
fmt.Println("Body: ")
fmt.Println(string(prettyBody.Bytes()))
fmt.Println("========================")
} else {
log.Printf(" -> %s; %s; %s; %s; %d; %.3fs\n",
r.Method, remoteAddr, endpoint.RequestURI(), query, resp.StatusCode, requestEnded.Seconds())
}
}
}
func main() {
var endpoint, listenAddress string
var verbose bool
var prettify bool
// TODO: Use a more sophisticated args parser that can enforce arguments
flag.StringVar(&endpoint, "endpoint", "", "Amazon ElasticSearch Endpoint (e.g: https://dummy-host.eu-west-1.es.amazonaws.com)")
flag.StringVar(&listenAddress, "listen", "127.0.0.1:9200", "Local TCP port to listen on")
flag.BoolVar(&verbose, "verbose", false, "Print user requests")
flag.BoolVar(&prettify, "pretty", false, "Prettify verbose output")
flag.Parse()
if len(os.Args) < 3 {
fmt.Println("You need to specify Amazon ElasticSearch endpoint.")
fmt.Println("Please run with '-h' for a list of available arguments.")
os.Exit(1)
}
// Start AWS session from ENV, Shared Creds or EC2Role
sess, err := session.NewSession()
if err != nil {
log.Fatalln(err)
}
signer := v4.NewSigner(sess.Config.Credentials)
mux := &proxy{Verbose: verbose, Prettify: prettify, Signer: signer}
parseEndpoint(endpoint, mux)
fmt.Printf("Listening on %s\n", listenAddress)
log.Fatal(http.ListenAndServe(listenAddress, mux))
}