@@ -259,12 +259,19 @@ func TestKibanaFetchToken(t *testing.T) {
259
259
}
260
260
261
261
func TestShouldEnroll (t * testing.T ) {
262
- enrollmentToken := "test-token"
262
+ // enroll token
263
+ enrollmentToken := "test-enroll-token"
263
264
enrollmentTokenHash , err := crypto .GeneratePBKDF2FromPassword ([]byte (enrollmentToken ))
264
265
require .NoError (t , err )
265
266
enrollmentTokenHashBase64 := base64 .StdEncoding .EncodeToString (enrollmentTokenHash )
267
+ enrollmentTokenOther := "test-enroll-token-other"
266
268
267
- enrollmentTokenOther := "test-token-other"
269
+ // replace token
270
+ replaceToken := "test-replace-token"
271
+ replaceTokenHash , err := crypto .GeneratePBKDF2FromPassword ([]byte (replaceToken ))
272
+ require .NoError (t , err )
273
+ replaceTokenHashBase64 := base64 .StdEncoding .EncodeToString (replaceTokenHash )
274
+ replaceTokenOther := "test-replace-token-other"
268
275
269
276
fleetNetworkErr := errors .New ("fleet network error" )
270
277
for name , tc := range map [string ]struct {
@@ -289,6 +296,41 @@ func TestShouldEnroll(t *testing.T) {
289
296
cfg : setupConfig {Fleet : fleetConfig {Enroll : true , Force : true }},
290
297
expectedShouldEnroll : true ,
291
298
},
299
+ "should enroll on agent id but no existing id" : {
300
+ statFn : func (path string ) (os.FileInfo , error ) { return nil , nil },
301
+ cfg : setupConfig {Fleet : fleetConfig {Enroll : true , URL : "host1" , ID : "diff-agent-id" }},
302
+ encryptedDiskStoreFn : func (t * testing.T , savedConfig * configuration.Configuration ) storage.Storage {
303
+ m := mockStorage .NewStorage (t )
304
+ m .On ("Load" ).Return (io .NopCloser (strings .NewReader (`fleet:
305
+ enabled: true
306
+ access_api_key: "test-key"
307
+ enrollment_token_hash: "test-hash"
308
+ hosts:
309
+ - host1
310
+ agent:
311
+ protocol: "https"` )), nil ).Once ()
312
+ return m
313
+ },
314
+ expectedShouldEnroll : true ,
315
+ },
316
+ "should enroll on agent id but diff agent id" : {
317
+ statFn : func (path string ) (os.FileInfo , error ) { return nil , nil },
318
+ cfg : setupConfig {Fleet : fleetConfig {Enroll : true , URL : "host1" , ID : "diff-agent-id" }},
319
+ encryptedDiskStoreFn : func (t * testing.T , savedConfig * configuration.Configuration ) storage.Storage {
320
+ m := mockStorage .NewStorage (t )
321
+ m .On ("Load" ).Return (io .NopCloser (strings .NewReader (`fleet:
322
+ enabled: true
323
+ access_api_key: "test-key"
324
+ enrollment_token_hash: "test-hash"
325
+ hosts:
326
+ - host1
327
+ agent:
328
+ id: "agent-id"
329
+ protocol: "https"` )), nil ).Once ()
330
+ return m
331
+ },
332
+ expectedShouldEnroll : true ,
333
+ },
292
334
"should enroll on fleet url change" : {
293
335
statFn : func (path string ) (os.FileInfo , error ) { return nil , nil },
294
336
cfg : setupConfig {Fleet : fleetConfig {Enroll : true , URL : "host1" }},
@@ -321,6 +363,26 @@ func TestShouldEnroll(t *testing.T) {
321
363
- host2
322
364
- host3
323
365
agent:
366
+ protocol: "https"` )), nil ).Once ()
367
+ return m
368
+ },
369
+ expectedShouldEnroll : true ,
370
+ },
371
+ "should enroll on replace token change" : {
372
+ statFn : func (path string ) (os.FileInfo , error ) { return nil , nil },
373
+ cfg : setupConfig {Fleet : fleetConfig {Enroll : true , URL : "host1" , EnrollmentToken : enrollmentToken , ReplaceToken : replaceTokenOther }},
374
+ encryptedDiskStoreFn : func (t * testing.T , savedConfig * configuration.Configuration ) storage.Storage {
375
+ m := mockStorage .NewStorage (t )
376
+ m .On ("Load" ).Return (io .NopCloser (strings .NewReader (`fleet:
377
+ enabled: true
378
+ access_api_key: "test-key"
379
+ enrollment_token_hash: "` + enrollmentTokenHashBase64 + `"
380
+ replace_token_hash: "` + replaceTokenHashBase64 + `"
381
+ hosts:
382
+ - host1
383
+ - host2
384
+ - host3
385
+ agent:
324
386
protocol: "https"` )), nil ).Once ()
325
387
return m
326
388
},
@@ -373,6 +435,44 @@ func TestShouldEnroll(t *testing.T) {
373
435
- host2
374
436
- host3
375
437
agent:
438
+ protocol: "https"` )), nil ).Once ()
439
+ return m
440
+ },
441
+ fleetClientFn : func (t * testing.T ) client.Sender {
442
+ tries := 0
443
+ m := mockFleetClient .NewSender (t )
444
+ call := m .On ("Send" , mock .Anything , mock .Anything , mock .Anything , mock .Anything , mock .Anything , mock .Anything )
445
+ call .Run (func (args mock.Arguments ) {
446
+ if tries <= 1 {
447
+ call .Return (nil , fleetNetworkErr )
448
+ } else {
449
+ call .Return (& http.Response {
450
+ StatusCode : http .StatusOK ,
451
+ Body : io .NopCloser (strings .NewReader (`{"action": "acks", "items":[]}` )),
452
+ }, nil )
453
+ }
454
+ tries ++
455
+ }).Times (3 )
456
+ return m
457
+ },
458
+ expectedShouldEnroll : false ,
459
+ },
460
+ "should not enroll on no changes with agent ID and replace token" : {
461
+ statFn : func (path string ) (os.FileInfo , error ) { return nil , nil },
462
+ cfg : setupConfig {Fleet : fleetConfig {Enroll : true , URL : "host1" , ID : "custom-id" , EnrollmentToken : enrollmentToken , ReplaceToken : replaceToken }},
463
+ encryptedDiskStoreFn : func (t * testing.T , savedConfig * configuration.Configuration ) storage.Storage {
464
+ m := mockStorage .NewStorage (t )
465
+ m .On ("Load" ).Return (io .NopCloser (strings .NewReader (`fleet:
466
+ enabled: true
467
+ access_api_key: "test-key"
468
+ enrollment_token_hash: "` + enrollmentTokenHashBase64 + `"
469
+ replace_token_hash: "` + replaceTokenHashBase64 + `"
470
+ hosts:
471
+ - host1
472
+ - host2
473
+ - host3
474
+ agent:
475
+ id: "custom-id"
376
476
protocol: "https"` )), nil ).Once ()
377
477
return m
378
478
},
@@ -433,6 +533,33 @@ func TestShouldEnroll(t *testing.T) {
433
533
- host2
434
534
- host3
435
535
agent:
536
+ protocol: "https"` )), nil ).Once ()
537
+ return m
538
+ },
539
+ fleetClientFn : func (t * testing.T ) client.Sender {
540
+ m := mockFleetClient .NewSender (t )
541
+ m .On ("Send" , mock .Anything , mock .Anything , mock .Anything , mock .Anything , mock .Anything , mock .Anything ).
542
+ Return (& http.Response {
543
+ StatusCode : http .StatusOK ,
544
+ Body : io .NopCloser (strings .NewReader (`{"action": "acks", "items":[]}` )),
545
+ }, nil ).Once ()
546
+ return m
547
+ },
548
+ expectedShouldEnroll : false ,
549
+ },
550
+ "should not update the replace token hash if it does not exist in setup configuration" : {
551
+ statFn : func (path string ) (os.FileInfo , error ) { return nil , nil },
552
+ cfg : setupConfig {Fleet : fleetConfig {Enroll : true , URL : "host1" , EnrollmentToken : "" , ReplaceToken : "" }},
553
+ encryptedDiskStoreFn : func (t * testing.T , savedConfig * configuration.Configuration ) storage.Storage {
554
+ m := mockStorage .NewStorage (t )
555
+ m .On ("Load" ).Return (io .NopCloser (strings .NewReader (`fleet:
556
+ enabled: true
557
+ access_api_key: "test-key"
558
+ hosts:
559
+ - host1
560
+ - host2
561
+ - host3
562
+ agent:
436
563
protocol: "https"` )), nil ).Once ()
437
564
return m
438
565
},
@@ -486,6 +613,48 @@ func TestShouldEnroll(t *testing.T) {
486
613
},
487
614
expectedShouldEnroll : false ,
488
615
},
616
+ "should not enroll on no changes and update the stored enrollment and replace token hash" : {
617
+ statFn : func (path string ) (os.FileInfo , error ) { return nil , nil },
618
+ cfg : setupConfig {Fleet : fleetConfig {Enroll : true , URL : "host1" , EnrollmentToken : enrollmentToken , ReplaceToken : replaceToken }},
619
+ encryptedDiskStoreFn : func (t * testing.T , savedConfig * configuration.Configuration ) storage.Storage {
620
+ m := mockStorage .NewStorage (t )
621
+ m .On ("Load" ).Return (io .NopCloser (strings .NewReader (`fleet:
622
+ enabled: true
623
+ access_api_key: "test-key"
624
+ hosts:
625
+ - host1
626
+ - host2
627
+ - host3
628
+ agent:
629
+ protocol: "https"` )), nil ).Once ()
630
+ m .On ("Save" , mock .Anything ).Run (func (args mock.Arguments ) {
631
+ reader := args .Get (0 ).(io.Reader )
632
+ data , _ := io .ReadAll (reader )
633
+ _ = yaml .Unmarshal (data , savedConfig )
634
+ }).Return (nil ).Times (0 )
635
+ return m
636
+ },
637
+ fleetClientFn : func (t * testing.T ) client.Sender {
638
+ m := mockFleetClient .NewSender (t )
639
+ m .On ("Send" , mock .Anything , mock .Anything , mock .Anything , mock .Anything , mock .Anything , mock .Anything ).
640
+ Return (& http.Response {
641
+ StatusCode : http .StatusOK ,
642
+ Body : io .NopCloser (strings .NewReader (`{"action": "acks", "items":[]}` )),
643
+ }, nil ).Once ()
644
+ return m
645
+ },
646
+ expectedSavedConfig : func (t * testing.T , savedConfig * configuration.Configuration ) {
647
+ require .NotNil (t , savedConfig )
648
+ require .NotNil (t , savedConfig .Fleet )
649
+ enrollmentTokenHash , err := base64 .StdEncoding .DecodeString (savedConfig .Fleet .EnrollmentTokenHash )
650
+ require .NoError (t , err )
651
+ require .NoError (t , crypto .ComparePBKDF2HashAndPassword (enrollmentTokenHash , []byte (enrollmentToken )))
652
+ replaceTokenHash , err := base64 .StdEncoding .DecodeString (savedConfig .Fleet .ReplaceTokenHash )
653
+ require .NoError (t , err )
654
+ require .NoError (t , crypto .ComparePBKDF2HashAndPassword (replaceTokenHash , []byte (replaceToken )))
655
+ },
656
+ expectedShouldEnroll : false ,
657
+ },
489
658
} {
490
659
t .Run (name , func (t * testing.T ) {
491
660
savedConfig := & configuration.Configuration {}
0 commit comments