Skip to main content
logoTetrate Service BridgeVersion: 1.12.x

Validating API calls with OpenAPI

When Tetrate Gateways deliver RESTFul and other APIs, they function as an API Gateway. API delivery often relies on three specific capabilities:

  • Authentication Offload, generally using JWT Token validation or OIDC flows, relieving the API service of the burden of implementing and managing authentication;
  • Rate Limiting, controlling the volume of requests to an API instance, based on a global limit or defined by various dimensions, such as an access token;
  • Payload Validation, evaluating each API request against an OpenAPI Specification, to verify compliance and block potential attempts to abuse the API

This document concentrates on the third capability, OpenAPI Specification-based payload validation:

  • OpenAPI Spec definitions are added to a Tetrate Workspace using either the TSB UI or the TSB API (tctl, GitOps)
  • Gateway performs validation according to the openapi settings in the Gateway resource
  • Requests are admitted or blocked depending on whether or not they comply with the OpenAPI Spec definition

Before you begin - a demo service​

To illustrate OAS Validation, the workflow in this document will use the jmalloc/echo-server application. This application responds to every HTTP request, echoing back the request it was given, and is an ideal base with which to test API traffic:

  1. Create the environment​

    Select or create a namespace in your TSB-managed Workload cluster to use for testing. This namespace must be managed by TSB, so it must be included in a TSB workspace and in a Gateway Group for that workspace.

    In the examples below, we use:

    • Namespace: my-namespace (set as ${NS})
    • Workspace: my-workspace
    • Gateway Group: my-workspace-gatewaygroup
    • Tenant: my-tenant
    • Organization: my-organization

    Edit the examples to match your environment.

  2. Deploy the echo-server app​

    Deploy the echo-server app to the namespace in the TSB workload cluster, for example, as follows:

    NS=my-namespace

    cat <<EOF > ${NS}-echo-server.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: echo
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: echo-server
    template:
    metadata:
    labels:
    app: echo-server
    spec:
    containers:
    - name: echo-server
    image: jmalloc/echo-server
    ports:
    - name: http-port
    containerPort: 8080
    ---
    apiVersion: v1
    kind: Service
    metadata:
    name: echo-service
    spec:
    ports:
    - name: http-port
    port: 80
    targetPort: http-port
    protocol: TCP
    selector:
    app: echo-server
    EOF

    kubectl apply -n ${NS} -f ${NS}-echo-server.yaml
  3. Deploy a Tetrate gateway​

    Deploy a Tetrate gateway to the same namespace; this action deploys an Envoy instance within the namespace. Note the annotation needed to enable the OpenAPI validation feature:

    cat <<EOF > ${NS}-ingress-gw.yaml
    apiVersion: install.tetrate.io/v1alpha1
    kind: Gateway
    metadata:
    name: ${NS}-ingress-gw
    annotations:
    install.tetrate.io/enable-openapi-validation: "true"
    spec:
    type: UNIFIED
    kubeSpec:
    service:
    ports:
    - port: 8080
    name: http-8080
    EOF

    kubectl apply -n ${NS} -f ${NS}-ingress-gw.yaml
    Enable-Openapi-Validation annotation

    The enable-openapi-validation annotation is necessary for this use case. The default Envoy gateway deployed by Tetrate does not include the additional functionality to parse and validate API request against an OAS definition; the annotation instructs the Tetrate platform to deploy an extended Envoy gateway.

    From TSB 1.12.16, you can also use the install.tetrate.io/enable-tetrate-proxy: "true" annotation to deploy the extended Envoy gateway. The enable-openapi-validation annotation keeps working.

    From TSB 1.12.16, the extended gateway image (proxyv2:<tag>-pv-distroless) is pulled from the same registry as your other gateway proxies. If you pull proxy images from your own registry, make sure that this image is also there.

  4. Deploy a Gateway Resource to Expose the App​

    Deploy a Tetrate Gateway Resource to expose the application through the gateway:

    cat <<EOF > ${NS}-echo-ingress-plain.yaml
    apiVersion: gateway.tsb.tetrate.io/v2
    kind: Gateway
    metadata:
    organization: my-organization
    tenant: my-tenant
    workspace: my-workspace
    group: my-workspace-gatewaygroup
    name: echo-gateway
    spec:
    workloadSelector:
    namespace: ${NS}
    labels:
    app: ${NS}-ingress-gw
    http:
    - name: echo
    port: 8080
    hostname: "echo.tetrate.io"
    routing:
    rules:
    - route:
    serviceDestination:
    host: "${NS}/echo-service.${NS}.svc.cluster.local"
    port: 80
    EOF

    tctl apply -f ${NS}-echo-ingress-plain.yaml
  5. Verify you can access the App​

    GW_IP=`kubectl get svc -n ${NS} ${NS}-ingress-gw -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}"`
    GW_ADDRESS=${GW_IP}:8080

    echo GW_ADDRESS is $GW_ADDRESS

    curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/users/123 -d '{"username":"john"}'

