安全性对任何产品来说都非常重要,比如著名的HeartBleed就就曾经给很多忽视安全问题的企业带来了很大的影响。而随着容器化的推进,早在2015年的一次调查中,研究者就曾发现取样的Dockerhub上有30%-40%的镜像存在安全性的问题。Clair正是由coreos所推出的这样一款针对容器的安全扫描的工具,类似于Docker在其收费版中提供的功能那样,能对应用容器的脆弱性进行静态扫描,同时支持APPC和DOCKER。
项目 | 详细 |
---|---|
项目地址 | https://github.com/coreos/clair |
随着容器化的逐渐推进,使用的安全性也受到越来越多地重视。在很多场景下,都需要对容器的脆弱性进行扫描,比如
项目 | 详细 |
---|---|
镜像来源不明 | 在互联网上下载的镜像,可以直接使用,非常的方便,但是是否真正安全还非常难说 |
生产环境的实践 | 容器上到生产环境之后,生产环境对容器的安全性要求一般较高,此时需要容器的安全性得到保证 |
clair的目标是能够从一个更加透明的维度去看待基于容器化的基础框架的安全性。Clair=clear + bright + transparent
通过对容器的layer进行扫描,发现漏洞并进行预警,其使用数据是基于Common Vulnerabilities and Exposures数据库(简称CVE), 各Linux发行版一般都有自己的CVE源,而Clair则是与其进行匹配以判断漏洞的存在与否,比如HeartBleed的CVE为:CVE-2014-0160。
目前Clair支持如下数据源:
数据源 | 具体数据 | 格式 | License |
---|---|---|---|
Debian Security Bug Tracker | Debian 6, 7, 8, unstable namespaces | dpkg | Debian |
Ubuntu CVE Tracker | Ubuntu 12.04, 12.10, 13.04, 14.04, 14.10, 15.04, 15.10, 16.04 namespaces | dpkg | GPLv2 |
Red Hat Security Data | CentOS 5, 6, 7 namespaces | rpm | CVRF |
Oracle Linux Security Data | Oracle Linux 5, 6, 7 namespaces | rpm | CVRF |
Alpine SecDB | Alpine 3.3, Alpine 3.4, Alpine 3.5 namespaces | apk | MIT |
NIST NVD | Generic Vulnerability Metadata | N/A | Public Domain |
Clair的运行需要一个数据库实例,目前经过测试的数据库的版本为
数据库 | 版本 |
---|---|
postgresql | 9.6 |
postgresql | 9.5 |
postgresql | 9.4 |
因为clair以镜像的方式进行打包和发布,所以其运行支持如下几种方式:
本文使用docker-compose来演示起使用方法,其他方式大同小异。
[root@liumiaocn ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Go version: go1.7.5
Git commit: 092cba3
Built: Wed Feb 8 08:47:51 2017
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 092cba3
Built: Wed Feb 8 08:47:51 2017
OS/Arch: linux/amd64
Experimental: false
[root@liumiaocn ~]#
[root@liumiaocn ~]# docker-compose version
docker-compose version 1.14.0, build c7bdf9e
docker-py version: 2.4.2
CPython version: 2.7.5
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
[root@liumiaocn ~]#
使用docker-compose方式运行Clair,下载相关的yaml文件
[root@liumiaocn ~]# curl -L https://raw.githubusercontent.com/coreos/clair/master/docker-compose.yml -o $HOME/docker-compose.yml
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 497 100 497 0 0 450 0 0:00:01 0:00:01 --:--:-- 453
[root@liumiaocn ~]# ls
anaconda-ks.cfg docker-compose.yml
[root@liumiaocn ~]#
确认内容,可以看到postgresql和clair的设定以及券和端口的设定,以及依赖的config.yaml文件
[root@liumiaocn ~]# cat docker-compose.yml
version: '2'
services:
postgres:
container_name: clair_postgres
image: postgres:latest
restart: unless-stopped
environment:
POSTGRES_PASSWORD: password
clair:
container_name: clair_clair
image: quay.io/coreos/clair-git:latest
restart: unless-stopped
depends_on:
- postgres
ports:
- "6060-6061:6060-6061"
links:
- postgres
volumes:
- /tmp:/tmp
- ./clair_config:/config
command: [-config, /config/config.yaml]
[root@liumiaocn ~]#
注意: 镜像有时会发现很难下载下来,尤其是clair-git的镜像,因此使用liumiaocn/clair-git的镜像作为替代(此镜像仅仅是搬运而已,未作任何自定义)
[root@liumiaocn ~]# grep clair-git *.yml
image: liumiaocn/clair-git:latest
[root@liumiaocn ~]#
[root@liumiaocn ~]# mkdir $HOME/clair_config
[root@liumiaocn ~]# ls
anaconda-ks.cfg clair_config docker-compose.yml
[root@liumiaocn ~]#
[root@liumiaocn ~]# curl -L https://raw.githubusercontent.com/coreos/clair/master/config.example.yaml -o $HOME/clair_config/config.yaml
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2810 100 2810 0 0 1431 0 0:00:01 0:00:01 --:--:-- 1438
[root@liumiaocn ~]# ls clair_config/
config.yaml
[root@liumiaocn ~]#
[root@liumiaocn ~]# cp clair_config/config.yaml clair_config/config.yaml.org
[root@liumiaocn ~]# vi clair_config/config.yaml
[root@liumiaocn ~]# diff clair_config/config.yaml clair_config/config.yaml.org
23,24c23
< #source: host=localhost port=5432 user=postgres sslmode=disable statement_timeout=60000
< source: postgresql://postgres:password@postgres:5432?sslmode=disable
---
> source: host=localhost port=5432 user=postgres sslmode=disable statement_timeout=60000
[root@liumiaocn ~]#
使用 docker-compose -f $HOME/docker-compose.yml up -d启动clair, 第一次如果本地没有相关镜像,会自动去pull该镜像。
[root@liumiaocn ~]# docker-compose -f $HOME/docker-compose.yml up -d
Creating network "root_default" with the default driver
Pulling postgres (postgres:latest)...
latest: Pulling from library/postgres
ad74af05f5a2: Pull complete
8996b4a29b2b: Pull complete
bea3311ef15b: Pull complete
b1b9eb0ac9c8: Pull complete
1d6d551d6af0: Pull complete
ba16377760f9: Pull complete
2b514f6e3ef7: Pull complete
0e114ac261be: Pull complete
eb5c1803318c: Pull complete
2969cff5710e: Pull complete
78a73d03b014: Pull complete
d38698b91d10: Pull complete
efdd4c790d34: Pull complete
Digest: sha256:07d178f4b071971208b912f888c0375838435a06728c431f8a1ef3cc04cdee8d
Status: Downloaded newer image for postgres:latest
Pulling clair (liumiaocn/clair-git:latest)...
latest: Pulling from liumiaocn/clair-git
019300c8a437: Already exists
ba570f08729a: Pull complete
65f959ba8ec2: Pull complete
43126a124ea6: Pull complete
70c83f72e91e: Pull complete
edfbbcd5d91d: Pull complete
a3ed95caeb02: Pull complete
db01cda90a09: Pull complete
ad9b36e2881f: Pull complete
988d3a801f0c: Pull complete
Digest: sha256:1e73a24713bae3a6dc45945bd7914bc85d00a55fc9b14dd01880f45b6cd06aa7
Status: Downloaded newer image for liumiaocn/clair-git:latest
Creating clair_postgres ...
Creating clair_postgres ... done
Creating clair_clair ...
Creating clair_clair ... done
[root@liumiaocn ~]#
[root@liumiaocn ~]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------
clair_clair /clair -config /config/con ... Up 0.0.0.0:6060->6060/tcp, 0.0.0.0:6061->6061/tcp
clair_postgres docker-entrypoint.sh postgres Up 5432/tcp
[root@liumiaocn ~]#
Clair提供Restful服务,但是演示略显繁琐,可以通过辅助工具比如clairctl来实现。
下载go的tar包,解压到/usr/local下即可
[root@liumiaocn ~]# ls /usr/local/go
api AUTHORS bin blog CONTRIBUTING.md CONTRIBUTORS doc favicon.ico lib LICENSE misc PATENTS pkg README.md robots.txt src test VERSION
[root@liumiaocn ~]# export PATH=$PATH:/usr/local/go/bin
[root@liumiaocn ~]# export GOROOT=/usr/local/go
[root@liumiaocn ~]# go version
go version go1.8.3 linux/amd64
[root@liumiaocn ~]#
analyze-local-images是一个用来与clair进行交互分析本地镜像的工具,由于使用上有很多限制,已经不再建议使用,而且不再更新,详细参看如下说明
项目 | 详细 |
---|---|
地址 | https://github.com/coreos/analyze-local-images |
下载如下go的package
项目 | 详细 |
---|---|
地址 | https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz |
解压目录 | /usr/local/go |
PATH | export PATH=$PATH:/usr/local/go/bin |
GOROOT | export GOROOT=/usr/local/go |
确认结果如下
[root@ku8-1 ~]# go version
go version go1.8.3 linux/amd64
[root@ku8-1 ~]#
curl https://glide.sh/get | sh
git clone git@github.com:jgsqware/clairctl.git $GOPATH/src/github.com/jgsqware/clairctl
cd $GOPATH/src/github.com/jgsqware/clairctl
glide install -v
go generate ./clair
go build
curl -L https://raw.githubusercontent.com/jgsqware/clairctl/master/install.sh | sh
[root@liumiaocn ~]# clairctl version
Clairctl version 1.2.8
[root@liumiaocn ~]#
如果显示✔,则说明clairctl和clair能够进行正常连接。
[root@liumiaocn ~]# clairctl health
Clair: ✔
[root@liumiaocn ~]#
[root@liumiaocn ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
94ed0c431eb5: Pull complete
9406c100a1c3: Pull complete
aa74daafd50c: Pull complete
Digest: sha256:788fa27763db6d69ad3444e8ba72f947df9e7e163bad7c1f5614f8fd27a311c3
Status: Downloaded newer image for nginx:latest
[root@liumiaocn ~]# docker images |grep nginx |grep latest
nginx latest b8efb18f159b 11 days ago 107 MB
[root@liumiaocn ~]#
[root@liumiaocn ~]# clairctl push -l nginx
nginx:latest has been pushed to Clair
[root@liumiaocn ~]#
使用analyze命令可以对镜像进行分析,如下可以显示对于pull下来的nginx的镜像相关的分析结果如下:
[root@liumiaocn ~]# clairctl analyze -l nginx
Image: /nginx:latest
Unknown: 4
Negligible: 25
Low: 3
Medium: 16
High: 4
Critical: 0
Defcon1: 0
[root@liumiaocn ~]#
如果希望堪当更加详细的信息,可以使用report命令
[root@liumiaocn ~]# clairctl report -l nginx
HTML report at reports/html/analysis-nginx-latest.html
[root@liumiaocn ~]#
这篇文章介绍了Clair的使用方式,了解到了Clair的原理,并使用clairctl的客户端演示了如何利用clair进行镜像的安全扫描并获取扫描的报告信息。容器作为目前比较热门的一种虚拟化的技术虽然已经开始被广泛地使用,但是安全性方面还是有很多事情需要做,而这些也是不容忽视的重要内容。