1 Chef InSpec 介绍
任何业务都依赖于基础设施环境。近年来,基础设施领域已发生了巨大的变化。从最初的传统数据中心到数据中心托管服务,而如今,基础设施即服务和云平台在企业中更受欢迎。因此,大多数企业正在将其工作负载从本地数据中心迁移到云平台。
为了部署和管理这些复杂的基础设施配置,很多企业使用代码即基础设施(IaC)。当您使用 IaC 在 AWS 云平台上创建成百上千个资源时,如何知道代码按预期方式部署了这些资源?这些资源是否遵守合规性和安全性措施?我们并不确定,因此需要基础设施测试程序。
Chef InSpec 是一个用于测试和审核应用程序和基础设施的开源代码框架。
Chef InSpec 的工作原理是将系统的实际状态与您编写的 Chef InSpec 代码表达的期望状态进行比较。Chef InSpec 可以检测违规并以报告的形式显示。
2 Chef InSpec 入门
下面将主要介绍 Chef InSpec 的安装及环境配置。
2.1 Chef InSpec 安装
Chef InSpec 提供了适配多种操作系统环境的安装包,支持的操作系统类型如下:
Amazon Linux
Debian GNU/Linux
Red Hat Enterprise Linux/CentOS
macOS
SUSE Linux Enterprise Server
Ubuntu Linux
Windows
软件包的下载地址如下:
https://downloads.chef.io/products/inspec
本文以 Amazon Linux 环境为例进行说明,下载当前最新的 Chef InSpec 软件包,使用下面的命令进行 Chef InSpec 安装:
rpm -ivh inspec-<version>.x86_64.rpm安装完成后可以看到 inspec 命令可以正常运行,并提示 inspec 命令可以使用的参数帮助信息

2.2 Chef InSpec 配置介绍
使用 Chef InSpec 时主要需要弄清楚下面的三部分:
Command-line interface
Profile
Resource
InSpec CLI 使用 local,SSH,WinRM,Docker 或 AWS 等连接方式,可针对不同基础设施目标运行测试和审核。常用命令说明如下:
Chef InSpec 支持创建复杂的测试和合规性配置文件,每个 profile 文件都是独立的结构,具有自己的执行流程。用户可以自己编写 profile 文件,还可以在社区支持的 Chef Supermarket 和 GitHub 上找到别人写的 profile。
Profile 文件通常具有下面的目录结构:
examples/profile├── README.md├── controls│ ├── example.rb├── libraries│ └── extension.rb└── inspec.yml其中各个文件和目录的说明如下:
md 是用于解释 profile 的适用范围和用法
controls 是所有测试用例所在的目录
libraries 是用于存放扩展 Chef InSpec 资源代码的目录
yml 包含配置文件的描述
Chef InSpec 拥有 80 多种可供使用的资源。如果您需要的资源尚未提供,也可以编写自己的自定义资源。Chef InSpec 支持的资源列表如下:
https://docs.chef.io/inspec/resources/
2.3 使用 Chef InSpec 测试 AWS 资源
典型的使用环境如下图所示,在数据中心内使用 Chef InSpec 对 AWS 进行基础设施资源测试。

