A Kubernetes network policy is a specification of how groups of pods are allowed to communicate with each other and other network endpoints. These network policies are configured as YAML files. By looking at these files alone, it is often hard to identify whether the applied network policies achieve the desired network topology.
Red Hat Advanced Cluster Security for Kubernetes (RHACS) gathers all defined network policies from your orchestrator and provides tools to make these policies easier to use.
To support network policy enforcement, RHACS provides the following tools:
Network graph
Network policy generator
Network policy simulator
Build-time network policy generator
The network graph provides high-level and detailed information about deployments, network flows, and network policies in your environment.
RHACS processes all network policies in each secured cluster to show you which deployments can contact each other and which can reach external networks. It also monitors running deployments and tracks traffic between them. You can view the following items in the network graph:
From the top menu, you can select namespaces (indicated by the NS label) and deployments (indicated by the D label) to display on the graph for a chosen cluster (indicated by the CL label). You can further filter deployments by using the drop-down list and selecting criteria on which to filter, such as common vulnerabilities and exposures (CVEs), labels, and images.
These represent entities that are connected outside of your cluster. For more information, see "External entities and connections in the network graph".
You can view existing policies for a selected component or view components that have no policies.
You can select one of the following flows for the graph:
Selecting this default option shows observed traffic, focused on the namespace or specific deployment that you selected. You can select the time period for which to display information.
Selecting this option shows potential flows allowed by your network policies, helping you identify missing network policies needed to achieve tighter isolation. You can select the time period for which to display information.
You can also simulate network policies from the network graph view. See "Simulating network policies from the network graph" for more information.
Clicking on items in the graph lets you view additional information about components or perform actions such as adding a network flow to your baseline.
Opening the legend provides information about the symbols in use and their meaning. The legend shows explanatory text for symbols representing namespaces, deployments, and connections on the network graph.
Selecting additional display options from the drop-down list controls whether the graph displays icons such as the network policy status badge, active external traffic badge, and port and protocol labels for edge connections.
RHACS detects changes in network traffic, such as nodes joining or leaving. If changes are detected, the network graph displays a notification showing the number of updates available. To avoid interrupting your focus, the graph is not updated automatically. Click the notification to update the graph.
The network graph view shows network connections between managed clusters and external sources. In addition, RHACS automatically discovers and highlights public Classless Inter-Domain Routing (CIDR) address blocks, such as Google Cloud, AWS, Microsoft Azure, Oracle Cloud, and Cloudflare. Using this information, you can identify deployments with active external connections and decide if they are making or receiving unauthorized connections from outside your network.
By default, the external connections point to a common External Entities icon and different CIDR address blocks in the network graph. However, you can choose not to show auto-discovered CIDR blocks by clicking Manage CIDR blocks and deselecting Auto-discovered CIDR blocks.
RHACS includes IP ranges for the following cloud providers:
Google Cloud
AWS
Microsoft Azure
Oracle Cloud
Cloudflare
RHACS fetches and updates the cloud providers' IP ranges every 7 days, and updates CIDR blocks daily. If you are using offline mode, you can update these ranges by installing new support packages.
The following image provides an example of the network graph. In this example, based on the options that the user has chosen, the graph depicts deployments in the selected namespace. Traffic flows are not displayed until you click on an item such as a deployment. The graph uses a red badge to indicate deployments that are missing policies and therefore allowing all network traffic.
When you click an item in the graph, the rearranged side panel with collapsible sections presents information about that item. You can click on the following items:
Deployments
Namespaces
External entities
CIDR blocks
External groups
The side panel displays relevant information based on the item in the graph that you have selected. The D or NS label next to the item name in the header (in this example, "visa-processor,") indicates if it is a deployment or a namespace. The following example illustrates the side panel for a deployment.
When viewing a namespace, the side panel includes a search bar and a list of deployments. You can click on a deployment to view its information. The side panel also includes a Network policies tab. From this tab, you can view, copy to the clipboard, or export any network policy defined in that namespace, as shown in the following example.
To view network graphs, the user must have at least the permissions granted to the Network Graph Viewer default permission set.
The following permissions are granted to the Network Graph Viewer permission set:
Read Deployment
Read NetworkGraph
Read NetworkPolicy
For more information, see "System permission sets" in the "Additional resources" section.
The network graph provides a visual map of deployments, namespaces, and connections that RHACS has discovered. By clicking on a deployment in the graph, you can view information about the deployment, including the following details:
Network security, such as the number of flows, existing or missing network policy rules, and listening ports
Labels and annotations
Port configurations
Container information
Anomalous and baseline flows for ingress and egress connections, including protocols and port numbers
Network policies
To view details for deployments in a namespace:
In the RHACS portal, navigate to Network Graph and select your cluster from the drop-down list.
Click the Namespaces list and use the search field to locate a namespace, or select individual namespaces.
Click the Deployments list and use the search field to locate a deployment, or select individual deployments to display in the network graph.
In the network graph, click on a deployment to view the information panel.
Click the Details, Flows, Baseline, or Network policies tab to view the corresponding information.
Network policies specify how groups of pods are allowed to communicate with each other and with other network endpoints.
Kubernetes NetworkPolicy
resources use labels to select pods and define rules that specify what traffic is allowed to or from the selected pods.
RHACS discovers and displays network policy information for all your Kubernetes clusters, namespaces, deployments, and pods, in the network graph.
In the RHACS portal, navigate to Network Graph and select your cluster from the drop-down list.
Click the Namespaces list and select individual namespaces, or use the search field to locate a namespace.
Click the Deployments list and select individual deployments, or use the search field to locate a deployment.
In the network graph, click on a deployment to view the information panel.
In the Details tab, in the Network security section, you can view summary messages about network policy rules that give the following information:
If policies exist in the network that regulate ingress or egress traffic
If your network is missing policies and is therefore allowing all ingress or egress traffic
To view the YAML file for the network policies, you can click on the policy rule, or click the Network policies tab.
You can specify custom CIDR blocks or configure the display of auto-discovered CIDR blocks in the network graph.
In the RHACS portal, navigate to Network Graph, and then select Manage CIDR Blocks. You can perform the following actions:
Toggle Auto-discovered CIDR blocks to hide auto-discovered CIDR blocks in the network graph.
When you hide the auto-discovered CIDR blocks, the auto-discovered CIDR blocks are hidden for all clusters, and not only for the selected cluster in the network graph. |
Add a custom CIDR block to the graph by performing the following steps:
Enter the CIDR name and CIDR address in the fields. To add additional CIDR blocks, click Add CIDR block and enter information for each block.
Click Update Configuration to save the changes.
A Kubernetes network policy controls which pods receive incoming network traffic, and which pods can send outgoing traffic. By using network policies to enable and disable traffic to or from pods, you can limit your network attack surface.
These network policies are YAML configuration files. It is often difficult to gain insights into the network flow and manually create these files. You can use RHACS to generate these files. When you automatically generate network policies, RHACS follows these guidelines:
RHACS generates a single network policy for each deployment in the namespace. The pod selector for the policy is the pod selector of the deployment.
If a deployment already has a network policy, RHACS does not generate new policies or delete existing policies.
Generated policies only restrict traffic to existing deployments.
Deployments that you create later will not have any restrictions unless you create or generate new network policies for them.
If a new deployment needs to contact a deployment with a network policy, you might need to edit the network policy to allow access.
Each policy has the same name as the deployment name, prefixed with stackrox-generated-
.
For example, the policy name for the deployment depABC
in the generated network policy is stackrox-generated-depABC
.
All generated policies also have an identifying label.
RHACS generates a single rule allowing traffic from any IP address if one of the following conditions are met:
The deployment has an incoming connection from outside the cluster within the selected time
The deployment is exposed through a node port or load balancer service
RHACS generates one ingress
rule for every deployment from which there is an incoming connection.
For deployments in the same namespace, this rule uses the pod selector labels from the other deployment.
For deployments in different namespaces, this rule uses a namespace selector.
To make this possible, RHACS automatically adds a label, namespace.metadata.stackrox.io/name
, to each namespace.
In rare cases, if a standalone pod does not have any labels, the generated policy allows traffic from or to the pod’s entire namespace. |
RHACS lets you automatically generate network policies based on the actual observed network communication flows in your environment.
You can generate policies based on the cluster, namespaces, and deployments that you have selected in the network graph. Policies are generated for any deployments that are included in the current Network Graph scope. For example, the current scope could include the entire cluster, a cluster and namespaces, or individually selected deployments in the selected namespaces. You can also further reduce the scope by applying one of the filters from the Filter deployments field with any combination of the cluster, namespace, and deployment selections. For example, you could narrow the scope to deployments in a specific cluster and namespace that are affected by a specific CVE. Policies are generated from the traffic observed during the baseline discovery period.
In the RHACS portal, navigate to Network Graph.
Select a cluster, and then select one or more namespaces.
Optional: Select individual deployments to restrict the policy generated to only those deployments. You can also use the Filter deployments feature to further narrow the scope.
In the network graph header, select Network policy generator.
Optional: In the information panel that opens, select Exclude ports & protocols to remove the port/protocol restrictions when generating network policies from a baseline.
As an example, the nginx3
deployment makes a port 80 connection to nginx4
, and this is included as part of the baseline for nginx4
. If policies are generated and this checkbox is not selected (the default behavior), the generated policy will restrict the allowed connections from nginx3
to nginx4
to only port 80. If policies are generated with this option selected, the generated policy will allow any port in the connection from nginx3
to nginx4
.
Click Generate and simulate network policies. RHACS generates policies for the scope that you have chosen. This scope is displayed at the top of the Generate network policies panel.
Clicking on the deployment information in the scope displays a list of the deployments that are included. |
Optional: Copy the generated network policy configuration YAML file to the clipboard or download it by clicking the download icon in the panel.
Optional: To compare the generated network policies to the existing network policies, click Compare. The YAML files for existing and generated network policies are shown in a side-by-side view.
Some items do not have generated policies, such as namespaces with existing ingress policies or deployments in certain protected namespaces such as as |
Optional: Click the Actions menu to perform the following activities:
Share the YAML file with notifiers: Sends the YAML file to one of the system notifiers you have configured, for example, Slack, ServiceNow, or an application that uses generic webhooks. These notifiers are configured by navigating to Platform Configuration → Integrations. See the documentation in the "Additional resources" section for more information.
Rebuild rules from active traffic: Refreshes the generated policies that are displayed.
Revert rules to previously applied YAML: Removes the simulated policy and reverts to the last network policy.
You can download and save the generated network policies from RHACS. Use this option to download policies so that you can commit the policies into a version control system such as Git.
After generating a network policy, click the Download YAML icon in the Network Policy Simulator panel.
After you download the network policies that RHACS generates, you can test them by applying them to your cluster by using the CLI or your automated deployment procedures. You cannot apply generated network policies directly in the network graph.
To create policies using the saved YAML file, run the following command:
$ oc create -f "<generated_file>.yml" (1)
1 | If you use Kubernetes, enter kubectl instead of oc . |
If the generated policies cause problems, you can remove them by running the following command:
$ oc delete -f "<generated_file>.yml" (1)
1 | If you use Kubernetes, enter kubectl instead of oc . |
Directly applying network policies might cause problems for running applications. Always download and test the network policies in a development environment or test clusters before applying them to production workloads. |
You can remove a policy and revert to a previously applied policy.
In the RHACS portal, navigate to Network Graph.
Select a cluster name from the menu on the top bar.
Select one or more namespaces and deployments.
Select Simulate network policy.
Select View active YAMLS.
From the Actions menu, select Revert rules to previously applied YAML.
Directly applying network policies might cause problems for running applications. Always download and test the network policies in a development environment or test clusters before applying them to production workloads. |
You can delete all automatically generated policies from your cluster that you have created by using RHACS.
Run the following command:
$ oc get ns -o jsonpath='{.items[*].metadata.name}' | \
xargs -n 1 oc delete networkpolicies -l \
'network-policy-generator.stackrox.io/generated=true' -n (1)
1 | If you use Kubernetes, enter kubectl instead of oc . |
Your current network policies might allow unneeded network communications. You can use the network policy generator to create network policies that restrict ingress traffic to the computed baselines for a set of deployments.
The Network Graph does not display the generated policies in the visualization. Generated policies are only for ingress traffic and policies that restrict egress traffic are not generated. |
In the RHACS portal, navigate to Network Graph.
Select a cluster, and then select one or more namespaces.
On the network graph header, select Network policy generator.
Optional: To generate a YAML file with network policies to use in the simulation, click Generate and simulate network policies. For more information, see "Generating network policies in the network graph".
Upload a YAML file of a network policy that you want to use in the simulation. The network graph view displays what your proposed network policies would achieve. Perform the following steps:
Click Upload YAML and then select the file.
Click Open. The system displays a message to indicate the processing status of the uploaded policy.
You can view active YAML files that correspond to the current network policies by clicking the View active YAMLS tab, and then selecting policies from the drop-down list. You can also perform the following actions:
Click the appropriate button to copy or download the displayed YAML file.
Use the Actions menu to rebuild rules from active traffic or revert rules to a previously applied YAML. For more information, see "Generating network policies in the network graph".
In RHACS, you can minimize your risks by using network baselining. It is a proactive approach to keep your infrastructure secure. RHACS first discovers existing network flows and creates a baseline, and then it treats network flows outside of this baseline as anomalous.
When you install RHACS, there is no default network baseline. As RHACS discovers network flows, it creates a baseline and then it adds all discovered network flows to it, following these guidelines:
When RHACS discovers new network activity, it adds that network flow to the network baseline.
Network flows do not show up as anomalous flows and do not trigger any violations.
After the discovery phase, the following actions occur:
RHACS stops adding network flows to the network baselines.
New network flows that are not in the network baseline show up as anomalous flows but they do not trigger any violations.
You can view network baselines from the network graph view.
Click the Namespaces list and use the search field to locate a namespace, or select individual namespaces.
Click the Deployments list and use the search field to locate a deployment, or select individual deployments to display in the network graph.
In the network graph, click on a deployment to view the information panel.
Select the Baseline tab. Use the filter by entity name field to further restrict the flows that are displayed.
Optional: You can mark baseline flows as anomalous by performing one of the following actions:
Select an individual entity, and then click and select Mark as anomalous.
Select multiple entities, and then click Bulk actions and select Mark as anomalous.
Optional: Check the box to exclude ports and protocols.
Optional: To save the baseline as a network policy YAML file, click Download baseline as network policy.
You can download network baselines as YAML files from the network graph view.
In the RHACS portal, navigate to Network Graph.
Click the Namespaces list and use the search field to locate a namespace, or select individual namespaces.
Click the Deployments list and use the search field to locate a deployment, or select individual deployments to display in the network graph.
In the network graph, click on a deployment to view the information panel.
The Baseline tab lists the baseline flows. Use the filter by entity name field to further restrict the list of flows.
Optional: Check the box to exclude ports and protocols.
Click Download baseline as network policy.
You can configure RHACS to detect anomalous network flows and trigger violations for traffic that is not in the baseline. This can help you determine if the network contains unwanted traffic before you block traffic with a network policy.
Click the Namespaces list and use the search field to locate a namespace, or select individual namespaces.
Click the Deployments list and use the search field to locate a deployment, or select individual deployments to display in the network graph.
In the network graph, click on a deployment to view the information panel.
In the Baseline tab, you can view baseline flows. Use the filter by entity name field to further restrict the flows that are displayed.
Toggle the Alert on baseline violations option.
After you toggle the Alert on baseline violations option, anomalous network flows trigger violations.
You can toggle the Alert on baseline violations option again to stop receiving violations for anomalous network flows.
Build time network policy tools let you automate the creation and validation of Kubernetes network policies in your development and operations workflows using the roxctl
CLI. These tools work with a specified file directory containing your project’s workload and network policy manifests and do not require RHACS authentication.
Command | Description |
---|---|
|
Generates Kubernetes network policies by analyzing your project’s YAML manifests in a specified directory. The previous syntax was |
|
Lists the allowed connections between workloads in your project directory by examining the workload and Kubernetes network policy manifests. Additionally, a graphical representation in |
|
Creates a list of variations in the allowed connections between two project versions. This is determined by the workload and Kubernetes network policy manifests in each version’s directory. You can view the semantic differences, which might not be immediately obvious, in a syntactic difference output. For more information, see Identifying the differences in allowed connections between project versions. |
Build-time network policy generation 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. |
The build-time network policy generator can automatically generate Kubernetes network policies based on application YAML manifests. You can use it to develop network policies as part of the continuous integration/continuous deployment (CI/CD) pipeline before deploying applications on your cluster.
Red Hat developed this feature in partnership with the developers of the NP-Guard project.
First, the build-time network policy generator analyzes Kubernetes manifests in a local folder, including service manifests, config maps, and workload manifests such as Pod
, Deployment
, ReplicaSet
, Job
, DaemonSet
, and StatefulSet
.
Then, it discovers the required connectivity and creates the Kubernetes network policies to achieve pod isolation.
These policies allow no more and no less than the needed ingress and egress traffic.
The build-time network policy generator is included in the roxctl
CLI.
For the build-time network policy generation feature, roxctl
CLI does not need to communicate with RHACS Central so you can use it in any development environment.
The build-time network policy generator recursively scans the directory you specify when you run the command.
Therefore, before you run the command, you must already have service manifests, config maps, and workload manifests such as Pod
, Deployment
, ReplicaSet
, Job
, DaemonSet
, and StatefulSet
as YAML files in the specified directory.
Verify that you can apply these YAML files as-is using the kubectl apply -f
command. The build-time network policy generator does not work with files that use Helm style templating.
Verify that the service network addresses are not hard-coded. Every workload that needs to connect to a service must specify the service network address as a variable. You can specify this variable by using the workload’s resource environment variable or in a config map.
Service network addresses must match the following official regular expression pattern:
(http(s)?://)?<svc>(.<ns>(.svc.cluster.local)?)?(:<portNum>)? (1)
1 | In this pattern,
|
Following are some examples that match the pattern:
wordpress-mysql:3306
redis-follower.redis.svc.cluster.local:6379
redis-leader.redis
http://rating-service.
Verify that the build-time network policy generation feature is available by running the help command:
$ roxctl netpol generate -h
Generate the policies by using the netpol generate
command:
$ roxctl netpol generate <folder-path> (1)
1 | Specify the path of the folder that has the Kubernetes manifests. |
The roxctl netpol generate
command supports the following options:
|
Description |
|
View the help text for the |
|
Save the generated policies into a target folder. One file per policy. |
|
Save and merge the generated policies into a single YAML file. |
|
Fail on the first encountered error. The default value is |
|
Remove the output path if it already exist. |
|
Treat warnings as errors. The default value is |
After generating the policies, you must inspect them for completeness and accuracy, in case any relevant network address was not specified as expected in the YAML files. Most importantly, verify that required connections are not blocked by the isolating policies. To help with this inspection, you can use RHACS to simulate the generated network policies .
Applying network policies to the cluster as part of the workload deployment using automation saves time and ensures accuracy. You can follow a GitOps approach by submitting the generated policies using Pull Requests, providing the team an opportunity to review the policies before deploying them as part of the pipeline. |
Connectivity mapping reports provide insights into the allowed connections between different workloads based on network policies defined in Kubernetes manifests. You can visualize and understand how different workloads in your Kubernetes environment are allowed to communicate with each other according to the network policies you set up.
In the following example, dir1
is netpol-analysis-example-minimal/
, and dir2
is netpol-diff-example-minimal/
.
Run the following command to retrieve the connectivity mapping information from dir1
:
$ roxctl netpol connectivity map netpols-analysis-example-minimal/
0.0.0.0-255.255.255.255 => default/frontend[Deployment] : TCP 8080
default/frontend[Deployment] => 0.0.0.0-255.255.255.255 : UDP 53
default/frontend[Deployment] => default/backend[Deployment] : TCP 9090
Run the following command to retrieve the connectivity mapping information from dir2
:
$ roxctl netpol connectivity map netpol-diff-example-minimal/
0.0.0.0-255.255.255.255 => default/backend[Deployment] : TCP 9090
0.0.0.0-255.255.255.255 => default/frontend[Deployment] : TCP 8080
default/frontend[Deployment] => 0.0.0.0-255.255.255.255 : UDP 53
default/frontend[Deployment] => default/backend[Deployment] : TCP 9090,UDP 53
Based on the workload and Kubernetes network policy manifests present in each version directory, you can list the differences in allowed connections between two versions of a project. You can view the semantic differences that might not be immediately apparent in a syntactic difference output.
Run the following command to determine the connectivity differences between the Kubernetes manifests in the specified directories:
$ roxctl netpol connectivity diff --dir1=<folder1> --dir2=<folder2> (1)
1 | Specify the actual path to the directories containing the Kubernetes manifests you want to compare. |
The command considers all YAML files that you can accept using |
The semantic difference report gives you an overview of the connections that were changed, added, or removed in dir2
compared to the connections allowed in dir1
. When you review the output, each line represents one allowed connection that was added, removed, or changed in dir2
compared to dir1
.
If applicable, the workloads-diff-info
provides additional details about the workloads associated with the added or removed connections.
In the following example, dir1
is netpol-analysis-example-minimal/
, and dir2
is netpol-diff-example-minimal/
. The difference between the directories is a small change in the network policy backend-netpol
.
dir1
:apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
creationTimestamp: null
name: backend-netpol
spec:
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- port: 9090
protocol: TCP
podSelector:
matchLabels:
app: backendservice
policyTypes:
- Ingress
- Egress
status: {}
The change in dir2
is an added -
before the ports attribute, which produces a difference output.
Run the following command to compare the contents of the netpols.yaml
files in the two specified directories:
$ diff netpol-diff-example-minimal/netpols.yaml netpol-analysis-example-minimal/netpols.yaml
12c12
< - ports:
---
> ports:
Run the following command to analyze the connectivity differences between the Kubernetes manifests and network policies in the two specified directories:
$ roxctl netpol connectivity diff --dir1=roxctl/netpol/connectivity/diff/testdata/netpol-analysis-example-minimal/ --dir2=roxctl/netpol/connectivity/diff/testdata/netpol-diff-example-minimal
Connectivity diff:
diff-type: changed, source: default/frontend[Deployment], destination: default/backend[Deployment], dir1: TCP 9090, dir2: TCP 9090,UDP 53
diff-type: added, source: 0.0.0.0-255.255.255.255, destination: default/backend[Deployment], dir1: No Connections, dir2: TCP 9090