+-
Kubernetes安全之鉴权
首页 专栏 云计算 文章详情
0

Kubernetes安全之鉴权

Yuan_sr 发布于 5 月 19 日

Authorization

上面认证过程,只是确认通信的双方都确认了对方是可信的,可以相互通信。而鉴权是确定请求方有哪些资源的权限。API Server目前支持以下几种授权策略(通过API Server的启动参数“--authorization-mode”设置)

AlwaysDeny:表示拒绝所有的请求,一般用于测试 AlwaysAllow:允许接收所有请求,如果集群不需要授权流程,则可以采用该策略 ABAC(Attribute-Based Access Control):基于属性的访问控制,表示使用用户配置的授权规则对用户请求进行匹配和控制 Webbook:通过调用外部REST服务对用户进行授权 RBAC(Role-Based Access Control):基于角色的访问控制,现行默认规则

RBAC授权模式

RBAC(Role-Based Access Control)基于角色的访问控制,在Kubernetes 1.5中引入,现行版本成为默认标准。相对其它访问控制方式,拥有以下优势:

对集群中的资源和非资源均拥有完整的覆盖 整个RBAC完全由几个API对象完成,同其它API对象一样,可以用kubectl或API进行操作 可以在运行时进行调整,无需重启API Server

Ⅰ、RBAC的API资源对象说明

RBAC引入了4个新的顶级资源对象:Role、ClusterRole、RoleBinding、ClusterRoleBinding,4种对象类型均可以通过kubectl与API操作

需要注意的是Kubenetes并不会提供用户管理,那么User、Group、ServiceAccount指定的用户又是从哪里来的呢?Kubenetes组件(kubectl、kube-proxy)或是其他自定义的用户在向CA申请证书时,需要提供一个证书请求文件

{ "CN": "admin", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "system:masters", "OU": "System" } ] }

API Server会把客户端证书的CN字段作为User,把names.O字段作为Group

kubelet使用TLS Bootstaping认证时,API Server可以使用Bootstrap Tokens或者Token authenticationfile验证=token,无论哪一种,Kubenetes都会为token绑定一个默认的User和Group

Pod使用ServiceAccount认证时,service-account-token中的JWT会保存User信息

有了用户信息,再创建一对角色/角色绑定(集群角色/集群角色绑定)资源对象,就可以完成权限绑定了

Role and ClusterRole

在RBAC API中,Role表示一组规则权限,权限只会增加(累加权限),不存在一个资源一开始就有很多权限而通过RBAC对其进行减少的操作;Role可以定义在一个namespace中,如果想要跨namespace则可以创建ClusterRole

kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: namespace: default name: pod-reader rules: - apiGroups: [""] # "" indicates the core API group resources: ["pods"] verbs: ["get","watch","list"]

ClusterRole具有与Role相同的权限角色控制能力,不同的是ClusterRole是集群级别的,ClusterRole可以用于:

集群级别的资源控制(例如node访问权限) 非资源型endpoints(例如/healthz访问) 所有命名空间资源控制(例如pods ) kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: # "namespace" omitted since ClusterRoles are not namespaced name: secret-reader rules: - apiGroups: [""] resources: ["secrets"] verbs: ["get","watch","list"]

RoleBinding and ClusterRoleBinding

RoloBinding可以将角色中定义的权限授予用户或用户组,RoleBinding包含一组权限列表(subjects),权限列表中包含有不同形式的待授予权限资源类型(users, groups, or service accounts);RoloBinding同样包含对被Bind的Role引用;RoleBinding适用于某个命名空间内授权,而ClusterRoleBinding适用于集群范围内的授权

将default命名空间的pod-reader Role授予jane用户,此后jane用户在default命名空间中将具有podreader的权限

kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-pods namespace: default subjects: - kind: User name: jane apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io

RoleBinding同样可以引用ClusterRole来对当前namespace内用户、用户组或ServiceAccount进行授权,这种操作允许集群管理员在整个集群内定义一些通用的ClusterRole,然后在不同的namespace中使用RoleBinding来引用

例如,以下RoleBinding引用了一个ClusterRole,这个ClusterRole具有整个集群内对secrets的访问权限;但是其授权用户dave只能访问development空间中的secrets(因为RoleBinding定义在development命名空间)

# This role binding allows "dave" to read secrets in the "development" namespace. kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-secrets namespace: development # This only grants permissions within the "development" namespace. subjects: - kind: User name: dave apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.io

