|
1 | 1 | <p align="center"><img src="https://raw.githubusercontent.com/Labs64/.github/refs/heads/master/assets/labs64-io-ecosystem.png"></p> |
2 | 2 |
|
3 | | -# Labs64.IO :: API Gateway |
4 | | -## Unified API Gateway for Labs64.IO Microservices |
| 3 | +## Traefik Auth (M2M) Middleware |
5 | 4 |
|
6 | | - |
| 5 | +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. |
7 | 6 |
|
8 | | -Key responsibilities of the API Gateway: |
| 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. |
9 | 8 |
|
10 | | -- Request Routing: Directs incoming HTTP/S requests to the appropriate backend service or RabbitMQ exchange/queue. |
11 | | -- Protocol Translation: Translates synchronous HTTP/S requests from clients into asynchronous messages for RabbitMQ, and vice-versa for responses. |
12 | | -- Authentication and Authorization: Enforces security policies before requests reach your internal services. |
13 | | -- Rate Limiting and Throttling: Protects your backend from abuse and ensures fair usage. |
14 | | -- Caching: Caches responses for frequently accessed data, reducing load on your backend. |
15 | | -- Request/Response Transformation: Modifies request or response bodies/headers to fit external API contracts. |
16 | | -- Load Balancing: Distributes requests across multiple instances of your services. |
17 | | -- Monitoring and Logging: Provides a central point for tracking API usage and performance. |
| 9 | +## Features |
18 | 10 |
|
19 | | -## Star History |
| 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. |
20 | 15 |
|
21 | | -[](https://www.star-history.com/#Labs64/labs64.io-api-gateway&Date) |
| 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 | +- `KEYCLOAK_DISCOVERY_URL`: The URL to the Keycloak discovery endpoint (e.g., http://keycloak.default.svc.cluster.local:8080/realms/labs64io/.well-known/openid-configuration). |
| 31 | +- `ROLE_MAPPING_FILE`: YAML file defining the path-to-role mapping. This can be passed as a ConfigMap in a production environment. |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +- Once deployed, Traefik will intercept any request to *whoami.example.com* and forward it to the auth-middleware for authentication. |
| 36 | +- 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. |
| 37 | +- 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. |
| 38 | + |
| 39 | +### For example: |
| 40 | + |
| 41 | +- A request to `/api/admin` would require the `admin` role. |
| 42 | +- A request to `/api/users` would require either the `user` or `admin` role. |
| 43 | + |
| 44 | +## License |
| 45 | + |
| 46 | +This project is licensed under the MIT License. |
0 commit comments