PolicyManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.InformationProtection;
  7. using Microsoft.InformationProtection.Policy;
  8. namespace AipGateway.AIP
  9. {
  10. public class PolicyManager : AbstractManager
  11. {
  12. private IPolicyProfile _profile = null;
  13. private IPolicyEngine _engine = null;
  14. public PolicyManager()
  15. {
  16. }
  17. ~PolicyManager() => this.Dispose(false);
  18. public override void Dispose()
  19. {
  20. this.Dispose(true);
  21. GC.SuppressFinalize((object)this);
  22. }
  23. protected virtual void Dispose(bool disposing)
  24. {
  25. lock (this)
  26. {
  27. if (_profile != null && _engine != null)
  28. {
  29. //_profile.UnloadEngineAsync(_engine.Settings.Id).Wait();
  30. _profile.Dispose();
  31. _engine.Dispose();
  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 PolicyProfileSettings(mipContext,
  42. CacheStorageType.OnDiskEncrypted);
  43. // IFileProfile은 특정 애플리케이션에 대한 모든 SDK 작업의 루트입니다.
  44. _profile = Task.Run(async () => await MIP.LoadPolicyProfileAsync(profileSettings)).Result;
  45. }
  46. catch (Exception e)
  47. {
  48. SetError(1, "PolicyManager::CreateProfile Failed.", e.Message);
  49. return false;
  50. }
  51. return true;
  52. }
  53. public override bool CreateEngine(ref Identity identity, ref AuthDelegateImplementation authDelegate)
  54. {
  55. try
  56. {
  57. authDelegate.ResetError();
  58. var engineSettings = new PolicyEngineSettings(identity.Email, authDelegate, "", "en-US")
  59. {
  60. // Provide the identity for service discovery.
  61. Identity = identity
  62. };
  63. _engine = Task.Run(async () => await _profile.AddEngineAsync(engineSettings)).Result;
  64. Console.WriteLine("Policy Engine Sensitivity Labels ======================================================");
  65. var labels = _engine.ListSensitivityLabels();
  66. for (int ii = 0; ii < labels.Count; ii++)
  67. {
  68. Console.WriteLine("{0}: {1}, {2}", ii.ToString(), labels[ii].Id + " : " + labels[ii].Name, labels[ii].IsActive);
  69. Label label = _engine.GetLabelById(labels[ii].Id);
  70. if (label.Children.Count > 0)
  71. {
  72. for (int jj = 0; jj < label.Children.Count; jj++)
  73. {
  74. Console.WriteLine("\t{0}: {1}, {2}", jj.ToString(), label.Children[jj].Id + " : " + label.Children[jj].Name, label.Children[jj].IsActive);
  75. }
  76. }
  77. }
  78. Console.WriteLine("=======================================================================");
  79. }
  80. catch (Exception e)
  81. {
  82. if (authDelegate.LastErrNo != 0)
  83. {
  84. SetError(authDelegate.LastErrNo, "PolicyManager::CreateEngine Failed.", authDelegate.LastErrMsg);
  85. }
  86. else
  87. {
  88. SetError(2, "PolicyManager::CreateEngine Failed.", e.Message);
  89. }
  90. return false;
  91. }
  92. return true;
  93. }
  94. public IEnumerable<Label> ListSensitivityLabels()
  95. {
  96. // 사용자 주체의 경우 이는 사용자별로 다릅니다.
  97. // 서비스 주체의 경우 이는 서비스별로 또는 전역적일 수 있습니다.
  98. return _engine.ListSensitivityLabels();
  99. }
  100. }
  101. }