使用ClusterRoleBinding可以对整个集群中的所有命名空间资源权限进行授权;以下ClusterRoleBinding样例展示了授权manager组内所有用户在全部命名空间中对secrets进行访问

# This cluster role binding allows anyone in the " manager" group to read secrets in any namespace. kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-secrets-global subjects: - kind: Group name: manager apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.io

Resources

Kubernetes集群内一些资源一般以其名称字符串来表示,这些字符串一般会在API的URL地址中出现;同时某些资源也会包含子资源,例如logs资源就属于pods的子资源,API中URL样例如下

GET /api/v1/namespaces/{namespace}/pods/{name}/log

如果要在RBAC授权模型中控制这些子资源的访问权限,可以通过 / 分隔符来实现,以下是一个定义pods资资源logs访问权限的Role定义样例

kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: namespace: default name: pod-and-pod-logs-reader rules: - apiGroups: [""] resources: ["pods/log"] verbs: ["get","list"]

to Subjects

RoleBinding和ClusterRoleBinding可以将Role绑定到Subjects;Subjects可以是groups、users或者service accounts

Subjects中Users使用字符串表示,它可以是一个普通的名字字符串,如“alice”;也可以是email格式的邮箱地址,如“[email protected]”;甚至是一组字符串形式的数字ID。但是Users的前缀 system: 是系统保留的,集群管理员应该确保普通用户不会使用这个前缀格式

Groups书写格式与Users相同,都为一个字符串,并且没有特定的格式要求;同样 system: 前缀为系统保留

实践:创建一个用户只能管理dev空间

在集群中的一个节点创建一个普通用户devuser

useradd devuser passwd devuser

此时登录devuser后执行kubectl get pod 时什么也看不到,包括default 命名空间下的,因此需要给devuser创建证书访问信息--config

在集群中创建一个命名空间供devuser用户使用

kubectl create namespace dev

编写证书生成文件的信息devuser-csr.json

{ # 用户名 "CN": "devuser", # 指定只能在kubernetes中哪个节点可以登录该用户 "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "ChengDu", "L": "ChengDu", # 组名 "O": "k8s", "OU": "System" } ] } # 下载证书生成工具 wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 mv cfssl_linux-amd64 /usr/local/bin/cfssl wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 mv cfssljson_linux-amd64 /usr/local/bin/cfssljson wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 mv cfssl-certinfo_linux-amd64 /usr/local/bin/cfssl-certinfo # 这里首先进入/etc/kubernetes/pki目录下,目的是使用根证书签发devuser的客户端证书请求、证书私钥和证书,执行后生成devuser.csr和deruser-key.pem和deruser.pem三个文件 cfssl gencert -ca=ca.crt -ca-key=ca.key -profile=kubernetes /root/devuser-csr.json | cfssljson -bare devuser # 设置集群参数 export KUBE_APISERVER="https://172.20.0.113:6443" # apiserver的访问地址 # 生成deruser.kubeconfig认证文件 kubectl config set-cluster kubernetes \ --certificate-authority=/etc/kubernetes/pki/ca.crt \ --embed-certs=true \ --server=${KUBE_APISERVER} \ --kubeconfig=devuser.kubeconfig # 设置客户端认证参数(客户端认证信息),追加到devuser.kubeconfig文件中 kubectl config set-credentials devuser \ --client-certificate=/etc/kubernetes/pki/devuser.pem \ --client-key=/etc/kubernetes/pki/devuser-key.pem \ --embed-certs=true \ --kubeconfig=devuser.kubeconfig # 设置上下文参数(命名空间),追加到devuser.kubeconfig文件中 kubectl config set-context kubernetes \ --cluster=kubernetes \ --user=devuser \ --namespace=dev \ --kubeconfig=devuser.kubeconfig # 将集群中默认用户admin绑定到dev命名空间下,这样admin就可以在该命名空间下为所欲为了 kubectl create rolebinding devuser-admin-binding --clusterrole=admin --user=devuser --namespace=dev # 登录deruser用户后在/root/下创建.kube文件夹 mkdir .kube # 将面生成的devuser.kubeconfig文件拷贝到devuser用户的/root/.kube/下,并重命名为config cp -f ./devuser.kubeconfig /home/devuser/.kube/config # 给config文件权限 chown devuser:devuser /home/devuser/.kube/config # 到这里devuser还不能访问集群中的信息,需要设置默认上下文,登录devuser用户执行: kubectl config use-context kubernetes --kubeconfig=config
kubernetes 云计算 云原生-cloud-native
阅读 40 发布于 5 月 19 日
举报
收藏
分享
本作品系原创, 采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议
Yuan_sr
4 声望
1 粉丝
关注作者
0 条评论
得票数 最新
提交评论
Yuan_sr
4 声望
1 粉丝
关注作者
宣传栏
目录

Authorization

上面认证过程,只是确认通信的双方都确认了对方是可信的,可以相互通信。而鉴权是确定请求方有哪些资源的权限。API Server目前支持以下几种授权策略(通过API Server的启动参数“--authorization-mode”设置)

AlwaysDeny:表示拒绝所有的请求,一般用于测试 AlwaysAllow:允许接收所有请求,如果集群不需要授权流程,则可以采用该策略 ABAC(Attribute-Based Access Control):基于属性的访问控制,表示使用用户配置的授权规则对用户请求进行匹配和控制 Webbook:通过调用外部REST服务对用户进行授权 RBAC(Role-Based Access Control):基于角色的访问控制,现行默认规则

RBAC授权模式

RBAC(Role-Based Access Control)基于角色的访问控制,在Kubernetes 1.5中引入,现行版本成为默认标准。相对其它访问控制方式,拥有以下优势:

对集群中的资源和非资源均拥有完整的覆盖 整个RBAC完全由几个API对象完成,同其它API对象一样,可以用kubectl或API进行操作 可以在运行时进行调整,无需重启API Server

Ⅰ、RBAC的API资源对象说明

RBAC引入了4个新的顶级资源对象:Role、ClusterRole、RoleBinding、ClusterRoleBinding,4种对象类型均可以通过kubectl与API操作

需要注意的是Kubenetes并不会提供用户管理,那么User、Group、ServiceAccount指定的用户又是从哪里来的呢?Kubenetes组件(kubectl、kube-proxy)或是其他自定义的用户在向CA申请证书时,需要提供一个证书请求文件

{ "CN": "admin", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "system:masters", "OU": "System" } ] }

