/ToMigrate/Raven.Tests.Raft/RaftWithAuth.cs

https://github.com/fitzchak/ravendb · C# · 183 lines · 141 code · 33 blank · 9 comment · 0 complexity · aa707144962690dc9e0e15f4a2580932 MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="RaftWithAuth.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using Rachis.Transport;
  7. using Raven.Abstractions.Data;
  8. using Raven.Abstractions.Extensions;
  9. using Raven.Database.Config;
  10. using Raven.Database.Raft;
  11. using Raven.Database.Raft.Dto;
  12. using Raven.Database.Raft.Util;
  13. using Raven.Database.Server.Security;
  14. using System;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using Raven.Client.Document;
  18. using Xunit;
  19. namespace Raven.Tests.Raft
  20. {
  21. public class RaftWithAuth : RaftTestBase
  22. {
  23. protected override void ModifyConfiguration(InMemoryRavenConfiguration configuration)
  24. {
  25. Authentication.EnableOnce();
  26. }
  27. [Fact]
  28. public async Task CanCreateClusterWithApiKeyAndSendCommandToLeader()
  29. {
  30. NodeConnectionInfo leaderNci;
  31. var leader = CreateServerWithOAuth(8079, "Ayende/abc", out leaderNci);
  32. leader.Options.ClusterManager.Value.InitializeTopology(leaderNci,forceCandidateState:true);
  33. Assert.True(leader.Options.ClusterManager.Value.Engine.WaitForLeader());
  34. NodeConnectionInfo secondConnectionInfo;
  35. CreateServerWithOAuth(8078, "Marcin/cba", out secondConnectionInfo);
  36. Assert.True(leader.Options.ClusterManager.Value.Engine.AddToClusterAsync(secondConnectionInfo).Wait(3000));
  37. //If we don't wait here we can cause two topology changes to happen at the same time and fail.
  38. WaitForClusterToBecomeNonStale(2);
  39. NodeConnectionInfo thirdConnectionInfo;
  40. CreateServerWithOAuth(8077, "User3/pass", out thirdConnectionInfo);
  41. Assert.True(leader.Options.ClusterManager.Value.Engine.AddToClusterAsync(thirdConnectionInfo).Wait(3000));
  42. Assert.True(servers[0].Options.ClusterManager.Value.IsLeader());
  43. var client = servers[0].Options.ClusterManager.Value.Client;
  44. await client.SendClusterConfigurationAsync(new ClusterConfiguration { EnableReplication = true });
  45. Assert.Equal(3, stores.Count);
  46. stores.ForEach(store =>
  47. {
  48. WaitForDocument(store.DatabaseCommands.ForSystemDatabase(), Constants.Cluster.ClusterConfigurationDocumentKey, TimeSpan.FromMinutes(1));
  49. var configurationJson = store.DatabaseCommands.ForSystemDatabase().Get(Constants.Cluster.ClusterConfigurationDocumentKey);
  50. var configuration = configurationJson.DataAsJson.JsonDeserialization<ClusterConfiguration>();
  51. Assert.True(configuration.EnableReplication);
  52. });
  53. }
  54. [Fact(Skip = "Windows credentials required.")]
  55. public async Task CanCreateClusterWithWindowsAuthAndSendCommandToLeader()
  56. {
  57. const string username = "";
  58. const string password = "";
  59. const string domain = "";
  60. Assert.NotNull(password);
  61. NodeConnectionInfo leaderNci;
  62. var leader = CreateServerWithWindowsCredentials(8079, username, password, domain, out leaderNci);
  63. leader.Options.ClusterManager.Value.InitializeTopology(leaderNci,forceCandidateState: true);
  64. Assert.True(leader.Options.ClusterManager.Value.Engine.WaitForLeader());
  65. NodeConnectionInfo secondConnectionInfo;
  66. CreateServerWithWindowsCredentials(8078, username, password, domain, out secondConnectionInfo);
  67. Assert.True(leader.Options.ClusterManager.Value.Engine.AddToClusterAsync(secondConnectionInfo).Wait(3000));
  68. //If we don't wait here we can cause two topology changes to happen at the same time and fail.
  69. WaitForClusterToBecomeNonStale(2);
  70. NodeConnectionInfo thirdConnectionInfo;
  71. CreateServerWithWindowsCredentials(8077, username, password, domain, out thirdConnectionInfo);
  72. Assert.True(leader.Options.ClusterManager.Value.Engine.AddToClusterAsync(thirdConnectionInfo).Wait(3000));
  73. Assert.True(servers[0].Options.ClusterManager.Value.IsLeader());
  74. var client = servers[0].Options.ClusterManager.Value.Client;
  75. await client.SendClusterConfigurationAsync(new ClusterConfiguration { EnableReplication = true });
  76. Assert.Equal(3, stores.Count);
  77. stores.ForEach(store =>
  78. {
  79. WaitForDocument(store.DatabaseCommands.ForSystemDatabase(), Constants.Cluster.ClusterConfigurationDocumentKey, TimeSpan.FromMinutes(1));
  80. var configurationJson = store.DatabaseCommands.ForSystemDatabase().Get(Constants.Cluster.ClusterConfigurationDocumentKey);
  81. var configuration = configurationJson.DataAsJson.JsonDeserialization<ClusterConfiguration>();
  82. Assert.True(configuration.EnableReplication);
  83. });
  84. }
  85. protected override void ModifyStore(DocumentStore documentStore)
  86. {
  87. documentStore.Credentials = null;
  88. }
  89. [Fact]
  90. public async Task CanCreateClusterWithApiKeyAndSendCommandToNonLeader()
  91. {
  92. NodeConnectionInfo leaderNci;
  93. var leader = CreateServerWithOAuth(8079, "Ayende/abc", out leaderNci);
  94. leader.Options.ClusterManager.Value.InitializeTopology(leaderNci, forceCandidateState: true);
  95. Assert.True(leader.Options.ClusterManager.Value.Engine.WaitForLeader());
  96. NodeConnectionInfo secondConnectionInfo;
  97. CreateServerWithOAuth(8078, "Marcin/cba", out secondConnectionInfo);
  98. Assert.True(leader.Options.ClusterManager.Value.Engine.AddToClusterAsync(secondConnectionInfo).Wait(3000));
  99. //If we don't wait here we can cause two topology changes to happen at the same time and fail.
  100. WaitForClusterToBecomeNonStale(2);
  101. NodeConnectionInfo thirdConnectionInfo;
  102. CreateServerWithOAuth(8077, "User3/pass", out thirdConnectionInfo);
  103. Assert.True(leader.Options.ClusterManager.Value.Engine.AddToClusterAsync(thirdConnectionInfo).Wait(3000));
  104. WaitForClusterToBecomeNonStale(3);
  105. Assert.False(servers[1].Options.ClusterManager.Value.IsLeader());
  106. var client = servers[1].Options.ClusterManager.Value.Client;
  107. await client.SendClusterConfigurationAsync(new ClusterConfiguration { EnableReplication = false });
  108. Assert.Equal(3, stores.Count);
  109. stores.ForEach(store =>
  110. {
  111. WaitForDocument(store.DatabaseCommands.ForSystemDatabase(), Constants.Cluster.ClusterConfigurationDocumentKey, TimeSpan.FromMinutes(1));
  112. var configurationJson = store.DatabaseCommands.ForSystemDatabase().Get(Constants.Cluster.ClusterConfigurationDocumentKey);
  113. var configuration = configurationJson.DataAsJson.JsonDeserialization<ClusterConfiguration>();
  114. Assert.False(configuration.EnableReplication);
  115. });
  116. }
  117. [Fact(Skip = "Windows credentials required.")]
  118. public async Task CanCreateClusterWithWindowsAuthAndSendCommandToNonLeader()
  119. {
  120. const string username = "";
  121. const string password = "";
  122. const string domain = "";
  123. Assert.NotNull(password);
  124. NodeConnectionInfo leaderNci;
  125. var leader = CreateServerWithWindowsCredentials(8079, username, password, domain, out leaderNci);
  126. leader.Options.ClusterManager.Value.InitializeTopology(leaderNci, forceCandidateState: true);
  127. Assert.True(leader.Options.ClusterManager.Value.Engine.WaitForLeader());
  128. NodeConnectionInfo secondConnectionInfo;
  129. CreateServerWithWindowsCredentials(8078, username, password, domain, out secondConnectionInfo);
  130. Assert.True(leader.Options.ClusterManager.Value.Engine.AddToClusterAsync(secondConnectionInfo).Wait(3000));
  131. //If we don't wait here we can cause two topology changes to happen at the same time and fail.
  132. WaitForClusterToBecomeNonStale(2);
  133. NodeConnectionInfo thirdConnectionInfo;
  134. CreateServerWithWindowsCredentials(8077, username, password, domain, out thirdConnectionInfo);
  135. Assert.True(leader.Options.ClusterManager.Value.Engine.AddToClusterAsync(thirdConnectionInfo).Wait(3000));
  136. WaitForClusterToBecomeNonStale(3);
  137. Assert.False(servers[1].Options.ClusterManager.Value.IsLeader());
  138. var client = servers[1].Options.ClusterManager.Value.Client;
  139. await client.SendClusterConfigurationAsync(new ClusterConfiguration { EnableReplication = false });
  140. Assert.Equal(3, stores.Count);
  141. stores.ForEach(store =>
  142. {
  143. WaitForDocument(store.DatabaseCommands.ForSystemDatabase(), Constants.Cluster.ClusterConfigurationDocumentKey, TimeSpan.FromMinutes(1));
  144. var configurationJson = store.DatabaseCommands.ForSystemDatabase().Get(Constants.Cluster.ClusterConfigurationDocumentKey);
  145. var configuration = configurationJson.DataAsJson.JsonDeserialization<ClusterConfiguration>();
  146. Assert.False(configuration.EnableReplication);
  147. });
  148. }
  149. }
  150. }