2018-12-09 03:49:27 +00:00
|
|
|
package extensions
|
|
|
|
|
|
|
|
import (
|
2020-07-07 21:57:52 +00:00
|
|
|
"errors"
|
2018-12-09 03:49:27 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
2018-12-09 03:49:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type extensionCreatePayload struct {
|
|
|
|
License string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *extensionCreatePayload) Validate(r *http.Request) error {
|
|
|
|
if govalidator.IsNull(payload.License) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid license")
|
2018-12-09 03:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) extensionCreate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
var payload extensionCreatePayload
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
extensionIdentifier, err := strconv.Atoi(string(payload.License[0]))
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid license format", err}
|
|
|
|
}
|
|
|
|
extensionID := portainer.ExtensionID(extensionIdentifier)
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
extensions, err := handler.DataStore.Extension().Extensions()
|
2018-12-09 03:49:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve extensions status from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, existingExtension := range extensions {
|
2019-01-17 21:00:18 +00:00
|
|
|
if existingExtension.ID == extensionID && existingExtension.Enabled {
|
2020-07-07 21:57:52 +00:00
|
|
|
return &httperror.HandlerError{http.StatusConflict, "Unable to enable extension", errors.New("This extension is already enabled")}
|
2018-12-09 03:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension := &portainer.Extension{
|
|
|
|
ID: extensionID,
|
|
|
|
}
|
|
|
|
|
|
|
|
extensionDefinitions, err := handler.ExtensionManager.FetchExtensionDefinitions()
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve extension definitions", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, def := range extensionDefinitions {
|
|
|
|
if def.ID == extension.ID {
|
|
|
|
extension.Version = def.Version
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.ExtensionManager.EnableExtension(extension, payload.License)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to enable extension", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension.Enabled = true
|
|
|
|
|
2019-05-24 06:04:58 +00:00
|
|
|
if extension.ID == portainer.RBACExtension {
|
|
|
|
err = handler.upgradeRBACData()
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "An error occured during database update", err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
err = handler.DataStore.Extension().Persist(extension)
|
2018-12-09 03:49:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist extension status inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Empty(w)
|
|
|
|
}
|