/
depsync.go
353 lines (312 loc) · 8.74 KB
/
depsync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package sync
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/micromdm/micromdm/dep"
conf "github.com/micromdm/micromdm/platform/config"
"github.com/micromdm/micromdm/platform/pubsub"
)
const (
SyncTopic = "mdm.DepSync"
syncDuration = 30 * time.Minute
cursorValidDuration = 7 * 24 * time.Hour
)
type Syncer interface{ SyncNow() }
type WatcherDB interface {
LoadCursor() (*Cursor, error)
SaveCursor(c Cursor) error
LoadAutoAssigners() ([]AutoAssigner, error)
}
type Watcher struct {
mtx sync.RWMutex
logger log.Logger
client Client
publisher pubsub.Publisher
db WatcherDB
startSync chan bool
syncNow chan bool
cursor Cursor
}
func NewWatcher(db WatcherDB, pub pubsub.PublishSubscriber, opts ...Option) (*Watcher, error) {
w := Watcher{
logger: log.NewNopLogger(),
db: db,
publisher: pub,
startSync: make(chan bool),
syncNow: make(chan bool),
}
for _, optFn := range opts {
optFn(&w)
}
cursor, err := w.db.LoadCursor()
if err != nil {
return nil, err
}
if cursor.Valid() {
level.Debug(w.logger).Log("msg", "loaded DEP config", "cursor", cursor.Value)
w.cursor = *cursor
}
if err := w.updateClient(pub); err != nil {
return nil, err
}
saveCursor := func() {
if err := db.SaveCursor(w.cursor); err != nil {
level.Info(w.logger).Log("err", err, "msg", "saving cursor")
return
}
level.Info(w.logger).Log("msg", "saved DEP config", "cursor", w.cursor.Value)
}
go func() {
defer saveCursor()
if w.client == nil {
// block until we have a DEP client to start sync process
level.Info(w.logger).Log("msg", "waiting for DEP token to be added before starting sync")
<-w.startSync
}
err := w.Run()
// the DEP sync should never end without an error, but log
// unconditionally anyway so we never silently stop watching
level.Info(w.logger).Log("err", err, "msg", "DEP watcher stopped")
}()
return &w, nil
}
type Client interface {
FetchDevices(...dep.DeviceRequestOption) (*dep.DeviceResponse, error)
SyncDevices(string, ...dep.DeviceRequestOption) (*dep.DeviceResponse, error)
AssignProfile(string, ...string) (*dep.ProfileResponse, error)
}
type Option func(*Watcher)
func WithClient(client Client) Option {
return func(w *Watcher) {
w.client = client
}
}
func WithLogger(logger log.Logger) Option {
return func(w *Watcher) {
w.logger = logger
}
}
func (w *Watcher) updateClient(pubsub pubsub.Subscriber) error {
tokenAdded, err := pubsub.Subscribe(context.TODO(), "token-events", conf.DEPTokenTopic)
if err != nil {
return err
}
go func() {
for {
select {
case event := <-tokenAdded:
var token conf.DEPToken
if err := json.Unmarshal(event.Message, &token); err != nil {
level.Info(w.logger).Log("err", err, "msg", "unmarshalling tokenAdd to token")
continue
}
client, err := token.Client()
if err != nil {
level.Info(w.logger).Log("err", err, "msg", "creating new DEP client")
continue
}
w.mtx.Lock()
w.client = client
w.mtx.Unlock()
go func() { w.startSync <- true }() // unblock Run
}
}
}()
return nil
}
func (w *Watcher) SyncNow() {
if w.client == nil {
level.Info(w.logger).Log("msg", "waiting for DEP token to be added before starting sync")
return
}
w.syncNow <- true
}
// TODO this needs to be a proper error in the micromdm/dep package.
func isCursorExhausted(err error) bool {
return strings.Contains(err.Error(), "EXHAUSTED_CURSOR")
}
func isCursorExpired(err error) bool {
return strings.Contains(err.Error(), "EXPIRED_CURSOR")
}
func isCursorInvalid(err error) bool {
return strings.Contains(err.Error(), "INVALID_CURSOR")
}
// Process DEP messages and pull out filter-matching serial numbers
// associated to profile UUIDs for auto-assignment.
func (w *Watcher) filteredAutoAssignments(devices []dep.Device) (map[string][]string, error) {
// load auto-assigners every run to make sure we get the latest set of
// auto-assigner profile UUIDs/filters. Note this makes every *watcher
// (i.e. every DEP sync instance) share the current DB set of auto-
// assigners. perhaps to refactor to be more separated.
assigners, err := w.db.LoadAutoAssigners()
if err != nil {
return nil, err
}
assigned := make(map[string][]string)
// skip looping over serials if we have no autoassigners
if len(assigners) < 1 {
return assigned, nil
}
for _, d := range devices {
// only process DEP "added" OpType messages
if d.OpType != "added" {
continue
}
// filter our devices by our assigner filters and get list of
// which devices are to be assigned to which profiles
for _, assigner := range assigners {
if assigner.Filter == "*" { // only supported filter type right now
if serials, ok := assigned[assigner.ProfileUUID]; ok {
assigned[assigner.ProfileUUID] = append(serials, d.SerialNumber)
} else {
assigned[assigner.ProfileUUID] = []string{d.SerialNumber}
}
}
}
}
return assigned, nil
}
func (w *Watcher) processAutoAssign(devices []dep.Device) error {
assignments, err := w.filteredAutoAssignments(devices)
if err != nil {
return err
}
for profileUUID, serials := range assignments {
resp, err := w.client.AssignProfile(profileUUID, serials...)
if err != nil {
level.Info(w.logger).Log(
"err", err,
"msg", "auto-assign error assigning serials to profile",
"profile", profileUUID,
)
continue
}
// count our results for logging
resultCounts := map[string]int{
"SUCCESS": 0,
"NOT_ACCESSIBLE": 0,
"FAILED": 0,
}
for _, result := range resp.Devices {
if ct, ok := resultCounts[result]; ok {
// NOTE: we're logging _only_ the above pre-defined result types
resultCounts[result] = ct + 1
}
}
// TODO: alternate strategy is to log all failed devices
// TODO: handle/requeue failed devices?
level.Info(w.logger).Log(
"msg", "DEP auto-assigned",
"profile", profileUUID,
"success", resultCounts["SUCCESS"],
"not_accessible", resultCounts["NOT_ACCESSIBLE"],
"failed", resultCounts["FAILED"],
)
}
return nil
}
func (w *Watcher) publishAndProcessDevices(devices []dep.Device) error {
e := NewEvent(devices)
data, err := MarshalEvent(e)
if err != nil {
return err
}
err = w.publisher.Publish(context.TODO(), SyncTopic, data)
if err != nil {
return err
}
// TODO: instead of directly kicking off the auto-assigner process
// consider placing a subscriber on the DEP pubsub topic. The same
// information gets marshalled but it allows us the future
// flexibility to separate out that component if we desired.
go func() {
err := w.processAutoAssign(devices)
if err != nil {
level.Info(w.logger).Log("err", err, "msg", "auto-assign error")
}
}()
return nil
}
func (w *Watcher) Run() error {
var (
err error
resp *dep.DeviceResponse
fetchNext = true
// for logging
fetchNextLabel = map[bool]string{
true: "fetch",
false: "sync",
}
)
ticker := time.NewTicker(syncDuration).C
for {
if fetchNext {
resp, err = w.client.FetchDevices(dep.Limit(100), dep.Cursor(w.cursor.Value))
if err != nil && isCursorExhausted(err) {
level.Info(w.logger).Log(
"msg", "DEP cursor returned all devices previously",
"phase", fetchNextLabel[fetchNext],
"cursor", w.cursor.Value,
)
fetchNext = false
continue
}
} else {
resp, err = w.client.SyncDevices(w.cursor.Value)
}
if err != nil && (isCursorExpired(err) || isCursorInvalid(err)) {
level.Info(w.logger).Log(
"msg", "DEP cursor error, retrying with empty cursor",
"phase", fetchNextLabel[fetchNext],
"cursor", w.cursor.Value,
"err", err,
)
w.cursor.Value = ""
fetchNext = true
continue
} else if err != nil {
// log any other error, but do not return from the run loop.
// probably just a transient network issue.
level.Info(w.logger).Log(
"msg", "error syncing DEP devices",
"phase", fetchNextLabel[fetchNext],
"cursor", w.cursor.Value,
"err", err,
)
} else {
level.Info(w.logger).Log(
"msg", "DEP sync",
"phase", fetchNextLabel[fetchNext],
"cursor", resp.Cursor,
"fetched", resp.FetchedUntil,
"devices", len(resp.Devices),
"more", resp.MoreToFollow,
)
if err := w.publishAndProcessDevices(resp.Devices); err != nil {
return fmt.Errorf("publish and process devices: %w", err)
}
w.cursor = Cursor{Value: resp.Cursor, CreatedAt: time.Now()}
if err := w.db.SaveCursor(w.cursor); err != nil {
return errors.Wrap(err, "saving cursor from fetch")
}
if resp.MoreToFollow {
continue
} else if fetchNext {
fetchNext = false
continue
}
}
select {
case <-ticker:
case <-w.syncNow:
level.Info(w.logger).Log("msg", "explicit DEP sync requested")
}
}
}