/ToMigrate/Raven.Tests/Security/ReplicationWithMixedSecurity.cs

https://github.com/fitzchak/ravendb · C# · 228 lines · 182 code · 41 blank · 5 comment · 4 complexity · 08c5d89a425bc1ed1ca006670ce95f61 MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="ReplicationWithMixedSecurity.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using Raven.Abstractions.Replication;
  7. using Raven.Tests.Common;
  8. using Raven.Tests.Common.Attributes;
  9. using Raven.Tests.Common.Dto;
  10. using System.Collections.Generic;
  11. using System.Net;
  12. using System.Threading.Tasks;
  13. using Raven.Abstractions.Data;
  14. using Raven.Client.Connection;
  15. using Raven.Client.Document;
  16. using Raven.Database.Server;
  17. using Raven.Database.Server.Security;
  18. using Raven.Database.Server.Security.Windows;
  19. using Raven.Json.Linq;
  20. using Raven.Tests.Helpers.Util;
  21. using Xunit;
  22. namespace Raven.Tests.Security
  23. {
  24. public class ReplicationWithMixedSecurity : ReplicationBase
  25. {
  26. public ReplicationWithMixedSecurity()
  27. {
  28. FactIfWindowsAuthenticationIsAvailable.LoadCredentials();
  29. }
  30. private string apiKey = "test1/ThisIsMySecret";
  31. private int _storeCounter, _databaseCounter;
  32. protected override void ModifyStore(DocumentStore store)
  33. {
  34. FactIfWindowsAuthenticationIsAvailable.LoadCredentials();
  35. var isApiStore = _storeCounter % 2 == 0;
  36. store.Conventions.FailoverBehavior = FailoverBehavior.AllowReadsFromSecondaries;
  37. if (isApiStore)
  38. {
  39. store.Credentials = null;
  40. store.ApiKey = apiKey;
  41. }
  42. else
  43. {
  44. store.Credentials = new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain);
  45. store.ApiKey = null;
  46. }
  47. ConfigurationHelper.ApplySettingsToConventions(store.Conventions);
  48. _storeCounter++;
  49. }
  50. protected override void ConfigureDatabase(Database.DocumentDatabase database, string databaseName = null)
  51. {
  52. var isApiDatabase = _databaseCounter % 2 == 0;
  53. if (isApiDatabase)
  54. {
  55. database.Documents.Put(
  56. "Raven/ApiKeys/" + apiKey.Split('/')[0],
  57. null,
  58. RavenJObject.FromObject(
  59. new ApiKeyDefinition
  60. {
  61. Name = apiKey.Split('/')[0],
  62. Secret = apiKey.Split('/')[1],
  63. Enabled = true,
  64. Databases =
  65. new List<ResourceAccess>
  66. {
  67. new ResourceAccess { TenantId = "*" },
  68. new ResourceAccess { TenantId = Constants.SystemDatabase },
  69. new ResourceAccess {TenantId = databaseName}
  70. }
  71. }),
  72. new RavenJObject(),
  73. null);
  74. }
  75. else
  76. {
  77. database.Documents.Put("Raven/Authorization/WindowsSettings", null,
  78. RavenJObject.FromObject(new WindowsAuthDocument
  79. {
  80. RequiredUsers = new List<WindowsAuthData>
  81. {
  82. new WindowsAuthData()
  83. {
  84. Name = FactIfWindowsAuthenticationIsAvailable.Admin.UserName,
  85. Enabled = true,
  86. Databases = new List<ResourceAccess>
  87. {
  88. new ResourceAccess {TenantId = "*"},
  89. new ResourceAccess {TenantId = Constants.SystemDatabase},
  90. new ResourceAccess {TenantId = databaseName}
  91. }
  92. }
  93. }
  94. }), new RavenJObject(), null);
  95. }
  96. _databaseCounter++;
  97. }
  98. [Fact]
  99. public void DocumentStoreShouldSwitchFromApiKeyToCredentials()
  100. {
  101. var store1 = CreateStore(enableAuthorization: true);
  102. Authentication.EnableOnce();
  103. var store2 = CreateStore(enableAuthorization: true, anonymousUserAccessMode: AnonymousUserAccessMode.None);
  104. TellFirstInstanceToReplicateToSecondInstance(username: FactIfWindowsAuthenticationIsAvailable.Admin.UserName, password: FactIfWindowsAuthenticationIsAvailable.Admin.Password, domain: FactIfWindowsAuthenticationIsAvailable.Admin.Domain, authenticationScheme: store2.Conventions.AuthenticationScheme);
  105. using (var session = store1.OpenSession())
  106. {
  107. session.Store(new Company { Name = "Hibernating Rhinos" });
  108. session.SaveChanges();
  109. }
  110. var company = WaitForDocument<Company>(store2, "companies/1");
  111. Assert.Equal("Hibernating Rhinos", company.Name);
  112. var serverClient = ((ServerClient)store1.DatabaseCommands);
  113. GetReplicationInformer(serverClient).RefreshReplicationInformation(serverClient);
  114. servers[0].Dispose();
  115. using (var session = store1.OpenSession())
  116. {
  117. Assert.NotNull(session.Load<Company>(1));
  118. }
  119. }
  120. [Fact]
  121. public async Task DocumentStoreShouldSwitchFromApiKeyToCredentialsAsync()
  122. {
  123. var store1 = CreateStore(enableAuthorization: true);
  124. Authentication.EnableOnce();
  125. var store2 = CreateStore(enableAuthorization: true, anonymousUserAccessMode: AnonymousUserAccessMode.None);
  126. TellFirstInstanceToReplicateToSecondInstance(username: FactIfWindowsAuthenticationIsAvailable.Admin.UserName, password: FactIfWindowsAuthenticationIsAvailable.Admin.Password, domain: FactIfWindowsAuthenticationIsAvailable.Admin.Domain, authenticationScheme: store2.Conventions.AuthenticationScheme);
  127. using (var session = store1.OpenAsyncSession())
  128. {
  129. await session.StoreAsync(new Company { Name = "Hibernating Rhinos" });
  130. await session.SaveChangesAsync();
  131. }
  132. var company = WaitForDocument<Company>(store2, "companies/1");
  133. Assert.Equal("Hibernating Rhinos", company.Name);
  134. var serverClient = ((ServerClient)store1.DatabaseCommands);
  135. GetReplicationInformer(serverClient).RefreshReplicationInformation(serverClient);
  136. servers[0].Dispose();
  137. using (var session = store1.OpenAsyncSession())
  138. {
  139. Assert.NotNull(await session.LoadAsync<Company>(1));
  140. }
  141. }
  142. [Fact]
  143. public void DocumentStoreShouldSwitchFromCredentialsToApiKey()
  144. {
  145. var store1 = CreateStore(enableAuthorization: true);
  146. Authentication.EnableOnce();
  147. var store2 = CreateStore(enableAuthorization: true, anonymousUserAccessMode: AnonymousUserAccessMode.None);
  148. TellSecondInstanceToReplicateToFirstInstance(apiKey);
  149. using (var session = store2.OpenSession())
  150. {
  151. session.Store(new Company { Name = "Hibernating Rhinos" });
  152. session.SaveChanges();
  153. }
  154. var company = WaitForDocument<Company>(store1, "companies/1");
  155. Assert.Equal("Hibernating Rhinos", company.Name);
  156. var serverClient = ((ServerClient)store2.DatabaseCommands);
  157. GetReplicationInformer(serverClient).RefreshReplicationInformation(serverClient);
  158. servers[1].Dispose();
  159. using (var session = store2.OpenSession())
  160. {
  161. Assert.NotNull(session.Load<Company>(1));
  162. }
  163. }
  164. [Fact]
  165. public async Task DocumentStoreShouldSwitchFromCredentialsToApiKeyAsync()
  166. {
  167. var store1 = CreateStore(enableAuthorization: true);
  168. Authentication.EnableOnce();
  169. var store2 = CreateStore(enableAuthorization: true, anonymousUserAccessMode: AnonymousUserAccessMode.None);
  170. TellSecondInstanceToReplicateToFirstInstance(apiKey);
  171. using (var session = store2.OpenAsyncSession())
  172. {
  173. await session.StoreAsync(new Company { Name = "Hibernating Rhinos" });
  174. await session.SaveChangesAsync();
  175. }
  176. var company = WaitForDocument<Company>(store1, "companies/1");
  177. Assert.Equal("Hibernating Rhinos", company.Name);
  178. var serverClient = ((ServerClient)store2.DatabaseCommands);
  179. GetReplicationInformer(serverClient).RefreshReplicationInformation(serverClient);
  180. servers[1].Dispose();
  181. using (var session = store2.OpenAsyncSession())
  182. {
  183. Assert.NotNull(await session.LoadAsync<Company>(1));
  184. }
  185. }
  186. }
  187. }