搜索

7.7. 高级安全配置集 Operator 任务

download PDF

使用高级任务来启用指标、配置 Webhook 或限制系统调用。

7.7.1. 限制 seccomp 配置集中允许的 syscalls

默认情况下,Security Profiles Operator 不限制 seccomp 配置集中的 syscalls。您可以在 spod 配置中定义允许的 syscalls 列表。

流程

  • 要定义 allowedSyscalls 列表,请运行以下命令来调整 spec 参数:

    $ oc -n openshift-security-profiles patch spod spod --type merge \
        -p '{"spec":{"allowedSyscalls": ["exit", "exit_group", "futex", "nanosleep"]}}'
重要

Operator 将仅安装 seccomp 配置集,该配置集在允许列表中定义了 syscalls 的子集。所有不符合这个规则集的配置集都会被拒绝。

当在 spod 配置中修改了允许的 syscalls 列表时,Operator 将识别已安装的不合规的配置集,并自动删除它们。

7.7.2. 容器运行时的基本系统调用

您可以使用 baseProfileName 属性为给定运行时指定最少需要 syscalls 来启动一个容器。

流程

  • 编辑 SeccompProfile kind 对象,并将 baseProfileName: runc-v1.0.0 添加到 spec 字段中:

    apiVersion: security-profiles-operator.x-k8s.io/v1beta1
    kind: SeccompProfile
    metadata:
      namespace: my-namespace
      name: example-name
    spec:
      defaultAction: SCMP_ACT_ERRNO
      baseProfileName: runc-v1.0.0
      syscalls:
        - action: SCMP_ACT_ALLOW
          names:
            - exit_group

7.7.3. 在 spod 守护进程中启用内存优化

spod 守护进程内运行的控制器会在启用配置集记录时监视集群中的所有 pod。这会导致大型集群中的内存用量很高,从而导致 spod 守护进程内存不足或崩溃。

为防止崩溃,spod 守护进程只能配置为仅加载标记为配置集记录的 pod。

注意

默认情况下不启用 SPO 内存优化。

流程

  1. 运行以下命令来启用内存优化:

    $ oc -n openshift-security-profiles patch spod spod --type=merge -p '{"spec":{"enableMemoryOptimization":true}}'
  2. 要记录 pod 的安全配置集,pod 必须使用 spo.x-k8s.io/enable-recording: "true" 标记:

    apiVersion: v1
    kind: Pod
    metadata:
      name: my-recording-pod
      labels:
        spo.x-k8s.io/enable-recording: "true"
    # ...

7.7.4. 自定义守护进程资源要求

可以使用 spod 配置中的字段 daemonResourceRequirements 来调整守护进程容器的默认资源要求。

流程

  • 要指定守护进程容器的内存和 cpu 请求和限值,请运行以下命令:

    $ oc -n openshift-security-profiles patch spod spod --type merge -p \
        '{"spec":{"daemonResourceRequirements": { \
        "requests": {"memory": "256Mi", "cpu": "250m"}, \
        "limits": {"memory": "512Mi", "cpu": "500m"}}}}'

7.7.5. 为 spod 守护进程 pod 设置自定义优先级类名称

spod 守护进程 pod 的默认优先级类名称设置为 system-node-critical。通过在 priorityClassName 字段中设置值,可以在 spod 配置中配置自定义优先级类名称。

流程

  • 运行以下命令来配置优先级类名称:

    $ oc -n openshift-security-profiles patch spod spod --type=merge -p '{"spec":{"priorityClassName":"my-priority-class"}}'

    输出示例

    securityprofilesoperatordaemon.openshift-security-profiles.x-k8s.io/spod patched

7.7.6. 使用指标

openshift-security-profiles 命名空间提供指标端点,这些端点由 kube-rbac-proxy 容器进行保护。所有指标都由 openshift-security-profiles 命名空间中的 metrics 服务公开。

Security Profiles Operator 包含一个集群角色和对应的绑定 spo-metrics-client,用于从集群中检索指标。有两个指标路径可用:

  • metrics.openshift-security-profiles/metrics: 用于控制器运行时指标
  • metrics.openshift-security-profiles/metrics-spod: 用于 Operator 守护进程指标

