Chapter 5. Egress Firewall
5.1. Viewing an egress firewall for a project
As a cluster administrator, you can list the names of any existing egress firewalls and view the traffic rules for a specific egress firewall.
5.1.1. Viewing an EgressFirewall custom resource (CR)
					You can view an EgressFirewall CR in your cluster.
				
Prerequisites
- A cluster using the OVN-Kubernetes network plugin.
- 
							Install the OpenShift Command-line Interface (CLI), commonly known as oc.
- You must log in to the cluster.
Procedure
- Optional: To view the names of the - EgressFirewallCR defined in your cluster, enter the following command:- oc get egressfirewall --all-namespaces - $ oc get egressfirewall --all-namespaces- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- To inspect a policy, enter the following command. Replace - <policy_name>with the name of the policy to inspect.- oc describe egressfirewall <policy_name> - $ oc describe egressfirewall <policy_name>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Example output - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
5.2. Editing an egress firewall for a project
As a cluster administrator, you can modify network traffic rules for an existing egress firewall.
5.2.1. Editing an EgressFirewall custom resource (CR)
As a cluster administrator, you can update the egress firewall for a project.
Prerequisites
- A cluster using the OVN-Kubernetes network plugin.
- 
							Install the OpenShift CLI (oc).
- You must log in to the cluster as a cluster administrator.
Procedure
- Find the name of the - EgressFirewallCR for the project. Replace- <project>with the name of the project.- oc get -n <project> egressfirewall - $ oc get -n <project> egressfirewall- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Optional: If you did not save a copy of the - EgressFirewallobject when you created the egress network firewall, enter the following command to create a copy.- oc get -n <project> egressfirewall <name> -o yaml > <filename>.yaml - $ oc get -n <project> egressfirewall <name> -o yaml > <filename>.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Replace - <project>with the name of the project. Replace- <name>with the name of the object. Replace- <filename>with the name of the file to save the YAML to.
- After making changes to the policy rules, enter the following command to replace the - EgressFirewallCR. Replace- <filename>with the name of the file containing the updated- EgressFirewallCR.- oc replace -f <filename>.yaml - $ oc replace -f <filename>.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
5.3. Removing an egress firewall from a project
As a cluster administrator, you can remove an egress firewall from a project to remove all restrictions on network traffic from the project that leaves the OpenShift Container Platform cluster.
5.3.1. Removing an EgressFirewall CR
As a cluster administrator, you can remove an egress firewall from a project.
Prerequisites
- A cluster using the OVN-Kubernetes network plugin.
- 
							Install the OpenShift CLI (oc).
- You must log in to the cluster as a cluster administrator.
Procedure
- Find the name of the - EgressFirewallCR for the project. Replace- <project>with the name of the project.- oc get egressfirewall -n <project> - $ oc get egressfirewall -n <project>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Delete the - EgressFirewallCR by entering the following command. Replace- <project>with the name of the project and- <name>with the name of the object.- oc delete -n <project> egressfirewall <name> - $ oc delete -n <project> egressfirewall <name>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
5.4. Configuring an egress firewall for a project
As a cluster administrator, you can create an egress firewall for a project that restricts egress traffic leaving your OpenShift Container Platform cluster.
5.4.1. How an egress firewall works in a project
As a cluster administrator, you can use an egress firewall to limit the external hosts that some or all pods can access from within the cluster. An egress firewall supports the following scenarios:
- A pod can only connect to internal hosts and cannot initiate connections to the public internet.
- A pod can only connect to the public internet and cannot initiate connections to internal hosts that are outside the OpenShift Container Platform cluster.
- A pod cannot reach specified internal subnets or hosts outside the OpenShift Container Platform cluster.
- A pod can only connect to specific external hosts.
For example, you can allow one project access to a specified IP range but deny the same access to a different project. Or, you can restrict application developers from updating from Python pip mirrors, and force updates to come only from approved sources.
					You configure an egress firewall policy by creating an EgressFirewall custom resource (CR). The egress firewall matches network traffic that meets any of the following criteria:
				
- An IP address range in CIDR format
- A DNS name that resolves to an IP address
- A port number
- A protocol that is one of the following protocols: TCP, UDP, and SCTP
5.4.1.1. Limitations of an egress firewall
An egress firewall has the following limitations:
- 
								No project can have more than one EgressFirewallCR.