API Server会把客户端证书的CN字段作为User,把names.O字段作为Group

kubelet使用TLS Bootstaping认证时,API Server可以使用Bootstrap Tokens或者Token authenticationfile验证=token,无论哪一种,Kubenetes都会为token绑定一个默认的User和Group

Pod使用ServiceAccount认证时,service-account-token中的JWT会保存User信息

有了用户信息,再创建一对角色/角色绑定(集群角色/集群角色绑定)资源对象,就可以完成权限绑定了

Role and ClusterRole

在RBAC API中,Role表示一组规则权限,权限只会增加(累加权限),不存在一个资源一开始就有很多权限而通过RBAC对其进行减少的操作;Role可以定义在一个namespace中,如果想要跨namespace则可以创建ClusterRole

kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: namespace: default name: pod-reader rules: - apiGroups: [""] # "" indicates the core API group resources: ["pods"] verbs: ["get","watch","list"]

ClusterRole具有与Role相同的权限角色控制能力,不同的是ClusterRole是集群级别的,ClusterRole可以用于:

集群级别的资源控制(例如node访问权限) 非资源型endpoints(例如/healthz访问) 所有命名空间资源控制(例如pods ) kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: # "namespace" omitted since ClusterRoles are not namespaced name: secret-reader rules: - apiGroups: [""] resources: ["secrets"] verbs: ["get","watch","list"]

RoleBinding and ClusterRoleBinding

RoloBinding可以将角色中定义的权限授予用户或用户组,RoleBinding包含一组权限列表(subjects),权限列表中包含有不同形式的待授予权限资源类型(users, groups, or service accounts);RoloBinding同样包含对被Bind的Role引用;RoleBinding适用于某个命名空间内授权,而ClusterRoleBinding适用于集群范围内的授权

将default命名空间的pod-reader Role授予jane用户,此后jane用户在default命名空间中将具有podreader的权限

kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-pods namespace: default subjects: - kind: User name: jane apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io

RoleBinding同样可以引用ClusterRole来对当前namespace内用户、用户组或ServiceAccount进行授权,这种操作允许集群管理员在整个集群内定义一些通用的ClusterRole,然后在不同的namespace中使用RoleBinding来引用

例如,以下RoleBinding引用了一个ClusterRole,这个ClusterRole具有整个集群内对secrets的访问权限;但是其授权用户dave只能访问development空间中的secrets(因为RoleBinding定义在development命名空间)

# This role binding allows "dave" to read secrets in the "development" namespace. kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-secrets namespace: development # This only grants permissions within the "development" namespace. subjects: - kind: User name: dave apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.io

