JWT claim based routing

This feature is targeted at developers / expert users and is considered Alpha.

This task shows you how to route requests based on JWT claims on an Istio ingress gateway using the request authentication and virtual service.

Note: this feature only supports Istio ingress gateway and requires the use of both request authentication and virtual service to properly validate and route based on JWT claims.

Before you begin

  • Understand Istio authentication policy and virtual service concepts.

  • Install Istio using the Istio installation guide.

  • Deploy a workload, httpbin in a namespace, for example foo, and expose it through the Istio ingress gateway with this command:

    ZipZip

    1. $ kubectl create ns foo
    2. $ kubectl apply -f <(istioctl kube-inject -f @samples/httpbin/httpbin.yaml@) -n foo
    3. $ kubectl apply -f @samples/httpbin/httpbin-gateway.yaml@ -n foo
  • Follow the instructions in Determining the ingress IP and ports to define the INGRESS_HOST and INGRESS_PORT environment variables.

  • Verify that the httpbin workload and ingress gateway are working as expected using this command:

    1. $ curl "$INGRESS_HOST:$INGRESS_PORT"/headers -s -o /dev/null -w "%{http_code}\n"
    2. 200

If you don’t see the expected output, retry after a few seconds. Caching and propagation overhead can cause a delay.

Configuring ingress routing based on JWT claims

The Istio ingress gateway supports routing based on authenticated JWT, which is useful for routing based on end user identity and more secure compared using the unauthenticated HTTP attributes (e.g. path or header).

  1. In order to route based on JWT claims, first create the request authentication to enable JWT validation:

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: security.istio.io/v1
    3. kind: RequestAuthentication
    4. metadata:
    5. name: ingress-jwt
    6. namespace: istio-system
    7. spec:
    8. selector:
    9. matchLabels:
    10. istio: ingressgateway
    11. jwtRules:
    12. - issuer: "testing@secure.istio.io"
    13. jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.21/security/tools/jwt/samples/jwks.json"
    14. EOF

    The request authentication enables JWT validation on the Istio ingress gateway so that the validated JWT claims can later be used in the virtual service for routing purposes.

    The request authentication is applied on the ingress gateway because the JWT claim based routing is only supported on ingress gateways.

    Note: the request authentication will only check the JWT if it exists in the request. To make the JWT required and reject the request if it does not include JWT, apply the authorization policy as specified in the task.

  2. Update the virtual service to route based on validated JWT claims:

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: VirtualService
    4. metadata:
    5. name: httpbin
    6. namespace: foo
    7. spec:
    8. hosts:
    9. - "*"
    10. gateways:
    11. - httpbin-gateway
    12. http:
    13. - match:
    14. - uri:
    15. prefix: /headers
    16. headers:
    17. "@request.auth.claims.groups":
    18. exact: group1
    19. route:
    20. - destination:
    21. port:
    22. number: 8000
    23. host: httpbin
    24. EOF

    The virtual service uses the reserved header "@request.auth.claims.groups" to match with the JWT claim groups. The prefix @ denotes it is matching with the metadata derived from the JWT validation and not with HTTP headers.

    Claim of type string, list of string and nested claims are supported. Use the . or [] as a separator for nested claim names. For example, "@request.auth.claims.name.givenName" or "@request.auth.claims[name][givenName]" matches the nested claim name and givenName, they are equivalent here. When the claim name contains ., only [] can be used as a separator.

Validating ingress routing based on JWT claims

  1. Validate the ingress gateway returns the HTTP code 404 without JWT:

    1. $ curl -s -I "http://$INGRESS_HOST:$INGRESS_PORT/headers"
    2. HTTP/1.1 404 Not Found
    3. ...

    You can also create the authorization policy to explicitly reject the request with HTTP code 403 when JWT is missing.

  2. Validate the ingress gateway returns the HTTP code 401 with invalid JWT:

    1. $ curl -s -I "http://$INGRESS_HOST:$INGRESS_PORT/headers" -H "Authorization: Bearer some.invalid.token"
    2. HTTP/1.1 401 Unauthorized
    3. ...

    The 401 is returned by the request authentication because the JWT failed the validation.

  3. Validate the ingress gateway routes the request with a valid JWT token that includes the claim groups: group1:

    1. $ TOKEN_GROUP=$(curl https://raw.githubusercontent.com/istio/istio/release-1.21/security/tools/jwt/samples/groups-scope.jwt -s) && echo "$TOKEN_GROUP" | cut -d '.' -f2 - | base64 --decode
    2. {"exp":3537391104,"groups":["group1","group2"],"iat":1537391104,"iss":"testing@secure.istio.io","scope":["scope1","scope2"],"sub":"testing@secure.istio.io"}
    1. $ curl -s -I "http://$INGRESS_HOST:$INGRESS_PORT/headers" -H "Authorization: Bearer $TOKEN_GROUP"
    2. HTTP/1.1 200 OK
    3. ...
  4. Validate the ingress gateway returns the HTTP code 404 with a valid JWT but does not include the claim groups: group1:

    1. $ TOKEN_NO_GROUP=$(curl https://raw.githubusercontent.com/istio/istio/release-1.21/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN_NO_GROUP" | cut -d '.' -f2 - | base64 --decode
    2. {"exp":4685989700,"foo":"bar","iat":1532389700,"iss":"testing@secure.istio.io","sub":"testing@secure.istio.io"}
    1. $ curl -s -I "http://$INGRESS_HOST:$INGRESS_PORT/headers" -H "Authorization: Bearer $TOKEN_NO_GROUP"
    2. HTTP/1.1 404 Not Found
    3. ...

Cleanup

  • Remove the namespace foo:

    1. $ kubectl delete namespace foo
  • Remove the request authentication:

    1. $ kubectl delete requestauthentication ingress-jwt -n istio-system