/src/Raven.Server/Commercial/SetupInfo.cs

https://github.com/fitzchak/ravendb · C# · 346 lines · 303 code · 43 blank · 0 comment · 3 complexity · 35be73426a5cd5deae5319e88019ac2f MD5 · raw file

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.Cryptography.X509Certificates;
  6. using Raven.Client.Documents.Operations;
  7. using Raven.Client.Util;
  8. using Sparrow.Json.Parsing;
  9. using Sparrow.Logging;
  10. namespace Raven.Server.Commercial
  11. {
  12. public class SetupInfo
  13. {
  14. public bool RegisterClientCert { get; set; }
  15. public License License { get; set; }
  16. public string Email { get; set; }
  17. public string Domain { get; set; }
  18. public string RootDomain { get; set; }
  19. public bool ModifyLocalServer { get; set; }
  20. public string Certificate { get; set; }
  21. public string Password { get; set; }
  22. public Dictionary<string, NodeInfo> NodeSetupInfos { get; set; }
  23. public DynamicJsonValue ToJson()
  24. {
  25. return new DynamicJsonValue
  26. {
  27. [nameof(License)] = License.ToJson(),
  28. [nameof(Email)] = Email,
  29. [nameof(Domain)] = Domain,
  30. [nameof(RootDomain)] = RootDomain,
  31. [nameof(ModifyLocalServer)] = ModifyLocalServer,
  32. [nameof(RegisterClientCert)] = RegisterClientCert,
  33. [nameof(Certificate)] = Certificate,
  34. [nameof(Password)] = Password,
  35. [nameof(NodeSetupInfos)] = DynamicJsonValue.Convert(NodeSetupInfos)
  36. };
  37. }
  38. public class NodeInfo
  39. {
  40. public string PublicServerUrl { get; set; }
  41. public string PublicTcpServerUrl { get; set; }
  42. public int Port { get; set; }
  43. public int TcpPort { get; set; }
  44. public string ExternalIpAddress { get; set; }
  45. public int ExternalPort { get; set; }
  46. public int ExternalTcpPort { get; set; }
  47. public List<string> Addresses { get; set; }
  48. public DynamicJsonValue ToJson()
  49. {
  50. return new DynamicJsonValue
  51. {
  52. [nameof(PublicServerUrl)] = PublicServerUrl,
  53. [nameof(PublicTcpServerUrl)] = PublicTcpServerUrl,
  54. [nameof(Port)] = Port,
  55. [nameof(TcpPort)] = TcpPort,
  56. [nameof(ExternalIpAddress)] = ExternalIpAddress,
  57. [nameof(ExternalPort)] = ExternalPort,
  58. [nameof(ExternalTcpPort)] = ExternalTcpPort,
  59. [nameof(Addresses)] = new DynamicJsonArray(Addresses)
  60. };
  61. }
  62. }
  63. public X509Certificate2 GetX509Certificate()
  64. {
  65. try
  66. {
  67. var localCertBytes = Convert.FromBase64String(Certificate);
  68. return string.IsNullOrEmpty(Password)
  69. ? new X509Certificate2(localCertBytes)
  70. : new X509Certificate2(localCertBytes, Password);
  71. }
  72. catch (Exception e)
  73. {
  74. throw new InvalidOperationException("Could not load the provided certificate.", e);
  75. }
  76. }
  77. }
  78. public class UnsecuredSetupInfo
  79. {
  80. public List<string> Addresses { get; set; }
  81. public int Port { get; set; }
  82. public int TcpPort { get; set; }
  83. public DynamicJsonValue ToJson()
  84. {
  85. return new DynamicJsonValue
  86. {
  87. [nameof(Addresses)] = new DynamicJsonArray(Addresses),
  88. [nameof(Port)] = Port,
  89. [nameof(TcpPort)] = TcpPort
  90. };
  91. }
  92. }
  93. public class ContinueSetupInfo
  94. {
  95. public string NodeTag { get; set; }
  96. public bool RegisterClientCert { get; set; }
  97. public string Zip { get; set; }
  98. public DynamicJsonValue ToJson()
  99. {
  100. return new DynamicJsonValue
  101. {
  102. [nameof(NodeTag)] = NodeTag,
  103. [nameof(RegisterClientCert)] = RegisterClientCert,
  104. [nameof(Zip)] = Zip
  105. };
  106. }
  107. }
  108. public class ListDomainsInfo
  109. {
  110. public License License { get; set; }
  111. public DynamicJsonValue ToJson()
  112. {
  113. return new DynamicJsonValue
  114. {
  115. [nameof(License)] = License.ToJson(),
  116. };
  117. }
  118. }
  119. public class ClaimDomainInfo
  120. {
  121. public License License { get; set; }
  122. public string Domain { get; set; }
  123. public DynamicJsonValue ToJson()
  124. {
  125. return new DynamicJsonValue
  126. {
  127. [nameof(License)] = License.ToJson(),
  128. [nameof(Domain)] = Domain
  129. };
  130. }
  131. }
  132. public class RegistrationInfo
  133. {
  134. public License License { get; set; }
  135. public string Domain { get; set; }
  136. public string RootDomain { get; set; }
  137. public List<RegistrationNodeInfo> SubDomains { get; set; }
  138. public DynamicJsonValue ToJson()
  139. {
  140. return new DynamicJsonValue
  141. {
  142. [nameof(License)] = License.ToJson(),
  143. [nameof(Domain)] = Domain,
  144. [nameof(RootDomain)] = RootDomain,
  145. [nameof(SubDomains)] = SubDomains.Select(o => o.ToJson()).ToArray()
  146. };
  147. }
  148. }
  149. public class RegistrationNodeInfo
  150. {
  151. public List<string> Ips { get; set; }
  152. public string SubDomain { get; set; }
  153. public string Challenge { get; set; }
  154. public DynamicJsonValue ToJson()
  155. {
  156. return new DynamicJsonValue
  157. {
  158. [nameof(Ips)] = new DynamicJsonArray(Ips),
  159. [nameof(SubDomain)] = SubDomain,
  160. [nameof(Challenge)] = Challenge
  161. };
  162. }
  163. }
  164. public class SubDomainAndIps
  165. {
  166. public string SubDomain { get; set; }
  167. public List<string> Ips { get; set; }
  168. public SubDomainAndIps()
  169. {
  170. Ips = new List<string>();
  171. }
  172. public DynamicJsonValue ToJson()
  173. {
  174. return new DynamicJsonValue
  175. {
  176. [nameof(SubDomain)] = SubDomain,
  177. [nameof(Ips)] = new DynamicJsonArray(Ips)
  178. };
  179. }
  180. }
  181. public class UserDomainsAndLicenseInfo
  182. {
  183. public UserDomainsWithIps UserDomainsWithIps { get; set; }
  184. public int MaxClusterSize { get; set; }
  185. public LicenseType LicenseType { get; set; }
  186. public DynamicJsonValue ToJson()
  187. {
  188. return new DynamicJsonValue
  189. {
  190. [nameof(UserDomainsWithIps)] = UserDomainsWithIps.ToJson(),
  191. [nameof(MaxClusterSize)] = MaxClusterSize,
  192. [nameof(LicenseType)] = LicenseType
  193. };
  194. }
  195. }
  196. public class UserDomainsWithIps
  197. {
  198. public string[] Emails { get; set; }
  199. public string[] RootDomains { get; set; }
  200. public Dictionary<string, List<SubDomainAndIps>> Domains { get; set; }
  201. public DynamicJsonValue ToJson()
  202. {
  203. return new DynamicJsonValue
  204. {
  205. [nameof(Emails)] = Emails,
  206. [nameof(RootDomains)] = RootDomains,
  207. [nameof(Domains)] = DynamicJsonValue.Convert(Domains)
  208. };
  209. }
  210. }
  211. public class UserDomainsResult
  212. {
  213. public string[] Emails { get; set; }
  214. public string[] RootDomains { get; set; }
  215. public Dictionary<string, List<string>> Domains { get; set; }
  216. public DynamicJsonValue ToJson()
  217. {
  218. return new DynamicJsonValue
  219. {
  220. [nameof(Emails)] = Emails,
  221. [nameof(RootDomains)] = RootDomains,
  222. [nameof(Domains)] = DynamicJsonValue.Convert(Domains)
  223. };
  224. }
  225. }
  226. public class RegistrationResult
  227. {
  228. public string Status { get; set; }
  229. public DynamicJsonValue ToJson()
  230. {
  231. return new DynamicJsonValue
  232. {
  233. [nameof(Status)] = Status
  234. };
  235. }
  236. }
  237. public enum SetupMode
  238. {
  239. None,
  240. Initial,
  241. LetsEncrypt,
  242. Secured,
  243. Unsecured
  244. }
  245. public enum SetupStage
  246. {
  247. Initial = 0,
  248. Agreement,
  249. Setup,
  250. Validation,
  251. GenarateCertificate,
  252. Finish
  253. }
  254. public class SetupProgressAndResult : IOperationResult, IOperationProgress
  255. {
  256. public long Processed { get; set; }
  257. public long Total { get; set; }
  258. public string Certificate { get; set; }
  259. public string Readme { get; set; }
  260. public readonly ConcurrentQueue<string> Messages;
  261. public byte[] SettingsZipFile; // not sent as part of the result
  262. private static readonly Logger Logger = LoggingSource.Instance.GetLogger<LicenseManager>("Server");
  263. public SetupProgressAndResult()
  264. {
  265. Messages = new ConcurrentQueue<string>();
  266. Certificate = null;
  267. }
  268. public string Message { get; private set; }
  269. public DynamicJsonValue ToJson()
  270. {
  271. var json = new DynamicJsonValue(GetType())
  272. {
  273. [nameof(Processed)] = Processed,
  274. [nameof(Total)] = Total,
  275. [nameof(Readme)] = Readme,
  276. [nameof(Messages)] = Messages.ToArray()
  277. };
  278. if (Certificate != null)
  279. json[nameof(Certificate)] = Certificate;
  280. return json;
  281. }
  282. public void AddWarning(string message)
  283. {
  284. AddMessage("WARNING", message);
  285. }
  286. public void AddInfo(string message)
  287. {
  288. AddMessage("INFO", message);
  289. }
  290. public void AddError(string message, Exception ex = null)
  291. {
  292. AddMessage("ERROR", message, ex);
  293. }
  294. private void AddMessage(string type, string message, Exception ex = null) //<-- remember last message here
  295. {
  296. Message = $"[{SystemTime.UtcNow:T} {type}] {message}";
  297. Messages.Enqueue(Message);
  298. if (Logger.IsInfoEnabled)
  299. Logger.Info(Message, ex);
  300. }
  301. public bool ShouldPersist => false;
  302. }
  303. }