使用虛擬私有雲防火牆規則

這個頁面說明設定虛擬私有雲 (VPC) 防火牆規則的正確指令,並提供使用範例。虛擬私有雲防火牆規則可讓您根據通訊埠編號、標記或通訊協定,允許或拒絕虛擬私有雲網路中虛擬機器 (VM) 執行個體的往來流量。

事前準備

如要進一步瞭解虛擬私有雲防火牆規則,例如預設網路的隱含規則和系統產生的規則,請參閱「虛擬私有雲防火牆規則」。

設定防火牆規則前,請先查看防火牆規則元件,熟悉 Google Cloud中使用的防火牆元件。

建立虛擬私有雲防火牆規則

虛擬私有雲防火牆規則是在網路層級定義,且僅適用於建立規則的網路;不過,您為每項規則選擇的名稱在專案中不得重複。

防火牆規則可包含 IPv4 或 IPv6 範圍,但不能同時包含兩者。

建立防火牆規則時,您可以選擇啟用防火牆規則記錄功能。如果啟用記錄功能,可以略過中繼資料欄位,節省儲存空間費用。詳情請參閱「使用虛擬私有雲防火牆規則記錄」。

如要為目標或來源服務帳戶欄位指定多個服務帳戶,請使用 Google Cloud CLI、API 或用戶端程式庫。

預設網路會在建立時提供自動防火牆規則。如果您使用 Google Cloud 控制台,自訂和自動模式網路可讓您在建立網路時,輕鬆建立類似的防火牆。如果您使用 gcloud CLI 或 API,且想建立與預設網路提供的防火牆規則類似的規則,請參閱「針對常見用途設定防火牆規則」。

控制台

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 按一下「Create firewall rule」(建立防火牆規則)。

  3. 輸入防火牆規則的「Name」(名稱)。

    此名稱在專案中不得重複。

  4. (選用) 您可以啟用防火牆規則記錄:

    • 按一下 [Logs] (記錄) > [On] (啟用)。
    • 如要省略中繼資料,請按一下「Show logs details」(顯示記錄詳細資料),然後清除「Include metadata」(包含中繼資料) 核取方塊。
  5. 指定防火牆規則的「網路」。

  6. 指定規則的「優先順序」。

    數值越低,優先順序越高。

  7. 針對「Direction of traffic」(流量方向),選擇輸入或輸出。

  8. 針對「Action on match」(相符時執行的動作),選擇允許或拒絕。

  9. 指定規則的「Targets」(目標)。

    • 如要將規則套用到網路中的所有執行個體,請選擇 All instances in the network。
    • 如要依網路 (目標) 標記將規則套用至所選執行個體,請選擇「Specified target tags」(指定的目標標記),然後在「Target tags」(目標標記) 欄位中輸入規則應套用的標記。
    • 如要依據相關聯的服務帳戶,將規則套用至所選執行個體,請選擇「Specified service account」(指定的服務帳戶),在「Service account scope」(服務帳戶範圍) 中指明服務帳戶位於目前專案或其他專案,然後在「Target service account」(目標服務帳戶) 欄位中選擇或輸入服務帳戶名稱。
  10. 如果是輸入規則,請指定「來源篩選器」:

    • 如要依來源 IPv4 範圍篩選傳入流量,請選取「IPv4 ranges」(IPv4 範圍),然後在「Source IPv4 ranges」(來源 IPv4 範圍) 欄位中輸入 CIDR 區塊。如要指定任何 IPv4 來源,請使用 0.0.0.0/0。
    • 如要依來源 IPv6 範圍篩選傳入流量,請選取「IPv6 ranges」(IPv6 範圍),然後在「Source IPv6 ranges」(來源 IPv6 範圍) 欄位中輸入 CIDR 區塊。任何 IPv6 來源請使用 ::/0。
    • 如要依網路標記篩選傳入流量,請選擇「Source tags」(來源標記),然後在「Source tags」(來源標記) 欄位中輸入網路標記。如要瞭解來源標記數量限制,請參閱「個別網路限制」。只有在目標「不是」由服務帳戶指定時,才能依據來源標記篩選。
    • 如要依服務帳戶篩選傳入流量,請選擇「Service account」(服務帳戶),在「Service account scope」(服務帳戶範圍) 中指明服務帳戶位於目前專案或其他專案,然後在「Source service account」(來源服務帳戶) 欄位中選擇或輸入服務帳戶名稱。只有在目標「不是」由網路標記指定時,才能依據來源服務帳戶篩選。
    • 必要時,可以指定「Second source filter」(次要來源篩選器)。次要來源篩選器不得使用與主要來源相同的篩選條件。來源 IP 範圍可與來源標記或來源服務帳戶搭配使用。有效來源集是來源範圍 IP 位址與網路標記或服務帳戶識別的執行個體的聯集。也就是說,如果來源 IP 範圍「或」來源標記 (或來源服務帳戶) 符合篩選條件,來源就會納入有效來源集。
    • 「Source tags」(來源標記) 和「Source service account」(來源服務帳戶) 無法同時使用。

    如要進一步瞭解如何使用服務帳戶和網路標記,請參閱「依服務帳戶與網路標記篩選」。

  11. 如果是輸入規則,請指定「目的地篩選器」:

    • 如要依目的地 IPv4 範圍篩選傳入流量,請選取「IPv4 ranges」(IPv4 範圍),然後在「Destination IPv4 ranges」(目的地 IPv4 範圍) 欄位中輸入 CIDR 區塊。任何 IPv4 目的地請使用 0.0.0.0/0。
    • 如要依目的地 IPv6 範圍篩選傳入流量,請選取「IPv6 ranges」(IPv6 範圍),然後在「Destination IPv6 ranges」(目的地 IPv6 範圍) 欄位中輸入 CIDR 區塊。任何 IPv6 目的地請使用 ::/0。

    詳情請參閱「輸入規則的目的地」。

  12. 如果是輸出規則,請指定「目的地篩選器」:

    • 如要依目的地 IPv4 範圍篩選傳出流量,請選取「IPv4 ranges」(IPv4 範圍),然後在「Destination IPv4 ranges」(目的地 IPv4 範圍) 欄位中輸入 CIDR 區塊。任何 IPv4 目的地請使用 0.0.0.0/0。
    • 如要依目的地 IPv6 範圍篩選傳出流量,請選取「IPv6 ranges」(IPv6 範圍),然後在「Destination IPv6 ranges」(目的地 IPv6 範圍) 欄位中輸入 CIDR 區塊。任何 IPv6 目的地請使用 ::/0。

    詳情請參閱「輸出規則的目的地」。

  13. 如果是輸出規則,請指定「Source filter」(來源篩選器):

    • 如要依來源 IPv4 範圍篩選傳出流量,請選取「IPv4 ranges」(IPv4 範圍),然後在「Source IPv4 ranges」(來源 IPv4 範圍) 欄位中輸入 CIDR 區塊。如要指定任何 IPv4 來源,請使用 0.0.0.0/0。
    • 如要依來源 IPv6 範圍篩選傳出流量,請選取「IPv6 ranges」(IPv6 範圍),然後在「Source IPv6 ranges」(來源 IPv6 範圍) 欄位中輸入 CIDR 區塊。任何 IPv6 目的地請使用 ::/0。

    詳情請參閱「輸出規則的來源」。

  14. 定義規則適用的「通訊協定和通訊埠」:

    • 如要將規則套用至所有通訊協定和目的地通訊埠,請依據動作選取「Allow all」(全部允許) 或「Deny all」(全部拒絕)。
    • 如要定義特定通訊協定和目的地通訊埠,請選取「Specified protocols and ports」(指定的通訊協定和通訊埠):
      • 選取「TCP」TCP,納入 TCP 通訊協定和目的地通訊埠。輸入 all 或以半形逗號分隔的目的地通訊埠清單,例如 20-22, 80, 8080。
      • 選取「UDP」UDP,即可納入 UDP 通訊協定和目的地通訊埠。輸入 all 或以半形逗號分隔的目的地通訊埠清單,例如 67-69, 123。
      • 選取「Other」(其他),即可加入 icmp、sctp 等通訊協定或通訊協定編號。例如,IPv4 ICMP 請使用 icmp 或通訊協定編號 1。IPv6 ICMP 請使用通訊協定編號 58。

    詳情請參閱「通訊協定和目的地通訊埠」。

  15. (選用) 您可以建立防火牆規則,但將強制執行狀態設為「disabled」(停用),不強制執行。按一下「Disable rule」(停用規則),然後選取「Disabled」(已停用)。

  16. 點選「建立」。

gcloud

如要建立虛擬私有雲防火牆規則,請使用 gcloud compute firewall-rules create 指令:

gcloud compute firewall-rules create RULE_NAME \
    [--network NETWORK; default="default"] \
    [--priority PRIORITY;default=1000] \
    [--direction (ingress|egress|in|out); default="ingress"] \
    [--action (deny | allow )] \
    [--target-tags TAG[,TAG,...]] \
    [--target-service-accounts=IAM_SERVICE_ACCOUNT[,IAM_SERVICE_ACCOUNT,...]] \
    [--source-ranges CIDR_RANGE[,CIDR_RANGE,...]] \
    [--source-tags TAG[,TAG, ...]] \
    [--source-service-accounts=IAM_SERVICE_ACCOUNT[,IAM_SERVICE_ACCOUNT,...]] \
    [--destination-ranges CIDR_RANGE[,CIDR_RANGE,...]] \
    [--rules (PROTOCOL[:PORT[-PORT]],[PROTOCOL[:PORT[-PORT]],...]] | all ) \
    [--disabled | --no-disabled] \
    [--enable-logging | --no-enable-logging] \
    [--logging-metadata LOGGING_METADATA]

