12.2. Using JSON Web Token authentication with Service Mesh and OpenShift Serverless
You can enable JSON Web Token (JWT) authentication for Knative services by creating a policy in your serverless application namespace that only allows requests with valid JWTs.
Prerequisites
- Install OpenShift Serverless.
- Install Red Hat OpenShift Service Mesh.
- Configure Service Mesh with OpenShift Serverless, including enabling sidecar injection for your Knative services.
Adding sidecar injection to pods in system namespaces such as knative-serving
and knative-serving-ingress
is not supported.
Procedure
Copy the following
Policy
resource into a YAML file:重要The paths
/metrics
and/healthz
must be included inexcludedPaths
because they are accessed from system pods in theknative-serving
namespace.apiVersion: authentication.istio.io/v1alpha1 kind: Policy metadata: name: default spec: origins: - jwt: issuer: testing@secure.istio.io jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/jwks.json" triggerRules: - excludedPaths: - prefix: /metrics - prefix: /healthz principalBinding: USE_ORIGIN
Apply the
Policy
resource YAML file:$ oc apply -f <filename>
Verification
If you try to use a
curl
request to get the Knative service URL, it is denied.$ curl http://hello-example-default.apps.mycluster.example.com/
Example output
Origin authentication failed.
Verify the request with a valid JWT.
Get the valid JWT token by entering the following command:
$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
Access the service by using the valid token in the
curl
request header:$ curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"
The request is now allowed.
Example output
Hello OpenShift!
12.2.1. Additional resources
- See Red Hat OpenShift Service Mesh architecture.
-
For more information about verifying Knative services and using
curl
requests, see Verifying your serverless application deployment.