最小化服务漏洞

使用第三方的开源软件、准入控制器来提升系统的安全性

PSP (pod security policy) pod的安全策略

我们创建的pod基本上是没有任何限制的
1.此pod里的进程,可以以任何用户身份运行
2.可以使用任一类型的存储
3.比如使用hostPath,可以使用任何任一类型的宿主机目录
4.使用宿主机的网络空间
5.pod里也可以使用特权运行

我想限制:
不允许使用特权运行?
不允许使用emptyDir类型的存储?
如果使用hostPath存储的话,只能映射到宿主机的/tmp目录

PSP 启动(默认关闭) PSP开启后 用户即使有权限在创建pod时不会直接创建,而是会在psp中询问是否能创建

1.启动PSP,启动之后任何用户就不能创建pod了,要配置后才可以

root@vms81:~/cks# vim /etc/kubernetes/manifests/kube-apiserver.yaml- --enable-admission-plugins=NodeRestriction,PodSecurityPolicy
root@vms81:~/cks# systemctl restart kubelet.serviceroot@vms81:~/cks# kubectl apply -f pod1.yaml
Error from server (Forbidden): error when creating "pod1.yaml": pods "audit-pod" is forbidden: PodSecurityPolicy: no providers available to validate pod request

2.运用psp

测试禁止特权运行

2.1 创建一个psp
root@vms81:~/day3# vim psp.yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:name: mypsp1   # 名为mypsp1
spec:privileged: false  # Don't allow privileged pods!   # 不允许使用特权运行# The rest fills in some required fields.seLinux:rule: RunAsAnysupplementalGroups:rule: RunAsAnyrunAsUser:rule: RunAsAnyfsGroup:rule: RunAsAnyvolumes:- '*'
root@vms81:~/day3# kubectl apply -f psp.yaml

给john一个权限,可以对pod进行管理
john–权限1
master

crole1 cbind1 ---john create,delete,get,list
root@vms81:~/day3# kubectl create clusterrole crole1 --verb=create,delete,get,list --resource=pod --dry-run=client -o yaml > crole1.yaml
root@vms81:~/day3# kubectl apply -f crole1.yaml
clusterrole.rbac.authorization.k8s.io/crole1 createdroot@vms81:~/day3# kubectl create clusterrolebinding  cbind1 --clusterrole=crole1 --user=john
clusterrolebinding.rbac.authorization.k8s.io/cbind1 created

client

root@vms82:~# kubectl apply -f pod1.yaml
Error from server (Forbidden): error when creating "pod1.yaml": pods "pod1" is forbidden: PodSecurityPolicy: unable to admit pod: []
**提示psp无法接纳pod,但是用master是可以创建这个pod的,这边报错原因是开启psp后,用户即使有权限在创建pod时不会直接创建,而是会在psp中询问是否能创建,由于john这个用户不是特殊用户没有资格访问psp**

john–权限2
master

crole2 cbind2 --john 访问mypsp1

用户访问psp是需要授权的

root@vms81:~/day3# kubectl create clusterrole crole2 --verb=use --resource=psp --resource-name=mypsp1
clusterrole.rbac.authorization.k8s.io/crole2 createdroot@vms81:~/day3# kubectl create clusterrolebinding cbind2 --clusterrole=crole2 --user=john
clusterrolebinding.rbac.authorization.k8s.io/cbind2 created

client 成功创建pod

root@vms82:~# kubectl apply -f pod1.yaml
pod/pod1 created

测试是否允许使用hostNetwork(默认允许)

root@vms81:~/day3# kubectl apply -f pod1.yaml
Error from server (Forbidden): error when creating "pod1.yaml": pods "pod1" is forbidden: PodSecurityPolicy: unable to admit pod: [spec.securityContext.hostNetwork: Invalid value: true: Host network is not allowed to be used]
**开启PSP后 hostNetwork: true pod设置了true,创建时报错PSP 禁止**
root@vms81:~/day3# vim psp.yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:name: mypsp1
spec:privileged: false  # Don't allow privileged pods!# The rest fills in some required fields.hostNetwork: true                                    # 允许使用hostNetworkseLinux:rule: RunAsAnysupplementalGroups:rule: RunAsAnyrunAsUser:rule: RunAsAnyfsGroup:rule: RunAsAnyvolumes:- '*'

pod 可以正常创建

测试是否允许使用存储(默认允许)

apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:name: mypsp1
spec:privileged: false  # Don't allow privileged pods!# The rest fills in some required fields.hostNetwork: trueseLinux:rule: RunAsAnysupplementalGroups:rule: RunAsAnyrunAsUser:rule: RunAsAnyfsGroup:rule: RunAsAnyvolumes:- 'emptyDir'      # PSP指定只能使用emptyDir

当pod使用的是hostPath时就会报错

root@vms81:~/day3# kubectl apply -f pod1.yaml
Error from server (Forbidden): error when creating "pod1.yaml": pods "pod1" is forbidden: PodSecurityPolicy: unable to admit pod: [spec.volumes[0]: Invalid value: "hostPath": hostPath volumes are not allowed to be used spec.volumes[1]: Invalid value: "projected": projected volumes are not allowed to be used]

问题,即使pod内改为了emptyDir挂载,在创建时还是报错,原因是在运行pod时会默认挂载sa到容器,而这个挂载模式是 projected ,所以还是会报错

root@vms81:~/day3# kubectl apply -f pod1.yaml
Error from server (Forbidden): error when creating "pod1.yaml": pods "pod1" is forbidden: PodSecurityPolicy: unable to admit pod: [spec.volumes[1]: Invalid value: "projected": projected volumes are not allowed to be used]

如何解决

方法一: automountServiceAccountToken: false 新增,以后凡是使用这个sa1的pod,都不自动挂载token
方法二: PSP规则加上 projected 这个挂载模式

root@vms81:~/day3# vim psp.yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:name: mypsp1
spec:privileged: false  # Don't allow privileged pods!# The rest fills in some required fields.hostNetwork: trueseLinux:rule: RunAsAnysupplementalGroups:rule: RunAsAnyrunAsUser:rule: RunAsAnyfsGroup:rule: RunAsAnyvolumes:- 'emptyDir'- 'projected'    # sa挂载的默认模式

在PSP中指定hostPath的挂载目录#########

root@vms81:~/day3# vim psp.yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:name: mypsp1
spec:privileged: false  # Don't allow privileged pods!# The rest fills in some required fields.hostNetwork: trueseLinux:rule: RunAsAnysupplementalGroups:rule: RunAsAnyrunAsUser:rule: RunAsAnyfsGroup:rule: RunAsAnyvolumes:- 'emptyDir'- 'projected'- 'hostPath'                                 # 指定了允许使用allowedHostPaths:                            # 指定了允许使用挂载的目录- pathPrefix: /tmp- pathPrefix: /xxx                           # 设置多目录允许root@vms81:~/day3# kubectl apply -f pod1.yaml
Error from server (Forbidden): error when creating "pod1.yaml": pods "pod1" is forbidden: PodSecurityPolicy: unable to admit pod: [spec.volumes[0].hostPath.pathPrefix: Invalid value: "/xx": is not allowed to be used]

pod里面就不能再设置挂载目录了,只能挂载到PSP允许的目录,

测试容器内只能以某个用户运行

root@vms81:~/day3# vim psp.yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:name: mypsp1
spec:privileged: false  # Don't allow privileged pods!# The rest fills in some required fields.hostNetwork: trueseLinux:rule: RunAsAnysupplementalGroups:rule: RunAsAnyrunAsUser:#rule: RunAsAnyrule: MustRunAs          # 设置必须要满足要求的用户才可以运行ranges:                  # 设置了UID的范围- min: 1500max: 2000fsGroup:rule: RunAsAnyvolumes:- 'emptyDir'- 'projected'- 'hostPath'allowedHostPaths:- pathPrefix: /tmp

当pod指定使用1000的UID用户运行时就会报错

spec:securityContext:runAsUser: 1000runAsGroup: 3000volumes:- name: v1hostPath:
...
root@vms81:~/day3# kubectl apply -f pod1.yaml
Error from server (Forbidden): error when creating "pod1.yaml": pods "pod1" is forbidden: PodSecurityPolicy: unable to admit pod: [spec.containers[0].securityContext.runAsUser: Invalid value: 1000: must be in the ranges: [{1500 2000]]}

需求:如何实现 — john用户可以特权访问day3(ns)的用户,但是不能特权访问day4(ns)的用户

PSP中 允许的优先级要高于拒绝的优先级,所以可以设定一个针对所有ns 的都不能特权运行的mypsp1,再创建一个可以对day3 特权运行的mypsp2

  1. 创建psp
    mypsp1 — privileged: false,用于clusterrole mypsp2 — privileged: true,用于role