Configure OpenAPI Payload Validation​

We will upload an OAS definition to the Tetrate workspace, and then configure our Gateway Resource to perform OpenAPI Payload Validation.

  1. Upload a definition​

    Upload an OpenAPI definition, using either the UI, API or GitOps interfaces:

    Navigate to the Workspace > OpenAPI Payload Validation page and create a new API resource:

    Create a new OpenAPI API resource Create a new OpenAPI API resource

    Copy-and-paste the OpenAPI Spec content from the tctl or GitOps example.

  2. Update the Gateway resource to enable OAS Payload Validation​

    Update the Gateway resource, adding the openapi stanza. Check the organization, tenant and workspace names in the fdn:

    cat <<EOF > ${NS}-echo-ingress-oas.yaml
    apiVersion: gateway.tsb.tetrate.io/v2
    kind: Gateway
    metadata:
    organization: my-organization
    tenant: my-tenant
    workspace: my-workspace
    group: my-workspace-gatewaygroup
    name: echo-gateway
    spec:
    workloadSelector:
    namespace: ${NS}
    labels:
    app: ${NS}-ingress-gw
    http:
    - name: echo
    port: 8080
    hostname: "echo.tetrate.io"
    routing:
    rules:
    - route:
    serviceDestination:
    host: "${NS}/echo-service.${NS}.svc.cluster.local"
    port: 80
    openapi:
    fqn: organizations/my-organization/tenants/my-tenant/workspaces/${NS}-ws/apis/openapi-test
    validation:
    mode: ENFORCE
    pathPrefix: ""
    EOF

    tctl apply -f ${NS}-echo-ingress-oas.yaml
  3. Verify you can access the App​

    Finally, verify once again that you can access the app:

    GW_IP=`kubectl get svc -n ${NS} ${NS}-ingress-gw -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}"`
    GW_ADDRESS=${GW_IP}:8080

    echo GW_ADDRESS is $GW_ADDRESS

    curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/users/123 -d '{"username":"john"}'

Validation Settings​

OAS Validation is configured as follows:

openapi:
fqn: organizations/my-organization/tenants/my-tenant/workspaces/${NS}-ws/apis/openapi-test
validation:
mode: ENFORCE
pathPrefix: ""
logValues: false
  • fqn: the 'fully-qualified name' identifies the OAS definition in the TSB hierarchy. A Gateway can access any OAS definitions that are published to the same parent Workspace
  • mode: ENFORCE, MONITOR or DISABLED (the default). See Choose a validation mode. Available from TSB 1.12.16
  • pathPrefix: the value, such as /api/v1, is stripped from the client-provided URL before the URL is matched against the OAS definition; this allows you to expose APIs under specific paths
  • logValues: when set to true, validation errors in the gateway logs include the invalid request body and parameter values, and the reason they failed. Defaults to false, so errors give only the location of the invalid value. See Follow the Gateway Logs. Available from TSB 1.12.16
  • enabled: deprecated; use mode instead

The API documentation is described in more detail in the Reference Guide.

Supported OpenAPI Features and Limits​

