6.3. 在 Service Mesh 1.x 中使用 JSON Web 令牌身份验证
您可以使用 Service Mesh 1.x 和 OpenShift Serverless 在 Knative 服务中使用 JSON Web Token (JWT) 身份验证。要做到这一点,您必须在作为 ServiceMeshMemberRoll 对象的成员的应用程序命名空间中创建策略。您还必须为该服务启用 sidecar 注入。
在启用了 Kourier 时,不支持在系统命名空间中向 pod 添加 sidecar 注入,如 knative-serving 和 knative-serving-ingress。
对于 OpenShift Container Platform,如果您需要这些命名空间中的 pod 进行 sidecar 注入,请参阅 OpenShift Serverless 文档来 原生将 Service Mesh 与 OpenShift Serverless 集成。
先决条件
- 您已在集群中安装了 OpenShift Serverless Operator、Knative Serving 和 Red Hat OpenShift Service Mesh。
-
安装 OpenShift CLI (
oc) 。 - 您已创建了一个项目,或者具有适当的角色和权限访问项目,以便在 OpenShift Container Platform 中创建应用程序和其他工作负载。
流程
在您的服务中添加
sidecar.istio.io/inject="true"注解:服务示例
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: <service_name> spec: template: metadata: annotations: sidecar.istio.io/inject: "true"1 sidecar.istio.io/rewriteAppHTTPProbers: "true"2 ...应用
Service资源:$ oc apply -f <filename>在作为
ServiceMeshMemberRoll对象的成员的无服务器应用程序命名空间中创建策略,该策略只允许具有有效 JSON Web Tokens(JWT)的请求:重要路径
/metrics和/healthz必须包含在excludePaths中,因为它们是从knative-serving命名空间中的系统 pod 访问的。apiVersion: authentication.istio.io/v1alpha1 kind: Policy metadata: name: default namespace: <namespace> 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: /metrics1 - prefix: /healthz2 principalBinding: USE_ORIGIN应用
Policy资源:$ oc apply -f <filename>
验证
如果您尝试使用
curl请求来获取 Knative 服务 URL,则会被拒绝:$ curl http://hello-example-default.apps.mycluster.example.com/输出示例
Origin authentication failed.使用有效 JWT 验证请求。
获取有效的 JWT 令牌:
$ 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 -使用
curl请求标头中的有效令牌访问该服务:$ curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"现在允许请求:
输出示例
Hello OpenShift!