关于kubernetes:Kubernetes安全之鉴权

3次阅读

共计 6158 个字符,预计需要花费 16 分钟才能阅读完成。

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
正文完
 0