From TSB 1.12.16, OpenAPI payload validation works as follows.

OAS definitions

  • OpenAPI 3.0, 3.1 and 3.2 definitions are supported. OpenAPI 3.1 and 3.2 schemas use JSON Schema 2020-12, which the gateway validates as JSON Schema draft 7. As a result:
    • unevaluatedProperties, unevaluatedItems, minContains and maxContains aren't enforced.
    • format is checked as in draft 7, for the formats the validator supports.
    • Keywords next to a $ref are ignored, as in OpenAPI 3.0. They aren't combined with the referenced schema, as OpenAPI 3.1 specifies.
  • TSB checks an OAS definition when you create or update the API. If the gateway can't use the definition for validation, TSB refuses it and gives the reason. If an existing API's definition can't be used, you must fix the definition before you can update the API.
  • pattern values are regular expressions in RE2 syntax, not the ECMA-262 syntax that the OpenAPI specification uses. RE2 doesn't support lookahead or lookbehind, so TSB refuses a definition with a pattern that uses them. This keeps the time the gateway spends checking a value proportional to the value's length.
  • example values aren't checked against their schemas, because they don't affect validation.

Requests

  • Path, query, header and cookie parameters are validated.
  • Query parameters that the OAS definition doesn't declare are allowed.
  • Request bodies are validated against the operation's JSON media types. Bodies of other media types, such as multipart/form-data, pass through without validation.
  • The request's Content-Type isn't checked against the media types the operation declares.
  • A path that declares GET also accepts HEAD requests.
  • Request and response bodies larger than 10 MB are rejected, with a 413 for a request and a 500 for a response.
  • Validation runs after authentication and authorization. A request that fails authentication or authorization gets a 401 or 403 before it is validated.

Test Validation​

You can test validation against the OAS spec used above, with requests resembling the following:

echo; echo The following requests will work
curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/users/123 -d '{"username":"john"}' | head -1
curl -s -D - -X GET -H "Host: echo.tetrate.io" http://$GW_ADDRESS/messages | head -1
curl -s -D - -X GET -H "Host: echo.tetrate.io" http://$GW_ADDRESS/messages\?limit=10 | head -1

echo; echo These requests will return 403 Forbidden as they do not match a path in the spec
curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/u-sers/123 -d '{"username":"john"}' | head -1
curl -s -D - -X GET -H "Host: echo.tetrate.io" http://$GW_ADDRESS/m-essages | head -1
curl -s -D - -X GET -H "Host: echo.tetrate.io" http://$GW_ADDRESS/m-essages\?limit=10 | head -1

echo; echo These requests will return 422 Unprocessable Entity as they fail validation in the spec
curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/users/123 | head -1
curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/users/123 -d '{"username":123}' | head -1
curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/users/123 -d '{}' | head -1
curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/users/123 -d '{"username":"john","name":"john"}' | head -1
curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/users/john -d '{"username":"john"}' | head -1
curl -s -D - -X GET -H "Host: echo.tetrate.io" http://$GW_ADDRESS/messages\?limit=0 | head -1
curl -s -D - -X GET -H "Host: echo.tetrate.io" http://$GW_ADDRESS/messages\?limit=101 | head -1

Follow the Gateway Logs​

Follow the gateway logs:

kubectl logs -f -n ${NS} -l app=${NS}-ingress-gw

Whenever a request is refused (fails validation), the response is logged with either a 403 (request did not match a path in the spec) or 422 (payload failed validation) status code. If the gateway also enforces authentication or authorization, a 401 or 403 can come from those checks, which run before validation. The access log entry includes the reason the request failed validation.

For example, this request includes a name property that the spec doesn't allow:

curl -s -D - -X POST -H "Host: echo.tetrate.io" http://$GW_ADDRESS/users/123 -d '{"username":"john","name":"john"}' | head -1

By default, the reason gives only the location of the invalid value, and not the value itself:

