Create cross-project network policies

This page provides instructions to configure cross-project traffic network policies in Google Distributed Cloud (GDC) air-gapped.

Cross-project traffic refers to the communication between services and workloads from different project namespaces but within the same organization.

Services and workloads in a project are isolated from external services and workloads by default. However, services and workloads from different project namespaces and within the same organization can communicate with each other by applying cross-project traffic network policies.

By default, these policies apply globally across all zones. For more information on global resources in a GDC universe, see Multi-zone overview.

If cross-project traffic enforcement is needed within a single zone, see Create a single zone workload-level cross-project policy.

Before you begin

To configure cross-project traffic network policies, you must have the following:

  • The necessary identity and access roles. To manage policies for a specific project, you need the project-networkpolicy-admin role. For multi-zone environments where you need to manage policies that span across all zones, you need the global-project-networkpolicy-admin role. For more information, see Prepare predefined roles and access.
  • An existing project. For more information, see Create a project.

Create a cross-project policy

You can define ingress or egress cross-project traffic policies to manage the communication between projects.

Create an ingress cross-project policy

For project workloads or services to allow connections from other workloads in another project within your organization, you must configure an ingress firewall rule to allow the inbound traffic of other project workloads.

This policy applies to all zones in your organization.

Work through the following steps to create a new firewall rule and allow inbound traffic from workloads in another project:

Console

  1. Within the GDC console of the project you are configuring, go to Networking > Firewall in the navigation menu to open the Firewall page.
  2. Click Create in the action bar to begin creating a new firewall rule.
  3. On the Firewall rule details page, fill out the following information:

    1. In the Name field, enter a valid name for your firewall rule.
    2. In the Direction of traffic section, select Ingress to allow inbound traffic from workloads in other projects.
    3. In the Target section, select one of the following options:
      • All user workloads: allow connections to the workloads of the project you are configuring.
      • Service: indicate that this firewall rule targets a specific service within the project you are configuring.
    4. If your target is a project service, select the name of the service from the list of available services on the Service drop-down menu.
    5. In the From section, select one of the following two options:
      • All projects: allow connections from workloads in all the projects of the same organization.
      • Another project and All user workloads: allow connections from workloads in another project of the same organization.
    6. If you want to transfer workloads only from another project, select a project that you can access from the list of projects on the Project ID drop-down menu.
    7. If your target is all user workloads, select one of the following options in the Protocols and ports section:
      • Allow all: allow connections using any protocol or port.
      • Specified protocols and ports: allow connections using only the protocols and ports that you specify in the corresponding fields for the ingress firewall rule.
  4. On the Firewall rule details page, click Create.

You've now permitted connections from other project workloads within the same organization. After creating the firewall rule, the rule is visible in a table on the Firewall page.

API

The following policy enables workloads in the PROJECT_1 project to permit connections from workloads in the PROJECT_2 project, as well as the return traffic for the same flows. Apply the policy:

kubectl --kubeconfig GLOBAL_API_SERVER apply -f - <<EOF
apiVersion: networking.global.gdc.goog/v1
kind: ProjectNetworkPolicy
metadata:
  namespace: PROJECT_1
  name: allow-inbound-traffic-from-PROJECT_2
spec:
  policyType: Ingress
  subject:
    subjectType: UserWorkload
  ingress:
  - from:
    - projectSelector:
        projects:
          matchNames:
          - PROJECT_2
EOF

Replace GLOBAL_API_SERVER with the global API server's kubeconfig path. For more information, see Global and zonal API servers. If you have not yet generated a kubeconfig file for the API server, see Sign in for details.

The preceding command allows PROJECT_2 to go to PROJECT_1, but doesn't allow connections initiated from PROJECT_1 to PROJECT_2. For the latter, you require a reciprocal policy in the PROJECT_2 project. Apply the reciprocal policy:

kubectl --kubeconfig GLOBAL_API_SERVER apply -f - <<EOF
apiVersion: networking.global.gdc.goog/v1
kind: ProjectNetworkPolicy
metadata:
  namespace: PROJECT_2
  name: allow-inbound-traffic-from-PROJECT_1
spec:
  policyType: Ingress
  subject:
    subjectType: UserWorkload
  ingress:
  - from:
    - projectSelector:
        projects:
          matchNames:
          - PROJECT_1
EOF

Connections are now permitted to and from PROJECT_1 and PROJECT_2.

Create an egress cross-project policy

When you grant an ingress cross-project traffic policy to let workloads in one project to allow connections from workloads in another project, this action also grants the return traffic for the same flows. Therefore, you don't need an egress cross-project traffic network policy in the original project.

For example, if you create a policy allowing traffic from PROJECT_1 to PROJECT_2 and data exfiltration protection is disabled, you must create an ingress policy in PROJECT_2 and an egress policy on PROJECT_1. However, the reply packets are excluded from the policy enforcement, so you don't require any additional policies.

Work through the following steps to create a new firewall rule and allow outbound traffic from workloads in a project:

  1. Within the GDC console of the project you are configuring, go to Networking > Firewall in the navigation menu to open the Firewall page.
  2. Click Create in the action bar to begin creating a new firewall rule.
  3. On the Firewall rule details page, fill out the following information:

    1. In the Name field, enter a valid name for your firewall rule.
    2. In the Direction of traffic section, select Egress to indicate that this firewall rule is controlling outbound traffic.
    3. In the Target section, select one of the following options:
      • All user workloads: allow connections from the workloads of the project you are configuring.
      • Service: indicate that this firewall rule targets a specific service within the project you are configuring.
    4. If your target is a project service, select the name of the service from the list of available services on the Service drop-down menu.
    5. In the To section, select one of the following two options:
      • All projects: allow connections to workloads in all the projects of the same organization.
      • Another project and All user workloads: allow connections to workloads in another project of the same organization.
    6. If you want to transfer workloads only to another project, select a project that you can access from the list of projects on the Project ID drop-down menu.
    7. If your target is all user workloads, select one of the following options in the Protocols and ports section:
      • Allow all: allow connections using any protocol or port.
      • Specified protocols and ports: allow connections using only the protocols and ports that you specify in the corresponding fields for the egress firewall rule.
  4. On the Firewall rule details page, click Create.

You've now permitted connections to other project workloads within the same organization. After creating the firewall rule, the rule is visible in a table on the Firewall page.

Create a workload-level cross-project policy

Workload-level network policies offer granular control over communication between individual workloads across projects. This granularity allows stricter control of network access, improving security and resource use.

Create an ingress workload-level cross-project policy

  • To create an ingress workload-level cross-project policy, create and apply the following custom resource:

    kubectl --kubeconfig GLOBAL_API_SERVER apply -f - <<EOF
    apiVersion: networking.global.gdc.goog/v1
    kind: ProjectNetworkPolicy
    metadata:
     namespace: PROJECT_1
     name: allow-cross-project-inbound-traffic-from-target-to-subject
    spec:
     policyType: Ingress
     subject:
       subjectType: UserWorkload
       workloadSelector:
         matchLabels:
           SUBJECT_LABEL_KEY: SUBJECT_LABEL_VALUE
     ingress:
     - from:
       - projectSelector:
           projects:
             matchNames:
             - PROJECT_2
           workloads:
             matchLabels:
               TARGET_LABEL_KEY: TARGET_LABEL_VALUE
    EOF
    

    Replace the following:

    • GLOBAL_API_SERVER: the global API server's kubeconfig path. For more information, see Global and zonal API servers. If you have not yet generated a kubeconfig file for the API server, see Sign in for details.
    • PROJECT_1: The name of the project that is receiving the traffic.
    • PROJECT_2: The name of the project that the traffic is coming from.
    • SUBJECT_LABEL_KEY: the key of the label used to select the source workloads. For example, app, tier, or role.
    • SUBJECT_LABEL_VALUE: the value associated with the SUBJECT_LABEL_KEY. It specifies which workloads are the source of the allowed traffic. For example, if SUBJECT_LABEL_KEY is app, and SUBJECT_LABEL_VALUE is backend, then workloads with the label app: backend is the traffic source.
    • TARGET_LABEL_KEY: the key of the label used to select the destination workloads.
    • TARGET_LABEL_VALUE: the value associated with the TARGET_LABEL_KEY. It specifies which workloads are the destination of the allowed traffic.

