OPA란?

 - Open Policy Agent의 약어

 - OPA는 플랫폼 관리자에게 세밀한 권한 관리를 할 수 있도록 지원하는 범용 정책 엔진으로 K8S 뿐 아니라 OPA 엔진을 이용하는 모든 플랫폼에서 사용 가능

 

 

(1) OPA 동작

 - 요청이 들어오면 서비스는 JSON을 이용하여 OPA에 허용 여부 질의
 - 질의를 받은 OPA는 저장된 Policy를 불러와서 요청에 대한 평가를 하고 결과를 다시 JSON 형식으로 서비스에 반환

 

(2) 정책 설정

 - OPA는 Policy를 기반으로하여 사용자 접근을 관리하기 때문에 Policy를 어떻게 작성하는지가 중요함
 - Policy는 Rego라는 자체 질의언어를 이용하여 작성해야 하며, Rego 언어가 선언적으로 동작하기 때문에 해당 문법에 대한 이해 필요

 

 

 

Gatekeeper 란?

 - 게이트키퍼는 내부적으로 OPA 엔진을 사용하는 OPA의 K8S 승인/제어를 위해 제작된 솔루션

(1) Gatekeeper 동작

 - K8S에서는 정책적인 결정을 API 서버와 분리하여 독립적으로 할 수 있게 Admission Controller Webhook (이하 웹훅) 제공
 - 웹훅은 클러스터가 변경될 때 무조건 실행되며, 게이트키퍼는 웹훅을 확인하여 OPA 정책 엔진에서 정의한 대로 실행

 

(2) 주요 기능

 Validating Admission Control
  - 웹훅을 트릭거로 OPA와 api-server 다리 역할을 하여 정책 적용
Policies and Constraints
  - Rego 언어로 작성되며 정의한 요구하상을 위반하는 리소스들을 확인
Audit
  - 배포된 자원을 정기적으로 감사하여 Constraints에 반하는 것이 있는지 확인
Data Replication
  - 리소스를 복제 한 후 감사가 진행되며, 복제할 수 있도록 권한 부여 필요

 

 

(3) Gatekeeper Library

 - 게이트키퍼에서는 K8S에서 일반적으로 사용하는 정책에 대한 샘플 제공

탬플릿 설명
allowedrepos 허용된 이미지 Repository만 사용하도록 설정
block-endpoint-edit-default-role endpoint 편집 불가능하도록 설정(CVE-2021-25740)
block-nodeport-services NodePort 서비스 불가능하도록 설정
containerlimit 컨테이너의 리소스 제한 (CPU, Memory)
containerresourceratios 컨테이너 리소스 사용률 제한
disallowdtags 사용할 수 없는 image 태그 지정
externalip 허용 리스트에 포함되지 않은 external IP 제한
httpsonly 인그레스가 https만 사용하도록 설정
imagedigests 컨테이너 이미지에 digest를 확인 (이미지에 대한 유니크확인)
replicalimits deployment에서 pod에 대한 min/max 값 제한
requiredannotations 모든 리소스에 대해 지정된 정규표현식에 맞는 Annotation을 포함하도록
requiredlabels 모든 리소스에 대해 지정된 정규표현식에 맞는 Label을 포함하도록
requiredprobes 파드에 readiness probe, lineness probe가 있어야
uniqueingresshost 모든 인그레스의 host가 유니크해야
uniqueserviceselector 네임스페이스 내의 serviceselector 가 유니크해야

 

 

 

- Pod Security Policy의 설정을 게이트키퍼에서 제약조건 및 탬플릿을 배포하여 정책 설정 가능

