Hotfix/last check in (#34)

* Convert all time.Time pointers to value types.
Use UTC time when updating `last_checkin`.
Update `last_checkin` correctly on Authenticate, TokenUpdate, and query response.
Set database defaults to golangs zero value for time.Time which is `0001-01-01`.

* Add down migration for time defaults.
This commit is contained in:
mosen
2016-10-12 16:20:48 +11:00
committed by GitHub
parent 1cd3a459eb
commit e4ab7e0b17
6 changed files with 40 additions and 14 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/micromdm/micromdm/command"
"github.com/micromdm/micromdm/device"
"github.com/micromdm/micromdm/management"
"time"
)
// Service defines methods for and MDM Checkin service
@@ -60,6 +61,7 @@ func (svc service) Authenticate(cmd mdm.CheckinCommand) error {
MDMTopic: cmd.Topic,
Model: cmd.Model,
DeviceName: cmd.DeviceName,
LastCheckin: time.Now().UTC(),
}
_, err := svc.devices.New("authenticate", dev)
@@ -83,6 +85,8 @@ func (svc service) TokenUpdate(cmd mdm.CheckinCommand) error {
existing.UnlockToken = unlockToken
existing.AwaitingConfiguration = cmd.AwaitingConfiguration
existing.Enrolled = true
existing.LastCheckin = time.Now().UTC()
err = svc.devices.Save("tokenUpdate", existing)
if err != nil {
return err

View File

@@ -123,8 +123,7 @@ func (svc service) ackQueryResponses(req mdm.Response) error {
existing := devices[0]
now := time.Now()
existing.LastCheckin = &now
existing.LastCheckin = time.Now().UTC()
existing.LastQueryResponse, err = json.Marshal(req.QueryResponses)
if err != nil {

View File

@@ -24,9 +24,10 @@ var (
dep_profile_push_time,
dep_profile_assigned_date,
dep_profile_assigned_by,
dep_device
dep_device,
last_checkin
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
ON CONFLICT (serial_number)
DO UPDATE SET
model = $2,
@@ -39,7 +40,8 @@ var (
dep_profile_push_time = $9,
dep_profile_assigned_date = $10,
dep_profile_assigned_by = $11,
dep_device = $12
dep_device = $12,
last_checkin = $13
RETURNING device_uuid;`
authenticateMDM = `INSERT INTO devices (
@@ -50,9 +52,11 @@ var (
product_name,
serial_number,
imei,
meid
meid,
model,
last_checkin
)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
ON CONFLICT (serial_number)
DO UPDATE SET
udid=$1,
@@ -63,7 +67,8 @@ var (
serial_number=$6,
imei=$7,
meid=$8,
model=$9
model=$9,
last_checkin=$10
RETURNING device_uuid;`
selectDevicesStmt = `SELECT
@@ -151,6 +156,7 @@ func (store pgStore) New(src string, d *Device) (string, error) {
d.DEPProfileAssignedDate,
d.DEPProfileAssignedBy,
true,
time.Time{},
).Scan(&d.UUID)
if err != nil {
return "", err
@@ -168,6 +174,7 @@ func (store pgStore) New(src string, d *Device) (string, error) {
d.IMEI,
d.MEID,
d.Model,
d.LastCheckin,
).Scan(&d.UUID)
if err != nil {
return "", err
@@ -202,7 +209,8 @@ func (store pgStore) Save(msg string, dev *Device) error {
apple_push_magic=:apple_push_magic,
apple_mdm_token=:apple_mdm_token,
mdm_enrolled=:mdm_enrolled,
unlock_token=:unlock_token
unlock_token=:unlock_token,
last_checkin=:last_checkin
WHERE device_uuid=:device_uuid`
case "checkout":
stmt = `UPDATE devices SET
@@ -218,7 +226,8 @@ func (store pgStore) Save(msg string, dev *Device) error {
imei=:imei,
meid=:meid,
os_version=:os_version,
build_version=:build_version
build_version=:build_version,
last_checkin=:last_checkin
WHERE device_uuid=:device_uuid`
default:
return errors.New("device: unsupported update msg")

View File

@@ -48,11 +48,11 @@ type Device struct {
AssetTag string `json:"asset_tag,omitempty" db:"asset_tag"`
DEPProfileStatus DEPProfileStatus `json:"dep_profile_status,omitempty" db:"dep_profile_status"`
DEPProfileUUID string `json:"dep_profile_uuid,omitempty" db:"dep_profile_uuid"`
DEPProfileAssignTime *time.Time `json:"dep_profile_assign_time,omitempty" db:"dep_profile_assign_time"`
DEPProfilePushTime *time.Time `json:"dep_profile_push_time,omitempty" db:"dep_profile_push_time"`
DEPProfileAssignedDate *time.Time `json:"dep_profile_assigned_date,omitempty" db:"dep_profile_assigned_date"`
DEPProfileAssignTime time.Time `json:"dep_profile_assign_time,omitempty" db:"dep_profile_assign_time"`
DEPProfilePushTime time.Time `json:"dep_profile_push_time,omitempty" db:"dep_profile_push_time"`
DEPProfileAssignedDate time.Time `json:"dep_profile_assigned_date,omitempty" db:"dep_profile_assigned_date"`
DEPProfileAssignedBy string `json:"dep_profile_assigned_by,omitempty" db:"dep_profile_assigned_by"`
LastCheckin *time.Time `json:"last_checkin" db:"last_checkin"`
LastCheckin time.Time `json:"last_checkin" db:"last_checkin"`
DeviceName string `json:"device_name" db:"device_name"`
LastQueryResponse []byte `json:"last_query_response" db:"last_query_response"`
}

View File

@@ -0,0 +1,5 @@
ALTER TABLE devices
ALTER COLUMN dep_profile_assign_time DROP DEFAULT,
ALTER COLUMN dep_profile_push_time DROP DEFAULT,
ALTER COLUMN dep_profile_assigned_date DROP DEFAULT,
ALTER COLUMN last_checkin DROP DEFAULT;

View File

@@ -0,0 +1,9 @@
-- Add golang's zero value for time.Time as the default column value for last_checkin which allows us to pass time.Time
-- as a value type as per the docs.
ALTER TABLE devices
ALTER COLUMN dep_profile_assign_time SET DEFAULT '0001-01-01 00:00:00',
ALTER COLUMN dep_profile_push_time SET DEFAULT '0001-01-01 00:00:00',
ALTER COLUMN dep_profile_assigned_date SET DEFAULT '0001-01-01 00:00:00',
ALTER COLUMN last_checkin SET DEFAULT '0001-01-01 00:00:00'