我们可以用gcloud 命令
gcloud iam service-accounts list
gcloud iam service-accounts list
DISPLAY NAME EMAIL DISABLED
terraform [email protected] False
vm-common [email protected] False
terraform2 [email protected] False
Compute Engine default service account 912156613264[email protected] False
gcloud iam service-accounts create SA_NAME
–description=“DESCRIPTION”
–display-name=“DISPLAY_NAME”
gcloud iam service-accounts create pubsub-publisher-a --description="be used to publish message to topicA" --display-name="pubsub publisher a"
Created service account [pubsub-publisher-a].
gcloud iam service-accounts list
DISPLAY NAME EMAIL DISABLED
terraform [email protected] False
vm-common [email protected] False
pubsub publisher a [email protected] False
terraform2 [email protected] False
Compute Engine default service account 912156613264[email protected] False
新的service account 被创建出来了。
当然, 在gcp console 也可以利用UI 创建它.
gcloud pubsub topics add-iam-policy-binding projects/jason-hsbc/topics/TopicA --member=serviceAccount:[email protected] --role=roles/pubsub.publisher
上面语句是1个例子, 分配某个pubsub topic(TopicA) 的publish 权限。
gcloud pubsub topics add-iam-policy-binding projects/jason-hsbc/topics/TopicA --member=serviceAccount:[email protected] --role=roles/pubsub.publisher
Updated IAM policy for topic [TopicA].
bindings:
- members:
- serviceAccount:[email protected]
role: roles/pubsub.publisher
etag: BwYGwRVxij4=
version: 1
下面的例子是把storage admin 分配给这个service account
gcloud projects add-iam-policy-binding jason-hsbc --member=serviceAccount:[email protected] --role=roles/storage.admin
gcloud projects add-iam-policy-binding jason-hsbc --member=serviceAccount:[email protected] --role=roles/storage.admin
Updated IAM policy for project [jason-hsbc].
注意上面两个例子是有区别的
例如 分配某个pubsub subscription 的读权限
某个 BQ table的权限等
例如上面的storage admin, pubsub admin等.
其实 还有第三种
包括seviceAccountUser等。这个很重要
例如我要用terraform account 去部署1个 cloud run service, 而这个service 必须用 pubsub-publisher-a account 去启动(以获得推送消息权限), 则terraform account必须拥有对于 pubsub-publisher-a的 serviceAccountUser权限
不过较真的话, 这个第3种也是属于第一种(资源粒度), service account 也是一种资源
命令:
gcloud iam service-accounts add-iam-policy-binding [email protected] --member=serviceAccount:[email protected] --role=roles/iam.serviceAccountUser
例子:
❯ gcloud iam service-accounts add-iam-policy-binding [email protected] --member=serviceAccount:[email protected] --role=roles/iam.serviceAccountUser
Updated IAM policy for serviceAccount [[email protected]].
bindings:
- members:
- serviceAccount:[email protected]
role: roles/iam.serviceAccountUser
etag: BwYGwZ2IcnA=
version: 1
上面已经对权限大概分了类
我暂时找不到直接gcloud 命令or UI 查看某个service account对什么资源的权限细节。
但是我们可以查看某个资源的 service account 权限列表
具体点来讲,上面我们已经分配 TopicA 的publisher 权限给 service account pubsub-publisher-a .
1.基于pubsub-publisher-a 去查看 它的TopicA权限我暂时不知道方法
2.基于 TopicA去查看 权限, 是可以见到 pubsub-publisher-a 在其列表内的。
方法1
gcloud 命令
gcloud pubsub topics get-iam-policy projects/jason-hsbc/topics/TopicA
bindings:
- members:
- serviceAccount:[email protected]
role: roles/pubsub.publisher
etag: BwYGwRVxij4=
version: 1
这个相对简单。
方法一:
gcloud 命令:
gcloud projects get-iam-policy jason-hsbc
–flatten=“bindings[].members”
–format=‘table(bindings.role)’
–filter=“bindings.members:[email protected]”
gcloud projects get-iam-policy jason-hsbc \
--flatten="bindings[].members" \
--format='table(bindings.role)' \
--filter="bindings.members:[email protected]"
ROLE
roles/storage.admin
可以看出, 它是列不出资源粒度权限的, 但是可以把storage admin列出来
gcloud command
gcloud iam service-accounts get-iam-policy [email protected]
❯ gcloud iam service-accounts get-iam-policy [email protected]
bindings:
- members:
- serviceAccount:[email protected]
role: roles/iam.serviceAccountUser
etag: BwYGwZ2IcnA=
version: 1
UI上也可以:
至于为何 上面terraform 账户也拥有 对 pubsub publish a 的owner权限? 因为 terraform 被我设置成了project owenr, 就继承过来了。