請依照以下說明的方式來使用參數。如要進一步瞭解每個參數,請參閱 SDK 參考資料說明文件。

  • --network 規則的網路。如果省略,系統會在 default 網路中建立規則。如果您沒有預設網路,或想在特定網路中建立規則,就必須使用這個欄位。
  • --priority:表示規則優先順序的數值。數值越低,優先順序越高。
  • --direction流量方向,可為 INGRESS 或 EGRESS。
  • --action 相符時執行的動作,可以是 allow 或 deny。必須與 --rules 旗標搭配使用。
  • 請透過下列任一方式指定目標:
    • 如果規則應套用至網路中的所有目標,請省略 --target-tags 和 --target-service-accounts。
    • 使用 --target-tags 旗標,依網路標記定義目標。
    • 使用 --target-service-accounts 旗標,依據相關聯的服務帳戶定義目標。
  • 如要進一步調整輸入規則的目的地,請使用 --destination-ranges,以 CIDR 格式指定 IPv4 或 IPv6 位址範圍。如果省略 --destination-ranges,則輸入目的地為 任何 IPv4 位址 0.0.0.0/0。詳情請參閱「輸入規則的目的地」和「輸入規則的目標和 IP 位址」。

  • 如果是輸入規則,請指定來源:

    • --source-ranges:使用這個旗標,以 CIDR 格式指定來源 IPv4 或 IPv6 位址範圍。
    • 如果省略 --source-ranges、source-tags 和 --source-service-accounts,輸入來源會是任何 IPv4 位址 0.0.0.0/0。
    • --source-tags:使用這個旗標,依網路標記指定來源執行個體。只有在目標「不是」由服務帳戶指定時,才能依來源標記篩選。詳情請參閱「依服務帳戶與網路標記篩選」。
    • --source-ranges 和 --source-tags 可以搭配使用。如果兩者皆指定,則有效來源集是來源範圍 IP 位址和網路標記識別的執行個體的聯集,即使標記的執行個體在來源範圍中沒有 IP 也是如此。
    • --source-service-accounts:使用這個旗標,依據執行個體使用的服務帳戶指定執行個體。只有在目標「不是」由網路標記指定時,才能依來源服務帳戶篩選。詳情請參閱「依服務帳戶與網路標記篩選」。--source-ranges 和 --source-service-accounts 可以搭配使用。如果兩者皆指定,則有效來源集是來源範圍 IP 位址和來源服務帳戶識別的執行個體的「聯集」,即使來源服務帳戶識別的執行個體在來源範圍中沒有 IP 也是如此。
  • 如要進一步調整輸出規則的來源,請使用 --source-ranges,以 CIDR 格式指定 IPv4 或 IPv6 位址範圍。如果省略 --source-ranges,則輸出來源為任何 IPv4 位址 0.0.0.0/0。詳情請參閱「輸出規則的來源」和「輸出規則的目標和 IP 位址」。

  • 如果是輸出規則,請指定目的地:

    • --destination-ranges:使用這個旗標,以 CIDR 格式指定目的地 IPv4 或 IPv6 位址範圍。
    • 如果省略 --destination-ranges,則輸出目的地為任何 IPv4 位址 0.0.0.0/0。
  • --rules 規則適用的通訊協定和目的地通訊埠清單。使用 all,將規則套用至所有通訊協定和所有目的地通訊埠。需要 --action 旗標。

  • 根據預設,防火牆規則建立後會自動強制執行,不過您可以變更這項行為。

    • 如果同時省略 --disabled 和 --no-disabled,系統會建立並強制執行防火牆規則。
    • --disabled 新增這個旗標,即可建立防火牆規則,但不強制執行。防火牆規則會保持停用狀態,直到您更新防火牆規則來啟用為止。
    • --no-disabled 新增這個旗標,確保強制執行防火牆規則。
  • --enable-logging | --no-enable-logging您可以在建立或更新規則時,啟用防火牆規則記錄功能。虛擬私有雲防火牆規則記錄可讓您稽核、驗證及分析防火牆規則的效果。詳情請參閱「虛擬私有雲防火牆規則記錄」。

    • --logging-metadata如果啟用記錄功能,防火牆規則記錄預設會包含基本和中繼資料欄位。您可以略過中繼資料欄位,節省儲存空間費用。詳情請參閱「使用虛擬私有雲防火牆規則記錄」。

Terraform

如要建立防火牆規則,可以使用 google_compute_firewall 資源。

resource "google_compute_firewall" "rules" {
  name        = "my-firewall-rule"
  network     = "default"
  description = "Creates firewall rule targeting tagged instances"

  allow {
    protocol = "tcp"
    ports    = ["80", "443"]
  }

  source_ranges = ["0.0.0.0/0"]
  target_tags   = ["web"]
}

如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。

API

建立虛擬私有雲防火牆規則。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls
{
  "name": "RULE_NAME",
  "network": "projects/PROJECT-ID/global/networks/NETWORK",
  ... other fields
}

更改下列內容:

  • PROJECT_ID:虛擬私有雲網路所在專案的 ID。
  • NETWORK:建立防火牆規則的虛擬私有雲網路名稱。
  • RULE_NAME:防火牆規則的名稱。

  • 針對輸入防火牆規則,請指定輸入來源和目的地:

    • 使用 sourceRanges、sourceTags 或 sourceServiceAccounts 欄位指定輸入來源。

    • sourceRanges 可以是 IPv4 或 IPv6 範圍,但不能同時包含兩者。如要使用 0.0.0.0/0 範圍,請勿指定任何欄位。

    • sourceTags 和 sourceServiceAccounts 欄位無法同時使用。不過,您可以搭配使用 sourceRanges 與 sourceTags 或 sourceServiceAccounts。如果這麼做,連線只要符合其中一項,防火牆規則就會套用。

    • 對於目標欄位,如果您使用 sourceTags 欄位,就無法使用 targetServiceAccounts 欄位。您必須使用 targetTags 欄位,或不使用目標欄位。同樣地,如果使用 sourceServiceAccounts 欄位,就無法使用 targetTags 欄位。如未指定目標欄位,規則會套用至網路中的所有目標。

    • 使用 destinationRanges 欄位指定輸入目的地。destinationRanges 可以是 IPv4 或 IPv6 範圍,但不能同時包含兩者。
      如未指定目的地, Google Cloud 會使用 0.0.0.0/0。詳情請參閱「輸入規則的目的地」和「輸入規則的目標和 IP 位址」。

  • 如果是輸出防火牆規則,請指定輸出來源和目的地:

    • 使用 sourceRanges 欄位指定輸出來源。sourceRange 可以是 IPv4 或 IPv6 範圍,但不能同時包含兩者。
      如未指定來源, Google Cloud 會使用0.0.0.0/0。詳情請參閱「輸出規則的來源」和「輸出規則的目標和 IP 位址」。

    • 使用 destinationRanges 欄位指定目的地。destinationRanges 可以是 IPv4 或 IPv6 範圍,但不能同時包含兩者。
      如未指定目的地, Google Cloud會使用 0.0.0.0/0。使用 targetTags 或 targetServiceAccounts 欄位,指定規則適用的目標。如未指定目標欄位,規則會套用至網路中的所有目標。

如需各欄位的詳細資訊和說明,請參閱 firewalls.insert 方法。

C#


using Google.Cloud.Compute.V1;
using System.Threading.Tasks;

public class CreateFirewallRuleAsyncSample
{
    public async Task CreateFirewallRuleAsync(
        // TODO(developer): Set your own default values for these parameters or pass different values when calling this method.
        string projectId = "your-project-id",
        string firewallRuleName = "my-test-firewall-rule",
        // Name of the network the rule will be applied to. Some available name formats:
        // projects/{project_id}/global/networks/{network}
        // global/networks/{network}
        string networkName = "global/networks/default")
    {
        Firewall firewallRule = new Firewall
        {
            Name = firewallRuleName,
            Network = networkName,
            Direction = ComputeEnumConstants.Firewall.Direction.Ingress,
            Allowed =
            {
                new Allowed
                {
                    Ports = { "80", "443" },
                    IPProtocol = "tcp"
                }
            },
            TargetTags = { "web" },
            Description = "Allows TCP traffic on port 80 and 443 from anywhere."
        };

        // Note that the default value of priority for the firewall API is 1000.
        // If you check the value of firewallRule.Priority at this point it
        // will be equal to 0, however it is not treated as "set" by the library, and thus
        // the default will be applied to the new rule. If you want to create a rule that
        // has priority == 0, you'll need to explicitly set it: firewallRule.Priority = 0.
        // You can use the firewallRule.HasPriority property to check if the priority has been set.
        // You can use the firewallRule.ClearPriority() method to unset the priority.

        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        FirewallsClient client = await FirewallsClient.CreateAsync();

        // Create the firewall rule in the specified project.
        var firewallRuleCreation = await client.InsertAsync(projectId, firewallRule);

        // Wait for the operation to complete using client-side polling.
        await firewallRuleCreation.PollUntilCompletedAsync();
    }
}

Go

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "cloud.google.com/go/compute/apiv1/computepb"
	"google.golang.org/protobuf/proto"
)

// createFirewallRule creates a firewall rule allowing for incoming HTTP and HTTPS access from the entire Internet.
func createFirewallRule(w io.Writer, projectID, firewallRuleName, networkName string) error {
	// projectID := "your_project_id"
	// firewallRuleName := "europe-central2-b"
	// networkName := "global/networks/default"

	ctx := context.Background()
	firewallsClient, err := compute.NewFirewallsRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewInstancesRESTClient: %w", err)
	}
	defer firewallsClient.Close()

	firewallRule := &computepb.Firewall{
		Allowed: []*computepb.Allowed{
			{
				IPProtocol: proto.String("tcp"),
				Ports:      []string{"80", "443"},
			},
		},
		Direction: proto.String(computepb.Firewall_INGRESS.String()),
		Name:      &firewallRuleName,
		TargetTags: []string{
			"web",
		},
		Network:     &networkName,
		Description: proto.String("Allowing TCP traffic on port 80 and 443 from Internet."),
	}

	// Note that the default value of priority for the firewall API is 1000.
	// If you check the value of `firewallRule.GetPriority()` at this point it
	// will be equal to 0, however it is not treated as "set" by the library and thus
	// the default will be applied to the new rule. If you want to create a rule that
	// has priority == 0, you need to explicitly set it so:

	// firewallRule.Priority = proto.Int32(0)

	req := &computepb.InsertFirewallRequest{
		Project:          projectID,
		FirewallResource: firewallRule,
	}

	op, err := firewallsClient.Insert(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to create firewall rule: %w", err)
	}

	if err = op.Wait(ctx); err != nil {
		return fmt.Errorf("unable to wait for the operation: %w", err)
	}

	fmt.Fprintf(w, "Firewall rule created\n")

	return nil
}

Java


import com.google.cloud.compute.v1.Allowed;
import com.google.cloud.compute.v1.Firewall;
import com.google.cloud.compute.v1.Firewall.Direction;
import com.google.cloud.compute.v1.FirewallsClient;
import com.google.cloud.compute.v1.InsertFirewallRequest;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateFirewallRule {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample
    /* project: project ID or project number of the Cloud project you want to use.
       firewallRuleName: name of the rule that is created.
       network: name of the network the rule will be applied to. Available name formats:
        * https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
        * projects/{project_id}/global/networks/{network}
        * global/networks/{network} */
    String project = "your-project-id";
    String firewallRuleName = "firewall-rule-name-" + UUID.randomUUID();
    String network = "global/networks/default";

    // The rule will be created with default priority of 1000.
    createFirewall(project, firewallRuleName, network);
  }

  // Creates a simple firewall rule allowing for incoming HTTP and 
  // HTTPS access from the entire Internet.
  public static void createFirewall(String project, String firewallRuleName, String network)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    /* Initialize client that will be used to send requests. This client only needs to be created
       once, and can be reused for multiple requests. After completing all of your requests, call
       the `firewallsClient.close()` method on the client to safely
       clean up any remaining background resources. */
    try (FirewallsClient firewallsClient = FirewallsClient.create()) {

      // The below firewall rule is created in the default network.
      Firewall firewallRule = Firewall.newBuilder()
          .setName(firewallRuleName)
          .setDirection(Direction.INGRESS.toString())
          .addAllowed(
              Allowed.newBuilder().addPorts("80").addPorts("443").setIPProtocol("tcp").build())
          .addSourceRanges("0.0.0.0/0")
          .setNetwork(network)
          .addTargetTags("web")
          .setDescription("Allowing TCP traffic on port 80 and 443 from Internet.")
          .build();

      /* Note that the default value of priority for the firewall API is 1000.
         If you check the value of `firewallRule.getPriority()` at this point it
         will be equal to 0, however it is not treated as "set" by the library and thus
         the default will be applied to the new rule. If you want to create a rule that
         has priority == 0, you'll need to explicitly set it so: setPriority(0) */

      InsertFirewallRequest insertFirewallRequest = InsertFirewallRequest.newBuilder()
          .setFirewallResource(firewallRule)
          .setProject(project).build();

      firewallsClient.insertAsync(insertFirewallRequest).get(3, TimeUnit.MINUTES);

      System.out.println("Firewall rule created successfully -> " + firewallRuleName);
    }
  }
}

