2022-01-23 19:48:04 +00:00
|
|
|
package fdo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
|
|
|
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
deploymentScriptName = "fdo.sh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type deviceConfigurePayload struct {
|
|
|
|
EdgeID string `json:"edgeID"`
|
|
|
|
EdgeKey string `json:"edgeKey"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ProfileID int `json:"profile"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *deviceConfigurePayload) Validate(r *http.Request) error {
|
|
|
|
if payload.EdgeID == "" {
|
|
|
|
return errors.New("invalid edge ID provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.EdgeKey == "" {
|
|
|
|
return errors.New("invalid edge key provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Name == "" {
|
|
|
|
return errors.New("the device name cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.ProfileID < 1 {
|
|
|
|
return errors.New("invalid profile id provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// @id fdoConfigureDevice
|
|
|
|
// @summary configures an FDO device
|
|
|
|
// @description configures an FDO device
|
|
|
|
// @description **Access policy**: administrator
|
|
|
|
// @tags intel
|
|
|
|
// @security jwt
|
|
|
|
// @produce json
|
|
|
|
// @param body body deviceConfigurePayload true "Device Configuration"
|
|
|
|
// @success 200 "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 403 "Permission denied to access settings"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /fdo/configure/{guid} [post]
|
|
|
|
func (handler *Handler) fdoConfigureDevice(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
guid, err := request.RetrieveRouteVariableValue(r, "guid")
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: request.RetrieveRouteVariableValue()")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: guid not found", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var payload deviceConfigurePayload
|
|
|
|
|
|
|
|
err = request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Invalid request payload")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid request payload", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
profile, err := handler.DataStore.FDOProfile().FDOProfile(portainer.FDOProfileID(payload.ProfileID))
|
|
|
|
if handler.DataStore.IsErrObjectNotFound(err) {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.NotFound("Unable to find a FDO Profile with the specified identifier inside the database", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
} else if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to find a FDO Profile with the specified identifier inside the database", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fileContent, err := handler.FileService.GetFileContent(profile.FilePath, "")
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: GetFileContent")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: GetFileContent", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fdoClient, err := handler.newFDOClient()
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: newFDOClient()")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: newFDOClient()", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// enable fdo_sys
|
|
|
|
if err = fdoClient.PutDeviceSVIRaw(url.Values{
|
|
|
|
"guid": []string{guid},
|
|
|
|
"priority": []string{"0"},
|
|
|
|
"module": []string{"fdo_sys"},
|
|
|
|
"var": []string{"active"},
|
|
|
|
"bytes": []string{"F5"}, // this is "true" in CBOR
|
|
|
|
}, []byte("")); err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: PutDeviceSVIRaw()")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: PutDeviceSVIRaw()", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = fdoClient.PutDeviceSVIRaw(url.Values{
|
|
|
|
"guid": []string{guid},
|
|
|
|
"priority": []string{"1"},
|
|
|
|
"module": []string{"fdo_sys"},
|
|
|
|
"var": []string{"filedesc"},
|
|
|
|
"filename": []string{"DEVICE_edgeid.txt"},
|
|
|
|
}, []byte(payload.EdgeID)); err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: PutDeviceSVIRaw(edgeid)")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: PutDeviceSVIRaw(edgeid)", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write down the edgekey
|
|
|
|
if err = fdoClient.PutDeviceSVIRaw(url.Values{
|
|
|
|
"guid": []string{guid},
|
|
|
|
"priority": []string{"1"},
|
|
|
|
"module": []string{"fdo_sys"},
|
|
|
|
"var": []string{"filedesc"},
|
|
|
|
"filename": []string{"DEVICE_edgekey.txt"},
|
|
|
|
}, []byte(payload.EdgeKey)); err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: PutDeviceSVIRaw(edgekey)")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: PutDeviceSVIRaw(edgekey)", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write down the device name
|
|
|
|
if err = fdoClient.PutDeviceSVIRaw(url.Values{
|
|
|
|
"guid": []string{guid},
|
|
|
|
"priority": []string{"1"},
|
|
|
|
"module": []string{"fdo_sys"},
|
|
|
|
"var": []string{"filedesc"},
|
|
|
|
"filename": []string{"DEVICE_name.txt"},
|
|
|
|
}, []byte(payload.Name)); err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: PutDeviceSVIRaw(name)")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: PutDeviceSVIRaw(name)", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write down the device GUID - used as the EDGE_DEVICE_GUID too
|
|
|
|
if err = fdoClient.PutDeviceSVIRaw(url.Values{
|
|
|
|
"guid": []string{guid},
|
|
|
|
"priority": []string{"1"},
|
|
|
|
"module": []string{"fdo_sys"},
|
|
|
|
"var": []string{"filedesc"},
|
|
|
|
"filename": []string{"DEVICE_GUID.txt"},
|
|
|
|
}, []byte(guid)); err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: PutDeviceSVIRaw()")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: PutDeviceSVIRaw()", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = fdoClient.PutDeviceSVIRaw(url.Values{
|
|
|
|
"guid": []string{guid},
|
|
|
|
"priority": []string{"1"},
|
|
|
|
"module": []string{"fdo_sys"},
|
|
|
|
"var": []string{"filedesc"},
|
|
|
|
"filename": []string{deploymentScriptName},
|
|
|
|
}, fileContent); err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: PutDeviceSVIRaw()")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: PutDeviceSVIRaw()", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := cbor.Marshal([]string{"/bin/sh", deploymentScriptName})
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("failed to marshal string to CBOR")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: PutDeviceSVIRaw() failed to encode", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cborBytes := strings.ToUpper(hex.EncodeToString(b))
|
|
|
|
logrus.WithField("cbor", cborBytes).WithField("string", deploymentScriptName).Info("converted to CBOR")
|
|
|
|
|
|
|
|
if err = fdoClient.PutDeviceSVIRaw(url.Values{
|
|
|
|
"guid": []string{guid},
|
|
|
|
"priority": []string{"2"},
|
|
|
|
"module": []string{"fdo_sys"},
|
|
|
|
"var": []string{"exec"},
|
|
|
|
"bytes": []string{cborBytes},
|
|
|
|
}, []byte("")); err != nil {
|
|
|
|
logrus.WithError(err).Info("fdoConfigureDevice: PutDeviceSVIRaw()")
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("fdoConfigureDevice: PutDeviceSVIRaw()", err)
|
2022-01-23 19:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Empty(w)
|
|
|
|
}
|