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

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

實作:新增政策引擎

實作:建立政策引擎設定

與設定檔類似,引擎也需要設定物件 mip::PolicyEngine::Settings 此物件儲存唯一引擎識別碼、實作物件 mip::AuthDelegate 、可自訂的客戶端資料用於除錯或遙測,以及可選的地點。

以下範例利用應用程式使用者的身份建立一個 PolicyEngine::Settings 名為 engineTSettings 的物件:

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 建立 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;
  }
}

你可以使用 mip::Label 傳回的 ListSensitivityLabels() 集合來顯示使用者可使用的所有標籤,然後在選取後,使用該 ID 將標籤套用至檔案。

下一步