본문 바로가기

CKS

[CKS] Cluster Hardening - RABC, ServiceAccount

Cluster Hardening (15%)

 - Restrict access to Kubernetes API
 - Use Role Based Access Controls to minimize exposure
 - Exercise caution in using service accounts e.g. disable defaults, minimize permissions on newly created ones
 - Update Kubernetes frequently


RBAC

RBAC(Role-Based Access Control) : 쿠버네티스 클러스터 내의 자원에 대한 엑세스 권한을 부여하고 관리할 떄 사용

 

구성요소

  • Role : 특정 네임스페이스 내에서 User 또는 Service Acocunt 에게 부여되는 권한의 집합
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: mynamespace
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "create"]

 

  • ClusterRole : 클러스터 전체에서 User 또는 Service Acocunt 에게 부여되는 권한의 집합, 네임스페이스에 제한되지 않으며 클러스터 레벨의 자원 및 작업을 관리하기 위해 사용
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  # "namespace" omitted since ClusterRoles are not namespaced
  name: secret-reader
rules:
- apiGroups: [""]
  #
  # at the HTTP level, the name of the resource for accessing Secret
  # objects is "secrets"
  resources: ["secrets"]
  verbs: ["get", "watch", "list"]
  • RoleBinding : Role과 User, ServiceAccount 간 매핑을 정의. RoleBinding을 통해 특정 역할을 매핑함. RoleBinding을 통해 ClusterRole 도 사용가능 
apiVersion: rbac.authorization.k8s.io/v1
# This role binding allows "jane" to read pods in the "default" namespace.
# You need to already have a Role named "pod-reader" in that namespace.
kind: RoleBinding
metadata:
  name: read-pods
  namespace: default
subjects:
# You can specify more than one "subject"
- kind: User
  name: jane # "name" is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  # "roleRef" specifies the binding to a Role / ClusterRole
  kind: Role #this must be Role or ClusterRole
  name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
# This role binding allows "dave" to read secrets in the "development" namespace.
# You need to already have a ClusterRole named "secret-reader".
kind: RoleBinding
metadata:
  name: read-secrets
  #
  # The namespace of the RoleBinding determines where the permissions are granted.
  # This only grants permissions within the "development" namespace.
  namespace: development
subjects:
- kind: User
  name: dave # Name is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: secret-reader
  apiGroup: rbac.authorization.k8s.io
  • ClusterRoleBinding : ClusterRole을 User 또는 ServiceAccount에 매핑할 때 사용
apiVersion: rbac.authorization.k8s.io/v1
# This cluster role binding allows anyone in the "manager" group to read secrets in any namespace.
kind: ClusterRoleBinding
metadata:
  name: read-secrets-global
subjects:
- kind: Group
  name: manager # Name is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: secret-reader
  apiGroup: rbac.authorization.k8s.io

 

Command-line 예시

  • Create Role
kubectl create role pod-reader --verb=get --verb=list --verb=watch --resource=pods
  • Create ClusterRole
kubectl create clusterrole pod-reader --verb=get,list,watch --resource=pods
  • Create RoleBinding
kubectl create rolebinding bob-admin-binding --clusterrole=admin --user=bob --namespace=acme
  • Create ClusterRoleBinding
kubectl create clusterrolebinding root-cluster-admin-binding --clusterrole=cluster-admin --user=root

 

RBAC 권한 테스트 방법

kubectl can-i get ns -n test --as=system:serviceaccount:user02:web-sa

 

주의사항

  • RoleBinding / ClusterRoleBinding 이후 Role과 ClusterRole 변경 불가.
  • 변경이 필요하다면 Binding Object 삭제 후 재생성 해야함.

 

 


Service Account

Service Account : 클러스터 내의 애플리케이션 또는 프로세스가 클러스터 자원에 접근할 수 있도록 하는 일종의 식별자

  • Pod는 클러스터 자원에 액세스하기 위해 Service Account를 사용
  • Default Service Account : 모든 네임스페이스 생성되는 default SA.
    • Pod 생성시 SA를 명시적으로 지정하지 않으면, 해당 네임스페이스의 default SA가 자동 할당됨 
  • SA 생성 : 사용자는 필요에 따라 SA 를 생성하여 세부적인 권한 관리를 할 수 있음 
  • SA 권한부여 : RBAC을 활용함 
  • Security를 적용한 Service ACcount 생성방법
--- SA에 직접 설정
apiVersion: v1
kind: ServiceAccount
metadata:
  name: build-robot
automountServiceAccountToken: false // ***
...
--- 특정 pod에  설정 
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  serviceAccountName: build-robot
  automountServiceAccountToken: false  // ***
  ...

 


예제문제

  • RBAC이 적용된 NS의 secret 값을 확인해보세요! (pod 목록과, pod access만 가능한 상황)
    • (Case1) secret이 pod 에 할당되었을 경우
    • (Case2) secret이 pod에 할당되지 않았을 경우
더보기

# (Case1) container 에서 확인

# 다음 명령어로 secret 할당 경로 확인 

k get pod -n rbac -o yaml | grep -i secret     # /etc/secret-volume/pw  확인 가능

 

# exec 명령어로 컨테이너 내부에서 secret 값 확인

k exec -it <pod-name> -n rbac -- cat /etc/secret-volume/pw

더보기

# (Case2) SA에 할당된 token을 통해 secret 값 확인

# pod에 할당된 sa정보 확인

k get pod -n rbac -o yaml | grep ServiceAccount 

# container 진입

k exec -it <pod-name> -n rbac -- sh

# mount 명령어로 serviceaccount 확인

mount | grep serviceaccount

# serviceaccount 경로 확인

ls <serviceaccount 경로> 

# apiserver 정보를 manual 하게 확인

curl https://kubernetes.default/api/v1/namespaces/<ns-name>/secrets -H


참고페이지

https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#before-you-begin

 

Configure Service Accounts for Pods

Kubernetes offers two distinct ways for clients that run within your cluster, or that otherwise have a relationship to your cluster's control plane to authenticate to the API server. A service account provides an identity for processes that run in a Pod, a

kubernetes.io

https://kubernetes.io/docs/reference/access-authn-authz/rbac/

 

Using RBAC Authorization

Role-based access control (RBAC) is a method of regulating access to computer or network resources based on the roles of individual users within your organization. RBAC authorization uses the rbac.authorization.k8s.io API group to drive authorization decis

kubernetes.io

https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/

 

Managing Service Accounts

A ServiceAccount provides an identity for processes that run in a Pod. A process inside a Pod can use the identity of its associated service account to authenticate to the cluster's API server. For an introduction to service accounts, read configure servic

kubernetes.io

https://kubernetes.io/docs/tasks/run-application/access-api-from-pod/

 

Accessing the Kubernetes API from a Pod

This guide demonstrates how to access the Kubernetes API from within a pod. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutoria

kubernetes.io