【问题标题】:gcloud - check member has a role on a resourcegcloud - 检查成员在资源上的角色
【发布时间】:2026-01-28 15:15:01
【问题描述】:

我正在尝试使用 gitlab ci 中的 for 循环删除对谷歌云功能的某些权限。

for i in ${!CFS[@]}; do
        gcloud functions remove-iam-policy-binding ${API_VERSION}-${CFS[$i]} --member=${MEMBER} --role=${ROLE}
done

问题是,如果资源没有给定的角色,对于该成员,我会收到错误消息。

ERROR: (gcloud.functions.remove-iam-policy-binding) Policy binding with the specified member and role not found!.

我想通过在执行remove-iam-policy-binding gcloud 命令之前检查成员是否在资源上具有给定角色来避免这种情况。有没有办法在删除之前检查给定资源上的成员是否存在权限?

【问题讨论】:

    标签: google-cloud-platform google-cloud-functions gitlab-ci gcloud google-iam


    【解决方案1】:

    我能够使用gcloud functions get-iam-policy 并过滤我想要的权限和角色来实现这一点。如果为给定用户设置了角色,那么我将其删除。

        for mem in $(gcloud functions get-iam-policy ${CFS[$i]} --flatten="bindings[].members" --filter="bindings.role:roles/cloudfunctions.invoker" --format="value(bindings.members)") 
        do
            echo $mem
            gcloud functions remove-iam-policy-binding ${CFS[$i]} --member=$mem --role="roles/cloudfunctions.invoker"
        done
    

    【讨论】: