# OAS Validator

`oas-validator` 插件在将传入的 HTTP 请求转发到上游之前，根据 OpenAPI 3 规范进行验证。它不会验证上游响应。

OpenAPI 3.1 支持自 API7 企业版 3.9.8 和 APISIX 3.17.0 起引入。

## 示例[​](#示例 "示例的直接链接")

以下示例使用固定了镜像清单摘要的官方 `swaggerapi/petstore3:1.0.27` 镜像。Petstore 同时提供 OpenAPI 文档和示例 API，使验证结果可复现。

### 启动 Petstore[​](#启动-petstore "启动 Petstore的直接链接")

请使用与网关部署方式相符的标签页：

* Docker
* Kubernetes

将 `GATEWAY_CONTAINER` 设为运行中的 APISIX 或 API7 Gateway 容器，然后将网关和 Petstore 放在专用网络中：

```
export GATEWAY_CONTAINER=replace-with-gateway-container-name



docker network inspect gateway-petstore-net > /dev/null 2>&1 || \

  docker network create gateway-petstore-net



if ! docker inspect "$GATEWAY_CONTAINER" \

  --format '{{json .NetworkSettings.Networks}}' \

  | grep -q '"gateway-petstore-net"'; then

  docker network connect gateway-petstore-net "$GATEWAY_CONTAINER"

fi
```

创建一个 Compose 文件，使 Petstore 加入网关的共享网络：

docker-compose.petstore.yaml

```
services:

  petstore:

    image: swaggerapi/petstore3:1.0.27@sha256:749432676c31cd05256f0d9e878302164debb94daa1c1223e193747e1b34e7d1

    platform: linux/amd64

    networks:

      - gateway-petstore-net

    ports:

      - "18080:8080"



networks:

  gateway-petstore-net:

    external: true
```

使用固定的 Compose 项目名称启动或更新 Petstore。可以在这些指南中的任意一篇重复运行同一命令：

```
docker compose -p api7-docs-petstore \

  -f docker-compose.petstore.yaml up -d
```

容器启动后，确认 OpenAPI 文档可用：

```
curl -fsS "http://127.0.0.1:18080/api/v3/openapi.json" > /dev/null
```

Petstore 镜像支持 `linux/amd64`。请将 Petstore 部署在与网关相同的命名空间中。清单和命令使用 Ingress Controller 示例的命名空间 `aic`。如果部署方式不同，请将所有 `aic` 替换为网关命名空间。对于 API7 Gateway，该命名空间通常为 `api7`。

petstore.yaml

```
apiVersion: apps/v1

kind: Deployment

metadata:

  namespace: aic

  name: petstore

spec:

  replicas: 1

  selector:

    matchLabels:

      app: petstore

  template:

    metadata:

      labels:

        app: petstore

    spec:

      nodeSelector:

        kubernetes.io/arch: amd64

      containers:

        - name: petstore

          image: swaggerapi/petstore3:1.0.27@sha256:749432676c31cd05256f0d9e878302164debb94daa1c1223e193747e1b34e7d1

          ports:

            - containerPort: 8080

---

apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: petstore

spec:

  selector:

    app: petstore

  ports:

    - port: 8080

      targetPort: 8080
```

应用清单：

```
kubectl apply -f petstore.yaml
```

等待 Petstore 就绪：

```
kubectl rollout status -n aic deployment/petstore
```

Admin API 示例以嵌入方式加载 OpenAPI 文档。如果 Petstore 在 Kubernetes 上运行，请在另一个终端中使该文档可在本地访问：

```
kubectl port-forward -n aic service/petstore 18080:8080
```

对于 Admin API 标签页，请获取文档并对其进行转义，以用于 JSON 请求体：

```
export OPEN_API_SPEC="$(curl -fsS "http://127.0.0.1:18080/api/v3/openapi.json" | jq -Rs .)"
```

ADC 和 Ingress Controller 示例使用 `spec_url`，由网关直接从 Petstore 获取同一文档。

### 验证请求体[​](#验证请求体 "验证请求体的直接链接")

以下示例演示如何根据 OpenAPI 规范验证请求体。

