You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository contains a custom Traefik ForwardAuth middleware. The middleware is designed to verify M2M (Machine-to-Machine) JWT tokens issued by Keycloak and enforce path-based role-based access control (RBAC) for microservices deployed on Kubernetes.
6
+
7
+
It receives incoming requests from Traefik, validates the JWT token, extracts user roles, and checks them against a configurable path/role mapping. If the request is authorized, it allows Traefik to forward the request to the backend service. Otherwise, it returns a `401 Unauthorized` or `403 Forbidden` response.
8
+
9
+
## Features
10
+
11
+
- JWT Verification: Validates tokens issued by Keycloak using public keys from the `.well-known` endpoint.
12
+
- Role-Based Access Control (RBAC): Enforces access based on roles assigned to the user/client.
13
+
- Configurable Role Mapping: Allows administrators to define a mapping of URL paths to required roles.
14
+
- FastAPI Backend: A lightweight and performant backend for handling authentication logic.
15
+
16
+
## Prerequisites
17
+
18
+
- A running Kubernetes cluster.
19
+
- Traefik installed as an Ingress Controller in your cluster.
20
+
- A configured Keycloak instance.
21
+
- Docker for building the middleware container image.
22
+
23
+
## Configuration
24
+
25
+
The middleware is configured using environment variables.
26
+
27
+
-`KEYCLOAK_URL`: The base URL of your Keycloak instance (e.g., http://keycloak.default.svc.cluster.local:8080).
28
+
-`KEYCLOAK_REALM`: The name of the realm in Keycloak (e.g., labs64io).
29
+
-`KEYCLOAK_AUDIENCE`: The audience claim to verify in the JWT (e.g., labs64io_client).
30
+
-`ROLE_MAPPING`: A JSON string defining the path-to-role mapping. This can be passed as a ConfigMap in a production environment.
31
+
32
+
## Usage
33
+
34
+
- Once deployed, Traefik will intercept any request to *whoami.example.com* and forward it to the auth-middleware for authentication.
35
+
- For a request to be successful, it must include a valid JWT in the Authorization header with the format `Bearer <token>`. The roles contained in the JWT must match the required roles for the requested path as defined in your role mapping.
36
+
- The role mapping is a key part of the middleware's logic. You would define a dictionary that maps a path prefix to a list of required roles.
37
+
38
+
### For example:
39
+
40
+
- A request to `/api/admin` would require the `admin` role.
41
+
- A request to `/api/users` would require either the `user` or `admin` role.
0 commit comments