Allow command callers to specify Command UUID for idempotency (#754)

This commit is contained in:
Damon Aw
2021-06-03 13:08:48 -04:00
committed by GitHub
parent fcb8c1f985
commit b75a7575ce
4 changed files with 54 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
## [Unreleased](https://github.com/micromdm/micromdm/compare/v1.8.0...main) TBD
- Fix SetFirmwarePassword and VerifyFirmwarePassword parameters (#743)
- Command UUID can now be passed in as as a request parameter (#754)
## [v1.8.0](https://github.com/micromdm/micromdm/compare/v1.7.1...v1.8.0) February, 2021

View File

@@ -1,12 +1,15 @@
package mdm
import (
"strings"
"github.com/google/uuid"
"github.com/micromdm/micromdm/mdm/appmanifest"
)
type CommandRequest struct {
UDID string `json:"udid"`
UDID string `json:"udid"`
CommandUUID string `json:"command_uuid"`
*Command
}
@@ -17,9 +20,12 @@ type CommandPayload struct {
func NewCommandPayload(request *CommandRequest) (*CommandPayload, error) {
payload := &CommandPayload{
CommandUUID: uuid.New().String(),
CommandUUID: request.CommandUUID,
Command: request.Command,
}
if strings.TrimSpace(payload.CommandUUID) == "" {
payload.CommandUUID = uuid.New().String()
}
return payload, nil
}

View File

@@ -133,6 +133,49 @@ func TestUnmarshalCommandPayload(t *testing.T) {
})
}
}
func TestNewCommandPayload(t *testing.T) {
// Unit test cases for request params
var tests = []struct {
name string
request CommandRequest
testFn func(t *testing.T, payload *CommandPayload)
}{
{
name: "Uses UUID passed to CommandRequest",
request: CommandRequest{CommandUUID: "this-uuid-should-be-used"},
testFn: func(t *testing.T, payload *CommandPayload) {
if payload.CommandUUID != "this-uuid-should-be-used" {
t.Error("CommandUUID is not set to CommandRequest.CommandUUID")
}
},
},
{
name: "Defaults to generated UUID if CommandUUID is an empty string",
request: CommandRequest{CommandUUID: ""},
testFn: func(t *testing.T, payload *CommandPayload) {
if payload.CommandUUID == "" {
t.Error("CommandUUID should be a generated UUID")
}
},
},
{
name: "Defaults to generated UUID if CommandUUID is all whitespace",
request: CommandRequest{CommandUUID: " "},
testFn: func(t *testing.T, payload *CommandPayload) {
if payload.CommandUUID == " " {
t.Error("CommandUUID should be a generated UUID")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var payload, _ = NewCommandPayload(&tt.request)
tt.testFn(t, payload)
})
}
}
func mustLoadFile(t *testing.T, filename string) []byte {
t.Helper()

View File

@@ -11,12 +11,14 @@ func (c *CommandRequest) UnmarshalJSON(data []byte) error {
var request = struct {
UDID string `json:"udid"`
RequestType string `json:"request_type"`
CommandUUID string `json:"command_uuid"`
}{}
if err := json.Unmarshal(data, &request); err != nil {
return errors.Wrap(err, "mdm: unmarshal json command request")
}
c.UDID = request.UDID
c.Command = &Command{}
c.CommandUUID = request.CommandUUID
return c.Command.UnmarshalJSON(data)
}