[2026-10-28T10:55:12.000Z] "POST /users/123 HTTP/1.1" 422 - "Body does not match schema: Payload does not match the schema: At root" - "-" 33 25 0 - "172.20.29.165" "curl/7.81.0" "5d2a5d5d-c04d-451c-93ee-f4066ac0518b" "echo.tetrate.io" "-" outbound|80||echo-service.echo-test.svc.cluster.local; - 172.20.19.199:8080 172.20.29.165:53610 - echo-gateway-echo-external-0

From TSB 1.12.16, you can set logValues: true in the validation settings to include the invalid values and the reason they failed:

[2026-10-28T10:55:55.878Z] "POST /users/123 HTTP/1.1" 422 - "Body does not match schema: Payload does not match the schema: At root of {\"name\":\"john\",\"username\":\"john\"} - validation failed for additional property 'name': instance invalid as per false-schema" - "-" 33 25 0 - "172.20.29.165" "curl/7.81.0" "f859a3c1-8136-4691-8b88-08f0d3b128da" "echo.tetrate.io" "-" outbound|80||echo-service.echo-test.svc.cluster.local; - 172.20.19.199:8080 172.20.29.165:58777 - echo-gateway-echo-external-0
Sensitive data in logs

With logValues: true, the gateway logs request body and parameter values, which may include personal or other sensitive data. Leave it unset in production unless your logging policy allows this.

The response returned to the client gives the status code, but not the reason the request failed validation.

Choose a validation mode​

The mode setting controls how the gateway validates requests and responses against the OAS definition:

ModeBehavior
ENFORCERequests and responses that fail validation are rejected.
MONITORRequests and responses are validated, but those that fail validation still go through. Failures are counted and logged instead. Available from TSB 1.12.16.
DISABLEDRequests and responses aren't validated. This is the default.
openapi:
fqn: organizations/my-organization/tenants/my-tenant/workspaces/${NS}-ws/apis/openapi-test
validation:
mode: ENFORCE # or MONITOR, or DISABLED
pathPrefix: ""
The enabled setting is deprecated

From TSB 1.12.16, mode replaces the enabled setting. If mode is DISABLED or unset, enabled: true still enforces validation, so existing configurations keep working.

Monitor validation without rejecting requests​

Use MONITOR mode to see how much real traffic an OAS definition would reject before you enforce it. In this mode, everything that would be rejected goes through, including:

  • Requests and responses with invalid bodies or parameters
  • Requests for paths and methods that aren't in the OAS definition
  • Bodies larger than the size limit, which would otherwise get a 413 (request) or 500 (response)

By default, the gateway doesn't show the failure counts or reasons. To see them, add these annotations to the gateway pods, using the install.tetrate.io Gateway resource:

apiVersion: install.tetrate.io/v1alpha1
kind: Gateway
metadata:
name: ${NS}-ingress-gw
annotations:
install.tetrate.io/enable-openapi-validation: "true"
spec:
type: UNIFIED
kubeSpec:
deployment:
podAnnotations:
sidecar.istio.io/statsInclusionRegexps: ".*payload_validator.*"
sidecar.istio.io/componentLogLevel: "payload_validator:info"
service:
ports:
- port: 8080
name: http-8080
  • sidecar.istio.io/statsInclusionRegexps exports the payload validator counters, such as envoy_http_<listener>_payload_validator_pv_requests_validation_failed. In MONITOR mode, the requests_validation_failed and responses_validation_failed counters go up, and the _enforced counters stay at 0.
  • sidecar.istio.io/componentLogLevel writes the reason for each failure to the gateway's log, such as Request validation failed: path /foo is not allowed. It writes one line for each failing request, which can be a lot on a busy API, and only the first failure of a request is logged.

In MONITOR mode, the reason isn't in the access log, because the access log only includes it when the gateway rejects the request.

note

Gateways in clusters whose control plane is older than TSB 1.12.16 don't validate requests in MONITOR mode.

Update the OAS definition​

If you update or edit the OAS definition, the Gateway will reload the new definition and log the parsed version.

For example, in the example above, you can change the parameters to validate the limit value, allowing requests such as http://$GW_ADDRESS/messages\?limit=101.