使用ClusterRoleBinding可以对整个集群中的所有命名空间资源权限进行授权;以下ClusterRoleBinding样例展示了授权manager组内所有用户在全部命名空间中对secrets进行访问

# This cluster role binding allows anyone in the " manager" group to read secrets in any namespace. kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-secrets-global subjects: - kind: Group name: manager apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.io

Resources

Kubernetes集群内一些资源一般以其名称字符串来表示,这些字符串一般会在API的URL地址中出现;同时某些资源也会包含子资源,例如logs资源就属于pods的子资源,API中URL样例如下

GET /api/v1/namespaces/{namespace}/pods/{name}/log

如果要在RBAC授权模型中控制这些子资源的访问权限,可以通过 / 分隔符来实现,以下是一个定义pods资资源logs访问权限的Role定义样例

kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: namespace: default name: pod-and-pod-logs-reader rules: - apiGroups: [""] resources: ["pods/log"] verbs: ["get","list"]

to Subjects

RoleBinding和ClusterRoleBinding可以将Role绑定到Subjects;Subjects可以是groups、users或者service accounts

Subjects中Users使用字符串表示,它可以是一个普通的名字字符串,如“alice”;也可以是email格式的邮箱地址,如“[email protected]”;甚至是一组字符串形式的数字ID。但是Users的前缀 system: 是系统保留的,集群管理员应该确保普通用户不会使用这个前缀格式

Groups书写格式与Users相同,都为一个字符串,并且没有特定的格式要求;同样 system: 前缀为系统保留

实践:创建一个用户只能管理dev空间

在集群中的一个节点创建一个普通用户devuser

useradd devuser passwd devuser

此时登录devuser后执行kubectl get pod 时什么也看不到,包括default 命名空间下的,因此需要给devuser创建证书访问信息--config

在集群中创建一个命名空间供devuser用户使用

kubectl create namespace dev

编写证书生成文件的信息devuser-csr.json

{ # 用户名 "CN": "devuser", # 指定只能在kubernetes中哪个节点可以登录该用户 "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "ChengDu", "L": "ChengDu", # 组名 "O": "k8s", "OU": "System" } ] } # 下载证书生成工具 wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 mv cfssl_linux-amd64 /usr/local/bin/cfssl wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 mv cfssljson_linux-amd64 /usr/local/bin/cfssljson wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 mv cfssl-certinfo_linux-amd64 /usr/local/bin/cfssl-certinfo # 这里首先进入/etc/kubernetes/pki目录下,目的是使用根证书签发devuser的客户端证书请求、证书私钥和证书,执行后生成devuser.csr和deruser-key.pem和deruser.pem三个文件 cfssl gencert -ca=ca.crt -ca-key=ca.key -profile=kubernetes /root/devuser-csr.json | cfssljson -bare devuser # 设置集群参数 export KUBE_APISERVER="https://172.20.0.113:6443" # apiserver的访问地址 # 生成deruser.kubeconfig认证文件 kubectl config set-cluster kubernetes \ --certificate-authority=/etc/kubernetes/pki/ca.crt \ --embed-certs=true \ --server=${KUBE_APISERVER} \ --kubeconfig=devuser.kubeconfig # 设置客户端认证参数(客户端认证信息),追加到devuser.kubeconfig文件中 kubectl config set-credentials devuser \ --client-certificate=/etc/kubernetes/pki/devuser.pem \ --client-key=/etc/kubernetes/pki/devuser-key.pem \ --embed-certs=true \ --kubeconfig=devuser.kubeconfig # 设置上下文参数(命名空间),追加到devuser.kubeconfig文件中 kubectl config set-context kubernetes \ --cluster=kubernetes \ --user=devuser \ --namespace=dev \ --kubeconfig=devuser.kubeconfig # 将集群中默认用户admin绑定到dev命名空间下,这样admin就可以在该命名空间下为所欲为了 kubectl create rolebinding devuser-admin-binding --clusterrole=admin --user=devuser --namespace=dev # 登录deruser用户后在/root/下创建.kube文件夹 mkdir .kube # 将面生成的devuser.kubeconfig文件拷贝到devuser用户的/root/.kube/下,并重命名为config cp -f ./devuser.kubeconfig /home/devuser/.kube/config # 给config文件权限 chown devuser:devuser /home/devuser/.kube/config # 到这里devuser还不能访问集群中的信息,需要设置默认上下文,登录devuser用户执行: kubectl config use-context kubernetes --kubeconfig=config