Fix nil panic on command request API (#198)

Fixes issues introduced by
micromdm/mdm#15
micromdm/mdm#16
and solved in
micromdm/mdm#17

The micromdm/mdm library changed the structure of the mdm.CommandRequest field, causing the decode method to panic. Fixed and added test.
This commit is contained in:
Victor Vrantchan
2017-05-30 08:08:48 -04:00
committed by GitHub
parent bff40d2479
commit a0d1498b1b
3 changed files with 38 additions and 3 deletions

2
Gopkg.lock generated
View File

@@ -95,7 +95,7 @@
branch = "master"
name = "github.com/micromdm/mdm"
packages = ["."]
revision = "2e3f297ba6a2e801dec12771c92c301b8e15f76c"
revision = "33dc653861c4691c3579fd1d26a4b5bf26302cb4"
[[projects]]
name = "github.com/micromdm/scep"

View File

@@ -26,7 +26,7 @@ func MakeNewCommandEndpoint(svc Service) endpoint.Endpoint {
if req.UDID == "" || req.RequestType == "" {
return newCommandResponse{Err: errEmptyRequest}, nil
}
payload, err := svc.NewCommand(ctx, req.CommandRequest)
payload, err := svc.NewCommand(ctx, &req.CommandRequest)
if err != nil {
return newCommandResponse{Err: err}, nil
}
@@ -66,7 +66,7 @@ func EndpointLoggingMiddleware(logger log.Logger) endpoint.Middleware {
}
type newCommandRequest struct {
*mdm.CommandRequest
mdm.CommandRequest
}
type newCommandResponse struct {

View File

@@ -0,0 +1,35 @@
package command
import (
"context"
"net/http/httptest"
"strings"
"testing"
)
func TestDecodeRequest(t *testing.T) {
requestData := `
{
"request_type": "InstallApplication",
"udid" : "564D38A0-4C3B-AD69-803B-DAC58A298191",
"manifest_url" : "https://mdm.acme.co/repo/munkitools-3.0.0.3298.plist",
"management_flags" : 1
}
`
req := httptest.NewRequest("POST", "https://mdm.acme.co/v1/commands", strings.NewReader(requestData))
request, err := decodeRequest(context.Background(), req)
if err != nil {
t.Fatal(err)
}
decoded := request.(newCommandRequest)
if have, want := decoded.RequestType, "InstallApplication"; have != want {
t.Errorf("have %s, want %s", have, want)
}
if have, want := decoded.CommandRequest.InstallApplication.ManifestURL,
"https://mdm.acme.co/repo/munkitools-3.0.0.3298.plist"; have != want {
t.Errorf("have %s, want %s", have, want)
}
}