10.2. 为 Knative 服务配置 JSON Web 令牌身份验证

OpenShift Serverless 当前没有用户定义的授权功能。要为部署添加用户定义的授权,您必须将 OpenShift Serverless 与 Red Hat OpenShift Service Mesh 集成,然后为 Knative 服务配置 JSON Web Token (JWT) 身份验证和 sidecar 注入。

10.2.1. 在 Service Mesh 2.x 和 OpenShift Serverless 中使用 JSON Web 令牌身份验证

您可以使用 Service Mesh 2.x 和 OpenShift Serverless 在 Knative 服务中使用 JSON Web Token (JWT) 身份验证。要做到这一点,您必须在作为 ServiceMeshMemberRoll 对象成员的应用程序命名空间中创建身份验证请求和策略。您还必须为该服务启用 sidecar 注入。

重要

在启用了 Kourier 时,不支持在系统命名空间中向 pod 添加 sidecar 注入,如 knative-servingknative-serving-ingress

如果需要对这些命名空间中的 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: testing@secure.istio.io
        jwksUri: https://raw.githubusercontent.com/istio/istio/release-1.8/security/tools/jwt/samples/jwks.json
  4. 应用 RequestAuthentication 资源:

    $ oc apply -f <filename>
  5. 通过创建以下 AuthorizationPolicy 资源,允许从 ServiceMeshMemberRoll 对象中的每个无服务器应用程序命名空间的系统 pod 访问 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: ["testing@secure.istio.io/testing@secure.istio.io"]
  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!

10.2.2. 在 Service Mesh 1.x 和 OpenShift Serverless 中使用 JSON Web 令牌身份验证

您可以使用 Service Mesh 1.x 和 OpenShift Serverless 在 Knative 服务中使用 JSON Web Token (JWT) 身份验证。要做到这一点,您必须在作为 ServiceMeshMemberRoll 对象的成员的应用程序命名空间中创建策略。您还必须为该服务启用 sidecar 注入。

重要

在启用了 Kourier 时,不支持在系统命名空间中向 pod 添加 sidecar 注入,如 knative-servingknative-serving-ingress

如果需要对这些命名空间中的 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 对象的成员的无服务器应用程序命名空间中创建策略,该策略只允许具有有效 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: /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!