AipFileManager.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using Microsoft.InformationProtection;
  6. using Microsoft.InformationProtection.File;
  7. using Microsoft.InformationProtection.Policy;
  8. using Microsoft.InformationProtection.Protection;
  9. using LogLevel = Microsoft.InformationProtection.LogLevel;
  10. namespace AipGateway.AIP
  11. {
  12. public class AipFileManager
  13. {
  14. public int LastErrNo { get; internal set; }
  15. public string LastErrMsg { get; internal set; }
  16. private readonly AipConfig _aipConfig;
  17. private readonly ApplicationInfo _appInfo;
  18. private AuthDelegateImplementation _authDelegate;
  19. private MipContext _mipContext;
  20. private readonly LabelManager _labelManager = null;
  21. private readonly PolicyManager _policyManager = null;
  22. private readonly ProtectionManager _protectionManager = null;
  23. public AipFileManager(AipConfig aipConfig)
  24. {
  25. _aipConfig = aipConfig;
  26. _appInfo = new ApplicationInfo
  27. {
  28. ApplicationId = aipConfig.ClientId,
  29. ApplicationName = aipConfig.AppName,
  30. ApplicationVersion = aipConfig.AppVersion
  31. };
  32. LastErrNo = 0;
  33. LastErrMsg = string.Empty;
  34. _labelManager = new LabelManager();
  35. _policyManager = new PolicyManager();
  36. _protectionManager = new ProtectionManager();
  37. }
  38. ~AipFileManager() => this.Dispose(false);
  39. public void Dispose()
  40. {
  41. this.Dispose(true);
  42. GC.SuppressFinalize((object)this);
  43. }
  44. protected virtual void Dispose(bool disposing)
  45. {
  46. lock (this)
  47. {
  48. if (_labelManager != null)
  49. {
  50. _labelManager.Dispose();
  51. }
  52. if (_policyManager != null)
  53. {
  54. _policyManager.Dispose();
  55. }
  56. if (_protectionManager != null)
  57. {
  58. _protectionManager.Dispose();
  59. }
  60. _authDelegate = null;
  61. }
  62. }
  63. private void SetError(int errNo, string errMsg1, string errMsg2 = "No Exception Message.")
  64. {
  65. LastErrNo = errNo;
  66. LastErrMsg = errMsg1 + "\r\n" + errMsg2;
  67. Console.WriteLine("AipFileManager::SetError ==> {0}, {1}, {2}", errNo, errMsg1, errMsg2);
  68. }
  69. public bool Initialize()
  70. {
  71. _authDelegate = new AuthDelegateImplementation(_aipConfig);
  72. try {
  73. // Initialize SDK DLLs. If DLLs are missing or wrong type, this will throw an exception
  74. MIP.Initialize(MipComponent.File);
  75. //MIP.Initialize(MipComponent.Policy);
  76. //MIP.Initialize(MipComponent.Protection); // Protection
  77. }
  78. catch (Exception ex) {
  79. SetError(1, "MIP.Initialize Failed.", ex.Message);
  80. return false;
  81. }
  82. // Create MipConfiguration Object
  83. var mipConfiguration = new MipConfiguration(_appInfo, _aipConfig.MipData, LogLevel.Trace, false);
  84. // Create MipContext using MipConfiguration
  85. _mipContext = MIP.CreateMipContext(mipConfiguration);
  86. return true;
  87. }
  88. public bool CreateProfile()
  89. {
  90. if (_authDelegate == null || _mipContext == null)
  91. {
  92. SetError(999, "AipFileManager::CreateProfile Failed.", "Library Is not Initialized.");
  93. return false;
  94. }
  95. if (!_labelManager.CreateProfile(ref _mipContext))
  96. {
  97. SetError(11, "AipFileManager::CreateProfile ==> LabelManager::CreateProfile Failed.");
  98. return false;
  99. }
  100. if (!_policyManager.CreateProfile(ref _mipContext))
  101. {
  102. SetError(12, "AipFileManager::CreateProfile ==> PolicyManager::CreateProfile Failed.");
  103. return false;
  104. }
  105. if (!_protectionManager.CreateProfile(ref _mipContext))
  106. {
  107. SetError(13, "AipFileManager::CreateProfile ==> ProtectionManager::CreateProfile Failed.");
  108. return false;
  109. }
  110. return true;
  111. }
  112. public bool CreateEngine()
  113. {
  114. if (_authDelegate == null || _mipContext == null)
  115. {
  116. SetError(999, "AipFileManager::CreateEngine Failed.", "Library Is not Initialized.");
  117. return false;
  118. }
  119. Identity identity = new Identity(_aipConfig.EMail);
  120. if (!_labelManager.CreateEngine(ref identity, ref _authDelegate))
  121. {
  122. SetError(21, "AipFileManager::CreateEngine ==> LabelManager::CreateEngine Failed.");
  123. return false;
  124. }
  125. if (!_policyManager.CreateEngine(ref identity, ref _authDelegate))
  126. {
  127. SetError(22, "AipFileManager::CreateEngine ==> PolicyManager::CreateEngine Failed.");
  128. return false;
  129. }
  130. if (!_protectionManager.CreateEngine(ref identity, ref _authDelegate))
  131. {
  132. SetError(23, "AipFileManager::CreateEngine ==> ProtectionManager::CreateEngine Failed.");
  133. return false;
  134. }
  135. return true;
  136. }
  137. public AipFileInfo GetFileInfo(string fileName)
  138. {
  139. if (_authDelegate == null || _mipContext == null)
  140. {
  141. SetError(999, "AipFileManager::GetFileInfo Failed.", "Library Is not Initialized.");
  142. return null;
  143. }
  144. AipFileInfo fileInfo = _labelManager.GetFileInfo(fileName);
  145. if (fileInfo == null)
  146. {
  147. SetError(_labelManager.LastErrNo, "AipFileManager::GetFileInfo Failed.", _labelManager.LastErrMsg);
  148. }
  149. return fileInfo;
  150. }
  151. public List<AipLabel> SensitivityLabels()
  152. {
  153. if (_authDelegate == null || _mipContext == null)
  154. {
  155. SetError(999, "AipFileManager::SensitivityLabels Failed.", "Library Is not Initialized.");
  156. return null;
  157. }
  158. var result = new List<AipLabel>();
  159. try
  160. {
  161. var labels = _labelManager.SensitivityLabels();
  162. foreach (var label in labels)
  163. {
  164. var aipLabel = Utilities.LabelToAip(label);
  165. if (aipLabel != null)
  166. {
  167. if (label.Children.Count > 0)
  168. {
  169. foreach (var child in label.Children)
  170. {
  171. var aipChildLabel = Utilities.LabelToAip(child);
  172. if (aipChildLabel != null) aipLabel.Children.Add(aipChildLabel);
  173. }
  174. }
  175. result.Add(aipLabel);
  176. }
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. SetError(31, "AipFileManager::SensitivityLabels Failed.", ex.Message);
  182. result = new List<AipLabel>();
  183. }
  184. return result;
  185. }
  186. public List<AipLabel> ListSensitivityLabels()
  187. {
  188. if (_authDelegate == null || _mipContext == null)
  189. {
  190. SetError(999, "AipFileManager::ListSensitivityLabels Failed.", "Library Is not Initialized.");
  191. return null;
  192. }
  193. var result = new List<AipLabel>();
  194. try
  195. {
  196. var labels = _policyManager.ListSensitivityLabels();
  197. foreach (var label in labels)
  198. {
  199. var aipLabel = Utilities.LabelToAip(label);
  200. if (aipLabel != null)
  201. {
  202. if (label.Children.Count > 0)
  203. {
  204. foreach (var child in label.Children)
  205. {
  206. var aipChildLabel = Utilities.LabelToAip(child);
  207. if (aipChildLabel != null) aipLabel.Children.Add(aipChildLabel);
  208. }
  209. }
  210. result.Add(aipLabel);
  211. }
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. SetError(32, "AipFileManager::ListSensitivityLabels Failed.", ex.Message);
  217. result = new List<AipLabel>();
  218. }
  219. return result;
  220. }
  221. public List<AipTemplate> GetTemplates()
  222. {
  223. if (_authDelegate == null || _mipContext == null)
  224. {
  225. SetError(999, "AipFileManager::GetTemplates Failed.", "Library Is not Initialized.");
  226. return null;
  227. }
  228. var result = new List<AipTemplate>();
  229. try
  230. {
  231. var templates = _protectionManager.GetTemplates();
  232. foreach (var template in templates)
  233. {
  234. var aipTemplate = Utilities.TemplateToAip(template);
  235. if (aipTemplate != null)
  236. {
  237. result.Add(aipTemplate);
  238. }
  239. }
  240. }
  241. catch (Exception ex)
  242. {
  243. SetError(33, "AipFileManager::GetTemplates Failed.", ex.Message);
  244. result = new List<AipTemplate>();
  245. }
  246. return result;
  247. }
  248. }
  249. }