17

kubernetes 用户扮演 API

 3 years ago
source link: https://mozillazg.com/2020/06/k8s-kubernetes-kubectl-syntax-of-impersonate-as-user-or-serviceaccount-or-group.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

记录一下 kubernetes 中扮演另一个用户的 API 和用法(信息主要来自官方文档和源码)。

请求 api server 的时候指定以下 http header 之一即可实现用户扮演的需求(当然,请求方必须有扮演该用户的权限) (信息来自 文档 ):

  • Impersonate-User : 扮演的用户的用户名
  • Impersonate-Group : 扮演的用户组,多个值(出现多次)表示多个组,需要同时指定 Impersonate-User
  • Impersonate-Extra-( extra name ) : 动态指定的 key,用于指定用户的其他信息,需要同时指定 Impersonate-User

例子:

Impersonate-User: [email protected]
Impersonate-Group: developers
Impersonate-Group: admins
Impersonate-Extra-dn: cn=jane,ou=engineers,dc=example,dc=com
Impersonate-Extra-acme.com%2Fproject: some-project
Impersonate-Extra-scopes: view
Impersonate-Extra-scopes: development

Impersonate-User 的值即可以是 User 也可以是 service account,对应的语法如下:

  • <User> : 普通的自定义 User ,比如 alice , bob
  • system:serviceaccount:<namespace>:<serviceaccount> : 某个 namespace 下的 service account,比如 system:serviceaccount:kube-system:default 表示 kube-system namespace 下的 default service account

Impersonate-Group 的值即可以是普通的自定义 Group 也可以是系统内置的一些特定 group ,对应的语法如下:

  • <Group> : 普通的自定义 Group ,比如 admins , developers
  • system:authenticated : 所有已认证用户
  • system:unauthenticated : 所有未认证用户
  • system:serviceaccounts : 所有 serviceaccount (无论是哪个 namespace 下的)
  • system:serviceaccounts:<namespace> : 某个 namespace 下的所有 serviceaccount

kubectl 命令的 --as 可以配置 Impersonate-User 的值, --as-group 可以配置 Impersonate-Group 的值,例子:

  • kubectl --as=system:serviceaccount:kube-system:default
  • kubectl --as=superman --as-group=system:masters

配置扮演用户的权限

默认情况下大部分 user 或 serviceaccount 都是没有扮演用户的权限的,可以通过 RBAC 的方式配置权限。

简单来说就是需要为发起扮演的用户绑定一个拥有 impersonate 权限的 ClusterRole 。

可以扮演 user、group、serviceaccount 的 ClusterRole 例子:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: impersonator
rules:
- apiGroups: [""]
  resources: ["users", "groups", "serviceaccounts"]
  verbs: ["impersonate"]

Impersonate-Extra-( extra name ) header 支持的 (extra name) 也是需要绑定相应的 ClusterRole 。

比如下面的 ClusterRole 表示可以在扮演是设置 Impersonate-Extra-scopes header,其中的 scopes 就是在下面的 resources 里定义的:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: scopes-impersonator
rules:
# Can set "Impersonate-Extra-scopes" header.
- apiGroups: ["authentication.k8s.io"]
  resources: ["userextras/scopes"]
  verbs: ["impersonate"]

同时还可以通过 resourceNames 的值限制 header 的有效值,例子:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: limited-impersonator
rules:
# Can impersonate the user "[email protected]"
- apiGroups: [""]
  resources: ["users"]
  verbs: ["impersonate"]
  resourceNames: ["[email protected]"]

# Can impersonate the groups "developers" and "admins"
- apiGroups: [""]
  resources: ["groups"]
  verbs: ["impersonate"]
  resourceNames: ["developers","admins"]

# Can impersonate the extras field "scopes" with the values "view" and "development"
- apiGroups: ["authentication.k8s.io"]
  resources: ["userextras/scopes"]
  verbs: ["impersonate"]
  resourceNames: ["view", "development"]

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK