Skip to content

Cloud Armor & Edge Security — WAF và DDoS Protection cho GCP LB

Tại sao Cloud Armor là edge security, không phải backend security

Cloud Armor không phải một agent cài trên backend VMs. Đây là security policy enforce tại GFE (Google Front End) layer — trước khi traffic đến backends. Điều này có nghĩa:

  • DDoS protection xảy ra tại edge PoP của Google, không tiêu tốn bandwidth hay compute của bạn
  • WAF rules được evaluate trên hàng trăm PoPs toàn cầu
  • Backend không bao giờ thấy traffic đã bị block bởi Cloud Armor

Vì Cloud Armor hoạt động tại proxy layer, nó chỉ available cho proxy-based LBs (Global External Application LB, Regional External ALB, Internal ALB) — không available cho Passthrough NLB (không có proxy để run WAF).

Kiến trúc: Security Policy gắn vào Backend Service

Cloud Armor security policy được gắn vào Backend Service, không phải Frontend (Target Proxy, Forwarding Rule):

Client request


GFE (Google Front End)
    │ TLS terminate, URL map lookup

Backend Service
    │ ← Cloud Armor policy evaluated HERE
    ├── Rule 1: Allow US traffic
    ├── Rule 2: Block IP 1.2.3.4
    ├── Rule 3: WAF SQLi detection
    └── Default rule: Allow


NEG Endpoint (Pod)

Vì policy gắn vào Backend Service, một policy có thể apply cho nhiều Ingresses nếu chúng share cùng Backend Service (cùng Service:port trong Kubernetes). Ngược lại, không thể có hai Ingresses với cùng backend nhưng different policies.

Rule Priority và Evaluation

Cloud Armor rules được evaluated theo priority thấp hơn = ưu tiên cao hơn (tương tự GCP Firewall):

Priority 1000: Deny IP 1.2.3.4       ← Evaluated first
Priority 2000: Allow region=US
Priority 2147483647: Allow ALL        ← Default rule (highest number)

Khi request match một rule, evaluation dừng lại — remaining rules không được check. Rule đầu tiên match quyết định fate của request.

Mỗi rule có một action: allow, deny(status_code), rate_based_ban, throttle, hay redirect.

Preconfigured WAF Rules

Cloud Armor cung cấp sẵn các WAF rule bundles dựa trên ModSecurity Core Rule Set:

SQL Injection (SQLi):

bash
gcloud compute security-policies rules create 1000 \
  --security-policy=my-policy \
  --expression="evaluatePreconfiguredExpr('sqli-v33-stable')" \
  --action=deny-403

Cross-Site Scripting (XSS):

bash
--expression="evaluatePreconfiguredExpr('xss-v33-stable')"

Remote File Inclusion (RFI), Local File Inclusion (LFI), Remote Code Execution (RCE) cũng có sẵn.

Preconfigured rules được Google update khi có vulnerabilities mới. Bạn không cần maintain rule sets thủ công.

Sensitivity levels: Preconfigured rules có các levels (0-4). Level cao hơn = detect nhiều hơn nhưng tăng false positive rate. Default thường là level 1 hoặc 2.

Custom Rules với Common Expression Language (CEL)

Ngoài preconfigured rules, bạn có thể viết custom rules bằng CEL:

bash
# Block traffic từ specific IP
--expression="inIpRange(origin.ip, '1.2.3.0/24')"

# Block based on country
--expression="origin.region_code == 'RU'"

# Block specific User-Agent
--expression="request.headers['user-agent'].contains('badbot')"

# Combine conditions
--expression="origin.region_code == 'CN' && request.path.startsWith('/admin')"

# Rate limiting: block nếu > 1000 requests trong 60 giây
--expression="true"
--action=rate-based-ban \
--rate-limit-threshold-count=1000 \
--rate-limit-threshold-interval-sec=60 \
--ban-duration-sec=300

Rate Limiting và Throttling

Cloud Armor hỗ trợ rate limiting per key:

throttle: Giới hạn request rate, excess requests bị drop (hoặc redirect).

rate-based-ban: Khi rate vượt threshold, ban source IP trong ban-duration-sec giây.

Rate limit key có thể là:

  • ALL — áp dụng cho tất cả traffic (global rate limit)
  • IP — rate limit per source IP (phổ biến nhất)
  • HTTP-HEADER:X-API-Key — rate limit per API key trong header
  • HTTP-COOKIE:session_id — rate limit per session
bash
gcloud compute security-policies rules create 2000 \
  --security-policy=my-policy \
  --expression="true" \
  --action=throttle \
  --rate-limit-threshold-count=100 \
  --rate-limit-threshold-interval-sec=60 \
  --enforce-on-key=IP \
  --exceed-action=deny-429

Adaptive Protection — ML-based DDoS Detection

Cloud Armor Adaptive Protection là tính năng ML tự động phát hiện và đề xuất rules cho DDoS attacks đang diễn ra:

  1. Adaptive Protection monitor baseline traffic patterns
  2. Khi detect anomaly (có thể là DDoS), tạo alert với suggested rule
  3. Rule có thể được auto-apply (nếu bật) hoặc manual review
bash
# Enable Adaptive Protection
gcloud compute security-policies update my-policy \
  --enable-layer7-ddos-defense

Adaptive Protection phân biệt legitimate traffic spikes với attack traffic bằng cách analyze traffic patterns, request attributes, và so sánh với baseline.

Gắn Cloud Armor với GKE

Qua BackendConfig (Ingress)

yaml
apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: api-backendconfig
spec:
  securityPolicy:
    name: "my-cloud-armor-policy"
yaml
# Service reference BackendConfig
metadata:
  annotations:
    cloud.google.com/backend-config: '{"default": "api-backendconfig"}'

Qua GCPBackendPolicy (Gateway API)

yaml
apiVersion: networking.gke.io/v1
kind: GCPBackendPolicy
metadata:
  name: api-backend-policy
spec:
  targetRef:
    kind: Service
    name: api-service
  default:
    securityPolicy: "my-cloud-armor-policy"

Constraint quan trọng

IAP và Cloud Armor không thể dùng cùng nhau trên cùng Backend Service. Hai tính năng này conflict về authentication flow.

Cloud Armor và CDN: Có thể dùng cùng nhau. Cloud Armor được evaluated trước khi CDN check cache — request bị block bởi Cloud Armor không đến CDN layer.

Logging và Monitoring

Cloud Armor log tất cả requests (bị block lẫn bị allow) nếu bạn enable logging:

bash
gcloud compute security-policies update my-policy \
  --log-level=VERBOSE

Log được gửi đến Cloud Logging với resource type cloud_armor_rule. Có thể query:

sql
-- Các request bị block trong 1 giờ qua
resource.type="cloud_armor_rule"
jsonPayload.enforcedSecurityPolicy.outcome="DENY"
timestamp >= "2024-01-01T00:00:00Z"

Metrics quan trọng:

  • networksecurity.googleapis.com/https/request_count — total requests per rule outcome
  • networksecurity.googleapis.com/https/blocked_request_count — bị block

Edge Security Policy vs Backend Security Policy

GCP có hai loại Cloud Armor policy:

Backend security policy (thông thường): Gắn vào Backend Service. Evaluate sau khi LB frontend nhận request. Cho phép dùng request headers, body (với advanced tiers), và ML features.

Edge security policy (preview): Evaluate trước khi LB frontend. Chỉ hỗ trợ IP-based và geo-based rules. Không thể dùng request headers. Phù hợp cho L3/L4 blocking trước khi traffic vào LB layer.

Cho hầu hết use cases, Backend security policy là đủ và recommended.

References