Menggunakan kebijakan otorisasi untuk mendelegasikan otorisasi ke IAP dan IAM

Halaman ini menjelaskan cara menggunakan kebijakan otorisasi untuk menetapkan Identity-Aware Proxy (IAP) sebagai mesin otorisasi kustom dalam kebijakan otorisasi untuk Load Balancer Aplikasi.

Untuk mendelegasikan otorisasi ke IAP dan Identity and Access Management (IAM), Anda mengizinkan traffic ke aturan penerusan berdasarkan identitas IAM atau pengguna akhir untuk load balancer terkelola. Tindakan ini memungkinkan Anda menerapkan kontrol akses berbasis IAP untuk layanan backend Anda.

Sebelum memulai

Membuat kebijakan otorisasi dan mengaktifkan IAP

Untuk membuat kebijakan otorisasi, buat file kebijakan yang menentukan target dan aturan, lalu aktifkan IAP pada aturan penerusan. Anda dapat memilih untuk menerapkan IAP hanya ke permintaan tertentu dengan menentukan kondisi di bagian httpRules file kebijakan Anda. Jika bagian httpRules dihilangkan, IAP akan diterapkan pada semua permintaan.

gcloud

  1. 运行以下命令以准备 policy.yaml 文件。该政策允许 IP 地址范围为 10.0.0.0/24 的客户端在转发规则上启用 IAP。

    $ cat << EOF > policy.yaml
    action: CUSTOM
    description: authz policy with Cloud IAP
    name: AUTHZ_POLICY_NAME
    httpRules:
      - from:
          sources:
          - ipBlocks:
            - prefix: "10.0.0.0"
              length: 24
    customProvider:
      cloudIap: {}
    target:
      loadBalancingScheme: EXTERNAL_MANAGED
      resources:
      - https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_ID
    EOF
    
  2. 运行以下命令,在转发规则上启用 IAP。

    gcloud network-security authz-policies import AUTHZ_POLICY_NAME \
    --source=policy.yaml \
    --location=LOCATION \
    --project=PROJECT_ID
    

    替换以下内容:

    • PROJECT_ID: Google Cloud 项目 ID。

    • LOCATION:资源所在的区域。

    • FORWARDING_RULE_ID:转发规则资源的 ID。

    • AUTHZ_POLICY_NAME:授权政策的名称。

API

  1. 运行以下命令,准备 policy.json 文件。

    cat << EOF > policy.json
    {
     "name": "AUTHZ_POLICY_NAME",
     "target": {
         "loadBalancingScheme": "INTERNAL_MANAGED",
         "resources": [
           "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_ID"
         ],
     },
     "action": "CUSTOM",
     "httpRules": [
         {
           "from": {
             "sources": {
               "ipBlocks": [
                 {
                   "prefix": "10.0.0.0",
                   "length": 24
                 }
               ]
             }
           }
         }
     ],
     "customProvider": {
         "cloudIap": {}
     }
    }
    EOF
    
  2. 运行以下命令,在转发规则上启用 IAP。

    curl -X PATCH \\
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \\
    -H "Accept: application/json" \\
    -H "Content-Type: application/json" \\
    -d @policy.json \\
    "https://networksecurity.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/authzPolicies"
    

    替换以下内容:

    • PROJECT_ID: Google Cloud 项目 ID。

    • LOCATION:资源所在的区域。

    • FORWARDING_RULE_ID:转发规则资源的 ID。

    • AUTHZ_POLICY_NAME:授权政策的名称。

Menerapkan izin ke resource

Anda dapat menggunakan IAP API untuk menerapkan izin IAM ke setiap resource dalam project yang diamankan IAP. Izin IAM yang diberikan pada tingkat tertentu berlaku untuk semua tingkat di bawahnya. Misalnya, izin yang diberikan di level project berlaku untuk semua resource Google Cloud dalam project.

