Skip to content

Commit

Permalink
Merge pull request #37 from roperzh/fix-mdm-cache-check
Browse files Browse the repository at this point in the history
ensure cached DEP status is read if a check was made in the last 24 hours
  • Loading branch information
grahamgilbert authored Aug 16, 2023
2 parents 2f4bc7c + ff6c817 commit b26b6a9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
14 changes: 8 additions & 6 deletions tables/mdm/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func getDEPStatus(status profileStatus) depStatus {
return depStatus{DEPCapable: true}
}
var depstatus depStatus
hasAlreadyChecked := hasCheckedCloudConfigInPast24Hours()
hasAlreadyChecked := hasCheckedCloudConfigInPast24Hours(CloudConfigTimerCheck)
if !hasAlreadyChecked {
cmd := exec.Command("/usr/bin/profiles", "show", "-type", "enrollment")
out, err := cmd.CombinedOutput()
Expand All @@ -225,14 +225,16 @@ func getDEPStatus(status profileStatus) depStatus {
return depstatus
}

// Returns true if the device has checked it's cloud config record in the past hour, false if the file is missing or the time is more thab 24 hours ago
func hasCheckedCloudConfigInPast24Hours() bool {
if !utils.FileExists(CloudConfigTimerCheck) {
// hasCheckedCloudConfigInPast24Hours returns true if the device has checked
// it's cloud config record in the past 24 hours, false if the file is missing
// or the time is more than 24 hours ago.
func hasCheckedCloudConfigInPast24Hours(cloudConfigPath string) bool {
if !utils.FileExists(cloudConfigPath) {
return false
}

var cloudConfigTimerCheck cloudConfigTimerCheck
plistFile, err := os.Open(CloudConfigTimerCheck)
plistFile, err := os.Open(cloudConfigPath)
if err != nil {
// could not open file
return false
Expand All @@ -251,7 +253,7 @@ func hasCheckedCloudConfigInPast24Hours() bool {
}

dayAgo := time.Now().Add(-24 * time.Hour)
return !cloudConfigTimerCheck.LastCloudConfigCheckTime.After(dayAgo)
return cloudConfigTimerCheck.LastCloudConfigCheckTime.After(dayAgo)

}

Expand Down
38 changes: 36 additions & 2 deletions tables/mdm/mdm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package mdm

import (
"context"
"fmt"
"os"
"testing"
"time"

"github.com/osquery/osquery-go/plugin/table"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -79,9 +82,26 @@ func TestGetDEPStatus(t *testing.T) {

// TestHasCheckedCloudConfigInPast24Hours tests the hasCheckedCloudConfigInPast24Hours function
func TestHasCheckedCloudConfigInPast24Hours(t *testing.T) {
result := hasCheckedCloudConfigInPast24Hours()
cases := []struct {
name string
cloudConfigContents string
want bool
}{
{"empty contents", "", false},
{"invalid xml", "invalid", false},
{"date in the past", generateCloudConfigContents(time.Now().Add(-48 * time.Hour)), false},
{"date in the last 24 hours", generateCloudConfigContents(time.Now().Add(-1 * time.Hour)), true},
}

assert.NotNil(t, result)
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "")
assert.NoError(t, err)
_, err = tmpFile.WriteString(c.cloudConfigContents)
assert.NoError(t, err)
assert.Equal(t, c.want, hasCheckedCloudConfigInPast24Hours(tmpFile.Name()))
})
}
}

// TestGetCachedDEPStatus tests the getCachedDEPStatus function
Expand All @@ -90,3 +110,17 @@ func TestGetCachedDEPStatus(t *testing.T) {

assert.NotNil(t, result)
}

func generateCloudConfigContents(t time.Time) string {
template := `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>lastCloudConfigCheckTime</key>
<date>%s</date>
</dict>
</plist>
`
return fmt.Sprintf(template, t.Format("2006-01-02T15:04:05Z"))
}

0 comments on commit b26b6a9

Please sign in to comment.