-
Notifications
You must be signed in to change notification settings - Fork 40
/
storage.go
544 lines (492 loc) · 20 KB
/
storage.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// FIXME: golangci-lint
// nolint:gocritic,govet,revive
package routes
import (
"context"
"fmt"
"io"
"net/http"
url2 "net/url"
"strconv"
"strings"
"github.com/redhatinsights/edge-api/pkg/db"
"github.com/redhatinsights/edge-api/pkg/models"
"github.com/redhatinsights/edge-api/config"
"github.com/go-chi/chi/v5"
"github.com/redhatinsights/edge-api/pkg/dependencies"
"github.com/redhatinsights/edge-api/pkg/errors"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type installerTypeKey string
type updateTransactionTypeKey string
type storageImageTypeKey string
const installerKey installerTypeKey = "installer_key"
const updateTransactionKey updateTransactionTypeKey = "update_transaction_key"
const storageImageKey storageImageTypeKey = "storage_image_key"
func setContextInstaller(ctx context.Context, installer *models.Installer) context.Context {
return context.WithValue(ctx, installerKey, installer)
}
func setContextUpdateTransaction(ctx context.Context, installer *models.UpdateTransaction) context.Context {
return context.WithValue(ctx, updateTransactionKey, installer)
}
func setContextStorageImage(ctx context.Context, image *models.Image) context.Context {
return context.WithValue(ctx, storageImageKey, image)
}
// MakeStorageRouter adds support for external storage
func MakeStorageRouter(sub chi.Router) {
sub.Route("/isos/{installerID}", func(r chi.Router) {
r.Use(InstallerByIDCtx)
r.Get("/", GetInstallerIsoStorageContent)
})
sub.Route("/update-repos/{updateID}", func(r chi.Router) {
r.Use(UpdateTransactionCtx)
r.Get("/content/*", GetUpdateTransactionRepoFileContent)
r.Get("/*", GetUpdateTransactionRepoFile)
})
sub.Route("/images-repos/{imageID}", func(r chi.Router) {
r.Use(storageImageCtx)
r.Get("/content/*", GetImageRepoFileContent)
r.Get("/*", GetImageRepoFile)
})
}
// redirectToStorageSignedURL redirect request to real content storage url using a signed url
func redirectToStorageSignedURL(w http.ResponseWriter, r *http.Request, path string) {
ctxServices := dependencies.ServicesFromContext(r.Context())
logContext := ctxServices.Log.WithField("service", "device-repository-storage")
signedURL, err := ctxServices.FilesService.GetSignedURL(path)
if err != nil {
logContext.WithFields(log.Fields{
"error": err.Error(),
path: path,
}).Error("error occurred when signing url")
respondWithAPIError(w, ctxServices.Log, errors.NewInternalServerError())
return
}
logContext.WithField("path", signedURL).Debug("redirect")
http.Redirect(w, r, signedURL, http.StatusSeeOther)
}
// serveStorageContent return the real content from storage
func serveStorageContent(w http.ResponseWriter, r *http.Request, path string) {
ctxServices := dependencies.ServicesFromContext(r.Context())
logContext := ctxServices.Log.WithField("service", "device-repository-storage")
requestFile, err := ctxServices.FilesService.GetFile(path)
if err != nil {
logContext.WithFields(log.Fields{
"error": err.Error(),
}).Error("error occurred when getting file from request path")
var apiError errors.APIError
if strings.Contains(err.Error(), "was not found on the S3 bucket") {
apiError = errors.NewNotFound(fmt.Sprintf("file '%s' was not found", path))
} else {
apiError = errors.NewInternalServerError()
}
respondWithAPIError(w, ctxServices.Log, apiError)
return
}
defer func(requestFile io.ReadCloser) {
err := requestFile.Close()
if err != nil {
logContext.WithFields(log.Fields{"path": path, "error": err.Error()}).Error("error closing request file")
}
}(requestFile)
w.Header().Set("Content-Type", "application/octet-stream; charset=binary")
w.WriteHeader(http.StatusOK)
if ind, err := io.Copy(w, requestFile); err != nil {
logContext.WithField("error", err.Error()).
WithField("Content-Type", w.Header().Values("Content-Type")).
WithField("len-content", ind).Error("error writing content")
}
}
// InstallerByIDCtx is a handler for Installer ISOs requests
func InstallerByIDCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctxServices := dependencies.ServicesFromContext(r.Context())
installerIDString := chi.URLParam(r, "installerID")
if installerIDString == "" {
ctxServices.Log.Debug("Installer ID was not passed to the request or it was empty")
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("installer ID required"))
return
}
installerID, err := strconv.Atoi(installerIDString)
if err != nil {
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("installer id must be an integer"))
return
}
orgID := readOrgID(w, r, ctxServices.Log)
if orgID == "" {
return
}
var installer models.Installer
if result := db.Org(orgID, "").First(&installer, installerID); result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
respondWithAPIError(w, ctxServices.Log, errors.NewNotFound("installer not found"))
return
}
respondWithAPIError(w, ctxServices.Log, errors.NewInternalServerError())
return
}
if installer.OrgID != orgID {
ctxServices.Log.WithFields(log.Fields{
"org_id": orgID,
}).Error("installer doesn't belong to org_id")
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("installer doesn't belong to org_id"))
return
}
ctx := setContextInstaller(r.Context(), &installer)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func getContextInstaller(w http.ResponseWriter, r *http.Request) *models.Installer {
ctx := r.Context()
installer, ok := ctx.Value(installerKey).(*models.Installer)
if !ok {
ctxServices := dependencies.ServicesFromContext(ctx)
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("Failed getting installer from context"))
return nil
}
return installer
}
// GetInstallerIsoStorageContent redirect to a signed installer iso url
// @Summary Redirect to a signed installer
// @ID RedirectSignedInstaller
// @Description This method will redirect request to a signed installer iso url
// @Tags Storage
// @Accept json
// @Produce octet-stream
// @Param installerID path string true "Installer ID"
// @Success 303 {string} string "URL to redirect"
// @Failure 400 {object} errors.BadRequest "The request send couln't be processed."
// @Failure 404 {object} errors.NotFound "installer not found."
// @Failure 500 {object} errors.InternalServerError
// @Router /storage/isos/{installerID}/ [get]
func GetInstallerIsoStorageContent(w http.ResponseWriter, r *http.Request) {
ctxServices := dependencies.ServicesFromContext(r.Context())
installer := getContextInstaller(w, r)
if installer == nil {
return
}
if installer.ImageBuildISOURL == "" {
respondWithAPIError(w, ctxServices.Log, errors.NewNotFound("empty installer iso url"))
return
}
url, err := url2.Parse(installer.ImageBuildISOURL)
if err != nil {
ctxServices.Log.WithFields(log.Fields{
"error": err.Error(),
"URL": installer.ImageBuildISOURL,
}).Error("error occurred when parsing url")
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("bad installer iso url"))
return
}
signedURL, err := ctxServices.FilesService.GetSignedURL(url.Path)
if err != nil {
ctxServices.Log.WithField("error", err.Error()).Error("error occurred when signing url")
respondWithAPIError(w, ctxServices.Log, errors.NewInternalServerError())
return
}
http.Redirect(w, r, signedURL, http.StatusSeeOther)
}
// UpdateTransactionCtx is a handler for Update transaction requests
func UpdateTransactionCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctxServices := dependencies.ServicesFromContext(r.Context())
orgID := readOrgID(w, r, ctxServices.Log)
if orgID == "" {
// readOrgID handle response and logging on failure
return
}
updateIDString := chi.URLParam(r, "updateID")
if updateIDString == "" {
ctxServices.Log.Debug("Update transaction ID was not passed to the request or it was empty")
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("update transaction ID required"))
return
}
updateTransactionID, err := strconv.Atoi(updateIDString)
if err != nil {
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("update transaction id must be an integer"))
return
}
var updateTransaction models.UpdateTransaction
if result := db.Org(orgID, "").Preload("Repo").First(&updateTransaction, updateTransactionID); result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
ctxServices.Log.WithField("error", result.Error.Error()).Error("device update transaction not found")
respondWithAPIError(w, ctxServices.Log, errors.NewNotFound("device update transaction not found"))
return
}
ctxServices.Log.WithField("error", result.Error.Error()).Error("failed to retrieve update transaction")
respondWithAPIError(w, ctxServices.Log, errors.NewInternalServerError())
return
}
ctx := setContextUpdateTransaction(r.Context(), &updateTransaction)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func getContextStorageUpdateTransaction(w http.ResponseWriter, r *http.Request) *models.UpdateTransaction {
ctx := r.Context()
updateTransaction, ok := ctx.Value(updateTransactionKey).(*models.UpdateTransaction)
if !ok {
ctxServices := dependencies.ServicesFromContext(ctx)
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("Failed getting update transaction from context"))
return nil
}
return updateTransaction
}
// ValidateStorageUpdateTransaction validate storage update transaction and return the request path
func ValidateStorageUpdateTransaction(w http.ResponseWriter, r *http.Request) string {
ctxServices := dependencies.ServicesFromContext(r.Context())
updateTransaction := getContextStorageUpdateTransaction(w, r)
if updateTransaction == nil {
return ""
}
logContext := ctxServices.Log.WithFields(log.Fields{
"service": "device-repository-storage",
"orgID": updateTransaction.OrgID,
"updateTransactionID": updateTransaction.ID,
})
filePath := chi.URLParam(r, "*")
if filePath == "" {
logContext.Error("target repository file path is missing")
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("target repository file path is missing"))
return ""
}
if updateTransaction.Repo == nil || updateTransaction.Repo.URL == "" {
logContext.Error("update transaction repository does not exist")
respondWithAPIError(w, logContext, errors.NewNotFound("update transaction repository does not exist"))
return ""
}
RepoURL, err := url2.Parse(updateTransaction.Repo.URL)
if err != nil {
logContext.WithFields(log.Fields{
"error": err.Error(),
"URL": updateTransaction.Repo.URL,
}).Error("error occurred when parsing repository url")
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("bad update transaction repository url"))
return ""
}
requestPath := fmt.Sprintf(RepoURL.Path + "/" + filePath)
return requestPath
}
// GetUpdateTransactionRepoFileContent redirect to a signed url of an update-transaction repository path content
// @Summary redirect to a signed url of an update-transaction repository path content
// @ID RedirectUpdateTransactionRepositoryPath
// @Description Method will redirect to asigned url of an update-transaction based on repository content
// @Tags Storage
// @Accept json
// @Produce octet-stream
// @Param updateTransactionID path int true "id for update transaction id"
// @Param repoFilePath path string true "path to repository to be checked"
// @Success 303 {string} string "URL signed to be redirect"
// @Failure 400 {object} errors.BadRequest
// @Failure 404 {object} errors.NotFound
// @Failure 500 {object} errors.InternalServerError
// @Router /storage/update-repos/{updateTransactionID}/content/{repoFilePath} [get]
func GetUpdateTransactionRepoFileContent(w http.ResponseWriter, r *http.Request) {
ctxServices := dependencies.ServicesFromContext(r.Context())
logContext := ctxServices.Log.WithField("service", "device-repository-storage")
updateTransaction := getContextStorageUpdateTransaction(w, r)
if updateTransaction == nil {
return
}
requestPath := ValidateStorageUpdateTransaction(w, r)
if requestPath == "" {
// ValidateStorageUpdateTransaction will handle errors
return
}
logContext.WithFields(log.Fields{
"orgID": updateTransaction.OrgID,
"updateTransactionID": updateTransaction.ID,
"path": requestPath,
}).Debug("redirect storage update transaction repo resource")
redirectToStorageSignedURL(w, r, requestPath)
}
// GetUpdateTransactionRepoFile return the content of an update-transaction repository path
// @Summary Return the content od an update-transaction repository path
// @ID RedirectUpdateTransactionRepositoryContent
// @Description Request will get access to content of an update-transaction file based on the path
// @Tags Storage
// @Accept json
// @Produce octet-stream
// @Param updateTransactionID path int true "Update Transaction Id"
// @Param repoFilePath path int true "path for repository file"
// @Success 200 {string} string "Stream object from file content"
// @Failure 400 {object} errors.BadRequest
// @Failure 404 {object} errors.NotFound
// @Failure 500 {object} errors.InternalServerError
// @Router /storage/update-repos/{updateTransactionID}/{repoFilePath} [get]
func GetUpdateTransactionRepoFile(w http.ResponseWriter, r *http.Request) {
ctxServices := dependencies.ServicesFromContext(r.Context())
logContext := ctxServices.Log.WithField("service", "device-repository-storage")
updateTransaction := getContextStorageUpdateTransaction(w, r)
if updateTransaction == nil {
return
}
requestPath := ValidateStorageUpdateTransaction(w, r)
if requestPath == "" {
// ValidateStorageUpdateTransaction will handle errors
return
}
logContext = logContext.WithFields(log.Fields{
"orgID": updateTransaction.OrgID,
"updateTransactionID": updateTransaction.ID,
"path": requestPath,
})
logContext.Debug("return storage update transaction repo resource content")
serveStorageContent(w, r, requestPath)
}
// storageImageCtx is a handler for storage image requests
func storageImageCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctxServices := dependencies.ServicesFromContext(r.Context())
orgID := readOrgID(w, r, ctxServices.Log)
if orgID == "" {
// readOrgID handle response and logging on failure
return
}
imageIDString := chi.URLParam(r, "imageID")
if imageIDString == "" {
ctxServices.Log.Debug("storage image ID was not passed to the request or it was empty")
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("storage image ID required"))
return
}
imageID, err := strconv.Atoi(imageIDString)
if err != nil {
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("storage image ID must be an integer"))
return
}
imageBuilderOrgID := config.Get().ImageBuilderOrgID
var dbFilter *gorm.DB
if imageBuilderOrgID != "" && orgID == imageBuilderOrgID {
// image-builder have read access to all image commit repositories
dbFilter = db.DB
} else {
dbFilter = db.Org(orgID, "images")
}
var image models.Image
if result := dbFilter.Preload("Commit.Repo").Joins("Commit").First(&image, imageID); result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
ctxServices.Log.WithField("error", result.Error.Error()).Error("storage image not found")
respondWithAPIError(w, ctxServices.Log, errors.NewNotFound("storage image not found"))
return
}
ctxServices.Log.WithField("error", result.Error.Error()).Error("failed to retrieve storage image")
respondWithAPIError(w, ctxServices.Log, errors.NewInternalServerError())
return
}
ctx := setContextStorageImage(r.Context(), &image)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func getContextStorageImage(w http.ResponseWriter, r *http.Request) *models.Image {
ctx := r.Context()
image, ok := ctx.Value(storageImageKey).(*models.Image)
if !ok {
ctxServices := dependencies.ServicesFromContext(ctx)
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("Failed getting storage image from context"))
return nil
}
return image
}
// ValidateStorageImage validate storage image and return the request path
func ValidateStorageImage(w http.ResponseWriter, r *http.Request) string {
ctxServices := dependencies.ServicesFromContext(r.Context())
image := getContextStorageImage(w, r)
if image == nil {
return ""
}
logContext := ctxServices.Log.WithFields(log.Fields{
"service": "image-repository-storage",
"orgID": image.OrgID,
"imageID": image.ID,
})
filePath := chi.URLParam(r, "*")
if filePath == "" {
logContext.Error("target repository file path is missing")
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("target repository file path is missing"))
return ""
}
if image.Commit.Repo == nil || image.Commit.Repo.URL == "" {
logContext.Error("image repository does not exist")
respondWithAPIError(w, logContext, errors.NewNotFound("image repository does not exist"))
return ""
}
RepoURL, err := url2.Parse(image.Commit.Repo.URL)
if err != nil {
logContext.WithFields(log.Fields{
"error": err.Error(),
"URL": image.Commit.Repo.URL,
}).Error("error occurred when parsing repository url")
respondWithAPIError(w, ctxServices.Log, errors.NewBadRequest("bad image repository url"))
return ""
}
requestPath := fmt.Sprintf(RepoURL.Path + "/" + filePath)
return requestPath
}
// GetImageRepoFileContent redirect to a signed url of an image commit repository path content
// @Summary redirect to a signed url of an image commit repository path content
// @Description Redirect request to a signed and valid url for an image commit repository from the path content
// @ID RedirectSignedImageCommitRepository
// @Tags Storage
// @Accept json
// @Produce json
// @Param imageID path string true "Id to identify Image"
// @Param repoFilePath path string true "path to file repository"
// @Success 303 {string} url response
// @Failure 400 {object} errors.BadRequest
// @Failure 404 {object} errors.NotFound
// @Failure 500 {object} errors.InternalServerError
// @Router /storage/images-repos/{imageID}/content/{repoFilePath} [get]
func GetImageRepoFileContent(w http.ResponseWriter, r *http.Request) {
ctxServices := dependencies.ServicesFromContext(r.Context())
logContext := ctxServices.Log.WithField("service", "image-repository-storage")
image := getContextStorageImage(w, r)
if image == nil {
// getContextStorageImage will handle errors
return
}
requestPath := ValidateStorageImage(w, r)
if requestPath == "" {
// ValidateStorageImage will handle errors
return
}
logContext.WithFields(log.Fields{
"orgID": image.OrgID,
"imageID": image.ID,
"path": requestPath,
}).Debug("redirect storage image repo resource")
redirectToStorageSignedURL(w, r, requestPath)
}
// GetImageRepoFile return the content of an image commit repository path
// @Summary return the content of an image commit repository path
// @ID ContentImageCommitRepositoryPath
// @Description Bring the content for a image commit in a repository path
// @Tags Storage
// @Accept json
// @Produce octet-stream
// @Param imageID path string true "Id to identify Image"
// @Param repoFilePath path string true "path to file repository"
// @Success 200 {string} string "Stream object from file content"
// @Failure 400 {object} errors.BadRequest
// @Failure 404 {object} errors.NotFound
// @Failure 500 {object} errors.InternalServerError
// @Router /storage/images-repos/{imageID}/{repoFilePath} [get]
func GetImageRepoFile(w http.ResponseWriter, r *http.Request) {
ctxServices := dependencies.ServicesFromContext(r.Context())
logContext := ctxServices.Log.WithField("service", "image-repository-storage")
image := getContextStorageImage(w, r)
if image == nil {
// getContextStorageImage will handle errors
return
}
requestPath := ValidateStorageImage(w, r)
if requestPath == "" {
// ValidateStorageImage will handle errors
return
}
logContext.WithFields(log.Fields{
"orgID": image.OrgID,
"imageID": image.ID,
"path": requestPath,
}).Debug("return storage image repo resource content")
serveStorageContent(w, r, requestPath)
}