我正在使用Google Java SDK创建和管理服务帐户。通常,它效果很好。但是,我正在尝试将" bigquery.jobuser"角色添加到新创建的ServiceAccount中,但是API一直告诉我,该角色对我的资源无效。
我可以在此领域找到Java SDK的大量文档,因此我显然做错了什么,也许是指定我的资源。
希望有人能看到这个,看看是否有什么跳出来的?我已经去了几天了,我觉得我"我很关闭....
// assume successful service account creation:
serviceAccount = create.execute();
// now set the IAM policy for bigquery.user for this service account.
String[] serviceAccountsArray = new String[] {"serviceAccount:" + serviceAccount.getEmail()};
String targetRole = "roles/bigquery.jobUser";
LinkedList<Binding> bindings = new LinkedList<>();
Binding targetBinding = new Binding();
targetBinding.setRole(targetRole);
bindings.add(targetBinding);
targetBinding.setMembers(Arrays.asList(serviceAccountsArray));
Policy policy = new Policy();
policy.setBindings(bindings);
SetIamPolicyRequest setIamPolicyRequest = new SetIamPolicyRequest();
setIamPolicyRequest.setPolicy(policy);
Iam.Projects.ServiceAccounts.SetIamPolicy setIamPolicy = iam.
projects()
.serviceAccounts()
.setIamPolicy("projects/" + bigQueryProjectId + "/serviceAccounts/" + serviceAccount.getEmail(), setIamPolicyRequest);
Policy newPolicy = setIamPolicy.execute();
不幸的是,我总是得到以下例外:
com.google.api.client.googleapis.json.GoogleJsonResponseException:
400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Role roles/bigquery.jobUser is not supported for this resource.",
"reason" : "badRequest"
} ],
"message" : "Role roles/bigquery.jobUser is not supported for this resource.",
"status" : "INVALID_ARGUMENT"
}
bigquery.*
角色似乎不适用于服务帐户,根据此处的角色列表https://cloud.google.com/iam/docs/docs/understanding-roles和https https://cloud.google.com/bigquery/docs/access-control,尽管可以通过云控制台创建服务帐户,并具有所需的权限(这表明可能有可能)。
您可以使用API使用更通用的角色,例如观众,Editior或所有者(此处提到https://cloud.google.com/bigquery/docs/access-control#transition_from_primimimistive)。
我确实使用SDK找到了使用拉/更新/推动方法(尊重ETAG)更新IAM策略的整个地图的方法。但是,在一次测试中,我错误地发送了一个空的有效载荷:
[]
到IAM端点,完全吹走了我整个项目中的每个ACL,甚至所有者特权!幸运的是,这次只有开发环境,但是每次有效载荷的完整帖子对我来说似乎都有风险。
最终,我最终找到了一个gcloud命令行选项(https://cloud.google.com/sdk/gcloud/reference/project/project/project/projects/add-iam-policy-binding),它像我一样附加策略绑定想要,例如:
gcloud项目add-am-policy-bunting
并将此调用包装在我的Java代码(伪代码)
的ProcessBuilder.start()中 List<String> commands = new ArrayList<>();
commands.add(gcloudCommandLineLocation);
commands.add("projects");
commands.add("add-iam-policy-binding");
commands.add(bigQueryProjectId);
commands.add("--member");
commands.add("serviceAccount:" + serviceAccount.getEmail());
commands.add("--role");
commands.add("roles/bigquery.jobUser");
// build the process
ProcessBuilder pb = new ProcessBuilder(commands);
// cross the streams!
pb.redirectErrorStream(true);
// start the process
Process process = pb.start();
不是一个完美的解决方案,而是达到了我的目标,即在Java的新创建的服务帐户上附加了ACL,所以我很高兴继续前进。最终,我想在SDK中找到适当的方法,但是现在我觉得我找到了一个满足我们项目的答案。