17

Dockerfile 安全最佳实践

 3 years ago
source link: https://www.infoq.cn/article/P07y163CttcsqLbaa2Uk
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

容器安全涉及问题很多,有许多“唾手可得”的方案能用来降低风险。不过,一个好的开始是编写 Dockerfile 文件时遵循一些规则。

在本文,我列出了一些常见的安全问题和如何规避它们。对于每一个问题,我还写了一个开放策略代理( Open Policy Agent ,OPA)规则来使用 conftest 静态分析你的 Dockerfile 文件。

你可以在 这个库 找到 .rego 规则集。

不要在环境变量中存放密钥

密钥部署是一个很棘手的问题,而且很容易出错。对于容器化的应用程序,可以通过挂载卷从文件系统中显示它们,也可以更方便地通过环境变量显示。

使用 ENV 来存储密钥通常是不好的,因为 Dockerfile 文件通常与应用程序一起部署,因此这与在代码中硬编码密钥没有什么差别。

如何检测这一点:

secrets_env = [

"passwd",

"password",

"pass",

# "pwd", can't use this one

"secret",

"key",

"access",

"api_key",

"apikey",

"token",

"tkn"

]

deny[msg] {

input[i].Cmd == "env"

val := input[i].Value

contains(lower(val[_]), secrets_env[_])

msg = sprintf("Line %d: Potential secret in ENV key found: %s", [i, val])

}

复制代码

只使用信任的根镜像

针对容器化应用程序的攻击链也来自构建容器本身所使用的层次结构。其中,主要的罪魁祸首明显是使用的根镜像。不受信的根镜像是一个高风险,任何时候都应该避免使用。

Docker 为大多数使用的操作系统和应用程序提供了 一组官方根镜像 。使用这些镜像,我们通过 Docker 自身分担的一些责任降低了协议风险。

如何检测这一点:

deny[msg] {

input[i].Cmd == "from"

val := split(input[i].Value[0], "/")

count(val) > 1

msg = sprintf("Line %d: use a trusted base image", [i])

}

复制代码

这条规则针对的是 DockerHub 的官方镜像。由于我只检测到了 namespace 的缺失,这是非常愚蠢的。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK