feat(openmt): use .ts services with axios for OpenAMT (#6312)
This commit is contained in:
@@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
type deviceConfigurePayload struct {
|
||||
EdgeKey string `json:"edgekey"`
|
||||
EdgeKey string `json:"edgeKey"`
|
||||
Name string `json:"name"`
|
||||
Profile int `json:"profile"`
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func NewHandler(bouncer *security.RequestBouncer, dataStore dataservices.DataSto
|
||||
DataStore: dataStore,
|
||||
}
|
||||
|
||||
h.Handle("/fdo", bouncer.AdminAccess(httperror.LoggerHandler(h.fdoConfigure))).Methods(http.MethodPost)
|
||||
h.Handle("/fdo/configure", bouncer.AdminAccess(httperror.LoggerHandler(h.fdoConfigure))).Methods(http.MethodPost)
|
||||
h.Handle("/fdo/list", bouncer.AdminAccess(httperror.LoggerHandler(h.fdoListAll))).Methods(http.MethodGet)
|
||||
h.Handle("/fdo/register", bouncer.AdminAccess(httperror.LoggerHandler(h.fdoRegisterDevice))).Methods(http.MethodPost)
|
||||
h.Handle("/fdo/configure/{guid}", bouncer.AdminAccess(httperror.LoggerHandler(h.fdoConfigureDevice))).Methods(http.MethodPost)
|
||||
|
||||
@@ -16,17 +16,17 @@ import (
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
)
|
||||
|
||||
type openAMTConfigureDefaultPayload struct {
|
||||
EnableOpenAMT bool
|
||||
MPSServer string
|
||||
MPSUser string
|
||||
MPSPassword string
|
||||
CertFileText string
|
||||
CertPassword string
|
||||
DomainName string
|
||||
type openAMTConfigurePayload struct {
|
||||
EnableOpenAMT bool
|
||||
MPSServer string
|
||||
MPSUser string
|
||||
MPSPassword string
|
||||
CertFileText string
|
||||
CertPassword string
|
||||
DomainName string
|
||||
}
|
||||
|
||||
func (payload *openAMTConfigureDefaultPayload) Validate(r *http.Request) error {
|
||||
func (payload *openAMTConfigurePayload) Validate(r *http.Request) error {
|
||||
if payload.EnableOpenAMT {
|
||||
if payload.MPSServer == "" {
|
||||
return errors.New("MPS Server must be provided")
|
||||
@@ -51,7 +51,7 @@ func (payload *openAMTConfigureDefaultPayload) Validate(r *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// @id OpenAMTConfigureDefault
|
||||
// @id OpenAMTConfigure
|
||||
// @summary Enable Portainer's OpenAMT capabilities
|
||||
// @description Enable Portainer's OpenAMT capabilities
|
||||
// @description **Access policy**: administrator
|
||||
@@ -59,14 +59,14 @@ func (payload *openAMTConfigureDefaultPayload) Validate(r *http.Request) error {
|
||||
// @security jwt
|
||||
// @accept json
|
||||
// @produce json
|
||||
// @param body body openAMTConfigureDefaultPayload true "OpenAMT Settings"
|
||||
// @param body body openAMTConfigurePayload true "OpenAMT Settings"
|
||||
// @success 204 "Success"
|
||||
// @failure 400 "Invalid request"
|
||||
// @failure 403 "Permission denied to access settings"
|
||||
// @failure 500 "Server error"
|
||||
// @router /open_amt [post]
|
||||
func (handler *Handler) openAMTConfigureDefault(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
||||
var payload openAMTConfigureDefaultPayload
|
||||
func (handler *Handler) openAMTConfigure(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
||||
var payload openAMTConfigurePayload
|
||||
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("Invalid request payload")
|
||||
@@ -124,7 +124,7 @@ func isValidIssuer(issuer string) bool {
|
||||
strings.Contains(formattedIssuer, "godaddy")
|
||||
}
|
||||
|
||||
func (handler *Handler) enableOpenAMT(configurationPayload openAMTConfigureDefaultPayload) error {
|
||||
func (handler *Handler) enableOpenAMT(configurationPayload openAMTConfigurePayload) error {
|
||||
configuration := portainer.OpenAMTConfiguration{
|
||||
Enabled: true,
|
||||
MPSServer: configurationPayload.MPSServer,
|
||||
@@ -139,7 +139,7 @@ func (handler *Handler) enableOpenAMT(configurationPayload openAMTConfigureDefau
|
||||
},
|
||||
}
|
||||
|
||||
err := handler.OpenAMTService.ConfigureDefault(configuration)
|
||||
err := handler.OpenAMTService.Configure(configuration)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("error configuring OpenAMT server")
|
||||
return err
|
||||
@@ -187,4 +187,4 @@ func (handler *Handler) disableOpenAMT() error {
|
||||
|
||||
logrus.Info("OpenAMT successfully disabled")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,6 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Devices struct {
|
||||
Devices []portainer.OpenAMTDeviceInformation
|
||||
}
|
||||
|
||||
// @id OpenAMTDevices
|
||||
// @summary Fetch OpenAMT managed devices information for endpoint
|
||||
// @description Fetch OpenAMT managed devices information for endpoint
|
||||
@@ -52,27 +48,21 @@ func (handler *Handler) openAMTDevices(w http.ResponseWriter, r *http.Request) *
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve device information", err}
|
||||
}
|
||||
|
||||
devicesInformation := Devices{
|
||||
Devices: []portainer.OpenAMTDeviceInformation{
|
||||
*device,
|
||||
},
|
||||
devices := []portainer.OpenAMTDeviceInformation{
|
||||
*device,
|
||||
}
|
||||
|
||||
return response.JSON(w, devicesInformation)
|
||||
return response.JSON(w, devices)
|
||||
}
|
||||
|
||||
type deviceActionPayload struct {
|
||||
DeviceID string
|
||||
DeviceAction string
|
||||
Action string
|
||||
}
|
||||
|
||||
func (payload *deviceActionPayload) Validate(r *http.Request) error {
|
||||
if payload.DeviceAction == "" {
|
||||
if payload.Action == "" {
|
||||
return errors.New("device action must be provided")
|
||||
}
|
||||
if payload.DeviceID == "" {
|
||||
return errors.New("device GUID must be provided")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -89,10 +79,15 @@ func (payload *deviceActionPayload) Validate(r *http.Request) error {
|
||||
// @failure 400 "Invalid request"
|
||||
// @failure 403 "Permission denied to access settings"
|
||||
// @failure 500 "Server error"
|
||||
// @router /open_amt/{id}/devices/{deviceId}/{deviceAction} [post]
|
||||
// @router /open_amt/{id}/devices/{deviceId}/action [post]
|
||||
func (handler *Handler) deviceAction(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
||||
deviceID, err := request.RetrieveRouteVariableValue(r, "deviceId")
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusBadRequest, "Invalid device identifier route variable", err}
|
||||
}
|
||||
|
||||
var payload deviceActionPayload
|
||||
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
||||
err = request.DecodeAndValidateJSONPayload(r, &payload)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("Invalid request payload")
|
||||
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
||||
@@ -103,7 +98,7 @@ func (handler *Handler) deviceAction(w http.ResponseWriter, r *http.Request) *ht
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err}
|
||||
}
|
||||
|
||||
err = handler.OpenAMTService.ExecuteDeviceAction(settings.OpenAMTConfiguration, payload.DeviceID, payload.DeviceAction)
|
||||
err = handler.OpenAMTService.ExecuteDeviceAction(settings.OpenAMTConfiguration, deviceID, payload.Action)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("Error executing device action")
|
||||
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Error executing device action", Err: err}
|
||||
@@ -113,15 +108,11 @@ func (handler *Handler) deviceAction(w http.ResponseWriter, r *http.Request) *ht
|
||||
}
|
||||
|
||||
type deviceFeaturesPayload struct {
|
||||
DeviceID string
|
||||
EnabledFeatures portainer.OpenAMTDeviceEnabledFeatures
|
||||
Features portainer.OpenAMTDeviceEnabledFeatures
|
||||
}
|
||||
|
||||
func (payload *deviceFeaturesPayload) Validate(r *http.Request) error {
|
||||
if payload.DeviceID == "" {
|
||||
return errors.New("device GUID must be provided")
|
||||
}
|
||||
if payload.EnabledFeatures.UserConsent == "" {
|
||||
if payload.Features.UserConsent == "" {
|
||||
return errors.New("device user consent status must be provided")
|
||||
}
|
||||
return nil
|
||||
@@ -147,8 +138,13 @@ type AuthorizationResponse struct {
|
||||
// @failure 500 "Server error"
|
||||
// @router /open_amt/{id}/devices_features/{deviceId} [post]
|
||||
func (handler *Handler) deviceFeatures(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
||||
deviceID, err := request.RetrieveRouteVariableValue(r, "deviceId")
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusBadRequest, "Invalid device identifier route variable", err}
|
||||
}
|
||||
|
||||
var payload deviceFeaturesPayload
|
||||
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
||||
err = request.DecodeAndValidateJSONPayload(r, &payload)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("Invalid request payload")
|
||||
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid request payload", Err: err}
|
||||
@@ -159,20 +155,20 @@ func (handler *Handler) deviceFeatures(w http.ResponseWriter, r *http.Request) *
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err}
|
||||
}
|
||||
|
||||
_, err = handler.OpenAMTService.DeviceInformation(settings.OpenAMTConfiguration, payload.DeviceID)
|
||||
_, err = handler.OpenAMTService.DeviceInformation(settings.OpenAMTConfiguration, deviceID)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve device information", err}
|
||||
}
|
||||
|
||||
token, err := handler.OpenAMTService.EnableDeviceFeatures(settings.OpenAMTConfiguration, payload.DeviceID, payload.EnabledFeatures)
|
||||
token, err := handler.OpenAMTService.EnableDeviceFeatures(settings.OpenAMTConfiguration, deviceID, payload.Features)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("Error executing device action")
|
||||
return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Error executing device action", Err: err}
|
||||
}
|
||||
|
||||
credentials := AuthorizationResponse{
|
||||
authorizationResponse := AuthorizationResponse{
|
||||
Server: settings.OpenAMTConfiguration.MPSServer,
|
||||
Token: token,
|
||||
}
|
||||
return response.JSON(w, credentials)
|
||||
return response.JSON(w, authorizationResponse)
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ import (
|
||||
// Handler is the HTTP handler used to handle OpenAMT operations.
|
||||
type Handler struct {
|
||||
*mux.Router
|
||||
OpenAMTService portainer.OpenAMTService
|
||||
DataStore dataservices.DataStore
|
||||
DockerClientFactory *docker.ClientFactory
|
||||
OpenAMTService portainer.OpenAMTService
|
||||
DataStore dataservices.DataStore
|
||||
DockerClientFactory *docker.ClientFactory
|
||||
}
|
||||
|
||||
// NewHandler returns a new Handler
|
||||
@@ -30,12 +30,12 @@ func NewHandler(bouncer *security.RequestBouncer, dataStore dataservices.DataSto
|
||||
Router: mux.NewRouter(),
|
||||
}
|
||||
|
||||
h.Handle("/open_amt", bouncer.AdminAccess(httperror.LoggerHandler(h.openAMTConfigureDefault))).Methods(http.MethodPost)
|
||||
h.Handle("/open_amt/configure", bouncer.AdminAccess(httperror.LoggerHandler(h.openAMTConfigure))).Methods(http.MethodPost)
|
||||
h.Handle("/open_amt/{id}/info", bouncer.AdminAccess(httperror.LoggerHandler(h.openAMTHostInfo))).Methods(http.MethodGet)
|
||||
h.Handle("/open_amt/{id}/activate", bouncer.AdminAccess(httperror.LoggerHandler(h.openAMTActivate))).Methods(http.MethodPost)
|
||||
h.Handle("/open_amt/{id}/devices", bouncer.AdminAccess(httperror.LoggerHandler(h.openAMTDevices))).Methods(http.MethodGet)
|
||||
h.Handle("/open_amt/{id}/devices/{deviceId}/{deviceAction}", bouncer.AdminAccess(httperror.LoggerHandler(h.deviceAction))).Methods(http.MethodPost)
|
||||
h.Handle("/open_amt/{id}/devices_features/{deviceId}", bouncer.AdminAccess(httperror.LoggerHandler(h.deviceFeatures))).Methods(http.MethodPost)
|
||||
h.Handle("/open_amt/{id}/devices/{deviceId}/action", bouncer.AdminAccess(httperror.LoggerHandler(h.deviceAction))).Methods(http.MethodPost)
|
||||
h.Handle("/open_amt/{id}/devices/{deviceId}/features", bouncer.AdminAccess(httperror.LoggerHandler(h.deviceFeatures))).Methods(http.MethodPost)
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user