Node.js

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const firewallRuleName = 'YOUR_FIREWALL_RULE_NAME'
// const networkName = 'global/networks/default'

const compute = require('@google-cloud/compute');
const computeProtos = compute.protos.google.cloud.compute.v1;

async function createFirewallRule() {
  const firewallsClient = new compute.FirewallsClient();
  const operationsClient = new compute.GlobalOperationsClient();

  const firewallRule = new computeProtos.Firewall();
  firewallRule.name = firewallRuleName;
  firewallRule.direction = 'INGRESS';
  firewallRule.allowed = [
    {
      IPProtocol: 'tcp',
      ports: ['80', '443'],
    },
  ];
  firewallRule.targetTags = ['web'];
  firewallRule.network = networkName;
  firewallRule.description =
    'Allowing TCP traffic on port 80 and 443 from Internet.';

  // Note that the default value of priority for the firewall API is 1000.
  // If you check the value of `firewallRule.priority` at this point it
  // will be equal to null, however it is not treated as "set" by the library and thus
  // the default will be applied to the new rule. If you want to create a rule that
  // has priority == 0, you need to explicitly set it so:

  // firewallRule.priority = 0

  const [response] = await firewallsClient.insert({
    project: projectId,
    firewallResource: firewallRule,
  });
  let operation = response.latestResponse;

  // Wait for the create operation to complete.
  while (operation.status !== 'DONE') {
    [operation] = await operationsClient.wait({
      operation: operation.name,
      project: projectId,
    });
  }

  console.log('Firewall rule created');
}

createFirewallRule();

PHP

use Google\Cloud\Compute\V1\Allowed;
use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\Firewall;
use Google\Cloud\Compute\V1\Firewall\Direction;
use Google\Cloud\Compute\V1\InsertFirewallRequest;

/**
 * Creates a simple firewall rule allowing incoming HTTP and HTTPS access from the entire internet.
 *
 * @param string $projectId Project ID or project number of the Cloud project you want to create a rule for.
 * @param string $firewallRuleName Name of the rule that is created.
 * @param string $network Name of the network the rule will be applied to. Available name formats:
 *                        https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
 *                        projects/{project_id}/global/networks/{network}
 *                        global/networks/{network}
 *
 * @throws \Google\ApiCore\ApiException if the remote call fails.
 * @throws \Google\ApiCore\ValidationException if local error occurs before remote call.
 */

function create_firewall_rule(string $projectId, string $firewallRuleName, string $network = 'global/networks/default')
{
    $firewallsClient = new FirewallsClient();
    $allowedPorts = (new Allowed())
      ->setIPProtocol('tcp')
      ->setPorts(['80', '443']);
    $firewallResource = (new Firewall())
      ->setName($firewallRuleName)
      ->setDirection(Direction::name(Direction::INGRESS))
      ->setAllowed([$allowedPorts])
      ->setSourceRanges(['0.0.0.0/0'])
      ->setTargetTags(['web'])
      ->setNetwork($network)
      ->setDescription('Allowing TCP traffic on ports 80 and 443 from Internet.');

    /**
    * Note that the default value of priority for the firewall API is 1000.
    * If you check the value of its priority at this point it will be
    * equal to 0, however it is not treated as "set" by the library and thus
    * the default will be applied to the new rule. If you want to create a rule
    * that has priority == 0, you need to explicitly set it so:
    *
    *   $firewallResource->setPriority(0);
    */

    //Create the firewall rule using Firewalls Client.
    $request = (new InsertFirewallRequest())
        ->setFirewallResource($firewallResource)
        ->setProject($projectId);
    $operation = $firewallsClient->insert($request);

    // Wait for the operation to complete.
    $operation->pollUntilComplete();
    if ($operation->operationSucceeded()) {
        printf('Created rule %s.' . PHP_EOL, $firewallRuleName);
    } else {
        $error = $operation->getError();
        printf('Firewall rule creation failed: %s' . PHP_EOL, $error?->getMessage());
    }
}

Python

from __future__ import annotations

import sys
from typing import Any

from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1


def wait_for_extended_operation(
    operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
    """
    Waits for the extended (long-running) operation to complete.

    If the operation is successful, it will return its result.
    If the operation ends with an error, an exception will be raised.
    If there were any warnings during the execution of the operation
    they will be printed to sys.stderr.

    Args:
        operation: a long-running operation you want to wait on.
        verbose_name: (optional) a more verbose name of the operation,
            used only during error and warning reporting.
        timeout: how long (in seconds) to wait for operation to finish.
            If None, wait indefinitely.

    Returns:
        Whatever the operation.result() returns.

    Raises:
        This method will raise the exception received from `operation.exception()`
        or RuntimeError if there is no exception set, but there is an `error_code`
        set for the `operation`.

        In case of an operation taking longer than `timeout` seconds to complete,
        a `concurrent.futures.TimeoutError` will be raised.
    """
    result = operation.result(timeout=timeout)

    if operation.error_code:
        print(
            f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
            file=sys.stderr,
            flush=True,
        )
        print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
        raise operation.exception() or RuntimeError(operation.error_message)

    if operation.warnings:
        print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
        for warning in operation.warnings:
            print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

    return result


def create_firewall_rule(
    project_id: str, firewall_rule_name: str, network: str = "global/networks/default"
) -> compute_v1.Firewall:
    """
    Creates a simple firewall rule allowing for incoming HTTP and HTTPS access from the entire Internet.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        firewall_rule_name: name of the rule that is created.
        network: name of the network the rule will be applied to. Available name formats:
            * https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
            * projects/{project_id}/global/networks/{network}
            * global/networks/{network}

    Returns:
        A Firewall object.
    """
    firewall_rule = compute_v1.Firewall()
    firewall_rule.name = firewall_rule_name
    firewall_rule.direction = "INGRESS"

    allowed_ports = compute_v1.Allowed()
    allowed_ports.I_p_protocol = "tcp"
    allowed_ports.ports = ["80", "443"]

    firewall_rule.allowed = [allowed_ports]
    firewall_rule.source_ranges = ["0.0.0.0/0"]
    firewall_rule.network = network
    firewall_rule.description = "Allowing TCP traffic on port 80 and 443 from Internet."

    firewall_rule.target_tags = ["web"]

    # Note that the default value of priority for the firewall API is 1000.
    # If you check the value of `firewall_rule.priority` at this point it
    # will be equal to 0, however it is not treated as "set" by the library and thus
    # the default will be applied to the new rule. If you want to create a rule that
    # has priority == 0, you need to explicitly set it so:
    # TODO: Uncomment to set the priority to 0
    # firewall_rule.priority = 0

    firewall_client = compute_v1.FirewallsClient()
    operation = firewall_client.insert(
        project=project_id, firewall_resource=firewall_rule
    )

    wait_for_extended_operation(operation, "firewall rule creation")

    return firewall_client.get(project=project_id, firewall=firewall_rule_name)

Ruby


require "google/cloud/compute/v1"

# Creates a simple firewall rule allowing for incoming HTTP and HTTPS access from the entire Internet.
#
# @param [String] project project ID or project number of the Cloud project you want to use.
# @param [String] name: name of the rule that is created.
# @param network: name of the network the rule will be applied to. Available name formats:
#         * https://www.googleapis.com/compute/v1/projects/{project_id}/global/networks/{network}
#         * projects/{project_id}/global/networks/{network}
#         * global/networks/{network}
def create_firewall_rule project:, name:, network: "global/networks/default"
  rule = {
    name: name,
    direction: "INGRESS",
    allowed: [{
      I_p_protocol: "tcp",
      ports: ["80", "443"]
    }],
    source_ranges: ["0.0.0.0/0"],
    network: network,
    description: "Allowing TCP traffic on port 80 and 443 from Internet.",
    target_tags: ["web"]
  }

  # Note that the default value of priority for the firewall API is 1000.
  # If you want to create a rule that has priority == 0, you need to explicitly set it:
  #   rule[:priority] = 0
  # Use `rule.has_key? :priority` to check if the priority has been set.
  # Use `rule.delete :priority` method to unset the priority.

  request = {
    firewall_resource: rule,
    project: project
  }

  client = ::Google::Cloud::Compute::V1::Firewalls::Rest::Client.new
  operation = client.insert request

  wait_until_done operation: operation
end

更新虛擬私有雲防火牆規則

您可以修改虛擬私有雲防火牆規則的部分元件,例如比對條件的指定通訊協定和目的地通訊埠。您無法修改防火牆規則的名稱、網路、相符時執行的動作和流量方向。

如需變更名稱、網路、動作或方向元件,請刪除規則,並建立新規則。

如要新增或移除多個服務帳戶,請使用 gcloud CLI、API 或用戶端程式庫。您無法使用 Google Cloud 控制台指定多個目標服務帳戶或來源服務帳戶。

控制台

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 按一下要修改的防火牆規則。

  3. 按一下 [Edit] (編輯)。

  4. 修改任何可編輯的元件以符合您的需求。

  5. 按一下「儲存」。

gcloud

如要更新虛擬私有雲防火牆規則,請使用 gcloud compute firewall-rules update 指令:

gcloud compute firewall-rules update RULE_NAME \
    [--priority=PRIORITY] \
    [--description=DESCRIPTION] \
    [--target-tags=TAG,...] \
    [--target-service-accounts=IAM_SERVICE_ACCOUNT,_] \
    [--source-ranges=CIDR_RANGE,...] \
    [--source-tags=TAG,...] \
    [--source-service-accounts=IAM_SERVICE_ACCOUNT,_] \
    [--destination-ranges=CIDR_RANGE,...] \
    [--rules=[PROTOCOL[:PORT[-PORT]],…]] \
    [--disabled | --no-disabled] \
    [--enable-logging | --no-enable-logging]

每個旗標的說明與建立防火牆規則時相同,如需各旗標的詳細資訊,請參閱 SDK 參考文件。

API

使用 PATCH 更新下列欄位:allowed、description、sourceRanges、sourceTags 或 targetTags。所有其他欄位則使用 PUT 或 POST。

(PATCH|(POST|PUT)) https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls/RULE_NAME
{
  "name": "RULE_NAME",
  "network": "projects/PROJECT-ID/global/networks/NETWORK",
  ... other fields
}

更改下列內容:

  • PROJECT_ID:虛擬私有雲網路所在專案的 ID。
  • NETWORK:防火牆規則所在的虛擬私有雲網路名稱。
  • RULE_NAME:要更新的防火牆規則名稱。

如需各欄位的詳細資訊和說明,請參閱 firewalls.patch 或 firewalls.update 方法。

C#


using Google.Cloud.Compute.V1;
using System.Threading.Tasks;

public class PatchFirewallRuleAsyncSample
{
    public async Task PatchFirewallRuleAsync(
        // TODO(developer): Set your own default values for these parameters or pass different values when calling this method.
        string projectId = "your-project-id",
        string firewallRuleName = "my-test-firewall-rule",
        int newPriority = 10)
    {
        // The patch operation doesn't require the full definition of a Firewall object.
        // It will only update the values that were set in it,
        // in this case it will only change the priority.
        Firewall firewallRule = new Firewall
        {
            Priority = newPriority
        };

        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        FirewallsClient client = await FirewallsClient.CreateAsync();

        // Patch the firewall rule in the specified project.
        var firewallRulePatching = await client.PatchAsync(projectId, firewallRuleName, firewallRule);

        // Wait for the operation to complete using client-side polling.
        await firewallRulePatching.PollUntilCompletedAsync();
    }
}

Go

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "cloud.google.com/go/compute/apiv1/computepb"
	"google.golang.org/protobuf/proto"
)

