供应链安全
目录
文章目录
本节实战
实战名称 |
---|
💘 实战:安装Trivy-2023.6.4(测试成功) |
💘 实战:安装kubesec-2023.6.4(测试成功) |
💘 实战:准入控制器: ImagePolicyWebhook-2023.6.6(测试成功) |
可信任软件供应链概述
**可信任软件供应链:**指在建设基础架构过程中,涉及的软件都是可信任的。
在K8s领域可信软件供应链主要是指镜像,因为一些软件交付物都是镜像,部署的最小载体。
构建镜像Dockerfile文件优化
- 减少镜像层:一次RUN指令形成新的一层,尽量Shell命令都写在一行,减少镜像层。
- **清理无用文件:**清理对应的残留数据,例如yum缓存。
- **清理无用的软件包:**基础镜像默认会带一些debug工具,可以删除掉,仅保留应用程序所需软件,防止黑客利用。
- 选择最小的基础镜像:例如alpine
- **使用非root用户运行:**USER指令指定普通用户
- 注意:
一次RUN指令形成新的一层,尽量Shell命令都写在一行,减少镜像层。
- 删除掉这个缓存目录就行
- alpine镜像大小:5MB
镜像漏洞扫描工具:Trivy
**Trivy:**是一种用于容器镜像、文件系统、Git仓库的漏洞扫描工具。发现目标软件存在的漏洞。
Trivy易于使用,只需安装二进制文件即可进行扫描,方便集成CI系统。
项目地址:https://github.com/aquasecurity/trivy
💘 实战:安装Trivy-2023.6.4(测试成功)
- 实验环境
实验环境:
centos7
- 实验软件
链接:https://pan.baidu.com/s/17XstYWfyOWW3nyNgxhS4yQ?pwd=0820
提取码:0820
2023.6.4-trivy-code
- 安装步骤
1、下载软件
[root@k8s-master1 ~]#ll -h trivy_0.18.3_Linux-64bit.tar.gz #自己去官网下载,这里直接用提供的安装包
-rw-r--r-- 1 root root 11M Jun 4 07:39 trivy_0.18.3_Linux-64bit.tar.gz
2、移动二进制文件到/usr/bin目录
[root@k8s-master1 ~]#mkdir trivy
[root@k8s-master1 ~]#mv trivy_0.18.3_Linux-64bit.tar.gz trivy
[root@k8s-master1 ~]#cd trivy/
[root@k8s-master1 trivy]#tar xf trivy_0.18.3_Linux-64bit.tar.gz
[root@k8s-master1 trivy]#ls
contrib LICENSE README.md trivy trivy_0.18.3_Linux-64bit.tar.gz
[root@k8s-master1 trivy]#mv trivy /usr/bin/
3、测试
[root@k8s-master1 ~]#trivy --help
NAME:
trivy - A simple and comprehensive vulnerability scanner for containers
USAGE:
trivy [global options] command [command options] target
VERSION:
0.18.3
COMMANDS:
image, i scan an image
filesystem, fs scan local filesystem
repository, repo scan remote repository
client, c client mode
server, s server mode
plugin, p manage plugins
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--quiet, -q suppress progress bar and log output (default: false) [$TRIVY_QUIET]
--debug, -d debug mode (default: false) [$TRIVY_DEBUG]
--cache-dir value cache directory (default: "/root/.cache/trivy") [$TRIVY_CACHE_DIR]
--help, -h show help (default: false)
--version, -v print the version (default: false)
安装结束。😘
示例:
# 容器镜像扫描
trivy image nginx
trivy image -i nginx.tar
# 打印指定(高危、严重)漏洞信息
trivy image -s HIGH nginx
trivy image -s HIGH,CRITICAL nginx
# JSON格式输出并保存到文件
trivy image -f json -o output.json nginx
漏洞数据库:
检查YAML文件安全配置:kubesec
**kubesec:**是一个针对K8s资源清单文件进行安全配置评估的工具,根据安全配置最佳实践来验证并给出建议。
官网:https://kubesec.io
项目地址:https://github.com/controlplaneio/kubesec
💘 实战:安装kubesec-2023.6.4(测试成功)
- 实验环境
实验环境:
centos7
- 实验软件
链接:https://pan.baidu.com/s/1SvQ1ijvplpe-hfUv6cCUgQ?pwd=0820
提取码:0820
2023.6.4-kubesec-code
- 安装步骤
1、下载软件
root@k8s-master1 ~]#ll -h kubesec_linux_amd64.tar.gz
-rw-r--r-- 1 root root 3.9M Jun 4 07:39 kubesec_linux_amd64.tar.gz
2、解压
[root@k8s-master1 ~]#tar xf kubesec_linux_amd64.tar.gz
[root@k8s-master1 ~]#mv kubesec /usr/bin/
3、验证
[root@k8s-master1 ~]#kubesec --help
Validate Kubernetes resource security policies
Usage:
kubesec [command]
Available Commands:
help Help about any command
http Starts kubesec HTTP server on the specified port
scan Scans Kubernetes resource YAML or JSON
version Prints kubesec version
Flags:
-h, --help help for kubesec
Use "kubesec [command] --help" for more information about a command.
安装结束。😘
示例:
kubesec scan deployment.yaml
或者使用容器环境执行检查
docker run -i kubesec/kubesec scan /dev/stdin < deployment.yaml
kubesec内置一个HTTP服务器,可以直接启用,远程调用。
- 二进制
kubesec http 8080 &
- Docker容器
docker run -d -p 8080:8080 kubesec/kubesec http 8080
示例:
curl -sSX POST --data-binary @deployment.yaml http://192.168.31.71:8080/scan
测试过程:
[root@k8s-master1 ~]#kubectl create deployment web --image=nginx --dry-run=client -oyaml > deployment.yaml
[root@k8s-master1 ~]#kubesec scan deployment.yaml
[root@k8s-master1 ~]#docker run -d -p 8085:8080 kubesec/kubesec http 8080 #宿主机端口:容器端口
[root@k8s-master1 ~]#curl -sSX POST --data-binary @deployment.yaml http://172.29.9.31:8085/scan #宿主机的8085端口
准入控制器: Admission Webhook
**Admission Webhook:**准入控制器Webhook是准入控制插件的一种,用于拦截所有向APISERVER发送的请求,并且可以修改请求或拒绝请求。
Admission webhook为开发者提供了非常灵活的插件模式,在kubernetes资源持久化之前,管理员通过程序可以对指定资源做校验、修改等操作。例如为资源自动打标签、pod设置默认SA,自动注入sidecar容器等。
相关Webhook准入控制器:
- MutatingAdmissionWebhook:修改资源,理论上可以监听并修改任何经过ApiServer处理的请求
- ValidatingAdmissionWebhook:验证资源
- ImagePolicyWebhook:镜像策略,主要验证镜像字段是否满足条件
准入控制器: ImagePolicyWebhook
💘 实战:准入控制器: ImagePolicyWebhook-2023.6.6(测试成功)
- 实验环境
实验环境:
1、win10,vmwrokstation虚机;
2、k8s集群:3台centos7.6 1810虚机,1个master节点,2个node节点
k8s version:v1.20.0
docker://20.10.7
- 实验软件
链接:https://pan.baidu.com/s/1RGJtAhqWXMNVxk7vauaCeA?pwd=0820
提取码:0820
2023.6.6-ImagePolicyWebhook-code
- 课件步骤
1、准备配置文件
(在k8s-master1
上操作)
- 创建
admission_configuration.yaml
文件
#创建/etc/kubernetes/image-policy目录及/etc/kubernetes/image-policy/admission_configuration.yaml文件
[root@k8s-master1 ~]#mkdir /etc/kubernetes/image-policy/
[root@k8s-master1 ~]#vim /etc/kubernetes/image-policy/admission_configuration.yaml
apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
- name: ImagePolicyWebhook
configuration:
imagePolicy:
kubeConfigFile: /etc/kubernetes/image-policy/connect_webhook.yaml # 连接镜像策略服务器配置文件
allowTTL: 50 # 控制批准请求的缓存时间,单位秒
denyTTL: 50 # 控制拒绝请求的缓存时间,单位秒
retryBackoff: 500 # 控制重试间隔,单位毫秒
defaultAllow: true # 确定webhook后端失效的行为
- 创建
connect_webhook.yaml
文件
[root@k8s-master1 ~]#vim /etc/kubernetes/image-policy/connect_webhook.yaml
apiVersion: v1
kind: Config
clusters:
- cluster:
certificate-authority: /etc/kubernetes/image-policy/webhook.pem # 数字证书,用于验证远程服务
server: https://172.29.9.32:8081/image_policy # 镜像策略服务器地址,必须是https
name: webhook
contexts:
- context:
cluster: webhook
user: apiserver
name: webhook
current-context: webhook
preferences: {}
users:
- name: apiserver
user:
client-certificate: /etc/kubernetes/image-policy/apiserver-client.pem # webhook准入控制器使用的证书
client-key: /etc/kubernetes/image-policy/apiserver-client-key.pem # 对应私钥证书
注:涉及的证书文件,下一步将生成,然后会拷贝到相应路径。
2、部署镜像服务器
(在k8s-node1
上操作。)
自己用python开发一个简单的webhook端点服务器,作用是拒绝部署的镜像乜有指定标签(即latest)。
(1) 自签HTTPS证书
来到k8s-node1
节点:
- 将压缩包
image-policy-webhook.zip
拷贝到k8s-node1
节点并解压:
[root@k8s-node1 ~]#ll
total 4
-rw-r--r-- 1 root root 2910 Jun 5 07:09 image-policy-webhook.zip
[root@k8s-node1 ~]#unzip image-policy-webhook.zip
Archive: image-policy-webhook.zip
creating: image-policy-webhook/
inflating: image-policy-webhook/Dockerfile
inflating: image-policy-webhook/main.py
inflating: admission_configuration.yaml
inflating: connect_webhook.yaml
inflating: image-policy-certs.sh
[root@k8s-node1 ~]#ls
admission_configuration.yaml connect_webhook.yaml image-policy-certs.sh image-policy-webhook image-policy-webhook.zip
- 查看当前
image-policy-certs.sh
自签脚本文件:
[root@k8s-node1 ~]#cat image-policy-certs.sh
[root@k8s-node1 ~]#cat image-policy-certs.sh
cat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat > ca-csr.json <<EOF
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
cat > webhook-csr.json <<EOF
{
"CN": "webhook",
"hosts": [
"172.29.9.32"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes webhook-csr.json | cfssljson -bare webhook
cat > apiserver-client-csr.json <<EOF
{
"CN": "apiserver",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes apiserver-client-csr.json | cfssljson -bare apiserver-client
- 执行脚本:
[root@k8s-node1 ~]#sh image-policy-certs.sh
[root@k8s-node1 ~]#ll
total 5784
-rw-r--r-- 1 root root 508 Oct 21 2021 admission_configuration.yaml
-rw-r--r-- 1 root root 956 Jun 5 07:15 apiserver-client.csr
-rw-r--r-- 1 root root 182 Jun 5 07:15 apiserver-client-csr.json
-rw------- 1 root root 1679 Jun 5 07:15 apiserver-client-key.pem
-rw-r--r-- 1 root root 1306 Jun 5 07:15 apiserver-client.pem
-rw-r--r-- 1 root root 294 Jun 5 07:15 ca-config.json
-rw-r--r-- 1 root root 960 Jun 5 07:15 ca.csr
-rw-r--r-- 1 root root 212 Jun 5 07:15 ca-csr.json
-rw------- 1 root root 1679 Jun 5 07:15 ca-key.pem
-rw-r--r-- 1 root root 1273 Jun 5 07:15 ca.pem
-rw-r--r-- 1 root root 5850685 Jun 5 07:15 cfssl.tar.gz
-rw-r--r-- 1 root root 632 Oct 21 2021 connect_webhook.yaml
-rw-r--r-- 1 root root 1365 Jun 5 07:13 image-policy-certs.sh
drwxr-xr-x 2 root root 39 Jul 9 2021 image-policy-webhook
-rw-r--r-- 1 root root 2910 Jun 5 07:09 image-policy-webhook.zip
-rw-r--r-- 1 root root 1001 Jun 5 07:15 webhook.csr
-rw-r--r-- 1 root root 202 Jun 5 07:15 webhook-csr.json
-rw------- 1 root root 1679 Jun 5 07:15 webhook-key.pem
-rw-r--r-- 1 root root 1330 Jun 5 07:15 webhook.pem
- 拷贝证书文件到
k8s-master1
对应目录:
[root@k8s-node1 ~]#scp webhook.pem apiserver-client-key.pem apiserver-client.pem root@172.29.9.31:/etc/kubernetes/image-policy/
The authenticity of host '172.29.9.31 (172.29.9.31)' can't be established.
ECDSA key fingerprint is SHA256:XfMhwZeoqC6kPHaF1uPzLdY9t2ZgNoNvyEd0kJd24eY.
ECDSA key fingerprint is MD5:ec:5e:37:9d:fc:e7:af:e1:9e:3a:ac:21:81:92:b5:91.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.29.9.31' (ECDSA) to the list of known hosts.
root@172.29.9.31's password:
webhook.pem 100% 1330 1.3MB/s 00:00
apiserver-client-key.pem 100% 1679 858.4KB/s 00:00
apiserver-client.pem 100% 1306 330.6KB/s 00:00
[root@k8s-master1 ~]#cd /etc/kubernetes/image-policy/
[root@k8s-master1 image-policy]#ll
total 20
-rw-r--r-- 1 root root 509 Jun 5 07:01 admission_configuration.yaml
-rw------- 1 root root 1679 Jun 5 07:27 apiserver-client-key.pem
-rw-r--r-- 1 root root 1306 Jun 5 07:27 apiserver-client.pem
-rw-r--r-- 1 root root 633 Jun 5 07:04 connect_webhook.yaml
-rw-r--r-- 1 root root 1330 Jun 5 07:27 webhook.pem
(2) Docker容器启动镜像策略服务
- 构建镜像:
[root@k8s-node1 ~]#pwd
/root
[root@k8s-node1 ~]#ls
admission_configuration.yaml apiserver-client-key.pem ca.csr ca.pem image-policy-certs.sh webhook.csr webhook.pem
apiserver-client.csr apiserver-client.pem ca-csr.json cfssl.tar.gz image-policy-webhook webhook-csr.json
apiserver-client-csr.json ca-config.json ca-key.pem connect_webhook.yaml image-policy-webhook.zip webhook-key.pem
[root@k8s-node1 ~]#cd image-policy-webhook
[root@k8s-node1 image-policy-webhook]#ls
Dockerfile main.py
[root@k8s-node1 image-policy-webhook]#cat Dockerfile
FROM python
RUN useradd python
RUN mkdir /data/www -p
COPY . /data/www
RUN chown -R python /data
RUN pip install flask -i https://mirrors.aliyun.com/pypi/simple/
WORKDIR /data/www
USER python
CMD python main.py
[root@k8s-node1 image-policy-webhook]#docker build -t image-policy-webhook .
[root@k8s-node1 image-policy-webhook]#docker images|grep image-policy-webhook
image-policy-webhook latest 331d1c0b42d8 18 seconds ago 936MB
- 启动容器:
docker run -d -u root --name=image-policy-webhook \
-v $PWD/webhook.pem:/data/www/webhook.pem \
-v $PWD/webhook-key.pem:/data/www/webhook-key.pem \
-e PYTHONUNBUFFERED=1 -p 8081:8080 \
image-policy-webhook
#注意:这里的证书文件是放置在/root下的;
#注意:课件里宿主机端口用的是8080,但自己宿主机8080端口被nodeCache占用了,因此这里使用8081.
[root@k8s-node1 ~]#docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7145eb5dfa6d image-policy-webhook "/bin/sh -c 'python …" 5 seconds ago Up 4 seconds 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp image-policy-webhook
[root@k8s-node1 ~]#docker logs 7145eb5dfa6d
* Serving Flask app 'main'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on https://127.0.0.1:8080
* Running on https://172.17.0.2:8080
Press CTRL+C to quit
[root@k8s-node1 ~]#
3、启用准入控制插件
[root@k8s-master1 ~]#vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --enable-admission-plugins=NodeRestriction,ImagePolicyWebhook
- --admission-control-config-file=/etc/kubernetes/image-policy/admission_configuration.yaml
……
- mountPath: /etc/kubernetes/image-policy
name: image-policy
readOnly: true
……
- hostPath:
path: /etc/kubernetes/image-policy
type: DirectoryOrCreate
name: image-policy
报错退出,此时可以看到静态pod kube-apiserver-k8s-master1
发生重启了。
4、测试
- 在
k8s-node1
上持续查看容器image-policy
的日志
[root@k8s-node1 ~]#docker logs -f image-policy-webhook
- 创建不带tag的deployment资源测试
[root@k8s-master1 image-policy]#kubectl create deployment web-no-tag --image=nginx
deployment.apps/web-no-tag created
[root@k8s-master1 image-policy]#kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
web-no-tag 0/1 0 0 10s
[root@k8s-master1 image-policy]#kubectl describe replicaset web-no-tag-78bd59988f
- 创建tag的deployment资源测试
[root@k8s-master1 image-policy]#kubectl create deployment web-with-tag --image=nginx:1.16
deployment.apps/web-with-tag created
[root@k8s-master1 image-policy]#kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
web-no-tag 0/1 0 0 2m45s
web-with-tag 1/1 1 1 6s
带有tag的镜像会被成功创建,不带tag的镜像会被ImagePolicyWebhook
拒绝,符合预期。
测试结束。😘
关于我
我的博客主旨:
- 排版美观,语言精炼;
- 文档即手册,步骤明细,拒绝埋坑,提供源码;
- 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!
🍀 微信二维码
x2675263825 (舍得), qq:2675263825。
🍀 微信公众号
《云原生架构师实战》
🍀 语雀
https://www.yuque.com/xyy-onlyone
🍀 csdn
https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421
🍀 知乎
https://www.zhihu.com/people/foryouone
最后
好了,关于本次就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!