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:
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.
- Namespace:
Deploy the echo-server app
Deploy the
echo-serverapp 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.yamlDeploy 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.yamlEnable-Openapi-Validation annotationThe
enable-openapi-validationannotation 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. Theenable-openapi-validationannotation 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.Deploy a Gateway Resource to Expose the App
Deploy a Tetrate Gateway Resource to expose the application through the gateway:
- tctl
- kubectl (GitOps)
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.yamlcat <<EOF > ${NS}-echo-ingress-plain.yaml
apiVersion: gateway.tsb.tetrate.io/v2
kind: Gateway
metadata:
name: echo-gateway
annotations:
tsb.tetrate.io/organization: my-organization
tsb.tetrate.io/tenant: my-tenant
tsb.tetrate.io/workspace: my-workspace
tsb.tetrate.io/gatewayGroup: my-workspace-gatewaygroup
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
kubectl apply -n my-gitops-namespace -f ${NS}-echo-ingress-plain.yamlVerify 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.
Upload a definition
Upload an OpenAPI definition, using either the UI, API or GitOps interfaces:
- UI
- tctl
- kubectl (GitOps)
Navigate to the Workspace > OpenAPI Payload Validation page and create a new API resource:
Create a new OpenAPI API resourceCopy-and-paste the OpenAPI Spec content from the tctl or GitOps example.
Use tctl to apply a new OpenAPI API resource to the selected Workspace:
cat <<EOF > ${NS}-oas-defn.yaml
apiVersion: api.tsb.tetrate.io/v2
kind: API
metadata:
name: openapi-test
organization: my-organization
tenant: my-tenant
workspace: my-workspace
spec:
openapi: |
openapi: 3.0.0
info:
title: Sample API
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
version: 0.1.9
servers:
- url: http://api.example.com/v1
description: Optional server description, e.g. Main (production) server
- url: http://staging-api.example.com
description: Optional server description, e.g. Internal staging server for testing
paths:
/users/{userId}:
post:
summary: Creates a user.
parameters:
- in: path
name: userId
description: Numeric ID of the user to get
schema:
type: integer
required: true
- in: query
name: filter
content:
application/json:
schema:
type: object
properties:
type:
type: string
color:
type: string
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
username:
type: string
required:
- username
additionalProperties: false
responses:
'201':
description: Created
/messages:
get:
summary: Returns a list of messages.
parameters:
- in: query
name: limit
content:
application/json:
schema:
type: integer
minimum: 1
maximum: 100
responses:
'200':
description: A JSON array of messages
content:
application/json:
schema:
type: array
items:
type: string
post:
summary: Creates a message.
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
destination:
type: string
content:
type: string
ttl:
type: integer
minimum: 0
responses:
'201':
description: Created
EOF
tctl apply -f ${NS}-oas-defn.yamlUse kubectl to apply a new OpenAPI API resource to the selected Workspace:
cat <<EOF > ${NS}-oas-defn.yaml
apiVersion: tsb.tetrate.io/v2
kind: API
metadata:
name: openapi-test
annotations:
tsb.tetrate.io/organization: my-organization
tsb.tetrate.io/tenant: my-tenant
tsb.tetrate.io/workspace: my-workspace
spec:
openapi: |
openapi: 3.0.0
info:
title: Sample API
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
version: 0.1.9
servers:
- url: http://api.example.com/v1
description: Optional server description, e.g. Main (production) server
- url: http://staging-api.example.com
description: Optional server description, e.g. Internal staging server for testing
paths:
/users/{userId}:
post:
summary: Creates a user.
parameters:
- in: path
name: userId
description: Numeric ID of the user to get
schema:
type: integer
required: true
- in: query
name: filter
content:
application/json:
schema:
type: object
properties:
type:
type: string
color:
type: string
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
username:
type: string
required:
- username
additionalProperties: false
responses:
'201':
description: Created
/messages:
get:
summary: Returns a list of messages.
parameters:
- in: query
name: limit
content:
application/json:
schema:
type: integer
minimum: 1
maximum: 100
responses:
'200':
description: A JSON array of messages
content:
application/json:
schema:
type: array
items:
type: string
post:
summary: Creates a message.
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
destination:
type: string
content:
type: string
ttl:
type: integer
minimum: 0
responses:
'201':
description: Created
EOF
kubectl apply -n my-gitops-namespace -f ${NS}-oas-defn.yamlUpdate 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:
- tctl
- kubectl (GitOps)
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.yamlcat <<EOF > ${NS}-echo-ingress-oas.yaml
apiVersion: gateway.tsb.tetrate.io/v2
kind: Gateway
metadata:
name: echo-gateway
annotations:
tsb.tetrate.io/organization: my-organization
tsb.tetrate.io/tenant: my-tenant
tsb.tetrate.io/workspace: my-workspace
tsb.tetrate.io/gatewayGroup: my-workspace-gatewaygroup
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
kubectl apply -n my-gitops-namespace -f ${NS}-echo-ingress-oas.yamlVerify 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,MONITORorDISABLED(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 tofalse, so errors give only the location of the invalid value. See Follow the Gateway Logs. Available from TSB 1.12.16 - enabled: deprecated; use
modeinstead
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,minContainsandmaxContainsaren't enforced.formatis checked as in draft 7, for the formats the validator supports.- Keywords next to a
$refare 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.
patternvalues 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 apatternthat uses them. This keeps the time the gateway spends checking a value proportional to the value's length.examplevalues 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-Typeisn't checked against the media types the operation declares. - A path that declares
GETalso acceptsHEADrequests. - 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
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:
| Mode | Behavior |
|---|---|
ENFORCE | Requests and responses that fail validation are rejected. |
MONITOR | Requests and responses are validated, but those that fail validation still go through. Failures are counted and logged instead. Available from TSB 1.12.16. |
DISABLED | Requests 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: ""
enabled setting is deprecatedFrom 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/statsInclusionRegexpsexports the payload validator counters, such asenvoy_http_<listener>_payload_validator_pv_requests_validation_failed. InMONITORmode, therequests_validation_failedandresponses_validation_failedcounters go up, and the_enforcedcounters stay at 0.sidecar.istio.io/componentLogLevelwrites the reason for each failure to the gateway's log, such asRequest 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.
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.