TCP Proxy 程式碼研究室 - 使用 TCP Proxy 負載平衡器的頻率限制和 IP 拒絕清單

1. 簡介

Google Cloud Load Balancing 會部署於全球 Google 服務點 (POP) 中的 Google 網路邊緣。導向至 TCP Proxy 負載平衡器的使用者流量會進入離使用者最近的服務點,接著透過 Google 的全球網路進行負載平衡,最後再傳至具備足夠容量且距離最近的後端。

Cloud Armor 是 Google 的分散式阻斷服務和網頁應用程式防火牆 (WAF) 偵測系統。Cloud Armor 與 Google Cloud TCP Proxy 負載平衡器緊密結合,可讓您針對不需要的要求處理連入流量。這項服務的頻率限制功能,可讓您依據要求數量減少後端資源的流量,並避免惡意的流量耗用虛擬私有雲 (VPC) 網路上的資源。

Google Cloud TCP/SSL Proxy 負載平衡器可讓您在後端服務之間透過 Proxy/ SSL 類型流量進行 Proxy 處理。

在本研究室中,您將建立具備後端服務的 TCP/SSL 負載平衡器,並將負載平衡器的存取權限制為僅限一組特定的使用者用戶端。

be33dadf836374bb.png

課程內容

  • 如何建立 TCP/SSL Proxy 負載平衡器
  • 如何建立 Cloud Armor 安全性政策
  • 如何在 Cloud Armor 中為 TCP/SSL Proxy 負載平衡器建立 IP 拒絕清單規則
  • 如何在 Cloud Armor 中為 TCP Proxy 負載平衡器建立頻率限制規則
  • 如何將安全性政策新增至 TCP/SSL 負載平衡後端服務

軟硬體需求

  • Google Compute Engine 的基本知識 ( codelab)
  • 基本的網路和 TCP/IP 知識
  • 基本的 Unix/Linux 指令列知識
  • 建議您先透過 Google Cloud 中的網路功能完成 GCP 網路導覽,

2. 需求條件

自修環境設定

  1. 登入 Cloud 控制台建立新專案,或是重複使用現有專案。如果您還沒有 Gmail 或 Google Workspace 帳戶,請先建立帳戶

注意:只要記住控制台網址 ( console.cloud.google.com),即可輕鬆存取 Cloud 控制台。

96a9c957bc475304.png

b9a10ebdf5b5a448.png

a1e3c01a38fa61c2.png

提醒您,專案 ID 是所有 Google Cloud 專案的專屬名稱 (已經有人使用上述名稱,很抱歉對您不符!)。稍後在本程式碼研究室中會稱為 PROJECT_ID。

注意:如果您使用 Gmail 帳戶,可以保留預設位置的「沒有機構」選項。如果您使用的是 Google Workspace 帳戶,請選擇適合貴機構的位置。

  1. 接下來,您需要在 Cloud 控制台中啟用計費功能,才能使用 Google Cloud 資源。

執行這個程式碼研究室並不會產生任何費用,如果有的話。請務必依照「清除所用資源」一節指示本節將說明如何關閉資源,這樣您就不會產生本教學課程結束後產生的費用。Google Cloud 的新使用者符合 $300 美元免費試用計畫的資格。

啟動 Cloud Shell

雖然 Google Cloud 可以從筆記型電腦遠端操作,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是一種在 Cloud 中執行的指令列環境。

在 GCP 控制台的右上方,按一下「Cloud Shell」圖示:

bce75f34b2c53987.png

佈建並連線至環境的作業只需幾分鐘的時間。完成後,您應該會看到類似下方的內容:

f6ef2b5f13479f3a.png

這部虛擬機器都裝載了您需要的所有開發工具。提供永久的 5 GB 主目錄,而且在 Google Cloud 中運作,大幅提高網路效能和驗證能力。這個研究室中的所有工作都可以透過瀏覽器完成。

事前準備

在 Cloud Shell 中,確認您已設定專案 ID

gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
PROJECT_ID=[YOUR-PROJECT-NAME]
echo $PROJECT_ID

