Skip to content

Commit

Permalink
Add Logging for plist parse errors
Browse files Browse the repository at this point in the history
This is useful to debug errors that occur because of bad plists sent by
clients. Usually in this case, one receives a not-very-useful error such
as `HTTP status 400 (Bad Request): <opaque error>`.

 ## Test Plan

1. Patch `mdmb` so that it sends bad checkins
```
diff --git a/internal/device/mdm.go b/internal/device/mdm.go
index 33a4c50..cda3d69 100644
--- a/internal/device/mdm.go
+++ b/internal/device/mdm.go
@@ -185,10 +185,7 @@ func (c *MDMClient) connect(client *http.Client, connReq interface{}) error {
                return errors.New("device not enrolled")
        }

-       plistBytes, err := plist.Marshal(connReq)
-       if err != nil {
-               return err
-       }
+       plistBytes := []byte("aoeuaoeuaoeu")
```

2. Run
```
$mdmb -uuids $uuid devices-connect
```

3. Observe more verbose NanoMDM log:
```
2022/07/21 15:52:43 level=info handler=checkin-command trace_id=1af087fdc6de5381 msg=command report results err=HTTP status 400 (Bad Request): decoding command results: parse error: EOF: raw body: aoeuaoeuaoeu
```
  • Loading branch information
Calvin Lee committed Jul 21, 2022
1 parent 9ca3cac commit 20a07b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions mdm/checkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ func (w *checkinUnmarshaller) UnmarshalPlist(f func(interface{}) error) error {
func DecodeCheckin(rawMessage []byte) (message interface{}, err error) {
w := &checkinUnmarshaller{raw: rawMessage}
err = plist.Unmarshal(rawMessage, w)
if err != nil {
err = &ParseError{Err: err, Body: rawMessage}
}
message = w.message
return
}
3 changes: 2 additions & 1 deletion mdm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func DecodeCommandResults(rawResults []byte) (results *CommandResults, err error
results = new(CommandResults)
err = plist.Unmarshal(rawResults, results)
if err != nil {
err = &ParseError{Err: err, Body: rawResults}
return
}
results.Raw = rawResults
Expand All @@ -58,7 +59,7 @@ func DecodeCommand(rawCommand []byte) (command *Command, err error) {
command = new(Command)
err = plist.Unmarshal(rawCommand, command)
if err != nil {
return
return nil, &ParseError{Err: err, Body: rawCommand}
}
command.Raw = rawCommand
if command.CommandUUID == "" || command.Command.RequestType == "" {
Expand Down
14 changes: 14 additions & 0 deletions mdm/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/x509"
"errors"
"fmt"
)

// Enrollment represents the various enrollment-related data sent with requests.
Expand Down Expand Up @@ -59,3 +60,16 @@ func (r *Request) Clone() *Request {
*r2 = *r
return r2
}

type ParseError struct {
Err error
Body []byte
}

func (e *ParseError) Unwrap() error {
return e.Err
}

func (e *ParseError) Error() string {
return fmt.Sprintf("parse error: %s: raw body: %v", e.Err, string(e.Body))
}

0 comments on commit 20a07b9

Please sign in to comment.