实现:添加保护引擎
在文件 SDK 中,该 mip::ProtectionProfile 类是所有 SDK 操作的根类。 创建配置文件后,可以将引擎添加到配置文件。
以下示例演示如何对单个经过身份验证的用户使用单个引擎。
实现:创建保护引擎设置
与配置文件类似,引擎还需要设置对象 mip::ProtectionEngine::Settings。 此对象存储唯一引擎标识符、 mip::AuthDelegate 用于获取令牌、可用于调试或遥测的可自定义客户端数据,以及(可选)区域设置。
以下代码创建一个名为 ProtectionEngine::SettingsengineSettings 的对象。
ProtectionEngine::Settings engineSettings("UniqueID", authDelegate, "");
Note
如果使用此方法创建保护设置对象,还应使用 SetCloud() 在 SetIdentity() 对象上设置标识,或使用 ProtectionEngine::Settings 设置目标云环境。
最佳做法是使用第一个参数、 ID 来标识关联的用户或使用 mip::Identity 对象。 若要使用 mip::Identity 初始化设置:
ProtectionEngine::Settings engineSettings(mip::Identity("Bob@Contoso.com"), authDelegate, "");
以这种方式创建 engineSettings 时,还要使用以下方法显式设置唯一的 engineId:
engineSettings.SetEngineId(engineId);
使用 用户名或电子邮件 来帮助确保每次用户使用服务或应用程序时引擎始终加载。
实现:添加保护引擎
使用加载 profile 时所用的 Future/Promise 模式来添加引擎。 与其为 mip::ProtectionProfile 创建 Promise,不如使用 mip::ProtectionEngine。
//auto profile will be std::shared_ptr<mip::ProtectionProfile>
auto profile = profileFuture.get();
//Create the ProtectionEngine::Settings object
ProtectionEngine::Settings engineSettings("UniqueID", authDelegate, "");
//Create a promise for std::shared_ptr<mip::ProtectionEngine>
auto enginePromise = std::make_shared<std::promise<std::shared_ptr<mip::ProtectionEngine>>>();
//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::ProtectionEngine>
auto engine = engineFuture.get();
该代码会将一个适用于已通过身份验证用户的引擎添加到配置文件中。
实现:列出模板
添加引擎后,可以通过调用 engine->GetTemplatesAsync()来列出可供经过身份验证的用户使用的所有敏感度模板。
GetTemplatesAsync() 提取模板描述符的列表。 该方法将结果存储在 . 的向量中 std::shared_ptr<mip::TemplateDescriptor>。
实现:ListSensitivityTemplates()
auto loadPromise = std::make_shared<std::promise<std::vector<std::shared_ptr<mip::TemplateDescriptor>>>>();
auto loadFuture = loadPromise->get_future();
engine->GetTemplatesAsync(engineObserver, loadPromise);
auto templates = loadFuture.get();
实现:打印模板 ID
//Iterate through all template IDs in the vector
for (const auto& temp : templates) {
cout << "Template:" << "\n\tId: " << temp->GetId() << endl;
}
输出这些名称表明,应用程序已成功从服务中拉取策略并获取了模板。 若要应用模板,需要模板标识符。
只能通过查看 ComputeActions() 的结果,使用策略 SDK 将模板映射到标签。
后续步骤
加载配置文件、添加引擎并具有模板后,可以添加处理程序以开始从文件中读取、写入或删除模板。 请参阅 保护处理程序概念。
- 有关示例代码,请参阅 GitHub 上的 MIP 保护 SDK C++ 示例。