root@vms81:~/day3# kubectl get psp
Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
NAME     PRIV    CAPS   SELINUX    RUNASUSER   FSGROUP    SUPGROUP   READONLYROOTFS   VOLUMES
mypsp1   false          RunAsAny   RunAsAny    RunAsAny   RunAsAny   false            *
mypsp2   true           RunAsAny   RunAsAny    RunAsAny   RunAsAny   false            *
  1. 创建一个不可以特权访问所有ns的PSP
    给john一个pod的相关权限(clusterrole 就相当于针对所有ns)
root@vms81:~/day3# kubectl create clusterrole crole1 --verb=create,delete,get,list --resource=pod
clusterrole.rbac.authorization.k8s.io/crole1 created
root@vms81:~/day3# kubectl create clusterrolebinding cbind1 --clusterrole=crole1 --user=john
clusterrolebinding.rbac.authorization.k8s.io/cbind1 created

给john一个mypsp1的相关权限

root@vms81:~/day3# kubectl create clusterrole crole2 --verb=use --resource=psp  --resource-name=mypsp1
clusterrole.rbac.authorization.k8s.io/crole2 created
root@vms81:~/day3# kubectl create clusterrolebinding cbind2 --clusterrole=crole2 --user=john
clusterrolebinding.rbac.authorization.k8s.io/cbind2 created

给john一个mypsp2的相关权限

root@vms81:~/day3# kubectl create role role3 --verb=use --resource=psp  --resource-name=mypsp2
role.rbac.authorization.k8s.io/role2 created
root@vms81:~/day3# kubectl create rolebinding rbind3 --role=role3 --user=john
rolebinding.rbac.authorization.k8s.io/rbind2 created

client pod 以特权创建测试

root@vms82:~# vim pod1.yaml
apiVersion: v1
kind: Pod
metadata:creationTimestamp: nulllabels:run: pod1name: pod1
spec:containers:- image: nginximagePullPolicy: IfNotPresentname: pod1resources: {securityContext:                    # 开启特权privileged: truednsPolicy: ClusterFirstrestartPolicy: Always
status: {}}

实现需求,在day4下无法创建,在day3下可以创建

root@vms82:~# kubectl apply -f pod1.yaml -n day4
Error from server (Forbidden): error when creating "pod1.yaml": pods "pod1" is forbidden: PodSecurityPolicy: unable to admit pod: [spec.containers[0].securityContext.privileged: Invalid value: true: Privileged containers are not allowed]
root@vms82:~# kubectl apply -f pod1.yaml -n day3
pod/pod1 created

开启PSP 对deployment的影响

开启PSP后deploy可以正常运行,但是pod不会创建

root@vms81:~/day3# kubectl describe deployments.apps deploy1
...
NewReplicaSet:     deploy1-cf4694566 (0/1 replicas created)  **没发启动成功**
...

原因是
1.deploy在运行时不是直接创建pod,而是先运行replicaSet(kubectl get rc ),再由replicaSet去创建pod
2.但是replicaSet以sa的身份去创建pod的,由于开启了PSP,sa没有权限去访问psp,所以pod就无法成功创建
解决方案:给这个sa授权可以访问PSP
给sa一个mypsp1的相关权限,由于上面 crole2 已创建并拥有 --resource=psp --resource-name=mypsp1 权限

root@vms81:~/day3# kubectl create clusterrolebinding  cbing22 --clusterrole=crole2 --group=system:serviceaccounts -n kube-system
clusterrolebinding.rbac.authorization.k8s.io/cbing22 created

–group=system:serviceaccounts 授权给所有sa 指定组

root@vms81:~/day3# kubectl get pod
NAME                      READY   STATUS    RESTARTS   AGE
deploy1-cf4694566-bxwld   1/1     Running   0          22m
pod1                      1/1     Running   0          3d18h

