列出当前GCP项目的所有service account
我们可以用gcloud 命令
gcloud iam service-accounts list
gcloud iam service-accounts list
DISPLAY NAME EMAIL DISABLED
terraform terraform@jason-hsbc.iam.gserviceaccount.com False
vm-common vm-common@jason-hsbc.iam.gserviceaccount.com False
terraform2 terraform2@jason-hsbc.iam.gserviceaccount.com False
Compute Engine default service account 912156613264-compute@developer.gserviceaccount.com False
当然在gcp console 上也可以查看
创建1个新的gcp service account
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 terraform@jason-hsbc.iam.gserviceaccount.com False
vm-common vm-common@jason-hsbc.iam.gserviceaccount.com False
pubsub publisher a pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com False
terraform2 terraform2@jason-hsbc.iam.gserviceaccount.com False
Compute Engine default service account 912156613264-compute@developer.gserviceaccount.com False
新的service account 被创建出来了。
当然, 在gcp console 也可以利用UI 创建它.
分配service account权限
gcloud pubsub topics add-iam-policy-binding projects/jason-hsbc/topics/TopicA --member=serviceAccount:pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com --role=roles/pubsub.publisher
上面语句是1个例子, 分配某个pubsub topic(TopicA) 的publish 权限。
gcloud pubsub topics add-iam-policy-binding projects/jason-hsbc/topics/TopicA --member=serviceAccount:pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com --role=roles/pubsub.publisher
Updated IAM policy for topic [TopicA].
bindings:
- members:
- serviceAccount:pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com
role: roles/pubsub.publisher
etag: BwYGwRVxij4=
version: 1
下面的例子是把storage admin 分配给这个service account
gcloud projects add-iam-policy-binding jason-hsbc --member=serviceAccount:pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com --role=roles/storage.admin
gcloud projects add-iam-policy-binding jason-hsbc --member=serviceAccount:pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com --role=roles/storage.admin
Updated IAM policy for project [jason-hsbc].
注意上面两个例子是有区别的
第一种 是分配资源粒度的权限
例如 分配某个pubsub subscription 的读权限
某个 BQ table的权限等
第二种是分配项目级别的权限, 通常是reader , editor 和 owner
例如上面的storage admin, pubsub admin等.
其实 还有第三种
就是定义谁(另1个 account) 对这个service account的权限
包括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 pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com --member=serviceAccount:terraform@jason-hsbc.iam.gserviceaccount.com --role=roles/iam.serviceAccountUser
例子:
❯ gcloud iam service-accounts add-iam-policy-binding pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com --member=serviceAccount:terraform@jason-hsbc.iam.gserviceaccount.com --role=roles/iam.serviceAccountUser
Updated IAM policy for serviceAccount [pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com].
bindings:
- members:
- serviceAccount:terraform@jason-hsbc.iam.gserviceaccount.com
role: roles/iam.serviceAccountUser
etag: BwYGwZ2IcnA=
version: 1
查看service account的权限
上面已经对权限大概分了类
对于第一种 分配资源粒度的权限
我暂时找不到直接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:pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com
role: roles/pubsub.publisher
etag: BwYGwRVxij4=
version: 1
方法2, 通过gcp console
查看 第二种 项目级别的权限
这个相对简单。
方法一:
gcloud 命令:
gcloud projects get-iam-policy jason-hsbc
–flatten=“bindings[].members”
–format=‘table(bindings.role)’
–filter=“bindings.members:pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com”
gcloud projects get-iam-policy jason-hsbc \
--flatten="bindings[].members" \
--format='table(bindings.role)' \
--filter="bindings.members:pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com"
ROLE
roles/storage.admin
可以看出, 它是列不出资源粒度权限的, 但是可以把storage admin列出来
方法二: gcp console
查看 第三种 service account 的 User
gcloud command
gcloud iam service-accounts get-iam-policy pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com
❯ gcloud iam service-accounts get-iam-policy pubsub-publisher-a@jason-hsbc.iam.gserviceaccount.com
bindings:
- members:
- serviceAccount:terraform@jason-hsbc.iam.gserviceaccount.com
role: roles/iam.serviceAccountUser
etag: BwYGwZ2IcnA=
version: 1
UI上也可以:
至于为何 上面terraform 账户也拥有 对 pubsub publish a 的owner权限? 因为 terraform 被我设置成了project owenr, 就继承过来了。