×

您可以通过使用 Service Mesh 1.x 和 OpenShift Serverless,使用 JSON Web Token (JWT) 身份验证与 Knative 服务进行身份验证。为此,您必须在属于ServiceMeshMemberRoll对象的应用程序命名空间中创建一个策略。您还必须为服务启用 sidecar 注射。

为 Service Mesh 1.x 和 OpenShift Serverless 配置 JSON Web Token 身份验证

启用 Kourier 时,不支持向系统命名空间(例如knative-servingknative-serving-ingress)中的 Pod 添加 sidecar 注射。

对于 OpenShift Container Platform,如果您需要为这些命名空间中的 Pod 进行 sidecar 注射,请参阅 OpenShift Serverless 文档中的“原生集成 Service Mesh 与 OpenShift Serverless”部分。

先决条件
  • 您已在集群上安装了 OpenShift Serverless 运算符、Knative Serving 和 Red Hat OpenShift Service Mesh。

  • 安装 OpenShift CLI (oc)。

  • 您已创建项目或有权访问具有相应角色和权限的项目,以便在 OpenShift Container Platform 中创建应用程序和其他工作负载。

步骤
  1. 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)
    ...
    1 添加sidecar.istio.io/inject="true"注释。
    2 您必须在您的 Knative 服务中设置注释sidecar.istio.io/rewriteAppHTTPProbers: "true",因为 OpenShift Serverless 1.14.0 及更高版本默认使用 HTTP 探测作为 Knative 服务的准备就绪探测。
  2. 应用Service资源

    $ oc apply -f <filename>
  3. 在属于ServiceMeshMemberRoll对象的无服务器应用程序命名空间中创建一个策略,该策略仅允许具有有效 JSON Web Token (JWT) 的请求。

    必须将路径/metrics/healthz包含在excludedPaths中,因为它们是由knative-serving命名空间中的系统 Pod 访问的。

    apiVersion: authentication.istio.io/v1alpha1
    kind: Policy
    metadata:
      name: default
      namespace: <namespace>
    spec:
      origins:
      - jwt:
          issuer: [email protected]
          jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/jwks.json"
          triggerRules:
          - excludedPaths:
            - prefix: /metrics (1)
            - prefix: /healthz (2)
      principalBinding: USE_ORIGIN
    1 您的应用程序上由系统 Pod 收集指标的路径。
    2 您的应用程序上由系统 Pod 探测的路径。
  4. 应用Policy资源

    $ oc apply -f <filename>
验证
  1. 如果您尝试使用curl请求获取 Knative 服务 URL,则会被拒绝。

    $ curl http://hello-example-default.apps.mycluster.example.com/
    示例输出
    Origin authentication failed.
  2. 使用有效的 JWT 验证请求。

    1. 获取有效的 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 -
    2. 使用curl请求头中的有效令牌访问服务

      $ curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"

      请求现在已允许

      示例输出
      Hello OpenShift!