Use Keycloak as OIDC Provider to Grant Access to Kubernetes Cluster with kube-oidc Plugin and Headlamp UI
Run ./cluster-setup.sh
to create a Kubernetes cluster with:
- 1 control-plane node
- 4 worker nodes
- Installed ingress-nginx
- Installed MetalLB
- 4 proxy image repositories in Docker containers within one network
Run ./cert-manager-setup.sh
Run ./keycloak-setup.sh
This will create 3 groups with 4 users having specific RBAC permissions:
Group | User | Cluster Permission |
---|---|---|
kube-dev | dev1 | view role on namespace app |
kube-dev | dev2 | view role on namespace app |
kube-dev-lead | dev-lead | edit role on namespace app |
kube-manager | manager | view role on cluster |
kube-admin | admin1 | admin role on cluster |
Run: terraform init && terraform apply -auto-approve
Run: ./headlamp-setup.sh
Run ./kubectl-oidc.sh
This will set the current context to oidc-client
on the kind-kind
cluster.
-
Run
kubectl get po -A
-
In the opened browser, enter credentials:
- Username:
manager
- Password:
manager
- Username:
-
Attempt to create a pod (should be forbidden):
-
Clean previous session:
kubectl oidc-login clean
Don't forget to logout from Keycloak. -
Enter credentials:
- Username:
dev-lead
- Password:
dev-lead
- Username:
-
Run
kubectl get po -A
:dev-lead
doesn't have permission to list pods in all namespaces. -
Create a new pod in
app
namespace:kubectl run nginx2 --image nginx -n app
dev-lead
has permission to run pods as expected.
-
Clean previous session:
kubectl oidc-login clean
Don't forget to logout from Keycloak.
-
Enter credentials:
- Username:
dev1
- Password:
dev1
- Username:
-
List pods in
app
namespace:kubectl get po -n app
User can list pods in namespace
app
. -
Attempt to create a pod in
app
namespace (should be forbidden):
User can't create pods in namespace app
as expected.
-
Go to
https://console.kind.cluster
Don't forget to logout from Keycloak. -
Enter credentials:
- Username:
admin1
- Password:
admin1
- Username:
-
Got access to cluster as cluster admin