Create an egress workload-level cross-project policy

  • To create an egress workload-level cross-project policy, create and apply the following custom resource:

    kubectl --kubeconfig GLOBAL_API_SERVER apply -f - <<EOF
    apiVersion: networking.global.gdc.goog/v1
    kind: ProjectNetworkPolicy
    metadata:
      namespace: PROJECT_1
      name: allow-cross-project-outbound-traffic-to-subject-from-target
    spec:
      policyType: Egress
      subject:
        subjectType: UserWorkload
        workloadSelector:
          matchLabels:
            SUBJECT_LABEL_KEY: SUBJECT_LABEL_VALUE
      egress:
      - to:
        - projectSelector:
            projects:
              matchNames:
              - PROJECT_2
            workloads:
              matchLabels:
                TARGET_LABEL_KEY: TARGET_LABEL_VALUE
    EOF
    

    Replace the following:

    • GLOBAL_API_SERVER: the global API server's kubeconfig path. For more information, see Global and zonal API servers. If you have not yet generated a kubeconfig file for the API server, see Sign in for details.
    • PROJECT_1: The name of the project that is sending the traffic.
    • PROJECT_2: The name of the project that is receiving the traffic.
    • SUBJECT_LABEL_KEY: the key of the label used to select the source workloads. For example, app, tier, or role.
    • SUBJECT_LABEL_VALUE: the value associated with the SUBJECT_LABEL_KEY. It specifies which workloads are the source of the allowed traffic. For example, if SUBJECT_LABEL_KEY is app, and SUBJECT_LABEL_VALUE is backend, then workloads with the label app: backend is the traffic source.
    • TARGET_LABEL_KEY: the key of the label used to select the destination workloads.
    • TARGET_LABEL_VALUE: the value associated with the TARGET_LABEL_KEY. It specifies which workloads are the destination of the allowed traffic.

Create a single zone workload-level cross-project policy

Workload-level network policies can enforce PNP along a single zone. Specific labels can be added to workloads within a single zone, allowing you to control communication between individual workloads within a project or in different projects for that zone.

Create a single zone ingress workload-level cross-project policy

  1. To create a single zone ingress workload-level cross-project policy, create and apply the following custom resource:

    kubectl --kubeconfig GLOBAL_API_SERVER apply -f - <<EOF
    apiVersion: networking.global.gdc.goog/v1
    kind: ProjectNetworkPolicy
    metadata:
      namespace: PROJECT_1
      name: allow-single-zone-cross-project-inbound-traffic-from-target-to-subject
    spec:
      policyType: Ingress
      subject:
        subjectType: UserWorkload
        workloadSelector:
          matchLabels:
            SUBJECT_LABEL_KEY: SUBJECT_LABEL_VALUE
            ZONE_SUBJECT_LABEL_KEY: ZONE_SUBJECT_LABEL_VALUE
      ingress:
      - from:
        - projectSelector:
            projects:
              matchNames:
              - PROJECT_2
            workloads:
              matchLabels:
                TARGET_LABEL_KEY: TARGET_LABEL_VALUE
                ZONE_TARGET_LABEL_KEY: ZONE_TARGET_LABEL_VALUE
    EOF
    

    Replace the following:

    • GLOBAL_API_SERVER: the global API server's kubeconfig path. For more information, see Global and zonal API servers. If you have not yet generated a kubeconfig file for the API server, see Sign in for details.
    • PROJECT_1: The name of the project that is receiving the traffic.
    • PROJECT_2: The name of the project that the traffic is coming from.
    • SUBJECT_LABEL_KEY: the key of the label used to select the source workloads. For example, app, tier, or role.
    • SUBJECT_LABEL_VALUE: the value associated with the SUBJECT_LABEL_KEY. It specifies which workloads are the source of the allowed traffic. For example, if SUBJECT_LABEL_KEY is app, and SUBJECT_LABEL_VALUE is backend, then workloads with the label app: backend is the traffic source.
    • TARGET_LABEL_KEY: the key of the label used to select the destination workloads.
    • TARGET_LABEL_VALUE: the value associated with the TARGET_LABEL_KEY. It specifies which workloads are the destination of the allowed traffic.
    • ZONE_SUBJECT_LABEL_KEY: the key of the label used to select the source zone. For example, zone, or region.
    • ZONE_SUBJECT_LABEL_VALUE: the value associated with the ZONE_SUBJECT_LABEL_KEY. It specifies which zone is the source of the allowed traffic. For example, if ZONE_SUBJECT_LABEL_KEY is zone, and ZONE_SUBJECT_LABEL_VALUE is us-central1-a, then workloads with the label zone: us-central1-a is the traffic source.
    • ZONE_TARGET_LABEL_KEY: the key of the label used to select the destination zone.
    • ZONE_TARGET_LABEL_VALUE: the value associated with the ZONE_TARGET_LABEL_KEY. It specifies which zone is the destination of the allowed traffic.

