THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

This is Gitea test Portainer repository mirror from Github
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

76 lines
2.2 KiB

  1. package extensions
  2. import (
  3. "net/http"
  4. "strconv"
  5. httperror "github.com/portainer/libhttp/error"
  6. "github.com/portainer/libhttp/request"
  7. "github.com/portainer/libhttp/response"
  8. "github.com/portainer/portainer/api"
  9. )
  10. type extensionUploadPayload struct {
  11. License string
  12. ExtensionArchive []byte
  13. ArchiveFileName string
  14. }
  15. func (payload *extensionUploadPayload) Validate(r *http.Request) error {
  16. license, err := request.RetrieveMultiPartFormValue(r, "License", false)
  17. if err != nil {
  18. return portainer.Error("Invalid license")
  19. }
  20. payload.License = license
  21. fileData, fileName, err := request.RetrieveMultiPartFormFile(r, "file")
  22. if err != nil {
  23. return portainer.Error("Invalid extension archive file. Ensure that the file is uploaded correctly")
  24. }
  25. payload.ExtensionArchive = fileData
  26. payload.ArchiveFileName = fileName
  27. return nil
  28. }
  29. func (handler *Handler) extensionUpload(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
  30. payload := &extensionUploadPayload{}
  31. err := payload.Validate(r)
  32. if err != nil {
  33. return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
  34. }
  35. extensionIdentifier, err := strconv.Atoi(string(payload.License[0]))
  36. if err != nil {
  37. return &httperror.HandlerError{http.StatusBadRequest, "Invalid license format", err}
  38. }
  39. extensionID := portainer.ExtensionID(extensionIdentifier)
  40. extension := &portainer.Extension{
  41. ID: extensionID,
  42. }
  43. _ = handler.ExtensionManager.DisableExtension(extension)
  44. err = handler.ExtensionManager.InstallExtension(extension, payload.License, payload.ArchiveFileName, payload.ExtensionArchive)
  45. if err != nil {
  46. return &httperror.HandlerError{http.StatusInternalServerError, "Unable to install extension", err}
  47. }
  48. extension.Enabled = true
  49. if extension.ID == portainer.RBACExtension {
  50. err = handler.upgradeRBACData()
  51. if err != nil {
  52. return &httperror.HandlerError{http.StatusInternalServerError, "An error occured during database update", err}
  53. }
  54. }
  55. err = handler.ExtensionService.Persist(extension)
  56. if err != nil {
  57. return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist extension status inside the database", err}
  58. }
  59. return response.Empty(w)
  60. }