Add endpoint for inspecting the MDM command queue (#895)

This commit is contained in:
Graham Gilbert
2023-07-12 00:07:42 +01:00
committed by GitHub
parent 7a3715454a
commit c4a1cd97ef
9 changed files with 157 additions and 2 deletions

View File

@@ -11,6 +11,7 @@ type Endpoints struct {
NewCommandEndpoint endpoint.Endpoint
NewRawCommandEndpoint endpoint.Endpoint
ClearQueueEndpoint endpoint.Endpoint
ViewQueueEndpoint endpoint.Endpoint
}
func MakeServerEndpoints(s Service, outer endpoint.Middleware, others ...endpoint.Middleware) Endpoints {
@@ -18,10 +19,19 @@ func MakeServerEndpoints(s Service, outer endpoint.Middleware, others ...endpoin
NewCommandEndpoint: endpoint.Chain(outer, others...)(MakeNewCommandEndpoint(s)),
NewRawCommandEndpoint: endpoint.Chain(outer, others...)(MakeNewRawCommandEndpoint(s)),
ClearQueueEndpoint: endpoint.Chain(outer, others...)(MakeClearQueueEndpoint(s)),
ViewQueueEndpoint: endpoint.Chain(outer, others...)(MakeViewQueueEndpoint(s)),
}
}
func RegisterHTTPHandlers(r *mux.Router, e Endpoints, options ...httptransport.ServerOption) {
// GET /v1/commands/udid View device queue.
r.Methods("GET").Path("/v1/commands/{udid}").Handler(httptransport.NewServer(
e.ViewQueueEndpoint,
decodeViewQueueRequest,
httputil.EncodeJSONResponse,
options...,
))
// POST /v1/commands Add new MDM Command to device queue.
r.Methods("POST").Path("/v1/commands").Handler(httptransport.NewServer(
e.NewCommandEndpoint,

View File

@@ -12,11 +12,13 @@ type Service interface {
NewCommand(context.Context, *mdm.CommandRequest) (*mdm.CommandPayload, error)
NewRawCommand(context.Context, *RawCommand) error
ClearQueue(ctx context.Context, udid string) error
ViewQueue(ctx context.Context, udid string) ([]*mdmsvc.Command, error)
}
// Queue is an MDM Command Queue.
type Queue interface {
Clear(context.Context, mdmsvc.CheckinEvent) error
ViewQueue(context.Context, mdmsvc.CheckinEvent) ([]*mdmsvc.Command, error)
}
type CommandService struct {

54
platform/command/view.go Normal file
View File

@@ -0,0 +1,54 @@
package command
import (
"context"
"net/http"
"github.com/go-kit/kit/endpoint"
"github.com/gorilla/mux"
"github.com/micromdm/micromdm/mdm"
"github.com/pkg/errors"
)
func (svc *CommandService) ViewQueue(ctx context.Context, udid string) ([]*mdm.Command, error) {
commands, err := svc.queue.ViewQueue(
ctx,
mdm.CheckinEvent{Command: mdm.CheckinCommand{UDID: udid}},
)
if err != nil {
return nil, errors.Wrap(err, "clearing command queue")
}
return commands, nil
}
type viewQueueRequest struct {
UDID string
}
type viewQueueResponse struct {
Err error `json:"error,omitempty"`
Commands []*mdm.Command `json:"commands,omitempty"`
}
func (r viewQueueResponse) Failed() error { return r.Err }
func (r viewQueueResponse) StatusCode() int { return http.StatusOK }
func decodeViewQueueRequest(ctx context.Context, r *http.Request) (interface{}, error) {
return viewQueueRequest{UDID: mux.Vars(r)["udid"]}, nil
}
// MakeViewQueueEndpoint creates an endpoint which views device queues.
func MakeViewQueueEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(viewQueueRequest)
if req.UDID == "" {
return viewQueueResponse{Err: errEmptyRequest}, nil
}
commands, err := svc.ViewQueue(ctx, req.UDID)
if err != nil {
return viewQueueResponse{Err: err}, nil
}
return viewQueueResponse{Commands: commands}, nil
}
}