|
| 1 | +# Request Authentication |
| 2 | + |
| 3 | +This guide provides instructions for configuring [JSON Web Token (JWT)][jwt] authentication. JWT authentication checks |
| 4 | +if an incoming request has a valid JWT before routing the request to a backend service. Currently, Envoy Gateway only |
| 5 | +supports validating a JWT from an HTTP header, e.g. `Authorization: Bearer <token>`. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Follow the steps from the [Quickstart](quickstart.md) guide to install Envoy Gateway and the example manifest. |
| 10 | +Before proceeding, you should be able to query the example backend using HTTP. |
| 11 | + |
| 12 | +## Configuration |
| 13 | + |
| 14 | +Allow requests with a valid JWT by creating an [AuthenticationFilter][] and referencing it from the example HTTPRoute. |
| 15 | + |
| 16 | +```shell |
| 17 | +kubectl apply -f https://raw.githubusercontent.com/envoyproxy/gateway/latest/examples/kubernetes/authn/jwt.yaml |
| 18 | +``` |
| 19 | + |
| 20 | +The HTTPRoute is now updated to authenticate requests for `/foo` and allow unauthenticated requests to `/bar`. The |
| 21 | +`/foo` route rule references an AuthenticationFilter that provides the JWT authentication configuration. |
| 22 | + |
| 23 | +Verify the HTTPRoute configuration and status: |
| 24 | + |
| 25 | +```shell |
| 26 | +kubectl get httproute/backend -o yaml |
| 27 | +``` |
| 28 | + |
| 29 | +The AuthenticationFilter is configured for JWT authentication and uses a single [JSON Web Key Set (JWKS)][jwks] |
| 30 | +provider for authenticating the JWT. |
| 31 | + |
| 32 | +Verify the AuthenticationFilter configuration: |
| 33 | + |
| 34 | +```shell |
| 35 | +kubectl get authenticationfilter/jwt-example -o yaml |
| 36 | +``` |
| 37 | + |
| 38 | +## Testing |
| 39 | + |
| 40 | +Ensure the `GATEWAY_HOST` environment variable from the [Quickstart](quickstart.md) guide is set. If not, follow the |
| 41 | +Quickstart instructions to set the variable. |
| 42 | + |
| 43 | +```shell |
| 44 | +echo $GATEWAY_HOST |
| 45 | +``` |
| 46 | + |
| 47 | +Verify that requests to `/foo` are denied without a JWT: |
| 48 | + |
| 49 | +```shell |
| 50 | +curl -sS -o /dev/null -H "Host: www.example.com" -w "%{http_code}\n" http://$GATEWAY_HOST/foo |
| 51 | +``` |
| 52 | + |
| 53 | +A `401` HTTP response code should be returned. |
| 54 | + |
| 55 | +Get the JWT used for testing request authentication: |
| 56 | + |
| 57 | +```shell |
| 58 | +TOKEN=$(curl https://raw.githubusercontent.com/envoyproxy/gateway/main/examples/kubernetes/authn/test.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode - |
| 59 | +``` |
| 60 | + |
| 61 | +__Note:__ The above command decodes and returns the token's payload. You can replace `f2` with `f1` to view the token's |
| 62 | +header. |
| 63 | + |
| 64 | +Verify that a request to `/foo` with a valid JWT is allowed: |
| 65 | + |
| 66 | +```shell |
| 67 | +curl -sS -o /dev/null -H "Host: www.example.com" -H "Authorization: Bearer $TOKEN" -w "%{http_code}\n" http://$GATEWAY_HOST/foo |
| 68 | +``` |
| 69 | + |
| 70 | +A `200` HTTP response code should be returned. |
| 71 | + |
| 72 | +Verify that requests to `/bar` are allowed __without__ a JWT: |
| 73 | + |
| 74 | +```shell |
| 75 | +curl -sS -o /dev/null -H "Host: www.example.com" -w "%{http_code}\n" http://$GATEWAY_HOST/bar |
| 76 | +``` |
| 77 | + |
| 78 | +## Clean-Up |
| 79 | + |
| 80 | +Follow the steps from the [Quickstart](quickstart.md) guide to uninstall Envoy Gateway and the example manifest. |
| 81 | + |
| 82 | +Delete the AuthenticationFilter: |
| 83 | + |
| 84 | +```shell |
| 85 | +kubectl delete authenticationfilter/jwt-example |
| 86 | +``` |
| 87 | + |
| 88 | +## Next Steps |
| 89 | + |
| 90 | +Checkout the [Developer Guide](../dev/README.md) to get involved in the project. |
| 91 | + |
| 92 | +[jwt]: https://tools.ietf.org/html/rfc7519 |
| 93 | +[AuthenticationFilter]: https://github.com/envoyproxy/gateway/blob/main/api/v1alpha1/authenticationfilter_types.go |
| 94 | +[jwks]: https://tools.ietf.org/html/rfc7517 |
0 commit comments