|
1 | | -# traefik-auth-middleware |
2 | | -Keycloak gatekeeper middleware for traefik |
| 1 | +# Traefik Auth Middleware |
| 2 | + |
| 3 | +This repository contains a Traefik middleware for authentication via Keycloak, with a **local debug server** to test it outside of Traefik. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Structure |
| 8 | + |
| 9 | +``` |
| 10 | +traefik-auth-middleware/ |
| 11 | +|── auth_test.go # Unit tests |
| 12 | +├── auth.go # Middleware code |
| 13 | +├── go.mod |
| 14 | +└── debug/ |
| 15 | + └── main.go # Local debug server |
| 16 | +``` |
| 17 | + |
| 18 | +* `auth.go` → middleware used by Traefik. |
| 19 | +* `debug/main.go` → minimal HTTP server to test the middleware and view Keycloak responses. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Dev setup |
| 24 | + |
| 25 | +Install tools and dev dependencies |
| 26 | + |
| 27 | +```bash |
| 28 | +mise install |
| 29 | +mise run install_yaegi |
| 30 | +``` |
| 31 | + |
| 32 | +### Run tests |
| 33 | + |
| 34 | +```bash |
| 35 | +mise run test |
| 36 | +``` |
| 37 | + |
| 38 | +### Format and lint the code |
| 39 | + |
| 40 | +```bash |
| 41 | +mise run format |
| 42 | +mise run lint |
| 43 | +``` |
| 44 | + |
| 45 | +## Running the debug server |
| 46 | + |
| 47 | +1. Run the server: |
| 48 | + |
| 49 | +```bash |
| 50 | +go run debug/. |
| 51 | +``` |
| 52 | + |
| 53 | +2. The server listens on port `8080`. |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Testing the middleware |
| 58 | + |
| 59 | +Using `curl`: |
| 60 | + |
| 61 | +```bash |
| 62 | +curl "http://localhost:8080?shop_name=test&api_key=secret" |
| 63 | +``` |
| 64 | + |
| 65 | +### Example response on success: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "message": "OK - middleware passed", |
| 70 | + "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI..." |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +If the request is malformed or authentication fails, the middleware returns an appropriate HTTP status code and an error message. |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Logs |
| 79 | + |
| 80 | +The middleware outputs JSON logs to standard output: |
| 81 | + |
| 82 | +```json |
| 83 | +{ |
| 84 | + "level": "INFO", |
| 85 | + "message": "Traefik-auth-middleware - Fetching auth token success for shop: test", |
| 86 | + "plugin_name": "sw-auth-plugin" |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +These logs allow you to track: |
| 91 | + |
| 92 | +* The incoming request and its parameters |
| 93 | +* The HTTP request to Keycloak |
| 94 | +* Authentication success or failure |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +## Notes |
| 99 | + |
| 100 | +* `main.go` is **only used for local debugging**. |
| 101 | +* When using Traefik, running this server is not necessary. |
| 102 | +* IAM variables must be configured in the Traefik config or via `CreateConfig()` for debugging. |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## Debugging points |
| 107 | + |
| 108 | +1. Ensure that the **query params** (`shop_name` and `api_key`) are present. |
| 109 | +2. Check that the **JSON logs** show the HTTP request to Keycloak and the `status code`. |
| 110 | +3. Using the debug server, you can directly see the Keycloak response in the HTTP response. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Complete debug output example |
| 115 | + |
| 116 | +``` |
| 117 | +Middleware debug server started on :8080 |
| 118 | +{"level":"INFO","message":"Traefik-auth-middleware - Fetching auth token success for shop: test","plugin_name":"sw-auth-plugin"} |
| 119 | +``` |
| 120 | + |
| 121 | +HTTP Response: |
| 122 | + |
| 123 | +```json |
| 124 | +{ |
| 125 | + "message": "OK - middleware passed", |
| 126 | + "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI..." |
| 127 | +} |
| 128 | +``` |
0 commit comments