从本地数据中心连接 AWS 环境需要使用 Access Key 和 Secret Access Key(AKSK)。在企业环境中,通常会将 IAM User 集中在一个 Master 账号中进行管理,用户使用 IAM User 登陆 AWS 环境,然后再用 Assume Role 的方式操作其它 AWS 账户。
使用 Chef InSpec 连接 AWS 前,首先需要创建 IAM 用户,并生成 AKSK。另外在 UseCase 账号中,需要创建 Assume Role,给予适当的权限并允许 Master 账号中的 IAM User 切换到 Use Case 账号。
在本地数据中心连接 AWS 环境的配置示例如下:
# cat .aws/credentials [default]aws_access_key_id = <Access Key>aws_secret_access_key = <Secret Access Key># cat ~/.aws/config [default]region = cn-north-1output = json[profile usecase]role_arn = <UseCase Account IAM Role Arn>使用如下命令创建示例 profile,该命令会自动生成 profile 目录结构。
# inspec init profile --platform aws example ─────────────────────────── InSpec Code Generator ─────────────────────────── Creating new profile at /root/example • Creating file README.md • Creating file attributes.yml • Creating directory controls • Creating file controls/example.rb • Creating file inspec.yml示例代码的测试内容在 controls/example.rb 文件中
# copyright: 2018, The Authorstitle "Sample Section"aws_vpc_id = attribute("aws_vpc_id", value: "", description: "Optional AWS VPC identifier.")# You add controls herecontrol "aws-single-vpc-exists-check" do # A unique ID for this control. only_if { aws_vpc_id != "" } # Only run this control if the `aws_vpc_id` attribute is provided. impact 1.0 # The criticality, if this control fails. title "Check to see if custom VPC exists." # A human-readable title. describe aws_vpc(aws_vpc_id) do # The test itself. it { should exist } endend# Plural resources can be inspected to check for specific resource details.control "aws-vpcs-check" do impact 1.0 title "Check in all the VPCs for default sg not allowing 22 inwards" aws_vpcs.vpc_ids.each do |vpc_id| describe aws_security_group(vpc_id: vpc_id, group_name: "default") do it { should allow_in(port: 22) } end endendcontrol "aws-vpcs-multi-region-status-check" do # A unique ID for this control. impact 1.0 # The criticality, if this control fails. title 'Check AWS VPCs in all regions have status "available"' # A human-readable title. aws_regions.region_names.each do |region| # Loop over all available AWS regions aws_vpcs(aws_region: region).vpc_ids.each do |vpc| # Find all VPCs in a single AWS region describe aws_vpc(aws_region: region, vpc_id: vpc) do # The test itself. it { should exist } # Confirms AWS VPC exists it { should be_available } # Confirms AWS VPC has status "available" end end endend该测试的主要内容包含:
如果传入指定的 VPC ID,则检查指定的 VPC 是否存在
检查所有 VPC 的安全组内是否包含允许 22 端口访问的策略
检查所有区域内的状态为 available 的 VPC
执行如下命令运行该 profile 对 AWS 环境进行测试:
# inspec exec example -t aws://cn-north-1/usecaseProfile: AWS InSpec Profile (example)Version: 0.1.0Target: aws://cn-north-1 ↺ aws-single-vpc-exists-check: Check to see if custom VPC exists. ↺ Skipped control due to only_if condition. ✔ aws-vpcs-check: Check in all the VPCs for default sg not allowing 22 inwards ✔ EC2 Security Group ID: sg-01d67dds39bcffe17 Name: default VPC ID: vpc-032cdd155d561c7bf is expected to allow in {:port=>22} ✔ aws-vpcs-multi-region-status-check: Check AWS VPCs in all regions have status "available" ✔ VPC vpc-032cdd155d561c7bf in cn-north-1 is expected to exist ✔ VPC vpc-032cdd155d561c7bf in cn-north-1 is expected to be availableProfile: Amazon Web Services Resource Pack (inspec-aws)Version: 1.31.0Target: aws://cn-north-1 No tests executed.Profile Summary: 2 successful controls, 0 control failures, 1 control skippedTest Summary: 3 successful, 0 failures, 1 skipped3 总结
在实际的应用场景中,可以将 Chef InSpec 用于 Terraform 等 IaC 工具进行环境部署之后的测试与审核,另外,还可以将 Chef InSpec 与 Jenkins 等 CI/CD 工具集合使用,以持续测试您部署的基础架构,以确保其符合合规性策略。
作者介绍:
卢冲 – 亚马逊 AWS 专业服务团队云架构咨询顾问。负责企业级客户的云架构设计和优化、云上运维咨询和技术实施。曾就职于 Novell、RedHat 等企业,具有多年企业级客户项目实施经验。
本文转载自亚马逊 AWS 官方博客。
原文链接:





