使用Keycloak的OIDC身份验证,在Kubernetes上实现租户控制
首先
※本記事は前回投稿した「KeycloakによるKubernetesのOIDC認証を試す」という記事の続きになります。
通过在Keycloak中创建的组,为Kubernetes的特定Namespace授予操作权限,实现租户控制。

Keycloakの設定
KeycloakのAdministration Consoleにログインしてユーザーとグループを作成する。
创建群组
group-a、group-bというグループをそれぞれ作成する。
group-a: KubernetesクラスターのNamespace=tenant-a(後程作成)に対して管理者権限を持つグループ
group-b: KubernetesクラスターのNamespace=tenant-b(後程作成)に対して管理者権限を持つグループ
尽管截图中只显示了创建group-a, 但同样要创建group-b。

ユーザーの作成
创建用户A和用户B。
user-a: group-aグループに所属
user-b: group-bグループに所属
※キャプチャではuser-aの作成しか示していないがuser-bについても同様に作成する。
※パスワードは任意の値を設定(ここではP@ssw0rdという値を設定した)

Kubernetesクラスターの設定
使用以下的manifest文件创建Namespace、Role和RoleBinding。
* 仅由具有Cluster-Role=cluster-admin等效权限的用户创建资源。
租户-甲.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
租户-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
确认动作
在这里,我们正在对用户A进行操作确认,但是对于用户B也是一样的。
确认
以中文将以下内容翻译,只需要一个选项:
通过用户A,该用户属于A组,进行身份验证,并获取身份令牌和刷新令牌。
# 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-tokenとrefresh-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クラスタ名>".
クラスターアクセス
确认用户A能够访问Namespace=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-aでNamespace=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。
一只狐狸,不是狸狗。
Tweets by mochizuki875