본문 바로가기

CKS

[CKS] Monitoring, Logging and Runtime Security - Audit Log

Monitoring, Logging and Runtime Security (20%)

 - Perform behavioral analytics of syscall process and file activities at the host and container level to detect malicious activities
 - Detect threats within physical infrastructure, apps, networks, data, users and workloads
 - Detect all phases of attack regardless where it occurs and how it spreads
 - Perform deep analytical investigation and identification of bad actors within environment
 - Ensure immutability of containers at runtime
 - Use Audit Logs to monitor access


 

Audit in Kubernetes

 

 

 

K8s의 Audit(감사) 기능은 클러스터에서 발생하는 각종 이벤트에 대한 로깅을 수행하고, 클러스터 활동에 대한 추적과 감사 정보를 생성하며, K8s API 서버에서 발생하는 이벤트를 모니터링하여 이를 로그로 남기는 방식으로 동작한다.

 

주요 구성 요소

  1. Audit Policy:
    • Kubernetes 감사는 감사 정책을 통해 어떤 이벤트를 기록할지 결정합니다. 감사 정책은 YAML 형식으로 작성되며, 어떤 동작에 대한 감사를 활성화할지, 어떤 수준의 세부 정보를 기록할지 등을 정의합니다.
  2. Audit Level:
    • Kubernetes에서는 다양한 감사 수준을 제공합니다. 주요 감사 수준에는 None, Metadata, Request, RequestResponse 등이 있습니다. 높은 수준의 감사는 더 많은 정보를 기록하게 됩니다.
  3. Audit Backends:
    • 감사 로그는 여러 백엔드에 저장될 수 있습니다. 주로 파일 시스템, 이벤트 버스, 외부 감사 로깅 서비스 등이 사용됩니다. 클러스터 관리자는 이러한 백엔드 중 하나를 선택하여 감사 로그를 저장할 수 있습니다.
  4. Kube-apiserver Configuration:
    • 감사 로그는 주로 kube-apiserver의 구성에서 설정됩니다. 감사 정책과 감사 로그의 경로, 형식 등이 kube-apiserver의 구성 파일에 정의됩니다.
  5. Audit Log Format:
    • 감사 로그는 주로 JSON 형식으로 제공됩니다. 각 로그 항목에는 API 요청에 대한 세부 정보가 포함되며, 사용자, 작업 시간, 동작 등이 로그에 기록됩니다.
  6. Audit Log Path:
    • 감사 로그는 특정 경로에 저장됩니다. 이 경로는 kube-apiserver의 구성 파일에서 설정되며, 클러스터 관리자는 로그의 저장 위치를 지정할 수 있습니다.
  7. Log Rotation:
    • Kubernetes는 감사 로그의 로테이션을 지원하여 로그 파일이 과도하게 커지는 것을 방지합니다. 로그 로테이션 정책은 구성 파일에서 설정할 수 있습니다.

 

Setting up Audit Logging

Audit policy configuration file 생성

#audit-policy.yaml

apiVersion: audit.k8s.io/v1
kind: Policy
rules:
# Log changes to Namespaces at the RequestResponse level.
- level: RequestResponse
resources:
- group: ""
resources: ["namespaces"]
# Log pod changes in the audit-test Namespace at Request level
- level: Request
resources:
- group: ""
resources: ["pods"]
namespaces: ["audit-test"]
# Log all ConfigMap and Secret changes at the Metadata level.
- level: Metadata
resources:
- group: ""
resources: ["secrets", "configmaps"]
# Catch-all - Log all requests at the metadata level.
- level: Metadata

 

Configure Audit Logging 설정 

kube-apiserver.yaml 파일에 flag를 통해 설정하며 주요 flag는 다음과 같다.

  • --audit-policy-file : audit policy config file의 위치 지정
  • --audit-log-path   : audit log파일들이 저장될 위치 지정
  • --audit-log-maxage  : old log file들을 저장하는 기간
  • --audit-log-maxbackup : old log file 갯수 지정
# kube-apiserver.yaml
apiVersion: v1
kind: Pod
metadata:
  name: kube-apiserver
  namespace: kube-system
spec:
  containers:
  - command:
    - kube-apiserver
    - ...
    - --audit-policy-file=/path/to/audit-policy.yaml
    - --audit-log-path=/var/log/kubernetes/audit.log
    - --audit-log-maxage=30
    - --audit-log-maxbackup=3
    - --audit-log-maxsize=100
    ...
    volumeMounts:
    - mountPath: /path/to/audit-policy.yaml
      name: audit-policy
      readOnly: true
    - mountPath: /var/log/kubernetes
      name: var-log-kubernetes
      readOnly: false
  volumes:
  - hostPath:
      path: /etc/kubernetes/audit-policy.yaml
    name: audit-policy
  - hostPath:
      path: /var/log/kubernetes
    name: var-log-kubernetes