流程

  1. 要查看 metrics 服务的状态,请运行以下命令:

    $ oc get svc/metrics -n openshift-security-profiles

    输出示例

    NAME      TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    metrics   ClusterIP   10.0.0.228   <none>        443/TCP   43s

  2. 要检索指标,请运行以下命令使用 openshift-security-profiles 命名空间中的默认 ServiceAccount 令牌查询服务端点:

    $ oc run --rm -i --restart=Never --image=registry.fedoraproject.org/fedora-minimal:latest \
        -n openshift-security-profiles metrics-test -- bash -c \
        'curl -ks -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" https://metrics.openshift-security-profiles/metrics-spod'

    输出示例

    # HELP security_profiles_operator_seccomp_profile_total Counter about seccomp profile operations.
    # TYPE security_profiles_operator_seccomp_profile_total counter
    security_profiles_operator_seccomp_profile_total{operation="delete"} 1
    security_profiles_operator_seccomp_profile_total{operation="update"} 2

  3. 要从不同的命名空间中检索指标,请运行以下命令将 ServiceAccount 链接到 spo-metrics-client ClusterRoleBinding

    $ oc get clusterrolebinding spo-metrics-client -o wide

    输出示例

    NAME                 ROLE                             AGE   USERS   GROUPS   SERVICEACCOUNTS
    spo-metrics-client   ClusterRole/spo-metrics-client   35m                    openshift-security-profiles/default

7.7.6.1. controller-runtime 指标

controller-runtime metrics 和 DaemonSet 端点 metrics-spod 提供了一组默认的指标。守护进程提供了其他指标,该指标始终以 security_profiles_operator_ 前缀。

表 7.1. 可用的 controller-runtime 指标
指标键可能的标签类型用途

seccomp_profile_total

operation={delete,update}

计数

seccomp 配置集操作的数量。

seccomp_profile_audit_total

node, namespace, pod, container, executable, syscall

计数

seccomp 配置集审计操作的数量。需要启用日志增强。

seccomp_profile_bpf_total

node, mount_namespace, profile

计数

seccomp 配置集 bpf 操作的数量。需要启用 bpf 记录器。

seccomp_profile_error_total

reason={
SeccompNotSupportedOnNode,
InvalidSeccompProfile,
CannotSaveSeccompProfile,
CannotRemoveSeccompProfile,
CannotUpdateSeccompProfile,
CannotUpdateNodeStatus
}

计数

seccomp 配置集数量。

selinux_profile_total

operation={delete,update}

计数

SELinux 配置集操作的数量。

selinux_profile_audit_total

node, namespace, pod, container, executable, scontext,tcontext

计数

SELinux 配置集审核操作的数量。需要启用日志增强。

selinux_profile_error_total

reason={
CannotSaveSelinuxPolicy,
CannotUpdatePolicyStatus,
CannotRemoveSelinuxPolicy,
CannotContactSelinuxd,
CannotWritePolicyFile,
CannotGetPolicyStatus
}

计数

SELinux 配置集的数量错误。

7.7.7. 使用日志增强

Security Profiles Operator 包含一个日志功能,它默认是禁用的。日志增强容器以 privileged 权限运行,以便从本地节点读取审计日志。日志增强在主机 PID 命名空间 hostPID 中运行。

重要

日志增强必须具有读取主机进程的权限。

流程

  1. 运行以下命令修补 spod 配置以启用日志增强:

    $ oc -n openshift-security-profiles patch spod spod \
        --type=merge -p '{"spec":{"enableLogEnricher":true}}'

    输出示例

    securityprofilesoperatordaemon.security-profiles-operator.x-k8s.io/spod patched

    注意

    Security Profiles Operator 将自动重新部署 spod 守护进程集。

  2. 运行以下命令来查看审计日志:

    $ oc -n openshift-security-profiles logs -f ds/spod log-enricher

    输出示例

    I0623 12:51:04.257814 1854764 deleg.go:130] setup "msg"="starting component: log-enricher"  "buildDate"="1980-01-01T00:00:00Z" "compiler"="gc" "gitCommit"="unknown" "gitTreeState"="clean" "goVersion"="go1.16.2" "platform"="linux/amd64" "version"="0.4.0-dev"
    I0623 12:51:04.257890 1854764 enricher.go:44] log-enricher "msg"="Starting log-enricher on node: 127.0.0.1"
    I0623 12:51:04.257898 1854764 enricher.go:46] log-enricher "msg"="Connecting to local GRPC server"
    I0623 12:51:04.258061 1854764 enricher.go:69] log-enricher "msg"="Reading from file /var/log/audit/audit.log"
    2021/06/23 12:51:04 Seeked /var/log/audit/audit.log - &{Offset:0 Whence:2}

