使用命令行检查敏感文本
本页面介绍如何使用命令行界面在 Cloud Data Loss Prevention API 执行基本任务。具体来说,本快速入门介绍了如何将短字符串发送到 DLP API 进行检查。
开始前须知
- 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the DLP API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Enable the DLP API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.
权限
检查内容需要 parent
中指定的项目的 serviceusage.services.use
权限。roles/editor
、roles/owner
和 roles.dlp.user
角色包含所需的权限,您也可以定义自己的自定义角色。
如需在项目级层为您的用户授予 dlp.user
角色,请执行以下操作:
控制台
在 Google Cloud Console 中打开 IAM 页面。
如果尚未选择项目,请点击项目选择器,然后选择您的项目。
在 IAM 页面上,请按照以下步骤操作:
- 要添加新用户,请点击 添加。
- 如需为现有用户添加
dlp.user
角色,请点击该用户对应的 修改主帐号,然后点击修改权限窗格中的添加其他角色。
在添加主帐号窗格中,请按照以下步骤操作:
- 在新主帐号字段中,输入要添加的用户的电子邮件地址,例如
test@example.com
。 - 对于角色,点击选择角色,然后选择 Cloud DLP &DLP 用户。
- 在新主帐号字段中,输入要添加的用户的电子邮件地址,例如
点击添加。
如需了解详情,请参阅授予 IAM 角色。
gcloud
要将单个绑定添加到项目的 IAM 政策,请输入以下命令:
gcloud projects add-iam-policy-binding PROJECT_ID --member serviceAccount:SERVICE_ID --role roles/dlp.user
替换以下内容:
PROJECT_ID
:项目 ID。SERVICE_ID
:要使用的服务帐号。
将更新后的政策写入控制台窗口:
bindings: - members: - group: EMAIL_ADDRESS role: roles/dlp.user
将
EMAIL_ADDRESS
替换为要添加的用户的电子邮件地址。
设置 Cloud DLP CLI 应用
Node.js
克隆或下载 Node.js DLP 客户端库的 zip 文件,然后展开所下载的文件。
打开命令行工具并导航到展开目录中的
samples
目录。在
samples
目录中运行npm install
以安装应用依赖项。如果您尚未创建
GCLOUD_PROJECT
环境变量,请创建该变量,并将其设置为与 Cloud DLP 搭配使用的 Google Cloud 项目的项目 ID:
gcloud alpha dlp
-
此过程还需要 gcloud Alpha 命令组件。您可以立即安装,也可以在系统提示后进行安装。
激活服务帐号:
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 发送了第一个请求。
后续步骤
- 浏览方法指南,开始检查文本和图片,并隐去文本中的敏感数据。
- 了解概念,从而更好地了解检查、隐去、infoType 和可能性。
- 请参阅 Service API 参考和 Node.js 客户端库 API 参考。