forked from abutaha/aws-es-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws-es-proxy.go
320 lines (263 loc) · 7.57 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
uuid "github.com/satori/go.uuid"
)
// set a default global logger
var logger = log.NewJSONLogger(os.Stderr)
type requestStruct struct {
Requestid string
Datetime string
Remoteaddr string
Requesturi string
Method string
Statuscode int
Elapsed float64
Body string
}
type responseStruct struct {
Requestid string
Body string
}
type proxy struct {
scheme string
host string
region string
service string
endpoint string
verbose bool
prettify bool
logtofile bool
nosignreq bool
fileRequest *os.File
fileResponse *os.File
credentials *credentials.Credentials
client *http.Client
}
func newProxy(args ...interface{}) *proxy {
return &proxy{
endpoint: args[0].(string),
verbose: args[1].(bool),
prettify: args[2].(bool),
logtofile: args[3].(bool),
nosignreq: args[4].(bool),
}
}
var client = &http.Client{
CheckRedirect: noRedirect,
Timeout: 300 * time.Second,
}
func noRedirect(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
func (p *proxy) parseEndpoint() error {
var link *url.URL
var err error
if link, err = url.Parse(p.endpoint); err != nil {
return fmt.Errorf("error: failure while parsing endpoint: %s. Error: %s",
p.endpoint, err.Error())
}
// Only http/https are supported schemes
switch link.Scheme {
case "http", "https":
default:
link.Scheme = "https"
}
// Unknown schemes sometimes result in empty host value
if link.Host == "" {
return fmt.Errorf("error: empty host or protocol information in submitted endpoint (%s)",
p.endpoint)
}
// AWS SignV4 enabled, extract required parts for signing process
if !p.nosignreq {
// Extract region and service from link
parts := strings.Split(link.Host, ".")
if len(parts) == 5 {
p.region, p.service = parts[1], parts[2]
} else {
return fmt.Errorf("error: submitted endpoint is not a valid Amazon ElasticSearch Endpoint")
}
}
// Update proxy struct
p.scheme = link.Scheme
p.host = link.Host
return nil
}
func (p *proxy) getSigner() *v4.Signer {
// Refresh credentials after expiration. Required for STS
if p.credentials == nil {
sess := session.Must(session.NewSession())
credentials := sess.Config.Credentials
p.credentials = credentials
level.Info(logger).Log("msg", "session expired, generated fresh aws credentials object")
}
return v4.NewSigner(p.credentials)
}
func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requestStarted := time.Now()
dump, err := httputil.DumpRequest(r, true)
if err != nil {
level.Error(logger).Log("msg", "error while dumping request", "err", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer r.Body.Close()
ep := *r.URL
ep.Host = p.host
ep.Scheme = p.scheme
req, err := http.NewRequest(r.Method, ep.String(), r.Body)
if err != nil {
level.Error(logger).Log("msg", "error creating new request. ", "err", err.Error())
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
addHeaders(r.Header, req.Header)
// Make signV4 optional
if !p.nosignreq {
// Start AWS session from ENV, Shared Creds or EC2Role
signer := p.getSigner()
// Sign the request with AWSv4
payload := bytes.NewReader(replaceBody(req))
signer.Sign(req, payload, p.service, p.region, time.Now())
}
resp, err := client.Do(req)
if err != nil {
level.Error(logger).Log("msg", "BadGateway", "err", err.Error(), "request", req.URL.String())
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
if !p.nosignreq {
// AWS credentials expired, need to generate fresh ones
if resp.StatusCode == 403 {
p.credentials = nil
return
}
}
defer resp.Body.Close()
// Write back headers to requesting client
copyHeaders(w.Header(), resp.Header)
// Send response back to requesting client
body := bytes.Buffer{}
if _, err := io.Copy(&body, resp.Body); err != nil {
level.Error(logger).Log("msg", "something went wrong", "err", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.WriteHeader(resp.StatusCode)
w.Write(body.Bytes())
requestEnded := time.Since(requestStarted)
/*############################
## Logging
############################*/
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 {
level.Info(logger).Log("method", r.Method, "remoteAddress", r.RemoteAddr, "requestUri", ep.RequestURI(), "query", query, "status", resp.StatusCode, "timeElapsed", requestEnded.Seconds())
}
}
// Recent versions of ES/Kibana require
// "kbn-version" and "content-type: application/json"
// headers to exist in the request.
// If missing requests fails.
func addHeaders(src, dest http.Header) {
if val, ok := src["Kbn-Version"]; ok {
dest.Add("Kbn-Version", val[0])
}
if val, ok := src["Content-Type"]; ok {
dest.Add("Content-Type", val[0])
}
}
// Signer.Sign requires a "seekable" body to sum body's sha256
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 copyHeaders(dst, src http.Header) {
for k, vals := range src {
for _, v := range vals {
dst.Add(k, v)
}
}
}
func init() {
// ensure global logger uses UTC timestamps
logger = log.With(logger, "timestamp", log.DefaultTimestampUTC)
}
func main() {
var (
verbose bool
prettify bool
logtofile bool
nosignreq bool
endpoint string
listenAddress string
fileRequest *os.File
fileResponse *os.File
err error
)
flag.StringVar(&endpoint, "endpoint", "http://localhost", "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", true, "Print user requests")
flag.BoolVar(&nosignreq, "no-sign-reqs", false, "Disable AWS Signature v4")
flag.Parse()
p := newProxy(
endpoint,
verbose,
prettify,
logtofile,
nosignreq,
)
if err = p.parseEndpoint(); err != nil {
level.Error(logger).Log("msg", err)
os.Exit(1)
}
if p.logtofile {
u1, _ := uuid.NewV4()
u2, _ := uuid.NewV4()
requestFname := fmt.Sprintf("request-%s.log", u1.String())
responseFname := fmt.Sprintf("response-%s.log", u2.String())
if fileRequest, err = os.Create(requestFname); err != nil {
level.Error(logger).Log("msg", err.Error())
os.Exit(1) // go-kit/log doesn't have a level.Fatal so we're exiting with os.Exit(1)
}
if fileResponse, err = os.Create(responseFname); err != nil {
level.Error(logger).Log("msg", err.Error())
os.Exit(1) // go-kit/log doesn't have a level.Fatal so we're exiting with os.Exit(1)
}
defer fileRequest.Close()
defer fileResponse.Close()
p.fileRequest = fileRequest
p.fileResponse = fileResponse
}
level.Info(logger).Log("status", "starting")
level.Info(logger).Log("status", "started", "listening", listenAddress)
level.Error(logger).Log("status", "failed", "err", http.ListenAndServe(listenAddress, p))
}