快速入门:使用命令行检查敏感文本

使用命令行检查敏感文本

本页面介绍如何使用命令行界面在 Cloud Data Loss Prevention API 执行基本任务。具体来说,本快速入门介绍了如何将短字符串发送到 DLP API 进行检查。

开始前须知

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 DLP API。

    启用 API

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    6. Click Continue.
    7. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  6. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  7. 将环境变量 GOOGLE_APPLICATION_CREDENTIALS 设置为包含凭据的 JSON 文件的路径。 此变量仅适用于当前的 shell 会话,因此,如果您打开新的会话,请重新设置该变量。

  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  9. 确保您的 Google Cloud 项目已启用结算功能

  10. 启用 DLP API。

    启用 API

  11. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    6. Click Continue.
    7. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  12. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  13. 将环境变量 GOOGLE_APPLICATION_CREDENTIALS 设置为包含凭据的 JSON 文件的路径。 此变量仅适用于当前的 shell 会话,因此,如果您打开新的会话,请重新设置该变量。

权限

检查内容需要 parent 中指定的项目的 serviceusage.services.use 权限。roles/editorroles/ownerroles.dlp.user 角色包含所需的权限,您也可以定义自己的自定义角色

如需在项目级层为您的用户授予 dlp.user 角色,请执行以下操作:

控制台

  1. 在 Google Cloud Console 中打开 IAM 页面。

    转到 IAM

  2. 如果尚未选择项目,请点击项目选择器,然后选择您的项目。

  3. 在 IAM 页面上,请按照以下步骤操作:

    • 要添加新用户,请点击 添加
    • 如需为现有用户添加 dlp.user 角色,请点击该用户对应的 修改主帐号,然后点击修改权限窗格中的添加其他角色

  4. 添加主帐号窗格中,请按照以下步骤操作:

    • 新主帐号字段中,输入要添加的用户的电子邮件地址,例如 test@example.com
    • 对于角色,点击选择角色,然后选择 Cloud DLP &DLP 用户

  5. 点击添加

如需了解详情,请参阅授予 IAM 角色

gcloud

  1. 要将单个绑定添加到项目的 IAM 政策,请输入以下命令:

    gcloud projects add-iam-policy-binding PROJECT_ID --member serviceAccount:SERVICE_ID --role roles/dlp.user

    替换以下内容:

    • PROJECT_ID项目 ID
    • SERVICE_ID:要使用的服务帐号。
  2. 将更新后的政策写入控制台窗口:

    bindings:
    - members:
      - group: EMAIL_ADDRESS
        role: roles/dlp.user
    

    EMAIL_ADDRESS 替换为要添加的用户的电子邮件地址。

设置 Cloud DLP CLI 应用

Node.js

  1. 下载并安装 Node.js 和 NPM

  2. 克隆或下载 Node.js DLP 客户端库的 zip 文件,然后展开所下载的文件。

  3. 打开命令行工具并导航到展开目录中的 samples 目录。

  4. samples 目录中运行 npm install 以安装应用依赖项。

  5. 如果您尚未创建 GCLOUD_PROJECT 环境变量,请创建该变量,并将其设置为与 Cloud DLP 搭配使用的 Google Cloud 项目的项目 ID

gcloud alpha dlp

  1. 安装并初始化 gcloud CLI

    此过程还需要 gcloud Alpha 命令组件。您可以立即安装,也可以在系统提示后进行安装。

  2. 激活服务帐号:

    gcloud auth activate-service-account SERVICE_ACCOUNT \\
     --key-file=PATH_TO_SERVICE_ACCOUNT_KEY

    替换以下内容:

    • SERVICE_ACCOUNT:您的服务帐号的 ID。
    • PATH_TO_SERVICE_ACCOUNT_KEY:包含服务帐号私钥的 JSON 文件的路径。这是您在准备工作部分中设置身份验证时下载的 JSON 文件。

检查字符串中的敏感信息

本部分介绍了如何使用 DLP API 扫描示例文本。

Node.js

此示例使用 inspectString Node.js 脚本。如果您尚未打开命令行工具,请将其打开。导航到在上一部分中下载并展开的 Node.js 示例代码库的 samples 文件夹。

运行以下命令:

node inspectString.js PROJECT_ID "My email address is joe@example.com."

PROJECT_ID 替换为您的项目 ID

您会收到以下输出结果:

Findings:
  Quote: joe@example.com
  Info type: EMAIL_ADDRESS
  Likelihood: LIKELY

gcloud alpha dlp

本示例使用 gcloud alpha dlp text inspect 命令。如果您尚未打开命令行工具,请将其打开。

运行以下命令:

gcloud alpha dlp text inspect --project="PROJECT_ID" \
--content="My email address is joe@example.com." \
--include-quote --info-types="EMAIL_ADDRESS"

PROJECT_ID 替换为您的项目 ID

如果您尚未安装 gcloud Alpha Commands 组件,系统会询问是否要先安装它。如需继续,请按 Y

您会收到以下输出结果:

result:
findings:
- createTime: '2021-02-26T19:31:28.051Z'
  findingId: 2021-02-26T19:31:28.054696Z5687834655654299045
  infoType:
    name: EMAIL_ADDRESS
  likelihood: LIKELY
  location:
    byteRange:
      end: '35'
      start: '20'
    codepointRange:
      end: '35'
      start: '20'
  quote: joe@example.com

您刚刚向 DLP API 发送了第一个请求。

后续步骤