Chapter 9. Create security policies


Create a security policy to report and validate your cluster compliance based on your specified security standards, categories, and controls.

Required access: Cluster administrator

Prerequisites

  • You must define a placement and placement binding to apply your policy to a specific cluster. The PlacementBinding resource binds the placement. Enter a valid value for the cluster Label selector field to define a Placement and PlacementBinding resource.
  • To use a Placement resource, a ManagedClusterSet resource must be bound to the namespace of the Placement resource with a ManagedClusterSetBinding resource. Refer to Creating a ManagedClusterSetBinding resource for additional details.

Procedure

  1. Create a policy by running the following command:

    oc create -f policy.yaml -n <policy-namespace>
  2. Edit your YAML file by adding a policy-templates field to define the template the policy uses. Your policy might resemble the following YAML file:

    apiVersion: policy.open-cluster-management.io/v1
    kind: Policy
    metadata:
      name: policy1
    spec:
      remediationAction: "enforce"
      disabled: false
      namespaceSelector:
        include:
        - "default"
        - "my-namespace"
      policy-templates:
        - objectDefinition:
            apiVersion: policy.open-cluster-management.io/v1
            kind: ConfigurationPolicy
            metadata:
              name: operator
               namespace:
            spec:
              remediationAction: "inform"
              object-templates:
              - complianceType: "musthave" # at this level, it means the role must exist and must have the following rules
                apiVersion: rbac.authorization.k8s.io/v1
                kind: Role
                metadata:
                  name: example
                objectDefinition:
                  rules:
                    - complianceType: "musthave" # at this level, it means if the role exists the rule is a musthave
                      apiGroups: ["extensions", "apps"]
                      resources: ["deployments"]
                      verbs: ["get", "list", "watch", "create", "delete","patch"]

    Note: The namespace parameter is optional within the template metadata configuration. The namespaceSelector parameter defines the target namespaces for the deployed policy.

  3. Define a PlacementBinding resource to bind your policy to your Placement resource. Your PlacementBinding resource might resemble the following YAML sample:

    apiVersion: policy.open-cluster-management.io/v1
    kind: PlacementBinding
    metadata:
      name: binding1
    placementRef:
      name: placement1
      apiGroup: cluster.open-cluster-management.io
      kind: Placement
    subjects:
    - name: policy1
      apiGroup: policy.open-cluster-management.io
      kind: Policy

Create a cluster security policy from the console with placement resources.

Procedure

  1. Log in to your Red Hat Advanced Cluster Management.
  2. Go to the Governance page and click Create policy. As you create your new policy from the console, a YAML file is also created in the YAML editor.
  3. To view the YAML editor, select the toggle at the beginning of the Create policy form to enable it.
  4. Complete the Create policy form, then select the Submit button. If you choose to create a pod policy, your YAML file might resemble the following policy:

    apiVersion: policy.open-cluster-management.io/v1
    kind: Policy
    metadata:
      name: policy-pod
      annotations:
        policy.open-cluster-management.io/categories: 'SystemAndCommunicationsProtections,SystemAndInformationIntegrity'
        policy.open-cluster-management.io/controls: 'control example'
        policy.open-cluster-management.io/standards: 'NIST,HIPAA'
        policy.open-cluster-management.io/description:
    spec:
      complianceType: musthave
      namespaces:
        exclude: ["kube*"]
        include: ["default"]
        pruneObjectBehavior: None
      object-templates:
      - complianceType: musthave
        objectDefinition:
          apiVersion: v1
          kind: Pod
          metadata:
            name: pod1
          spec:
            containers:
            - name: pod-name
              image: 'pod-image'
              ports:
              - containerPort: 80
      remediationAction: enforce
      disabled: false
  5. Create the following PlacementBinding example:

    apiVersion: policy.open-cluster-management.io/v1
    kind: PlacementBinding
    metadata:
      name: binding-pod
    placementRef:
      name: placement-pod
      kind: Placement
      apiGroup: cluster.open-cluster-management.io
    subjects:
    - name: policy-pod
      kind: Policy
      apiGroup: policy.open-cluster-management.io
  6. Create the following Placement example:

    apiVersion: cluster.open-cluster-management.io/v1beta1
     kind: Placement
     metadata:
       name: placement-pod
    spec:
      predicates:
      - requiredClusterSelector:
          labelSelector:
            matchLabels:
              cloud: "IBM"
        tolerations:
            - key: cluster.open-cluster-management.io/unavailable
              operator: Exists
            - key: cluster.open-cluster-management.io/unreachable
              operator: Exists
  7. Optional: Add a description for your policy.
  8. Click Create Policy.