// patchFirewallPriority modifies the priority of a given firewall rule.
func patchFirewallPriority(w io.Writer, projectID, firewallRuleName string, priority int32) error {
	// projectID := "your_project_id"
	// firewallRuleName := "europe-central2-b"
	// priority := 10

	ctx := context.Background()
	firewallsClient, err := compute.NewFirewallsRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewInstancesRESTClient: %w", err)
	}
	defer firewallsClient.Close()

	firewallRule := &computepb.Firewall{
		Priority: proto.Int32(priority),
	}

	req := &computepb.PatchFirewallRequest{
		Project:          projectID,
		Firewall:         firewallRuleName,
		FirewallResource: firewallRule,
	}

	// The patch operation doesn't require the full definition of a Firewall interface. It will only update
	// the values that were set in it, in this case it will only change the priority.
	op, err := firewallsClient.Patch(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to patch firewall rule: %w", err)
	}

	if err = op.Wait(ctx); err != nil {
		return fmt.Errorf("unable to wait for the operation: %w", err)
	}

	fmt.Fprintf(w, "Firewall rule updated\n")

	return nil
}

Java


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.compute.v1.Firewall;
import com.google.cloud.compute.v1.FirewallsClient;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.PatchFirewallRequest;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class PatchFirewallRule {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample
    // project: project ID or project number of the Cloud project you want to use.
    // firewallRuleName: name of the rule you want to modify.
    // priority: the new priority to be set for the rule.
    String project = "your-project-id";
    String firewallRuleName = "firewall-rule-name-" + UUID.randomUUID();
    int priority = 10;

    patchFirewallPriority(project, firewallRuleName, priority);
  }

  // Modifies the priority of a given firewall rule.
  public static void patchFirewallPriority(String project, String firewallRuleName, int priority)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    /* Initialize client that will be used to send requests. This client only needs to be created
       once, and can be reused for multiple requests. After completing all of your requests, call
       the `firewallsClient.close()` method on the client to safely
       clean up any remaining background resources. */
    try (FirewallsClient firewallsClient = FirewallsClient.create()) {

      /* The patch operation doesn't require the full definition of a Firewall object. It will only 
         update the values that were set in it, in this case it will only change the priority. */
      Firewall firewall = Firewall.newBuilder()
          .setPriority(priority).build();

      PatchFirewallRequest patchFirewallRequest = PatchFirewallRequest.newBuilder()
          .setProject(project)
          .setFirewall(firewallRuleName)
          .setFirewallResource(firewall).build();

      OperationFuture<Operation, Operation> operation = firewallsClient.patchAsync(
          patchFirewallRequest);
      operation.get(3, TimeUnit.MINUTES);
      System.out.println("Firewall Patch applied successfully ! ");
    }
  }
}

Node.js

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const firewallRuleName = 'FIREWALL_RULE_NAME';
// const priority = 10;

const compute = require('@google-cloud/compute');
const computeProtos = compute.protos.google.cloud.compute.v1;

async function patchFirewallPriority() {
  const firewallsClient = new compute.FirewallsClient();
  const operationsClient = new compute.GlobalOperationsClient();

  const firewallRule = new computeProtos.Firewall();
  firewallRule.priority = priority;

  // The patch operation doesn't require the full definition of a Firewall object. It will only update
  // the values that were set in it, in this case it will only change the priority.
  const [response] = await firewallsClient.patch({
    project: projectId,
    firewall: firewallRuleName,
    firewallResource: firewallRule,
  });
  let operation = response.latestResponse;

  // Wait for the create operation to complete.
  while (operation.status !== 'DONE') {
    [operation] = await operationsClient.wait({
      operation: operation.name,
      project: projectId,
    });
  }

  console.log('Firewall rule updated');
}

patchFirewallPriority();

PHP

use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\Firewall;
use Google\Cloud\Compute\V1\PatchFirewallRequest;

/**
 * Modifies the priority of a given firewall rule.
 *
 * @param string $projectId Project ID or project number of the Cloud project you want to patch a rule from.
 * @param string $firewallRuleName Name of the rule that you want to modify.
 * @param int $priority The new priority to be set for the rule.
 *
 * @throws \Google\ApiCore\ApiException if the remote call fails.
 * @throws \Google\ApiCore\ValidationException if local error occurs before remote call.
 */
function patch_firewall_priority(string $projectId, string $firewallRuleName, int $priority)
{
    $firewallsClient = new FirewallsClient();
    $firewallResource = (new Firewall())->setPriority($priority);

    // The patch operation doesn't require the full definition of a Firewall object. It will only update
    // the values that were set in it, in this case it will only change the priority.
    $request = (new PatchFirewallRequest())
        ->setFirewall($firewallRuleName)
        ->setFirewallResource($firewallResource)
        ->setProject($projectId);
    $operation = $firewallsClient->patch($request);

    // Wait for the operation to complete.
    $operation->pollUntilComplete();
    if ($operation->operationSucceeded()) {
        printf('Patched %s priority to %d.' . PHP_EOL, $firewallRuleName, $priority);
    } else {
        $error = $operation->getError();
        printf('Patching failed: %s' . PHP_EOL, $error?->getMessage());
    }
}

Python

from __future__ import annotations

import sys
from typing import Any

from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1


def wait_for_extended_operation(
    operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
    """
    Waits for the extended (long-running) operation to complete.

    If the operation is successful, it will return its result.
    If the operation ends with an error, an exception will be raised.
    If there were any warnings during the execution of the operation
    they will be printed to sys.stderr.

    Args:
        operation: a long-running operation you want to wait on.
        verbose_name: (optional) a more verbose name of the operation,
            used only during error and warning reporting.
        timeout: how long (in seconds) to wait for operation to finish.
            If None, wait indefinitely.

    Returns:
        Whatever the operation.result() returns.

    Raises:
        This method will raise the exception received from `operation.exception()`
        or RuntimeError if there is no exception set, but there is an `error_code`
        set for the `operation`.

        In case of an operation taking longer than `timeout` seconds to complete,
        a `concurrent.futures.TimeoutError` will be raised.
    """
    result = operation.result(timeout=timeout)

    if operation.error_code:
        print(
            f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
            file=sys.stderr,
            flush=True,
        )
        print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
        raise operation.exception() or RuntimeError(operation.error_message)

    if operation.warnings:
        print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
        for warning in operation.warnings:
            print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

    return result


def patch_firewall_priority(
    project_id: str, firewall_rule_name: str, priority: int
) -> None:
    """
    Modifies the priority of a given firewall rule.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        firewall_rule_name: name of the rule you want to modify.
        priority: the new priority to be set for the rule.
    """
    firewall_rule = compute_v1.Firewall()
    firewall_rule.priority = priority

    # The patch operation doesn't require the full definition of a Firewall object. It will only update
    # the values that were set in it, in this case it will only change the priority.
    firewall_client = compute_v1.FirewallsClient()
    operation = firewall_client.patch(
        project=project_id, firewall=firewall_rule_name, firewall_resource=firewall_rule
    )

    wait_for_extended_operation(operation, "firewall rule patching")

Ruby


require "google/cloud/compute/v1"

# Modifies the priority of a given firewall rule.
#
# @param [String] project project ID or project number of the Cloud project you want to use.
# @param [String] name name of the rule you want to modify.
# @param [Google::Protobuf::RepeatedField] allowed the repeated instances of the Allowed field in the rule.
#         Compute errors out if allowed is empty.
# @param [Integer] priority the new priority to be set for the rule.
def patch_firewall_priority project:, name:, allowed:, priority:
  allowed_arr = allowed.map do |instance|
    {
      I_p_protocol: instance.I_p_protocol,
      ports: instance.ports.to_a
    }
  end.to_a

  rule = {
    priority: priority,
    allowed: allowed_arr
  }

  request = {
    project: project,
    firewall: name,
    firewall_resource: rule
  }

  # The patch operation doesn't require the full definition of a Firewall object. It will only update
  # the values that were set in it, in this case it will only change the priority.
  client = ::Google::Cloud::Compute::V1::Firewalls::Rest::Client.new
  operation = client.patch request

  wait_until_done operation: operation
end

列出虛擬私有雲網路的虛擬私有雲防火牆規則

您可以列出專案或特定虛擬私有雲網路的所有虛擬私有雲防火牆規則。Google Cloud 會顯示每項防火牆規則的詳細資料,例如規則類型、目標和篩選器。

啟用虛擬私有雲防火牆規則記錄後,防火牆洞察功能會提供防火牆規則的洞察資料,協助您進一步瞭解並安全地最佳化設定。舉例來說,您可以查看過去六週內未使用的 allow 規則。詳情請參閱防火牆洞察說明文件中的「使用防火牆規則詳細資料畫面」。

控制台

如要顯示專案中所有網路的虛擬私有雲防火牆規則,請執行下列指令:

如要顯示特定網路中的虛擬私有雲防火牆規則,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

    前往「VPC networks」(虛擬私有雲網路)

  2. 按一下虛擬私有雲網路的「Name」(名稱),前往詳細資料頁面。

  3. 在網路的詳細資料頁面中,按一下「防火牆」分頁標籤。

  4. 展開「vpc-firewall-rules」。

gcloud

如要產生特定網路的虛擬私有雲防火牆規則排序清單,請使用 gcloud compute firewall-rules list 指令:

gcloud compute firewall-rules list --filter network=NETWORK \
    --sort-by priority \
    --format="table(
        name,
        network,
        direction,
        priority,
        sourceRanges.list():label=SRC_RANGES,
        destinationRanges.list():label=DEST_RANGES,
        allowed[].map().firewall_rule().list():label=ALLOW,
        denied[].map().firewall_rule().list():label=DENY,
        sourceTags.list():label=SRC_TAGS,
        targetTags.list():label=TARGET_TAGS
        )"

