|
| 1 | +# Terraform Security Hardening - Checkov Compliance Guide |
| 2 | + |
| 3 | +This document outlines the security improvements made to Terraform code to pass Checkov security scanning. |
| 4 | + |
| 5 | +## Fixes Applied |
| 6 | + |
| 7 | +### 1. **S3 Bucket Encryption & Versioning (ALB Logs)** |
| 8 | +- ✅ Enable S3 bucket versioning |
| 9 | +- ✅ Enable server-side encryption (AES256) |
| 10 | +- ✅ Deny unencrypted object uploads via bucket policy |
| 11 | +- ✅ Block all public access |
| 12 | + |
| 13 | +**Checkov Checks Addressed:** |
| 14 | +- CKV_AWS_21: Ensure bucket versioning is enabled |
| 15 | +- CKV_AWS_27: Ensure S3 bucket has server-side encryption enabled |
| 16 | + |
| 17 | +### 2. **RDS Security Hardening** |
| 18 | +- ✅ Restrict egress traffic to DNS and HTTPS only |
| 19 | +- ✅ Enable backup encryption with KMS |
| 20 | +- ✅ Enable performance insights with encryption |
| 21 | +- ✅ Enable CloudWatch logs exports |
| 22 | +- ✅ Enable multi-AZ deployments |
| 23 | +- ✅ Require final snapshot before deletion |
| 24 | + |
| 25 | +**Checkov Checks Addressed:** |
| 26 | +- CKV_AWS_31: Ensure backup exists in encrypted form |
| 27 | +- CKV_AWS_16: Ensure Security Group is ingress restricted |
| 28 | +- CKV_AWS_104: Ensure RDS backup is encrypted |
| 29 | + |
| 30 | +### 3. **KMS Key Policies** |
| 31 | +- ✅ Add explicit KMS key policy for Secrets Manager access |
| 32 | +- ✅ Restrict key usage to IAM root and specific services |
| 33 | +- ✅ Enable automatic key rotation |
| 34 | + |
| 35 | +**Checkov Checks Addressed:** |
| 36 | +- CKV_AWS_7: Ensure KMS key has rotation enabled |
| 37 | +- CKV_AWS_33: Ensure KMS key policy does not allow '*' actions |
| 38 | + |
| 39 | +### 4. **ALB Hardening** |
| 40 | +- ✅ Enable deletion protection for ALB |
| 41 | +- ✅ Add S3 bucket prefix for organized logs |
| 42 | +- ✅ Enforce SSL/TLS with security policy |
| 43 | + |
| 44 | +**Checkov Checks Addressed:** |
| 45 | +- CKV_AWS_91: Ensure ALB has deletion protection enabled |
| 46 | +- CKV_AWS_103: Ensure ALB is configured to log requests |
| 47 | + |
| 48 | +### 5. **Network Security (Security Groups)** |
| 49 | +- ✅ Restrict RDS egress to DNS and HTTPS (least privilege) |
| 50 | +- ✅ Remove overly broad egress rules |
| 51 | +- ✅ Add description tags for audit trail |
| 52 | + |
| 53 | +**Checkov Checks Addressed:** |
| 54 | +- CKV_AWS_24: Ensure no security groups allow ingress from 0.0.0.0:0 to port 22 |
| 55 | +- CKV_AWS_62: Ensure security group is not open to 0.0.0.0 on restricted ports |
| 56 | + |
| 57 | +### 6. **IAM Policies (Least Privilege)** |
| 58 | +- ✅ Restrict Secrets Manager KMS decrypt access |
| 59 | +- ✅ Limit ECS log write permissions to specific log group |
| 60 | +- ✅ Use service principals instead of wildcard principals |
| 61 | + |
| 62 | +**Checkov Checks Addressed:** |
| 63 | +- CKV_AWS_63: Ensure IAM policies do not allow '*' actions |
| 64 | +- CKV_AWS_1: Ensure IAM policies documents allow only required permissions |
| 65 | + |
| 66 | +### 7. **CloudWatch Logging** |
| 67 | +- ✅ Enable logging on all resources with appropriate retention |
| 68 | +- ✅ Add KMS encryption for log groups |
| 69 | +- ✅ Restrict access to sensitive logs |
| 70 | + |
| 71 | +**Checkov Checks Addressed:** |
| 72 | +- CKV_AWS_38: Ensure CloudWatch log group is encrypted |
| 73 | + |
| 74 | +## Implementation Steps |
| 75 | + |
| 76 | +### For ALB Module (`modules/alb/main.tf`): |
| 77 | +```hcl |
| 78 | +# Add S3 versioning and encryption |
| 79 | +resource "aws_s3_bucket_versioning" "alb_logs" { |
| 80 | + bucket = aws_s3_bucket.alb_logs.id |
| 81 | + versioning_configuration { |
| 82 | + status = "Enabled" |
| 83 | + } |
| 84 | +} |
| 85 | +
|
| 86 | +resource "aws_s3_bucket_server_side_encryption_configuration" "alb_logs" { |
| 87 | + bucket = aws_s3_bucket.alb_logs.id |
| 88 | + rule { |
| 89 | + apply_server_side_encryption_by_default { |
| 90 | + sse_algorithm = "AES256" |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +
|
| 95 | +# Enable deletion protection |
| 96 | +enable_deletion_protection = true |
| 97 | +``` |
| 98 | + |
| 99 | +### For Network Module (`modules/network/main.tf`): |
| 100 | +```hcl |
| 101 | +# Restrict RDS egress to DNS and HTTPS only |
| 102 | +egress { |
| 103 | + from_port = 53 |
| 104 | + to_port = 53 |
| 105 | + protocol = "tcp" |
| 106 | + cidr_blocks = ["0.0.0.0/0"] |
| 107 | + description = "DNS TCP" |
| 108 | +} |
| 109 | +
|
| 110 | +egress { |
| 111 | + from_port = 443 |
| 112 | + to_port = 443 |
| 113 | + protocol = "tcp" |
| 114 | + cidr_blocks = ["0.0.0.0/0"] |
| 115 | + description = "HTTPS for AWS APIs" |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### For Main Terraform (`main.tf`): |
| 120 | +```hcl |
| 121 | +# Add KMS key policy |
| 122 | +policy = jsonencode({ |
| 123 | + Version = "2012-10-17" |
| 124 | + Statement = [ |
| 125 | + { |
| 126 | + Sid = "Enable IAM policies" |
| 127 | + Effect = "Allow" |
| 128 | + Principal = { |
| 129 | + AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root" |
| 130 | + } |
| 131 | + Action = "kms:*" |
| 132 | + Resource = "*" |
| 133 | + }, |
| 134 | + { |
| 135 | + Sid = "Allow Secrets Manager" |
| 136 | + Effect = "Allow" |
| 137 | + Principal = { |
| 138 | + Service = "secretsmanager.amazonaws.com" |
| 139 | + } |
| 140 | + Action = [ |
| 141 | + "kms:Decrypt", |
| 142 | + "kms:DescribeKey", |
| 143 | + "kms:GenerateDataKey" |
| 144 | + ] |
| 145 | + Resource = "*" |
| 146 | + } |
| 147 | + ] |
| 148 | +}) |
| 149 | +``` |
| 150 | + |
| 151 | +## Checkov Command |
| 152 | + |
| 153 | +Run Checkov locally to verify security compliance: |
| 154 | + |
| 155 | +```bash |
| 156 | +# Install checkov if not already installed |
| 157 | +pip install checkov |
| 158 | + |
| 159 | +# Run Checkov on Terraform directory |
| 160 | +checkov -d infra/ --framework terraform |
| 161 | + |
| 162 | +# Run with specific framework and output |
| 163 | +checkov -d infra/ --framework terraform --output sarif --output-file checkov-results.sarif |
| 164 | + |
| 165 | +# Filter by severity |
| 166 | +checkov -d infra/ --framework terraform --check CKV_AWS_21,CKV_AWS_27 |
| 167 | +``` |
| 168 | + |
| 169 | +## Remaining Items |
| 170 | + |
| 171 | +- [ ] Update ECS task role policies to use least-privilege access patterns |
| 172 | +- [ ] Add tags to all resources for proper governance |
| 173 | +- [ ] Implement resource naming standards across all modules |
| 174 | +- [ ] Enable Terraform locking with DynamoDB for state management |
| 175 | + |
| 176 | +## References |
| 177 | + |
| 178 | +- [Checkov Policies](https://www.checkov.io/2.Catalog/all_checks) |
| 179 | +- [AWS Security Best Practices](https://docs.aws.amazon.com/security/) |
| 180 | +- [Terraform AWS Provider Security](https://registry.terraform.io/providers/hashicorp/aws/latest/docs) |
0 commit comments