Microsoft 信息保护 SDK - 策略 SDK 配置文件概念

在可以执行任何策略 SDK 操作之前,必须先加载 mip::Profile

以下两个示例演示了如何使用状态存储的本地存储以及仅在内存中创建 profileSettings 对象。

加载配置文件

现在,已经定义了 MipContextProfileObserver,我们将使用它来实例化 mip::PolicyProfile。 创建 mip::PolicyProfile 对象需要 mip::PolicyProfile::Settingsmip::MipContext

Profile::Settings 参数

PolicyProfile::Settings 构造函数接受四个参数,如下所列:

  • const std::shared_ptr<MipContext>:已初始化为存储应用程序信息、状态路径等的 mip::MipContext 对象。
  • mip::CacheStorageType:定义如何存储状态:在内存中、在磁盘上或在磁盘上并加密。 有关更多详细信息,请参阅缓存存储概念
  • std::shared_ptr<mip::PolicyProfile::Observer> observer:指向配置文件 Observer 实现的共享指针(在 PolicyProfileProtectionProfileFileProfile 中)。

以下两个示例演示了如何使用状态存储的本地存储以及仅在内存中创建 profileSettings 对象。

仅在内存中存储状态

mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };

std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
				                                                                                  "mip_data",
                                                                                        		  mip::LogLevel::Trace,
                                                                                                  false);

std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);

PolicyProfile::Settings profileSettings(
    mMipContext,                                  // mipContext object
    mip::CacheStorageType::InMemory,              // use in memory storage
    std::make_shared<PolicyProfileObserverImpl>()); // new protection profile observer

从磁盘上的存储路径读/写配置文件设置

mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };

std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
			                                                                                      "mip_data",
                                                                                       			  mip::LogLevel::Trace,
                                                                                                  false);

std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);

PolicyProfile::Settings profileSettings(
    mipContext,                                    // mipContext object
    mip::CacheStorageType::OnDisk,                 // use on disk storage
    std::make_shared<PolicyProfileObserverImpl>());  // new protection profile observer

然后使用承诺/未来模式加载 Profile

auto profilePromise = std::make_shared<std::promise<std::shared_ptr<Profile>>>();
auto profileFuture = profilePromise->get_future();
Profile::LoadAsync(profileSettings, profilePromise);

如果配置文件 ProfileObserver::OnLoadSuccess 加载成功,则会通知已实现 mip::Profile::Observer::OnLoadSuccess。 在这种情况下,生成的对象 mip::Profile 以及上下文将作为参数传入观察者函数。

“上下文”是指向我们为了处理异步操作而创建的 std::promise 的指针。 该函数只是将承诺的值设置为作为第一个参数传入的配置文件对象。 当主函数使用 Future.get() 时,结果可以存储在发起调用的线程中的新对象中。

//get the future value and store in profile.
auto profile = profileFuture.get();

综合运用

完全实现观察者和身份验证委派后,现在可以完全加载配置文件。 下面的代码截图假定已包含所有必要的标头。

int main()
{
    const string userName = "MyTestUser@consoto.com";
    const string password = "P@ssw0rd!";
    const string clientId = "MyClientId";

    mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };
 
    std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
				                                                                                       "mip_data",
                                                                                        			   mip::LogLevel::Trace,
                                                                                                       false);

    std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);

    PolicyProfile::Settings profileSettings(
        mMipContext,                                    // mipContext object
        mip::CacheStorageType::OnDisk,                 // use on disk storage
        std::make_shared<PolicyProfileObserverImpl>());  // new protection profile observer

    auto profilePromise = std::make_shared<promise<shared_ptr<PolicyProfile>>>();
    auto profileFuture = profilePromise->get_future();
    Profile::LoadAsync(profileSettings, profilePromise);
    auto profile = profileFuture.get();
}

最终结果是,我们已成功加载配置文件并将其存储在名为 profile 的对象中。

后续步骤

现在已经添加好配置文件,下一步将需要向配置文件添加引擎。

策略引擎概念