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
PlacementBindingresource binds the placement. Enter a valid value for the cluster Label selector field to define aPlacementandPlacementBindingresource. -
To use a
Placementresource, aManagedClusterSetresource must be bound to the namespace of thePlacementresource with aManagedClusterSetBindingresource. Refer to Creating a ManagedClusterSetBinding resource for additional details.
Procedure
Create a policy by running the following command:
oc create -f policy.yaml -n <policy-namespace>Edit your YAML file by adding a
policy-templatesfield 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
namespaceparameter is optional within the template metadata configuration. ThenamespaceSelectorparameter defines the target namespaces for the deployed policy.Define a
PlacementBindingresource to bind your policy to yourPlacementresource. YourPlacementBindingresource 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
Additional resources
- View your security policy from the command line
- Create a cluster security policy from the console
- View your security policy from the console
- Create policy sets from the command line
- Add a policy to a policy set
- Disable security policies
- Delete a security policy
- Delete policy sets
- Cleaning up resources that are created by policies
- Hub cluster policy framework
- Resources that support support set-based requirements
- PolicySets-- Stable
- Governance
9.1. Create a cluster security policy from the console Copy linkLink copied to clipboard!
Create a cluster security policy from the console with placement resources.
Procedure
- Log in to your Red Hat Advanced Cluster Management.
- 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.
- To view the YAML editor, select the toggle at the beginning of the Create policy form to enable it.
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: falseCreate the following
PlacementBindingexample: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.ioCreate the following
Placementexample: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- Optional: Add a description for your policy.
- Click Create Policy.
9.2. View your security policy from the console Copy linkLink copied to clipboard!
View policy details and violations on the Governance page.
Procedure
- 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.
-
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. - Alternatively, select the Policies tab to view the list of policies. Expand a policy row to view the Description, Standards, Controls, and Categories details.
9.3. View your security policy from the command line Copy linkLink copied to clipboard!
View policy details from the command line.
Procedure
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 yamlView 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 Copy linkLink copied to clipboard!
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-resolverThe
template-resolveris a subcommand forpolicytoolsthat resolves managed cluster and hub cluster templates that are embedded in policies. Thetemplate-resolverreads from either a file, or from standard input.To resolve a policy with hub cluster templates, you must provide the
--cluster-nameargument with the name of a managed cluster that is imported into Red Hat Advanced Cluster Management, and you must provide the--hub-kubeconfigargument with the path to akubeconfigfile that references the hub cluster.If the input policy uses the
.ObjectNameor.ObjectNamespacetemplate variables, you need to set the values with the--object-nameand--object-namespacearguments.The
template-resolversubcommand connects to the managed cluster and hub cluster to retrieve information that is necessary to resolve the templates. Provide paths by using the--save-resourcesand--save-hub-resourcesarguments to save the resources that thetemplate-resolverretrieves. Those saved files can be used as inputs for thedryrunsubcommand.Technology Preview: The
policytoolscommand-line interface introduces a--lintargument for thetemplate-resolversubcommand 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--sarifargument.dryrunThe
dryrunis a subcommand forpolicytoolsthat evaluates aConfigurationPolicyresource locally by using static input resources for the cluster state.To run a policy, add the path to the
ConfigurationPolicyYAML by using the--policyflag. 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-clusterflag or set theDRYRUN_FROM_CLUSTERenvironment variable totrue.
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 Copy linkLink copied to clipboard!
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
Run the following command to create a policy set from the command line:
oc apply -f <policyset-filename>- To create a policy set from the console, log in and select Governance.
- Select the Policy sets tab.
- Select the Create policy set button and complete the form.
- Add the details for your policy set and select the Submit button.
9.6. Add a policy to a policy set Copy linkLink copied to clipboard!
Add an existing policy to a policy set from the command-line interface or console.
Procedure
Add a policy to a policy set from the command-line interface:
- Run the following command to edit your policy set:
oc edit policysets <your-policyset-name>-
Add the policy name to the list in the
policiessection of the policy set. 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.
Add a policy to a policy set from the console:
- Add a policy to the policy set by selecting the Policy sets tab.
- Select the Actions icon and select Edit. The Edit policy set form appears.
- Navigate to the Policies section of the form to select a policy to add to the policy set.
9.7. Delete policy sets Copy linkLink copied to clipboard!
Delete a policy set from the console.
Procedure
- Delete policy sets from the console
- From the Policy sets tab, select the Actions icon for the policy set. When you click Delete, the Permanently delete Policyset? dialogue box appears.
- Click the Delete button.
9.8. Disable security policies Copy linkLink copied to clipboard!
Your policy is enabled by default. Disable a policy from the console without deleting it.
Procedure
- Log in to your console, navigate to the Governance page to view a table list of your policies.
- Select the Actions icon > Disable policy. The Disable Policy dialog box appears.
- Click Disable policy.
9.9. Delete a security policy Copy linkLink copied to clipboard!
Delete a security policy from the command line or the console.
Procedure
Delete a security policy from the command line interface:
Delete a security policy by running the following command:
oc delete policies.policy.open-cluster-management.io <policy-name> -n <policy-namespace>- 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:
- From the navigation menu, click Governance to view a table list of your policies.
- Click the Actions icon for the policy you want to delete in the policy violation table.
- Click Remove.
- From the Remove policy dialog box, click Remove policy.
9.10. Resource clean up for policies Copy linkLink copied to clipboard!
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
pruneObjectBehaviorparameter defined, then additional clean up is needed to complete the operator uninstall. You might need to delete the operatorClusterServiceVersionobject as part of this cleanup. -
As you disable the
config-policy-addonresource on the managed cluster, thepruneObjbectBehavioris 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.