Microsoft 信息保护文件 SDK - 减少文件上的敏感度标签的操作理由 (C++)

本快速入门解决了在标签策略需要理由时处理降级标签操作的问题。 此处,我们将使用 mip::FileHandler 类更改文件的标签。 有关更多详细信息,请参阅 API 参考

先决条件

如果尚未完成,请确保先完成以下先决条件,然后再继续:

  • 首先完成快速入门:设置/获取敏感度标签 (C++),构建一个入门级 Visual Studio 解决方案,以列出组织的敏感度标签,设置和读取文件的敏感度标签。 此“如何 - 降级/删除需要理由的标签 (C++)”快速入门建立在上一个快速入门的基础之上。
  • 可选:查看 MIP SDK 概念中的文件处理程序概念

添加逻辑以将较低的标签设置为受保护的文件

使用 mip::FileHandler 对象添加逻辑以在文件上设置敏感度标签。

  1. 打开在前面的快速入门:设置/获取敏感度标签 (C++)中创建的 Visual Studio 解决方案。

  2. 使用“解决方案资源管理器”,打开项目中包含 main() 方法实现的 .cpp 文件。 该文件默认与包含它的项目同名,该名称在项目创建期间指定。

  3. 在该文件顶部将以下 #include 和 using 指令添加到相应的现有指令之下:

    
        #include "mip/file/file_error.h"
    
        using mip::JustificationRequiredError;
        using std::cin;
    
    
  4. 将先前快速入门中的 <label-id> 值更新为需要减少理由的敏感度标签。 在此快速入门运行期间,我们将首先设置此标签,然后在后续步骤中尝试通过代码片段减少它。

  5. main() 主体的末尾,在 system("pause"); 下方和关闭块上方(你在上一个快速入门中离开的位置),插入以下代码:


// Downgrade label
// Set paths and lower label ID
// Set a new label on input file.

string lowerlabelId = "<lower-label-id>";
cout << "\nApplying new Label ID " << lowerlabelId << " to " << filePathOut << endl;
mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);

// Try to apply a label with lower sensitivity.
try
{
    handler->SetLabel(engine->GetLabelById(lowerlabelId), labelingOptions, mip::ProtectionSettings());
}

catch (const mip::JustificationRequiredError& e)
{
    // Request justification from user.
    cout<<"Please provide justification for downgrading a label: "<<endl;
    string justification;
    cin >> justification;

    // Set Justification provided flag
    bool isDowngradeJustified = true;
    mip::LabelingOptions labelingOptions(mip::AssignmentMethod::PRIVILEGED);
    labelingOptions.SetDowngradeJustification(isDowngradeJustified,justification);

    //Set new label.
    handler->SetLabel(engine->GetLabelById(lowerlabelId), labelingOptions, mip::ProtectionSettings());
}

catch (const std::exception& e)
{
    cout << "An exception occurred... did you specify a valid label ID?\n\n" << e.what() << "'\n";
    system("pause");
    return 1;
}

// Commit changes, save as a different output file
string lowerFilePathOut = "<lower-output-file-path>";
try
{
    cout << "Committing changes" << endl;
    auto commitPromise = std::make_shared<std::promise<bool>>();
    auto commitFuture = commitPromise->get_future();
    handler->CommitAsync(lowerFilePathOut, commitPromise);
    if (commitFuture.get()) {
        cout << "\nLabel committed to file: " << lowerFilePathOut << endl;
    }
    else {
        cout << "Failed to label: " + lowerFilePathOut << endl;
        return 1;
    }
}
catch (const std::exception& e)
{
    cout << "An exception occurred... did you specify a valid commit file path?\n\n" << e.what() << "'\n";
    system("pause");
    return 1;
}
system("pause");

