控制对 Cloud Healthcare API 资源的访问权限

本页面介绍如何使用身份和访问权限管理 (IAM) 权限控制对 Cloud Healthcare API 数据集和数据存储区的权限。您可以通过 IAM 控制谁有权访问您的数据集和数据存储区。如需详细了解适用于 Cloud Healthcare API 的 IAM,请参阅访问权限控制

IAM 政策概览

您可以通过 IAM 政策管理对资源的访问权限。政策包含数组 bindings。此数组包含一组绑定,这些绑定是主账号(如用户账号或服务账号)与角色之间的关联。政策使用 JSON 或 YAML 表示。

以下示例政策展示了已被授予 roles/healthcare.datasetAdmin 角色的 user-1@example.com,以及已被授予 roles/healthcare.datasetViewer 角色的 user-2@example.comservice-account-13@appspot.gserviceaccount.com

{
  "etag":"bytes",
  "bindings": [
    {
      "role":"roles/healthcare.datasetAdmin",
      "members": [
        "user:user-1@example.com"
      ]
    },
    {
      "role":"roles/healthcare.datasetViewer",
      "members": [
        "serviceAccount:service-account-13@appspot.gserviceaccount.com",
        "user:user-2@example.com"
      ]
    }
  ]
}

如需更新资源的政策,请使用读取-修改-写入模式。没有单独的方法用于创建、修改和撤消用户访问权限。

如需更新政策,请完成以下步骤:

  1. 通过调用资源的 getIamPolicy() 方法读取当前政策。例如,要读取某个数据集的当前政策,请调用 projects.locations.datasets.getIamPolicy
  2. 使用文本编辑器或以编程方式修改返回的政策,以添加或移除任何适用的主账号以及为其授予的角色。
  3. 通过调用资源的 setIamPolicy() 方法写入更新后的政策。例如,要写入某个数据集更新后的政策,请调用 projects.locations.datasets.setIamPolicy

以下各部分介绍了如何获取、修改和设置许可存储区的政策。这些部分使用以下示例政策作为初始政策:

{
  "etag":"bytes",
  "bindings": [
    {
      "role":"roles/healthcare.consentStoreAdmin",
      "members": [
        "user:user-1@example.com"
      ]
    },
    {
      "role":"roles/healthcare.consentReader",
      "members": [
        "serviceAccount:service-account-13@appspot.gserviceaccount.com",
        "user:user-2@example.com"
      ]
    }
  ]
}

获取政策

以下示例展示了如何读取许可存储区级层的 IAM 政策。如需了解详情,请参阅 projects.locations.datasets.consentStores.getIamPolicy

如需查看许可存储区的 IAM 政策,请执行以下操作:

  1. 在 Google Cloud 控制台中,前往“数据集”页面。

    转到“数据集”

  2. 点击包含许可存储区的数据集的 ID,然后选择要为其获取政策的许可存储区。
  3. 点击显示信息面板
  4. 要查看分配给某个角色的主账号,请展开该角色。

如需查看许可存储区的 IAM 政策,请运行 gcloud healthcare consent-stores get-iam-policy 命令。指定许可存储区名称、数据集名称和位置。

gcloud healthcare consent-stores get-iam-policy CONSENT_STORE_ID \
    --dataset=DATASET_ID \
    --location=LOCATION

如果请求成功,则会显示绑定。

bindings:
- members:
  - user:user-1@example.com
  role: roles/healthcare.consentStoreAdmin
  - serviceAccount:service-account-13@appspot.gserviceaccount.com
  - user:user-2@example.com
  role: roles/healthcare.consentReader
etag: bytes
version: VERSION_NUMBER
const google = require('@googleapis/healthcare');
const healthcare = google.healthcare({
  version: 'v1',
  auth: new google.auth.GoogleAuth({
    scopes: ['https://www.googleapis.com/auth/cloud-platform'],
  }),
});

const getConsentStoreIamPolicy = async () => {
  // TODO(developer): uncomment these lines before running the sample
  // const cloudRegion = 'us-central1';
  // const projectId = 'adjective-noun-123';
  // const datasetId = 'my-dataset';
  // const consentStoreId = 'my-consent-store';
  const resource_ = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}/consentStores/${consentStoreId}`;
  const request = {resource_};

  const consentStore =
    await healthcare.projects.locations.datasets.consentStores.getIamPolicy(
      request
    );
  console.log(
    'Got consent store IAM policy:',
    JSON.stringify(consentStore.data, null, 2)
  );
};

getConsentStoreIamPolicy();
def get_consent_store_iam_policy(
    project_id: str, location: str, dataset_id: str, consent_store_id: str
):
    """Gets the IAM policy for the specified consent store.
    See https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/healthcare/api-client/v1/consent
    before running the sample."""
    # Imports the Google API Discovery Service.
    from googleapiclient import discovery

    api_version = "v1"
    service_name = "healthcare"
    # Returns an authorized API client by discovering the Healthcare API
    # and using GOOGLE_APPLICATION_CREDENTIALS environment variable.
    client = discovery.build(service_name, api_version)

    # TODO(developer): Uncomment these lines and replace with your values.
    # project_id = 'my-project'  # replace with your GCP project ID
    # location = 'us-central1'  # replace with the parent dataset's location
    # dataset_id = 'my-dataset'  # replace with the consent store's parent dataset ID
    # consent_store_id = 'my-consent-store'  # replace with the consent store's ID
    consent_store_parent = "projects/{}/locations/{}/datasets/{}".format(
        project_id, location, dataset_id
    )
    consent_store_name = "{}/consentStores/{}".format(
        consent_store_parent, consent_store_id
    )

    request = (
        client.projects()
        .locations()
        .datasets()
        .consentStores()
        .getIamPolicy(resource=consent_store_name)
    )
    response = request.execute()

    print("etag: {}".format(response.get("name")))
    return response

如需读取许可存储区的 IAM 政策,请发出 GET 请求并指定数据集的名称、许可存储区的名称和访问令牌。

以下示例展示了使用 curlGET 请求。

curl -X GET \
     -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
     "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID/consentStores/CONSENT_STORE_ID:getIamPolicy"

响应如下:

{
  "etag":"bytes",
  "bindings": [
    {
      "role":"roles/healthcare.consentStoreAdmin",
      "members": [
        "user:user-1@example.com"
      ]
    },
    {
      "role":"roles/healthcare.consentReader",
      "members": [
        "serviceAccount:service-account-13@appspot.gserviceaccount.com",
        "user:user-2@example.com"
      ]
    }
  ]
}

如需读取许可存储区的 IAM 政策,请发出 GET 请求并指定数据集的名称、许可存储区的名称和访问令牌。

以下示例展示了使用 Windows PowerShell 的 GET 请求:

$cred = gcloud auth application-default print-access-token
$headers = @{ Authorization = "Bearer $cred" }

Invoke-WebRequest `
  -Method Get `
  -Headers $headers `
  -Uri "https://healthcare.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/datasets/DATASET_ID/consentStores/CONSENT_STORE_ID:getIamPolicy" | Select-Object -Expand Content

响应如下:

{
  "etag":"bytes",
  "bindings": [
    {
      "role":"roles/healthcare.consentStoreAdmin",
      "members": [
        "user:user-1@example.com"
      ]
    },
    {
      "role":"roles/healthcare.consentReader",
      "members": [
        "serviceAccount:service-account-13@appspot.gserviceaccount.com",
        "user:user-2@example.com"
      ]
    }
  ]
}

修改政策

以下示例向新用户授予 roles/healthcare.consentReader 角色。如需了解详情,请参阅 projects.locations.datasets.consentStores.setIamPolicy

设置政策

如需设置许可存储区级层 IAM 政策,请完成以下步骤:

  1. 在 Google Cloud 控制台中,前往“数据集”页面。

    转到“数据集”

  2. 点击包含许可存储区的数据集的 ID,然后选择要为其设置政策的许可存储区。
  3. 点击显示信息面板
  4. 点击添加主账号
  5. 新主账号字段中,输入需要访问许可存储区的一个或多个身份。
  6. 选择角色列表中的 Cloud Healthcare 下,选择您要授予的权限。例如 Healthcare Consent Store Viewer。
  7. 点击保存

通过以编程方式或使用文本编辑器修改检索到的政策,向用户授予角色或撤消用户的角色。etag 值会随政策更改而变化,因此您必须指定当前值。