我正在使用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"
}
答案 0 :(得分:0)
虽然可以通过云控制台创建服务帐户,但bigquery.*
角色似乎不适用于此处https://cloud.google.com/iam/docs/understanding-roles和此处https://cloud.google.com/bigquery/docs/access-control的角色列表中的服务帐户具有所需的权限(这表明它可能以某种方式)。
您可以使用api应用更多常规角色,例如Viewer,Editior或Owner(此处提及https://cloud.google.com/bigquery/docs/access-control#transitioning_from_primitive)。
答案 1 :(得分:0)
是的,事后看来,返回的错误消息显然是这样告诉我的,但对于将IAM策略绑定添加到服务帐户,这是正确的SDK调用似乎是合理的(对于相对谷歌云新手)。
我确实找到了一种方法,使用pull / update / push方法(尊重etag)来更新IAM策略的ENTIRE映射。但是,在一次测试中,我错误地发送了一个空的有效载荷:
[]
到IAM端点,并彻底清除整个项目中的每个ACL,甚至是所有者权限!幸运的是这次只有Dev环境,但每次对整个有效负载进行完整的POST对我来说都是冒险的。
最终,我最终找到了一个gcloud命令行选项(https://cloud.google.com/sdk/gcloud/reference/projects/add-iam-policy-binding),它只是附加了我想要的策略绑定,如:
gcloud项目add-iam-policy-binding
将此调用包装在我的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 BigQuery ACL附加到Java中新创建的服务帐户的目标,所以我很乐意继续前进。最后,我想在SDK中找到正确的方法,但是现在我觉得我找到了满足我们项目的答案。