关于kubernetes:Kubernetes安全之鉴权

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格局的邮箱地址,如“Yuan_sr@163.com”;甚至是一组字符串模式的数字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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理