7.5. 管理 seccomp 配置集
创建和管理 seccomp 配置集,并将它们绑定到工作负载。
7.5.1. 创建 seccomp 配置集
使用 SeccompProfile
对象来创建配置集。
seccompProfile
对象可以限制容器内的系统调用,限制应用程序的访问。
流程
创建
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
容器创建 Security Profiles Operator 的根目录,以便在没有 root
组或用户 ID 权限的情况下运行 Operator。会创建一个符合链接,从无根配置存储 /var/lib/openshift-security-profiles
到 kubelet root /var/lib/kubelet/seccomp/operator
中的默认 seccomp
root 路径。
7.5.2. 将 seccomp 配置集应用到 pod
创建 pod 以应用其中一个创建的配置集。
流程
创建定义
securityContext
的 pod 对象:apiVersion: v1 kind: Pod metadata: name: test-pod spec: securityContext: seccompProfile: type: Localhost localhostProfile: operator/my-namespace/profile1.json containers: - name: test-container image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
运行以下命令,查看
seccompProfile.localhostProfile
属性的配置集路径:$ oc -n my-namespace get seccompprofile profile1 --output wide
输出示例
NAME STATUS AGE SECCOMPPROFILE.LOCALHOSTPROFILE profile1 Active 14s operator/my-namespace/profile1.json
运行以下命令,查看 localhost 配置集的路径:
$ oc get sp profile1 --output=jsonpath='{.status.localhostProfile}'
输出示例
operator/my-namespace/profile1.json
将
localhostProfile
输出应用到补丁文件:spec: template: spec: securityContext: seccompProfile: type: Localhost localhostProfile: operator/my-namespace/profile1.json
运行以下命令,将配置集应用到
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" } }
7.5.2.1. 使用 ProfileBindings 将工作负载绑定到配置集
您可以使用 ProfileBinding
资源将安全配置集绑定到容器的 SecurityContext
。
流程
要将使用
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
运行以下命令,使用
enable-binding=true
标记命名空间:$ oc label ns my-namespace spo.x-k8s.io/enable-binding=true
删除并重新创建 pod 以使用
ProfileBinding
对象:$ oc delete pods test-pod && oc create -f pod01.yaml
验证
运行以下命令确认 pod 会继承
ProfileBinding
:$ oc get pod test-pod -o jsonpath='{.spec.containers[*].securityContext.seccompProfile}'
输出示例
{"localhostProfile":"operator/my-namespace/profile.json","type":"Localhost"}
7.5.3. 从工作负载记录配置集
Security Profiles Operator 可以使用 ProfileRecording
对象记录系统调用,从而更轻松地为应用程序创建基准配置集。
当使用日志增强器来记录 seccomp 配置集时,请验证日志增强功能是否已启用。如需更多信息,请参阅附加资源。
具有 privileged: true
安全上下文保留的容器可防止基于日志的记录。特权容器不受到 seccomp 策略的影响,基于日志的记录利用特殊的 seccomp 配置集来记录事件。
流程
运行以下命令,使用
enable-recording=true
标记命名空间:$ oc label ns my-namespace spo.x-k8s.io/enable-recording=true
创建包含
recorder: logs
变量的ProfileRecording
对象:apiVersion: security-profiles-operator.x-k8s.io/v1alpha1 kind: ProfileRecording metadata: name: test-recording spec: kind: SeccompProfile recorder: logs podSelector: matchLabels: app: my-app
创建一个工作负载来记录:
apiVersion: v1 kind: Pod metadata: name: my-pod labels: app: my-app spec: containers: - name: nginx image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21 ports: - containerPort: 8080 - name: redis image: quay.io/security-profiles-operator/redis:6.2.1
输入以下命令确认 pod 处于
Running
状态:$ oc -n openshift-security-profiles get pods
输出示例
NAME READY STATUS RESTARTS AGE my-pod 2/2 Running 0 18s
确认增强器表示它接收这些容器的审计日志:
$ oc -n openshift-security-profiles logs --since=1m --selector name=spod -c log-enricher
输出示例
I0517 13:55:36.383187 348295 enricher.go:376] log-enricher "msg"="audit" "container"="redis" "namespace"="my-namespace" "node"="ip-10-0-189-53.us-east-2.compute.internal" "perm"="name_bind" "pod"="my-pod" "profile"="test-recording_redis_6kmrb_1684331729" "scontext"="system_u:system_r:selinuxrecording.process:s0:c4,c27" "tclass"="tcp_socket" "tcontext"="system_u:object_r:redis_port_t:s0" "timestamp"="1684331735.105:273965" "type"="seccomp"
验证
删除 pod:
$ oc -n openshift-security-profiles delete pod my-pod
确认 Security Profiles Operator 协调两个 seccomp 配置集:
$ oc get seccompprofiles -n my-namespace
输出示例
NAME USAGE STATE test-recording-nginx test-recording-nginx_my-namespace.process Installed test-recording-redis test-recording-redis_my-namespace.process Installed
7.5.3.1. 每个容器配置集实例合并
默认情况下,每个容器实例记录都记录到单独的配置文件中。Security Profiles Operator 可将每个容器配置集合并到一个配置集中。当使用 ReplicaSet
或 Deployment
对象部署应用程序时,合并配置集很有用。
流程
编辑
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 spec: kind: SeccompProfile recorder: logs mergeStrategy: containers podSelector: matchLabels: app: sp-record
创建工作负载:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deploy 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
要记录单个配置集,请运行以下命令删除部署:
$ oc delete deployment nginx-deploy
要合并配置集,请运行以下命令删除配置集记录:
$ oc delete profilerecording test-recording
要启动合并操作并生成结果配置集,请运行以下命令:
$ oc get seccompprofiles -lspo.x-k8s.io/recording-id=test-recording
输出示例
NAME USAGE STATE test-recording-nginx-record test-recording-nginx-record_mytest1.process Installed
要查看任何容器使用的权限,请运行以下命令:
$ oc get seccompprofiles test-recording-nginx-record -o yaml