5.15. 了解自定义资源定义
OpenShift Container Platform 中的 Compliance Operator 为您提供了几个自定义资源定义(CRD)来完成合规性扫描。要运行合规性扫描,它会利用预定义的安全策略,该策略从 ComplianceAsCode 社区项目衍生而来。Compliance Operator 把这些安全策略转换为 CRD,您可以使用它来运行合规性扫描,并为发现的问题获取补救。
5.15.1. CRD 工作流
CRD 为您提供了以下工作流来完成合规性扫描:
- 定义合规性扫描要求
- 配置合规性扫描设置
- 使用合规性扫描设置处理合规性要求
- 监控合规性扫描
- 检查合规性扫描结果
5.15.2. 定义合规性扫描要求
默认情况下,Compliance Operator CRD 包含 ProfileBundle 和 Profile 对象,您可以在其中定义和设置合规性扫描要求的规则。您还可以使用 TailoredProfile 对象自定义默认配置集。
5.15.2.1. ProfileBundle 对象
安装 Compliance Operator 时,它包含 ready-to-run ProfileBundle 对象。Compliance Operator 解析 ProfileBundle 对象,并为捆绑包中的每个配置集创建一个 Profile 对象。它还会解析 Rule 和 Variable 对象,这些对象会被 Profile 对象使用。
ProfileBundle 对象示例
apiVersion: compliance.openshift.io/v1alpha1
kind: ProfileBundle
name: <profile bundle name>
namespace: openshift-compliance
status:
dataStreamStatus: VALID 1
- 1
- 指明 Compliance Operator 是否能够解析内容文件。
当 contentFile 失败时,会出现一个 errorMessage 属性,它提供所发生错误的详细信息。
故障排除
当您从无效的镜像回滚到已知内容镜像时,ProfileBundle 对象停止响应并显示 PENDING 状态。作为临时解决方案,您可以移到与上一个镜像不同的镜像。另外,您可以删除并重新创建 ProfileBundle 对象以返回到工作状态。
5.15.2.2. 配置集对象
Profile 对象定义可以为某个合规性标准评估的规则和变量。它包含 OpenSCAP 配置集的解析详情,如其 XCCDF 标识符和配置集会检查 Node 或 Platform 类型。您可以直接使用 Profile 对象,也可以使用 TailorProfile 对象进一步自定义它。
您无法手动创建或修改 Profile 对象,因为它是从单个 ProfileBundle 对象衍生而来。通常,单个 ProfileBundle 对象可以包含多个 Profile 对象。
Profile 对象示例
apiVersion: compliance.openshift.io/v1alpha1 description: <description of the profile> id: xccdf_org.ssgproject.content_profile_moderate 1 kind: Profile metadata: annotations: compliance.openshift.io/product: <product name> compliance.openshift.io/product-type: Node 2 creationTimestamp: "YYYY-MM-DDTMM:HH:SSZ" generation: 1 labels: compliance.openshift.io/profile-bundle: <profile bundle name> name: rhcos4-moderate namespace: openshift-compliance ownerReferences: - apiVersion: compliance.openshift.io/v1alpha1 blockOwnerDeletion: true controller: true kind: ProfileBundle name: <profile bundle name> uid: <uid string> resourceVersion: "<version number>" selfLink: /apis/compliance.openshift.io/v1alpha1/namespaces/openshift-compliance/profiles/rhcos4-moderate uid: <uid string> rules: 3 - rhcos4-account-disable-post-pw-expiration - rhcos4-accounts-no-uid-except-zero - rhcos4-audit-rules-dac-modification-chmod - rhcos4-audit-rules-dac-modification-chown title: <title of the profile>
5.15.2.3. 规则对象
Rule 对象形成了配置文件,也会作为对象公开。使用 Rule 对象定义合规检查要求,并指定它是如何修复的。
Rule 对象示例
apiVersion: compliance.openshift.io/v1alpha1
checkType: Platform 1
description: <description of the rule>
id: xccdf_org.ssgproject.content_rule_configure_network_policies_namespaces 2
instructions: <manual instructions for the scan>
kind: Rule
metadata:
annotations:
compliance.openshift.io/rule: configure-network-policies-namespaces
control.compliance.openshift.io/CIS-OCP: 5.3.2
control.compliance.openshift.io/NERC-CIP: CIP-003-3 R4;CIP-003-3 R4.2;CIP-003-3
R5;CIP-003-3 R6;CIP-004-3 R2.2.4;CIP-004-3 R3;CIP-007-3 R2;CIP-007-3 R2.1;CIP-007-3
R2.2;CIP-007-3 R2.3;CIP-007-3 R5.1;CIP-007-3 R6.1
control.compliance.openshift.io/NIST-800-53: AC-4;AC-4(21);CA-3(5);CM-6;CM-6(1);CM-7;CM-7(1);SC-7;SC-7(3);SC-7(5);SC-7(8);SC-7(12);SC-7(13);SC-7(18)
labels:
compliance.openshift.io/profile-bundle: ocp4
name: ocp4-configure-network-policies-namespaces
namespace: openshift-compliance
rationale: <description of why this rule is checked>
severity: high 3
title: <summary of the rule>
Rule 对象获取适当的标签,以便轻松识别关联的 ProfileBundle 对象。ProfileBundle 也在此对象的 OwnerReferences 中指定。
5.15.2.4. TailoredProfile 对象
使用 TailoredProfile 对象根据您的机构要求修改默认的 Profile 对象。您可以启用或禁用规则,设置变量值,并为自定义提供合理化。验证后,TailoredProfile 对象会创建一个 ConfigMap,它可以被 ComplianceScan 对象引用。
您可以通过在 ScanSettingBinding 对象中引用 TailoredProfile 对象来使用 TailoredProfile 对象。有关 ScanSettingBinding 的更多信息,请参阅 ScanSettingBinding 对象。
TailoredProfile 对象示例
apiVersion: compliance.openshift.io/v1alpha1 kind: TailoredProfile metadata: name: rhcos4-with-usb spec: extends: rhcos4-moderate 1 title: <title of the tailored profile> disableRules: - name: <name of a rule object to be disabled> rationale: <description of why this rule is checked> status: id: xccdf_compliance.openshift.io_profile_rhcos4-with-usb 2 outputRef: name: rhcos4-with-usb-tp 3 namespace: openshift-compliance state: READY 4
使用 TailoredProfile 对象时,可以使用 TailoredProfile 构造来创建新的 Profile 对象。要创建新 配置集,请设置以下配置参数:
- 合适的标题
-
extends值必须为空 TailoredProfile对象上的扫描类型注解:compliance.openshift.io/product-type: Platform/Node
注意如果您没有设置
product-type注解,Compliance Operator 会默认使用Platform扫描类型。在TailoredProfile对象的名称中添加-node后缀会导致node扫描类型。
5.15.3. 配置合规性扫描设置
在定义了合规性扫描的要求后,您可以通过指定扫描类型、扫描和扫描位置来配置它。要做到这一点,Compliance Operator 为您提供了 ScanSetting 对象。
5.15.3.1. ScanSetting 对象
使用 ScanSetting 对象定义并重用操作策略来运行扫描。默认情况下,Compliance Operator 会创建以下 ScanSetting 对象:
- default - 它在 master 和 worker 节点(使用 1Gi PV)上每天的 1 AM 运行一次扫描,并保留最后三条结果。补救都不会自动更新。
-
default-auto-apply - 它在 control plane 和 worker 节点(使用 1Gi PV)上每天 1AM 运行一次扫描,并保留最后三个结果。
autoApplyRemediations和autoUpdateRemediations设置为 true。
ScanSetting 对象示例
Name: default-auto-apply
Namespace: openshift-compliance
Labels: <none>
Annotations: <none>
API Version: compliance.openshift.io/v1alpha1
Auto Apply Remediations: true
Auto Update Remediations: true
Kind: ScanSetting
Metadata:
Creation Timestamp: 2022-10-18T20:21:00Z
Generation: 1
Managed Fields:
API Version: compliance.openshift.io/v1alpha1
Fields Type: FieldsV1
fieldsV1:
f:autoApplyRemediations: 1
f:autoUpdateRemediations: 2
f:rawResultStorage:
.:
f:nodeSelector:
.:
f:node-role.kubernetes.io/master:
f:pvAccessModes:
f:rotation:
f:size:
f:tolerations:
f:roles:
f:scanTolerations:
f:schedule:
f:showNotApplicable:
f:strictNodeScan:
Manager: compliance-operator
Operation: Update
Time: 2022-10-18T20:21:00Z
Resource Version: 38840
UID: 8cb0967d-05e0-4d7a-ac1c-08a7f7e89e84
Raw Result Storage:
Node Selector:
node-role.kubernetes.io/master:
Pv Access Modes:
ReadWriteOnce
Rotation: 3 3
Size: 1Gi 4
Tolerations:
Effect: NoSchedule
Key: node-role.kubernetes.io/master
Operator: Exists
Effect: NoExecute
Key: node.kubernetes.io/not-ready
Operator: Exists
Toleration Seconds: 300
Effect: NoExecute
Key: node.kubernetes.io/unreachable
Operator: Exists
Toleration Seconds: 300
Effect: NoSchedule
Key: node.kubernetes.io/memory-pressure
Operator: Exists
Roles: 5
master
worker
Scan Tolerations:
Operator: Exists
Schedule: "0 1 * * *" 6
Show Not Applicable: false
Strict Node Scan: true
Events: <none>
5.15.4. 使用合规性扫描设置处理合规性扫描要求
当您定义了合规性扫描要求并将设置配置为运行扫描时,Compliance Operator 会使用 ScanSettingBinding 对象处理它。
5.15.4.1. ScanSettingBinding 对象
使用 ScanSettingBinding 对象指定您的合规要求,并引用 Profile 或 TailoredProfile 对象。然后,它会链接到一个 ScanSetting 对象,它为扫描提供操作限制。然后,Compliance Operator 根据 ScanSetting 和 ScanSettingBinding 对象生成 ComplianceSuite 对象。
ScanSettingBinding 对象示例
apiVersion: compliance.openshift.io/v1alpha1 kind: ScanSettingBinding metadata: name: <name of the scan> profiles: 1 # Node checks - name: rhcos4-with-usb kind: TailoredProfile apiGroup: compliance.openshift.io/v1alpha1 # Cluster checks - name: ocp4-moderate kind: Profile apiGroup: compliance.openshift.io/v1alpha1 settingsRef: 2 name: my-companys-constraints kind: ScanSetting apiGroup: compliance.openshift.io/v1alpha1
创建 ScanSetting 和 ScanSettingBinding 对象会导致合规性套件。要获取合规套件列表,请运行以下命令:
$ oc get compliancesuites
如果删除了 ScanSettingBinding,则也会删除合规性套件。
5.15.5. 跟踪合规性扫描
创建合规套件后,您可以使用 ComplianceSuite 对象监控部署的扫描的状态。
5.15.5.1. ComplianceSuite 对象
ComplianceSuite 对象可帮助您跟踪扫描的状态。它包含创建扫描的原始设置以及总体结果。
对于 Node 类型扫描,您应该将扫描映射到 MachineConfigPool,因为它包含任何问题的补救。如果指定了标签,请确保它直接应用到池。
ComplianceSuite 对象示例
apiVersion: compliance.openshift.io/v1alpha1 kind: ComplianceSuite metadata: name: <name of the scan> spec: autoApplyRemediations: false 1 schedule: "0 1 * * *" 2 scans: 3 - name: workers-scan scanType: Node profile: xccdf_org.ssgproject.content_profile_moderate content: ssg-rhcos4-ds.xml contentImage: quay.io/complianceascode/ocp4:latest rule: "xccdf_org.ssgproject.content_rule_no_netrc_files" nodeSelector: node-role.kubernetes.io/worker: "" status: Phase: DONE 4 Result: NON-COMPLIANT 5 scanStatuses: - name: workers-scan phase: DONE result: NON-COMPLIANT
后台中的套件会根据 scan 参数创建 ComplianceScan 对象。您可以以编程方式获取 ComplianceSuites 事件。要获取套件的事件,请运行以下命令:
$ oc get events --field-selector involvedObject.kind=ComplianceSuite,involvedObject.name=<name of the suite>
在手动定义 ComplianceSuite 时可能会创建错误,因为它包含 XCCDF 属性。
5.15.5.2. 高级 ComplianceScan 对象
Compliance Operator 包括用于调试或与现有工具集成的高级用户的选项。虽然建议您不要直接创建一个 ComplianceScan 对象,但您可以使用 ComplianceSuite 对象来管理它。
高级 ComplianceScan 对象示例
apiVersion: compliance.openshift.io/v1alpha1 kind: ComplianceScan metadata: name: <name of the scan> spec: scanType: Node 1 profile: xccdf_org.ssgproject.content_profile_moderate 2 content: ssg-ocp4-ds.xml contentImage: quay.io/complianceascode/ocp4:latest 3 rule: "xccdf_org.ssgproject.content_rule_no_netrc_files" 4 nodeSelector: 5 node-role.kubernetes.io/worker: "" status: phase: DONE 6 result: NON-COMPLIANT 7
- 1
- 指定
Node或Platform。节点配置集扫描集群节点和平台配置集扫描 Kubernetes 平台。 - 2
- 指定您要运行的配置集的 XCCDF 标识符。
- 3
- 指定封装配置集文件的容器镜像。
- 4
- 它是可选的。指定要运行单个规则的扫描。该规则必须使用 XCCDF ID 标识,并且必须属于指定的配置集。注意
如果您跳过
rule参数,则针对指定配置集的所有可用规则运行扫描。 - 5
- 如果您在 OpenShift Container Platform 上,并希望生成补救,则 nodeSelector 标签必须与
MachineConfigPool标签匹配。注意如果没有指定
nodeSelector参数或与MachineConfig标签匹配,则扫描仍将运行,但不会创建补救。 - 6
- 指示扫描的当前阶段。
- 7
- 表示扫描的结果。
如果您删除了 ComplianceSuite 对象,则所有关联的扫描都会被删除。
扫描完成后,它将生成结果作为 ComplianceCheckResult 对象的自定义资源。但是,原始结果以 ARF 格式提供。这些结果存储在持久性卷(PV)中,它具有与扫描名称关联的持久性卷声明(PVC)。您可以以编程方式获取 ComplianceScans 事件。要为套件生成事件,请运行以下命令:
oc get events --field-selector involvedObject.kind=ComplianceScan,involvedObject.name=<name of the suite>
5.15.6. 查看合规性结果
当合规性套件达到 DONE 阶段时,您可以查看扫描结果和可能的补救方法。
5.15.6.1. ComplianceCheckResult 对象
当使用特定配置集运行扫描时,会验证配置集中的多个规则。对于每个规则,都会创建一个 ComplianceCheckResult 对象,它为特定规则提供集群状态。
ComplianceCheckResult 对象示例
apiVersion: compliance.openshift.io/v1alpha1
kind: ComplianceCheckResult
metadata:
labels:
compliance.openshift.io/check-severity: medium
compliance.openshift.io/check-status: FAIL
compliance.openshift.io/suite: example-compliancesuite
compliance.openshift.io/scan-name: workers-scan
name: workers-scan-no-direct-root-logins
namespace: openshift-compliance
ownerReferences:
- apiVersion: compliance.openshift.io/v1alpha1
blockOwnerDeletion: true
controller: true
kind: ComplianceScan
name: workers-scan
description: <description of scan check>
instructions: <manual instructions for the scan>
id: xccdf_org.ssgproject.content_rule_no_direct_root_logins
severity: medium 1
status: FAIL 2
要获得套件中的所有检查结果,请运行以下命令:
oc get compliancecheckresults \ -l compliance.openshift.io/suite=workers-compliancesuite
5.15.6.2. ComplianceRemediation 对象
对于特定的检查,您可以有一个 datastream 指定的修复。但是,如果 Kubernetes 修复可用,则 Compliance Operator 会创建一个 ComplianceRemediation 对象。
ComplianceRemediation 对象示例
apiVersion: compliance.openshift.io/v1alpha1
kind: ComplianceRemediation
metadata:
labels:
compliance.openshift.io/suite: example-compliancesuite
compliance.openshift.io/scan-name: workers-scan
machineconfiguration.openshift.io/role: worker
name: workers-scan-disable-users-coredumps
namespace: openshift-compliance
ownerReferences:
- apiVersion: compliance.openshift.io/v1alpha1
blockOwnerDeletion: true
controller: true
kind: ComplianceCheckResult
name: workers-scan-disable-users-coredumps
uid: <UID>
spec:
apply: false 1
object:
current: 2
apiVersion: machineconfiguration.openshift.io/v1
kind: MachineConfig
spec:
config:
ignition:
version: 2.2.0
storage:
files:
- contents:
source: data:,%2A%20%20%20%20%20hard%20%20%20core%20%20%20%200
filesystem: root
mode: 420
path: /etc/security/limits.d/75-disable_users_coredumps.conf
outdated: {} 3
要从套件中获得所有补救,请运行以下命令:
oc get complianceremediations \ -l compliance.openshift.io/suite=workers-compliancesuite
要列出可自动修复的所有失败检查,请运行以下命令:
oc get compliancecheckresults \ -l 'compliance.openshift.io/check-status in (FAIL),compliance.openshift.io/automated-remediation'
要列出可手动修复的所有失败检查,请运行以下命令:
oc get compliancecheckresults \ -l 'compliance.openshift.io/check-status in (FAIL),!compliance.openshift.io/automated-remediation'