5.13. 了解自定义资源定义

OpenShift Container Platform 中的 Compliance Operator 为您提供了几个自定义资源定义(CRD)来完成合规性扫描。要运行合规性扫描,它会利用预定义的安全策略,该策略从 ComplianceAsCode 社区项目衍生而来。Compliance Operator 把这些安全策略转换为 CRD,您可以使用它来运行合规性扫描,并为发现的问题获取补救。

5.13.1. CRD 工作流

CRD 为您提供了以下工作流来完成合规性扫描:

  1. 定义合规性扫描要求
  2. 配置合规性扫描设置
  3. 使用合规性扫描设置处理合规性要求
  4. 监控合规性扫描
  5. 检查合规性扫描结果

5.13.2. 定义合规性扫描要求

默认情况下,Compliance Operator CRD 包含 ProfileBundleProfile 对象,您可以在其中定义和设置合规性扫描要求的规则。您还可以使用 TailoredProfile 对象自定义默认配置集。

5.13.2.1. ProfileBundle 对象

安装 Compliance Operator 时,它包含了 ready-to-run ProfileBundle 对象。Compliance Operator 解析 ProfileBundle 对象,并为捆绑包中的每个配置集创建一个 Profile 对象。它还会解析 RuleVariable 对象,这些对象会被 Profile 对象使用。

ProfileBundle 对象示例

apiVersion: compliance.openshift.io/v1alpha1
kind: ProfileBundle
  name: <profile bundle name>
  namespace: openshift-compliance
spec:
  contentFile: ssg-ocp4-ds.xml 1
  contentImage: quay.io/complianceascode/ocp4:latest 2
status:
  dataStreamStatus: VALID 3

1
指定配置文件所在的根目录(/)的路径。
2
指定封装配置集文件的容器镜像。
3
指明 Compliance Operator 是否能够解析内容文件。
注意

contentFile 失败时,会出现一个 errorMessage 属性,它提供所发生错误的详细信息。

故障排除

当您从无效的镜像回滚到已知内容镜像时,ProfileBundle 对象停止响应并显示 PENDING 状态。作为临时解决方案,您可以移到与上一个镜像不同的镜像。另外,您可以删除并重新创建 ProfileBundle 对象以返回到工作状态。

5.13.2.2. 配置集对象

Profile 对象定义可以为某个合规性标准评估的规则和变量。它包含 OpenSCAP 配置集的解析详情,如其 XCCDF 标识符和配置集会检查 NodePlatform 类型。您可以直接使用 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>

1
指定配置集的 XCCDF 名称。当您将 ComplianceScan 对象定义为扫描的配置集属性值时,请使用这个标识符。
2
指定 NodePlatform。节点配置集扫描集群节点和平台配置集扫描 Kubernetes 平台。
3
指定配置集的规则列表。每个规则都对应一个检查。

5.13.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>

1
指定检查此规则执行的类型。Node 配置集扫描集群节点和 Platform 配置集扫描 Kubernetes 平台。空值表示没有自动检查。
2
指定规则的 XCCDF 名称,该规则直接从 datastream 解析。
3
指定规则在失败时的严重性。
注意

Rule 对象获取适当的标签,以便轻松识别关联的 ProfileBundle 对象。ProfileBundle 也在此对象的 OwnerReferences 中指定。

5.13.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

1
这是可选的。构建 TailoredProfileProfile 对象的名称。如果没有设置值,则会从 enableRules 列表创建一个新配置集。
2
指定定制配置集的 XCCDF 名称。
3
指定 ConfigMap 名称,可用作 ComplianceScantailoringConfigMap.name 属性的值。
4
显示对象的状态,如 READYPENDINGFAILURE。如果对象的状态为 ERROR,则属性 status.errorMessage 会为失败提供原因。

使用 TailoredProfile 对象时,可以使用 TailoredProfile 构造来创建新的 Profile 对象。要创建新 配置集,请设置以下配置参数:

  • 合适的标题
  • extends 值必须为空
  • TailoredProfile 对象上的扫描类型注解:

    compliance.openshift.io/product-type: <scan type>
    注意

    如果您没有设置 product-type 注解,Compliance Operator 会默认使用 Platform 扫描类型。在 TailoredProfile 对象的名称中添加 -node 后缀会导致 node 扫描类型。

5.13.3. 配置合规性扫描设置

在定义了合规性扫描的要求后,您可以通过指定扫描类型、扫描和扫描位置来配置它。要做到这一点,Compliance Operator 为您提供了 ScanSetting 对象。

5.13.3.1. ScanSetting 对象

