11.6. 创建 WasmPlugin 自定义资源
Service Mesh 提供了一个自定义资源定义 (CRD) 来指定并应用 Proxy-WASM 扩展到 sidecar 代理,称为 WasmPlugin。Service Mesh 将自定义资源 (CR) 应用到需要使用 3scale 管理 HTTP API 的工作负载集合。
流程
- 在您的服务网格部署中识别要将其模块部署到的 OpenShift Container Platform (OCP) 命名空间,如 info 项目。
使用 registry.redhat.io 凭证获取 pull secret。
-
在与
WasmPlugin相同的命名空间中创建新的 pull secret 资源。
-
在与
您必须声明部署
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字段因应用程序而异。所有其他字段在该自定义资源的多个实例之间都存在。 -
此特定的
WasmPluginspec.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-host,system-host: 使用将 3scale URL 添加到 Service Mesh 中使用的相同主机。 -
system-url,backend-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 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
除了 WasmPlugin 本身外,为了使 OpenID Connect (OIDC) 可以正常工作,您还需要额外的名为 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