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

+ Recent posts