將 NETWORK 替換為要列出防火牆規則的網路名稱。

API

列出指定網路的所有虛擬私有雲防火牆規則。

GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls/?filter=network="NETWORK"

更改下列內容:

  • PROJECT_ID:虛擬私有雲網路所在專案的 ID。
  • NETWORK:虛擬私有雲網路名稱,其中包含要列出的防火牆規則。

詳情請參閱 firewalls.list 方法。

C#


using Google.Cloud.Compute.V1;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public class ListFirewallRulesAsyncSample
{
    public async Task ListFirewallRulesAsync(
        // TODO(developer): Set your own default values for these parameters or pass different values when calling this method.
        string projectId = "your-project-id")
    {
        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        FirewallsClient client = await FirewallsClient.CreateAsync();

        // Make the request to list all firewall rules.
        await foreach (var firewallRule in client.ListAsync(projectId))
        {
            // The result is a Firewall sequence that you can iterate over.
            Console.WriteLine($"Firewal Rule: {firewallRule.Name}");
        }
    }
}

Go

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "cloud.google.com/go/compute/apiv1/computepb"
	"google.golang.org/api/iterator"
)

// listFirewallRules prints the list of firewall names and their descriptions in specified project
func listFirewallRules(w io.Writer, projectID string) error {
	// projectID := "your_project_id"

	ctx := context.Background()
	firewallsClient, err := compute.NewFirewallsRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewInstancesRESTClient: %w", err)
	}
	defer firewallsClient.Close()

	req := &computepb.ListFirewallsRequest{
		Project: projectID,
	}

	it := firewallsClient.List(ctx, req)
	for {
		firewallRule, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return err
		}
		fmt.Fprintf(w, "- %s: %s\n", firewallRule.GetName(), firewallRule.GetDescription())
	}

	return nil
}

Java


import com.google.cloud.compute.v1.Firewall;
import com.google.cloud.compute.v1.FirewallsClient;
import com.google.cloud.compute.v1.FirewallsClient.ListPagedResponse;
import java.io.IOException;

public class ListFirewallRules {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample
    // project: project ID or project number of the Cloud project you want to use.
    String project = "your-project-id";
    listFirewallRules(project);
  }

  // Return a list of all the firewall rules in specified project.
  // Also prints the list of firewall names and their descriptions.
  public static ListPagedResponse listFirewallRules(String project)
      throws IOException {
    /* Initialize client that will be used to send requests. This client only needs to be created
       once, and can be reused for multiple requests. After completing all of your requests, call
       the `firewallsClient.close()` method on the client to safely
       clean up any remaining background resources. */
    try (FirewallsClient firewallsClient = FirewallsClient.create()) {
      ListPagedResponse firewallResponse = firewallsClient.list(project);
      for (Firewall firewall : firewallResponse.iterateAll()) {
        System.out.println(firewall.getName());
      }
      return firewallResponse;
    }
  }
}

Node.js

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';

const compute = require('@google-cloud/compute');

async function listFirewallRules() {
  const firewallsClient = new compute.FirewallsClient();

  const [firewallRules] = await firewallsClient.list({
    project: projectId,
  });

  for (const rule of firewallRules) {
    console.log(` - ${rule.name}: ${rule.description}`);
  }
}

listFirewallRules();

PHP

use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\ListFirewallsRequest;

/**
 * Return a list of all the firewall rules in specified project. Also prints the
 * list of firewall names and their descriptions.
 *
 * @param string $projectId Project ID or project number of the Cloud project you want to list rules from.
 *
 * @throws \Google\ApiCore\ApiException if the remote call fails.
 */
function list_firewall_rules(string $projectId)
{
    // List all firewall rules defined for the project using Firewalls Client.
    $firewallClient = new FirewallsClient();
    $request = (new ListFirewallsRequest())
        ->setProject($projectId);
    $firewallList = $firewallClient->list($request);

    print('--- Firewall Rules ---' . PHP_EOL);
    foreach ($firewallList->iterateAllElements() as $firewall) {
        printf(' -  %s : %s : %s' . PHP_EOL, $firewall->getName(), $firewall->getDescription(), $firewall->getNetwork());
    }
}

Python

from __future__ import annotations

from collections.abc import Iterable

from google.cloud import compute_v1


def list_firewall_rules(project_id: str) -> Iterable[compute_v1.Firewall]:
    """
    Return a list of all the firewall rules in specified project. Also prints the
    list of firewall names and their descriptions.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.

    Returns:
        A flat list of all firewall rules defined for given project.
    """
    firewall_client = compute_v1.FirewallsClient()
    firewalls_list = firewall_client.list(project=project_id)

    for firewall in firewalls_list:
        print(f" - {firewall.name}: {firewall.description}")

    return firewalls_list

Ruby


require "google/cloud/compute/v1"

# Return a list of all the firewall rules in specified project. Also prints the
# list of firewall names and their descriptions.
#
# @param [String] project project ID or project number of the project you want to use.
# @return [Array<::Google::Cloud::Compute::V1::Firewall>]
#     A list of all firewall rules defined for the given project.
def list_firewall_rules project:
  client = ::Google::Cloud::Compute::V1::Firewalls::Rest::Client.new
  firewalls = client.list project: project

  firewall_list = []
  firewalls.each do |firewall|
    puts " - #{firewall.name}: #{firewall.description}"
    firewall_list << firewall
  end

  firewall_list
end

列出 VM 執行個體網路介面的虛擬私有雲防火牆規則

針對每個網路介面, Google Cloud 控制台會列出適用於該介面的所有虛擬私有雲防火牆規則,以及該介面實際使用的規則。防火牆規則可以遮蓋其他規則,因此套用至介面的所有規則不一定會實際由介面使用。

防火牆規則會透過規則的目標參數,與 VM 執行個體建立關聯並套用至 VM 執行個體。您可以查看所有套用規則,確認特定規則是否正套用至介面。

啟用防火牆政策規則記錄後,防火牆洞察會提供防火牆規則的洞察資料,協助您進一步瞭解並安全地最佳化設定。舉例來說,您可以查看過去六週內,介面上有哪些規則發揮作用。詳情請參閱防火牆洞察說明文件的「使用 VM 網路介面詳細資料畫面」一節。

控制台

如要查看適用於 VM 執行個體特定網路介面的虛擬私有雲規則,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 找出要查看的執行個體。

  3. 在執行個體的「more actions」(更多動作) 選單 () 中,選取「View network details」(查看網路詳細資料)。

  4. 如果執行個體有多個網路介面,請在「Selected network interface」(所選網路介面) 欄位中選取要查看的網路介面。

  5. 在「Firewall and routes details」(防火牆和路徑詳細資料) 區段中,選取「Firewalls」(防火牆) 分頁標籤。

  6. 展開「vpc-firewall-rules」。

  7. 查看表格,判斷是否允許傳送至特定 IP 位址或來自特定 IP 位址的流量。

查看虛擬私有雲防火牆規則詳細資料

您可以檢查虛擬私有雲防火牆規則,查看其名稱、適用的網路和元件,包括規則是否啟用。

控制台

  1. 列出您的防火牆規則,您可以查看所有規則的清單或特定網路中的規則清單。
  2. 按一下要查看的規則。

gcloud

下列指令會說明個別虛擬私有雲防火牆規則。由於防火牆規則名稱在專案中是唯一的,因此在說明現有防火牆規則時,您不必指定網路。

gcloud compute firewall-rules describe RULE_NAME

將 RULE_NAME 替換為防火牆規則名稱。

API

說明指定的虛擬私有雲防火牆規則。

GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls/RULE_NAME

將預留位置替換為有效值:

  • PROJECT_ID:防火牆規則所在的專案 ID。
  • RULE_NAME:要說明的防火牆規則名稱。

詳情請參閱 firewalls.get 方法。

刪除虛擬私有雲防火牆規則

控制台

  1. 列出虛擬私有雲防火牆規則。您可以查看所有規則的清單,或只查看特定網路中的規則。
  2. 按一下要刪除的規則。
  3. 按一下 [Delete] (刪除)。
  4. 再按一下 [刪除] 加以確認。

gcloud

如要刪除虛擬私有雲防火牆規則,請使用 gcloud compute firewall-rules delete 指令:

gcloud compute firewall-rules delete RULE_NAME

將 RULE_NAME 替換為要刪除的規則名稱。

API

刪除虛擬私有雲防火牆規則。

DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls/RULE_NAME

更改下列內容:

  • PROJECT_ID:防火牆規則所在的專案 ID。
  • RULE_NAME:要刪除的防火牆規則名稱。

詳情請參閱 firewalls.delete 方法。

C#


using Google.Cloud.Compute.V1;
using System.Threading.Tasks;

public class DeleteFirewallRuleAsyncSample
{
    public async Task DeleteFirewallRuleAsync(
        // TODO(developer): Set your own default values for these parameters or pass different values when calling this method.
        string projectId = "your-project-id",
        string firewallRuleName = "my-test-firewall-rule")
    {

        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        FirewallsClient client = await FirewallsClient.CreateAsync();

        // Make the request to delete the firewall rule.
        var firewallRuleDeletion = await client.DeleteAsync(projectId, firewallRuleName);

        // Wait for the operation to complete using client-side polling.
        await firewallRuleDeletion.PollUntilCompletedAsync();
    }
}

Go

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "cloud.google.com/go/compute/apiv1/computepb"
)

// deleteFirewallRule deletes a firewall rule from the project.
func deleteFirewallRule(w io.Writer, projectID, firewallRuleName string) error {
	// projectID := "your_project_id"
	// firewallRuleName := "europe-central2-b"

	ctx := context.Background()
	firewallsClient, err := compute.NewFirewallsRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewInstancesRESTClient: %w", err)
	}
	defer firewallsClient.Close()

	req := &computepb.DeleteFirewallRequest{
		Project:  projectID,
		Firewall: firewallRuleName,
	}

	op, err := firewallsClient.Delete(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to delete firewall rule: %w", err)
	}

	if err = op.Wait(ctx); err != nil {
		return fmt.Errorf("unable to wait for the operation: %w", err)
	}

	fmt.Fprintf(w, "Firewall rule deleted\n")

	return nil
}

Java


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.compute.v1.FirewallsClient;
import com.google.cloud.compute.v1.Operation;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DeleteFirewallRule {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample
    // project: project ID or project number of the Cloud project you want to use.
    // firewallRuleName: name of the firewall rule you want to delete.
    String project = "your-project-id";
    String firewallRuleName = "firewall-rule-name-" + UUID.randomUUID();
    deleteFirewallRule(project, firewallRuleName);
  }


  // Deletes a firewall rule from the project.
  public static void deleteFirewallRule(String project, String firewallRuleName)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    /* Initialize client that will be used to send requests. This client only needs to be created
       once, and can be reused for multiple requests. After completing all of your requests, call
       the `firewallsClient.close()` method on the client to safely
       clean up any remaining background resources. */
    try (FirewallsClient firewallsClient = FirewallsClient.create()) {

      OperationFuture<Operation, Operation> operation = firewallsClient.deleteAsync(project,
          firewallRuleName);
      operation.get(3, TimeUnit.MINUTES);

      System.out.println("Deleted firewall rule -> " + firewallRuleName);
    }
  }
}

