6.2. Custom security policies
In addition to using the default policies, you can also create custom policies in Red Hat Advanced Cluster Security for Kubernetes.
You can create custom policies by using the following methods:
-
In the RHACS portal, go to Platform configuration
Policy management and click Create policy. - In the RHACS portal, go to Risk and use the filter to select the criteria that you want the policy to use. Click Create policy.
- Create and manage policies as code by saving policies as Kubernetes custom resources (CRs) and by applying them to clusters using a continuous delivery tool such as Argo CD.
See the following sections for more information.
6.2.1. Creating a security policy from the system policies view 링크 복사링크가 클립보드에 복사되었습니다!
You can create new security policies from the system policies view.
Procedure
-
In the RHACS portal, go to Platform Configuration
Policy Management. - Click Create policy.
- Configure the policy definition information in the following sections:
6.2.1.1. Enter policy details 링크 복사링크가 클립보드에 복사되었습니다!
Enter the following details about your policy in the Policy details section.
- Enter a Name for the policy.
- Select a Severity level for this policy.
- Select a policy category for the policy. This is a required field.
- Enter details about the policy in the Description field.
- Enter an explanation about why the policy exists in the Rationale field.
- Enter steps to resolve violations of this policy in the Guidance field.
Under the MITRE ATT&CK section, select the tactics and the techniques you want to specify for the policy.
- Click Add tactic, and then select a tactic from the drop-down list.
- Click the Add technique to add techniques for the selected tactic. You can specify multiple techniques for a tactic.
- Click Next.
6.2.1.2. Configure the policy lifecycle 링크 복사링크가 클립보드에 복사되었습니다!
In the Lifecycle section, complete the following steps:
Select the Lifecycle stages to which your policy is applicable: Build, Deploy, or Runtime. You can select more than one stage from the following choices:
- Build-time policies apply to image fields such as CVEs and Dockerfile instructions.
- Deploy-time policies can include all build-time policy criteria but they can also include data from your cluster configurations, such as running in privileged mode or mounting the Docker socket.
- Runtime policies can include all build-time and deploy-time policy criteria but they can also include data about process executions during runtime.
If you selected the Runtime lifecycle stage, you must select one of the following Event sources:
- Deployment: RHACS triggers policy violations when event sources include process and network activity, pod execution and pod port forwarding.
- Audit logs: RHACS triggers policy violations when event sources match Kubernetes audit log records.
- Click Next.
6.2.1.3. Configure the policy rules and criteria 링크 복사링크가 클립보드에 복사되었습니다!
To configure a policy rule:
- In the Rules section, configure the conditions that you want to trigger the policy. You can edit the rule titles and click Add a new rule to add an additional rule.
For each rule, click and drag policy fields into the Policy Section to add policy fields or criteria.
참고The policy fields that are available depend on the lifecycle stage you chose for the policy. For example, criteria under Kubernetes access policies or Networking are available when creating a policy for the runtime lifecycle, but not when creating a policy for the build lifecycle. See "Policy criteria" in the "Additional resources" section for more information about policy criteria, including information about criteria and the lifecycle phase in which they are available.
For each field, you can select from options that are specific to the field. These differ depending on the type of field. For example:
- The default behavior for a value that is a string is to match on a policy field, and you click Not to indicate when you do not want the field to match.
-
Some fields contain a value that is either
trueorfalse. - Some fields require you to select a value from a drop-down list.
-
If you select an attribute with Boolean values
Read-Only Root Filesystem, you will seeREAD-ONLYandWRITABLEoptions. If you select an attribute with compound values
Environment variable, you will see options to enter values forKey,Value, andValue Fromfields, and an icon to add more values for the available options.참고See "Policy criteria" in the "Additional resources" section for more information.
- To combine multiple values for an attribute, click the Add icon.
- Click Next.
6.2.1.4. Configure the policy scope 링크 복사링크가 클립보드에 복사되었습니다!
Create scopes to restrict or exclude your policy from entities, such as cluster or namespaces, within your environment.
- To restrict by scope, click Add inclusion scope. This enables this policy to only be applied for a specific cluster, a namespace, or a deployment label. You can add multiple scopes and also use regular expressions in RE2 Syntax for namespaces and labels.
To exclude by scope, for example, to exclude specific deployments, clusters, namespaces, and deployment labels from the policy, click Add exclusion scope. The policy will not apply to the entities that you select. You can add multiple scopes and also use regular expressions in RE2 Syntax for namespaces and labels. However, you cannot use regular expressions for selecting deployments.
참고This function is only available for policies configured for the deploy and runtime lifecycle stages.
For policies configured for the build lifecycle stage, you can exclude images from the policy. In the Exclude images (Build lifecycle only) field, enter the images that you do not want to trigger a violation for.
참고The Excluded Images setting only applies when you check images in a continuous integration system with the Build lifecycle stage. It does not have any effect if you use this policy to check running deployments in the Deploy lifecycle stage or runtime activities in the Runtime lifecycle stage.
- Click Next.
6.2.1.5. Configure policy actions 링크 복사링크가 클립보드에 복사되었습니다!
Configure the activation state, enforcement, and notifiers for the policy.
- Select an activation state for the policy.
Select an enforcement method:
- Inform: Include the violation in the violations list.
- Inform and enforce: enforce actions. If you select this option, you must select the enforcement behavior for the policy by using the toggle for each lifecycle. The enforcement behavior you can select depends on the lifecycle stages you selected for the policy in the Lifecycle section of the policy definition. The following enforcement behaviors are available depending on the lifecycle stage:
- Build: RHACS fails your continuous integration (CI) builds when images match the criteria of the policy.
Deploy: For the Deploy stage, RHACS blocks the creation and update of deployments that match the conditions of the policy if the RHACS admission controller is configured and running.
- In clusters with admission controller enforcement, the Kubernetes or OpenShift Container Platform API server blocks all noncompliant deployments. In other clusters, RHACS edits noncompliant deployments to prevent pods from being scheduled.
- For existing deployments, policy changes only result in enforcement at the next detection of the criteria, when a Kubernetes event occurs. For more information about enforcement, see "Security policy enforcement for the deploy stage".
Runtime: RHACS deletes all pods when an event in the pods matches the criteria of the policy.
주의Policy enforcement can impact running applications or development processes. Before you enable enforcement options, inform all stakeholders and plan how to respond to automated enforcement actions.
Attach notifiers to the policy to send policy violations to email recipients or external tooling such as Jira, Splunk, or other applications that use webhooks.
Select the notifiers from the list.
참고You must have previously configured the notification before it is visible and available to select in the list. You configure these integrations in the Platform Configuration
Integrations page, in the Notifier Integrations section.
- Click Next.
6.2.1.6. Review the policy and preview violations 링크 복사링크가 클립보드에 복사되었습니다!
Review the policy settings you have configured.
- Verify that the policy configuration is configured with the correct options.
The Preview violations panel provides additional information, including whether or not build phase or deploy phase deployments have violations of the policy.
참고Runtime violations are not available in this preview because they are generated in response to future events.
Before you save the policy, verify that the violations seem accurate.
- Click Save.
6.2.1.7. Adding logical conditions for the policy criteria 링크 복사링크가 클립보드에 복사되었습니다!
You can use the drag-and-drop policy fields panel to specify logical conditions for the policy criteria.
Prerequisites
- You must be using Red Hat Advanced Cluster Security for Kubernetes version 3.0.45 or newer.
Procedure
In the Policy Criteria section, select Add a new condition to add a new policy section.
- You can click on the Edit icon to rename the policy section.
- The Drag out a policy field section lists available policy criteria in multiple categories. You can expand and collapse these categories to view the policy criteria attributes.
- Drag an attribute to the Drop a policy field inside area of the policy section.
Depending on the type of the attribute you select, you get different options to configure the conditions for the selected attribute. For example:
-
If you select an attribute with Boolean values
Read-Only Root Filesystem, you will seeREAD-ONLYandWRITABLEoptions. If you select an attribute with compound values
Environment variable, you will see options to enter values forKey,Value, andValue Fromfields, and an icon to add more values for the available options.- To combine multiple values for an attribute, click the Add icon.
-
You can also click on the logical operator
ANDorORlisted in a policy section, to toggle betweenANDandORoperators. Toggling between operators only works inside a policy section and not between two different policy sections.
-
If you select an attribute with Boolean values
-
You can specify more than one
ANDandORcondition by repeating these steps. After you configure the conditions for the added attributes, click Next to continue with the policy creation.
6.2.2. Creating a security policy from the risk view 링크 복사링크가 클립보드에 복사되었습니다!
While evaluating risks in your deployments in the Risk view, when you apply local page filtering, you can create new security policies based on the filtering criteria you are using.
Procedure
- Go to the RHACS portal and select Risk from the navigation menu.
- Apply local page filtering criteria that you want to create a policy for.
- Select New Policy and fill in the required fields to create a new policy.
6.2.3. Modifying existing security policies 링크 복사링크가 클립보드에 복사되었습니다!
You can edit the policies you have created and the existing default policies provided by Red Hat Advanced Cluster Security for Kubernetes that you have cloned.
Procedure
-
In the RHACS portal, go to Platform Configuration
Policy Management. - From the Policies page, select the policy you want to edit.
Select Actions
Edit policy. 참고You cannot edit default policies. You must clone a default policy and edit the cloned policy.
- Edit the fields that you want to change and click Save.
6.2.4. Managing polices as code 링크 복사링크가 클립보드에 복사되었습니다!
You can create and manage policies as code by saving policies as Kubernetes custom resources (CRs) and applying them to clusters by using a Kubernetes-native continuous delivery (CD) tool such as Argo CD.
Policy as code is useful for Kubernetes security architects who want to author policies in YAML or JSON instead of using the RHACS portal. GitOps administrators who already manage Kubernetes configurations by using a GitOps workflow can also find it useful.
RHACS provides the ability to use default policies or create custom policies for your system. With the policy as code feature, you can create custom policies locally by downloading them and modifying them, or by creating them from empty files. To author policies locally, you create CRs that represent the desired state of the policies. You then use a continuous delivery tool such as Argo CD to track, manage, and apply policies to your clusters that are running RHACS. After you create or update CRs and use the CI/CD tool to apply them, the policies stored in the RHACS database are created or updated.
With this feature, RHACS installs a new Kubernetes controller in the namespace where Central is installed, typically the stackrox namespace. With an Argo CD workflow, you configure Argo CD to apply policy as code resources to the same namespace in which RHACS is installed. After you configure this connection, the controller in RHACS receives information from the Kubernetes API about new, updated, or deleted policies that are managed as individual Kubernetes CR files. RHACS reconciles the policy CR to the policy stored in the RHACS database.
With a GitOps workflow that does not use Argo CD, you configure your GitOps repository to connect to Central in RHACS through the RHACS API. A CR is not used.
6.2.4.1. About policy drift 링크 복사링크가 클립보드에 복사되었습니다!
Because policies can be edited, deleted, and created in the RHACS portal and also externally, sometimes policy drift can occur. Drift occurs when the version of a policy in Central in RHACS does not match the version of the policy in Kubernetes.
Drift can occur when a change is applied to an externally-managed policy by using the RHACS portal or the API instead of by modifying its Kubernetes custom resource. RHACS does not prevent drift, but it is not recommended. Drift is automatically resolved within ten hours after it was introduced.
6.2.4.2. Creating policies in code by using the RHACS portal 링크 복사링크가 클립보드에 복사되었습니다!
You can create new policies in code by using the RHACS portal to save existing policies as YAML files.
Prerequisites
- You must have RHACS release 4.6 or later installed.
If you installed RHACS by using the manifest installation method, also called the
roxctlmethod, you must manually apply theconfig.stackrox.ioCRD that is located in the .zip file athelm/chart/crds/config.stackrox.io_securitypolicies.yamlby using the following command:$ kubectl create -f helm/chart/crds/config.stackrox.io_securitypolicies.yaml
Procedure
To create a new policy in code by using the RHACS portal to create the CR:
In the Policy Management page, create a new policy or clone a default policy.
참고You must clone a default policy before you can save it as a CR.
-
In the row listing the policy, click the overflow menu,
, and then select Save as Custom Resource. To save multiple policies at one time, you can select them and click Bulk actions Save as Custom Resources. After editing your policy, you can apply the saved CR by doing one of the following:
-
Use the
oc applyorkubectl applycommand to apply the CR directly to the Kubernetes namespace where Central is installed. - Use Argo CD or your GitOps tool to push the CR to the Kubernetes namespace where Central is installed.
-
Use the
6.2.4.3. Creating policies in code by constructing a CR 링크 복사링크가 클립보드에 복사되었습니다!
You can create new policies in code by constructing a CR for the policy.
Use an editor to construct a CR for the policy with the following attributes:
kind: SecurityPolicy apiVersion: config.stackrox.io/v1alpha1 metadata: name: short-name spec: policyName: A longer form name # ...작은 정보Use online documentation, for example, by entering the
kubectl explain securitypolicy.speccommand, to understand the fields available for defining a policy specification.Apply the saved CR by doing one of the following:
-
Use the
oc applyorkubectl applycommand to apply the CR directly to the Kubernetes namespace where Central is installed. - Use Argo CD or your GitOps tool to push the CR to the Kubernetes namespace where Central is installed.
-
Use the
6.2.4.4. Disabling the policy as code feature 링크 복사링크가 클립보드에 복사되었습니다!
The policy as code feature is automatically enabled when you install RHACS, but you can disable it.
Procedure
To disable the policy as code feature, complete one of the following tasks, depending on the method you used to install RHACS:
-
If you installed RHACS by using the Operator, set the
spec.configAsCode.configAsCodeComponentfield toDisabled. -
If you installed RHACS by using Helm charts, set the
configAsCode.enabledfield in thevalues.yamlfile tofalse. If you installed RHACS by using the manifest installation method, also known as the
roxctlmethod, delete theconfig-controllerdeployment by running the following command:$ kubectl -n stackrox delete deployment config-controller1 - 1
- For OpenShift Container Platform, use
ocinstead ofkubectl.