饮墨

子安饮墨馀三斗,留与卿儿作赋来

用 Steampipe 以 SQL 查询云资源与安全合规:运维人的瑞士军刀,5 分钟上手实操

212 views

痛点:云资源查询散落在各种 CLI 和 Console 里

运维日常离不开这些场景:

  • "哪些 EC2 实例没打特定标签?" — 要写 aws ec2 describe-instances 加一堆 --filters 和 jq
  • "跨 3 个账号找出所有公开的 S3 Bucket" — 每个账号切一遍 profile,脚本写半天
  • "安全审计要检查所有 Security Group 是否开放了 0.0.0.0/0" — 手动翻 Console 翻到崩溃

核心问题:云资源数据分散在不同 API、不同格式里,缺少统一查询层。AWS CLI 输出 JSON 嵌套很深,jq 写复杂过滤几乎不可读,更别提跨云、跨服务的关联查询。

方案:Steampipe — 把云 API 变成 SQL 表

Steampipe 是一个开源工具,核心思路极简:将任意 API 映射为 PostgreSQL 虚拟表,用标准 SQL 查询。

架构原理:

┌─────────────┐     ┌──────────────────┐     ┌─────────────────┐
│  SQL Query  │────▶│  Steampipe Engine │────▶│  Plugin (API)   │
│  (psql/CLI) │◀────│  (嵌入式 Postgres)│◀────│  AWS/GCP/Azure  │
└─────────────┘     └──────────────────┘     └─────────────────┘

关键特性: - 100+ 插件:AWS、GCP、Azure、Kubernetes、GitHub、Cloudflare、Datadog 等 - 标准 SQL:支持 JOIN、聚合、子查询,复杂过滤一条语句搞定 - 实时查询:不需要提前同步数据,查询时实时调 API - 合规检查内置:官方提供 CIS Benchmark、SOC 2、PCI DSS 等合规 Mod

实操步骤

第 1 步:安装 Steampipe

# Linux / macOS 一键安装
sudo /bin/sh -c "$(curl -fsSL https://steampipe.io/install/steampipe.sh)"

# 验证安装
steampipe --version

安装后 Steampipe 会内嵌一个 PostgreSQL 实例(无需额外安装),端口默认 9193。

第 2 步:安装插件并配置凭据

以 AWS 为例:

# 安装 AWS 插件
steampipe plugin install aws

# 插件会自动读取 ~/.aws/credentials 和环境变量
# 多账号配置:编辑 ~/.steampipe/config/aws.spc

多账号配置示例(~/.steampipe/config/aws.spc):

connection "aws_prod" {
  plugin  = "aws"
  profile = "production"
  regions = ["us-east-1", "ap-northeast-1"]
}

connection "aws_dev" {
  plugin  = "aws"
  profile = "development"
  regions = ["us-east-1"]
}

# 聚合连接:跨账号查询
connection "aws_all" {
  plugin      = "aws"
  type        = "aggregator"
  connections = ["aws_prod", "aws_dev"]
}

第 3 步:用 SQL 查询云资源

启动交互式查询:

steampipe query

示例 1:找出所有未加密的 EBS 卷

select
  volume_id,
  size,
  state,
  encrypted,
  account_id,
  region
from
  aws_all.aws_ebs_volume
where
  not encrypted;

示例 2:列出所有对公网开放 22 端口的安全组

select
  group_name,
  group_id,
  ip_permission -> 'IpRanges' as ip_ranges,
  account_id
from
  aws_all.aws_vpc_security_group,
  jsonb_array_elements(ip_permissions) as ip_permission
where
  ip_permission ->> 'FromPort' = '22'
  and ip_permission -> 'IpRanges' @> '[{"CidrIp": "0.0.0.0/0"}]';

示例 3:跨服务关联 — 找出挂载了公网 IP 但没有关联安全组限制的 EC2

select
  i.instance_id,
  i.public_ip_address,
  i.instance_state,
  sg ->> 'GroupName' as security_group
from
  aws_all.aws_ec2_instance as i,
  jsonb_array_elements(security_groups) as sg
where
  i.public_ip_address is not null
order by
  i.instance_id;

第 4 步:运行合规基准检查

Steampipe 提供 Mod(合规检测包),一键执行标准合规检查:

# 安装 AWS CIS Benchmark Mod
mkdir -p ~/steampipe-checks && cd ~/steampipe-checks
steampipe mod init
steampipe mod install github.com/turbot/steampipe-mod-aws-compliance

# 运行 CIS v2.0 基准检查
steampipe check aws_compliance.benchmark.cis_v200 --output=brief

输出示例:

CIS v2.0.0 > 1 Identity and Access Management
  ✅ 1.1 Maintain current contact details ............................ OK
  ❌ 1.4 Ensure no root access key exists ............................ ALARM (2 access keys found)
  ✅ 1.5 Ensure MFA is enabled for root ............................... OK
  ❌ 1.12 Ensure credentials unused 45+ days are disabled ............ ALARM (3 users)

支持导出为 CSV、JSON、HTML 报告,适合定期发给安全团队。

避坑指南

坑 1:大规模查询超时或被限流

Steampipe 实时调 API,如果查询命中上千资源,可能触发 API Rate Limit。

解法: - 在 .spc 配置中设置 max_error_retry_attempts = 9 增加重试 - 查询时加 where region = 'us-east-1' 缩小范围 - 对高频查询启用缓存:steampipe query --cache-ttl 300(缓存 5 分钟)

坑 2:聚合连接性能陷阱

aggregator 类型连接会并行查所有子连接,20 个账号 × 15 个 Region = 300 次 API 调用。

解法: - 按需配置 regions 只包含实际使用的区域 - 聚合连接的 where 条件会下推到各子连接,务必带过滤条件

坑 3:与现有工具集成的正确姿势

Steampipe 内嵌 PostgreSQL 兼容接口,可以直接用 psql 或任何 PostgreSQL 客户端连接:

# 作为后台服务启动
steampipe service start

# 用 psql 连接(默认端口 9193)
psql -h localhost -p 9193 -U steampipe -d steampipe

# 用 Grafana 做可视化:添加 PostgreSQL 数据源指向 localhost:9193

这意味着你可以: - 用 Grafana 做云资源仪表盘 - 用 Python psycopg2 写自动化脚本 - 接入现有告警系统做资源合规实时监测

总结

维度 传统方式 (CLI + jq) Steampipe
学习成本 每个云 CLI 语法不同 统一 SQL,会写 SELECT 就行
跨账号/跨云 脚本循环切 profile aggregator 连接一条 SQL
合规检查 自己写脚本 + 维护规则 官方 Mod,CIS/SOC2/PCI 开箱即用
集成性 输出 JSON 需二次处理 PostgreSQL 协议,接 Grafana/Python 直连

核心结论: Steampipe 把云资源查询和安全合规检查的门槛从"写脚本"降到了"写 SQL"。对于管理多账号、需要定期安全审计的运维团队,投入 5 分钟安装就能替代大量定制脚本。

推荐实践: 1. 先从单账号 + steampipe check 合规检查开始,立即产出安全报告 2. 逐步添加多账号聚合连接,统一管理视图 3. 接入 Grafana 做持续可视化,配合 cron 定时输出合规报告到 Slack/钉钉

分享:

相关文章