Posted on :: 607 Words :: Tags: , , , ,

Stop Leaking Secrets: Real Security for Terraform

Chúng ta cần nói về con voi trong phòng.

Bạn có AWS keys hardcoded trong Terraform files. Database passwords committed to Git. API tokens floating around trong environment variables. Và bạn đang đổ mồ hôi mỗi khi ai đó đề cập "security audit."

Tôi đã từng ở đó. Chúng ta đều từng ở đó.

Đây là sự thật: Hầu hết infrastructure breaches không xảy ra vì hackers brilliant. Chúng xảy ra vì ai đó để credentials ngồi trong plain text ở đâu đó. CircleCI breach năm 2023? 1.8 triệu secrets exposed. Cost? Hơn $10M trong cleanup, plus customer trust nuked into orbit.

Đây không phải scare tactic. Đây là điều xảy ra khi security là afterthought.

Trong part này, tôi sẽ chỉ bạn cách production teams thực sự handle secrets - với HashiCorp Vault, SOPS encryption, OIDC authentication, và proper audit logging. Không lý thuyết. Chỉ stuff hoạt động trong real world.

📦 Code Examples

Repository: terraform-hcl-tutorial-series This Part: Part 11 - Security Practices

git clone https://github.com/khuongdo/terraform-hcl-tutorial-series.git
cd terraform-hcl-tutorial-series
git checkout part-11
cd examples/part-11-security/
terraform init
terraform plan

Năm Rules Thực Sự Quan Trọng

Rule 1: Secrets Never Touch Code

Secrets không thuộc về trong:

  • Terraform .tf files
  • Git repos (ngay cả private ones)
  • Unencrypted state files
  • CI/CD config files

Nơi chúng THUỘC: Dedicated secret managers như Vault, AWS Secrets Manager, hoặc GCP Secret Manager.

Rule 2: Credentials Should Expire

Static credentials giống sữa - chúng go bad, bạn chỉ không biết khi nào. Dynamic credentials expire automatically.

Rule 3: Least Privilege Không Phải Optional

Terraform của bạn chỉ creates S3 buckets? Thì nó không cần EC2 permissions. Period.

Rule 4: Encrypt Everything at Rest

Nếu nó sensitive và sitting trên disk, nó better được encrypted:

  • Terraform state files (dùng encrypted S3 backend)
  • Secret storage (Vault handles this)
  • Config files (đó là cái SOPS dành cho)

Rule 5: Audit Everything

Bạn cần answer three questions khi cái gì đó goes wrong:

  • Ai grabbed secret đó?
  • Khi nào họ access nó?
  • Họ làm gì với nó?

HashiCorp Vault: Credentials Thực Sự Expire

Vault là nơi magic xảy ra. Thay vì storing static credentials, Vault generates fresh ones on demand - và kills chúng automatically sau set time.

Getting Vault Running Locally

# macOS
brew tap hashicorp/tap
brew install hashicorp/tap/vault

# Check nó worked
vault version

Pulling Vault Secrets into Terraform

# provider.tf
provider "vault" {
  address = "https://vault.example.com:8200"
}

# main.tf
data "vault_kv_secret_v2" "db" {
  mount = "secret"
  name  = "database/prod"
}

resource "aws_db_instance" "main" {
  identifier = "production-db"
  username   = data.vault_kv_secret_v2.db.data["username"]
  password   = data.vault_kv_secret_v2.db.data["password"]
}

SOPS: Commit Secrets to Git (Safely)

SOPS encrypts config files nên bạn có thể commit chúng to Git mà security team không meltdown.

OIDC: Stop Storing CI/CD Credentials

OIDC fixes bằng cách dùng temporary tokens thay vì permanent keys.

Checkpoint

  1. Tại sao không thể hardcode secrets? End up trong Git history forever, zero audit trail, require code changes để rotate.

  2. Vault's dynamic secrets tốt hơn static keys? Generated fresh, expire automatically, unique per request.

  3. SOPS dành cho gì? Encrypting config files để commit safely to Git.

  4. OIDC làm CI/CD secure hơn? Temporary tokens expire trong hour thay vì permanent credentials.


Series navigation:


Part của "Terraform from Fundamentals to Production" series.