LoginSignup
5
1

More than 1 year has passed since last update.

KeycloakのOIDC認証を利用してKubernetesでテナント制御を行う

Last updated at Posted at 2022-05-11

はじめに

※本記事は前回投稿した「KeycloakによるKubernetesのOIDC認証を試す」という記事の続きになります。

Keycloakで作成したグループに対してKubernetesの特定Namespaceのみに対する操作権限を与えることで、テナント制御を実現する。

イメージは以下の通り。
pic29.png

Keycloakの設定

KeycloakAdministration Consoleにログインしてユーザーとグループを作成する。

グループの作成

group-agroup-bというグループをそれぞれ作成する。

  • group-a: KubernetesクラスターのNamespace=tenant-a(後程作成)に対して管理者権限を持つグループ
  • group-b: KubernetesクラスターのNamespace=tenant-b(後程作成)に対して管理者権限を持つグループ

※キャプチャではgroup-aの作成しか示していないがgroup-bについても同様に作成する。

pic30.png
pic31.png

ユーザーの作成

user-auser-bというユーザーをそれぞれ作成する。

  • user-a: group-aグループに所属
  • user-b: group-bグループに所属

※キャプチャではuser-aの作成しか示していないがuser-bについても同様に作成する。
※パスワードは任意の値を設定(ここではP@ssw0rdという値を設定した)

pic32.png
pic33.png
pic34.png

Kubernetesクラスターの設定

以下のmanifestを用いてNamespace,Role,RoleBindingを作成。
※リソースの作成はCluster-Role=cluster-admin相当の権限を持つユーザーで実施した。

tenant-a.yaml

kind: Namespace
apiVersion: v1
metadata:
  name: tenant-a
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: tenant-a
  namespace: tenant-a
rules:
- apiGroups:
  - '*'
  resources:
  - '*'
  verbs:
  - '*'
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: tenant-a-rb
  namespace: tenant-a
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: tenant-a
subjects:
- kind: Group
  name: "group-a"
  apiGroup: rbac.authorization.k8s.io

tenant-b.yaml

kind: Namespace
apiVersion: v1
metadata:
  name: tenant-b
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: tenant-b
  namespace: tenant-b
rules:
- apiGroups:
  - '*'
  resources:
  - '*'
  verbs:
  - '*'
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: tenant-b-rb
  namespace: tenant-b
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: tenant-b
subjects:
- kind: Group
  name: "group-b"
  apiGroup: rbac.authorization.k8s.io

リソースの作成

# kubectl apply -f tenant-a.yaml
namespace/tenant-a created
role.rbac.authorization.k8s.io/tenant-a created
rolebinding.rbac.authorization.k8s.io/tenant-a-rb created

# kubectl apply -f tenant-b.yaml
namespace/tenant-b created
role.rbac.authorization.k8s.io/tenant-b created
rolebinding.rbac.authorization.k8s.io/tenant-b-rb created

動作確認

ここではuser-aについて動作確認を行っているが、user-bについても同様。

認証

group-aに所属するuser-aで認証し、id-tokenrefresh-tokenを取得する。

# curl -k -d "grant_type=password" -d "scope=openid" -d "client_id=kubernetes" -d "client_secret=<client=kubernetesのsecret>" -d "username=user-a" -d "password=<Keycloakで作成したuser-aのパスワード>" https://keycloak.example.com:32084/realms/kubernetes/protocol/openid-connect/token | jq .

取得したid-tokenrefresh-tokenを用いてkubeconfigの設定を行う。

# kubectl config set-credentials user-a \
    "--auth-provider=oidc" \
    "--auth-provider-arg=idp-issuer-url=https://keycloak.example.com:32084/realms/kubernetes" \
    "--auth-provider-arg=client-id=kubernetes" \
    "--auth-provider-arg=idp-certificate-authority=<keycloak公開鍵のパス>" \
    "--auth-provider-arg=client-secret=<client=kubernetesのsecret>" \
    "--auth-provider-arg=id-token=<id-token>" \
    "--auth-provider-arg=refresh-token=<refresh-token>"
    
# kubectl config set-context user-a@<kubeconfigで定義されているk8sクラスタ名> --cluster=<kubeconfigで定義されているk8sクラスタ名> --user=user-a

# kubectl config use-context user-a@<kubeconfigで定義されているk8sクラスタ名>
Switched to context "user-a@<kubeconfigで定義されているk8sクラスタ名>".

クラスターアクセス

user-aNamespace=tenant-aにアクセスし、諸々の操作が行えることを確認。

tenant-aにPodの作成
# kubectl run nginx-a --image=nginx --restart=Never -n tenant-a
pod/nginx-a created

tenant-aのリソース参照
# kubectl get all -n tenant-a
NAME          READY   STATUS    RESTARTS   AGE
pod/nginx-a   1/1     Running   0          87m

user-aNamespace=tenant-bにアクセスし、諸々の操作が行えないことを確認。

# kubectl get all -n tenant-b
Error from server (Forbidden): pods is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "pods" in API group "" in the namespace "tenant-b"
Error from server (Forbidden): replicationcontrollers is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "replicationcontrollers" in API group "" in the namespace "tenant-b"
Error from server (Forbidden): services is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "services" in API group "" in the namespace "tenant-b"
Error from server (Forbidden): daemonsets.apps is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "daemonsets" in API group "apps" in the namespace "tenant-b"
Error from server (Forbidden): deployments.apps is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "deployments" in API group "apps" in the namespace "tenant-b"
Error from server (Forbidden): replicasets.apps is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "replicasets" in API group "apps" in the namespace "tenant-b"
Error from server (Forbidden): statefulsets.apps is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "statefulsets" in API group "apps" in the namespace "tenant-b"
Error from server (Forbidden): horizontalpodautoscalers.autoscaling is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "horizontalpodautoscalers" in API group "autoscaling" in the namespace "tenant-b"
Error from server (Forbidden): cronjobs.batch is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "cronjobs" in API group "batch" in the namespace "tenant-b"
Error from server (Forbidden): jobs.batch is forbidden: User "https://keycloak.example.com:32084/realms/kubernetes#a user" cannot list resource "jobs" in API group "batch" in the namespace "tenant-b"

宣伝

Twitterやってます。
よかったらフォローいただけると嬉しいです。

@mochizuki875
プロになりたいITエンジニア Kubernetesが好き
a fox, not a raccoon dog.
https://twitter.com/mochizuki875

5
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1