7.5. seccomp 프로필 관리

seccomp 프로필을 생성 및 관리하고 이를 워크로드에 바인딩합니다.

중요

Security Profiles Operator는 RHCOS(Red Hat Enterprise Linux CoreOS) 작업자 노드만 지원합니다. RHEL(Red Hat Enterprise Linux) 노드는 지원되지 않습니다.

7.5.1. seccomp 프로필 생성

SeccompProfile 오브젝트를 사용하여 프로필을 생성합니다.

SeccompProfile 오브젝트는 컨테이너 내에서 syscall을 제한하여 애플리케이션 액세스를 제한할 수 있습니다.

절차

  • 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를 실행합니다. 심볼릭 링크는 kubelet 루트 루트 루트 루트 경로 /var/lib/openshift-security-profiles 에서 kubelet 루트 경로 /var/lib/kubelet/seccomp/operator 로 생성됩니다.

7.5.2. Pod에 seccomp 프로필 적용

생성된 프로필 중 하나를 적용하려면 Pod를 생성합니다.

절차

  1. 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
  2. 다음 명령을 실행하여 seccompProfile.localhostProfile 속성의 프로필 경로를 확인합니다.

    $ oc -n my-namespace get seccompprofile profile1 --output wide

    출력 예

    NAME       STATUS   AGE   SECCOMPPROFILE.LOCALHOSTPROFILE
    profile1   Active   14s   operator/my-namespace/profile1.json

  3. 다음 명령을 실행하여 localhost 프로필의 경로를 확인합니다.

    $ 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"
      }
    }

7.5.2.1. ProfileBindings를 사용하여 워크로드에 프로필 바인딩

ProfileBinding 리소스를 사용하여 보안 프로필을 컨테이너의 SecurityContext 에 바인딩할 수 있습니다.

절차

  1. quay.io/security-profiles-operator/test-nginx-unprivileged:1.21 이미지를 예제 SeccompProfile 프로필에 사용하는 Pod를 바인딩하려면 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
    1
    kind: 변수는 프로필 이름을 나타냅니다.
    2
    name: 변수는 프로필 이름을 나타냅니다.
  2. 다음 명령을 실행하여 enable-binding=true 로 네임스페이스에 레이블을 지정합니다.

    $ oc label ns my-namespace spo.x-k8s.io/enable-binding=true
  3. ProfileBinding 오브젝트를 사용하도록 Pod를 삭제하고 다시 생성합니다.

    $ 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 security context restraints가 있는 컨테이너는 로그 기반 레코딩을 방지합니다. 권한 있는 컨테이너에는 seccomp 정책이 적용되지 않으며 로그 기반 기록은 이벤트를 기록하기 위해 특수 seccomp 프로필을 사용합니다.

절차

  1. 다음 명령을 실행하여 enable-recording=true 로 네임스페이스에 레이블을 지정합니다.

    $ oc label ns my-namespace spo.x-k8s.io/enable-recording=true
  2. 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
  3. 기록할 워크로드를 생성합니다.

    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
  4. 다음 명령을 입력하여 Pod가 Running 상태인지 확인합니다.

    $ oc -n openshift-security-profiles get pods

    출력 예

    NAME     READY   STATUS    RESTARTS   AGE
    my-pod   2/2     Running   0          18s

  5. 보강기에서 해당 컨테이너에 대한 감사 로그를 수신함을 나타냅니다.

    $ 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"

검증

  1. Pod를 제거합니다.

    $ oc -n openshift-security-profiles delete pod my-pod
  2. 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 오브젝트를 사용하여 애플리케이션을 배포할 때 유용합니다.

절차

  1. mergeStrategy: containers 변수를 포함하도록 ProfileRecording 오브젝트를 편집합니다.

    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
  2. 워크로드를 생성합니다.

    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
  3. 개별 프로필을 기록하려면 다음 명령을 실행하여 배포를 삭제합니다.

    $ oc delete deployment nginx-deploy
  4. 프로필을 병합하려면 다음 명령을 실행하여 프로파일 레코딩을 삭제합니다.

    $ oc delete profilerecording test-recording
  5. 병합 작업을 시작하고 결과 프로필을 생성하려면 다음 명령을 실행합니다.

    $ 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

  6. 컨테이너에서 사용하는 권한을 보려면 다음 명령을 실행합니다.

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

추가 리소스