// Set up async FileHandler for output file operations
string lowerActualFilePath = "<lower-content-identifier>";
try
{
    auto handlerPromise = std::make_shared<std::promise<std::shared_ptr<FileHandler>>>();
    auto handlerFuture = handlerPromise->get_future();
    engine->CreateFileHandlerAsync(
        lowerFilePathOut,
        lowerActualFilePath,
        true,
        std::make_shared<FileHandlerObserver>(),
        handlerPromise);

    handler = handlerFuture.get();
}
catch (const std::exception& e)
{
    cout << "An exception occurred... did you specify a valid output file path?\n\n" << e.what() << "'\n";
    system("pause");
    return 1;
}

// Get the lowered label from output file
try
{
    cout << "\nGetting the label committed to file: " << lowerFilePathOut << endl;
    auto lowerLabel = handler->GetLabel();
    cout << "Name: " + lowerLabel->GetLabel()->GetName() << endl;
    cout << "Id: " + lowerLabel->GetLabel()->GetId() << endl;
}
catch (const std::exception& e)
{
    cout << "An exception occurred... did you specify a valid label ID?\n\n" << e.what() << "'\n";
    system("pause");
    return 1;
}
system("pause");

  1. 将源代码中的占位符值替换为以下值:

    占位符
    <lower-label-id> 从上一个快速入门中的控制台输出复制的标签 ID,例如:bb7ed207-046a-4caf-9826-647cff56b990。 确保它的敏感度低于先前受保护的文件标签。
    <lower-output-file-path> 要将修改后的文件保存到的输出文件路径。
    <lower-content-identifier> 内容的易记标识符。

生成并测试应用

生成和测试客户端应用程序。

  1. 使用 CTRL-SHIFT-B(生成解决方案)生成客户端应用程序。 如果没有生成错误,请使用 F5(启动调试)运行应用程序。

  2. 如果项目成功生成并运行,则应用程序在 SDK 每次调用 AcquireOAuth2Token() 方法时都会提示输入访问令牌。

  Non-Business : 87ba5c36-17cf-14793-bbc2-bd5b3a9f95cz
  Public : 83867195-f2b8-2ac2-b0b6-6bb73cb33afz
  General : f42a3342-8706-4288-bd31-ebb85995028z
  Confidential : 074e457c-5848-4542-9a6f-34a182080e7z
  Highly Confidential : f55c2dea-db0f-47cd-8520-a52e1590fb6z
  Press any key to continue . . .

  Applying Label ID f55c2dea-db0f-47cd-8520-a52e1590fb6z to c:\Test\Test.docx
  Committing changes


  Label committed to file: c:\Test\Test.docx
  Press any key to continue . . .

  Run the PowerShell script to generate an access token using the following values, then copy/paste it below:
  Set $authority to: https://login.windows.net/37f4583d-9985-4e7f-a1ab-71afd8b55ba0
  Set $resourceUrl to: https://aadrm.com
  Sign in with user account: user1@tenant.onmicrosoft.com
  Enter access token: <paste-access-token-here>
  Press any key to continue . . .

  Getting the label committed to file: c:\Test\Test_labeled.docx
  Name: Highly Confidential
  Id: f55c2dea-db0f-47cd-8520-a52e1590fb6z
  Press any key to continue . . . 

  Applying new Label ID f42a3342-8706-4288-bd31-ebb85995028z to c:\Test\Test_labeled.docx
  Please provide justification for downgrading a label:
  Need for sharing with wider audience.
  Committing changes

  Label committed to file: c:\Test\Test_downgraded.docx
  Press any key to continue . . .

  Getting the label committed to file: c:\Test\Test_downgraded.docx
  Name: General
  Id: f42a3342-8706-4288-bd31-ebb85995028z
  Press any key to continue . . .

请注意,类似的方法应适用于 DeleteLabel() 操作,以防从文件中删除的标签需要根据标签策略要求的理由。 DeleteLabel() 函数会引发 mip::JustificationRequiredError 异常。 在成功删除标签之前,应在异常处理中将 isDowngradeJustified 标志设置为 true。