JWT Token
This task shows you how to set up an Istio authorization policy to enforce access based on a JSON Web Token (JWT). An Istio authorization policy supports both string typed and list-of-string typed JWT claims.
Before you begin
Before you begin this task, do the following:
- Complete the Istio end user authentication task. 
- Read the Istio authorization concepts. 
- Install Istio using Istio installation guide. 
- Deploy two workloads: - httpbinand- curl. Deploy these in one namespace, for example- foo. Both workloads run with an Envoy proxy in front of each. Deploy the example namespace and workloads using these commands:- $ kubectl create ns foo $ kubectl apply -f <(istioctl kube-inject -f @samples/httpbin/httpbin.yaml@) -n foo $ kubectl apply -f <(istioctl kube-inject -f @samples/curl/curl.yaml@) -n foo
- Verify that - curlsuccessfully communicates with- httpbinusing this command:- $ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl http://httpbin.foo:8000/ip -sS -o /dev/null -w "%{http_code}\n" 200
Allow requests with valid JWT and list-typed claims
- The following command creates the - jwt-examplerequest authentication policy for the- httpbinworkload in the- foonamespace. This policy for- httpbinworkload accepts a JWT issued by- testing@secure.istio.io:- $ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: RequestAuthentication metadata: name: "jwt-example" namespace: foo spec: selector: matchLabels: app: httpbin jwtRules: - issuer: "testing@secure.istio.io" jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.27/security/tools/jwt/samples/jwks.json" EOF
- Verify that a request with an invalid JWT is denied: - $ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer invalidToken" -w "%{http_code}\n" 401
- Verify that a request without a JWT is allowed because there is no authorization policy: - $ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -w "%{http_code}\n" 200
- The following command creates the - require-jwtauthorization policy for the- httpbinworkload in the- foonamespace. The policy requires all requests to the- httpbinworkload to have a valid JWT with- requestPrincipalset to- testing@secure.istio.io/testing@secure.istio.io. Istio constructs the- requestPrincipalby combining the- issand- subof the JWT token with a- /separator as shown:- $ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: AuthorizationPolicy metadata: name: require-jwt namespace: foo spec: selector: matchLabels: app: httpbin action: ALLOW rules: - from: - source: requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"] EOF
- Get the JWT that sets the - issand- subkeys to the same value,- testing@secure.istio.io. This causes Istio to generate the attribute- requestPrincipalwith the value- testing@secure.istio.io/testing@secure.istio.io:- $ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.27/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode {"exp":4685989700,"foo":"bar","iat":1532389700,"iss":"testing@secure.istio.io","sub":"testing@secure.istio.io"}
- Verify that a request with a valid JWT is allowed: - $ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN" -w "%{http_code}\n" 200
- Verify that a request without a JWT is denied: - $ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -w "%{http_code}\n" 403
- The following command updates the - require-jwtauthorization policy to also require the JWT to have a claim named- groupscontaining the value- group1:- $ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: AuthorizationPolicy metadata: name: require-jwt namespace: foo spec: selector: matchLabels: app: httpbin action: ALLOW rules: - from: - source: requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"] when: - key: request.auth.claims[groups] values: ["group1"] EOF
- Get the JWT that sets the - groupsclaim to a list of strings:- group1and- group2:- $ TOKEN_GROUP=$(curl https://raw.githubusercontent.com/istio/istio/release-1.27/security/tools/jwt/samples/groups-scope.jwt -s) && echo "$TOKEN_GROUP" | cut -d '.' -f2 - | base64 --decode {"exp":3537391104,"groups":["group1","group2"],"iat":1537391104,"iss":"testing@secure.istio.io","scope":["scope1","scope2"],"sub":"testing@secure.istio.io"}
- Verify that a request with the JWT that includes - group1in the- groupsclaim is allowed:- $ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN_GROUP" -w "%{http_code}\n" 200
- Verify that a request with a JWT, which doesn’t have the - groupsclaim is rejected:- $ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN" -w "%{http_code}\n" 403
Clean up
Remove the namespace foo:
$ kubectl delete namespace foo