共用方式為


Microsoft 資訊保護 SDK - 策略 SDK 引擎概念

mip::PolicyEngine 會實作原則 SDK 可以執行的所有作業,但載入配置檔除外。

實作:新增原則引擎

實施:建立政策引擎設定

與設定檔類似,引擎也需要設定物件 mip::PolicyEngine::Settings 此物件會儲存唯一的引擎標識碼、實 mip::AuthDelegate 作的物件、可用於偵錯或遙測的可自定義客戶端數據,以及選擇性地儲存地區設定。

在這裡,我們會使用應用程式使用者的身分識別來建立 FileEngine::Settings 名為 engineSettings 的物件:

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

使用使用者名稱或電子郵件有助於確保每次使用者使用服務或應用程式時,都會載入相同的引擎。

同樣有效的是提供自定義引擎識別碼:

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,應該是能夠讓引擎輕鬆連接到相關用戶的識別符,最好是使用者主體名稱。

實作:新增策略引擎

若要新增引擎,我們將回到用於載入設定檔的 Future/Promise 模式。 我們不會為 mip::Profile建立承諾,而是使用 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;
  }
}

mip::Label回的 ListSensitivityLabels() 集合可用來顯示使用者可用的所有標籤,並在選取後,使用 ID 將標籤套用到檔案上。