Skip to content

Commit

Permalink
Add Logging for plist parse errors (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
4e554c4c authored Jul 27, 2022
1 parent 773ad3b commit d0bcd3c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
22 changes: 20 additions & 2 deletions http/mdm/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mdm

import (
"errors"
"fmt"
"net/http"
"strings"

Expand Down Expand Up @@ -37,12 +38,21 @@ func CheckinHandler(svc service.Checkin, logger log.Logger) http.HandlerFunc {
}
respBytes, err := service.CheckinRequest(svc, mdmReqFromHTTPReq(r), bodyBytes)
if err != nil {
logger.Info("msg", "check-in request", "err", err)
logs := []interface{}{"msg", "check-in request"}
httpStatus := http.StatusInternalServerError
var statusErr *service.HTTPStatusError
if errors.As(err, &statusErr) {
httpStatus = statusErr.Status
err = fmt.Errorf("HTTP error: %w", statusErr.Unwrap())
}
// manualy unwrapping the `StatusErr` is not necessary as `errors.As` manually unwraps
var parseErr *mdm.ParseError
if errors.As(err, &parseErr) {
logs = append(logs, "content", string(parseErr.Content))
err = fmt.Errorf("parse error: %w", parseErr.Unwrap())
}
logs = append(logs, "http_status", httpStatus, "err", err)
logger.Info(logs...)
http.Error(w, http.StatusText(httpStatus), httpStatus)
}
w.Write(respBytes)
Expand All @@ -61,12 +71,20 @@ func CommandAndReportResultsHandler(svc service.CommandAndReportResults, logger
}
respBytes, err := service.CommandAndReportResultsRequest(svc, mdmReqFromHTTPReq(r), bodyBytes)
if err != nil {
logger.Info("msg", "command report results", "err", err)
logs := []interface{}{"msg", "command report results"}
httpStatus := http.StatusInternalServerError
var statusErr *service.HTTPStatusError
if errors.As(err, &statusErr) {
httpStatus = statusErr.Status
err = fmt.Errorf("HTTP error: %w", statusErr.Unwrap())
}
var parseErr *mdm.ParseError
if errors.As(err, &parseErr) {
logs = append(logs, "content", string(parseErr.Content))
err = fmt.Errorf("parse error: %w", parseErr.Unwrap())
}
logs = append(logs, "http_status", httpStatus, "err", err)
logger.Info(logs...)
http.Error(w, http.StatusText(httpStatus), httpStatus)
}
w.Write(respBytes)
Expand Down
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, Content: rawMessage}
}
message = w.message
return
}
4 changes: 2 additions & 2 deletions mdm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func DecodeCommandResults(rawResults []byte) (results *CommandResults, err error
results = new(CommandResults)
err = plist.Unmarshal(rawResults, results)
if err != nil {
return
return nil, &ParseError{Err: err, Content: rawResults}
}
results.Raw = rawResults
if results.Status == "" {
Expand All @@ -58,7 +58,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, Content: rawCommand}
}
command.Raw = rawCommand
if command.CommandUUID == "" || command.Command.RequestType == "" {
Expand Down
17 changes: 17 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,19 @@ func (r *Request) Clone() *Request {
*r2 = *r
return r2
}

// ParseError represents a failure to parse an MDM structure (usually Apple Plist)
type ParseError struct {
Err error
Content []byte
}

// Unwrap returns the underlying error of the ParseError
func (e *ParseError) Unwrap() error {
return e.Err
}

// Error formats the ParseError as a string
func (e *ParseError) Error() string {
return fmt.Sprintf("parse error: %v: raw content: %v", e.Err, string(e.Content))
}

0 comments on commit d0bcd3c

Please sign in to comment.