×

创建和管理 seccomp 配置文件并将它们绑定到工作负载。

安全配置文件操作符仅支持 Red Hat Enterprise Linux CoreOS (RHCOS) 工作节点。不支持 Red Hat Enterprise Linux (RHEL) 节点。

创建 seccomp 配置文件

使用SeccompProfile对象创建配置文件。

SeccompProfile对象可以限制容器内的系统调用,从而限制应用程序的访问权限。

步骤
  1. 运行以下命令创建项目

    $ oc new-project my-namespace
  2. 创建SeccompProfile对象

    apiVersion: security-profiles-operator.x-k8s.io/v1beta1
    kind: SeccompProfile
    metadata:
      namespace: my-namespace
      name: profile1
    spec:
      defaultAction: SCMP_ACT_LOG

seccomp 配置文件将保存到/var/lib/kubelet/seccomp/operator/<namespace>/<name>.json

一个init容器创建安全配置文件操作符的根目录,以便在没有root组或用户ID权限的情况下运行操作符。从无根配置文件存储/var/lib/openshift-security-profiles创建一个符号链接到kubelet根目录/var/lib/kubelet/seccomp/operator内的默认seccomp根路径。

将 seccomp 配置文件应用于 Pod

创建一个 Pod 来应用已创建的配置文件之一。

步骤
  1. 创建一个定义securityContext的 Pod 对象

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      securityContext:
        runAsNonRoot: true
        seccompProfile:
          type: Localhost
          localhostProfile: operator/my-namespace/profile1.json
      containers:
        - name: test-container
          image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop: [ALL]
  2. 运行以下命令查看seccompProfile.localhostProfile属性的配置文件路径

    $ oc -n my-namespace get seccompprofile profile1 --output wide
    示例输出
    NAME       STATUS     AGE   SECCOMPPROFILE.LOCALHOSTPROFILE
    profile1   Installed  14s   operator/my-namespace/profile1.json
  3. 运行以下命令查看本地配置文件的路径

    $ oc get sp profile1 --output=jsonpath='{.status.localhostProfile}'
    示例输出
    operator/my-namespace/profile1.json
  4. localhostProfile输出应用于补丁文件

    spec:
      template:
        spec:
          securityContext:
            seccompProfile:
              type: Localhost
              localhostProfile: operator/my-namespace/profile1.json
  5. 运行以下命令将配置文件应用于任何其他工作负载,例如Deployment对象

    $ oc -n my-namespace patch deployment myapp --patch-file patch.yaml --type=merge
    示例输出
    deployment.apps/myapp patched
验证
  • 运行以下命令确认配置文件已正确应用

    $ oc -n my-namespace get deployment myapp --output=jsonpath='{.spec.template.spec.securityContext}' | jq .
    示例输出
    {
      "seccompProfile": {
        "localhostProfile": "operator/my-namespace/profile1.json",
        "type": "localhost"
      }
    }

使用 ProfileBindings 将工作负载绑定到配置文件

您可以使用ProfileBinding资源将安全配置文件绑定到容器的SecurityContext

步骤
  1. 要将使用quay.io/security-profiles-operator/test-nginx-unprivileged:1.21镜像的 Pod 绑定到示例SeccompProfile配置文件,请在与 Pod 和SeccompProfile对象相同的命名空间中创建一个ProfileBinding对象

    apiVersion: security-profiles-operator.x-k8s.io/v1alpha1
    kind: ProfileBinding
    metadata:
      namespace: my-namespace
      name: nginx-binding
    spec:
      profileRef:
        kind: SeccompProfile (1)
        name: profile (2)
      image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21 (3)
    1 kind:变量指的是配置文件的种类。
    2 name:变量指的是配置文件的名称。
    3 您可以使用镜像属性中的通配符启用默认安全配置文件:image: "*"

    使用image: "*"通配符属性会在给定命名空间中将所有新的 Pod 与默认安全配置文件绑定。

  2. 运行以下命令为命名空间添加enable-binding=true标签

    $ oc label ns my-namespace spo.x-k8s.io/enable-binding=true
  3. 定义名为test-pod.yaml的 Pod

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
      - name: test-container
        image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
  4. 创建 Pod

    $ oc create -f test-pod.yaml

    如果 Pod 已存在,则必须重新创建 Pod 才能使绑定正常工作。

验证
  • 运行以下命令确认 Pod 继承了ProfileBinding

    $ oc get pod test-pod -o jsonpath='{.spec.containers[*].securityContext.seccompProfile}'
    示例输出
    {"localhostProfile":"operator/my-namespace/profile.json","type":"Localhost"}

从工作负载记录配置文件

安全配置文件操作符可以使用ProfileRecording对象记录系统调用,从而更轻松地为应用程序创建基线配置文件。

