mip::PolicyEngine 实现策略 SDK 可以执行的所有操作,但不包括加载配置文件。
实现:添加策略引擎
实现:创建策略引擎设置
与配置文件类似,引擎还需要设置对象 mip::PolicyEngine::Settings。 此对象存储唯一引擎标识符、实现的对象 mip::AuthDelegate 、可用于调试或遥测的可自定义客户端数据,以及(可选)区域设置。
在这里,我们使用应用程序用户的标识创建名为 FileEngine::SettingsengineSettings 的对象:
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;
}
}
返回的mip::LabelListSensitivityLabels()集合可用于显示用户可用的所有标签,然后在选中时使用 ID 将标签应用于文件。