0

我正在尝试使用 API 自动执行 DLP 扫描。唯一阻碍我完成这个项目的是身份验证。尽管我自己的帐户拥有对所有项目的组织所有者访问权限,但在每个项目中创建和使用具有 BigQuery、存储和 DLP 管理员权限的服务帐户似乎是避免权限被拒绝错误的唯一方法。这是 IAM 问题还是 DLP API 的某些要求和令牌的使用?为什么我的令牌不起作用?我们有很多项目,将来可能会有更多项目,并且希望避免必须在每个项目中创建服务帐户的繁琐和乏味的问题,尤其是必须一次又一次地这样做以保持脚本正常工作。自动化应该是更少的工作而不是更多的工作。

我在下面的项目中使用自己的帐户令牌得到的错误是肯定启用了 DLP API。

"error": { "code": 403, "message": "Cloud Data Loss Prevention (DLP) API has not been used in project ###### before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/dlp.googleapis.com/overview?project=###### then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.", "status": "PERMISSION_DENIED", "details": [ { "@type": "type.googleapis.com/google.rpc.Help", "links": [ { "description": "Google developers console API activation", "url": "https://console.developers.google.com/apis/api/dlp.googleapis.com/overview?project=######"

4

4 回答 4

1

查看文档Authenticating to the Cloud DLP API

您必须对 Cloud DLP API 进行身份验证才能使用它。Cloud DLP API 可以处理 API 密钥和身份验证。这两种方法的主要区别是:

  • API 密钥标识调用 API 的调用项目(应用程序或站点)。
  • 身份验证令牌标识正在使用该项目的用户(个人)。

要使用服务帐号向 Cloud DLP API 进行身份验证,请执行以下操作

按照说明创建服务帐户。选择 JSON 作为您的密钥类型,并授予用户 DLP 用户角色 ( roles/dlp.user)。

一般来说,您应该遵循以下步骤:

  1. 启用计费
  2. 启用 DLP API
  3. 创建一个服务帐户,授予服务帐户角色roles/dlp.user并下载key.json文件:

    $ gcloud iam service-accounts create test-dlp --description "test-cloud" --display-name "test-dlp"
    $ gcloud projects add-iam-policy-binding class-run --member serviceAccount:test-dlp@class-run.iam.gserviceaccount.com --role roles/dlp.user
    $ gcloud iam service-accounts keys create key.json --iam-account test-dlp@class-run.iam.gserviceaccount.com
    
  4. 设置环境变量:

    export GOOGLE_APPLICATION_CREDENTIALS=[PATH_TO_key.json_FILE]
    
  5. 运行你的脚本

此外,请查看快速入门:使用命令行工具部分权限

检查内容需要serviceusage.services.use在父项中指定的项目的权限。、和角色包含所需的权限roles/editor, 或者您可以定义自己的自定义角色。roles/ownerroles.dlp.user

如果您仍然有问题,请尝试按照以下步骤对 bu 进行故障排除:

  1. 使用命令检查您的活动帐户gcloud auth list
  2. 检查是否启用了 DLP APIgcloud services list --enabled | grep DLP
  3. 使用命令激活您的 DLP 服务帐户gcloud auth activate-service-account并再次运行您的脚本

并使用命令和输出更新您的问题。另外,请说明您在脚本中使用 DLP 服务帐户的方式。

于 2020-03-03T17:38:16.507 回答
1

是的,您可以在 api 中使用您自己的用户帐户(所有者)。

  1. gcloud auth login 

  2. gcloud auth list 
    #ACTIVE  ACCOUNT with owner role
    *       user@cloud.com .

  3. gcloud auth print-access-token
    #xxxxxxxxxxxxxx

  4. curl -s -H 'Content-Type: application/json'   -H 'Authorization: Bearer xxxxxxxxxxx'   'https://dlp.googleapis.com/v2/infoTypes'

  #OR

  5. ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
     curl -s -H 'Content-Type: application/json'   -H "Authorization: Bearer $ACCESS_TOKEN"   'https://dlp.googleapis.com/v2/infoTypes'
   #you need Double Quotes and not Single Quotes for -H flag

3.1.2.2 单引号

将字符括在单引号 (') 中会保留引号内每个字符的文字值。单引号之间不能出现单引号,即使前面有反斜杠。

3.1.2.3 双引号

将字符括在双引号 (") 中会保留引号内所有字符的字面值,除了 $,, \, and, when history expansion is enabled, !. The characters $ and 在双引号内保留其特殊含义(请参阅 Shell 扩展)。反斜杠仅在后跟以下之一时才保留其特殊含义以下字符:$、`、"、\ 或换行符。在双引号内,删除后面跟有这些字符之一的反斜杠。没有特殊含义的字符前面的反斜杠保持不变。双引号可以用反斜杠在双引号中引用。如果启用,将执行历史扩展,除非 ! 出现在双引号中的使用反斜杠进行转义。! 前面的反斜杠 没有被删除。

特殊参数 * 和 @ 在双引号中时具有特殊含义(请参阅 Shell 参数扩展)。

输出:


{
  "infoTypes": [
    {
      "name": "AMERICAN_BANKERS_CUSIP_ID",
      "displayName": "American Bankers CUSIP identifier",
      "supportedBy": [
        "INSPECT"
      ],
      "description": "An American Bankers' Committee on Uniform Security Identification Procedures (CUSIP) number is a 9-character alphanumeric code that identifies a North American financial security."
    },
    {
      "name": "AUSTRALIA_DRIVERS_LICENSE_NUMBER",
      "displayName": "Australia driver's license number",
      "supportedBy": [
        "INSPECT"
      ],
      "description": "An Australian driver's license number."
    },
    {
      "name": "AUSTRALIA_MEDICARE_NUMBER",
      "displayName": "Australia medicare number",
      "supportedBy": [
        "INSPECT"

..................
于 2020-03-03T19:01:11.340 回答
1

One thing to note that's a little different with DLP. There are actually two service accounts involved in each request. The one you use to call the API, but the service itself uses the dlp service account to turn around and call other services to read from storage.

When you enable dlp you'll see this account automatically created. It looks like this ... service-[PROJECT_NUMBER]@dlp-api.iam.gserviceaccount.com and is discussed here https://cloud.google.com/dlp/docs/iam-permissions#service_account

A common setup I've seen is a security team will enable DLP in a single project to scan their entire organization. They will do this by granting the service-[PROJECT_NUMBER]@dlp-api.iam.gserviceaccount.com in the project they are running from permission to access all other projects in the org. This will let it read GCS/BQ across the company.

于 2020-03-04T17:54:45.620 回答
0

如果您发现这篇文章是因为您只想使用 API 密钥通过 Java 客户端调用 DLP API,请参阅我如何使其工作的要点(在 Scala 中)。基本上必须自己传递标头并明确表示没有其他身份验证机制

https://gist.github.com/kylejmcintyre/b7ff56cf08addb87ebcf53defa18461f

在我的情况下,我将所有要检查的数据传递给 API,因此除了调用检查 API 之外,我不关心任何其他身份验证。

于 2021-03-10T00:42:55.653 回答