7.6. seccomp プロファイルの管理
seccomp プロファイルを作成および管理し、それらをワークロードにバインドします。
Security Profiles Operator は、Red Hat Enterprise Linux CoreOS (RHCOS) ワーカーノードのみをサポートします。Red Hat Enterprise Linux (RHEL) ノードはサポートされていません。
7.6.1. seccomp プロファイルの作成
プロファイルを作成するには、SeccompProfile
オブジェクトを使用します。
SeccompProfile
オブジェクトは、コンテナー内のシステムコールを制限して、アプリケーションのアクセスを制限できます。
手順
次のコマンドを実行してプロジェクトを作成します。
Copy to clipboardCopied$ oc new-project my-namespace
SeccompProfile
オブジェクトを作成します。
Copy to clipboardCopiedapiVersion: 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.6.2. Pod への seccomp プロファイルの適用
Pod を作成して、作成したプロファイルの 1 つを適用します。
手順
securityContext
を定義する Pod オブジェクトを作成します。
Copy to clipboardCopiedapiVersion: 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
属性のプロファイルパスを表示します。
Copy to clipboardCopied$ oc -n my-namespace get seccompprofile profile1 --output wide
出力例
Copy to clipboardCopiedNAME STATUS AGE SECCOMPPROFILE.LOCALHOSTPROFILE profile1 Installed 14s operator/my-namespace/profile1.json
次のコマンドを実行して、localhost プロファイルへのパスを表示します。
Copy to clipboardCopied$ oc get sp profile1 --output=jsonpath='{.status.localhostProfile}'
出力例
Copy to clipboardCopiedoperator/my-namespace/profile1.json
localhostProfile
の出力をパッチファイルに適用します。
Copy to clipboardCopiedspec: template: spec: securityContext: seccompProfile: type: Localhost localhostProfile: operator/my-namespace/profile1.json
次のコマンドを実行して、プロファイルを他のワークロード (
Deployment
オブジェクトなど) に適用します。
Copy to clipboardCopied$ oc -n my-namespace patch deployment myapp --patch-file patch.yaml --type=merge
出力例
Copy to clipboardCopieddeployment.apps/myapp patched
検証
次のコマンドを実行して、プロファイルが正しく適用されたことを確認します。
Copy to clipboardCopied$ oc -n my-namespace get deployment myapp --output=jsonpath='{.spec.template.spec.securityContext}' | jq .
出力例
Copy to clipboardCopied{ "seccompProfile": { "localhostProfile": "operator/my-namespace/profile1.json", "type": "localhost" } }
7.6.2.1. ProfileBindings を使用してワークロードをプロファイルにバインドする
ProfileBinding
リソースを使用して、セキュリティープロファイルをコンテナーの SecurityContext
にバインドできます。
手順
quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
イメージを使用する Pod をサンプルのSeccompProfile
プロファイルにバインドするには、Pod とSeccompProfile
オブジェクトと同じ namespace にProfileBinding
オブジェクトを作成します。
Copy to clipboardCopiedapiVersion: 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
重要image: "*"
ワイルドカード属性を使用すると、すべての新しい Pod が、指定された namespace 内のデフォルトのセキュリティープロファイルにバインドされます。次のコマンドを実行して、namespace に
enable-binding=true
のラベルを付けます。
Copy to clipboardCopied$ oc label ns my-namespace spo.x-k8s.io/enable-binding=true
test-pod.yaml
という名前の Pod を定義します。
Copy to clipboardCopiedapiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: test-container image: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
Pod を作成します。
Copy to clipboardCopied$ oc create -f test-pod.yaml
注記Pod がすでに存在する場合、Pod を再作成しなければバインディングは適切に機能しません。
検証
次のコマンドを実行して、Pod が
ProfileBinding
を継承していることを確認します。
Copy to clipboardCopied$ oc get pod test-pod -o jsonpath='{.spec.containers[*].securityContext.seccompProfile}'
出力例
Copy to clipboardCopied{"localhostProfile":"operator/my-namespace/profile.json","type":"Localhost"}
7.6.3. ワークロードからのプロファイルの記録
Security Profiles Operator は、ProfileRecording
オブジェクトを使用してシステムコールを記録できるため、アプリケーションのベースラインプロファイルを簡単に作成できます。
ログエンリッチャーを使用して seccomp プロファイルを記録する場合は、ログエンリッチャー機能が有効になっていることを確認します。詳細は、関連情報 を参照してください。
privileged: true
のセキュリティーコンテキスト制限を持つコンテナーにより、ログベースの記録が防止されます。特権コンテナーは seccomp ポリシーの対象ではなく、ログベースの記録では特別な seccomp プロファイルを使用してイベントを記録します。
手順
次のコマンドを実行してプロジェクトを作成します。
Copy to clipboardCopied$ oc new-project my-namespace
次のコマンドを実行して、namespace に
enable-recording=true
のラベルを付けます。
Copy to clipboardCopied$ oc label ns my-namespace spo.x-k8s.io/enable-recording=true
recorder: logs
変数を含むProfileRecording
オブジェクトを作成します。
Copy to clipboardCopiedapiVersion: 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
記録するワークロードを作成します。
Copy to clipboardCopiedapiVersion: 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
状態であることを確認します。
Copy to clipboardCopied$ oc -n my-namespace get pods
出力例
Copy to clipboardCopiedNAME READY STATUS RESTARTS AGE my-pod 2/2 Running 0 18s
エンリッチャーがそれらのコンテナーの監査ログを受信することを示していることを確認します。
Copy to clipboardCopied$ oc -n openshift-security-profiles logs --since=1m --selector name=spod -c log-enricher
出力例
Copy to clipboardCopiedI0523 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 を削除します。
Copy to clipboardCopied$ oc -n my-namepace delete pod my-pod
Security Profiles Operator が 2 つの seccomp プロファイルを調整することを確認します。
Copy to clipboardCopied$ oc get seccompprofiles -lspo.x-k8s.io/recording-id=test-recording -n my-namespace
seccompprofile の出力例
Copy to clipboardCopiedNAME STATUS AGE test-recording-nginx Installed 2m48s test-recording-redis Installed 2m48s
7.6.3.1. コンテナーごとのプロファイルインスタンスのマージ
デフォルトでは、各コンテナーインスタンスは個別のプロファイルに記録されます。Security Profiles Operator は、コンテナーごとのプロファイルを 1 つのプロファイルにマージできます。プロファイルのマージは、ReplicaSet
または Deployment
オブジェクトを使用してアプリケーションをデプロイメントするときに役立ちます。
手順
ProfileRecording
オブジェクトを編集して、mergeStrategy: containers
変数を含めます。
Copy to clipboardCopiedapiVersion: 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
以下のコマンドを実行して namespace にラベルを付けます。
Copy to clipboardCopied$ 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 を使用してワークロードを作成します。
Copy to clipboardCopiedapiVersion: 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
個々のプロファイルを記録するには、次のコマンドを実行してデプロイメントを削除します。
Copy to clipboardCopied$ oc delete deployment nginx-deploy -n my-namespace
プロファイルをマージするには、次のコマンドを実行してプロファイルの記録を削除します。
Copy to clipboardCopied$ oc delete profilerecording test-recording -n my-namespace
マージ操作を開始して結果プロファイルを生成するには、次のコマンドを実行します。
Copy to clipboardCopied$ oc get seccompprofiles -lspo.x-k8s.io/recording-id=test-recording -n my-namespace
seccompprofiles の出力例
Copy to clipboardCopiedNAME STATUS AGE test-recording-nginx-record Installed 55s
いずれかのコンテナーで使用されている権限を表示するには、次のコマンドを実行します。
Copy to clipboardCopied$ oc get seccompprofiles test-recording-nginx-record -o yaml