使用 ScanSetting 对象定义并重用操作策略来运行扫描。默认情况下,Compliance Operator 会创建以下 ScanSetting 对象:

  • default - 它在 master 和 worker 节点(使用 1Gi PV)上每天的 1 AM 运行一次扫描,并保留最后三条结果。补救都不会自动更新。
  • default-auto-apply - 它在 control plane 和 worker 节点(使用 1Gi PV)上每天 1AM 运行一次扫描,并保留最后三个结果。autoApplyRemediationsautoUpdateRemediations 设置为 true。

ScanSetting 对象示例

apiVersion: compliance.openshift.io/v1alpha1
kind: ScanSetting
metadata:
  name: <name of the scan>
autoApplyRemediations: false 1
autoUpdateRemediations: false 2
schedule: "0 1 * * *" 3
rawResultStorage:
  size: "2Gi" 4
  rotation: 10 5
roles: 6
  - worker
  - master

1
设置为 true 以启用自动补救。设置为 false 可禁用自动补救。
2
设置为 true,为内容更新启用自动补救。设置为 false 以禁用内容更新自动补救。
3
指定扫描应以 cron 格式运行的频率。
4
指定应当为扫描创建存储大小以存储原始结果。默认值为 1Gi
5
指定存储原始结果的扫描量。默认值为 3。随着旧的结果轮转,管理员必须在轮转发生之前存储其他结果。
注意

要禁用轮转策略,请将值设为 0

6
指定 node-role.kubernetes.io 标签值,以调度 Node 类型的扫描。这个值必须与 MachineConfigPool 的名称匹配。

5.13.4. 使用合规性扫描设置处理合规性扫描要求

当您定义了合规性扫描要求并将设置配置为运行扫描时,Compliance Operator 会使用 ScanSettingBinding 对象处理它。

5.13.4.1. ScanSettingBinding 对象

使用 ScanSettingBinding 对象指定您的合规要求,并引用 ProfileTailoredProfile 对象。然后,它会链接到一个 ScanSetting 对象,它为扫描提供操作限制。然后,Compliance Operator 根据 ScanSettingScanSettingBinding 对象生成 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

1
指定 ProfileTailoredProfile 对象的详情,以扫描您的环境。
2
指定操作限制,如调度和存储大小。

创建 ScanSettingScanSettingBinding 对象会导致合规性套件。要获取合规套件列表,请运行以下命令:

$ oc get compliancesuites
重要

如果删除了 ScanSettingBinding,则也会删除合规性套件。

5.13.5. 跟踪合规性扫描

创建合规套件后,您可以使用 ComplianceSuite 对象监控部署的扫描的状态。

5.13.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

1
设置为 true 以启用自动补救。设置为 false 可禁用自动补救。
2
指定扫描应以 cron 格式运行的频率。
3
指定要在集群中运行的扫描规格列表。
4
表示扫描的进度。
5
表示套件的整体结果。

后台中的套件会根据 scan 参数创建 ComplianceScan 对象。您可以以编程方式获取 ComplianceSuites 事件。要获取套件的事件,请运行以下命令:

$ oc get events --field-selector involvedObject.kind=ComplianceSuite,involvedObject.name=<name of the suite>
重要

在手动定义 ComplianceSuite 时可能会创建错误,因为它包含 XCCDF 属性。

5.13.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
指定 NodePlatform。节点配置集扫描集群节点和平台配置集扫描 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.13.6. 查看合规性结果

当合规性套件达到 DONE 阶段时,您可以查看扫描结果和可能的补救方法。

5.13.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

1
描述扫描检查的严重性。
2
描述检查的结果。可能的值有:
  • PASS:检查成功。
  • FAIL:检查不成功。
  • INFO:检查成功,发现一些不严重的、不被视为错误的问题。
  • MANUAL:检查无法自动评估状态,需要手动检查。
  • INCONSISTENT:不同的节点报告不同的结果.
  • ERROR:检查运行成功,但无法完成。
  • NOTAPPLICABLE: 检查没有运行,因为它不适用。

要获得套件中的所有检查结果,请运行以下命令:

oc get compliancecheckresults -l compliance.openshift.io/suite=<suit name>

5.13.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

1
true 表示应用了补救。false 表示没有应用补救。
2
包括补救的定义。
3
表示之前从以前的内容版本解析的补救。Compliance Operator 仍然保留过时的对象,以便管理员在应用新补救前有机会查看新的补救。

要从套件中获得所有补救,请运行以下命令:

oc get complianceremediations -l compliance.openshift.io/suite=<suite name>

要列出可自动修复的所有失败检查,请运行以下命令:

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'