1717import org .elasticsearch .action .support .ActionTestUtils ;
1818import org .elasticsearch .action .support .IndicesOptions ;
1919import org .elasticsearch .action .support .PlainActionFuture ;
20+ import org .elasticsearch .action .support .RefCountingRunnable ;
2021import org .elasticsearch .cluster .metadata .ProjectId ;
2122import org .elasticsearch .cluster .node .DiscoveryNode ;
2223import org .elasticsearch .cluster .node .DiscoveryNodeRole ;
@@ -1625,9 +1626,12 @@ public void testUpdateRemoteClusterCredentialsRebuildsConnectionWithCorrectProfi
16251626 {
16261627 final MockSecureSettings secureSettings = new MockSecureSettings ();
16271628 secureSettings .setString ("cluster.remote.cluster_1.credentials" , randomAlphaOfLength (10 ));
1628- final PlainActionFuture <Void > listener = new PlainActionFuture <>();
1629+ final PlainActionFuture <RemoteClusterService . RemoteClusterConnectionStatus > listener = new PlainActionFuture <>();
16291630 final Settings settings = Settings .builder ().put (clusterSettings ).setSecureSettings (secureSettings ).build ();
1630- service .updateRemoteClusterCredentials (() -> settings , listener );
1631+ final var result = service .getRemoteClusterCredentialsManager ().updateClusterCredentials (settings );
1632+ assertThat (result .addedClusterAliases (), equalTo (Set .of ("cluster_1" )));
1633+ final var config = buildLinkedProjectConfig ("cluster_1" , Settings .EMPTY , settings );
1634+ service .updateRemoteCluster (config , true , listener );
16311635 listener .actionGet (10 , TimeUnit .SECONDS );
16321636 }
16331637
@@ -1637,12 +1641,13 @@ public void testUpdateRemoteClusterCredentialsRebuildsConnectionWithCorrectProfi
16371641 );
16381642
16391643 {
1640- final PlainActionFuture <Void > listener = new PlainActionFuture <>();
1641- service .updateRemoteClusterCredentials (
1642- // Settings without credentials constitute credentials removal
1643- () -> clusterSettings ,
1644- listener
1645- );
1644+ final PlainActionFuture <RemoteClusterService .RemoteClusterConnectionStatus > listener = new PlainActionFuture <>();
1645+ // Settings without credentials constitute credentials removal
1646+ final var result = service .getRemoteClusterCredentialsManager ().updateClusterCredentials (clusterSettings );
1647+ assertThat (result .addedClusterAliases ().size (), equalTo (0 ));
1648+ assertThat (result .removedClusterAliases (), equalTo (Set .of ("cluster_1" )));
1649+ final var config = buildLinkedProjectConfig ("cluster_1" , Settings .EMPTY , clusterSettings );
1650+ service .updateRemoteCluster (config , true , listener );
16461651 listener .actionGet (10 , TimeUnit .SECONDS );
16471652 }
16481653
@@ -1718,6 +1723,8 @@ public void testUpdateRemoteClusterCredentialsRebuildsMultipleConnectionsDespite
17181723 assertConnectionHasProfile (service .getRemoteClusterConnection (goodCluster ), "default" );
17191724 assertConnectionHasProfile (service .getRemoteClusterConnection (badCluster ), "default" );
17201725 expectThrows (NoSuchRemoteClusterException .class , () -> service .getRemoteClusterConnection (missingCluster ));
1726+ final Set <String > aliases = Set .of (badCluster , goodCluster , missingCluster );
1727+ final ActionListener <RemoteClusterService .RemoteClusterConnectionStatus > noop = ActionListener .noop ();
17211728
17221729 {
17231730 final MockSecureSettings secureSettings = new MockSecureSettings ();
@@ -1730,7 +1737,14 @@ public void testUpdateRemoteClusterCredentialsRebuildsMultipleConnectionsDespite
17301737 .put (cluster2Settings )
17311738 .setSecureSettings (secureSettings )
17321739 .build ();
1733- service .updateRemoteClusterCredentials (() -> settings , listener );
1740+ final var result = service .getRemoteClusterCredentialsManager ().updateClusterCredentials (settings );
1741+ assertThat (result .addedClusterAliases (), equalTo (aliases ));
1742+ try (var connectionRefs = new RefCountingRunnable (() -> listener .onResponse (null ))) {
1743+ for (String alias : aliases ) {
1744+ final var config = buildLinkedProjectConfig (alias , Settings .EMPTY , settings );
1745+ service .updateRemoteCluster (config , true , ActionListener .releaseAfter (noop , connectionRefs .acquire ()));
1746+ }
1747+ }
17341748 listener .actionGet (10 , TimeUnit .SECONDS );
17351749 }
17361750
@@ -1747,11 +1761,16 @@ public void testUpdateRemoteClusterCredentialsRebuildsMultipleConnectionsDespite
17471761 {
17481762 final PlainActionFuture <Void > listener = new PlainActionFuture <>();
17491763 final Settings settings = Settings .builder ().put (cluster1Settings ).put (cluster2Settings ).build ();
1750- service .updateRemoteClusterCredentials (
1751- // Settings without credentials constitute credentials removal
1752- () -> settings ,
1753- listener
1754- );
1764+ // Settings without credentials constitute credentials removal
1765+ final var result = service .getRemoteClusterCredentialsManager ().updateClusterCredentials (settings );
1766+ assertThat (result .addedClusterAliases ().size (), equalTo (0 ));
1767+ assertThat (result .removedClusterAliases (), equalTo (aliases ));
1768+ try (var connectionRefs = new RefCountingRunnable (() -> listener .onResponse (null ))) {
1769+ for (String alias : aliases ) {
1770+ final var config = buildLinkedProjectConfig (alias , Settings .EMPTY , settings );
1771+ service .updateRemoteCluster (config , true , ActionListener .releaseAfter (noop , connectionRefs .acquire ()));
1772+ }
1773+ }
17551774 listener .actionGet (10 , TimeUnit .SECONDS );
17561775 }
17571776
@@ -1828,6 +1847,12 @@ public void testLogsConnectionResult() throws IOException {
18281847 }
18291848 }
18301849
1850+ @ FixForMultiProject (description = "Refactor to add the linked project ID associated with the alias." )
1851+ private LinkedProjectConfig buildLinkedProjectConfig (String alias , Settings staticSettings , Settings newSettings ) {
1852+ final var mergedSettings = Settings .builder ().put (staticSettings , false ).put (newSettings , false ).build ();
1853+ return RemoteClusterSettings .toConfig (projectResolver .getProjectId (), ProjectId .DEFAULT , alias , mergedSettings );
1854+ }
1855+
18311856 private void updateRemoteCluster (
18321857 RemoteClusterService service ,
18331858 String alias ,
@@ -1846,10 +1871,7 @@ private void updateRemoteCluster(
18461871 Settings newSettings ,
18471872 ActionListener <RemoteClusterService .RemoteClusterConnectionStatus > listener
18481873 ) {
1849- final var mergedSettings = Settings .builder ().put (settings , false ).put (newSettings , false ).build ();
1850- @ FixForMultiProject (description = "Refactor to add the linked project ID associated with the alias." )
1851- final var config = RemoteClusterSettings .toConfig (projectResolver .getProjectId (), ProjectId .DEFAULT , alias , mergedSettings );
1852- service .updateRemoteCluster (config , false , listener );
1874+ service .updateRemoteCluster (buildLinkedProjectConfig (alias , settings , newSettings ), false , listener );
18531875 }
18541876
18551877 private void initializeRemoteClusters (RemoteClusterService remoteClusterService ) {
0 commit comments