要验证经过 gzip 或 deflate 压缩的请求体，请在同一路由上启用 [`request-decompress`](https://docs.apiseven.com/hub/request-decompress.md)。按默认优先级，它会先运行，因此 `oas-validator` 收到的是解压后的请求体。Request Decompress 自 API7 企业版 3.10.9 和 APISIX 3.20.0 起可用。

创建以下启用 OAS Validator 插件的路由：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d @- <<EOF

  {

    "id": "body_validation",

    "uri": "/*",

    "plugins": {

      "oas-validator": {

        "spec": ${OPEN_API_SPEC}

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "petstore:8080": 1

      },

      "scheme": "http",

      "pass_host": "node"

    }

  }

EOF
```

adc.yaml

```
services:

  - name: petstore

    labels:

      docs-example: oas-validator

    routes:

      - uris:

          - /*

        name: body_validation

        plugins:

          oas-validator:

            spec_url: http://petstore:8080/api/v3/openapi.json

    upstream:

      type: roundrobin

      scheme: http

      pass_host: node

      nodes:

        - host: petstore

          port: 8080

          weight: 1
```

ADC 根据期望状态协调服务。预览此示例负责的变更，并确认差异中没有意外的更新或删除：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

同步已审查的服务配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

* Gateway API
* APISIX CRD

oas-validator-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  type: ExternalName

  externalName: petstore.aic.svc.cluster.local

  ports:

    - port: 8080

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: oas-validator-plugin-config

spec:

  plugins:

    - name: oas-validator

      config:

        spec_url: http://petstore:8080/api/v3/openapi.json

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: body-validation

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: PathPrefix

            value: /

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: oas-validator-plugin-config

      backendRefs:

        - name: petstore-external-domain

          port: 8080

---

apiVersion: apisix.apache.org/v1alpha1

kind: BackendTrafficPolicy

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  targetRefs:

    - group: ""

      kind: Service

      name: petstore-external-domain

  passHost: node

  scheme: http
```

oas-validator-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  ingressClassName: apisix

  scheme: http

  passHost: node

  externalNodes:

  - type: Domain

    name: petstore.aic.svc.cluster.local

    port: 8080

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: body-validation

spec:

  ingressClassName: apisix

  http:

    - name: body-validation

      match:

        paths:

          - /*

      upstreams:

        - name: petstore-external-domain

      plugins:

        - name: oas-validator

          config:

            spec_url: http://petstore:8080/api/v3/openapi.json
```

应用配置：

```
kubectl apply -f oas-validator-ic.yaml
```

#### 验证失败[​](#验证失败 "验证失败的直接链接")

向该路由发送请求体不符合 OpenAPI 规范的请求：

```
curl -i "http://127.0.0.1:9080/api/v3/pet" -X POST \

  -H 'accept: application/json' \

  -H 'Content-Type: application/json' \

  -d '{"name": "doggie"}'
```

你应该会看到 `HTTP/1.1 400 Bad Request` 响应，其响应体类似如下内容：

```
{"message":"failed to validate request."}
```

#### 验证成功[​](#验证成功 "验证成功的直接链接")

向该路由发送请求体有效的请求：

```
curl -i "http://127.0.0.1:9080/api/v3/pet" -X POST \

  -H 'accept: application/json' \

  -H 'Content-Type: application/json' \

  -d '{

    "id": 1,

    "name": "doggie",

    "category": { "id": 1, "name": "Dogs" },

    "photoUrls": ["string"],

    "tags": [{ "id": 1, "name": "tag1" }],

    "status": "available"

  }'
```

你应该会看到 `HTTP/1.1 200 OK` 响应，其响应体类似如下内容：

```
{

  "id": 1,

  "category": { "id": 1, "name": "Dogs" },

  "name": "doggie",

  "photoUrls": ["string"],

  "tags": [{ "id": 1, "name": "tag1" }],

  "status": "available"

}
```

### 获取详细的错误响应[​](#获取详细的错误响应 "获取详细的错误响应的直接链接")

以下示例演示如何在验证失败时返回详细的错误响应。

创建启用 OAS Validator 插件的路由：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d @- <<EOF

  {

    "id": "body_validation",

    "uri": "/*",

    "plugins": {

      "oas-validator": {

        "spec": ${OPEN_API_SPEC},

        "verbose_errors": true

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "petstore:8080": 1

      },

      "scheme": "http",

      "pass_host": "node"

    }

  }

EOF
```

adc.yaml

```
services:

  - name: petstore

    labels:

      docs-example: oas-validator

    routes:

      - uris:

          - /*

        name: body_validation

        plugins:

          oas-validator:

            spec_url: http://petstore:8080/api/v3/openapi.json

            verbose_errors: true

    upstream:

      type: roundrobin

      scheme: http

      pass_host: node

      nodes:

        - host: petstore

          port: 8080

          weight: 1
```

ADC 根据期望状态协调服务。预览此示例负责的变更，并确认差异中没有意外的更新或删除：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

同步已审查的服务配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

* Gateway API
* APISIX CRD

oas-validator-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  type: ExternalName

  externalName: petstore.aic.svc.cluster.local

  ports:

    - port: 8080

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: oas-validator-plugin-config

spec:

  plugins:

    - name: oas-validator

      config:

        spec_url: http://petstore:8080/api/v3/openapi.json

        verbose_errors: true

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: body-validation

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: PathPrefix

            value: /

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: oas-validator-plugin-config

      backendRefs:

        - name: petstore-external-domain

          port: 8080

---

apiVersion: apisix.apache.org/v1alpha1

kind: BackendTrafficPolicy

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  targetRefs:

    - group: ""

      kind: Service

      name: petstore-external-domain

  passHost: node

  scheme: http
```

oas-validator-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  ingressClassName: apisix

  scheme: http

  passHost: node

  externalNodes:

  - type: Domain

    name: petstore.aic.svc.cluster.local

    port: 8080

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: body-validation

spec:

  ingressClassName: apisix

  http:

    - name: body-validation

      match:

        paths:

          - /*

      upstreams:

        - name: petstore-external-domain

      plugins:

        - name: oas-validator

          config:

            spec_url: http://petstore:8080/api/v3/openapi.json

            verbose_errors: true
```

应用配置：

```
kubectl apply -f oas-validator-ic.yaml
```

向上述创建的路由发送请求体无效的请求：

```
curl -i "http://127.0.0.1:9080/api/v3/pet" -X POST \

  -H 'accept: application/json' \

  -H 'Content-Type: application/json' \

  -d '{"name": "doggie"}'
```

你应该会看到 `HTTP/1.1 400 Bad Request` 响应，其响应体类似如下内容：

```
{

  "message": "failed to validate request. body: property \"photoUrls\" is required"

}
```

### 允许不符合规范的请求通过[​](#允许不符合规范的请求通过 "允许不符合规范的请求通过的直接链接")

默认情况下，未通过 OpenAPI 验证的请求会被拒绝。将 `reject_if_not_match` 设为 `false`，可记录验证错误并将请求转发到上游。此选项自 API7 企业版 3.9.6 和 APISIX 3.17.0 起引入。

更新路由，将 `reject_if_not_match` 设为 `false`：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d @- <<EOF

  {

    "id": "body_validation",

    "uri": "/*",

    "plugins": {

      "oas-validator": {

        "spec": ${OPEN_API_SPEC},

        "reject_if_not_match": false

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "petstore:8080": 1

      },

      "scheme": "http",

      "pass_host": "node"

    }

  }

EOF
```

adc.yaml

```
services:

  - name: petstore

    labels:

      docs-example: oas-validator

    routes:

      - uris:

          - /*

        name: body_validation

        plugins:

          oas-validator:

            spec_url: http://petstore:8080/api/v3/openapi.json

            reject_if_not_match: false

    upstream:

      type: roundrobin

      scheme: http

      pass_host: node

      nodes:

        - host: petstore

          port: 8080

          weight: 1
```

ADC 根据期望状态协调服务。预览此示例负责的变更，并确认差异中没有意外的更新或删除：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

同步已审查的服务配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

* Gateway API
* APISIX CRD

oas-validator-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  type: ExternalName

  externalName: petstore.aic.svc.cluster.local

  ports:

    - port: 8080

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: oas-validator-plugin-config

spec:

  plugins:

    - name: oas-validator

      config:

        spec_url: http://petstore:8080/api/v3/openapi.json

        reject_if_not_match: false

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: body-validation

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: PathPrefix

            value: /

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: oas-validator-plugin-config

      backendRefs:

        - name: petstore-external-domain

          port: 8080
```

oas-validator-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  ingressClassName: apisix

  scheme: http

  passHost: node

  externalNodes:

  - type: Domain

    name: petstore.aic.svc.cluster.local

    port: 8080

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: body-validation

spec:

  ingressClassName: apisix

  http:

    - name: body-validation

      match:

        paths:

          - /*

      upstreams:

        - name: petstore-external-domain

      plugins:

        - name: oas-validator

          config:

            spec_url: http://petstore:8080/api/v3/openapi.json

            reject_if_not_match: false
```

应用配置：

```
kubectl apply -f oas-validator-ic.yaml
```

发送请求体无效的请求：

```
curl -i "http://127.0.0.1:9080/api/v3/pet" -X POST \

  -H 'accept: application/json' \

  -H 'Content-Type: application/json' \

  -d '{"id": 101, "photoUrls": []}'
```

OpenAPI 文档要求提供 `name`，但 Petstore 接受缺少该字段的反序列化对象。你应该会收到 `HTTP/1.1 200 OK` 响应，其响应体类似如下内容：

```
{"id":101,"photoUrls":[],"tags":[]}
```

这表明插件转发了不符合规范的请求，而没有拒绝它。

你还应该看到记录请求方法、URI 和验证错误的错误日志，类似如下内容：

```
[error] error occurred while validating request [POST /api/v3/pet], err: ...
```

这样可以审查不符合规范的流量，同时由上游决定接受还是拒绝请求。

### 使用远程 OpenAPI 文档验证请求[​](#使用远程-openapi-文档验证请求 "使用远程 OpenAPI 文档验证请求的直接链接")

以下示例演示如何从远程 URL 加载 OpenAPI 规范。远程规范支持自 API7 企业版 3.9.12 和 APISIX 3.17.0 起引入。

创建从 URL 获取规范的路由：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "url_validation",

    "uri": "/*",

    "plugins": {

      "oas-validator": {

        "spec_url": "http://petstore:8080/api/v3/openapi.json",

        "timeout": 5000

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "petstore:8080": 1

      },

      "scheme": "http",

      "pass_host": "node"

    }

  }'
```

adc.yaml

```
services:

  - name: petstore

    labels:

      docs-example: oas-validator

    routes:

      - name: url-validation

        uris:

          - /*

        plugins:

          oas-validator:

            spec_url: http://petstore:8080/api/v3/openapi.json

            timeout: 5000

    upstream:

      type: roundrobin

      nodes:

        - host: petstore

          port: 8080

          weight: 1

      scheme: http

      pass_host: node
```

ADC 根据期望状态协调服务。预览此示例负责的变更，并确认差异中没有意外的更新或删除：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

同步已审查的服务配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

* Gateway API
* APISIX CRD

oas-validator-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  type: ExternalName

  externalName: petstore.aic.svc.cluster.local

  ports:

    - port: 8080

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: oas-validator-plugin-config

spec:

  plugins:

    - name: oas-validator

      config:

        spec_url: http://petstore:8080/api/v3/openapi.json

        timeout: 5000

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: url-validation

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: PathPrefix

            value: /

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: oas-validator-plugin-config

      backendRefs:

        - name: petstore-external-domain

          port: 8080

---

apiVersion: apisix.apache.org/v1alpha1

kind: BackendTrafficPolicy

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  targetRefs:

    - group: ""

      kind: Service

      name: petstore-external-domain

  passHost: node

  scheme: http
```

oas-validator-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: petstore-external-domain

spec:

  ingressClassName: apisix

  scheme: http

  passHost: node

  externalNodes:

    - type: Domain

      name: petstore.aic.svc.cluster.local

      port: 8080

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: url-validation

spec:

  ingressClassName: apisix

  http:

    - name: url-validation

      match:

        paths:

          - /*

      upstreams:

        - name: petstore-external-domain

      plugins:

        - name: oas-validator

          enable: true

          config:

            spec_url: http://petstore:8080/api/v3/openapi.json

            timeout: 5000
```

应用配置：

```
kubectl apply -f oas-validator-ic.yaml
```

`spec_url` 用于从 URL 加载 OpenAPI 文档。当文档过大而无法嵌入，或需要自动刷新时，请使用该字段。内嵌的 `spec` 字段接受大小不超过 2 MB 的文档。

插件会下载、验证并缓存远程规范。缓存过期后，网关在后台刷新规范期间仍使用缓存中的规范处理请求。

如果没有可用的缓存规范，在以下情况下网关会返回 `500 Internal Server Error`：

* 获取远程规范时收到非 200 响应。
* 由于网络或 TLS 错误无法获取远程规范。
* 获取的内容不是有效的 OpenAPI 文档。

不同产品的默认证书验证行为如下：

| 产品                          | 默认行为                                        |
| ----------------------------- | ----------------------------------------------- |
| APISIX 3.20.0                 | 使用网关共享的受信任 CA 存储验证远程服务器证书  |
| API7 企业版 3.9.23 和 3.10.10 | 默认不验证证书，除非将 `ssl_verify` 设为 `true` |

请按[配置 APISIX 与上游服务之间的 HTTPS](https://docs.apiseven.com/apisix/how-to-guide/traffic-management/tls-and-mtls/configure-upstream-https.md#%E9%AA%8C%E8%AF%81%E4%B8%8A%E6%B8%B8%E8%AF%81%E4%B9%A6)中的步骤配置受信任 CA 存储。

警告

仅在兼容性要求下禁用证书验证。攻击者如果拦截未经验证的获取请求，可能替换规范，并获得随该请求发送的凭据。

如果规范端点需要身份验证，请使用相同的配置结构，并将占位 URL、后端和令牌替换为你环境中的值：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "url_validation",

    "uri": "/*",

    "plugins": {

      "oas-validator": {

        "spec_url": "https://internal-api.example.com/openapi.json",

        "spec_url_request_headers": { "Authorization": "Bearer <token>" },

        "timeout": 5000

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "internal-api.example.com:443": 1

      },

      "scheme": "https",

      "pass_host": "node"

    }

  }'
```

adc.yaml

```
services:

  - name: internal-api

    labels:

      docs-example: oas-validator

    routes:

      - name: url-validation

        uris:

          - /*

        plugins:

          oas-validator:

            spec_url: https://internal-api.example.com/openapi.json

            spec_url_request_headers:

              Authorization: Bearer <token>

            timeout: 5000

    upstream:

      type: roundrobin

      nodes:

        - host: internal-api.example.com

          port: 443

          weight: 1

      scheme: https

      pass_host: node
```

ADC 根据期望状态协调服务。预览此示例负责的变更，并确认差异中没有意外的更新或删除：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

同步已审查的服务配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oas-validator
```

* Gateway API
* APISIX CRD

oas-validator-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: internal-api-external-domain

spec:

  type: ExternalName

  externalName: internal-api.example.com

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: oas-validator-plugin-config

spec:

  plugins:

    - name: oas-validator

      config:

        spec_url: https://internal-api.example.com/openapi.json

        spec_url_request_headers:

          Authorization: Bearer <token>

        timeout: 5000

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: url-validation

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: PathPrefix

            value: /

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: oas-validator-plugin-config

      backendRefs:

        - name: internal-api-external-domain

          port: 443

---

apiVersion: apisix.apache.org/v1alpha1

kind: BackendTrafficPolicy

metadata:

  namespace: aic

  name: internal-api-external-domain

spec:

  targetRefs:

    - group: ""

      kind: Service

      name: internal-api-external-domain

  passHost: node

  scheme: https
```

oas-validator-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: internal-api-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

    - type: Domain

      name: internal-api.example.com

      port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: url-validation

spec:

  ingressClassName: apisix

  http:

    - name: url-validation

      match:

        paths:

          - /*

      upstreams:

        - name: internal-api-external-domain

      plugins:

        - name: oas-validator

          enable: true

          config:

            spec_url: https://internal-api.example.com/openapi.json

            spec_url_request_headers:

              Authorization: Bearer <token>

            timeout: 5000
```

应用配置：

```
kubectl apply -f oas-validator-ic.yaml
```

要配置所获取规范的缓存 TTL（默认为 3600 秒），请设置插件元数据：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/plugin_metadata/oas-validator" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "spec_url_ttl": 1800

  }'
```

插件元数据是全局集合，无法使用标签选择器隔离单个条目。修改 OAS Validator 条目前，请导出完整集合：

```
adc dump -o adc-metadata.yaml --with-id \

  --include-resource-type plugin_metadata
```

在导出的文件中添加或更新 `oas-validator`，并保留其他所有条目：

adc-metadata.yaml (plugin\_metadata excerpt)

```
plugin_metadata:

  # 保留导出文件中的其他所有插件元数据条目。

  oas-validator:

    spec_url_ttl: 1800
```

预览完整的元数据变更，确认没有意外的更新或删除：

```
adc diff -f adc-metadata.yaml \

  --include-resource-type plugin_metadata
```

同步已审查的插件元数据集合：

```
adc sync -f adc-metadata.yaml \

  --include-resource-type plugin_metadata
```

在当前部署使用的完整 `GatewayProxy` 清单中，将以下条目添加到 `spec.pluginMetadata` 下：

```
oas-validator:

  spec_url_ttl: 1800
```

通过当前部署的常规 Kubernetes 或 GitOps 工作流应用更新后的完整清单。
