$ oc new-project my-namespace
创建和管理 seccomp 配置文件并将它们绑定到工作负载。
安全配置文件操作符仅支持 Red Hat Enterprise Linux CoreOS (RHCOS) 工作节点。不支持 Red Hat Enterprise Linux (RHEL) 节点。 |
使用SeccompProfile
对象创建配置文件。
SeccompProfile
对象可以限制容器内的系统调用,从而限制应用程序的访问权限。
运行以下命令创建项目
$ oc new-project my-namespace
创建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
根路径。
创建一个 Pod 来应用已创建的配置文件之一。
创建一个定义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]
运行以下命令查看seccompProfile.localhostProfile
属性的配置文件路径
$ oc -n my-namespace get seccompprofile profile1 --output wide
NAME STATUS AGE SECCOMPPROFILE.LOCALHOSTPROFILE
profile1 Installed 14s operator/my-namespace/profile1.json
运行以下命令查看本地配置文件的路径
$ 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"
}
}
您可以使用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 (3)
1 | kind: 变量指的是配置文件的种类。 |
2 | name: 变量指的是配置文件的名称。 |
3 | 您可以使用镜像属性中的通配符启用默认安全配置文件:image: "*" |
使用 |
运行以下命令为命名空间添加enable-binding=true
标签
$ oc label ns my-namespace spo.x-k8s.io/enable-binding=true
定义名为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
创建 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 配置文件时,请验证日志丰富器功能是否已启用。有关更多信息,请参见其他资源。
具有 |
运行以下命令创建项目
$ oc new-project my-namespace
运行以下命令为命名空间添加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:
namespace: my-namespace
name: test-recording
spec:
kind: SeccompProfile
recorder: logs
podSelector:
matchLabels:
app: my-app
创建一个要记录的工作负载
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]
输入以下命令确认 Pod 处于Running
状态
$ oc -n my-namespace 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
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"
删除 Pod
$ oc -n my-namepace delete pod my-pod
确认安全配置文件操作符协调了这两个 seccomp 配置文件
$ oc get seccompprofiles -lspo.x-k8s.io/recording-id=test-recording -n my-namespace
NAME STATUS AGE
test-recording-nginx Installed 2m48s
test-recording-redis Installed 2m48s
默认情况下,每个容器实例都会记录到单独的配置文件中。安全配置文件操作符可以将每个容器的配置文件合并到单个配置文件中。当使用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
namespace: my-namespace
spec:
kind: SeccompProfile
recorder: logs
mergeStrategy: containers
podSelector:
matchLabels:
app: sp-record
运行以下命令为命名空间添加标签:
$ 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
使用以下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
要记录单个配置文件,请运行以下命令删除部署:
$ oc delete deployment nginx-deploy -n my-namespace
要合并配置文件,请运行以下命令删除配置文件记录:
$ oc delete profilerecording test-recording -n my-namespace
要启动合并操作并生成结果配置文件,请运行以下命令:
$ oc get seccompprofiles -lspo.x-k8s.io/recording-id=test-recording -n my-namespace
NAME STATUS AGE
test-recording-nginx-record Installed 55s
要查看任何容器使用的权限,请运行以下命令:
$ oc get seccompprofiles test-recording-nginx-record -o yaml