-
Notifications
You must be signed in to change notification settings - Fork 82
/
handleCheckin.go
565 lines (467 loc) · 15.5 KB
/
handleCheckin.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package api
import (
"bytes"
"compress/flate"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"reflect"
"time"
"github.com/elastic/fleet-server/v7/internal/pkg/action"
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/cache"
"github.com/elastic/fleet-server/v7/internal/pkg/checkin"
"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/elastic/fleet-server/v7/internal/pkg/dl"
"github.com/elastic/fleet-server/v7/internal/pkg/logger"
"github.com/elastic/fleet-server/v7/internal/pkg/model"
"github.com/elastic/fleet-server/v7/internal/pkg/monitor"
"github.com/elastic/fleet-server/v7/internal/pkg/policy"
"github.com/elastic/fleet-server/v7/internal/pkg/smap"
"github.com/elastic/fleet-server/v7/internal/pkg/sqn"
"github.com/hashicorp/go-version"
"github.com/julienschmidt/httprouter"
"github.com/miolini/datacounter"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var (
ErrAgentNotFound = errors.New("agent not found")
ErrNoPolicyOutput = errors.New("output section not found")
ErrFailInjectAPIKey = errors.New("failure to inject api key")
)
const (
kEncodingGzip = "gzip"
)
//nolint:dupl // function body calls different internal hander then handleAck
func (rt *Router) handleCheckin(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
start := time.Now()
id := ps.ByName("id")
reqID := r.Header.Get(logger.HeaderRequestID)
zlog := log.With().
Str(LogAgentID, id).
Str(ECSHTTPRequestID, reqID).
Logger()
err := rt.ct.handleCheckin(&zlog, w, r, id)
if err != nil {
cntCheckin.IncError(err)
resp := NewHTTPErrResp(err)
zlog.WithLevel(resp.Level).
Err(err).
Int(ECSHTTPResponseCode, resp.StatusCode).
Int64(ECSEventDuration, time.Since(start).Nanoseconds()).
Msg("fail checkin")
if err := resp.Write(w); err != nil {
zlog.Error().Err(err).Msg("fail writing error response")
}
}
}
type CheckinT struct {
verCon version.Constraints
cfg *config.Server
cache cache.Cache
bc *checkin.Bulk
pm policy.Monitor
gcp monitor.GlobalCheckpointProvider
ad *action.Dispatcher
tr *action.TokenResolver
bulker bulk.Bulk
}
func NewCheckinT(
verCon version.Constraints,
cfg *config.Server,
c cache.Cache,
bc *checkin.Bulk,
pm policy.Monitor,
gcp monitor.GlobalCheckpointProvider,
ad *action.Dispatcher,
tr *action.TokenResolver,
bulker bulk.Bulk,
) *CheckinT {
ct := &CheckinT{
verCon: verCon,
cfg: cfg,
cache: c,
bc: bc,
pm: pm,
gcp: gcp,
ad: ad,
tr: tr,
bulker: bulker,
}
return ct
}
func (ct *CheckinT) handleCheckin(zlog *zerolog.Logger, w http.ResponseWriter, r *http.Request, id string) error {
start := time.Now()
agent, err := authAgent(r, &id, ct.bulker, ct.cache)
if err != nil {
return err
}
// Pointer is passed in to allow UpdateContext by child function
zlog.UpdateContext(func(ctx zerolog.Context) zerolog.Context {
return ctx.Str(LogAccessAPIKeyID, agent.AccessAPIKeyID)
})
ver, err := validateUserAgent(*zlog, r, ct.verCon)
if err != nil {
return err
}
// Safely check if the agent version is different, return empty string otherwise
newVer := agent.CheckDifferentVersion(ver)
return ct.processRequest(*zlog, w, r, start, agent, newVer)
}
func (ct *CheckinT) processRequest(zlog zerolog.Logger, w http.ResponseWriter, r *http.Request, start time.Time, agent *model.Agent, ver string) error {
ctx := r.Context()
body := r.Body
// Limit the size of the body to prevent malicious agent from exhausting RAM in server
if ct.cfg.Limits.CheckinLimit.MaxBody > 0 {
body = http.MaxBytesReader(w, body, ct.cfg.Limits.CheckinLimit.MaxBody)
}
readCounter := datacounter.NewReaderCounter(body)
var req CheckinRequest
decoder := json.NewDecoder(readCounter)
if err := decoder.Decode(&req); err != nil {
return errors.Wrap(err, "decode checkin request")
}
cntCheckin.bodyIn.Add(readCounter.Count())
// Compare local_metadata content and update if different
rawMeta, err := parseMeta(zlog, agent, &req)
if err != nil {
return err
}
// Resolve AckToken from request, fallback on the agent record
seqno, err := ct.resolveSeqNo(ctx, zlog, req, agent)
if err != nil {
return err
}
// Subscribe to actions dispatcher
aSub := ct.ad.Subscribe(agent.Id, seqno)
defer ct.ad.Unsubscribe(aSub)
actCh := aSub.Ch()
// Subscribe to policy manager for changes on PolicyId > policyRev
sub, err := ct.pm.Subscribe(agent.Id, agent.PolicyID, agent.PolicyRevisionIdx, agent.PolicyCoordinatorIdx)
if err != nil {
return errors.Wrap(err, "subscribe policy monitor")
}
defer func() {
err := ct.pm.Unsubscribe(sub)
if err != nil {
zlog.Error().Err(err).Str("policy_id", agent.PolicyID).Msg("unable to unsubscribe from policy")
}
}()
// Update check-in timestamp on timeout
tick := time.NewTicker(ct.cfg.Timeouts.CheckinTimestamp)
defer tick.Stop()
setupDuration := time.Since(start)
pollDuration, jitter := calcPollDuration(zlog, ct.cfg, setupDuration)
zlog.Debug().
Str("status", req.Status).
Str("seqNo", seqno.String()).
Dur("setupDuration", setupDuration).
Dur("jitter", jitter).
Dur("pollDuration", pollDuration).
Uint64("bodyCount", readCounter.Count()).
Msg("checkin start long poll")
// Chill out for a bit. Long poll.
longPoll := time.NewTicker(pollDuration)
defer longPoll.Stop()
// Initial update on checkin, and any user fields that might have changed
err = ct.bc.CheckIn(agent.Id, req.Status, req.Message, rawMeta, seqno, ver)
if err != nil {
zlog.Error().Err(err).Str("agent_id", agent.Id).Msg("checkin failed")
}
// Initial fetch for pending actions
var (
actions []ActionResp
ackToken string
)
// Check agent pending actions first
pendingActions, err := ct.fetchAgentPendingActions(ctx, seqno, agent.Id)
if err != nil {
return err
}
actions, ackToken = convertActions(agent.Id, pendingActions)
if len(actions) == 0 {
LOOP:
for {
select {
case <-ctx.Done():
return ctx.Err()
case acdocs := <-actCh:
var acs []ActionResp
acs, ackToken = convertActions(agent.Id, acdocs)
actions = append(actions, acs...)
break LOOP
case policy := <-sub.Output():
actionResp, err := processPolicy(ctx, zlog, ct.bulker, agent.Id, policy)
if err != nil {
return errors.Wrap(err, "processPolicy")
}
actions = append(actions, *actionResp)
break LOOP
case <-longPoll.C:
zlog.Trace().Msg("fire long poll")
break LOOP
case <-tick.C:
err := ct.bc.CheckIn(agent.Id, req.Status, req.Message, nil, nil, ver)
if err != nil {
zlog.Error().Err(err).Str("agent_id", agent.Id).Msg("checkin failed")
}
}
}
}
for _, action := range actions {
zlog.Info().
Str("ackToken", ackToken).
Str("createdAt", action.CreatedAt).
Str("id", action.ID).
Str("type", action.Type).
Str("inputType", action.InputType).
Int64("timeout", action.Timeout).
Msg("Action delivered to agent on checkin")
}
resp := CheckinResponse{
AckToken: ackToken,
Action: "checkin",
Actions: actions,
}
return ct.writeResponse(zlog, w, r, resp)
}
func (ct *CheckinT) writeResponse(zlog zerolog.Logger, w http.ResponseWriter, r *http.Request, resp CheckinResponse) error {
payload, err := json.Marshal(&resp)
if err != nil {
return errors.Wrap(err, "writeResponse marshal")
}
compressionLevel := ct.cfg.CompressionLevel
compressThreshold := ct.cfg.CompressionThresh
if len(payload) > compressThreshold && compressionLevel != flate.NoCompression && acceptsEncoding(r, kEncodingGzip) {
wrCounter := datacounter.NewWriterCounter(w)
zipper, err := gzip.NewWriterLevel(wrCounter, compressionLevel)
if err != nil {
return errors.Wrap(err, "writeResponse new gzip")
}
w.Header().Set("Content-Encoding", kEncodingGzip)
if _, err = zipper.Write(payload); err != nil {
return errors.Wrap(err, "writeResponse gzip write")
}
if err = zipper.Close(); err != nil {
err = errors.Wrap(err, "writeResponse gzip close")
}
cntCheckin.bodyOut.Add(wrCounter.Count())
zlog.Trace().
Err(err).
Int("lvl", compressionLevel).
Int("srcSz", len(payload)).
Uint64("dstSz", wrCounter.Count()).
Msg("compressing checkin response")
} else {
var nWritten int
nWritten, err = w.Write(payload)
cntCheckin.bodyOut.Add(uint64(nWritten))
if err != nil {
err = errors.Wrap(err, "writeResponse payload")
}
}
return err
}
func acceptsEncoding(r *http.Request, encoding string) bool {
for _, v := range r.Header.Values("Accept-Encoding") {
if v == encoding {
return true
}
}
return false
}
// Resolve AckToken from request, fallback on the agent record
func (ct *CheckinT) resolveSeqNo(ctx context.Context, zlog zerolog.Logger, req CheckinRequest, agent *model.Agent) (sqn.SeqNo, error) {
var err error
// Resolve AckToken from request, fallback on the agent record
ackToken := req.AckToken
var seqno sqn.SeqNo = agent.ActionSeqNo
if ct.tr != nil && ackToken != "" {
var sn int64
sn, err = ct.tr.Resolve(ctx, ackToken)
if err != nil {
if errors.Is(err, dl.ErrNotFound) {
zlog.Debug().Str("token", ackToken).Msg("revision token not found")
err = nil
} else {
return seqno, errors.Wrap(err, "resolveSeqNo")
}
}
seqno = []int64{sn}
}
return seqno, err
}
func (ct *CheckinT) fetchAgentPendingActions(ctx context.Context, seqno sqn.SeqNo, agentID string) ([]model.Action, error) {
actions, err := dl.FindAgentActions(ctx, ct.bulker, seqno, ct.gcp.GetCheckpoint(), agentID)
if err != nil {
return nil, errors.Wrap(err, "fetchAgentPendingActions")
}
return actions, err
}
func convertActions(agentID string, actions []model.Action) ([]ActionResp, string) {
var ackToken string
sz := len(actions)
respList := make([]ActionResp, 0, sz)
for _, action := range actions {
respList = append(respList, ActionResp{
AgentID: agentID,
CreatedAt: action.Timestamp,
StartTime: action.StartTime,
Expiration: action.Expiration,
Data: action.Data,
ID: action.ActionID,
Type: action.Type,
InputType: action.InputType,
Timeout: action.Timeout,
})
}
if sz > 0 {
ackToken = actions[sz-1].Id
}
return respList, ackToken
}
// A new policy exists for this agent. Perform the following:
// - Generate and update default ApiKey if roles have changed.
// - Rewrite the policy for delivery to the agent injecting the key material.
//
func processPolicy(ctx context.Context, zlog zerolog.Logger, bulker bulk.Bulk, agentID string, pp *policy.ParsedPolicy) (*ActionResp, error) {
zlog = zlog.With().
Str("fleet.ctx", "processPolicy").
Int64("fleet.policyRevision", pp.Policy.RevisionIdx).
Int64("fleet.policyCoordinator", pp.Policy.CoordinatorIdx).
Str(LogPolicyID, pp.Policy.PolicyID).
Logger()
// Repull and decode the agent object. Do not trust the cache.
agent, err := dl.FindAgent(ctx, bulker, dl.QueryAgentByID, dl.FieldID, agentID)
if err != nil {
zlog.Error().Err(err).Msg("fail find agent record")
return nil, err
}
// Parse the outputs maps in order to prepare the outputs
const outputsProperty = "outputs"
outputs, err := smap.Parse(pp.Fields[outputsProperty])
if err != nil {
return nil, err
}
if outputs == nil {
return nil, ErrNoPolicyOutput
}
// Iterate through the policy outputs and prepare them
for _, policyOutput := range pp.Outputs {
err = policyOutput.Prepare(ctx, zlog, bulker, &agent, outputs)
if err != nil {
return nil, fmt.Errorf("failed to prepare output %q: %w",
policyOutput.Name, err)
}
}
outputRaw, err := json.Marshal(outputs)
if err != nil {
return nil, err
}
// Dupe field map; pp is immutable
fields := make(map[string]json.RawMessage, len(pp.Fields))
for k, v := range pp.Fields {
fields[k] = v
}
// Update only the output fields to avoid duping the whole map
fields[outputsProperty] = json.RawMessage(outputRaw)
rewrittenPolicy := struct {
Policy map[string]json.RawMessage `json:"policy"`
}{fields}
r := policy.RevisionFromPolicy(pp.Policy)
resp := ActionResp{
AgentID: agent.Id,
CreatedAt: pp.Policy.Timestamp,
Data: rewrittenPolicy,
ID: r.String(),
Type: TypePolicyChange,
}
return &resp, nil
}
func findAgentByAPIKeyID(ctx context.Context, bulker bulk.Bulk, id string) (*model.Agent, error) {
agent, err := dl.FindAgent(ctx, bulker, dl.QueryAgentByAssessAPIKeyID, dl.FieldAccessAPIKeyID, id)
if err != nil {
if errors.Is(err, dl.ErrNotFound) {
err = ErrAgentNotFound
} else {
err = errors.Wrap(err, "findAgentByApiKeyId")
}
}
return &agent, err
}
// parseMeta compares the agent and the request local_metadata content
// and returns fields to update the agent record or nil
func parseMeta(zlog zerolog.Logger, agent *model.Agent, req *CheckinRequest) ([]byte, error) {
// Quick comparison first; compare the JSON payloads.
// If the data is not consistently normalized, this short-circuit will not work.
if bytes.Equal(req.LocalMeta, agent.LocalMetadata) {
zlog.Trace().Msg("quick comparing local metadata is equal")
return nil, nil
}
// Deserialize the request metadata
var reqLocalMeta interface{}
if err := json.Unmarshal(req.LocalMeta, &reqLocalMeta); err != nil {
return nil, errors.Wrap(err, "parseMeta request")
}
// If empty, don't step on existing data
if reqLocalMeta == nil {
return nil, nil
}
// Deserialize the agent's metadata copy
var agentLocalMeta interface{}
if err := json.Unmarshal(agent.LocalMetadata, &agentLocalMeta); err != nil {
return nil, errors.Wrap(err, "parseMeta local")
}
var outMeta []byte
// Compare the deserialized meta structures and return the bytes to update if different
if !reflect.DeepEqual(reqLocalMeta, agentLocalMeta) {
zlog.Trace().
RawJSON("oldLocalMeta", agent.LocalMetadata).
RawJSON("newLocalMeta", req.LocalMeta).
Msg("local metadata not equal")
zlog.Info().
RawJSON("req.LocalMeta", req.LocalMeta).
Msg("applying new local metadata")
outMeta = req.LocalMeta
}
return outMeta, nil
}
func calcPollDuration(zlog zerolog.Logger, cfg *config.Server, setupDuration time.Duration) (time.Duration, time.Duration) {
pollDuration := cfg.Timeouts.CheckinLongPoll
// Under heavy load, elastic may take along time to authorize the api key, many seconds to minutes.
// Short circuit the long poll to take the setup delay into account. This is particularly necessary
// in cloud where the proxy will time us out after 5m20s causing unnecessary errors.
if setupDuration >= pollDuration {
// We took so long to setup that we need to exit immediately
pollDuration = time.Millisecond
zlog.Warn().
Dur("setupDuration", setupDuration).
Dur("pollDuration", cfg.Timeouts.CheckinLongPoll).
Msg("excessive setup duration short cicuit long poll")
} else {
pollDuration -= setupDuration
if setupDuration > (time.Second * 10) {
zlog.Warn().
Dur("setupDuration", setupDuration).
Dur("pollDuration", pollDuration).
Msg("checking poll duration decreased due to slow setup")
}
}
var jitter time.Duration
if cfg.Timeouts.CheckinJitter != 0 {
jitter = time.Duration(rand.Int63n(int64(cfg.Timeouts.CheckinJitter))) //nolint:gosec // jitter time does not need to by generated from a crypto secure source
if jitter < pollDuration {
pollDuration = pollDuration - jitter
zlog.Trace().Dur("poll", pollDuration).Msg("Long poll with jitter")
}
}
return pollDuration, jitter
}