-
Notifications
You must be signed in to change notification settings - Fork 46
/
upload.go
344 lines (296 loc) · 10.2 KB
/
upload.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package upload
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httputil"
"regexp"
"sort"
"strconv"
"time"
"github.com/redhatinsights/insights-ingress-go/internal/announcers"
"github.com/redhatinsights/insights-ingress-go/internal/config"
l "github.com/redhatinsights/insights-ingress-go/internal/logger"
"github.com/redhatinsights/insights-ingress-go/internal/stage"
"github.com/redhatinsights/insights-ingress-go/internal/validators"
"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/redhatinsights/platform-go-middlewares/request_id"
"github.com/sirupsen/logrus"
)
type responseBody struct {
RequestID string `json:"request_id"`
Upload uploadData `json:"upload,omitempty"`
}
type uploadData struct {
Account string `json:"account_number,omitempty"`
OrgID string `json:"org_id,omitempty"`
}
// GetFile verifies that the proper upload field is in place and returns the file
func GetFile(r *http.Request, m int64) (multipart.File, *multipart.FileHeader, error) {
// Reduce memory usage of multipart form. This indicates of memory will be used
// before writting to disk. Default: 8MB
err := r.ParseMultipartForm(m)
if err != nil {
return nil, nil, fmt.Errorf("unable to parse form data: %v", err)
}
file, fileHeader, fileErr := r.FormFile("file")
if fileErr == nil {
return file, fileHeader, nil
}
file, fileHeader, uploadErr := r.FormFile("upload")
if uploadErr == nil {
return file, fileHeader, nil
}
keys := make([]string, 0, len(r.PostForm))
for name := range r.PostForm {
keys = append(keys, name)
}
sort.Strings(keys)
return nil, nil, fmt.Errorf("Unable to find file (%v) or upload (%v) parts in %v", fileErr, uploadErr, keys)
}
func readMetadataPart(r *http.Request) ([]byte, error) {
mdf, _, err := r.FormFile("metadata")
if err == nil {
defer mdf.Close()
return ioutil.ReadAll(mdf)
}
metadata := r.FormValue("metadata")
if metadata != "" {
return []byte(metadata), nil
}
return nil, errors.New("Failed to find metadata as a file or value")
}
// GetMetadata returns metadata content from a file or value part
func GetMetadata(r *http.Request) (*validators.Metadata, error) {
part, err := readMetadataPart(r)
if err != nil {
return nil, err
}
var md validators.Metadata
if err = json.Unmarshal(part, &md); err != nil {
return nil, err
}
md.Reporter = "ingress"
md.StaleTimestamp = time.Now().AddDate(0, 0, 30)
return &md, nil
}
// isTestRequest allows for two different test types from clients
// Current clients test using form data to the upload endpoint
// Legacy and satellite clients send a message body json of {"test": "test"}
// This function is meant to allow for both tests and use regex in the event that the
// json is sent differently in the message body depending on client version
func isTestRequest(r *http.Request) bool {
r.ParseForm()
if r.FormValue("test") == "test" {
return true
}
if r.Header.Get("Content-Type") == "application/json" {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
body := buf.String()
matched, _ := regexp.Match(`\{\s*\"test\"\s*\:\s*\"test\"\s*\}`, []byte(body))
if matched {
return true
}
}
return false
}
func createUploadResponse(vr *validators.Request) ([]byte, error) {
upload := uploadData{Account: vr.Account, OrgID: vr.OrgID}
response := responseBody{RequestID: vr.RequestID, Upload: upload}
jsonBody, err := json.Marshal(response)
return jsonBody, err
}
func handleTestRequest(reqID string, identity identity.Identity, w http.ResponseWriter, logerr func(msg string, err error)) {
mockVr := &validators.Request{
RequestID: reqID,
Account: identity.AccountNumber,
OrgID: identity.OrgID,
}
w.WriteHeader(http.StatusOK)
jsonBody, err := createUploadResponse(mockVr)
if err != nil {
logerr("Unable to marshal JSON response body", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(jsonBody)
}
// NewHandler returns a http handler configured with a Pipeline
func NewHandler(
stager stage.Stager,
validator validators.Validator,
tracker announcers.Announcer,
cfg config.IngressConfig) http.HandlerFunc {
isCustomerDenyListed := isRequestFromDenyListedOrgID(cfg)
return func(w http.ResponseWriter, r *http.Request) {
var id identity.XRHID
userAgent := r.Header.Get("User-Agent")
reqID := request_id.GetReqID(r.Context())
requestLogger := l.Log.WithFields(logrus.Fields{"request_id": reqID, "source_host": cfg.Hostname, "name": "ingress"})
logerr := func(msg string, err error) {
requestLogger.WithFields(logrus.Fields{"error": err}).Error(msg)
}
if cfg.Auth == true {
id = identity.Get(r.Context())
}
if cfg.Debug && cfg.DebugUserAgent.MatchString(userAgent) {
dumpBytes, err := httputil.DumpRequest(r, true)
if err != nil {
logerr("debug: failed to dump request", err)
} else {
requestLogger.WithFields(logrus.Fields{"raw_request": dumpBytes}).Info("dumping request")
}
}
if isTestRequest(r) {
handleTestRequest(reqID, id.Identity, w, logerr)
return
}
if id.Identity.OrgID == "" && id.Identity.Internal.OrgID != "" {
id.Identity.OrgID = id.Identity.Internal.OrgID
}
if isCustomerDenyListed(id) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Upload denied. Please contact Red Hat Support."))
requestLogger.WithFields(logrus.Fields{"account": id.Identity.AccountNumber, "org_id": id.Identity.OrgID}).Info("Upload rejected due to customer being denylisted")
return
}
incRequests(userAgent)
file, fileHeader, err := GetFile(r, cfg.MaxUploadMem)
if err != nil {
errString := "File or upload field not found"
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errString))
requestLogger.WithFields(logrus.Fields{"request_id": reqID, "status_code": http.StatusBadRequest, "account": id.Identity.AccountNumber, "org_id": id.Identity.OrgID}).Info(errString)
logerr("Invalid upload payload", err)
return
}
// If we exit early we need to make sure this gets closed
// later we will close this via the stageInput.close()
// in that case, this defer will return an error because
// the file is already closed.
defer file.Close()
contentType := fileHeader.Header.Get("Content-Type")
size := fileHeader.Size
observeSize(userAgent, size)
requestLogger = requestLogger.WithFields(logrus.Fields{"content-type": contentType, "size": size, "request_id": reqID, "account": id.Identity.AccountNumber, "org_id": id.Identity.OrgID})
requestLogger.Debug("ContentType received from client")
serviceDescriptor, validationErr := getServiceDescriptor(contentType)
if validationErr != nil {
logerr("Unable to validate", validationErr)
w.WriteHeader(http.StatusUnsupportedMediaType)
requestLogger.WithFields(logrus.Fields{"status_code": http.StatusUnsupportedMediaType}).Info("Unable to validate")
return
}
if err := validator.ValidateService(serviceDescriptor); err != nil {
requestLogger.WithFields(logrus.Fields{"status_code": http.StatusUnsupportedMediaType}).Info("Unrecognized Service")
logerr("Unrecognized service", err)
w.WriteHeader(http.StatusUnsupportedMediaType)
return
}
b64Identity := r.Header.Get("x-rh-identity")
vr := &validators.Request{
RequestID: reqID,
OrgID: id.Identity.OrgID,
Size: fileHeader.Size,
Service: serviceDescriptor.Service,
Category: serviceDescriptor.Category,
B64Identity: b64Identity,
ContentType: contentType,
}
var exceedsSizeLimit bool
if val, ok := cfg.MaxSizeMap[vr.Service]; ok {
fileSize, _ := strconv.ParseInt(val, 10, 64)
if fileHeader.Size > fileSize {
exceedsSizeLimit = true
}
} else if fileHeader.Size > cfg.DefaultMaxSize {
exceedsSizeLimit = true
} else {
exceedsSizeLimit = false
}
if exceedsSizeLimit {
requestLogger.WithFields(logrus.Fields{"status_code": http.StatusRequestEntityTooLarge}).Info(fmt.Sprintf("File exceeds maximum file size (%v) for upload: %v", cfg.DefaultMaxSize, fileHeader.Size))
w.WriteHeader(http.StatusRequestEntityTooLarge)
w.Write([]byte(fmt.Sprintf("File exceeds maximum file size (%v) for upload: %v", cfg.DefaultMaxSize, fileHeader.Size)))
return
}
if cfg.Auth == true {
vr.Account = id.Identity.AccountNumber
vr.Principal = id.Identity.OrgID
vr.OrgID = id.Identity.OrgID
requestLogger = requestLogger.WithFields(logrus.Fields{"account": vr.Account, "orgid": vr.OrgID})
}
md, err := GetMetadata(r)
if err != nil {
requestLogger.WithFields(logrus.Fields{"error": err}).Debug("Failed to read metadata")
} else {
vr.Metadata = *md
}
ps := &announcers.Status{
Account: vr.Account,
OrgID: vr.OrgID,
RequestID: reqID,
Status: "received",
StatusMsg: "Payload received by ingress",
}
requestLogger.Info("Payload received")
tracker.Status(ps)
stageInput := &stage.Input{
Payload: file,
Key: reqID,
Account: vr.Account,
OrgId: vr.OrgID,
Size: size,
}
start := time.Now()
url, err := stager.Stage(stageInput)
stageInput.Close()
observeStageElapsed(time.Since(start))
if err != nil {
logerr("Error staging", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
vr.URL = url
vr.Timestamp = time.Now()
ps = &announcers.Status{
Account: vr.Account,
OrgID: vr.OrgID,
RequestID: vr.RequestID,
Status: "success",
StatusMsg: fmt.Sprintf("Sent to validation service: %s", vr.Service),
}
requestLogger.WithFields(logrus.Fields{"service": vr.Service}).Info("Payload sent to validation service")
tracker.Status(ps)
validator.Validate(vr)
jsonBody, err := createUploadResponse(vr)
if err != nil {
logerr("Unable to marshal JSON response body", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
metadata, err := readMetadataPart(r)
if vr.Service == "advisor" && metadata == nil {
w.WriteHeader(http.StatusCreated)
} else {
w.WriteHeader(http.StatusAccepted)
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonBody)
}
}
func isRequestFromDenyListedOrgID(cfg config.IngressConfig) func(identity.XRHID) bool {
denyListedOrgIDs := make(map[string]bool)
for _, orgID := range cfg.DenyListedOrgIDs {
denyListedOrgIDs[orgID] = true
}
return func(id identity.XRHID) bool {
_, denyListed := denyListedOrgIDs[id.Identity.OrgID]
return denyListed
}
}