AipFileManager.cs 9.6 KB

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