9.2. View your security policy from the console

View policy details and violations on the Governance page.

Procedure

  1. Navigate to the Governance page to view a table list of your policies. Note: You can filter the table list of your policies by selecting the Policies tab or Cluster violations tab.
  2. Select one of your policies to view more details. The Details, Clusters, and Templates tabs are displayed. When the cluster or policy status cannot be determined, the following message is displayed: No status.
  3. Alternatively, select the Policies tab to view the list of policies. Expand a policy row to view the Description, Standards, Controls, and Categories details.

View policy details from the command line.

Procedure

  1. View details for a specific security policy by running the following command:

    oc get policies.policy.open-cluster-management.io <policy-name> -n <policy-namespace> -o yaml
  2. View a description of your security policy by running the following command:

    oc describe policies.policy.open-cluster-management.io <policy-name> -n <policy-namespace>

9.4. Command line policytools overview

Review the policytools command-line tool. You can interact with policies locally to help with creating and debugging. The policytools command-line tool is available for download from the hub cluster console.

template-resolver

The template-resolver is a subcommand for policytools that resolves managed cluster and hub cluster templates that are embedded in policies. The template-resolver reads from either a file, or from standard input.

To resolve a policy with hub cluster templates, you must provide the --cluster-name argument with the name of a managed cluster that is imported into Red Hat Advanced Cluster Management, and you must provide the --hub-kubeconfig argument with the path to a kubeconfig file that references the hub cluster.

If the input policy uses the .ObjectName or .ObjectNamespace template variables, you need to set the values with the --object-name and --object-namespace arguments.

The template-resolver subcommand connects to the managed cluster and hub cluster to retrieve information that is necessary to resolve the templates. Provide paths by using the --save-resources and --save-hub-resources arguments to save the resources that the template-resolver retrieves. Those saved files can be used as inputs for the dryrun subcommand.

Technology Preview: The policytools command-line interface introduces a --lint argument for the template-resolver subcommand to print any linting issues, along with the resolved Go templates from the input policy. To generate a Static Analysis Results Interchange Format (SARIF) report of the linter results, specify a file path with the --sarif argument.

dryrun

The dryrun is a subcommand for policytools that evaluates a ConfigurationPolicy resource locally by using static input resources for the cluster state.

To run a policy, add the path to the ConfigurationPolicy YAML by using the --policy flag. You can provide additional resource YAML files, which are used as the current cluster state. For example, if your policy defines a specific configuration for a namespace, provide the current namespace state to see what the policy does. Multiple resources can be in the same resource file.

To evaluate a policy against the current state of a live cluster, use the --from-cluster flag or set the DRYRUN_FROM_CLUSTER environment variable to true.

The dryrun subcommand reads the current state of resources directly from the cluster that you configured in your kubeconfig file or from the cluster that is specified by the KUBECONFIG environment variable. In both these scenarios, the dryun subcommand does not gather information from the resource files.

When you enable the --from-cluster flag, it ignores any YAML resource files that are provided as input. Enabling the --from-cluster flag allows you to test a policy against your actual cluster state without manually exporting and supporting resource files.

+ By default, dryrun prints the differences between the desired states of objects that are defined in the policy and the current state of those resources, as well as compliance messages that the ConfigurationPolicy creates.

To save the compliance messages to a separate file, use the --messages-path argument. To save the full policy status, including additional information about the related objects, use the --status-path argument.

