From a0d1498b1be897dab03823907ee5046a82f10f45 Mon Sep 17 00:00:00 2001 From: Victor Vrantchan Date: Tue, 30 May 2017 08:08:48 -0400 Subject: [PATCH] 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. --- Gopkg.lock | 2 +- command/endpoint.go | 4 ++-- command/transport_http_test.go | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 command/transport_http_test.go diff --git a/Gopkg.lock b/Gopkg.lock index 6634c9a4..90f67a0e 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -95,7 +95,7 @@ branch = "master" name = "github.com/micromdm/mdm" packages = ["."] - revision = "2e3f297ba6a2e801dec12771c92c301b8e15f76c" + revision = "33dc653861c4691c3579fd1d26a4b5bf26302cb4" [[projects]] name = "github.com/micromdm/scep" diff --git a/command/endpoint.go b/command/endpoint.go index 839cc874..f7526a9c 100644 --- a/command/endpoint.go +++ b/command/endpoint.go @@ -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 { diff --git a/command/transport_http_test.go b/command/transport_http_test.go new file mode 100644 index 00000000..281b875a --- /dev/null +++ b/command/transport_http_test.go @@ -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) + } + +}