- 
								Egress firewall rules do not apply to traffic that goes through routers. Any user with permission to create a RouteCR object can bypass egress firewall policy rules by creating a route that points to a forbidden destination.
- Egress firewall does not apply to the host network namespace. Pods with host networking enabled are unaffected by egress firewall rules.
- If your egress firewall includes a deny rule for - 0.0.0.0/0, access to your OpenShift Container Platform API servers is blocked. You must either add allow rules for each IP address or use the- nodeSelectortype allow rule in your egress policy rules to connect to API servers.- The following example illustrates the order of the egress firewall rules necessary to ensure API server access: - EgressFirewallAPI server access example- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - where: - <namespace>
- Specifies the namespace for the egress firewall.
- <api_server_address_range>
- Specifies the IP address range that includes your OpenShift Container Platform API servers.
- <cidrSelector>
- Specifies a value of - 0.0.0.0/0to set a global deny rule that prevents access to the OpenShift Container Platform API servers.- To find the IP address for your API servers, run - oc get ep kubernetes -n default.- For more information, see BZ#1988324. 
 
- 
								A maximum of one EgressFirewallobject with a maximum of 8,000 rules can be defined per project.
- If you are using the OVN-Kubernetes network plugin with shared gateway mode in Red Hat OpenShift Networking, return ingress replies are affected by egress firewall rules. If the egress firewall rules drop the ingress reply destination IP, the traffic is dropped.
- In general, using Domain Name Server (DNS) names in your egress firewall policy does not affect local DNS resolution through CoreDNS. However, if your egress firewall policy uses domain names and an external DNS server handles DNS resolution for an affected pod, you must include egress firewall rules that permit access to the IP addresses of your DNS server.
Violating any of these restrictions results in a broken egress firewall for the project. Consequently, all external network traffic is dropped, which can cause security risks for your organization.
						An EgressFirewall resource is created in the kube-node-lease, kube-public, kube-system, openshift and openshift- projects.
					
5.4.1.2. Matching order for egress firewall policy rules
OVN-Kubernetes evaluates egress firewall policy rules in the order they are defined in, from first to last. The first rule that matches an egress connection from a pod applies. Any subsequent rules are ignored for that connection.
5.4.1.3. How Domain Name Server (DNS) resolution works
If you use DNS names in any of your egress firewall policy rules, proper resolution of the domain names is subject to the following restrictions:
- Domain name updates are polled based on a time-to-live (TTL) duration. By default, the duration is 30 minutes. When the egress firewall controller queries the local name servers for a domain name, if the response includes a TTL and the TTL is less than 30 minutes, the controller sets the duration for that DNS name to the returned value. Each DNS name is queried after the TTL for the DNS record expires.
- The pod must resolve the domain from the same local name servers when necessary. Otherwise the IP addresses for the domain known by the egress firewall controller and the pod can be different. If the IP addresses for a hostname differ, the egress firewall might not be enforced consistently.
- 
								Because the egress firewall controller and pods asynchronously poll the same local name server, the pod might obtain the updated IP address before the egress controller does, which causes a race condition. Due to this current limitation, domain name usage in EgressFirewallobjects is only recommended for domains with infrequent IP address changes.
5.4.1.3.1. Improved DNS resolution and resolving wildcard domain names
There might be situations where the IP addresses associated with a DNS record change frequently, or you might want to specify wildcard domain names in your egress firewall policy rules.
							In this situation, the OVN-Kubernetes cluster manager creates a DNSNameResolver custom resource object for each unique DNS name used in your egress firewall policy rules. This custom resource stores the following information:
						
Improved DNS resolution for egress firewall rules is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.
For more information about the support scope of Red Hat Technology Preview features, see Technology Preview Features Support Scope.
Example DNSNameResolver CR definition
where:
- <name>
- Specifies the DNS name. This can be either a standard DNS name or a wildcard DNS name. For a wildcard DNS name, the DNS name resolution information contains all of the DNS names that match the wildcard DNS name.
- <dnsName>
- 
										Specifies the resolved DNS name matching the spec.namefield. If thespec.namefield contains a wildcard DNS name, then multiplednsNameentries are created that contain the standard DNS names that match the wildcard DNS name when resolved. If the wildcard DNS name can also be successfully resolved, then this field also stores the wildcard DNS name. <ip> Specifies the current IP addresses associated with the DNS name.
