Microsoft 信息保护 SDK - 策略 SDK 引擎概念

mip::PolicyEngine 实现策略 SDK 可以执行的所有操作(加载配置文件除外)。

实现:添加策略引擎

实现:创建策略引擎设置

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

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

PolicyEngine::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 也是有效的:

PolicyEngine::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 应能让引擎轻松连接到关联的用户,最好是用户主体名称。

实现:添加策略引擎

为了添加引擎,我们将返回用于加载配置文件的未来/承诺模式。 我们将使用 mip::PolicyEngine,而不是创建 mip::Profile 的承诺。


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

  // Create the delegate
  auto authDelegateImpl = std::make_shared<sample::auth::AuthDelegateImpl>(appInfo, userName, password);


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

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

  // 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::PolicyEngine>.
  auto engine = engineFuture.get();

上述代码的最终结果是,我们成功将已通过身份验证的用户的引擎添加到配置文件。

实现:列出敏感度标签

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

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

实现:ListSensitivityLabels()

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

实现:打印标签

//Iterate through all labels in the vector
for (const auto& label : labels) {
  //print the label name
  cout << label->GetName() << endl;
  //Iterate through all child labels
  for (const auto& child : label->GetChildren()) {
    //Print the label with some formatting
    cout << "->  " << child->GetName() << endl;
  }
}

打印名称是显示我们已成功从服务中拉取策略并且能够获取标签的简单方法。 要应用标签,则需要标签的标识符。 如果修改上面的截图以返回标签 ID,将会导致以下结果:

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;
  }
}

ListSensitivityLabels() 返回的 mip::Label 集合可用于显示可供用户使用的所有标签,然后时使用 ID(如果选中)将标签应用于文件。