Control Aspect Field Names in PSP Gatekeeper Constraint and Constraint Template
Running of privileged containers privileged privileged-containers
Usage of host namespaces hostPID, hostIPC host-namespaces
Usage of host networking and ports hostNetwork, hostPorts host-network-ports
Usage of volume types volumes volumes
Usage of the host filesystem allowedHostPaths host-filesystem
White list of Flexvolume drivers allowedFlexVolumes flexvolume-drivers
Requiring the use of a read only root file system readOnlyRootFilesystem read-only-root-filesystem
The user and group IDs of the container runAsUser, runAsGroup, supplementalGroups, fsgroup users*
Restricting escalation to root privileges allowPrivilegeEscalation, defaultAllowPrivilegeEscalation allow-privilege-escalation
Linux capabilities defaultAddCapabilities, requiredDropCapabilities, allowedCapabilities capabilities
The SELinux context of the container seLinux seLinux
The Allowed Proc Mount types for the container allowedProcMountTypes proc-mount
The AppArmor profile used by containers annotations apparmor
The seccomp profile used by containers annotations seccomp
The sysctl profile used by containers forbiddenSysctls,allowedUnsafeSysctls forbidden-sysctls

 

(4) Gatekeeper Library 샘플 구조

 - OPA 정책에 대한 탬플릿 파일과 제약사항 정의파일 및 예제파일로 구성

 

(5) Gatekeeper Library 샘플

[allowedrepos]
 - 허용된 이미지 repositor만 사용하도록 설정

 

 

 

 

참고)

https://github.com/open-policy-agent/gatekeeper

 

 

 

 

 

'DevOps' 카테고리의 다른 글

Service Mesh - Linkerd  (0) 2023.07.14
Harbor_Registry  (1) 2022.12.28
소프트웨어 스택  (0) 2022.11.08
[DevOps] ArgoCD 구축/테스트  (0) 2022.10.11

1.개요

- K8S에는 Pod Security Policy(PSP)라는 기능이 있어서 클러스터에 정책을 설정할 수 있음

- Open Policy Agent(OPA) 라는 솔루션은 K8S 뿐 아니라 범용적으로 정책을 만들어 적용할 수 있음

- OPA에서는 K8S 클러스터에 정책을 적용 할 수 있는 Gatekeeper라는 전용 솔루션을 제공

- PSP는 K8S 1.25 버전부터 지원 종료가 예정되어있으며, 그 이후부터는 OPA(Gatekeeper)를 사용해야 함 

 

 

2. Open Policy Agent

- OPA는 플랫폼 관리자에게 세밀한 권한 관리를 할 수 있도록 지원하는 범용 정책 엔진으로 K8S 뿐 아니라 OPA 엔진을 이용하는 모든 플랫폼에서 사용 가능

 

(1) OPA 동작

요청이 들어오면 서비스는 JSON을 이용하여 OPA에 허용 여부 질의

질의를 받은 OPA는 저장된 Policy를 불러와서 요청에 대한 평가를 하고 결과를 다시 JSON 형식으로 서비스에 반환

 

(2) 정책 설정

OPA는 Policy를 기반으로하여 사용자 접근을 관리하기 때문에 Policy를 어떻게 작성하는지가 중요함

PolicyRego라는 자체 질의언어를 이용하여 작성해야 하며, Rego 언어가 선언적으로 동작하기 때문에 해당 문법에 대한 이해 필요

 

 

 

3. 게이트키퍼

- 게이트키퍼는 내부적으로 OPA 엔진을 사용하는 OPA의 K8S 승인/제어를 위해 제작된 솔루션

(1) 게이트키퍼 동작

K8S에서는 정책적인 결정을 API 서버와 분리하여 독립적으로 할 수 있게 Admission Controller Webhook (이하 웹훅) 제공

• 웹훅은 클러스터가 변경될 때 무조건 실행되며, 게이트키퍼는 웹훅을 확인하여 OPA 정책 엔진에서 정의한 대로 실행

 

(2) 주요기능

Validating Admission Control

  - 웹훅을 트릭거로 OPA와 api-server 다리 역할을 하여 정책 적용

