52
52
import static org .elasticsearch .test .MockLog .assertThatLogger ;
53
53
import static org .elasticsearch .test .NodeRoles .masterOnlyNode ;
54
54
import static org .elasticsearch .test .NodeRoles .nonMasterNode ;
55
+ import static org .elasticsearch .test .NodeRoles .onlyRole ;
55
56
import static org .elasticsearch .test .NodeRoles .onlyRoles ;
56
57
import static org .elasticsearch .test .NodeRoles .removeRoles ;
57
58
import static org .hamcrest .Matchers .containsString ;
@@ -441,7 +442,7 @@ public void testGroupIndicesWithoutRemoteClusterClientRole() throws Exception {
441
442
Sets .newHashSet (DiscoveryNodeRole .DATA_ROLE )
442
443
);
443
444
try (RemoteClusterService service = new RemoteClusterService (settings , null )) {
444
- assertFalse ( service . isEnabled () );
445
+ expectThrows ( IllegalArgumentException . class , service :: ensureClientIsEnabled );
445
446
assertFalse (hasRegisteredClusters (service ));
446
447
final IllegalArgumentException error = expectThrows (
447
448
IllegalArgumentException .class ,
@@ -1383,7 +1384,10 @@ public void testSkipUnavailable() {
1383
1384
}
1384
1385
1385
1386
public void testRemoteClusterServiceNotEnabledGetRemoteClusterConnection () {
1386
- final Settings settings = removeRoles (Set .of (DiscoveryNodeRole .REMOTE_CLUSTER_CLIENT_ROLE ));
1387
+ final Settings settings = Settings .builder ()
1388
+ .put (removeRoles (Set .of (DiscoveryNodeRole .REMOTE_CLUSTER_CLIENT_ROLE )))
1389
+ .put (Node .NODE_NAME_SETTING .getKey (), "node-1" )
1390
+ .build ();
1387
1391
try (
1388
1392
MockTransportService service = MockTransportService .createNewService (
1389
1393
settings ,
@@ -1399,12 +1403,81 @@ public void testRemoteClusterServiceNotEnabledGetRemoteClusterConnection() {
1399
1403
IllegalArgumentException .class ,
1400
1404
() -> service .getRemoteClusterService ().getRemoteClusterConnection ("test" )
1401
1405
);
1402
- assertThat (e .getMessage (), equalTo ("this node does not have the remote_cluster_client role" ));
1406
+ assertThat (e .getMessage (), equalTo ("node [node-1] does not have the [remote_cluster_client] role" ));
1407
+ }
1408
+ }
1409
+
1410
+ public void testRemoteClusterServiceEnsureClientIsEnabled () throws IOException {
1411
+ final var nodeNameSettings = Settings .builder ().put (Node .NODE_NAME_SETTING .getKey (), "node-1" ).build ();
1412
+
1413
+ // Shouldn't throw when the remote cluster client role is enabled.
1414
+ final var settingsWithRemoteClusterClientRole = Settings .builder ()
1415
+ .put (nodeNameSettings )
1416
+ .put (onlyRole (DiscoveryNodeRole .REMOTE_CLUSTER_CLIENT_ROLE ))
1417
+ .build ();
1418
+ try (RemoteClusterService service = new RemoteClusterService (settingsWithRemoteClusterClientRole , null )) {
1419
+ service .ensureClientIsEnabled ();
1420
+ }
1421
+
1422
+ // Expect throws when missing the remote cluster client role.
1423
+ final var settingsWithoutRemoteClusterClientRole = Settings .builder ()
1424
+ .put (nodeNameSettings )
1425
+ .put (removeRoles (Set .of (DiscoveryNodeRole .REMOTE_CLUSTER_CLIENT_ROLE )))
1426
+ .build ();
1427
+ try (RemoteClusterService service = new RemoteClusterService (settingsWithoutRemoteClusterClientRole , null )) {
1428
+ final var exception = expectThrows (IllegalArgumentException .class , service ::ensureClientIsEnabled );
1429
+ assertThat (exception .getMessage (), equalTo ("node [node-1] does not have the [remote_cluster_client] role" ));
1430
+ }
1431
+
1432
+ // Expect throws when missing both the remote cluster client role and search node role when stateless is enabled.
1433
+ final var statelessEnabledSettingsOnNonSearchNode = Settings .builder ()
1434
+ .put (nodeNameSettings )
1435
+ .put (onlyRole (DiscoveryNodeRole .INDEX_ROLE ))
1436
+ .put (DiscoveryNode .STATELESS_ENABLED_SETTING_NAME , true )
1437
+ .build ();
1438
+ try (RemoteClusterService service = new RemoteClusterService (statelessEnabledSettingsOnNonSearchNode , null )) {
1439
+ final var exception = expectThrows (IllegalArgumentException .class , service ::ensureClientIsEnabled );
1440
+ assertThat (
1441
+ exception .getMessage (),
1442
+ equalTo (
1443
+ "node [node-1] must have the [remote_cluster_client] role or the [search] role "
1444
+ + "in stateless environments to use linked project client features"
1445
+ )
1446
+ );
1447
+ }
1448
+
1449
+ // Shouldn't throw when stateless is enabled on a search node, or a node with remote cluster client role, or both.
1450
+ final var statelessEnabledOnSearchNodeSettings = Settings .builder ()
1451
+ .put (nodeNameSettings )
1452
+ .put (onlyRole (DiscoveryNodeRole .SEARCH_ROLE ))
1453
+ .put (DiscoveryNode .STATELESS_ENABLED_SETTING_NAME , true )
1454
+ .build ();
1455
+ try (RemoteClusterService service = new RemoteClusterService (statelessEnabledOnSearchNodeSettings , null )) {
1456
+ service .ensureClientIsEnabled ();
1457
+ }
1458
+ final var statelessEnabledOnRemoteClusterClientSettings = Settings .builder ()
1459
+ .put (nodeNameSettings )
1460
+ .put (onlyRole (DiscoveryNodeRole .REMOTE_CLUSTER_CLIENT_ROLE ))
1461
+ .put (DiscoveryNode .STATELESS_ENABLED_SETTING_NAME , true )
1462
+ .build ();
1463
+ try (RemoteClusterService service = new RemoteClusterService (statelessEnabledOnRemoteClusterClientSettings , null )) {
1464
+ service .ensureClientIsEnabled ();
1465
+ }
1466
+ final var statelessEnabledOnSearchNodeAndRemoteClusterClientSettings = Settings .builder ()
1467
+ .put (nodeNameSettings )
1468
+ .put (onlyRoles (Set .of (DiscoveryNodeRole .SEARCH_ROLE , DiscoveryNodeRole .REMOTE_CLUSTER_CLIENT_ROLE )))
1469
+ .put (DiscoveryNode .STATELESS_ENABLED_SETTING_NAME , true )
1470
+ .build ();
1471
+ try (RemoteClusterService service = new RemoteClusterService (statelessEnabledOnSearchNodeAndRemoteClusterClientSettings , null )) {
1472
+ service .ensureClientIsEnabled ();
1403
1473
}
1404
1474
}
1405
1475
1406
1476
public void testRemoteClusterServiceNotEnabledGetCollectNodes () {
1407
- final Settings settings = removeRoles (Set .of (DiscoveryNodeRole .REMOTE_CLUSTER_CLIENT_ROLE ));
1477
+ final Settings settings = Settings .builder ()
1478
+ .put (removeRoles (Set .of (DiscoveryNodeRole .REMOTE_CLUSTER_CLIENT_ROLE )))
1479
+ .put (Node .NODE_NAME_SETTING .getKey (), "node-1" )
1480
+ .build ();
1408
1481
try (
1409
1482
MockTransportService service = MockTransportService .createNewService (
1410
1483
settings ,
@@ -1420,7 +1493,7 @@ public void testRemoteClusterServiceNotEnabledGetCollectNodes() {
1420
1493
IllegalArgumentException .class ,
1421
1494
() -> service .getRemoteClusterService ().collectNodes (Set .of (), ActionListener .noop ())
1422
1495
);
1423
- assertThat (e .getMessage (), equalTo ("this node does not have the remote_cluster_client role" ));
1496
+ assertThat (e .getMessage (), equalTo ("node [ node-1] does not have the [ remote_cluster_client] role" ));
1424
1497
}
1425
1498
}
1426
1499
0 commit comments