123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Microsoft.InformationProtection;
- using Microsoft.InformationProtection.File;
- using LogLevel = Microsoft.InformationProtection.LogLevel;
- using Serilog;
- using Serilog.Core;
- namespace AipGateway.AIP
- {
- public class AipFileManager
- {
- //private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- private readonly Logger _logger;
- private readonly ILogger _log;
- public int LastErrNo { get; internal set; }
- public string LastErrMsg { get; internal set; }
- private readonly AipConfig _aipConfig;
- private readonly ApplicationInfo _appInfo;
- private AuthDelegateImplementation _authDelegate;
- private MipContext _mipContext;
- private readonly FileManager _fileManager = null;
- private readonly PolicyManager _policyManager = null;
- private readonly ProtectionManager _protectionManager = null;
- public AipFileManager(Logger logger, AipConfig aipConfig)
- {
- _logger = logger;
- _log = logger.ForContext<AipFileManager>();
- _aipConfig = aipConfig;
- _appInfo = new ApplicationInfo
- {
- ApplicationId = aipConfig.ClientId,
- ApplicationName = aipConfig.AppName,
- ApplicationVersion = aipConfig.AppVersion
- };
- LastErrNo = 0;
- LastErrMsg = string.Empty;
- _fileManager = new FileManager(logger, aipConfig.ClientId);
- _policyManager = new PolicyManager(logger, aipConfig.ClientId);
- _protectionManager = new ProtectionManager(logger, aipConfig.ClientId);
- }
- ~AipFileManager() => this.Dispose(false);
- public void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize((object)this);
- }
- private void Dispose(bool disposing)
- {
- lock (this)
- {
- if (_fileManager != null)
- {
- _fileManager.Dispose();
- }
- if (_policyManager != null)
- {
- _policyManager.Dispose();
- }
- if (_protectionManager != null)
- {
- _protectionManager.Dispose();
- }
- _authDelegate = null;
- }
- }
- private void SetError(int errNo, string errMsg1, string errMsg2 = "", bool isThrowEx = true)
- {
- LastErrNo = errNo;
- if (errMsg2 == "")
- {
- LastErrMsg = errMsg1;
- }
- else
- {
- LastErrMsg = errMsg1 + "\r\n" + errMsg2;
- }
- _log.Error("AipFileManager::SetError ==> {0}, {1}, {2}", errNo, errMsg1, errMsg2);
- if (isThrowEx && LastErrNo != 0)
- {
- throw new AipFileException(errNo, LastErrMsg);
- }
- }
- public bool Initialize()
- {
- _authDelegate = new AuthDelegateImplementation(_logger, _aipConfig);
- try
- {
- // Initialize SDK DLLs. If DLLs are missing or wrong type, this will throw an exception
- MIP.Initialize(MipComponent.File);
- //MIP.Initialize(MipComponent.Policy);
- //MIP.Initialize(MipComponent.Protection); // Protection
- }
- catch (Exception ex) {
- SetError(1, "MIP.Initialize Failed.", ex.Message, false);
- return false;
- }
- // Create MipConfiguration Object
- var mipConfiguration = new MipConfiguration(_appInfo, _aipConfig.MipData, LogLevel.Warning, false);
- // mipConfiguration.LoggerDelegateOverride = this;
-
- //Enable DKE
- // Dictionary<FlightingFeature, bool> featureSettings = mipConfiguration.FeatureSettingsOverride ?? new Dictionary<FlightingFeature, bool>();
- // featureSettings[FlightingFeature.DoubleKey] = true;
- // mipConfiguration.FeatureSettingsOverride = featureSettings;
- // Create MipContext using MipConfiguration
- _mipContext = MIP.CreateMipContext(mipConfiguration);
- return true;
- }
- public bool CreateProfile()
- {
- if (_authDelegate == null || _mipContext == null)
- {
- SetError(999, "AipFileManager::CreateProfile Failed.", "Library Is not Initialized.", false);
- return false;
- }
- if (!_fileManager.CreateProfile(ref _mipContext))
- {
- SetError(11, "AipFileManager::CreateProfile Failed.", "FileManager::CreateProfile Failed.", false);
- return false;
- }
- if (!_policyManager.CreateProfile(ref _mipContext))
- {
- SetError(12, "AipFileManager::CreateProfile Failed.", "PolicyManager::CreateProfile Failed.", false);
- return false;
- }
- if (!_protectionManager.CreateProfile(ref _mipContext))
- {
- SetError(13, "AipFileManager::CreateProfile Failed.", "ProtectionManager::CreateProfile Failed.", false);
- return false;
- }
- return true;
- }
- public bool CreateEngine()
- {
- if (_authDelegate == null || _mipContext == null)
- {
- SetError(999, "AipFileManager::CreateEngine Failed.", "Library Is not Initialized.", false);
- return false;
- }
-
- Identity identity = new Identity(_aipConfig.EMail);
- if (!_fileManager.CreateEngine(ref identity, ref _authDelegate))
- {
- SetError(21, "AipFileManager::CreateEngine ", "FileManager::CreateEngine Failed.", false);
- return false;
- }
- if (!_policyManager.CreateEngine(ref identity, ref _authDelegate))
- {
- SetError(22, "AipFileManager::CreateEngine ", "PolicyManager::CreateEngine Failed.", false);
- return false;
- }
- if (!_protectionManager.CreateEngine(ref identity, ref _authDelegate))
- {
- SetError(23, "AipFileManager::CreateEngine ", "ProtectionManager::CreateEngine Failed.", false);
- return false;
- }
- return true;
- }
- public bool IsProtected(Stream inputStream, string filePath)
- {
- IFileStatus status = FileHandler.GetFileStatus(inputStream, filePath, _mipContext);
- bool result = status.IsProtected();
- return result;
- }
- public bool IsLabeledOrProtected(Stream inputStream, string filePath)
- {
- IFileStatus status = FileHandler.GetFileStatus(inputStream, filePath, _mipContext);
- bool isLabeled = status.IsLabeled();
- bool isProtected = status.IsProtected();
- return (isLabeled || isProtected);
- }
- public AipFileInfo GetFileInfo(string fileName)
- {
- if (_authDelegate == null || _mipContext == null)
- {
- SetError(10, "AipFileManager::GetFileInfo Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
- return new AipFileInfo
- {
- errorCode = 10,
- errorMessage = "AIP 라이브러리가 초기화 되지 않았습니다.",
- };
- }
- AipFileInfo fileInfo = _fileManager.GetFileInfo(fileName);
- if (fileInfo == null)
- {
- SetError(_fileManager.LastErrNo, "AipFileManager::GetFileInfo Failed.", _fileManager.LastErrMsg);
- return new AipFileInfo
- {
- errorCode = _fileManager.LastErrNo == 0 ? 11 : _fileManager.LastErrNo,
- errorMessage = _fileManager.LastErrMsg,
- };
- }
- return fileInfo;
- }
- public AipFileInfo GetFileInfo(Stream fileStream, string outputFileName)
- {
- if (_authDelegate == null || _mipContext == null)
- {
- SetError(10, "AipFileManager::GetFileInfo Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
- return new AipFileInfo
- {
- errorCode = 10,
- errorMessage = "AIP 라이브러리가 초기화 되지 않았습니다.",
- };
- }
- AipFileInfo fileInfo = _fileManager.GetFileInfo(fileStream, outputFileName);
- if (fileInfo == null)
- {
- SetError(_fileManager.LastErrNo, "AipFileManager::GetFileInfo Failed.", _fileManager.LastErrMsg);
- return new AipFileInfo
- {
- errorCode = _fileManager.LastErrNo == 0 ? 12 : _fileManager.LastErrNo,
- errorMessage = _fileManager.LastErrMsg,
- };
- }
- return fileInfo;
- }
- public List<AipLabel> SensitivityLabels()
- {
- if (_authDelegate == null || _mipContext == null)
- {
- SetError(10, "AipFileManager::SensitivityLabels Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
- return null;
- }
- var result = new List<AipLabel>();
- try
- {
- var labels = _fileManager.SensitivityLabels();
- foreach (var label in labels)
- {
- var aipLabel = Utilities.LabelToAip(label);
- if (aipLabel != null)
- {
- if (label.Children.Count > 0)
- {
- foreach (var child in label.Children)
- {
- var aipChildLabel = Utilities.LabelToAip(child);
- if (aipChildLabel != null) aipLabel.Children.Add(aipChildLabel);
- }
- }
- result.Add(aipLabel);
- }
- }
- }
- catch (Exception ex)
- {
- SetError(31, "AipFileManager::SensitivityLabels Failed.", ex.Message);
- result = new List<AipLabel>();
- }
- return result;
- }
- public List<AipLabel> ListSensitivityLabels()
- {
- if (_authDelegate == null || _mipContext == null)
- {
- SetError(10, "AipFileManager::ListSensitivityLabels Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
- return null;
- }
- var result = new List<AipLabel>();
- try
- {
- var labels = _policyManager.ListSensitivityLabels();
- foreach (var label in labels)
- {
- var aipLabel = Utilities.LabelToAip(label);
- if (aipLabel != null)
- {
- if (label.Children.Count > 0)
- {
- foreach (var child in label.Children)
- {
- var aipChildLabel = Utilities.LabelToAip(child);
- if (aipChildLabel != null) aipLabel.Children.Add(aipChildLabel);
- }
- }
- result.Add(aipLabel);
- }
- }
- }
- catch (Exception ex)
- {
- SetError(32, "AipFileManager::ListSensitivityLabels Failed.", ex.Message);
- result = new List<AipLabel>();
- }
- return result;
- }
- public List<AipTemplate> GetTemplates()
- {
- if (_authDelegate == null || _mipContext == null)
- {
- SetError(10, "AipFileManager::GetTemplates Failed.", "AIP 라이브러리가 초기화 되지 않았습니다.");
- return null;
- }
- var result = new List<AipTemplate>();
- try
- {
- var templates = _protectionManager.GetTemplates();
- foreach (var template in templates)
- {
- var aipTemplate = Utilities.TemplateToAip(template);
- if (aipTemplate != null)
- {
- result.Add(aipTemplate);
- }
- }
- }
- catch (Exception ex)
- {
- SetError(33, "AipFileManager::GetTemplates Failed.", ex.Message);
- result = new List<AipTemplate>();
- }
- return result;
- }
- public SetFileInfo SetLabel(string fileName, string actualFileName, string email, string labelId, string templateId, string comments = "")
- {
- // 레이블 및 템플릿 정보 가져오기
- if (comments == "")
- {
- comments = "SetLabel";
- }
- return _fileManager.SetLabel(fileName, actualFileName, email, labelId, templateId, comments);
- }
- public SetFileInfo SetLabel(Stream fileStream, string actualFileName, string email, string labelId, string templateId, string comments = "")
- {
- // 레이블 및 템플릿 정보 가져오기
- if (comments == "")
- {
- comments = "SetLabel by " + email;
- }
- return _fileManager.SetLabel(fileStream, actualFileName, email, labelId, templateId, comments);
- }
- public SetFileInfo DeleteLabel(string fileName, string actualFileName, string email, string comments = "", bool isDelProtection = false)
- {
- if (comments == "")
- {
- comments = "Delete Label by " + email;
- }
- return _fileManager.DeleteLabel(fileName, actualFileName, email, comments, isDelProtection);
- }
- public SetFileInfo DeleteLabel(Stream fileStream, string actualFileName, string email, string comments = "", bool isDelProtection = false)
- {
- if (comments == "")
- {
- comments = "Delete Label by " + email;
- }
- return _fileManager.DeleteLabel(fileStream, actualFileName, email, comments, isDelProtection);
- }
-
- public SetFileInfo SetProtection(string fileName, string actualFileName, string email, string templateId, string comments = "")
- {
- if (comments == "")
- {
- comments = "SetProtection by " + email;
- }
- return _fileManager.SetProtection(fileName, actualFileName, email, templateId, comments);
- }
- public SetFileInfo SetProtection(Stream fileStream, string actualFileName, string email, string templateId, string comments = "")
- {
- if (comments == "")
- {
- comments = "SetProtection by " + email;
- }
- return _fileManager.SetProtection(fileStream, actualFileName, email, templateId, comments);
- }
- public SetFileInfo DeleteProtection(string fileName, string actualFileName, string email, string comments = "")
- {
- if (comments == "")
- {
- comments = "Delete Protection by " + email;
- }
- return _fileManager.RemoveProtection(fileName, actualFileName, email, comments);
- }
- public SetFileInfo DeleteProtection(Stream fileStream, string actualFileName, string email, string comments = "")
- {
- if (comments == "")
- {
- comments = "Delete Protection by " + email;
- }
- return _fileManager.RemoveProtection(fileStream, actualFileName, email, comments);
- }
- public AipFileStatus GetAipFileStatus(string fileName)
- {
- try
- {
- var fileStatus = FileHandler.GetFileStatus(fileName, _mipContext);
- AipFileStatus result = new AipFileStatus
- {
- IsProtected = fileStatus.IsProtected(),
- IsLabeled = fileStatus.IsLabeled(),
- ContainsProtectedObjects = fileStatus.ContainsProtectedObjects()
- };
- return result;
- }
- catch (Exception ex)
- {
- SetError(81, "AipFileManager::GetAipFileStatus Failed.", ex.Message);
- }
- return null;
- }
- }
- }
|