配置 Google Cloud Firewall 以過濾標籤和子網

配置 Google Cloud Firewall 以過濾標籤和子網

我在 Google Cloud Compute Engine 實例上運行 PostgreSQL,並且 PostgreSQL 目前配置為接受來自任何地方的連接,其想法是我使用防火牆來控制訪問,而不必每次都登入伺服器。

目前我有一個名為 的防火牆規則development-allow-psql,為了確保沒有其他設定錯誤,我已將其設定為允許來自任何地方:

Targets: All instances on network
Source Filter: IP Ranges
Source IP Ranges: 0.0.0.0/0
Second Source Filter: None
Specified protocols and ports: tcp:5432

跑步

psql "dbname=mydb host=__.___.__.__ user=myuser password=mypassword port=5432"

立即連接我,但可以從任何地方,而不僅僅是我想要允許訪問的實例。

這些實例是透過範本自動建立的,Instance Group範本Instance Template 配置為使用以下設定建立實例:

Network tags: myapp-api, http-server, https-server
Network: development
Subnetwork: development (with address range 10.154.0.0/20)

我想將對這個資料庫實例的存取限制為具有myapp-apias 標籤或具有 子網路10.154.0.0/20或兩者兼而有之的實例。所以我將防火牆設定更改為以下內容:

Targets: Specified target tags
Target tags: myapp-api
Source Filter: Subnets
Subnets: 10.154.0.0/20
Second Source Filter: None
Specified protocols and ports: tcp:5432

這會阻止對我之前運行的命令的存取psql(psql 命令是從我可以透過存取的 docker 實例運行的docker exec -ti -u0 my-instance-api-dev-small bash

如果我現在改為

Targets: All instances on the network
Source Filter: Subnets
Subnets: 10.154.0.0/20
Second Source Filter: Source tags
Source tags: myapp-api
Specified protocols and ports: tcp:5432

它仍然阻止所有訪問。如果我現在刪除第二個來源過濾器並僅在子網路上進行過濾,仍然無法存取。

Targets: All instances on the network
Source Filter: Subnets
Subnets: 10.154.0.0/20
Second Source Filter: None
Specified protocols and ports: tcp:5432

如果我將子網來源過濾器替換為標籤過濾器:

Targets: All instances on the network
Source Filter: Source tags
Source tags: myapp-api
Second Source Filter: None
Specified protocols and ports: tcp:5432

....仍然無法訪問。

選擇子網來源過濾器並僅選擇所有子網路時相同。

切換到:

Targets: All instances on the network
Source Filter: IP ranges
Source IP ranges: 0.0.0.0/0
Second Source Filter: Source tags
Source tags: myapp-api
Specified protocols and ports: tcp:5432

即使我指定了來源標籤,它再次允許每個人(甚至在 Google Cloud 之外)進行連接

將 IP 位址範圍變更為 10.154.0.0/20 再次封鎖所有人

Targets: All instances on the network
Source Filter: IP ranges
Source IP ranges: 10.154.0.0/20
Second Source Filter: Source tags
Source tags: myapp-api
Specified protocols and ports: tcp:5432

例如,用實例的外部 IP 位址取代 IP 範圍35.189.124.141/32是可行的,但由於這些IP 位址是臨時的,因此這不是一個解決方案,因為每次自動擴展添加更多具有新IP 位址的實例時,都需要更新防火牆規則。

如何將防火牆配置為僅允許來自特定子網路和/或具有特定標籤的執行個體?我正在做的事情似乎不起作用。

答案1

當從資料庫執行個體的外部IP位址切換到其內部IP位址時,突然所有上述組合都起作用。

psql "dbname=mydb host=internal-ip-address-here user=myuser password=mypassword port=5432"

事實證明,當您使用標籤時,防火牆會查看內部 IP 位址而不是外部 IP 位址。

Targets: All instances on the network
Source Filter: Subnets
Subnets: 10.154.0.0/20
Second Source Filter: Source tags
Source tags: myapp-api
Specified protocols and ports: tcp:5432

相關內容