+ If the policy is NonCompliant based on the input, the tool returns a non-zero exit code. To compare the resulting status against a known status, use the --desired-status argument. For example, to verify that the policy reports a missing resource, provide a YAML file with the missing resource inside a .relatedObjects list. To verify that the policy is NonCompliant, create a YAML file with the compliant: NonCompliant line.

Additional resources

9.5. Create policy sets from the command line

Create a policy set from the CLI or console and assign policies. By default, the policy set is created with no policies or placements.

Important: You must create a placement for the policy set and have at least one policy that exists on your cluster. When you create a policy set, you can add numerous policies.

Procedure

  1. Run the following command to create a policy set from the command line:

    oc apply -f <policyset-filename>
  2. To create a policy set from the console, log in and select Governance.
  3. Select the Policy sets tab.
  4. Select the Create policy set button and complete the form.
  5. Add the details for your policy set and select the Submit button.

9.6. Add a policy to a policy set

Add an existing policy to a policy set from the command-line interface or console.

Procedure

  1. Add a policy to a policy set from the command-line interface:

    1. Run the following command to edit your policy set:
    oc edit policysets <your-policyset-name>
  2. Add the policy name to the list in the policies section of the policy set.
  3. Apply your added policy in the placement section of your policy set with the following command:

    oc apply -f <your-added-policy.yaml>

    Note: If you delete the placement binding, the policy is still placed by the policy set.

  4. Add a policy to a policy set from the console:

    1. Add a policy to the policy set by selecting the Policy sets tab.
    2. Select the Actions icon and select Edit. The Edit policy set form appears.
    3. Navigate to the Policies section of the form to select a policy to add to the policy set.

9.7. Delete policy sets

Delete a policy set from the console.

Procedure

  1. Delete policy sets from the console
  2. From the Policy sets tab, select the Actions icon for the policy set. When you click Delete, the Permanently delete Policyset? dialogue box appears.
  3. Click the Delete button.

9.8. Disable security policies

Your policy is enabled by default. Disable a policy from the console without deleting it.

Procedure

  1. Log in to your console, navigate to the Governance page to view a table list of your policies.
  2. Select the Actions icon > Disable policy. The Disable Policy dialog box appears.
  3. Click Disable policy.

9.9. Delete a security policy

Delete a security policy from the command line or the console.

Procedure

  • Delete a security policy from the command line interface:

    1. Delete a security policy by running the following command:

      oc delete policies.policy.open-cluster-management.io <policy-name> -n <policy-namespace>
    2. Verify that your policy is removed by running the following command:
    oc get policies.policy.open-cluster-management.io <policy-name> -n <policy-namespace>
  • Delete a security policy from the console:

    1. From the navigation menu, click Governance to view a table list of your policies.
    2. Click the Actions icon for the policy you want to delete in the policy violation table.
    3. Click Remove.
    4. From the Remove policy dialog box, click Remove policy.

9.10. Resource clean up for policies

Use the pruneObjectBehavior parameter in a configuration policy to clean up resources that are created by the policy. When pruneObjectBehavior is set, the related objects are only cleaned up after the configuration policy (or parent policy) associated with them is deleted.

View the following descriptions of the values that can be used for the parameter:

DeleteIfCreated
Cleans up any resources created by the policy.
DeleteAll
Cleans up all resources managed by the policy.
None
This is the default value and maintains the same behavior from previous releases, where no related resources are deleted.

You can set the value directly in the YAML file as you create a policy from the command line.

From the console, you can select the value in the Prune Object Behavior section of the Policy templates step.

Notes:

  • If a policy that installs an operator has the pruneObjectBehavior parameter defined, then additional clean up is needed to complete the operator uninstall. You might need to delete the operator ClusterServiceVersion object as part of this cleanup.
  • As you disable the config-policy-addon resource on the managed cluster, the pruneObjbectBehavior is ignored. To automatically clean up the related resources on the policies, you must remove the policies from the managed cluster before the add-on is disabled.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat Documentation

Legal Notice

Theme

© 2026 Red Hat
Back to top