使用日志丰富器记录 seccomp 配置文件时,请验证日志丰富器功能是否已启用。有关更多信息,请参见其他资源

具有privileged: true安全上下文限制的容器会阻止基于日志的记录。特权容器不受 seccomp 策略的约束,基于日志的记录使用特殊的 seccomp 配置文件来记录事件。

步骤
  1. 运行以下命令创建项目

    $ oc new-project my-namespace
  2. 运行以下命令为命名空间添加enable-recording=true标签

    $ oc label ns my-namespace spo.x-k8s.io/enable-recording=true
  3. 创建一个包含recorder: logs变量的ProfileRecording对象

    apiVersion: security-profiles-operator.x-k8s.io/v1alpha1
    kind: ProfileRecording
    metadata:
      namespace: my-namespace
      name: test-recording
    spec:
      kind: SeccompProfile
      recorder: logs
      podSelector:
        matchLabels:
          app: my-app
  4. 创建一个要记录的工作负载

    apiVersion: v1
    kind: Pod
    metadata:
      namespace: my-namespace
      name: my-pod
      labels:
        app: my-app
    spec:
      securityContext:
        runAsNonRoot: true
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: nginx
          image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
          ports:
            - containerPort: 8080
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop: [ALL]
        - name: redis
          image: quay.io/security-profiles-operator/redis:6.2.1
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop: [ALL]
  5. 输入以下命令确认 Pod 处于Running状态

    $ oc -n my-namespace get pods
    示例输出
    NAME     READY   STATUS    RESTARTS   AGE
    my-pod   2/2     Running   0          18s
  6. 确认丰富器表明它接收这些容器的审核日志

    $ oc -n openshift-security-profiles logs --since=1m --selector name=spod -c log-enricher
    示例输出
    I0523 14:19:08.747313  430694 enricher.go:445] log-enricher "msg"="audit" "container"="redis" "executable"="/usr/local/bin/redis-server" "namespace"="my-namespace" "node"="xiyuan-23-5g2q9-worker-eastus2-6rpgf" "pid"=656802 "pod"="my-pod" "syscallID"=0 "syscallName"="read" "timestamp"="1684851548.745:207179" "type"="seccomp"
验证
  1. 删除 Pod

    $ oc -n my-namepace delete pod my-pod
  2. 确认安全配置文件操作符协调了这两个 seccomp 配置文件

    $ oc get seccompprofiles -lspo.x-k8s.io/recording-id=test-recording -n my-namespace
    seccompprofile 的示例输出
    NAME                   STATUS      AGE
    test-recording-nginx   Installed   2m48s
    test-recording-redis   Installed   2m48s

合并每个容器的配置文件实例

默认情况下,每个容器实例都会记录到单独的配置文件中。安全配置文件操作符可以将每个容器的配置文件合并到单个配置文件中。当使用ReplicaSetDeployment对象部署应用程序时,合并配置文件非常有用。

步骤
  1. 编辑ProfileRecording对象以包含mergeStrategy: containers变量。

    apiVersion: security-profiles-operator.x-k8s.io/v1alpha1
    kind: ProfileRecording
    metadata:
      # The name of the Recording is the same as the resulting SeccompProfile CRD
      # after reconciliation.
      name: test-recording
      namespace: my-namespace
    spec:
      kind: SeccompProfile
      recorder: logs
      mergeStrategy: containers
      podSelector:
        matchLabels:
          app: sp-record
  2. 运行以下命令为命名空间添加标签:

    $ oc label ns my-namespace security.openshift.io/scc.podSecurityLabelSync=false pod-security.kubernetes.io/enforce=privileged pod-security.kubernetes.io/audit=privileged pod-security.kubernetes.io/warn=privileged --overwrite=true
  3. 使用以下YAML创建工作负载:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deploy
      namespace: my-namespace
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: sp-record
      template:
        metadata:
          labels:
            app: sp-record
        spec:
          serviceAccountName: spo-record-sa
          containers:
          - name: nginx-record
            image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
            ports:
            - containerPort: 8080
  4. 要记录单个配置文件,请运行以下命令删除部署:

    $ oc delete deployment nginx-deploy -n my-namespace
  5. 要合并配置文件,请运行以下命令删除配置文件记录:

    $ oc delete profilerecording test-recording -n my-namespace
  6. 要启动合并操作并生成结果配置文件,请运行以下命令:

    $ oc get seccompprofiles -lspo.x-k8s.io/recording-id=test-recording -n my-namespace
    seccompprofiles的示例输出:
    NAME                          STATUS       AGE
    test-recording-nginx-record   Installed    55s
  7. 要查看任何容器使用的权限,请运行以下命令:

    $ oc get seccompprofiles test-recording-nginx-record -o yaml

更多资源: