我目前正在尝试开发一个脚本,该脚本将从GCP上的安全指挥中心获取所有发现。
我在使用为我创建的服务帐户时遇到问题。
服务帐户是在项目X上创建的,但具有组织查看和列出调查结果的权限。
这就是我的想法(来自gcloudpython库):
from google.cloud import securitycenter
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('svc-scc.json')
# Create a client.
client = securitycenter.SecurityCenterClient(credentials=credentials)
# organization_id is the numeric ID of the organization. e.g.:
organization_id = "XXXXXXXXX"
org_name = "organizations/{org_id}".format(org_id=organization_id)
# The "sources/-" suffix lists findings across all sources. You
# also use a specific source_name instead.
all_sources = "{org_name}/sources/-".format(org_name=org_name)
finding_result_iterator = client.list_findings(all_sources)
for i, finding_result in enumerate(finding_result_iterator):
print("{}: name: {} resource: {}".format(i, finding_result.finding.name, finding_result.finding.resource_name))
svc-scc.json是json文件,其中包含从GCP上的IAM检索的凭据:
{
"type": "service_account",
"project_id": "Project X",
"private_key_id": "XXXXXXXXXXXXXXXXXXXXXXX",
"private_key": "-----BEGIN PRIVATE KEY-----
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
---END PRIVATE KEY-----\n",
"client_email": "svc-scc@xxxxxxxxxxxx.iam.gserviceaccount.com",
"client_id": "XXXXXXXXXXXXXX",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/svc-scc%40xxxxxxxxxxx.iam.gserviceaccount.com"
}
这是通过Terraform对此服务帐户的权限:
resource "google_organization_iam_member" "securitycenter-org-permissions" {
for_each = toset(["securitycenter.assetsViewer", "securitycenter.findingsViewer"])
org_id = var.org_id
role = "roles/${each.value}"
member = "serviceAccount:${module.service_accounts.service_account["svc-scc"].email}"
}
我得到了这个错误:
google.api_core.exceptions.PermissionDenied: 403 Security Command Center API has not been used in project X before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/securitycenter.googleapis.com/overview?project=X then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
为Project X启用securitycenter API不是我们的最佳选择。
代码中是否有方法将组织指定为API调用的默认项目?
如果没有,我是否需要更改服务帐户
谢谢你抽出时间
所以, 根据你的建议,我提出了以下建议: 我们已在安全项目中创建了服务帐户,但已在IAM项目(问题中的项目X)上设置了组织权限。 由于安全项目启用了API,所以当我想从组织中获得所有结果时,脚本工作正常
谢谢大家的帮助
当您使用客户端库时,这些库会在后台对您正在使用的产品进行API调用,并且这些调用是通过与服务帐户关联的项目进行的,因此在运行代码时,必须在使用的项目中激活API
如果您不想为项目X激活securitycenter API,但可能想使用已激活securitycenter API的项目Y,则需要向项目Y中的服务帐户(
svc-scc@xxxxxxxxxxxx.iam.gserviceaccount.com
)授予权限,然后在创建credentials
时,需要设置将要使用的项目相关问题 更多 >
编程相关推荐