啟用 API

啟用所有必要服務

gcloud services enable compute.googleapis.com
gcloud services enable logging.googleapis.com        
gcloud services enable monitoring.googleapis.com

3. 建立後端服務

按照下列方式建立 2 個執行個體 - 在 us-central1-b 可用區中建立 instance1-b1

gcloud compute instances create vm-1-b1 \
    --image-family debian-9 \
    --image-project debian-cloud \
    --tags tcp-lb \
    --zone us-central1-b \
    --metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install apache2 -y
      sudo sed -i '/Listen 80/c\Listen 110' /etc/apache2/ports.conf
      sudo service apache2 restart
      echo '<!doctype html><html><body><h1>This is VM1-b1 in central1-b</h1></body></html>' | tee /var/www/html/index.html
      EOF"

在可用區 us-central1-b 中建立執行個體 1-b2

gcloud compute instances create vm-1-b2 \
    --image-family debian-9 \
    --image-project debian-cloud \
    --tags tcp-lb \
    --zone us-central1-b \
    --metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install apache2 -y
      sudo sed -i '/Listen 80/c\Listen 110' /etc/apache2/ports.conf
      sudo service apache2 restart
      echo '<!doctype html><html><body><h1>This is VM1-b2 in central1-b</h1></body></html>' | tee /var/www/html/index.html
      EOF"

建立執行個體群組 vm-ig1

gcloud compute instance-groups unmanaged create vm-ig1  --zone us-central1-b

為執行個體群組建立已命名的通訊埠。本研究室將使用通訊埠 110

    gcloud compute instance-groups set-named-ports vm-ig1 \
--named-ports tcp 110:110 --zone us-central1-b

將執行個體新增至執行個體群組

gcloud compute instance-groups unmanaged add-instances vm-ig1 \
   --instances vm-1-b1,vm-1-b2 --zone us-central1-b

4. 設定負載平衡器

接下來,我們會建立健康狀態檢查。

gcloud compute health-checks create tcp my-tcp-health-check --port 110

建立後端服務

gcloud compute backend-services create my-tcp-lb  --global-health-checks --global \
--protocol TCP --health-checks my-tcp-health-check --timeout 5m --port-name tcp110

將執行個體群組新增至後端服務

gcloud compute backend-services add-backend my-tcp-lb --global --instance-group \ vm-ig1 --instance-group-zone us-central1-b --balancing-mode UTILIZATION \ --max-utilization 0.8

設定目標 TCP Proxy

gcloud compute target-tcp-proxies create my-tcp-lb-target-proxy --backend-service \ my-tcp-lb --proxy-header NONE

預留全域靜態 IPv4 位址

您將使用此 IP 位址來連線至已進行負載平衡的服務。

gcloud compute addresses create tcp-lb-static-ipv4  --ip-version=IPV4   --global

設定負載平衡器 IP 位址的全域轉送規則。

gcloud compute forwarding-rules create my-tcp-lb-ipv4-forwarding-rule \
    --global --target-tcp-proxy my-tcp-lb-target-proxy --address LB_STATIC_IPV4 \ --ports 110

5. 建立 TCP Proxy 負載平衡器的防火牆規則

gcloud compute firewall-rules create allow-tcplb-and-health \
   --source-ranges 130.211.0.0/22,35.191.0.0/16 \
   --target-tags tcp-lb \
   --allow tcp:110

建立負載平衡器後,請使用下列指令來測試負載平衡器

Curl LB_IP:110

接下來,請建立 VM 以驗證 LB 存取遭拒的情況

您應該建立 2 個執行個體,每個執行個體各有公開 IP 位址、名為「test-server1」和「test-server2」

6. 在 Cloud Armor 中建立安全性政策

在本節中,您會在 Cloud Armor 的政策中建立後端安全性政策和 2 項規則。

第一項規則會將安全性政策設為拒絕特定 IP,藉此拒絕一小部分的 IP 存取 TCP 負載平衡器,第二項規則則會執行頻率限制。

  1. 在 Cloud Shell 中(如需如何使用 Cloud Shell,請參閱「設定與需求」下方的「啟動 Cloud Shell」),建立名為 rate-limit-and-deny-tcp 的後端服務安全性政策,如下所示:
