LabelManager.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Policy;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Microsoft.InformationProtection;
  9. using Microsoft.InformationProtection.File;
  10. namespace AipGateway.AIP
  11. {
  12. public class LabelManager : AbstractManager
  13. {
  14. private IFileProfile _profile = null;
  15. private IFileEngine _engine = null;
  16. public LabelManager()
  17. {
  18. }
  19. ~LabelManager() => this.Dispose(false);
  20. public override void Dispose()
  21. {
  22. this.Dispose(true);
  23. GC.SuppressFinalize((object)this);
  24. }
  25. protected virtual void Dispose(bool disposing)
  26. {
  27. lock (this)
  28. {
  29. if (_profile != null & _engine != null)
  30. {
  31. //_profile.UnloadEngineAsync(_engine.Settings.EngineId).Wait();
  32. }
  33. _engine = null;
  34. _profile = null;
  35. }
  36. }
  37. public override bool CreateProfile(ref MipContext mipContext)
  38. {
  39. try
  40. {
  41. var profileSettings = new FileProfileSettings(mipContext,
  42. CacheStorageType.OnDiskEncrypted,
  43. new ConsentDelegateImplementation());
  44. // IFileProfile은 특정 애플리케이션에 대한 모든 SDK 작업의 루트입니다.
  45. _profile = Task.Run(async () => await MIP.LoadFileProfileAsync(profileSettings)).Result;
  46. }
  47. catch (Exception e)
  48. {
  49. SetError(1, "LabelManager::CreateProfile Failed.", e.Message);
  50. return false;
  51. }
  52. return true;
  53. }
  54. public override bool CreateEngine(ref Identity identity, ref AuthDelegateImplementation authDelegate)
  55. {
  56. try
  57. {
  58. authDelegate.ResetError();
  59. // 보호 엔진 설정 개체를 만듭니다. 첫 번째 매개변수인 엔진 ID에 빈 문자열을 전달하면 SDK가 GUID를 생성합니다.
  60. // 이메일 주소나 기타 고유한 값을 전달하면 동일한 사용자에 대해 캐시된 엔진이 매번 로드되도록 하는 데 도움이 됩니다.
  61. // 로캘 설정은 지원되며 특히 클라이언트 응용 프로그램의 경우 컴퓨터 로캘을 기반으로 제공되어야 합니다.
  62. var engineSettings = new FileEngineSettings(identity.Email, authDelegate, "", "en-US")
  63. {
  64. // Provide the identity for service discovery.
  65. Identity = identity,
  66. // Set ProtectionOnlyEngine to true for AD RMS as labeling isn't supported
  67. //ProtectionOnlyEngine = true
  68. };
  69. _engine = Task.Run(async () => await _profile.AddEngineAsync(engineSettings)).Result;
  70. Console.WriteLine(
  71. "File Engine Sensitivity Labels ======================================================");
  72. var labels = _engine.SensitivityLabels;
  73. for (int ii = 0; ii < labels.Count; ii++)
  74. {
  75. Console.WriteLine("{0}: {1}, {2}", ii.ToString(), labels[ii].Id + " : " + labels[ii].Name,
  76. labels[ii].IsActive);
  77. Label label = _engine.GetLabelById(labels[ii].Id);
  78. if (label.Children.Count > 0)
  79. {
  80. for (int jj = 0; jj < label.Children.Count; jj++)
  81. {
  82. Console.WriteLine("\t{0}: {1}, {2}", jj.ToString(),
  83. label.Children[jj].Id + " : " + label.Children[jj].Name, label.Children[jj].IsActive);
  84. }
  85. }
  86. // Console.WriteLine("{0}: {1}, {2}, {3}, {4}, {5}", ii.ToString(), label.Id + " : " + label.Name, label.IsActive,
  87. // label.Sensitivity, label.ActionSource, label.Description);
  88. }
  89. Console.WriteLine("=======================================================================");
  90. }
  91. catch (Exception e)
  92. {
  93. if (authDelegate.LastErrNo != 0)
  94. {
  95. SetError(authDelegate.LastErrNo, "LabelManager::CreateEngine Failed.", authDelegate.LastErrMsg);
  96. }
  97. else
  98. {
  99. SetError(2, "LabelManager::CreateEngine Failed.", e.Message);
  100. }
  101. return false;
  102. }
  103. return true;
  104. }
  105. public IEnumerable<Label> SensitivityLabels()
  106. {
  107. return _engine.SensitivityLabels;
  108. }
  109. private IFileHandler CreateFileHandler(string inputFile, string outputFile)
  110. {
  111. try
  112. {
  113. var handler = Task.Run(async () => await _engine.CreateFileHandlerAsync(inputFile, outputFile, true))
  114. .Result;
  115. return handler;
  116. }
  117. catch (Exception ex)
  118. {
  119. SetError(91, "LabelManager::CreateFileHandler Failed.", ex.Message);
  120. }
  121. return null;
  122. }
  123. public AipFileInfo GetFileInfo(string fileName)
  124. {
  125. var handler = CreateFileHandler(fileName, fileName);
  126. if (handler == null)
  127. {
  128. return null;
  129. }
  130. AipFileInfo fileInfo = new AipFileInfo
  131. {
  132. ContentLabel = null,
  133. Protection = null,
  134. OutputFileName = null
  135. };
  136. fileInfo.OutputFileName = handler.OutputFileName;
  137. if (handler.Label != null)
  138. {
  139. fileInfo.ContentLabel = new AipContentLabel()
  140. {
  141. Label = Utilities.LabelToAip(handler.Label.Label),
  142. CreationTime = handler.Label.CreationTime,
  143. AssignmentMethod = (AipAssignmentMethod)handler.Label.AssignmentMethod,
  144. IsProtectionAppliedFromLabel = handler.Label.IsProtectionAppliedFromLabel,
  145. };
  146. }
  147. if (handler.Protection != null)
  148. {
  149. fileInfo.Protection = new AipProtection()
  150. {
  151. Owner = handler.Protection.Owner,
  152. IssuedTo = handler.Protection.IssuedTo,
  153. IsIssuedToOwner = handler.Protection.IsIssuedToOwner,
  154. ContentId = handler.Protection.ContentId,
  155. AuditedExtractAllowed = handler.Protection.AuditedExtractAllowed,
  156. BlockSize = handler.Protection.BlockSize,
  157. ProtectionDescriptor = null,
  158. };
  159. if (handler.Protection.ProtectionDescriptor != null)
  160. {
  161. fileInfo.Protection.ProtectionDescriptor = new AipProtectionDescriptor()
  162. {
  163. ProtectionType = (AipProtectionType)handler.Protection.ProtectionDescriptor.ProtectionType,
  164. TemplateId = handler.Protection.ProtectionDescriptor.TemplateId,
  165. LabelInformation = new AipLabelInfo(),
  166. LabelId = handler.Protection.ProtectionDescriptor.LabelId,
  167. Owner = handler.Protection.ProtectionDescriptor.Owner,
  168. ContentId = handler.Protection.ProtectionDescriptor.ContentId,
  169. Name = handler.Protection.ProtectionDescriptor.Name,
  170. Description = handler.Protection.ProtectionDescriptor.Description,
  171. AllowOfflineAccess = handler.Protection.ProtectionDescriptor.AllowOfflineAccess,
  172. Referrer = handler.Protection.ProtectionDescriptor.Referrer,
  173. ContentValidUntil = handler.Protection.ProtectionDescriptor.ContentValidUntil,
  174. DoubleKeyUrl = handler.Protection.ProtectionDescriptor.DoubleKeyUrl,
  175. };
  176. if (handler.Protection.ProtectionDescriptor.LabelInformation != null)
  177. {
  178. fileInfo.Protection.ProtectionDescriptor.LabelInformation.LabelId =
  179. handler.Protection.ProtectionDescriptor.LabelInformation.LabelId;
  180. fileInfo.Protection.ProtectionDescriptor.LabelInformation.TenantId =
  181. handler.Protection.ProtectionDescriptor.LabelInformation.TenantId;
  182. }
  183. }
  184. }
  185. return fileInfo;
  186. }
  187. }
  188. }