Add service middleware for Connect service to record last_checkin each time a device contacts the MDM

Add method UpdateDeviceCheckinByUDID to device.Datastore which updates the current checkin timestamp for a given device.
Middleware set up in main.go
This commit is contained in:
Mosen
2016-06-22 19:10:38 +10:00
parent 621f5faf02
commit 15b5ec5cef
3 changed files with 38 additions and 0 deletions

28
connect/middleware.go Normal file
View File

@@ -0,0 +1,28 @@
package connect
import (
"github.com/micromdm/micromdm/device"
"golang.org/x/net/context"
)
type lastCheckinMiddleware struct {
devices device.Datastore
next Service
}
func NewMiddleware(datastore device.Datastore, next Service) (*lastCheckinMiddleware) {
return &lastCheckinMiddleware{
datastore,
next,
}
}
func (mw lastCheckinMiddleware) Acknowledge(ctx context.Context, req mdmConnectRequest) (int, error) {
mw.devices.UpdateDeviceCheckinByUDID(req.UDID)
return mw.next.Acknowledge(ctx, req)
}
func (mw lastCheckinMiddleware) NextCommand(ctx context.Context, req mdmConnectRequest) ([]byte, int, error) {
mw.devices.UpdateDeviceCheckinByUDID(req.UDID)
return mw.next.NextCommand(ctx, req)
}

View File

@@ -81,6 +81,7 @@ type Datastore interface {
GetDeviceByUDID(udid string, fields ...string) (*Device, error)
GetDeviceByUUID(uuid string, fields ...string) (*Device, error)
UpdateDeviceQueryResponseByUDID(udid string, responses mdm.QueryResponses) (error)
UpdateDeviceCheckinByUDID(udid string) (error)
Devices(params ...interface{}) ([]Device, error)
Save(msg string, dev *Device) error
}
@@ -160,6 +161,14 @@ func (store pgStore) UpdateDeviceQueryResponseByUDID(udid string, responses mdm.
return err
}
// Bump the last_checkin timestamp
func (store pgStore) UpdateDeviceCheckinByUDID(udid string) (error) {
stmt := `UPDATE devices SET last_checkin = NOW() WHERE udid = $1`
_, err := store.Exec(stmt, udid)
return err
}
func (store pgStore) New(src string, d *Device) (string, error) {
switch src {
case "fetch":

View File

@@ -187,6 +187,7 @@ func main() {
commandSvc := command.NewService(commandDB)
checkinSvc := checkin.NewService(deviceDB, mgmtSvc, commandSvc, enrollmentProfile)
connectSvc := connect.NewService(deviceDB, commandSvc)
connectSvc = connect.NewMiddleware(deviceDB, connectSvc)
httpLogger := log.NewContext(logger).With("component", "http")
managementHandler := management.ServiceHandler(ctx, mgmtSvc, httpLogger)