Node.js

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
// const firewallRuleName = 'FIREWALL_RULE_NAME';

const compute = require('@google-cloud/compute');

async function deleteFirewallRule() {
  const firewallsClient = new compute.FirewallsClient();
  const operationsClient = new compute.GlobalOperationsClient();

  const [response] = await firewallsClient.delete({
    project: projectId,
    firewall: firewallRuleName,
  });
  let operation = response.latestResponse;

  // Wait for the create operation to complete.
  while (operation.status !== 'DONE') {
    [operation] = await operationsClient.wait({
      operation: operation.name,
      project: projectId,
    });
  }

  console.log('Firewall rule deleted');
}

deleteFirewallRule();

PHP

use Google\Cloud\Compute\V1\Client\FirewallsClient;
use Google\Cloud\Compute\V1\DeleteFirewallRequest;

/**
 * Delete a firewall rule from the specified project.
 *
 * @param string $projectId Project ID or project number of the Cloud project you want to delete a rule for.
 * @param string $firewallRuleName Name of the rule that is deleted.
 *
 * @throws \Google\ApiCore\ApiException if the remote call fails.
 * @throws \Google\ApiCore\ValidationException if local error occurs before remote call.
 */
function delete_firewall_rule(string $projectId, string $firewallRuleName)
{
    $firewallsClient = new FirewallsClient();

    // Delete the firewall rule using Firewalls Client.
    $request = (new DeleteFirewallRequest())
        ->setFirewall($firewallRuleName)
        ->setProject($projectId);
    $operation = $firewallsClient->delete($request);

    // Wait for the operation to complete.
    $operation->pollUntilComplete();
    if ($operation->operationSucceeded()) {
        printf('Rule %s deleted successfully!' . PHP_EOL, $firewallRuleName);
    } else {
        $error = $operation->getError();
        printf('Failed to delete firewall rule: %s' . PHP_EOL, $error?->getMessage());
    }
}

Python

from __future__ import annotations

import sys
from typing import Any

from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1