7.7.7.1. 使用日志功能跟踪应用程序

您可以使用 Security Profiles Operator 日志增强来跟踪应用程序。

流程

  1. 要跟踪应用程序,请创建一个 SeccompProfile 日志记录配置集:

    apiVersion: security-profiles-operator.x-k8s.io/v1beta1
    kind: SeccompProfile
    metadata:
      name: log
      namespace: default
    spec:
      defaultAction: SCMP_ACT_LOG
  2. 创建 pod 对象以使用配置集:

    apiVersion: v1
    kind: Pod
    metadata:
      name: log-pod
    spec:
      securityContext:
        runAsNonRoot: true
        seccompProfile:
          type: Localhost
          localhostProfile: operator/default/log.json
      containers:
      - name: log-container
        image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop: [ALL]
  3. 运行以下命令检查日志增强的输出:

    $ oc -n openshift-security-profiles logs -f ds/spod log-enricher

    例 7.1. 输出示例

    …
    I0623 12:59:11.479869 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=3 "syscallName"="close" "timestamp"="1624453150.205:1061" "type"="seccomp"
    I0623 12:59:11.487323 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=157 "syscallName"="prctl" "timestamp"="1624453150.205:1062" "type"="seccomp"
    I0623 12:59:11.492157 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=157 "syscallName"="prctl" "timestamp"="1624453150.205:1063" "type"="seccomp"
    …
    I0623 12:59:20.258523 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=12 "syscallName"="brk" "timestamp"="1624453150.235:2873" "type"="seccomp"
    I0623 12:59:20.263349 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=21 "syscallName"="access" "timestamp"="1624453150.235:2874" "type"="seccomp"
    I0623 12:59:20.354091 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=257 "syscallName"="openat" "timestamp"="1624453150.235:2875" "type"="seccomp"
    I0623 12:59:20.358844 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=5 "syscallName"="fstat" "timestamp"="1624453150.235:2876" "type"="seccomp"
    I0623 12:59:20.363510 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=9 "syscallName"="mmap" "timestamp"="1624453150.235:2877" "type"="seccomp"
    I0623 12:59:20.454127 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=3 "syscallName"="close" "timestamp"="1624453150.235:2878" "type"="seccomp"
    I0623 12:59:20.458654 1854764 enricher.go:111] log-enricher "msg"="audit"  "container"="log-container" "executable"="/usr/sbin/nginx" "namespace"="default" "node"="127.0.0.1" "pid"=1905792 "pod"="log-pod" "syscallID"=257 "syscallName"="openat" "timestamp"="1624453150.235:2879" "type"="seccomp"
    …

7.7.8. 配置 Webhook

配置集绑定和配置集记录对象可以使用 Webhook。配置集绑定和记录对象配置是 MutatingWebhookConfiguration CR,由 Security Profiles Operator 管理。

要更改 webhook 配置,spod CR 会公开一个 webhookOptions 字段,允许修改 failurePolicynamespaceSelectorobjectSelector 变量。这可让您将 webhook 设置为 "soft-fail",或将它们限制为命名空间的子集,以便即使 Webhook 失败,其他命名空间或资源不受影响。

流程

  1. 通过创建以下补丁文件,将 record.spo.io Webhook 配置设置为仅记录带有 spo-record=true 标签的 pod:

    spec:
      webhookOptions:
        - name: recording.spo.io
          objectSelector:
            matchExpressions:
              - key: spo-record
                operator: In
                values:
                  - "true"
  2. 运行以下命令来修补 spod/spod 实例:

    $ oc -n openshift-security-profiles patch spod \
        spod -p $(cat /tmp/spod-wh.patch) --type=merge
  3. 要查看生成的 MutatingWebhookConfiguration 对象,请运行以下命令:

    $ oc get MutatingWebhookConfiguration \
        spo-mutating-webhook-configuration -oyaml
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.