×

OpenShift Container Platform 中的合规性操作符为您提供了多个自定义资源定义 (CRD) 来完成合规性扫描。为了运行合规性扫描,它利用预定义的安全策略,这些策略源自 ComplianceAsCode 社区项目。合规性操作符将这些安全策略转换为 CRD,您可以使用这些 CRD 来运行合规性扫描并获取对发现问题的补救措施。

CRD 工作流程

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

  1. 定义您的合规性扫描要求

  2. 配置合规性扫描设置

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

  4. 监控合规性扫描

  5. 检查合规性扫描结果

定义合规性扫描要求

默认情况下,合规性操作符 CRD 包括 `ProfileBundle` 和 `Profile` 对象,您可以在其中定义和设置合规性扫描要求的规则。您还可以使用 `TailoredProfile` 对象自定义默认配置文件。

ProfileBundle 对象

安装合规性运算符时,它包含可立即运行的ProfileBundle 对象。合规性运算符会解析ProfileBundle 对象,并为捆绑包中的每个配置文件创建一个Profile 对象。它还会解析RuleVariable 对象,这些对象由Profile 对象使用。

ProfileBundle 对象示例
apiVersion: compliance.openshift.io/v1alpha1
kind: ProfileBundle
  name: <profile bundle name>
  namespace: openshift-compliance
status:
  dataStreamStatus: VALID (1)
1 指示合规性运算符是否能够解析内容文件。

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

故障排除

当您从无效镜像回滚到已知的镜像时,ProfileBundle 对象将停止响应并显示PENDING 状态。作为解决方法,您可以切换到与之前的镜像不同的镜像。或者,您可以删除并重新创建ProfileBundle 对象以恢复工作状态。

Profile 对象

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 对象定义为扫描的 profile 属性的值时,使用此标识符。
2 指定NodePlatform。节点配置文件扫描集群节点,平台配置文件扫描 Kubernetes 平台。
3 指定配置文件的规则列表。每个规则对应于单个检查。

Rule 对象

构成配置文件的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 名称,该名称直接从数据流中解析。
3 指定规则失败时的严重性。

Rule 对象获得相应的标签,以便轻松识别关联的ProfileBundle 对象。ProfileBundle 也在该对象的OwnerReferences 中指定。

TailoredProfile 对象

使用TailoredProfile 对象根据您的组织要求修改默认Profile 对象。您可以启用或禁用规则,设置变量值,并为自定义提供理由。验证后,TailoredProfile 对象将创建一个ConfigMapComplianceScan 对象可以引用该ConfigMap

您可以通过在ScanSettingBinding 对象中引用它来使用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 对象。要创建新的Profile,请设置以下配置参数

  • 合适的标题

  • extends 值必须为空

  • TailoredProfile 对象上的扫描类型注释

    compliance.openshift.io/product-type: Platform/Node

    如果您未设置product-type 注释,则合规性运算符默认为Platform 扫描类型。在TailoredProfile 对象的名称后添加-node 后缀将导致node 扫描类型。

配置合规性扫描设置

定义合规性扫描的要求后,您可以通过指定扫描的类型、扫描的发生时间和扫描的位置来配置它。为此,合规性运算符为您提供了ScanSetting 对象。

ScanSetting 对象

使用ScanSetting 对象定义和重用运行扫描的操作策略。默认情况下,合规性运算符会创建以下ScanSetting 对象

  • default - 它每天凌晨 1 点在主节点和工作节点上运行扫描,使用 1Gi 持久卷 (PV) 并保留最后三个结果。补救措施既不自动应用也不自动更新。

  • default-auto-apply - 它每天凌晨 1 点在控制平面节点和工作节点上运行扫描,使用 1Gi 持久卷 (PV) 并保留最后三个结果。autoApplyRemediationsautoUpdateRemediations 都设置为 true。

ScanSetting 对象示例
apiVersion: compliance.openshift.io/v1alpha1
autoApplyRemediations: true (1)
autoUpdateRemediations: true (2)
kind: ScanSetting
maxRetryOnTimeout: 3
metadata:
  creationTimestamp: "2022-10-18T20:21:00Z"
  generation: 1
  name: default-auto-apply
  namespace: openshift-compliance
  resourceVersion: "38840"
  uid: 8cb0967d-05e0-4d7a-ac1c-08a7f7e89e84
rawResultStorage:
  nodeSelector:
    node-role.kubernetes.io/master: ""
  pvAccessModes:
  - 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
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  - effect: NoSchedule
    key: node.kubernetes.io/memory-pressure
    operator: Exists
roles: (6)
- master
- worker
scanTolerations:
- operator: Exists
schedule: 0 1 * * * (5)
showNotApplicable: false
strictNodeScan: true
timeout: 30m
1 设置为true 以启用自动补救。设置为false 以禁用自动补救。
2 设置为true 以启用内容更新的自动补救。设置为false 以禁用内容更新的自动补救。
3 指定以原始结果格式存储的扫描数量。默认值为3。随着较旧的结果被轮换,管理员必须在轮换发生之前将结果存储在其他地方。
4 指定应为扫描创建的存储大小,以存储原始结果。默认值为1Gi
5 以 cron 格式指定应运行扫描的频率。

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

6 指定node-role.kubernetes.io 标签值以安排Node 类型的扫描。此值必须与MachineConfigPool 的名称匹配。

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

定义合规性扫描要求并配置运行扫描的设置后,合规性运算符将使用ScanSettingBinding 对象处理它。

ScanSettingBinding 对象

使用ScanSettingBinding 对象指定您的合规性要求,并参考ProfileTailoredProfile 对象。然后将其链接到ScanSetting 对象,该对象提供扫描的操作约束。然后,合规性运算符将根据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,则合规性套件也将被删除。

跟踪合规性扫描

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

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: registry.redhat.io/compliance/openshift-compliance-content-rhel8@sha256:45dc...
      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 指示套件的总体结果。

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

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

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

高级 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: registry.redhat.io/compliance/openshift-compliance-content-rhel8@sha256:45dc... (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>

查看合规性结果

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

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=workers-compliancesuite

ComplianceRemediation 对象

对于特定检查,您可以指定数据流修复。但是,如果存在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=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'