本文將為大家介紹docker守護(hù)進(jìn)程的相關(guān)安全配置項(xiàng)目。
一、測試環(huán)境
1.1 安裝 centos 7
centos linux release 7.7.1908 (core)
升級內(nèi)核,重啟
# yum update kernel
[root@localhost docker]# uname -a
linux localhost 3.10.0-1062.12.1.el7.x86_64 #1 smp tue feb 4 23:02:59 utc 2020 x86_64 x86_64 x86_64 gnu/linux
[root@localhost docker]# cat /etc/redhat-release
centos linux release 7.7.1908 (core)
1.2 安裝 docker ce 19.03
1
2
3
4
5
6
7
|
# yum install -y yum-utils device-mapper-persistent-data lvm2 # yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # yum install -y docker-ce [root@localhost docker]# docker --version docker version 19.03.8, build afacb8b |
二、 守護(hù)進(jìn)程安全配置
默認(rèn)沒有配置文件,需要單獨(dú)創(chuàng)建/etc/docker/daemon.json
,下面配置都是在該文件上進(jìn)行配置,本地的測試示例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
{ "icc": false, "log-level": "info", "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file":"5", "labels": "somelabel", "env": "os,customer" }, "iptables": true, "userns-remap": "default", "userland-proxy": false, "experimental": false, "selinux-enabled": true, "live-restore": true, "no-new-privileges": true, "cgroup-parent": "/foobar", "seccomp-profile": "/etc/docker/seccomp/default-no-chmod.json", "tls": true, "tlsverify": true, "tlscacert": "/etc/docker/ca/ca.pem", "tlscert": "/etc/docker/ca/server-cert.pem", "tlskey": "/etc/docker/ca/server-key.pem" } |
2.1 配置通過 https 和證書認(rèn)證訪問 docker 守護(hù)進(jìn)程
服務(wù)器證書
創(chuàng)建 host,定義域(ip 也可以),會根據(jù)域來生成對應(yīng)的證書,一般用于注冊證書當(dāng)中的 cn:
創(chuàng)建證書目錄:
$ mkdir -p /etc/docker/dockerd/ca && cd /etc/docker/dockerd/ca
生成 key 證書,并填寫兩次 key 證書密碼:
$ openssl genrsa -aes256 -out ca-key.pem 4096
生成 ca 證書,需要輸入注冊證書基礎(chǔ)信息:
$ openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
創(chuàng)建 server 證書:
1
2
3
|
$ openssl genrsa -out server-key.pem 4096 $ openssl req -subj "/cn=localhsot" -sha256 -new -key server-key.pem -out server.csr |
設(shè)定證書指定的 ip 地址:
$ echo subjectaltname = dns:localhost,ip:127.0.0.1 >> extfile.cnf
將 docker 守護(hù)程序密鑰的擴(kuò)展使用屬性設(shè)置為僅用于服務(wù)器身份驗(yàn)證:
$ echo extendedkeyusage = serverauth >> extfile.cnf
生成 server cert 證書:
$ openssl x509 -req -days 3650 -sha256 -in server.csr -ca ca.pem -cakey ca-key.pem -cacreateserial -out server-cert.pem -extfile extfile.cnf
客戶端證書
創(chuàng)建客戶端證書:(還是當(dāng)前目錄)
1
2
|
$ openssl genrsa -out key.pem 4096 $ openssl req -subj '/cn=localhost' -new -key key.pem -out client.csr |
要使密鑰適合客戶端身份驗(yàn)證,請創(chuàng)建擴(kuò)展配置文件:
$ echo extendedkeyusage = clientauth >> extfile.cnf
生成 client cert 證書:
$ openssl x509 -req -days 3650 -sha256 -in client.csr -ca ca.pem -cakey ca-key.pem -cacreateserial -out cert.pem -extfile extfile.cnf
使用
對證書賦予相應(yīng)的權(quán)限:
1
2
3
4
5
|
$ chmod -v 0400 ca-key.pem key.pem server-key.pem $ chmod -v 0444 ca.pem server-cert.pem cert.pem [root@localhost ca]# ls ca-key.pem ca.pem ca.srl cert.pem client.csr extfile.cnf key.pem server-cert.pem server.csr server-key.pem |
服務(wù)端配置 /etc/docker/daemon.json
1
2
3
4
5
|
"tls": true, "tlsverify": true, "tlscacert": "/etc/docker/ca/ca.pem", "tlscert": "/etc/docker/ca/server-cert.pem", "tlskey": "/etc/docker/ca/server-key.pem" |
客戶端配置
設(shè)置客戶端證書到當(dāng)服務(wù)器上,并放置到相應(yīng)的位置:
1
2
|
$ cp -v {ca,cert,key}.pem ~/.docker $ export docker_host=tcp://$host:2376 docker_tls_verify=1 |
通過如下方式模擬測試:
1
2
3
4
5
6
|
$ curl https://$host:2376/images/json \ --cert ~/.docker/cert.pem \ --key ~/.docker/key.pem \ --cacert ~/.docker/ca.pem [{"containers":-1,"created":1540777343,"id":"sha256:55e7b305dc477345434ce3bd3941940481f982eea31c8f28c0670d59c63d544b","labels":nu |
2.2 使用namespace隔離技術(shù)
namespace是一種隔離技術(shù),docker就是使用隔離技術(shù)開啟特定的namespace創(chuàng)建出一些特殊的進(jìn)程,不過使用namespace是有條件的。系統(tǒng)會創(chuàng)建dockremap,通過/etc/subuid和/etc/subuid對應(yīng)的id值,映射到容器中去;實(shí)際情況還是使用的是dockremap普通權(quán)限,達(dá)到自動隔離的效果。
首先修改 /etc/sysctl.conf
# echo "user.max_user_namespaces=15076" >> /etc/sysctl.conf
在 /etc/docker/daemon.json
增加配置項(xiàng) "userns-remap": "default"
修改此項(xiàng)配置需要慎重,如果是已經(jīng)部署了一套docker環(huán)境,啟用此選項(xiàng)后,會切換到隔離環(huán)境,以前的docker容器將無法使用!
1
2
|
[root@localhost docker]# cat /etc/subuid dockremap:100000:65536 |
2.3 設(shè)置 docker 的分區(qū)
為容器創(chuàng)建單獨(dú)的分區(qū),默認(rèn)分區(qū)在\var\lib\docker\
,包含本地鏡像、容器、網(wǎng)絡(luò)等相關(guān)的東西。
[root@localhost docker]# ls /var/lib/docker
100000.100000 builder buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
可以使用 "data-root": "" 配置默認(rèn)的分區(qū)位置。
2.4 限制默認(rèn)網(wǎng)橋容器之間的流量
當(dāng)啟動 docker 服務(wù)時候,默認(rèn)會添加一條轉(zhuǎn)發(fā)策略到 iptables 的 forward 鏈上。策略為通過( accept )還是禁止( drop ),取決于配置 --icc=true (缺省值)還是 --icc=false 。如果手動指定 --iptables=false 則不會添加 iptables 規(guī)則。
默認(rèn)情況下,默認(rèn)網(wǎng)橋上同一主機(jī)上的容器之間允許所有網(wǎng)絡(luò)通信,如果不需要,限制所有容器間的通信。 將需要通信的特定容器鏈接在一起,或者創(chuàng)建自定義網(wǎng)絡(luò),并且僅加入需要與該自定義網(wǎng)絡(luò)進(jìn)行通信的容器。
配置限制默認(rèn)網(wǎng)橋上容器之間的流量 "icc":false
2.5 配置日志
配置集中的遠(yuǎn)程日志,設(shè)置日志進(jìn)程 --log-level 級別為 info ,日志記錄格式 json,本地日志記錄
1
2
3
4
5
6
7
8
|
"log-level": "info", "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file":"5", "labels": "somelabel", "env": "os,customer" }, |
配置遠(yuǎn)程日志
docker 日志記錄驅(qū)動程序接收容器日志并將其轉(zhuǎn)發(fā)到遠(yuǎn)程目標(biāo)或文件。 默認(rèn)的日志記錄驅(qū)動程序是json-file
。 它將容器日志以json格式存儲在本地磁盤上。 docker具有用于記錄日志的插件體系結(jié)構(gòu),因此有用于開源工具和商業(yè)工具的插件:
journald–將容器日志存儲在系統(tǒng)日志中.
syslog driver–支持udp,tcp,tls
fluentd –支持將tcp或unix套接字連接到fluentd
splunk – http / https轉(zhuǎn)發(fā)到splunk服務(wù)器
gelf – udp日志轉(zhuǎn)發(fā)到graylog2
示例 fluent
1
2
3
4
5
6
|
{ "log-driver": "fluentd", "log-opts": { "fluentd-address": "fluentdhost:24224" } } |
使用 syslog
1
2
3
4
5
6
|
{ "log-driver": "syslog", "log-opts": { "syslog-address": "udp://1.2.3.4:1111" } } |
2.6 設(shè)置 ulimit
1
2
3
4
5
6
7
8
9
|
{ "default-ulimits": { "nofile": { "name": "nofile", "hard": 64000, "soft": 64000 } } } |
2.7 設(shè)置 cgroup
--cgroup-parent 選項(xiàng)允許設(shè)置用于容器的默認(rèn)cgroup父級。 如果未設(shè)置此選項(xiàng),則對于fs cgroup驅(qū)動程序,默認(rèn)為 /docker ;對于systemd cgroup驅(qū)動程序,默認(rèn)為 system.slice 。
如果cgroup有一個正斜杠( / ),則cgroup在根cgroup下創(chuàng)建,否則cgroup在守護(hù)程序cgroup下創(chuàng)建。
假設(shè)守護(hù)程序在cgroup daemoncgroup中運(yùn)行,則 --cgroup-parent=/foobar 在 /sys/fs/cgroup/memory/foobar 中創(chuàng)建一個cgroup,而使用 --cgroup-parent=foobar 則創(chuàng)建 /sys/fs/cgroup/memory/daemoncgroup/foobar
中創(chuàng)建 cgroup。
systemd cgroup驅(qū)動程序?qū)?ndash;cgroup-parent具有不同的規(guī)則。 systemd按切片表示層次結(jié)構(gòu),切片的名稱對樹中的位置進(jìn)行編碼。 因此,systemd cgroup的 --cgroup-parent 應(yīng)為切片名稱。 名稱可以包含一系列用短劃線分隔的名稱,這些名稱描述了從根切片到切片的路徑。 例如, --cgroup-parent=user-a-b.slice 表示容器的內(nèi)存cgroup在 /sys/fs/cgroup/memory/user.slice/user-a.slice/user-a-b.slice/docker-<id>.scope
中創(chuàng)建。
也可以使用容器運(yùn)行來設(shè)置,使用docker create和docker run上的 --cgroup-parent 選項(xiàng),會優(yōu)先于守護(hù)程序上的 --cgroup-parent 選項(xiàng)。
2.8 配置 seccomp
使用的測試配置文件,禁止在 docker 里使用 chmod 命令
1
2
3
4
5
6
7
|
https://github.com/docker/labs/blob/master/security/seccomp/seccomp-profiles/default-no-chmod.json [root@localhost docker]# docker run --rm -it alpine sh / # ls bin etc lib mnt proc run srv tmp var dev home media opt root sbin sys usr / # touch foo.sh / # chmod +x foo.sh chmod: foo.sh: operation not permitted / # exit |
實(shí)際可以完成禁止、允許、告警某些系統(tǒng)相關(guān)的調(diào)用,參考:https://github.com/torvalds/linux/blob/master/arch/x86/entry/syscalls/syscall_64.tbl
2.9 配置支持無守護(hù)程序的容器
--live-restore 確保 docker 守護(hù)進(jìn)程關(guān)閉時不影響容器。
測試時再關(guān)閉 docker 守護(hù)進(jìn)程后,nginx 容器仍能正常提供訪問。
2.10 禁用 docker 的實(shí)驗(yàn)性功能
設(shè)置 "experimental": false
2.11 限制容器通過 suid 或 sgid 提權(quán)
no-new-privileges 安全選項(xiàng)可防止容器內(nèi)的應(yīng)用程序進(jìn)程在執(zhí)行期間獲得新的特權(quán)。
舉例:有一個在映像中設(shè)置了 setuid/setgid 位的程序,例如sudo,容器中的進(jìn)程也具有執(zhí)行該程序的(文件)權(quán)限,試圖通過諸如setuid/setgid之類的設(shè)施獲取特權(quán)的任何操作將被拒絕。
三、守護(hù)進(jìn)程配置示例說明(linux)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
{ "authorization-plugins": [],//訪問授權(quán)插件 "data-root": "",//docker數(shù)據(jù)持久化存儲的根目錄,默認(rèn)為/var/lib/docker "dns": [],//dns服務(wù)器 "dns-opts": [],//dns配置選項(xiàng),如端口等 "dns-search": [],//dns搜索域名 "exec-opts": [],//執(zhí)行選項(xiàng) "exec-root": "",//執(zhí)行狀態(tài)的文件的根目錄 "experimental": false,//是否開啟試驗(yàn)性特性 "features": {},//啟用或禁用特定功能。如:{"buildkit": true}使buildkit成為默認(rèn)的docker鏡像構(gòu)建器。 "storage-driver": "",//存儲驅(qū)動器類型 "storage-opts": [],//存儲選項(xiàng) "labels": [],//鍵值對式標(biāo)記docker元數(shù)據(jù) "live-restore": true,//dockerd掛掉是否保活容器(避免了docker服務(wù)異常而造成容器退出) "log-driver": "json-file",//容器日志的驅(qū)動器 "log-opts": { "max-size": "10m", "max-file":"5", "labels": "somelabel", "env": "os,customer" },//容器日志的選項(xiàng) "mtu": 0,//設(shè)置容器網(wǎng)絡(luò)mtu(最大傳輸單元) "pidfile": "",//daemon pid文件的位置 "cluster-store": "",//集群存儲系統(tǒng)的url "cluster-store-opts": {},//配置集群存儲 "cluster-advertise": "",//對外的地址名稱 "max-concurrent-downloads": 3,//設(shè)置每個pull進(jìn)程的最大并發(fā) "max-concurrent-uploads": 5,//設(shè)置每個push進(jìn)程的最大并發(fā) "default-shm-size": "64m",//設(shè)置默認(rèn)共享內(nèi)存的大小 "shutdown-timeout": 15,//設(shè)置關(guān)閉的超時時限 "debug": true,//開啟調(diào)試模式 "hosts": [],//dockerd守護(hù)進(jìn)程的監(jiān)聽地址 "log-level": "",//日志級別 "tls": true,//開啟傳輸層安全協(xié)議tls "tlsverify": true,//開啟輸層安全協(xié)議并驗(yàn)證遠(yuǎn)程地址 "tlscacert": "",//ca簽名文件路徑 "tlscert": "",//tls證書文件路徑 "tlskey": "",//tls密鑰文件路徑 "swarm-default-advertise-addr": "",//swarm對外地址 "api-cors-header": "",//設(shè)置cors(跨域資源共享-cross-origin resource sharing)頭 "selinux-enabled": false,//開啟selinux(用戶、進(jìn)程、應(yīng)用、文件的強(qiáng)制訪問控制) "userns-remap": "",//給用戶命名空間設(shè)置 用戶/組 "group": "",//docker所在組 "cgroup-parent": "",//設(shè)置所有容器的cgroup的父類 "default-ulimits": { "nofile": { "name": "nofile", "hard": 64000, "soft": 64000 } },//設(shè)置所有容器的ulimit "init": false,//容器執(zhí)行初始化,來轉(zhuǎn)發(fā)信號或控制(reap)進(jìn)程 "init-path": "/usr/libexec/docker-init",//docker-init文件的路徑 "ipv6": false,//支持ipv6網(wǎng)絡(luò) "iptables": false,//開啟防火墻規(guī)則 "ip-forward": false,//開啟net.ipv4.ip_forward "ip-masq": false,//開啟ip掩蔽(ip封包通過路由器或防火墻時重寫源ip地址或目的ip地址的技術(shù)) "userland-proxy": false,//用戶空間代理 "userland-proxy-path": "/usr/libexec/docker-proxy",//用戶空間代理路徑 "ip": "0.0.0.0",//默認(rèn)ip "bridge": "",//將容器依附(attach)到橋接網(wǎng)絡(luò)上的橋標(biāo)識 "bip": "",//指定橋接ip "fixed-cidr": "",//(ipv4)子網(wǎng)劃分,即限制ip地址分配范圍,用以控制容器所屬網(wǎng)段實(shí)現(xiàn)容器間(同一主機(jī)或不同主機(jī)間)的網(wǎng)絡(luò)訪問 "fixed-cidr-v6": "",//(ipv6)子網(wǎng)劃分 "default-gateway": "",//默認(rèn)網(wǎng)關(guān) "default-gateway-v6": "",//默認(rèn)ipv6網(wǎng)關(guān) "icc": false,//容器間通信 "raw-logs": false,//原始日志(無顏色、全時間戳) "allow-nondistributable-artifacts": [],//不對外分發(fā)的產(chǎn)品提交的registry倉庫 "registry-mirrors": [],//registry倉庫鏡像加速地址 "seccomp-profile": "",//seccomp配置文件 "insecure-registries": [],//配置非https的registry地址 "no-new-privileges": false,//禁止新優(yōu)先級 "default-runtime": "runc",//oci聯(lián)盟(the open container initiative)默認(rèn)運(yùn)行時環(huán)境 "oom-score-adjust": -500,//內(nèi)存溢出被殺死的優(yōu)先級(-1000~1000) "node-generic-resources": ["nvidia-gpu=uuid1", "nvidia-gpu=uuid2"],//對外公布的資源節(jié)點(diǎn) "runtimes": { "cc-runtime": { "path": "/usr/bin/cc-runtime" }, "custom": { "path": "/usr/local/bin/my-runc-replacement", "runtimeargs": [ "--debug" ] } },//運(yùn)行時 "default-address-pools":[ {"base":"172.80.0.0/16","size":24},//默認(rèn)的dhcp分配地址 {"base":"172.90.0.0/16","size":24} ] } |
總結(jié)
到此這篇關(guān)于docker守護(hù)進(jìn)程安全配置項(xiàng)目詳解的文章就介紹到這了,更多相關(guān)docker守護(hù)進(jìn)程內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.freebuf.com/articles/es/230587.html