Create a single zone egress workload-level cross-project policy

  • To create a single zone egress workload-level cross-project policy, create and apply the following custom resource:

    kubectl --kubeconfig GLOBAL_API_SERVER apply -f - <<EOF
    apiVersion: networking.global.gdc.goog/v1
    kind: ProjectNetworkPolicy
    metadata:
      namespace: PROJECT_1
      name: allow-single-zone-cross-project-outbound-traffic-to-subject-from-target
    spec:
      policyType: Egress
      subject:
        subjectType: UserWorkload
        workloadSelector:
          matchLabels:
            SUBJECT_LABEL_KEY: SUBJECT_LABEL_VALUE
            ZONE_SUBJECT_LABEL_KEY: ZONE_SUBJECT_LABEL_VALUE
      egress:
      - to:
        - projectSelector:
            projects:
              matchNames:
              - PROJECT_2
            workloads:
              matchLabels:
                TARGET_LABEL_KEY: TARGET_LABEL_VALUE
                ZONE_TARGET_LABEL_KEY: ZONE_TARGET_LABEL_VALUE
    EOF
    

    Replace the following:

    • GLOBAL_API_SERVER: the global API server's kubeconfig path. For more information, see Global and zonal API servers. If you have not yet generated a kubeconfig file for the API server, see Sign in for details.
    • PROJECT_1: The name of the project that is sending the traffic.
    • PROJECT_2: The name of the project that is receiving the traffic.
    • SUBJECT_LABEL_KEY: the key of the label used to select the source workloads. For example, app, tier, or role.
    • SUBJECT_LABEL_VALUE: the value associated with the SUBJECT_LABEL_KEY. It specifies which workloads are the source of the allowed traffic. For example, if SUBJECT_LABEL_KEY is app, and SUBJECT_LABEL_VALUE is backend, then workloads with the label app: backend is the traffic source.
    • TARGET_LABEL_KEY: the key of the label used to select the destination workloads.
    • TARGET_LABEL_VALUE: the value associated with the TARGET_LABEL_KEY. It specifies which workloads are the destination of the allowed traffic.
    • ZONE_SUBJECT_LABEL_KEY: the key of the label used to select the source zone. For example, zone, or region.
    • ZONE_SUBJECT_LABEL_VALUE: the value associated with the ZONE_SUBJECT_LABEL_KEY. It specifies which zone is the source of the allowed traffic. For example, if ZONE_SUBJECT_LABEL_KEY is zone, and ZONE_SUBJECT_LABEL_VALUE is us-central1-a, then workloads with the label zone: us-central1-a is the traffic source.
    • ZONE_TARGET_LABEL_KEY: the key of the label used to select the destination zone.
    • ZONE_TARGET_LABEL_VALUE: the value associated with the ZONE_TARGET_LABEL_KEY. It specifies which zone is the destination of the allowed traffic.