- <ttlSeconds>
- Specifies the last time-to-live (TTL) duration.
- <lastLookupTime>
- Specifies the last lookup time.
							If during DNS resolution the DNS name in the query matches any name defined in a DNSNameResolver CR, then the previous information is updated accordingly in the CR status field. For unsuccessful DNS wildcard name lookups, the request is retried after a default TTL of 30 minutes.
						
							The OVN-Kubernetes cluster manager watches for updates to an EgressFirewall custom resource object, and creates, modifies, or deletes DNSNameResolver CRs associated with those egress firewall policies when that update occurs.
						
								Do not modify DNSNameResolver custom resources directly. This can lead to unwanted behavior of your egress firewall.
							
5.4.2. EgressFirewall custom resource (CR)
					You can define one or more rules for an egress firewall. A rule is either an Allow rule or a Deny rule, with a specification for the traffic that the rule applies to.
				
					The following YAML describes an EgressFirewall CR:
				
EgressFirewall object
where:
- <ovn>
- 
								The name for the object must be default.
- <egress_rules>
- Specifies a collection of one or more egress network policy rules as described in the following section.
5.4.2.1. EgressFirewall rules
						The following YAML describes the rules for an EgressFirewall resource. The user can select either an IP address range in CIDR format, a domain name, or use the nodeSelector field to allow or deny egress traffic. The egress stanza expects an array of one or more objects.
					
Egress policy rule stanza
where:
- <type>
- 
									Specifies the type of rule. The value must be either AlloworDeny.
- <to>
- 
									Specifies a stanza describing an egress traffic match rule that specifies the cidrSelectorfield or thednsNamefield. You cannot use both fields in the same rule.
- <cidr_range>
- Specifies an IP address range in CIDR format.
- <dns_name>
- Specifies a DNS domain name.
- <nodeSelector>
- 
									Specifies labels which are key and value pairs that the user defines. Labels are attached to objects, such as pods. The nodeSelectorallows for one or more node labels to be selected and attached to pods.
- <ports>
- Specifies an optional field that describes a collection of network ports and protocols for the rule.
Ports stanza
ports: - port: protocol:
ports:
- port:
  protocol:where:
- <port>
- 
									Specifies a network port, such as 80or443. If you specify a value for this field, you must also specify a value for theprotocolfield.
- <protocol>
- 
									Specifies a network protocol. The value must be either TCP,UDP, orSCTP.
5.4.2.2. Example EgressFirewall CR
The following example defines several egress firewall policy rules:
where:
- <egress>
- Specifies a collection of egress firewall policy rule objects.
						The following example defines a policy rule that denies traffic to the host at the 172.16.1.1/32 IP address, if the traffic is using either the TCP protocol and destination port 80 or any protocol and destination port 443.
					
5.4.2.3. Example EgressFirewall CR using nodeSelector
						As a cluster administrator, you can allow or deny egress traffic to nodes in your cluster by specifying a label using nodeSelector field. Labels can be applied to one or more nodes. Labels can be helpful because instead of adding manual rules per node IP address, you can use node selectors to create a label that allows pods behind an egress firewall to access host network pods. The following is an example with the region=east label:
					
5.4.3. Creating an EgressFirewall custom resource (CR)
As a cluster administrator, you can create an egress firewall policy object for a project.
						If the project already has an EgressFirewall resource, you must edit the existing policy to make changes to egress firewall rules.
					
Prerequisites
- A cluster that uses the OVN-Kubernetes network plugin.
- 
							Install the OpenShift CLI (oc).
- You must log in to the cluster as a cluster administrator.
Procedure
- Create a policy rule: - 
									Create a <policy_name>.yamlfile where<policy_name>describes the egress policy rules.
- 
									Define the EgressFirewallobject in the file.
 
- 
									Create a 
- Create the policy object by entering the following command. Replace - <policy_name>with the name of the policy and- <project>with the project that the rule applies to.- oc create -f <policy_name>.yaml -n <project> - $ oc create -f <policy_name>.yaml -n <project>- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Successful output lists the - egressfirewall.k8s.ovn.org/v1name and the- createdstatus.
- 
							Optional: Save the <policy_name>.yamlfile so that you can make changes later.