iam RBAC 실습

xlwdn·2023년 9월 2일
0

iam RBAC 실습

eks에선 iam을 RBAC 구성에 사용할 수 있다.

$ aws eks update-kubeconfig --name <cluster-name>

위 명령어 입력 시 현재 aws sts get-caller-identity 를 통해 확인되는 iam으로 ~/.kube/config에 파일이 생성된다.

때문에 AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY 환경변수 설정 시 해당 iam으로 구성되고 또한 update-kubeconfig 뒤에 —profile을 붙여 이미 configure 된 iam을 사용할 수도 있다.

다만, 현재는 새로운 Role 구성 테스트이므로 eks를 생성한 계정으로 kube config를 생성한다.

k8s에서 role 구성

rbac.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: rbac-test-ns
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rbac-test-role
rules:
  - apiGroups: ["extensions", "apps", ""]
    resources: ["pods", "pods/log", "deployments", "replicasets", "services"]
    verbs: ["get", "watch", "list"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rbac-test-role-binding
  namespace: rbac-test-ns
roleRef:
  kind: ClusterRole
  name: rbac-test-role
  apiGroup: rbac.authorization.k8s.io
subjects:
  - kind: Group
    name: rbac-test-group
    apiGroup: rbac.authorization.k8s.io

위 매니페스트를 통해 rbac-test-ns를 생성하고 rbac-test-group에 속하는 사용자들에게 rbac-test-role을 할당하였다.

IAM 연결

rbac 구성을 위한 test iam을 대시보드에서 생성하여 arn을 복사한다.

(다만 이때 iam에 AmazonEKSWorkerNodePolicy정책을 연결한다. 추후 update-kubeconfig 시 필요)

$ eksctl create iamidentitymapping \
--cluster <cluster-name>
--group rbac-test-group
--arn <iam-arn>

위 명령어 수행 시 aws-auth의 mapUsers 아래에 rbac-test-group에 속하는 iam이 연결된다.

$ k get cm -n kube-system aws-auth -o yaml
apiVersion: v1
data:
  mapAccounts: |
    - <account-number>
  mapRoles: |
    - groups:
      - system:bootstrappers
      - system:nodes
      rolearn: <arn>
      username: system:node:{{EC2PrivateDNSName}}
  mapUsers: |
    - groups:
      - system:masters
      userarn: <eks 생성자 arn>
      username: <eks 생성자 arn>
    - groups:
      - rbac-test-group
      userarn: <test-iam>
kind: ConfigMap
metadata:
  creationTimestamp: "2023-09-02T05:03:29Z"
  name: aws-auth
  namespace: kube-system
  resourceVersion: "5014"
  uid: 77364edb-008d-4540-a719-e629e5307dcb

위와 같이 확인할 수 있으며, 이후 아래 명령어를 통해 권한이 제어되는지 확인할 수 있다.

$ aws configure
# test iam으로 연결
$ aws eks update-kubeconfig --name test-cluster
Updated context ...
$ kgp
Error from server (Forbidden): pods is forbidden: User "" cannot list resource "pods" in API group "" in the namespace "default"

위와 같이 kubectl get pods 를 실행했을 때 forbidden이 발생하는 것을 확인할 수 있다.

$ k get pods -o wide -n rbac-test-ns
No resources found in rbac-test-ns namespace.

role로 정의된 것과 같이 rbac-test-ns에 대한 요청은 정상적으로 수행할 수 있다.

linux user와 통합

$ adduser alice
...
$ adduser bob
...
$ su bob
$ aws configure
...
$ aws eks update-kubeconfig --name <cluster-name>
$ kubectl get pods -o wide
Error from server (Forbidden): pods is forbidden: User "" cannot list resource "pods" in API group "" in the namespace "default"

이처럼 iam과 kube config 등 관련 설정은 $HOME 아래에 위치하므로 linux user와 연계하여 특정 사용자에게만 권한을 줄 수 있다.

0개의 댓글