def wait_for_extended_operation(
    operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
    """
    Waits for the extended (long-running) operation to complete.

    If the operation is successful, it will return its result.
    If the operation ends with an error, an exception will be raised.
    If there were any warnings during the execution of the operation
    they will be printed to sys.stderr.

    Args:
        operation: a long-running operation you want to wait on.
        verbose_name: (optional) a more verbose name of the operation,
            used only during error and warning reporting.
        timeout: how long (in seconds) to wait for operation to finish.
            If None, wait indefinitely.

    Returns:
        Whatever the operation.result() returns.

    Raises:
        This method will raise the exception received from `operation.exception()`
        or RuntimeError if there is no exception set, but there is an `error_code`
        set for the `operation`.

        In case of an operation taking longer than `timeout` seconds to complete,
        a `concurrent.futures.TimeoutError` will be raised.
    """
    result = operation.result(timeout=timeout)

    if operation.error_code:
        print(
            f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
            file=sys.stderr,
            flush=True,
        )
        print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
        raise operation.exception() or RuntimeError(operation.error_message)

    if operation.warnings:
        print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
        for warning in operation.warnings:
            print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

    return result


def delete_firewall_rule(project_id: str, firewall_rule_name: str) -> None:
    """
    Deletes a firewall rule from the project.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        firewall_rule_name: name of the firewall rule you want to delete.
    """
    firewall_client = compute_v1.FirewallsClient()
    operation = firewall_client.delete(project=project_id, firewall=firewall_rule_name)

    wait_for_extended_operation(operation, "firewall rule deletion")

Ruby


require "google/cloud/compute/v1"

# Deletes a firewall rule from the project.
#
# @param [String] project project ID or project number of the Cloud project you want to use.
# @param [String] name name of the firewall rule you want to delete.
def delete_firewall_rule project:, name:
  client = ::Google::Cloud::Compute::V1::Firewalls::Rest::Client.new
  operation = client.delete project: project, firewall: name

  wait_until_done operation: operation
end

監控虛擬私有雲防火牆規則

您可以啟用虛擬私有雲防火牆規則記錄功能,查看哪些規則允許或封鎖了哪些流量。如需操作說明,請參閱「使用虛擬私有雲防火牆規則記錄」。

針對常見用途設定虛擬私有雲防火牆規則

以下各節提供範例,說明如何使用 gcloud CLI 和 API,重新建立為預設網路建立的預先定義虛擬私有雲防火牆規則。您可以參考這些範例,為自訂和自動模式網路建立類似規則。每項防火牆規則可包含 IPv4 或 IPv6 位址範圍,但不能同時包含兩者。

允許 VM 之間的內部輸入連線

下列範例會建立防火牆規則,允許內部 TCP、UDP 和 ICMP 連線至 VM 執行個體,類似於預設網路的 allow-internal 規則。

gcloud

使用 gcloud compute firewall-rules create 指令:

gcloud compute firewall-rules create RULE_NAME \
    --action=ALLOW \
    --direction=INGRESS \
    --network=NETWORK \
    --priority=1000 \
    --rules=tcp:0-65535,udp:0-65535,ICMP_PROTOCOL \
    --source-ranges=SUBNET_RANGES

更改下列內容:

  • RULE_NAME:這個防火牆規則的名稱。
  • NETWORK:這項防火牆規則適用的網路名稱。預設值為 default。
  • ICMP_PROTOCOL:ICMP 通訊協定類型。使用通訊協定名稱 icmp 或通訊協定編號 1 指定 ICMPv4。使用通訊協定編號 58 指定 ICMPv6。
  • SUBNET_RANGES:一或多個 IP 位址範圍。如果包含 IP 位址範圍,則該範圍內的流量可以連至虛擬私有雲網路中的任何 VM 目的地。您可以在特定防火牆規則中指定 IPv4 或 IPv6 範圍。

    IPv4 子網路範圍:

    • 自動模式虛擬私有雲網路使用的 IP 位址範圍在 10.128.0.0/9 內。
    • 自訂模式網路可以使用任何有效 IPv4 範圍。如果虛擬私有雲網路中的子網路並非使用連續範圍,您可能需要指定多個範圍。
    • 您可以使用 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,允許來自所有私人 IPv4 位址範圍 (RFC 1918 範圍) 的流量。

    IPv6 子網路範圍:

    • 如果您已為虛擬私有雲網路指派內部 IPv6 位址範圍,則可將該範圍設為來源範圍。使用虛擬私有雲網路的內部 IPv6 範圍,代表防火牆規則包含所有目前和未來的內部 IPv6 子網路範圍。您可以使用下列指令,找出虛擬私有雲網路的內部 IPv6 範圍:

      gcloud compute networks describe NETWORK \
        --format="flattened(internalIpv6Range)"
      

      您也可以指定特定的內部 IPv6 子網路範圍。

    • 如要允許來自外部 IPv6 子網路範圍的流量,您必須指定要納入的每個子網路 IPv6 位址範圍。

API

POST https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls
{
  "kind": "compute#firewall",
  "name": "RULE_NAME",
  "network": "projects/PROJECT_ID/global/networks/NETWORK",
  "direction": "INGRESS",
  "priority": 1000,
  "targetTags": [],
  "allowed": [
    {
      "IPProtocol": "tcp",
      "ports": [
        "0-65535"
      ]
    },
    {
      "IPProtocol": "udp",
      "ports": [
        "0-65535"
      ]
    },
    {
      "IPProtocol": "ICMP_PROTOCOL"
    }
  ],
  "sourceRanges": [
    "SUBNET_RANGES"
  ]
}

更改下列內容:

  • PROJECT_ID:虛擬私有雲網路所在專案的 ID。
  • RULE_NAME:防火牆規則的名稱。
  • NETWORK:建立防火牆規則的虛擬私有雲網路名稱。預設值為 default。
  • ICMP_PROTOCOL:ICMP 通訊協定類型。使用通訊協定名稱 icmp 或通訊協定編號 1 指定 ICMPv4。使用通訊協定編號 58 指定 ICMPv6。
  • INTERNAL_SOURCE_RANGES:一或多個 IP 範圍。如要允許虛擬私有雲網路中所有子網路的內部流量,請指定虛擬私有雲網路中使用的 IP 位址範圍。您可以在特定防火牆規則中指定 IPv4 或 IPv6 範圍。

    IPv4 子網路範圍:

    • 自動模式虛擬私有雲網路使用的 IP 位址範圍在 10.128.0.0/9 內。
    • 自訂模式網路可以使用任何有效 IPv4 範圍。如果虛擬私有雲網路中的子網路並非使用連續範圍,您可能需要指定多個範圍。
    • 您可以使用 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,允許來自所有私人 IPv4 位址範圍 (RFC 1918 範圍) 的流量。

    IPv6 子網路範圍:

    • 如果您已為虛擬私有雲網路指派內部 IPv6 位址範圍,則可將該範圍設為來源範圍。使用虛擬私有雲網路的內部 IPv6 範圍,代表防火牆規則包含所有目前和未來的內部 IPv6 子網路範圍。您可以使用下列指令,找出虛擬私有雲網路的內部 IPv6 範圍:

      gcloud compute networks describe NETWORK \
        --format="flattened(internalIpv6Range)"
      

      您也可以指定特定的內部 IPv6 子網路範圍。

    • 如要允許來自外部 IPv6 子網路範圍的流量,您必須指定要納入的每個子網路 IPv6 位址範圍。

允許傳入 VM 的 SSH 連線

下列範例會建立防火牆規則,允許透過 SSH 連線至 VM 執行個體,類似於預設網路的 allow-ssh 規則。

gcloud

使用 gcloud compute firewall-rules create 指令:

gcloud compute firewall-rules create RULE_NAME \
    --action=ALLOW \
    --direction=INGRESS \
    --network=NETWORK \
    --priority=1000 \
    --rules=tcp:22 \
    --source-ranges=RANGES_OUTSIDE_VPC_NETWORK

更改下列內容:

  • RULE_NAME:這個防火牆規則的名稱。
  • NETWORK:這項防火牆規則適用的網路名稱。預設值為 default。
  • RANGES_OUTSIDE_VPC_NETWORK:一或多個 IP 位址範圍。您可以在指定的防火牆規則中指定 IPv4 或 IPv6 範圍。最佳做法是指定需要允許存取的特定 IP 位址範圍,而非所有 IPv4 或 IPv6 來源。

    • 如果來源範圍包含 35.235.240.0/20,且符合所有其他必要條件,則允許使用 Identity-Aware Proxy (IAP) TCP 轉送的 SSH 連線。詳情請參閱「使用 IAP 進行 TCP 轉送」。
    • 如果來源範圍是 0.0.0.0/0,系統會允許來自所有 IPv4 來源的流量,包括 Google Cloud以外的來源。
    • 如果來源範圍是 ::/0,系統會允許來自所有 IPv6 來源的流量,包括 Google Cloud以外的來源。

API

POST https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls
{
  "kind": "compute#firewall",
  "name": "RULE_NAME",
  "network": "projects/PROJECT_ID/global/networks/NETWORK",
  "direction": "INGRESS",
  "priority": 1000,
  "targetTags": [],
  "allowed": [
    {
      "IPProtocol": "tcp",
      "ports": [
        "22"
      ]
    }
  ],
  "sourceRanges": [
    "RANGES_OUTSIDE_VPC_NETWORK"
  ]
}

更改下列內容:

  • PROJECT_ID:虛擬私有雲網路所在專案的 ID。
  • RULE_NAME:防火牆規則的名稱。
  • NETWORK:建立防火牆規則的虛擬私有雲網路名稱。
  • RANGES_OUTSIDE_VPC_NETWORK:一或多個 IP 位址範圍。您可以在指定的防火牆規則中指定 IPv4 或 IPv6 範圍。最佳做法是指定需要允許存取的特定 IP 位址範圍,而非所有 IPv4 或 IPv6 來源。

    • 如果來源範圍包含 35.235.240.0/20,且符合所有其他必要條件,則允許使用 Identity-Aware Proxy (IAP) TCP 轉送的 SSH 連線。詳情請參閱「使用 IAP 進行 TCP 轉送」。
    • 如果來源範圍是 0.0.0.0/0,系統會允許來自所有 IPv4 來源的流量,包括 Google Cloud以外的來源。
    • 如果來源範圍是 ::/0,系統會允許來自所有 IPv6 來源的流量,包括 Google Cloud以外的來源。

允許連入 VM 的 RDP 連線

下列範例會建立防火牆規則,允許 Microsoft 遠端桌面通訊協定 (RDP) 連線至 VM 執行個體,類似於預設網路的 allow-rdp 規則。

gcloud

使用 gcloud compute firewall-rules create 指令:

gcloud compute firewall-rules create RULE_NAME \
    --action=ALLOW \
    --direction=INGRESS \
    --network=NETWORK \
    --priority=1000 \
    --rules=tcp:3389 \
    --source-ranges=RANGES_OUTSIDE_VPC_NETWORK

更改下列內容:

  • RULE_NAME:這個防火牆規則的名稱。
  • NETWORK:這項防火牆規則適用的網路名稱。預設值為 default。
  • RANGES_OUTSIDE_VPC_NETWORK:一或多個 IP 位址範圍。您可以在指定的防火牆規則中指定 IPv4 或 IPv6 範圍。最佳做法是指定需要允許存取的特定 IP 位址範圍,而非所有 IPv4 或 IPv6 來源。

    • 如果來源範圍包含 35.235.240.0/20,且符合所有其他必要條件,則允許使用 Identity-Aware Proxy (IAP) TCP 轉送的 RDP 連線。詳情請參閱「使用 IAP 進行 TCP 轉送」。
    • 如果來源範圍是 0.0.0.0/0,系統會允許來自所有 IPv4 來源的流量,包括 Google Cloud以外的來源。
    • 如果來源範圍是 ::/0,系統會允許來自所有 IPv6 來源的流量,包括 Google Cloud以外的來源。

API

POST https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls
{
  "kind": "compute#firewall",
  "name": "RULE_NAME",
  "network": "projects/PROJECT_ID/global/networks/NETWORK",
  "direction": "INGRESS",
  "priority": 1000,
  "allowed": [
    {
      "IPProtocol": "tcp",
      "ports": [
        "3389"
      ]
    }
  ],
  "sourceRanges": [
    "EXTERNAL_SOURCE_RANGES"
  ]
}

更改下列內容:

  • PROJECT_ID:虛擬私有雲網路所在專案的 ID。
  • RULE_NAME:防火牆規則的名稱。
  • NETWORK:建立防火牆規則的虛擬私有雲網路名稱。
  • RANGES_OUTSIDE_VPC_NETWORK:一或多個 IP 位址範圍。您可以在指定的防火牆規則中指定 IPv4 或 IPv6 範圍。最佳做法是指定需要允許存取的特定 IP 位址範圍,而非所有 IPv4 或 IPv6 來源。

    • 如果來源範圍包含 35.235.240.0/20,且符合所有其他必要條件,則允許使用 Identity-Aware Proxy (IAP) TCP 轉送的 RDP 連線。詳情請參閱「使用 IAP 進行 TCP 轉送」。
    • 如果來源範圍是 0.0.0.0/0,系統會允許來自所有 IPv4 來源的流量,包括 Google Cloud以外的來源。
    • 如果來源範圍是 ::/0,系統會允許來自所有 IPv6 來源的流量,包括 Google Cloud以外的來源。

允許傳入 VM 的 ICMP 連線

下列範例會建立防火牆規則,允許 ICMP 連線至 VM 執行個體,類似於預設網路的 allow-icmp 規則。

gcloud

使用 gcloud compute firewall-rules create 指令:

gcloud compute firewall-rules create RULE_NAME \
    --action=ALLOW \
    --direction=INGRESS \
    --network=NETWORK \
    --priority=1000 \
    --rules=ICMP_PROTOCOL \
    --source-ranges=RANGES_OUTSIDE_VPC_NETWORK

更改下列內容:

  • RULE_NAME:防火牆規則的名稱。
  • NETWORK:適用這項防火牆 規則的網路名稱。預設值為 default。
  • ICMP_PROTOCOL:ICMP 通訊協定類型。使用通訊協定名稱 icmp 或通訊協定編號 1 指定 ICMPv4。使用通訊協定編號 58 指定 ICMPv6。
  • RANGES_OUTSIDE_VPC_NETWORK:一或多個 IP 位址範圍。您可以在指定的防火牆規則中指定 IPv4 或 IPv6 範圍。最佳做法是指定需要允許存取的特定 IP 位址範圍,而非所有 IPv4 或 IPv6 來源。

    • 如果來源範圍是 0.0.0.0/0,系統會允許來自所有 IPv4 來源的流量,包括 Google Cloud以外的來源。
    • 如果來源範圍是 ::/0,系統會允許來自所有 IPv6 來源的流量,包括 Google Cloud以外的來源。

API

POST https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/firewalls
{
  "kind": "compute#firewall",
  "name": "RULE_NAME",
  "network": "projects/PROJECT_ID/global/networks/NETWORK",
  "direction": "INGRESS",
  "priority": 1000,
  "targetTags": [],
  "allowed": [
    {
      "IPProtocol": "ICMP_PROTOCOL"
    }
  ],
  "sourceRanges": [
    "RANGES_OUTSIDE_VPC_NETWORK"
  ]
}

更改下列內容:

  • PROJECT_ID:虛擬私有雲網路所在專案的 ID。
  • RULE_NAME:防火牆規則的名稱。
  • NETWORK:建立防火牆規則的虛擬私有雲網路名稱。
  • ICMP_PROTOCOL:要使用的 ICMP 通訊協定類型。使用通訊協定名稱 icmp 或通訊協定編號 1 指定 ICMPv4。使用通訊協定編號 58 指定 ICMPv6。
  • RANGES_OUTSIDE_VPC_NETWORK:一或多個 IP 位址範圍。您可以在指定的防火牆規則中指定 IPv4 或 IPv6 範圍。最佳做法是指定需要允許存取的特定 IP 位址範圍,而非所有 IPv4 或 IPv6 來源。

    • 如果來源範圍是 0.0.0.0/0,系統會允許來自所有 IPv4 來源的流量,包括 Google Cloud以外的來源。
    • 如果來源範圍是 ::/0,系統會允許來自所有 IPv6 來源的流量,包括 Google Cloud以外的來源。

其他設定範例

圖 1 說明名為 my-network 的虛擬私有雲網路設定範例。網路包含下列項目:

  • 名為 subnet1 的子網路,IP 範圍為 10.240.10.0/24,且只有一個執行個體
  • 名為 subnet2 的子網路,IP 範圍為 192.168.1.0/24
  • subnet2 中名為 vm1 的執行個體,標記為 webserver,內部 IP 位址為 192.168.1.2
  • subnet2 中名為 vm2 的執行個體,標記為 database,內部 IP 位址為 192.168.1.3
這個虛擬私有雲網路包含兩個子網路,每個子網路都含有 VM;subnet2 包含已指派網路標記的 VM。
圖 1. 這個虛擬私有雲網路包含兩個子網路,每個子網路都含有 VM;subnet2 包含已指派網路標記的 VM (按一下即可放大)。

範例 1:拒絕所有輸入 TCP 連線,但允許從 subnet1 連至通訊埠 80 的連線

這個範例會建立一組防火牆虛擬私有雲規則,拒絕所有輸入 TCP 連線,但從 subnet1 傳送至通訊埠 80 的連線除外。

gcloud

  1. 建立防火牆規則,拒絕所有傳送至標記為 webserver 的執行個體的輸入 TCP 流量。

    gcloud compute firewall-rules create deny-subnet1-webserver-access \
        --network NETWORK_NAME \
        --action deny \
        --direction INGRESS \
        --rules tcp \
        --source-ranges 0.0.0.0/0 \
        --priority 1000 \
        --target-tags webserver
    

    將 NETWORK_NAME 替換為網路名稱。

  2. 建立防火牆規則,允許 subnet1 (10.240.10.0/24) 中的所有 IP 位址存取標記為 webserver 的執行個體上的 TCP 通訊埠 80。

    gcloud compute firewall-rules create vm1-allow-ingress-tcp-port80-from-subnet1 \
        --network NETWORK_NAME \
        --action allow \
        --direction INGRESS \
        --rules tcp:80 \
        --source-ranges 10.240.10.0/24 \
        --priority 50 \
        --target-tags webserver
    

    將 NETWORK_NAME 替換為網路名稱。

範例 2:拒絕所有輸出 TCP 連線,但連至 vm1 通訊埠 80 的連線除外

gcloud

  1. 建立防火牆規則,拒絕所有輸出 TCP 流量。

    gcloud compute firewall-rules create deny-all-access \
        --network NETWORK_NAME \
        --action deny \
        --direction EGRESS \
        --rules tcp \
        --destination-ranges 0.0.0.0/0 \
        --priority 1000
    

    將 NETWORK_NAME 替換為網路名稱。

  2. 建立防火牆規則,允許傳送至 vm1 通訊埠 80 的 TCP 流量。

    gcloud compute firewall-rules create vm1-allow-egress-tcp-port80-to-vm1 \
        --network NETWORK_NAME \
        --action allow \
        --direction EGRESS \
        --rules tcp:80 \
        --destination-ranges 192.168.1.2/32 \
        --priority 60
    

    將 NETWORK_NAME 替換為網路名稱。

範例 3:允許輸出 TCP 連線至外部主機的通訊埠 443

建立防火牆規則,允許標記為 webserver 的執行個體將輸出 TCP 流量傳送至範例外部 IP 位址 192.0.2.5 的通訊埠 443。

gcloud

gcloud compute firewall-rules create vm1-allow-egress-tcp-port443-to-192-0-2-5 \
    --network NETWORK_NAME \
    --action allow \
    --direction EGRESS \
    --rules tcp:443 \
    --destination-ranges 192.0.2.5/32 \
    --priority 70 \
    --target-tags webserver

將 NETWORK_NAME 替換為網路名稱。

範例 4:允許從 vm2 到 vm1 的 SSH 連線

建立防火牆規則,允許來自標記為 database (vm2) 的執行個體的 SSH 流量,傳送至標記為 webserver (vm1) 的執行個體。

gcloud

gcloud compute firewall-rules create vm1-allow-ingress-tcp-ssh-from-vm2 \
    --network NETWORK_NAME \
    --action allow \
    --direction INGRESS \
    --rules tcp:22 \
    --source-tags database \
    --priority 80 \
    --target-tags webserver

將 NETWORK_NAME 替換為網路名稱。

範例 5:使用服務帳戶允許從網路伺服器到資料庫之間的 TCP:1443

如要進一步瞭解服務帳戶和角色,請參閱「將角色授予服務帳戶」。

請考慮圖 2 中的情境,其中有兩個透過範本自動調度資源的應用程式:與 my-sa-webserver 服務帳戶相關聯的網路伺服器應用程式,以及與 my-sa-database 服務帳戶相關聯的資料庫應用程式。安全管理員希望允許 TCP 流量從具有 my-sa-webserver 服務帳戶的 VM 傳送至具有 my-sa-database 服務帳戶的 VM 的目的地通訊埠 1443。

防火牆規則允許來自具有服務帳戶 my-sa-webserver 的 VM 流量,傳送至具有服務帳戶 my-sa-database 的 VM 通訊埠 1443。
圖 2. 防火牆規則允許來自具有服務帳戶 my-sa-webserver 的 VM 流量,傳送至具有服務帳戶 my-sa-database 的 VM 通訊埠 1443 (按一下即可放大)。

設定步驟 (包括建立服務帳戶) 如下。

gcloud

  1. 專案編輯者或專案擁有者建立服務帳戶 my-sa-webserver 和 my-sa-database。

    gcloud iam service-accounts create my-sa-webserver \
        --display-name "webserver service account"
    
    gcloud iam service-accounts create my-sa-database \
        --display-name "database service account"
    
  2. 專案擁有者透過設定 Identity and Access Management (IAM) 政策,將服務帳戶 my-sa-webserver 的服務帳戶使用者角色 (serviceAccountUser) 指派給網路伺服器開發人員 web-dev@example.com。

    gcloud iam service-accounts add-iam-policy-binding \
    my-sa-webserver@my-project.iam.gserviceaccount.com \
        --member='user:web-dev@example.com' \
        --role='roles/iam.serviceAccountUser'
    
  3. 專案擁有者透過設定 IAM 政策,將服務帳戶 my-sa-database 的服務帳戶使用者角色 (serviceAccountUser) 指派給資料庫開發人員 db-dev@example.com。

    gcloud iam service-accounts add-iam-policy-binding \
    my-sa-database@my-project.iam.gserviceaccount.com \
        --member='user:db-dev@example.com' \
        --role='roles/iam.serviceAccountUser'
    
  4. 開發人員 web-dev@example.com 具有執行個體管理員角色,可建立網路伺服器執行個體範本,並授權執行個體以服務帳戶 my-sa-webserver 的身分執行。

    gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \
        --service-account my-sa-webserver@my-project-123.iam.gserviceaccount.com
    
  5. 開發人員 db-dev@example.com 具有執行個體管理員角色,可建立資料庫執行個體範本,並授權執行個體以服務帳戶 my-sa-database 的身分執行。

    gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \
        --service-account my-sa-database@my-project-123.iam.gserviceaccount.com
    
  6. 安全管理員建立防火牆規則,允許來自具有服務帳戶 my-sa-webserver 的 VM 的 TCP 流量,到達具有服務帳戶 my-sa-database 的 VM 的通訊埠 1443。

    gcloud compute firewall-rules create RULE_NAME \
        --network network_a \
        --allow TCP:1443 \
        --source-service-accounts my-sa-webserver@my-project.iam.gserviceaccount.com \
        --target-service-accounts my-sa-database@my-project.iam.gserviceaccount.com
    

疑難排解

建立或更新虛擬私有雲防火牆規則時的錯誤訊息

你可能會看到下列其中一則錯誤訊息:

  • Should not specify destination range for ingress direction.

    目的地範圍不是輸入防火牆規則的有效參數。除非明確指定 EGRESS 的方向,否則防火牆規則會視為輸入規則。如果建立規則時未指定方向,系統會將其建立為輸入規則,不允許指定目的地範圍。此外,來源範圍並非輸出規則的有效參數。

  • Firewall direction cannot be changed once created.

    您無法變更現有防火牆規則的方向。您必須使用正確的參數建立新的規則,然後刪除舊的規則。

  • Firewall traffic control action cannot be changed once created.

    您無法變更現有防火牆規則的動作。您必須使用正確的參數建立新的規則,然後刪除舊的規則。

  • Service accounts must be valid RFC 822 email addresses. 防火牆規則中指定的服務帳戶,必須是符合 RFC 822 格式的電子郵件地址。

    gcloud compute firewall-rules create bad --allow tcp --source-service-accounts invalid-email
    
    Creating firewall...failed.
    ERROR: (gcloud.compute.firewall-rules.create) Could not fetch resource:
    – Invalid value for field 'resource.sourceServiceAccounts[0]': 'invalid-email'. Service accounts must be valid RFC 822 email addresses.
    
  • ServiceAccounts and Tags are mutually exclusive and can't be combined in the same firewall rule. 您無法在同一規則中同時指定服務帳戶和標記。

    gcloud compute firewall-rules create bad --allow tcp --source-service-accounts test@google.com --target-tags target
    
    Creating firewall...failed.
     ERROR: (gcloud.compute.firewall-rules.create) Could not fetch resource:
    – ServiceAccounts and Tags are mutually exclusive and can't be combined in the same firewall rule.
    

找不到資源錯誤

刪除虛擬私有雲網路或防火牆規則時,您可能會看到類似下列訊息: The resource "aet-uscentral1-subnet--1-egrfw" was not found.

這項錯誤可能會導致您無法刪除默示防火牆規則或查看其詳細資料。處於這種狀態的防火牆規則,也可能導致您無法刪除虛擬私有雲網路。

如要刪除以此方式封鎖的防火牆規則或網路,請先刪除相關聯的無伺服器 VPC 存取連接器,然後再試一次。如要進一步瞭解如何刪除無伺服器 VPC 存取連接器,請參閱「刪除連接器」。

大型防火牆過多錯誤

您可能會看到下列錯誤訊息: Google Compute Engine: The network contains too many large firewalls.

為維護安全性和效能,虛擬私有雲網路中可實作的防火牆規則複雜度和數量有限。如果看到這則錯誤訊息,請要求帳戶管理團隊簡化或合併防火牆規則。

無法連線至 VM 執行個體

如果您無法連線至 VM 執行個體,請檢查您的防火牆規則。

gcloud

  1. 如果您從另一個 VM 執行個體啟動連線,請列出該執行個體的輸出防火牆規則。

    gcloud compute firewall-rules list --filter network=NETWORK_NAME \
      --filter EGRESS \
      --sort-by priority \
      --format="table(
          name,
          network,
          direction,
          priority,
          sourceRanges.list():label=SRC_RANGES,
          destinationRanges.list():label=DEST_RANGES,
          allowed[].map().firewall_rule().list():label=ALLOW,
          denied[].map().firewall_rule().list():label=DENY,
          sourceTags.list():label=SRC_TAGS,
          sourceServiceAccounts.list():label=SRC_SVC_ACCT,
          targetTags.list():label=TARGET_TAGS,
          targetServiceAccounts.list():label=TARGET_SVC_ACCT
          )"
    

    將 NETWORK_NAME 替換為網路名稱。

  2. 檢查是否有任何輸出規則拒絕目的地 IP。優先順序最高 (優先順序編號最低) 的規則會覆寫優先順序較低的規則。如果兩項規則的優先順序相同,則以拒絕規則為準。

  3. 針對包含目的地 VM 執行個體的網路,檢查輸入防火牆規則。

    gcloud compute firewall-rules list --filter network=NETWORK_NAME \
      --filter INGRESS \
      --sort-by priority \
      --format="table(
          name,
          network,
          direction,
          priority,
          sourceRanges.list():label=SRC_RANGES,
          destinationRanges.list():label=DEST_RANGES,
          allowed[].map().firewall_rule().list():label=ALLOW,
          denied[].map().firewall_rule().list():label=DENY,
          sourceTags.list():label=SRC_TAGS,
          sourceServiceAccounts.list():label=SRC_SVC_ACCT,
          targetTags.list():label=TARGET_TAGS,
          targetServiceAccounts.list():label=TARGET_SVC_ACCT
          )"
    

    將 NETWORK_NAME 替換為網路名稱。

    輸出內容範例。輸出內容會因防火牆規則清單而異。

    NAME                    NETWORK  DIRECTION  PRIORITY  SRC_RANGES    DEST_RANGES  ALLOW                         DENY  SRC_TAGS  SRC_SVC_ACCT      TARGET_TAGS  TARGET_SVC_ACCT
    default-allow-icmp      default  INGRESS    65534     0.0.0.0/0                  icmp
    default-allow-internal  default  INGRESS    65534     10.128.0.0/9               tcp:0-65535,udp:0-65535,icmp
    default-allow-rdp       default  INGRESS    65534     0.0.0.0/0                  tcp:3389
    default-allow-ssh       default  INGRESS    65534     0.0.0.0/0                  tcp:22
    firewall-with-sa        default  INGRESS    1000                                 tcp:10000                                     test1@google.com               target@google.com
    
  4. 您也可以執行 Connectivity Tests,檢查虛擬私有雲網路中的 VM 執行個體與其他虛擬私有雲網路或非 Google 雲端網路之間的連線,以便疑難排解流量是否因任何輸入或輸出防火牆規則而遭到捨棄。如要進一步瞭解如何執行 Connectivity Tests 來排解各種情境的問題,請參閱「執行 Connectivity Tests」。

我的虛擬私有雲防火牆規則是否已啟用?

如要查看是否啟用或停用防火牆規則,請參閱防火牆規則詳細資料。

在 Google Cloud console中,查看「Enforcement」(強制執行) 區段中的「Enabled」(已啟用) 或「Disabled」(已停用)。

在 gcloud CLI 輸出內容中,找出 disabled 欄位。如果顯示「disabled:false」,表示規則已啟用並強制執行。如果顯示「disabled: true」,表示規則已停用。

哪個規則會套用在 VM 執行個體上?

建立規則後,您可以檢查規則是否正確套用至特定執行個體。詳情請參閱「列出 VM 執行個體網路介面的防火牆規則」。

含有來源標記的虛擬私有雲防火牆規則不會立即生效

使用來源標記的輸入防火牆規則可能需要一些時間才能傳播。詳情請參閱輸入防火牆規則來源標記的注意事項。

後續步驟