theusc6/cloud-platform-engineering
GitHub: theusc6/cloud-platform-engineering
一个基于 Python 的自动化框架,用于在 AWS 上大规模执行安全合规的扫描、修复与报告。
Stars: 0 | Forks: 0
# 云平台工程

**自动化的 AWS 安全合规与大规模基础设施管理。**
## 问题
跨多个 AWS 账户管理安全合规是一项手动且容易出错的过程。Security Hub 生成数百个发现项,每个都需要调查和修复。团队花费数小时点击控制台,一次修复一个资源,结果却跟不上新发现项的出现速度。
## 解决方案
本仓库提供一个基于 Python 的自动化框架,能够:
1. **扫描** AWS 资源是否符合 [AWS 基础安全最佳实践](https://docs.aws.amazon.com/securityhub/latest/userguide/fsbp-standard.html),并生成合规性报告
2. **自动修复**不合规资源——包括加密、版本控制、阻止公开访问、日志记录等
3. **部署**安全工具(Inspector、Config 规则、流量日志),通过基础设施即代码实现跨账户部署
4. **导出**Security Hub 发现项,以便离线分析和执行层报告
基于可重用的 `core` 模块构建,提供共享的 AWS 会话管理、合规检查/修复框架以及结构化日志记录——确保每个脚本遵循相同模式并输出一致结果。
## 架构
```
┌─────────────────────────────┐
│ CLI (argparse) │
└──────────┬──────────────────┘
│
┌──────────▼──────────────────┐
│ cpe_project/core/ │
│ ┌───────────────────────┐ │
│ │ AWSClient │ │ Session management, retry, error handling
│ │ ComplianceCheck │ │ Check → Report → Remediate pipeline
│ │ Logger │ │ Structured logging
│ └───────────────────────┘ │
└──────────┬──────────────────┘
│
┌───────────┬────────────┼──────────┬──────────────┐
│ │ │ │ │
┌───▼────┐ ┌────▼─────┐ ┌───▼────┐ ┌───▼─────┐ ┌─────▼────┐
│ infra/ │ │security- │ │deploy- │ │queries/ │ │ github/ │
│ 22 AWS │ │hub/ │ │ments/ │ │Inventory│ │Org mgmt, │
│services│ │Findings │ │Inspect,│ │& audit │ │Dependabot│
└────────┘ │export │ │Logging │ └─────────┘ └──────────┘
└──────────┘ └────────┘
│
┌──────────▼──────────────────┐
│ CloudFormation/ │ IaC templates
│ asr/ │ Automated Security Response
│ policies/ │ Service Control Policies
└─────────────────────────────┘
```
详见 [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) 了解设计决策和多账户策略。
## 快速开始
```
# 克隆并设置
git clone https://github.com/theusc6/cloud-platform-engineering.git
cd cloud-platform-engineering
python3 -m venv .venv
source .venv/bin/activate
pip install -r cpe_project/requirements.txt
# 使用 AWS SSO 进行身份验证
aws sso login --profile
# 检查 S3 存储桶合规性
python3 -m cpe_project.infra.s3.s3_bucket_check -p -b
# 自动修复不符合规范的存储桶
python3 -m cpe_project.infra.s3.s3_make_bucket_comply -p -b
# 运行测试
pip install pytest
pytest
```
## 项目结构
```
cloud-platform-engineering/
├── cpe_project/
│ ├── core/ # Shared modules (AWSClient, ComplianceCheck, Logger)
│ ├── infra/ # Service-specific compliance & remediation scripts
│ │ ├── s3/ # S3 encryption, versioning, public access, logging
│ │ ├── ec2/ # IMDSv2, EBS encryption, security groups, default VPC
│ │ ├── rds/ # Deletion protection, IAM auth, monitoring, logging
│ │ ├── vpc/ # Flow logs, default SG, S2S VPN logging
│ │ ├── ecr/ # Image scanning, lifecycle policies, tag immutability
│ │ ├── api_gateway/ # Execution logging, X-Ray, access logging
│ │ ├── elbv2/ # Access logging, deletion protection
│ │ └── ... # +15 more (DynamoDB, KMS, CloudFront, WAF, etc.)
│ ├── securityhub/ # Security Hub findings export (CLI, Lambda, PDF reports)
│ ├── deployments/ # Multi-account deployment (Inspector, CloudTrail, GuardDuty)
│ ├── queries/ # AWS resource inventory (EC2, IAM, VPC, ACM, ECS, FSx)
│ ├── github/ # GitHub org management, Dependabot alerts, archiving
│ ├── config/ # AWS Config queries and compliance counts
│ ├── ACM/ # Certificate Manager automation
│ ├── tools/ # EBS snapshot management
│ ├── webapp/ # Flask web UI for S3 compliance
│ ├── scripts/ # Shell utilities (VPN diagnostics)
│ └── policies/ # Service Control Policies (SCPs)
├── CloudFormation/ # IaC templates (CloudWatch, Backup, S3, Nuke)
├── asr/ # AWS Automated Security Response (SHARR) templates
├── examples/ # End-to-end runnable workflows
├── tests/ # pytest suite (49 tests)
└── docs/ # Architecture and design documentation
```
## 核心能力
### 合规性检查与修复
每个合规脚本都使用 `ComplianceCheck` 基类,遵循相同模式:
```
from cpe_project.core import AWSClient, ComplianceCheck, ComplianceResult, Status
class S3BucketComplianceCheck(ComplianceCheck):
def __init__(self, s3_client, bucket_name):
super().__init__(resource_id=bucket_name, service="S3")
self.add_check("S3.4", self.check_encryption)
self.add_remediation("S3.4", self.fix_encryption)
```
运行检查、生成格式化报告,并可选地自动修复:
```
============================================================
Compliance Report: S3 — my-bucket
============================================================
[+] S3.4: Server-side encryption is enabled
[!] S3.14: Versioning is not enabled
[+] S3.1/S3.8: Block public access is enabled
[!] S3.9: Logging is not enabled
[+] Tags: Tags are present
──────────────────────────────────────────────────────────
Summary: 3 PASS, 2 FAIL
============================================================
```
### 覆盖的服务
| 服务 | 控制项 | 脚本数 |
|------|--------|--------|
| **S3** | 加密、版本控制、公开访问、日志记录、SSL、生命周期策略 | 10 |
| **EC2** | IMDSv2、EBS 回收站、安全组、默认 VPC、实例清单 | 9 |
| **RDS** | 删除保护、IAM 认证、增强监控、日志记录、多可用区、小版本升级、公开访问 | 8 |
| **VPC** | 流量日志、默认安全组复制与清理、S2S VPN 日志 | 4 |
| **API Gateway** | 执行日志(v1)、X-Ray 追踪(v1)、访问日志(v2) | 3 |
| **ECR** | 镜像扫描、生命周期策略、标签不可变性 | 3 |
| **ELBv2** | 访问日志、删除保护、无效头部丢弃 | 3 |
| **DynamoDB** | 点时间恢复、删除保护 | 2 |
| **Athena** | 工作组加密、查询日志 | 2 |
| **CloudFront** | 访问日志 | 1 |
| **CloudTrail** | 跟踪删除与清理 | 1 |
| **CloudWatch** | 日志组保留策略 | 1 |
| **Control Tower** | 自动账户配置 | 1 |
| **DocumentDB** | 审计日志 | 1 |
| **Kinesis** | 流加密 | 1 |
| **KMS** | 自动密钥轮换 | 1 |
| **OpenSearch** | 服务软件更新 | 1 |
| **SageMaker** | 禁用笔记本根访问 | 1 |
| **SNS** | 投递状态日志 | 1 |
| **SQS** | 服务器端加密 | 1 |
| **Step Functions** | 执行日志 | 1 |
| **WAF** | Web ACL 日志 | 1 |
| | **总计** | **58** |
### 基础设施即代码
提供 CloudFormation 模板,涵盖 CloudWatch 仪表盘、AWS 备份策略、S3 存储桶配置以及 AWS Nuke 账户清理,所有模板均可参数化并附带文档。
## 测试
```
pytest -v # Run all 49 tests
pytest tests/core/ # Core module tests only
pytest tests/infra/s3/ # S3 compliance tests only
```
测试使用模拟的 boto3 客户端,无需 AWS 凭证。
## 贡献
欢迎贡献。请参考 [CONTRIBUTING.md](cpe_project/CONTRIBUTING.md) 获取指南。
## 许可证
MIT 许可证。详见 [LICENSE](LICENSE)。
标签:AWS安全合规, CloudFormation, Pytest测试, Python自动化, Security Hub, 云平台工程, 关键词优化, 合规扫描, 图探索, 多账户管理, 安全最佳实践, 安全规则引擎, 属性图, 报告导出, 核心模块复用, 漏洞利用检测, 特权提升, 结构化日志, 自动修复, 自动化部署, 逆向工具