AipFileManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Microsoft.InformationProtection;
  5. using Microsoft.InformationProtection.File;
  6. using LogLevel = Microsoft.InformationProtection.LogLevel;
  7. using Serilog;
  8. using Serilog.Core;
  9. namespace AipGateway.AIP
  10. {
  11. public class AipFileManager
  12. {
  13. //private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  14. private readonly Logger _logger;
  15. private readonly ILogger _log;
  16. public int LastErrNo { get; internal set; }
  17. public string LastErrMsg { get; internal set; }
  18. private readonly AipConfig _aipConfig;
  19. private readonly ApplicationInfo _appInfo;
  20. private AuthDelegateImplementation _authDelegate;
  21. private MipContext _mipContext;
  22. private readonly FileManager _fileManager = null;
  23. private readonly PolicyManager _policyManager = null;
  24. private readonly ProtectionManager _protectionManager = null;
  25. public AipFileManager(Logger logger, AipConfig aipConfig)
  26. {
  27. _logger = logger;
  28. _log = logger.ForContext<AipFileManager>();
  29. _aipConfig = aipConfig;
  30. _appInfo = new ApplicationInfo
  31. {
  32. ApplicationId = aipConfig.ClientId,
  33. ApplicationName = aipConfig.AppName,
  34. ApplicationVersion = aipConfig.AppVersion
  35. };
  36. LastErrNo = 0;
  37. LastErrMsg = string.Empty;
  38. _fileManager = new FileManager(logger, aipConfig.ClientId);
  39. _policyManager = new PolicyManager(logger, aipConfig.ClientId);
  40. _protectionManager = new ProtectionManager(logger, aipConfig.ClientId);
  41. }
  42. ~AipFileManager() => this.Dispose(false);
  43. public void Dispose()
  44. {
  45. this.Dispose(true);
  46. GC.SuppressFinalize((object)this);
  47. }
  48. private void Dispose(bool disposing)
  49. {
  50. lock (this)
  51. {
  52. if (_fileManager != null)
  53. {
  54. _fileManager.Dispose();
  55. }
  56. if (_policyManager != null)
  57. {
  58. _policyManager.Dispose();
  59. }
  60. if (_protectionManager != null)
  61. {
  62. _protectionManager.Dispose();
  63. }
  64. _authDelegate = null;
  65. }
  66. }
  67. private void SetError(int errNo, string errMsg1, string errMsg2 = "", bool isThrowEx = true)
  68. {
  69. LastErrNo = errNo;
  70. if (errMsg2 == "")
  71. {
  72. LastErrMsg = errMsg1;
  73. }
  74. else
  75. {
  76. LastErrMsg = errMsg1 + "\r\n" + errMsg2;
  77. }
  78. _log.Error("AipFileManager::SetError ==> {0}, {1}, {2}", errNo, errMsg1, errMsg2);
  79. if (isThrowEx && LastErrNo != 0)
  80. {
  81. throw new AipFileException(errNo, LastErrMsg);
  82. }
  83. }
  84. public bool Initialize()
  85. {
  86. _authDelegate = new AuthDelegateImplementation(_logger, _aipConfig);
  87. try
  88. {
  89. // Initialize SDK DLLs. If DLLs are missing or wrong type, this will throw an exception
  90. MIP.Initialize(MipComponent.File);
  91. //MIP.Initialize(MipComponent.Policy);
  92. //MIP.Initialize(MipComponent.Protection); // Protection
  93. }
  94. catch (Exception ex) {
  95. SetError(1, "MIP.Initialize Failed.", ex.Message, false);
  96. return false;
  97. }
  98. // Create MipConfiguration Object
  99. var mipConfiguration = new MipConfiguration(_appInfo, _aipConfig.MipData, LogLevel.Warning, false);
  100. // mipConfiguration.LoggerDelegateOverride = this;
  101. //Enable DKE
  102. // Dictionary<FlightingFeature, bool> featureSettings = mipConfiguration.FeatureSettingsOverride ?? new Dictionary<FlightingFeature, bool>();
  103. // featureSettings[FlightingFeature.DoubleKey] = true;
  104. // mipConfiguration.FeatureSettingsOverride = featureSettings;
  105. // Create MipContext using MipConfiguration
  106. _mipContext = MIP.CreateMipContext(mipConfiguration);
  107. return true;
  108. }
  109. public bool CreateProfile()
  110. {
  111. if (_authDelegate == null || _mipContext == null)
  112. {
  113. SetError(999, "AipFileManager::CreateProfile Failed.", "Library Is not Initialized.", false);
  114. return false;
  115. }
  116. if (!_fileManager.CreateProfile(ref _mipContext))
  117. {
  118. SetError(11, "AipFileManager::CreateProfile Failed.", "FileManager::CreateProfile Failed.", false);
  119. return false;
  120. }
  121. if (!_policyManager.CreateProfile(ref _mipContext))
  122. {
  123. SetError(12, "AipFileManager::CreateProfile Failed.", "PolicyManager::CreateProfile Failed.", false);
  124. return false;
  125. }
  126. if (!_protectionManager.CreateProfile(ref _mipContext))
  127. {
  128. SetError(13, "AipFileManager::CreateProfile Failed.", "ProtectionManager::CreateProfile Failed.", false);
  129. return false;
  130. }
  131. return true;
  132. }
  133. public bool CreateEngine()
  134. {
  135. if (_authDelegate == null || _mipContext == null)
  136. {
  137. SetError(999, "AipFileManager::CreateEngine Failed.", "Library Is not Initialized.", false);
  138. return false;
  139. }
  140. Identity identity = new Identity(_aipConfig.EMail);
  141. if (!_fileManager.CreateEngine(ref identity, ref _authDelegate))
  142. {
  143. SetError(21, "AipFileManager::CreateEngine ", "FileManager::CreateEngine Failed.", false);
  144. return false;
  145. }
  146. if (!_policyManager.CreateEngine(ref identity, ref _authDelegate))
  147. {
  148. SetError(22, "AipFileManager::CreateEngine ", "PolicyManager::CreateEngine Failed.", false);
  149. return false;
  150. }
  151. if (!_protectionManager.CreateEngine(ref identity, ref _authDelegate))
  152. {
  153. SetError(23, "AipFileManager::CreateEngine ", "ProtectionManager::CreateEngine Failed.", false);
  154. return false;
  155. }
  156. return true;
  157. }
  158. public bool IsProtected(Stream inputStream, string filePath)
  159. {
  160. IFileStatus status = FileHandler.GetFileStatus(inputStream, filePath, _mipContext);
  161. bool result = status.IsProtected();
  162. return result;
  163. }
  164. public bool IsLabeledOrProtected(Stream inputStream, string filePath)
  165. {
  166. IFileStatus status = FileHandler.GetFileStatus(inputStream, filePath, _mipContext);
  167. bool isLabeled = status.IsLabeled();
  168. bool isProtected = status.IsProtected();
  169. return (isLabeled || isProtected);
  170. }
  171. public AipFileInfo GetFileInfo(string fileName)
  172. {
  173. if (_authDelegate == null || _mipContext == null)
  174. {
  175. SetError(10, "AipFileManager::GetFileInfo Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
  176. return new AipFileInfo
  177. {
  178. errorCode = 10,
  179. errorMessage = "AIP 라이브러리가 초기화 되지 않았습니다.",
  180. };
  181. }
  182. AipFileInfo fileInfo = _fileManager.GetFileInfo(fileName);
  183. if (fileInfo == null)
  184. {
  185. SetError(_fileManager.LastErrNo, "AipFileManager::GetFileInfo Failed.", _fileManager.LastErrMsg);
  186. return new AipFileInfo
  187. {
  188. errorCode = _fileManager.LastErrNo == 0 ? 11 : _fileManager.LastErrNo,
  189. errorMessage = _fileManager.LastErrMsg,
  190. };
  191. }
  192. return fileInfo;
  193. }
  194. public AipFileInfo GetFileInfo(Stream fileStream, string outputFileName)
  195. {
  196. if (_authDelegate == null || _mipContext == null)
  197. {
  198. SetError(10, "AipFileManager::GetFileInfo Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
  199. return new AipFileInfo
  200. {
  201. errorCode = 10,
  202. errorMessage = "AIP 라이브러리가 초기화 되지 않았습니다.",
  203. };
  204. }
  205. AipFileInfo fileInfo = _fileManager.GetFileInfo(fileStream, outputFileName);
  206. if (fileInfo == null)
  207. {
  208. SetError(_fileManager.LastErrNo, "AipFileManager::GetFileInfo Failed.", _fileManager.LastErrMsg);
  209. return new AipFileInfo
  210. {
  211. errorCode = _fileManager.LastErrNo == 0 ? 12 : _fileManager.LastErrNo,
  212. errorMessage = _fileManager.LastErrMsg,
  213. };
  214. }
  215. return fileInfo;
  216. }
  217. public List<AipLabel> SensitivityLabels()
  218. {
  219. if (_authDelegate == null || _mipContext == null)
  220. {
  221. SetError(10, "AipFileManager::SensitivityLabels Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
  222. return null;
  223. }
  224. var result = new List<AipLabel>();
  225. try
  226. {
  227. var labels = _fileManager.SensitivityLabels();
  228. foreach (var label in labels)
  229. {
  230. var aipLabel = Utilities.LabelToAip(label);
  231. if (aipLabel != null)
  232. {
  233. if (label.Children.Count > 0)
  234. {
  235. foreach (var child in label.Children)
  236. {
  237. var aipChildLabel = Utilities.LabelToAip(child);
  238. if (aipChildLabel != null) aipLabel.Children.Add(aipChildLabel);
  239. }
  240. }
  241. result.Add(aipLabel);
  242. }
  243. }
  244. }
  245. catch (Exception ex)
  246. {
  247. SetError(31, "AipFileManager::SensitivityLabels Failed.", ex.Message);
  248. result = new List<AipLabel>();
  249. }
  250. return result;
  251. }
  252. public List<AipLabel> ListSensitivityLabels()
  253. {
  254. if (_authDelegate == null || _mipContext == null)
  255. {
  256. SetError(10, "AipFileManager::ListSensitivityLabels Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
  257. return null;
  258. }
  259. var result = new List<AipLabel>();
  260. try
  261. {
  262. var labels = _policyManager.ListSensitivityLabels();
  263. foreach (var label in labels)
  264. {
  265. var aipLabel = Utilities.LabelToAip(label);
  266. if (aipLabel != null)
  267. {
  268. if (label.Children.Count > 0)
  269. {
  270. foreach (var child in label.Children)
  271. {
  272. var aipChildLabel = Utilities.LabelToAip(child);
  273. if (aipChildLabel != null) aipLabel.Children.Add(aipChildLabel);
  274. }
  275. }
  276. result.Add(aipLabel);
  277. }
  278. }
  279. }
  280. catch (Exception ex)
  281. {
  282. SetError(32, "AipFileManager::ListSensitivityLabels Failed.", ex.Message);
  283. result = new List<AipLabel>();
  284. }
  285. return result;
  286. }
  287. public List<AipTemplate> GetTemplates()
  288. {
  289. if (_authDelegate == null || _mipContext == null)
  290. {
  291. SetError(10, "AipFileManager::GetTemplates Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
  292. return null;
  293. }
  294. var result = new List<AipTemplate>();
  295. try
  296. {
  297. var templates = _protectionManager.GetTemplates();
  298. foreach (var template in templates)
  299. {
  300. var aipTemplate = Utilities.TemplateToAip(template);
  301. if (aipTemplate != null)
  302. {
  303. result.Add(aipTemplate);
  304. }
  305. }
  306. }
  307. catch (Exception ex)
  308. {
  309. SetError(33, "AipFileManager::GetTemplates Failed.", ex.Message);
  310. result = new List<AipTemplate>();
  311. }
  312. return result;
  313. }
  314. public SetFileInfo SetLabel(string fileName, string actualFileName, string email, string labelId, string templateId, string comments = "")
  315. {
  316. // 레이블 및 템플릿 정보 가져오기
  317. if (comments == "")
  318. {
  319. comments = "SetLabel";
  320. }
  321. return _fileManager.SetLabel(fileName, actualFileName, email, labelId, templateId, comments);
  322. }
  323. public SetFileInfo SetLabel(Stream fileStream, string actualFileName, string email, string labelId, string templateId, string comments = "")
  324. {
  325. // 레이블 및 템플릿 정보 가져오기
  326. if (comments == "")
  327. {
  328. comments = "SetLabel by " + email;
  329. }
  330. return _fileManager.SetLabel(fileStream, actualFileName, email, labelId, templateId, comments);
  331. }
  332. public SetFileInfo DeleteLabel(string fileName, string actualFileName, string email, string comments = "", bool isDelProtection = false)
  333. {
  334. if (comments == "")
  335. {
  336. comments = "Delete Label by " + email;
  337. }
  338. return _fileManager.DeleteLabel(fileName, actualFileName, email, comments, isDelProtection);
  339. }
  340. public SetFileInfo DeleteLabel(Stream fileStream, string actualFileName, string email, string comments = "", bool isDelProtection = false)
  341. {
  342. if (comments == "")
  343. {
  344. comments = "Delete Label by " + email;
  345. }
  346. return _fileManager.DeleteLabel(fileStream, actualFileName, email, comments, isDelProtection);
  347. }
  348. public SetFileInfo SetProtection(string fileName, string actualFileName, string email, string templateId, string comments = "")
  349. {
  350. if (comments == "")
  351. {
  352. comments = "SetProtection by " + email;
  353. }
  354. return _fileManager.SetProtection(fileName, actualFileName, email, templateId, comments);
  355. }
  356. public SetFileInfo SetProtection(Stream fileStream, string actualFileName, string email, string templateId, string comments = "")
  357. {
  358. if (comments == "")
  359. {
  360. comments = "SetProtection by " + email;
  361. }
  362. return _fileManager.SetProtection(fileStream, actualFileName, email, templateId, comments);
  363. }
  364. public SetFileInfo DeleteProtection(string fileName, string actualFileName, string email, string comments = "")
  365. {
  366. if (comments == "")
  367. {
  368. comments = "Delete Protection by " + email;
  369. }
  370. return _fileManager.RemoveProtection(fileName, actualFileName, email, comments);
  371. }
  372. public SetFileInfo DeleteProtection(Stream fileStream, string actualFileName, string email, string comments = "")
  373. {
  374. if (comments == "")
  375. {
  376. comments = "Delete Protection by " + email;
  377. }
  378. return _fileManager.RemoveProtection(fileStream, actualFileName, email, comments);
  379. }
  380. public AipFileStatus GetAipFileStatus(string fileName)
  381. {
  382. try
  383. {
  384. var fileStatus = FileHandler.GetFileStatus(fileName, _mipContext);
  385. AipFileStatus result = new AipFileStatus
  386. {
  387. IsProtected = fileStatus.IsProtected(),
  388. IsLabeled = fileStatus.IsLabeled(),
  389. ContainsProtectedObjects = fileStatus.ContainsProtectedObjects()
  390. };
  391. return result;
  392. }
  393. catch (Exception ex)
  394. {
  395. SetError(81, "AipFileManager::GetAipFileStatus Failed.", ex.Message);
  396. }
  397. return null;
  398. }
  399. }
  400. }