×

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

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

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

对于 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 中创建应用程序和其他工作负载的项目。

步骤
  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` 对象中的每个无服务器应用程序命名空间中创建一个 `RequestAuthentication` 资源

    apiVersion: security.istio.io/v1beta1
    kind: RequestAuthentication
    metadata:
      name: jwt-example
      namespace: <namespace>
    spec:
      jwtRules:
      - issuer: [email protected]
        jwksUri: https://raw.githubusercontent.com/istio/istio/release-1.8/security/tools/jwt/samples/jwks.json
  4. 应用 `RequestAuthentication` 资源

    $ oc apply -f <filename>
  5. 通过创建以下 `AuthorizationPolicy` 资源,允许系统 Pod 访问 `ServiceMeshMemberRoll` 对象中每个无服务器应用程序命名空间的 `RequestAuthenticaton` 资源

    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
      name: allowlist-by-paths
      namespace: <namespace>
    spec:
      action: ALLOW
      rules:
      - to:
        - operation:
            paths:
            - /metrics (1)
            - /healthz (2)
    1 您的应用程序上用于收集系统 Pod 指标的路径。
    2 您的应用程序上用于由系统 Pod 探测的路径。
  6. 应用 `AuthorizationPolicy` 资源

    $ oc apply -f <filename>
  7. 为 `ServiceMeshMemberRoll` 对象中的每个无服务器应用程序命名空间创建以下 `AuthorizationPolicy` 资源

    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
      name: require-jwt
      namespace: <namespace>
    spec:
      action: ALLOW
      rules:
      - from:
        - source:
           requestPrincipals: ["[email protected]/[email protected]"]
  8. 应用 `AuthorizationPolicy` 资源

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

    示例命令
    $ curl http://hello-example-1-default.apps.mycluster.example.com/
    示例输出
    RBAC: access denied
  2. 使用有效的 JWT 验证请求。

    1. 获取有效的 JWT 令牌

      $ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.8/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
    2. 使用 `curl` 请求头中的有效令牌访问服务

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

      现在允许请求

      示例输出
      Hello OpenShift!