티스토리 뷰

Security/Cloud Native

Gatekeeper mutatation

Jacob_baek 2023. 5. 4. 18:29

Gatekeeper를 이용해 validation 을 통한 deny/audit 등을 수행할수도 있지만 muatation을 통한 특정조건을 강제화시킬수도 있다.
mutation은 gatekeeper 3.10 이상부터 v1으로 제공되며 다음과 같은 4가지 방식의 CRD가 제공되어 이를 통한 강제화를 수행할수 있다.

  1. assign
  2. assignmetadata
  3. assignimage
  4. ModifySet

How it works

아래 pod를 기반으로 위 4가지 CRD를 확인해보자.
동일한 내용을 가진 container를 name을 다르게 하여 비교하여 확인해볼 예정이다.

jacob@laptop:~ $ cat testpod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: testpod
  namespace: testns
spec:
  containers:
  - name: busybox
    image: busybox
    command: ['sh', '-c', 'sleep 999']
    imagePullPolicy: IfNotPresent
    securityContext:
      privileged: true
      allowPrivilegeEscalation: true

jacob@laptop:~ $ cat testpod2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: testpod2
  namespace: testns
spec:
  containers:
  - name: busybox
    image: busybox
    command: ['sh', '-c', 'sleep 999']
    imagePullPolicy: IfNotPresent
    securityContext:
      privileged: true

1. assign

가장 많은 기능을 제공하는 CRD로 metadata 영역 외에 다양한 영역을 커버한다.
많이 알려진 privilege 를 제어하는 방식을 아래와 같이 지정하여 assign crd를 생성한다.

apiVersion: mutations.gatekeeper.sh/v1
kind: Assign
metadata:
  name: demo-privileged
spec:
  applyTo:
  - groups: [""]
    kinds: ["Pod"]
    versions: ["v1"]
  match:
    scope: Namespaced
    kinds:
    - apiGroups: ["*"]
      kinds: ["Pod"]
    namespaces: ["testns"]
    name : "testpod"
  location: "spec.containers[name:*].securityContext.privileged"
  parameters:
    assign:
      value: false

이후 testpod/testpod2 pod를 생성하게 되면 다음과 같은 결과를 얻게 된다.

jacob@laptop:~ $ kubectl get pod testpod -n testns -o jsonpath='{.spec.containers[].securityContext}'
{"privileged":false}
jacob@laptop:~ $ kubectl get pod testpod2 -n testns -o jsonpath='{.spec.containers[].securityContext}'
{"privileged":true}

matching되는 resource의 경우 privileged가 false로 변경되는것을 확인할 수 있다.

assign은 metadata외 다양한 항목을 커버하고 있기에 사용법이 다양할수 있다.
아래예제에서 다양한 방법을 소개하고 있으니 참고하길 바란다.

2. assignmetadata

metadata (ex. labels, annotations)에 대한 강제화를 해보자.

apiVersion: mutations.gatekeeper.sh/v1
kind: AssignMetadata
metadata:
  name: addannotationowner
spec:
  match:
    scope: Namespaced
    name: "testpod"
    namespaces: ["testns"] 
  location: "metadata.annotations.user"
  parameters:
    assign:
      value: "testuser"

실제 결과를 확인해보면 아래와 같이 지정된 이름(testpod)을 가진 testns에 존재하는 모든 resource는 annotation을 가지게 된다.

jacob@laptop:~ $ kubectl get pod testpod -n testns -o jsonpath='{.metadata.annotations.user}'
testuser
jacob@laptop:~ $ kubectl get pod testpod2 -n testns -o jsonpath='{.metadata.annotations.user}'
jacob@laptop:~ $

3. assignimage

배포되는 이미지의 tag 를 변조할수 있다.
아래와 같은 assignimage CRD를 생성하여 busybox image 버전을 강제화 해보자.

apiVersion: mutations.gatekeeper.sh/v1alpha1
kind: AssignImage
metadata:
  name: changebusyboxversion
spec:
  applyTo:
  - groups: [ "" ]
    kinds: [ "Pod" ]
    versions: [ "v1" ]
  location: "spec.containers[name:busybox].image"
  parameters:
    assignTag: ":1.35"
  match:
    source: "All"
    scope: Namespaced
    kinds:
    - apiGroups: [ "*" ]
      kinds: [ "Pod" ]
    name: "testpod"

이후 testpod를 생성하게 되면 다음과 같이 image가 지정된 버전(1.35)로 지정되어 배포되게 된다.

jacob@laptop:~ $ kubectl get pod testpod -n testns -o jsonpath='{.spec.containers[].image}'
busybox:1.35
jacob@laptop:~ $ kubectl get pod testpod2 -n testns -o jsonpath='{.spec.containers[].image}'
busybox

4. ModifySet

리스트 기반에 아이템들을 조건에 맞는 경우 prune/merge(default) 하도록 한다.

apiVersion: mutations.gatekeeper.sh/v1
kind: ModifySet
metadata:
  name: mergedebug
spec:
  applyTo:
  - groups: [""]
    kinds: ["Pod"]
    versions: ["v1"]
  location: "spec.containers[name: *].command"
  parameters:
    operation: merge
    values:
      fromList:
        - --debug
        - -v

위와 같이 modifyset이 배포된 상황에서 testpod를 배포하게 되면 아래와 같이 list형태로 정의되어있던 value들이 추가되어져 동작되는것을 확인할 수 있다.

jacob@laptop:~ $ kubectl get po testpod -n testns -o jsonpath='{.spec.containers[].command}'
["sh","-c","sleep 999","--debug","-v"]
jacob@laptop:~ $ kubectl get po testpod2 -n testns -o jsonpath='{.spec.containers[].command}'
["sh","-c","sleep 999"]

logging and monitoring

참고사이트

'Security > Cloud Native' 카테고리의 다른 글

Gatekeeper Library  (0) 2023.06.04
Rego test and http.send  (0) 2023.05.03
trivy db search  (0) 2023.04.02
Hashicorp Boundary  (0) 2020.10.28
How to use Hashicorp boundary  (0) 2020.10.24
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함