Microsoft 信息保护 SDK - 文件 SDK 引擎概念

MIP 文件 SDK 中的 mip::FileEngine 为代表指定标识执行的所有操作提供接口。 为登录到应用程序的每个用户添加一个引擎。 引擎在该身份的上下文中执行所有操作。

这 FileEngine 有两个主要责任:列出经过身份验证的用户的标签,并创建文件处理程序来代表用户执行文件操作。

  • mip::FileEngine
  • ListSensitivityLabels():获取已加载引擎的标签列表。
  • CreateFileHandler():为特定文件或流创建一个 mip::FileHandler。

添加文件引擎

如 配置文件和引擎对象中所述,引擎可以有两种状态 - CREATED 或 LOADED。 如果不是其中的两个状态之一,则不存在。 若要创建和加载状态,请对它进行单个调用 FileProfile::LoadAsync。 如果引擎已处于缓存状态,则为 LOADED. 如果不存在,则为 CREATED 和 LOADED。 CREATED 表示应用程序包含加载引擎所需的服务中的所有信息。 LOADED 表示使用引擎所需的所有数据结构都存在于内存中。

创建文件引擎设置

与配置文件类似,引擎也需要设置对象 mip::FileEngine::Settings。 此对象存储唯一引擎标识符、 mip::AuthDelegate 实现、用于调试或遥测的可自定义客户端数据,以及(可选)区域设置。

在这里,我们使用应用程序用户的标识创建一个名为 engineSettings 的 FileEngine::Settings 对象。

FileEngine::Settings engineSettings(
  mip::Identity(mUsername), // mip::Identity.
  authDelegateImpl,         // auth delegate object
  "",                       // Client data. Customizable by developer, stored with engine.
  "en-US",                  // Locale.
  false);                   // Load sensitive information types for driving classification.

以这种方式创建 engineSettings 时,还要显式设置一个唯一的 engineId:

engineSettings.SetEngineId(engineId);

使用 用户名或电子邮件 有助于确保每次用户使用服务或应用程序时,相同的引擎都会加载。

提供自定义引擎 ID 也是有效的:

FileEngine::Settings engineSettings(
  "myEngineId",     // string
  authDelegateImpl, // auth delegate object
  "",               // Client data in string format. Customizable by developer, stored with engine.
  "en-US",          // Locale. Default is en-US
  false);           // Load sensitive information types for driving classification. Default is false.

最佳做法是使用第一个参数, id将引擎连接到关联的用户。 电子邮件地址、UPN 或 Microsoft Entra 对象 GUID 有助于确保 ID 是唯一的,并且无需调用服务即可从本地状态加载。

添加文件引擎

若要添加引擎,请回到用于加载配置文件的 Promise/Future 模式。 与其为 mip::FileProfile 创建 Promise,不如使用 mip::FileEngine 来创建它。

  //auto profile will be std::shared_ptr<mip::FileProfile>
  auto profile = profileFuture.get();

  // Instantiate the AuthDelegate implementation.
  auto authDelegateImpl = std::make_shared<sample::auth::AuthDelegateImpl>(appInfo, userName, password);

  //Create the FileEngine::Settings object
  FileEngine::Settings engineSettings("UniqueID", authDelegateImpl, "");

  //Create a promise for std::shared_ptr<mip::FileEngine>
  auto enginePromise = std::make_shared<std::promise<std::shared_ptr<mip::FileEngine>>>();

  //Instantiate the future from the promise
  auto engineFuture = enginePromise->get_future();

  //Add the engine using AddEngineAsync, passing in the engine settings and the promise
  profile->AddEngineAsync(engineSettings, enginePromise);

  //get the future value and store in std::shared_ptr<mip::FileEngine>
  auto engine = engineFuture.get();

该代码可将经过身份验证的用户的引擎添加到配置文件中。

列出敏感度标签

使用添加的引擎,可以通过调用 engine->ListSensitivityLabels()来列出可供经过身份验证的用户使用的所有敏感度标签。

ListSensitivityLabels() 从服务中提取特定用户的标签和属性列表。 结果将存储在 std::shared_ptr<mip::Label> 的矢量中。

有关详细信息,请参阅 mip::Label 类参考。

ListSensitivityLabels()

std::vector<shared_ptr<mip::Label>> labels = engine->ListSensitivityLabels();

或者简化为:

auto labels = engine->ListSensitivityLabels();

打印这些名称表明,应用程序已成功从服务中拉取了策略并获取了标签。 若要应用标签,需要标签标识符。 以下代码遍历所有标签,并显示每个父标签和子标签的 name 和 id。

//Iterate through all labels in the vector
for (const auto& label : labels) {
  //Print label name and GUID
  cout << label->GetName() << " : " << label->GetId() << endl;

  //Print child label name and GUID
  for (const auto& child : label->GetChildren()) {
    cout << "->  " << child->GetName() <<  " : " << child->GetId() << endl;
  }
}

可以使用返回GetSensitivityLabels()的mip::Label集合来显示用户可用的所有标签,然后在选中后,使用 ID 将标签应用于文件。

后续步骤

现在,配置文件已加载、引擎已添加,并且标签已可用,您可以添加处理程序,开始读取、写入或删除文件中的标签。 有关详细信息,请参阅 MIP SDK 中的文件处理程序。