搜索

11.6. 创建 WasmPlugin 自定义资源

download PDF

Service Mesh 提供了一个自定义资源定义 (CRD) 来指定并应用 Proxy-WASM 扩展到 sidecar 代理,称为 WasmPlugin。Service Mesh 将自定义资源 (CR) 应用到需要使用 3scale 管理 HTTP API 的工作负载集合。

流程

  1. 在您的服务网格部署中识别要将其模块部署到的 OpenShift Container Platform (OCP) 命名空间,如 info 项目。
  2. 使用 registry.redhat.io 凭证获取 pull secret。

    • 在与 WasmPlugin 相同的命名空间中创建新的 pull secret 资源。
  3. 您必须声明部署 threescale-wasm-auth 模块的命名空间,以及一个选择器来标识模块要应用到的应用程序集合:以下示例是 threescale-wasm-auth 模块的 CR 的 YAML 格式:

    apiVersion: extensions.istio.io/v1alpha1
    kind: WasmPlugin
    metadata:
      name: <threescale_wasm_plugin_name>
     namespace: <namespace>
    spec:
      url: oci://registry.redhat.io/3scale-amp2/3scale-auth-wasm-rhel8:0.0.3
      imagePullSecret: <pull_secret_resource>
      phase: AUTHZ
      priority: 100
      match:
       - mode: CLIENT
      selector:
        matchLabels:
          app: <selector>
      pluginConfig:
        api: v1
        system:
          name: system
          upstream:
            name: outbound|443||<system_host>
            url: <system_url>
            timeout: 5000
          token: <access_token>
    
        backend:
          name: backend
          upstream:
            name: outbound|<backend_port>||<backend_host>
            url: <backend_url>
            timeout: 5000
          extensions:
          - no_body
        services:
        - id: '<product_id>'
          authorities:
          - "*"
          credentials:
            user_key:
              - query_string:
                  keys:
                    - user_key
              - header:
                  keys:
                    - user_key
    • spec.pluginConfig 字段因应用程序而异。所有其他字段在该自定义资源的多个实例之间都存在。
    • 此特定的 WasmPlugin spec.pluginConfig 配置有查询字符串中提供的 user_key 身份验证。
    • 解释:

      • name

        指定 3scale 中 WasmPlugin 的唯一名称或标识符。

      • namespace

        工作负载的命名空间。

      • imagePullSecret

        在第 2 步中创建的 pull secret 的名称。

      • selector

        工作负载标签选择器。使用 info 项目的 productpage。

      • backend-port

        取决于您使用的 3scale。请参阅在 Service Mesh 中添加 3scale URL。例如,内部 3scale 使用端口 80,外部 3scale 使用端口 443。

      • backend-hostsystem-host

        使用您在将 3scale URL 添加到 Service Mesh 时使用的同一主机。

      • system-urlbackend-url

        使用对应的主机并添加协议。例如: https://<system-host>

      • access-token

        系统租户的访问令牌。

      • product_id

        要使用的产品的 ID。如果您希望有多个产品,在 services 部分定义多个产品。

    • spec.pluginConfig 和其余自定义资源中配置了模块后,使用 oc apply 命令应用它:

      $ oc apply -f threescale-wasm-auth-info.yaml

11.6.1. 3scale API 管理 WasmPlugin 身份验证选项

这些是 3scale User 密钥 (App id/App key) 身份验证的配置示例。

用户密钥

apiVersion: extensions.istio.io/v1alpha1
kind: WasmPlugin
metadata:
  name: <threescale_wasm_plugin_name>
spec:
 ...
  pluginConfig:
  ...
    services:
    - id: '<service_id>'
      authorities:
      - "*"
      credentials:
        user_key:
          - query_string:
              keys:
                - user_key
          - header:
              keys:
                - user_key

App Id 和 App key

apiVersion: extensions.istio.io/v1alpha1
kind: WasmPlugin
metadata:
  name: <threescale_wasm_plugin_name>
spec:
  ...
  pluginConfig:
    ...
    services:
    - id: '<service_id>'
      authorities:
      - "*"
      credentials:
        app_id:
          - query_string:
              keys:
                - app_id
          - header:
              keys:
                - app_id
        app_key:
          - query_string:
              keys:
                - app_key
          - header:
              keys:
                - app_key

OIDC

除了用于 OpenID Connect (OIDC)的 WasmPlugin 本身外,您还需要一个名为 RequestAuthentication 的额外自定义资源。在应用 RequestAuthentication 时,它会使用原生插件配置 Envoy 以验证 JWT 令牌。代理会在运行模块前验证所有内容,因此任何失败的请求都不会将其发送到 3scale WebAssembly 模块。

apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
metadata:
  name: jwt-example
  namespace: <info>
spec:
  selector:
    matchLabels:
      app: <productpage>
  jwtRules:
  - issuer: >-
      "<url>/auth/realms/<realm_name>"
    jwksUri: >-
      "<url>/auth/realms/<realm_name>/protocol/openid-connect/certs"

解释

  • <url> : 当使用 keycloak 配置时,OIDC 实例的 URL 指定用于身份验证配置的 keycloak OIDC 供应商的元数据端点。
  • <realm_name>: OIDC 中使用的域的名称。
apiVersion: extensions.istio.io/v1alpha1
kind: WasmPlugin
metadata:
  name: <threescale_wasm_plugin_name>
spec:
  ...
  pluginConfig:
    ...
    services:
    - id: '<service_id>'
      authorities:
      - "*"
      credentials:
  app_id:
    - filter:
        path:
          - envoy.filters.http.jwt_authn
          - "0"
        keys:
          - azp
          - aud
        ops:
          - take:
              head: 1
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.