From e4ab7e0b17e323962aa6031b4db1e81df37b9d53 Mon Sep 17 00:00:00 2001 From: mosen Date: Wed, 12 Oct 2016 16:20:48 +1100 Subject: [PATCH] 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. --- checkin/service.go | 4 +++ connect/service.go | 3 +-- device/datastore.go | 25 +++++++++++++------ device/device.go | 8 +++--- ...0120001_devices_default_timestamp_down.sql | 5 ++++ ...610120001_devices_default_timestamp_up.sql | 9 +++++++ 6 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 migrations/201610120001_devices_default_timestamp_down.sql create mode 100644 migrations/201610120001_devices_default_timestamp_up.sql diff --git a/checkin/service.go b/checkin/service.go index de155651..ba1bc8f1 100644 --- a/checkin/service.go +++ b/checkin/service.go @@ -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 diff --git a/connect/service.go b/connect/service.go index 32f00e96..f9739221 100644 --- a/connect/service.go +++ b/connect/service.go @@ -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 { diff --git a/device/datastore.go b/device/datastore.go index 8417882f..4bb0d7cf 100644 --- a/device/datastore.go +++ b/device/datastore.go @@ -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") diff --git a/device/device.go b/device/device.go index de570204..08941011 100644 --- a/device/device.go +++ b/device/device.go @@ -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"` } diff --git a/migrations/201610120001_devices_default_timestamp_down.sql b/migrations/201610120001_devices_default_timestamp_down.sql new file mode 100644 index 00000000..0136056d --- /dev/null +++ b/migrations/201610120001_devices_default_timestamp_down.sql @@ -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; diff --git a/migrations/201610120001_devices_default_timestamp_up.sql b/migrations/201610120001_devices_default_timestamp_up.sql new file mode 100644 index 00000000..b56f467d --- /dev/null +++ b/migrations/201610120001_devices_default_timestamp_up.sql @@ -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' + +