@@ -394,48 +394,59 @@ public void testAutoReload() throws Exception {
394
394
assertThat (role .cluster ().check ("cluster:admin/foo/bar" , request , authentication ), is (false ));
395
395
396
396
// truncate to remove some
397
- final Set <String > truncatedFileRolesModified = new HashSet <>();
397
+ // Not asserting exact content of the role change set since file truncation and subsequent are not
398
+ // atomic and hence can result in different change set to be reported.
398
399
final CountDownLatch truncateLatch = new CountDownLatch (1 );
399
400
store = new FileRolesStore (settings , env , watcherService , roleSet -> {
400
- truncatedFileRolesModified .addAll (roleSet );
401
- truncateLatch .countDown ();
401
+ if (roleSet .contains ("dummy1" )) {
402
+ truncateLatch .countDown ();
403
+ }
402
404
}, new XPackLicenseState (Settings .EMPTY ), xContentRegistry ());
403
405
404
406
final Set <String > allRolesPreTruncate = store .getAllRoleNames ();
407
+ assertTrue (allRolesPreTruncate .contains ("role5" ));
408
+ // Use a marker role so that when the countdown latch is triggered,
409
+ // we are sure it is triggered by the new file content instead of the initial truncation
405
410
try (BufferedWriter writer = Files .newBufferedWriter (tmp , StandardCharsets .UTF_8 , StandardOpenOption .TRUNCATE_EXISTING )) {
406
411
writer .append ("role5:" ).append (System .lineSeparator ());
407
412
writer .append (" cluster:" ).append (System .lineSeparator ());
408
- writer .append (" - 'MONITOR'" );
413
+ writer .append (" - 'MONITOR'" ).append (System .lineSeparator ());
414
+ writer .append ("dummy1:" ).append (System .lineSeparator ());
415
+ writer .append (" cluster:" ).append (System .lineSeparator ());
416
+ writer .append (" - 'ALL'" );
409
417
}
410
418
411
- truncateLatch .await ();
412
- assertEquals (allRolesPreTruncate .size () - 1 , truncatedFileRolesModified .size ());
413
- assertTrue (allRolesPreTruncate .contains ("role5" ));
414
- assertFalse (truncatedFileRolesModified .contains ("role5" ));
419
+ assertTrue (truncateLatch .await (5 , TimeUnit .SECONDS ));
415
420
descriptors = store .roleDescriptors (Collections .singleton ("role5" ));
416
421
assertThat (descriptors , notNullValue ());
417
422
assertEquals (1 , descriptors .size ());
423
+ assertArrayEquals (new String []{"MONITOR" }, descriptors .iterator ().next ().getClusterPrivileges ());
418
424
419
425
// modify
420
426
final Set <String > modifiedFileRolesModified = new HashSet <>();
421
427
final CountDownLatch modifyLatch = new CountDownLatch (1 );
422
428
store = new FileRolesStore (settings , env , watcherService , roleSet -> {
423
429
modifiedFileRolesModified .addAll (roleSet );
424
- modifyLatch .countDown ();
430
+ if (roleSet .contains ("dummy2" )) {
431
+ modifyLatch .countDown ();
432
+ }
425
433
}, new XPackLicenseState (Settings .EMPTY ), xContentRegistry ());
426
434
427
435
try (BufferedWriter writer = Files .newBufferedWriter (tmp , StandardCharsets .UTF_8 , StandardOpenOption .TRUNCATE_EXISTING )) {
428
436
writer .append ("role5:" ).append (System .lineSeparator ());
429
437
writer .append (" cluster:" ).append (System .lineSeparator ());
438
+ writer .append (" - 'ALL'" ).append (System .lineSeparator ());
439
+ writer .append ("dummy2:" ).append (System .lineSeparator ());
440
+ writer .append (" cluster:" ).append (System .lineSeparator ());
430
441
writer .append (" - 'ALL'" );
431
442
}
432
443
433
- modifyLatch .await ();
434
- assertEquals (1 , modifiedFileRolesModified .size ());
444
+ assertTrue (modifyLatch .await (5 , TimeUnit .SECONDS ));
435
445
assertTrue (modifiedFileRolesModified .contains ("role5" ));
436
446
descriptors = store .roleDescriptors (Collections .singleton ("role5" ));
437
447
assertThat (descriptors , notNullValue ());
438
448
assertEquals (1 , descriptors .size ());
449
+ assertArrayEquals (new String []{"ALL" }, descriptors .iterator ().next ().getClusterPrivileges ());
439
450
} finally {
440
451
if (watcherService != null ) {
441
452
watcherService .stop ();
0 commit comments