在配置出站流量代理之前测试组织限制标头

本页面介绍 Google Cloud 管理员如何在不配置出站流量代理的情况下测试组织权限限制。

如需创建和测试组织权限限制标头,请执行以下操作:

  1. 如需获取组织的 Google Cloud 组织 ID,请使用 gcloud organizations list 命令

        gcloud organizations list
    

    以下是示例输出:

        DISPLAY_NAME: Organization A
        ID: 123456789
        DIRECTORY_CUSTOMER_ID: a1b2c3d4
    
  2. 获取组织 ID 后,请按以下格式编写标头值的 JSON 表示法:

     {
     "resources": ["organizations/123456789"],
      "options": "strict"
     }
    
  3. 按照 RFC 4648 第 5 节规范对请求标头的值进行编码。

    例如,如果标头值的 JSON 表示法存储在 authorized_orgs.json 文件中,则下面是通过 basenc 进行编码:

     $ OR_HEADER=`cat authorized_orgs.json | basenc --base64url -w0`
     $ echo $OR_HEADER
    ewogInJlc291cmNlcyI6IFsib3JnYW5pemF0aW9ucy8xMjM0NTY3ODkiXQogIm9wdGlvbnMiOiAic3RyaWN0Igp9Cgo=
    
  4. 如需测试标头配置是否允许访问已获授权的 Google Cloud 组织内的资源,请创建针对已获授权的 Google Cloud 组织内的资源的 curl 请求。以下是 curl 请求示例:

    # Get auth token
    $ TOKEN=$(gcloud auth print-access-token)
    
    # Make a request that includes the organization restriction header; this call makes a request to the logging API for a project within the same organization listed in the header
    $ curl -H "X-Goog-Allowed-Resources: ${OR_HEADER}" -X POST -d '{"projectIds":
    ["my-project-123"]}' -H 'Content-Type: application/json' -H "Authorization: Bearer ${TOKEN}" "https://logging.googleapis.com/v2/entries:list"
    # A successful request returns log entries in this project
    
  5. 如需测试标头配置是否拒绝访问已获授权的 Google Cloud 组织外部的资源,请创建针对已获授权的 Google Cloud 组织外部的资源的 curl 请求。以下是 curl 请求示例:

    # Get auth token
    $ TOKEN=$(gcloud auth print-access-token)
    
    # Make a request that includes the organization restriction header; this call makes a request to the logging API for a project not within the same organization listed in the header
    $ curl -H "X-Goog-Allowed-Resources: ${OR_HEADER}" -X POST -d '{"projectIds": ["other-project-123"]}' -H 'Content-Type: application/json' -H "Authorization: Bearer ${TOKEN}" "https://logging.googleapis.com/v2/entries:list"
    {
      "error": {
     "code": 403,
        "message": "Access denied by organization restriction. Contact your administrator for additional information.",
        "status": "PERMISSION_DENIED",
        "details": [
          {
            "@type": "type.googleapis.com/google.rpc.ErrorInfo",
            "reason": "ORG_RESTRICTION_VIOLATION",
            "domain": "googleapis.com",
            "metadata": {
              "service": "logging.googleapis.com",
              "consumer": "other-project-123"
            }
          }
        ]
      }
    }
    
    

    访问遭拒的错误消息表示对资源的请求被拒绝。

后续步骤