Berikut adalah beberapa contoh perintah untuk mengonfigurasi izin. Untuk mengetahui informasi selengkapnya, lihat gcloud iap web set-iam-policy.

gcloud

Dalam contoh perintah, ganti POLICY_FILE dengan jalur ke file berformat YAML yang berisi kebijakan yang valid. Berikut adalah contoh file YAML:

bindings:
  members: user:example@example.com
  role: roles/iap.httpsResourceAccessor

Memperbarui izin untuk siapa saja yang dapat mengakses resource aturan penerusan

gcloud iap web set-iam-policy POLICY_FILE [--organization=ORGANIZATION \
--folder=FOLDER --project=PROJECT_ID \
--resource-type=RESOURCE_TYPE --service=SERVICE]

Mengonfigurasi izin di tingkat organisasi

gcloud iap web set-iam-policy POLICY_FILE --organization=ORGANIZATION

Mengonfigurasi izin di level folder

gcloud iap web set-iam-policy POLICY_FILE --folder=FOLDER

Mengonfigurasi izin di level project

gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID --resource-type=iap_web

Mengonfigurasi izin untuk semua aturan penerusan global dalam sebuah project

gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID --resource-type=forwarding_rule

Mengonfigurasi izin untuk semua aturan penerusan dalam project di suatu region

gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID \
--resource-type=forwarding_rule --region=REGION

Mengonfigurasi izin untuk aturan penerusan global tertentu dalam project

gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID \
--resource-type=forwarding_rule --service=SERVICE

Mengonfigurasi izin untuk aturan penerusan tertentu dalam project di suatu region

gcloud iap web set-iam-policy POLICY_FILE --project=PROJECT_ID \
--resource-type=forwarding_rule --service=SERVICE --region=REGION

Ganti kode berikut:

  • POLICY_FILE: Jalur ke file YAML yang berisi kebijakan yang valid.

  • ORGANIZATION: ID organisasi Anda.

  • FOLDER: Folder yang berisi aplikasi Anda.

  • PROJECT_ID: Project ID Google Cloud .

  • RESOURCE_TYPE: Jenis resource IAP. Jenis resource harus berupa app-engine, iap_web, compute, organization, atau folder.

  • SERVICE: ID atau nama aturan penerusan.

  • REGION: Region tempat resource berada.

API

Mengonfigurasi izin di tingkat organisasi

organization/ORGANIZATION

Mengonfigurasi izin di level folder

folders/FOLDER

Mengonfigurasi izin di level project

projects/PROJECT_ID

Mengonfigurasi izin untuk semua aturan penerusan global dalam sebuah project

projects/PROJECT_ID/forwarding_rule

Mengonfigurasi izin untuk semua aturan penerusan dalam project di suatu region

projects/PROJECT_ID/iap_web/forwarding_rule-REGION

Mengonfigurasi izin untuk aturan penerusan global tertentu dalam project

projects/PROJECT_ID/iap_web/forwarding_rule/services/SERVICE

Mengonfigurasi izin untuk aturan penerusan tertentu dalam project di suatu region

projects/PROJECT_ID/iap_web/forwarding_rule-REGION/services/SERVICE

Menetapkan kebijakan otorisasi

curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d @policy.json \
"https://iap.googleapis.com/v1/RESOURCE_NAME:setIamPolicy"

Untuk policy.json, Anda dapat membuat file JSON dengan menjalankan perintah berikut. Perbarui nilai sesuai kebutuhan.

cat << EOF > policy.json
{
  "bindings": {
     "members": "user:example@example.com",
     "role": "roles/iap.httpsResourceAccessor",
   }
}
EOF

Ganti kode berikut:

  • ORGANIZATION: ID organisasi Anda.

  • FOLDER: Folder yang berisi aplikasi Anda.

  • PROJECT_ID: Project ID Google Cloud .

  • REGION: Region tempat resource berada.

  • SERVICE: ID atau nama aturan penerusan.