mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-13 05:45:41 +08:00
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:
28
connect/middleware.go
Normal file
28
connect/middleware.go
Normal 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)
|
||||
}
|
||||
@@ -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":
|
||||
|
||||
1
main.go
1
main.go
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user