CKS-最小化服务漏洞-pod security policy(PSP)相关推荐

  1. 【微服务架构】在微服务架构中最小化设计时间耦合

    理查森:我是克里斯·理查森.欢迎来到我关于在微服务架构中最小化设计时耦合的演讲.在这次演讲中,我将回答三个问题.什么是设计时耦合?这会造成什么问题?我们如何设计松散耦合的服务?这些年来我做了一些事情. ...

  2. OpenShift 4 - 从 1.3G 到 50M,以最小化的可执行程序运行 Quarkus 微服务

    <OpenShift / RHEL / DevSecOps 汇总目录> 文本已在 OpenShift 4.10 环境中进行验证. 文章目录 创建应用镜像,并 Java 微服务转换可执行程序 ...

  3. CentOS6最小化安装默认启动的服务说明

    centos6.2最小化安装后执行chkconfig --list,显示所有服务,如下图: 下边分别进行说明: auditd:审核守护进程 当 auditd 运行的时候,审核信息会被发送到一个用户配置 ...

  4. 手把手教你部署一个最小化的 Kubernetes 集群

    虽然网上有大量从零搭建 K8S 的文章,但大都针对老版本,若直接照搬去安装最新的 1.20 版本会遇到一堆问题.故此将我的安装步骤记录下来,希望能为读者提供 copy and paste 式的集群搭建 ...

  5. 牛批!简单几步,无坑部署最小化 K8S 集群

    点击下方"民工哥技术之路",选择"设为星标" 回复"1024"获取独家整理的学习资料! 虽然网上有大量从零搭建 K8S 的文章,但大都针对老 ...

  6. kubernetes CKS 4.3 Pod安全策略(PSP)

    4.3.1 简述 PodSecurityPolicy(简称PSP):Kubernetes中Pod部署时重要的安全校验手段,能够 有效地约束应用运行时行为安全. 使用PSP对象定义一组Pod在运行时必须 ...

  7. Centos 7 64位 minimal 最小化安装的系统中静默安装oracle 11g r2(无图形化安装)

    一 安装运行环境(操作系统环境) 1,最小化安装 linux CentOS7 我使用的镜像包:CentOS-7-x86_64-Minimal-1708.iso 操作系统安装步骤:https://blo ...

  8. CentOS7.2最小化安装后系统优化

    系统初始化技术的演变 1.sysvinit技术 (1)Linux系统的第一个进程(pid=1)为init: Linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 b ...

  9. 生产服务器环境最小化安装后Centos 6.5优化配置备忘

    本文 centos 6.5 优化 的项有18处: 1.centos6.5最小化安装后启动网卡 2.ifconfig查询IP进行SSH链接 3.更新系统源并且升级系统 4.系统时间更新和设定定时任 5. ...

最新文章

  1. MySql层级树查询
  2. weexapp 开发流程(一)开发环境配置
  3. Python matplotlib pyplot中title() xlabel() ylabel()无法显示在中文(方框乱码)的解决办法
  4. docker下用keepalived+Haproxy实现高可用负载均衡集群
  5. Android代码设置角标,Android上的Badge,快速实现给应用添加角标
  6. linux vps 迁移数据 ip screen ctrl a d,Linux学习笔记之screen命令的使用
  7. 阿里云图片如何获取缩略图
  8. notimplementedexception
  9. 微信小程序-自定义三级联动
  10. 回炉重造 | C语言学习
  11. Spring+SpringMVC+Mybatis开发
  12. Descriptors cannot not be created directly
  13. 去除滚动条 去除滚动条样式 css修改滚动条样式
  14. css3魔方3乘3每层旋转_学习做旋转魔方 (css3)
  15. 内容为王时代的 “内容策略师”职位描述、角色和职责
  16. 基于CCD摄像头智能车分段PID控制算法设计
  17. 第一Python第一个爬虫项目
  18. 金叉走势图解上半年暴跌行情,如何证明macd金叉死叉模型确实有用?
  19. [基础论文阅读]QMIX: Monotonic Value Function Factorization for Deep Multi-agent Reinforcement Learning
  20. 章鱼网络进展月报 | 2021.11.1-11.30

热门文章

  1. 【新提醒】LENOVO_WIN7_SP1_UM_64_CN_RDVD-远景-Windows7,Windows8,旗舰版,系统下载,主题
  2. 数据结构基础之动态顺序表详解
  3. BMFont制作美术字体
  4. 【报错记录】MySQL向数据库中导入txt文件报错ERROR 1148 (42000): The used command is not allowed with this MySQL version
  5. xp 应用计算机设置在哪里打开,XP电脑开机启动项在哪里设置 修改电脑开机启动项的技巧...
  6. AI算法撑起市值,快手如何管理视频?
  7. 使用windows自带的远程桌面连接
  8. 卷积层里的填充和步幅
  9. 【C语言】图片文件 预处理
  10. 用友t 的服务器找不到系统管理,用友U8.51A服务器登录软件提示找不到服务器