Add raw command and clear device queue endpoints (#864)

This commit is contained in:
Kory Prince
2023-04-23 16:47:42 -05:00
committed by GitHub
parent a5cf4e2742
commit 3d0746f3e3
13 changed files with 427 additions and 9 deletions

View File

@@ -1,9 +1,14 @@
package command
import (
"bytes"
"fmt"
"io"
"net/http"
"github.com/go-kit/kit/endpoint"
"github.com/gorilla/mux"
"github.com/groob/plist"
"github.com/pkg/errors"
"golang.org/x/net/context"
@@ -14,6 +19,9 @@ import (
const (
// CommandTopic is a PubSub topic that events are published to.
CommandTopic = "mdm.Command"
// RawCommandTopic is a PubSub topic that events are published to.
RawCommandTopic = "mdm.RawCommand"
)
func (svc *CommandService) NewCommand(ctx context.Context, request *mdm.CommandRequest) (*mdm.CommandPayload, error) {
@@ -35,6 +43,21 @@ func (svc *CommandService) NewCommand(ctx context.Context, request *mdm.CommandR
return payload, nil
}
func (svc *CommandService) NewRawCommand(ctx context.Context, cmd *RawCommand) error {
if cmd == nil {
return errors.New("empty RawCommand")
}
event := NewRawEvent(cmd)
msg, err := MarshalRawEvent(event)
if err != nil {
return errors.Wrap(err, "marshalling raw mdm command event")
}
if err := svc.publisher.Publish(context.TODO(), RawCommandTopic, msg); err != nil {
return errors.Wrapf(err, "publish raw mdm command on topic: %s", RawCommandTopic)
}
return nil
}
type newCommandRequest struct {
mdm.CommandRequest
}
@@ -69,3 +92,65 @@ func MakeNewCommandEndpoint(svc Service) endpoint.Endpoint {
return newCommandResponse{Payload: payload}, nil
}
}
type RawCommand struct {
UDID string `json:"udid" plist:"-"`
CommandUUID string `json:"command_uuid"`
Command struct {
RequestType string `json:"request_type"`
} `json:"command"`
Raw []byte `plist:"-" json:"payload"`
}
type newRawCommandRequest struct {
RawCommand
}
type newRawCommandResponse struct {
Payload *RawCommand `json:"payload,omitempty"`
Err error `json:"error,omitempty"`
}
func (r newRawCommandResponse) Failed() error { return r.Err }
func (r newRawCommandResponse) StatusCode() int { return http.StatusCreated }
func decodeNewRawCommandRequest(ctx context.Context, r *http.Request) (interface{}, error) {
udid, ok := mux.Vars(r)["udid"]
if !ok {
return nil, errors.New("empty udid")
}
payload, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("read payload body: %w", err)
}
// verify body is valid plist and parse CommandUUID and RequestType
var req newRawCommandRequest
if err := plist.NewXMLDecoder(bytes.NewBuffer(payload)).Decode(&req); err != nil {
return nil, fmt.Errorf("parse payload as plist: %w", err)
}
req.UDID = udid
req.Raw = payload
return req, nil
}
var errMalformedRequest = errors.New("request is malformed")
// MakeNewRawCommandEndpoint creates an endpoint which creates new raw MDM Commands.
func MakeNewRawCommandEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(newRawCommandRequest)
if req.UDID == "" {
return newRawCommandResponse{Err: errEmptyRequest}, nil
}
if req.CommandUUID == "" || req.Command.RequestType == "" {
return newRawCommandResponse{Err: errMalformedRequest}, nil
}
if err := svc.NewRawCommand(ctx, &req.RawCommand); err != nil {
return newRawCommandResponse{Err: err}, nil
}
return newRawCommandResponse{Payload: &req.RawCommand}, nil
}
}