1 个不稳定版本
| 0.7.0 | 2020 年 9 月 18 日 |
|---|
#10 in #cloud-formation
10KB
136 行
[预览] AWS CloudFormation Guard
此仓库包含以下工具的源代码
CloudFormation Guard一个 CLI 工具,- 使用简单的、基于策略代码的声明性语法检查 AWS CloudFormation 模板的策略合规性
- 可以从现有的 CloudFormation 模板自动生成规则
CloudFormation Guard Lambda是 CloudFormation Guard 的check功能的 AWS Lambda 版本CloudFormation Guard Rulegen Lambda是 CloudFormation Guard 的rulegen功能的 AWS Lambda 版本
工作原理
检查模板
CloudFormation Guard 使用简单的规则语法,允许您指定您想要(或不需要)在您的 CloudFormation 资源中具有的特性。
例如,给定一个 CloudFormation 模板
{
"Resources": {
"NewVolume" : {
"Type" : "AWS::EC2::Volume",
"Properties" : {
"Size" : 500,
"Encrypted": false,
"AvailabilityZone" : "us-west-2b"
}
},
"NewVolume2" : {
"Type" : "AWS::EC2::Volume",
"Properties" : {
"Size" : 50,
"Encrypted": false,
"AvailabilityZone" : "us-west-2c"
}
}
}
}
和一组规则
let encryption_flag = true
AWS::EC2::Volume Encrypted == %encryption_flag
AWS::EC2::Volume Size <= 100
您可以对模板进行检查,以确保它遵循规则。
$> cfn-guard check -t Examples/ebs_volume_template.json -r Examples/ebs_volume_template.ruleset
[NewVolume2] failed because [Encrypted] is [false] and the permitted value is [true]
[NewVolume] failed because [Encrypted] is [false] and the permitted value is [true]
[NewVolume] failed because [Size] is [500] and the permitted value is [<= 100]
Number of failures: 3
评估安全策略
CloudFormation Guard 可用于评估通过 CloudFormation 部署的基础设施的安全最佳实践。包括了一些示例规则
$> cfn-guard check -t Examples/security_template.json -r Examples/security_rules.ruleset
"[AmazonMQBroker] failed because [AutoMinorVersionUpgrade] is [false] and Version upgrades should be enabled to receive security updates"
"[AmazonMQBroker] failed because [EncryptionOptions.UseAwsOwnedKey] is [true] and CMKs should be used instead of AWS-provided KMS keys"
"[AmazonMQBroker] failed because [EngineVersion] is [5.15.9] and Broker engine version should be at least 5.15.10"
...
有关如何编写规则以及该工具如何与构建系统协同工作的更多详细信息,请参阅此处。
自动生成规则
您还可以使用 CloudFormation Guard 工具从已知良好的 CloudFormation 模板自动生成规则。
使用上面的相同模板,cfn-guard rulegen 将生成
$> cfn-guard rulegen Examples/ebs_volume_template.json
AWS::EC2::Volume Encrypted == false
AWS::EC2::Volume Size == 101 |OR| AWS::EC2::Volume Size == 99
AWS::EC2::Volume AvailabilityZone == us-west-2b |OR| AWS::EC2::Volume AvailabilityZone == us-west-2c
然后,您可以将它们管道输入文件中,并根据需要添加、编辑或删除规则。
作为 AWS Lambda 使用工具
可以通过Lambda 版本检查从工具的命令行版本可以检查的所有内容。同样适用于规则生成功能。
设置它
克隆此仓库
git clone git@github.com:aws-cloudformation/cloudformation-guard.git
或者点击绿色的“克隆或下载”按钮,然后选择“下载 Zip”。解压内容以查看和编译源代码。
安装 Rust
Mac/ Ubuntu
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
如果您还没有安装,请按照 Rust 安装程序的推荐运行以下命令:source $HOME/.cargo/env
更多信息请点击这里
如果在 Ubuntu 上构建,建议运行以下命令:sudo apt-get update; sudo apt install build-essential
Windows 10
- 创建一个 Windows 10 工作空间
- 安装只提供 Visual C++ 建设工具的 Microsoft Visual C++ Build Tools 2019 版本:https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019
- 下载安装程序并运行它。
- 选择“单独组件”选项卡,并勾选“Windows 10 SDK”
- 选择“语言包”选项卡,并确保至少选择了“英语”
- 点击“安装”
- 如果需要,请等待其下载并重新启动
- 安装 rust
- 下载 rust-init.exe
- 运行它并接受默认设置
阅读各个工具的文档
有关如何构建和使用这些工具的详细信息,请参阅每个工具的 README。
CloudFormation Guard Rulegen Lambda
使用 Makefile
到工具链的主要接口是Makefile。要构建二进制文件或部署所需的 lambda,只需运行相应的 make 目标(例如 make cfn-guard-rulegen)。结果二进制文件的副本将被移动到顶层 cfn-guard-toolchain/bin/ 目录。(注意,虽然 bin/ 目录中的文件不受版本控制。)
cfn-guard-lambda 有点复杂,因为它是一个 lambda,而不是二进制文件,因此其 install 和 update 步骤不同。它还需要在您可以从 Makefile 部署之前设置一些东西。(更多信息请参阅其文档)。一旦设置好,它可以从顶层 Makefile 使用 cfn-guard-lambda_install 和 cfn-guard-lambda_update 目标进行部署。
获取分发副本
有两个 make 目标可以将源代码打包,不包括 git 历史等。
make release 将将源代码打包到一个名为 cloudformation-guard.tar.gz 的文件中,不包括 git 历史。
make release_with_binaries 首先会对 cargo build --release 进行操作,针对 cfn-guard-rulegen 和 cfn-guard,目标是运行 make 命令的架构(例如,您的笔记本电脑的操作系统),并将二进制文件放在 cloudformation-guard/bin/ 目录中。然后,它会将它们以及必要的源文件打包成 cloudformation-guard.tar.gz。 (注意:带有二进制文件的 zip 文件可能会被垃圾邮件过滤器阻止。)
附加文档
使用 cfn-guard 方式为 AWS CloudFormation 模板编写预防性合规规则
常见问题解答
Q:为什么你应该使用 Guard?
A:Guard 解决了多个用例
- 它可以用于自动检查 CloudFormation 模板的存储库中的策略违规。
- 它可以是 CI/CD 流程中的部署门。
- 它允许你定义构成有效基础设施定义的单一来源。定义一次规则,并在你的 AWS 账户中的本地和 Lambda 上运行,以便与其他 AWS 服务集成。
- 它允许在部署之前对 CloudFormation 模板资源进行安全检查。你可以要求包含设置,并禁止导致之前问题的配置。
- 它很容易开始。您可以使用 CloudFormation Guard Rulegen 自动生成工具从现有的已知良好模板中提取您想要的规则。
Q:CloudFormation Guard 与其他服务有何关联?
A:Guard 是一个具有检查本地 CloudFormation 模板功能的命令行工具。它还可以作为 AWS Lambda 函数部署,并与其他具有 Lambda 集成的 AWS 服务相关联。
许可证
本项目根据 Apache-2.0 许可证授权。
依赖关系
~2.8–4MB
~72K SLoC