[英]How do I use the Google Cloud APIs (directly or using the GCP PHP Client SDK) to List, Add, and Remove Users from a GCP project programmatically?
簡要總結:我有一個 PHP web 應用程序,它使用 Google Cloud PHP 客戶端 ZF20E3C5E5E54C0AB8D3675F 集成到 603C0AB8D3675F。 這工作沒問題。 我現在需要讓我的用戶直接訪問 GCP Dialogflow 儀表板,其中一部分意味着他們需要能夠從我的應用程序中向用戶添加/刪除 Dialogflow 權限。 (出於顯而易見的原因,我不想給他們完整的項目 IAM 權限和訪問權限)。
顯然,權限由單獨的“Google APIs 客戶端庫 for PHP”處理。
這似乎是用於列出現有項目用戶策略的API:https://cloud.google.com/resource-manager/reference/rest/v1/projects/listOrgPolicies
我相信這是添加/刪除用戶部分的 API,但它並不那么明顯: https://cloud.google.com/resource-manager/reference/rest/v1/projects/setIamPolicy
===
問題:
但是,當我運行上面引用的 listOrgPolicies API 中提供的示例代碼時,我收到此錯誤:
PHP Fatal error: Uncaught Error: Class 'Google_Service_CloudResourceManager_ListOrgPoliciesRequest' not found
我嘗試了composer require google/apiclient-services
和composer require google/apiclient-services:dev-master
。 並得到了同樣的結果。
===
我的問題是:
這些甚至是該任務的正確 API 嗎?
在哪里可以找到用於這些操作的 SDK 庫? (或者,如果有人對如何在沒有 SDK 的情況下使用 PHP cURL 直接向 API 執行這些任務有任何建議)
我希望所有操作都發生在 PHP 中,但是如果您有使用其他方法或語言或控制台命令行的有效解決方案,只要它可以由我的應用程序執行,我就可以使用它。
提前致謝!
在得到答案之前,請確保資源管理器 API已啟用。 這是 REST 方法和編程方法所必需的。
要修改給定項目中用戶的 IAM 綁定,需要調用兩個端點, 此處記錄了它們。 那些將是:
在第 1 步和第 2 步之間,需要修改第一個端點返回的 json,要么添加具有您選擇的角色的用戶,要么從中刪除用戶條目。 要查看添加/刪除用戶所需的 json 結構是什么,我建議檢查上面共享的鏈接以及檢查第一個命令返回的有效負載。
雖然直接調用 REST api 是您可以使用的一種選擇,但您也可以使用 gcloud CLI 來執行此類操作。 看起來用戶將被一一添加/刪除,命令行將是一個簡單的選擇,並且它需要較少的設置。 這兩個動作的命令是:
/ To add user role
gcloud projects add-iam-policy-binding PROJECT_ID --member=user:somebody@example.com --role=roles/viewer
/ To remove user role
gcloud projects remove-iam-policy-binding PROJECT_ID --member=user:somebody@example.com --role=roles/viewer
第三種選擇是以編程方式進行。 雖然我想提供一個 PHP 示例,但我沒有足夠的語言經驗,但下面是一個 Python 腳本,它允許添加/刪除角色。 它是從 快速入門中引導出來的,只是做了一些小的改動,從我運行的測試來看,它應該可以正常工作。
# TODO: Install required libraries
# pip3 install --upgrade google-api-python-client google-auth google-auth-httplib2
import os
from google.oauth2 import service_account
import googleapiclient.discovery
def get_policy(crm_service, project_id, version=3):
"""Gets IAM policy for a project."""
policy = (
crm_service.projects()
.getIamPolicy(
resource=project_id,
body={"options": {"requestedPolicyVersion": version}},
)
.execute()
)
print(policy)
return policy
def set_policy(crm_service, project_id, policy):
"""Sets IAM policy for a project."""
policy = (
crm_service.projects()
.setIamPolicy(resource=project_id, body={"policy": policy})
.execute()
)
return policy
def initialize_service():
"""
Initializes a Cloud Resource Manager service.
The Environemnt variable GOOGLE_APPLICATION_CREDENTIALS must point to the service account key.json file
"""
credentials = service_account.Credentials.from_service_account_file(
filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"],
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
crm_service = googleapiclient.discovery.build(
"cloudresourcemanager", "v1", credentials=credentials
)
return crm_service
def modify_policy_add_role(crm_service, project_id, role, member):
"""Adds a new role binding to a policy."""
policy = get_policy(crm_service, project_id)
binding = None
for b in policy["bindings"]:
if b["role"] == role:
binding = b
break
if binding is not None and member not in binding["members"]:
binding["members"].append(member)
else:
binding = {"role": role, "members": [member]}
policy["bindings"].append(binding)
policy = set_policy(crm_service, project_id, policy)
def modify_policy_remove_member(crm_service, project_id, role, member):
"""Removes a member from a role binding."""
policy = get_policy(crm_service, project_id)
# The try-except below handles the case where the role isn't in the IAM policy
try:
binding = next(b for b in policy["bindings"] if b["role"] == role)
except StopIteration:
print("The role is not included in the IAM policy. Can't remove user")
raise KeyError
if "members" in binding and member in binding["members"]:
binding["members"].remove(member)
set_policy(crm_service, project_id, policy)
if __name__ == '__main__':
# TODO: Replace with your project ID
project_id = "projectID"
# TODO: Replace with the ID of your member in the form 'user:member@example.com'.
member = "user:somebody@example.com"
# TODO: Replace the role with the role you want to grant/remove
role = "roles/logging.logWriter"
# Initializes the client.
crm_service = initialize_service()
# Call modify_policy_add_role or modify_policy_remove_member as required
modify_policy_add_role(crm_service, project_id, role, member)
modify_policy_remove_member(crm_service, project_id, role, member)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.