Policies and Constraints

  - Rego 언어로 작성되며 정의한 요구하상을 위반하는 리소스들을 확인

Audit

  - 배포된 자원을 정기적으로 감사하여 Constraints에 반하는 것이 있는지 확인

Data Replication

  - 리소스를 복제 한 후 감사가 진행되며, 복제할있도록 권한 부여 필요

 

 

 

4. 게이트리퍼 라이브러리

- K8S에서 일반적으로 사용하는 정책에 대한 샘플 제공

* https://github.com/open-policy-agent/gatekeeper-library

탬플릿 설명
allowedrepos 허용된 이미지 Repository만 사용하도록 설정
block-endpoint-edit-default-role endpoint 편집 불가능하도록 설정(CVE-2021-25740)
block-nodeport-services NodePort 서비스 불가능하도록 설정
containerlimit 컨테이너의 리소스 제한 (CPU, Memory)
containerresourceratios 컨테이너 리소스 사용률 제한
disallowdtags 사용할 수 없는 image 태그 지정
externalip 허용 리스트에 포함되지 않은 external IP 제한
httpsonly 인그레스가 https만 사용하도록 설정
imagedigests 컨테이너 이미지에 digest를 확인 (이미지에 대한 유니크확인)
replicalimits deployment에서 pod에 대한 min/max 값 제한
requiredannotations 모든 리소스에 대해 지정된 정규표현식에 맞는 Annotation을 포함하도록
requiredlabels 모든 리소스에 대해 지정된 정규표현식에 맞는 Label을 포함하도록
requiredprobes 파드에 readiness probe, lineness probe가 있어야
uniqueingresshost 모든 인그레스의 host가 유니크해야
uniqueserviceselector 네임스페이스 내의 serviceselector 가 유니크해야

 

 

PSP에 해당하는 기능을 게이트키퍼 탬플릿으로도 제공 함

Control Aspect Field Names in PSP Gatekeeper Constraint and Constraint Template
Running of privileged containers privileged privileged-containers
Usage of host namespaces hostPID, hostIPC host-namespaces
Usage of host networking and ports hostNetwork, hostPorts host-network-ports
Usage of volume types volumes volumes
Usage of the host filesystem allowedHostPaths host-filesystem
White list of Flexvolume drivers allowedFlexVolumes flexvolume-drivers
Requiring the use of a read only root file system readOnlyRootFilesystem read-only-root-filesystem
The user and group IDs of the container runAsUser, runAsGroup, supplementalGroups, fsgroup users*
Restricting escalation to root privileges allowPrivilegeEscalation, defaultAllowPrivilegeEscalation allow-privilege-escalation
Linux capabilities defaultAddCapabilities, requiredDropCapabilities, allowedCapabilities capabilities
The SELinux context of the container seLinux seLinux
The Allowed Proc Mount types for the container allowedProcMountTypes proc-mount
The AppArmor profile used by containers annotations apparmor
The seccomp profile used by containers annotations seccomp
The sysctl profile used by containers forbiddenSysctls,allowedUnsafeSysctls forbidden-sysctls

 

 

 

 

 

5. 게이트키퍼 라이브러리 샘플

(1) 구조

 - OPA 정책에 대한 탬플릿 파일과 제약사항 정의파일 및 예제파일로 구성

 

 

(2) 테스트

 - template.yml 적용

 - constraint.yaml에 본인 repository만 허용하도록 적용

 - 허용되지 않은 repository image를 사용해서 pod 생성

 

 

'Kubernetes' 카테고리의 다른 글

[kubespray] 배포 방화벽  (0) 2021.11.01
kubespray secret 생성, Job Deploy  (0) 2021.10.22
[custom-columns] event 정렬  (0) 2021.09.10
VM 재기동시 kube-apiserver 기동 불가현상 - Kernel  (0) 2021.07.28
ETCD 백업 & 복구  (0) 2021.07.20

+ Recent posts