mip::PolicyEngine 实现策略 SDK 可以执行的所有操作,但不包括加载配置文件。
实现:添加策略引擎
实现:创建策略引擎设置
与配置文件类似,引擎还需要设置对象 mip::PolicyEngine::Settings。 此对象存储唯一引擎标识符、实现对象 mip::AuthDelegate 、用于调试或遥测的可自定义客户端数据,以及(可选)区域设置。
以下示例使用应用程序用户的标识创建名为 PolicyEngine::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::Profile 创建 Promise,不如使用 mip::PolicyEngine。
// 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 将标签应用于文件。