gcloud compute security-policies create rate-limit-and-deny-tcp \
    --description "policy for tcp proxy rate limiting and IP deny"

新增安全性政策至安全性政策

接著,請將拒絕清單規則新增至 Cloud Armor 政策「rate-limit-and-deny-tcp」。

gcloud compute security-policies rules create 1000 --action deny --security-policy \ rate-limit-and-deny-tcp --description "deny test-server1" --src-ip-ranges \ "enter-test-server-1ip-here"

將頻率限制規則新增至 Cloud Armor 安全性政策「rate-limit-and-deny-tcp」

gcloud compute security-policies rules create 3000   \ --security-policy=rate-limit-and-deny-tcp  \       
--expression="true"  --action=rate-based-ban  --rate-limit-threshold-count=5  \          
--rate-limit-threshold-interval-sec=60  --ban-duration-sec=300      \         
--conform-action=allow  --exceed-action=deny-404  --enforce-on-key=IP

將政策附加至 TCP Proxy 後端服務:

執行下列指令,確保安全性政策已附加至 TCP Proxy 後端服務。

gcloud compute backend-services update my-tcp-lb --security-policy \ rate-limit-and-deny-tcp

啟用 TCP Proxy 負載平衡器的記錄功能

gcloud beta compute backend-services update my-tcp-lb \ 
--enable-logging --logging-sample-rate=1

7. 驗證拒絕清單規則

登入拒絕清單規則中 IP 已指定 IP 的測試伺服器,然後執行下列指令,驗證拒絕清單規則

Curl LB_IP:110

立即要求可能會提供來自 LB 的回應,但請等到 curl 要求遭拒或捨棄後,再查看 Cloud Logging 中的記錄檔,確認所觸發 IP 拒絕規則的記錄項目。

前往 Cloud Logging,然後在「資源」下方選取「tcp_ssl_proxy_rule」做為資源類型並將後端目標設為「my-tcp-lb」。

透過定義用於篩選的資源。我們可以驗證 IP 拒絕規則是否在記錄項目的 PRIORITY 值 1000 生效,而已設定的動作「DENY」也同時生效,原因是拒絕規則和 IP 遭拒絕,如下所示

db9b835e0360dcaf.png

8. 驗證頻率限制規則

在短時間內傳送多個超過所設門檻 (每分鐘 5 個要求) 的要求,驗證頻率限制規則已生效。

完成後,點選 Cloud Armor 服務中的檢視記錄檔,即可前往 Cloud Logging 頁面,按照負載平衡器篩選記錄檔,查看傳入的 Cloud Armor 記錄檔。

頻率限制項目應如下方螢幕截圖所示。我們可以確認頻率限制規則是否生效:記錄項目的 PRIORITY 值 3000 和已設定的動作才生效,並按照頻率限制規則的指示啟用「以費率計算的 BAN」動作。

37c76e5d7532623.png

9. 環境清除

請務必清除建立的基礎架構,以免執行未使用的基礎架構費用。

最快的方法是刪除 GCP 中的整個專案,確保沒有閒置的資源。不過,請使用下列指令刪除個別資源

TCP Proxy 負載平衡器

gcloud compute target-tcp-proxies delete my-tcp-lb

執行個體群組。

gcloud compute instance-groups unmanaged delete vm-ig1

已建立 2 個測試 VM 執行個體

gcloud compute instances delete Instance_name --zone=instance_zone

後端服務

gcloud compute backend-services delete BACKEND_SERVICE_NAME

政策中的 Cloud Armor 規則

gcloud compute security-policies rules delete 1000  \ --security-policy=rate-limit-and-deny-tcp && 
gcloud compute security-policies rules delete 3000  \ --security-policy=rate-limit-and-deny-tcp

Cloud Armor 安全性政策

gcloud compute security-policies delete rate-limit-and-deny-tcp