Skip to main content
Version: v0.15

Middleware

A Middleware is a reusable request/response processor — authentication, rate limiting, header rewriting, redirects, and more. A Middleware resource is referenced by name from one or more Routes (spec.middlewares) or attached to monitoring endpoints from a Gateway.

  • API group: gateway.jkaninda.dev
  • Version: v1alpha1
  • Kind: Middleware

The spec.rule field is a free-form object whose shape depends on spec.type. The sections below document each supported type.

Supported types

TypePurpose
basicAuthHTTP basic authentication.
jwtAuthJWT validation against a JWKS endpoint.
oauthOAuth 2.0 authentication flow.
forwardAuthDelegate authn/authz to an external HTTP service.
ldapAuthLDAP authentication.
rateLimitPer-IP / per-route rate limiting.
accessAllow / deny rules by IP, header, etc.
accessPolicyFine-grained access policies.
addPrefixPrepend a prefix to the request path.
redirectRegexRegex-based redirect.
rewriteRegexRegex-based path rewrite.
redirectSchemeForce HTTP → HTTPS redirects.
httpCacheHTTP response cache.
bodyLimitLimit request body size.
responseHeadersAdd / remove response headers.
errorInterceptorMap upstream error codes to custom responses.
userAgentBlockBlock requests by User-Agent pattern.
geoBlockAllow / deny requests by country (GeoIP).

Basic auth

Passwords must be bcrypt-hashed. Generate with:

htpasswd -nbB admin 's3cret'
apiVersion: gateway.jkaninda.dev/v1alpha1
kind: Middleware
metadata:
name: admin-basic-auth
spec:
type: basicAuth
paths:
- /admin
rule:
realm: admin
users:
- "admin:$2a$12$EXAMPLEHASHREPLACEME.................."

Rate limiting

Per-IP rate limiter. When the parent Gateway is configured with a Redis backend, counters are shared across replicas.

apiVersion: gateway.jkaninda.dev/v1alpha1
kind: Middleware
metadata:
name: api-rate-limit
spec:
type: rateLimit
rule:
requestsPerUnit: 100
unit: minute # second | minute | hour
burst: 20

JWT authentication

Validate tokens against a remote JWKS endpoint (Auth0, Keycloak, Okta, etc.).

apiVersion: gateway.jkaninda.dev/v1alpha1
kind: Middleware
metadata:
name: api-jwt
spec:
type: jwtAuth
rule:
jwksUrl: https://auth.example.com/.well-known/jwks.json
issuer: https://auth.example.com/
audience: api.example.com
algorithms:
- RS256
forwardHeaders:
X-User-Id: sub
X-User-Email: email

Optional claimsExpression lets you assert claim values:

spec:
type: jwtAuth
rule:
jwksUrl: https://auth.example.com/.well-known/jwks.json
issuer: https://auth.example.com/
audience: api.example.com
algorithms:
- RS256
claimsExpression: >
Equals('email_verified', true) && !Equals('account_disabled', true)
forwardHeaders:
X-User-ID: sub
X-User-Email: email

Forward auth

Delegate authentication and authorization to an external HTTP endpoint. The gateway sends a subrequest to authUrl — a 2xx response allows the request through, anything else is returned to the client.

apiVersion: gateway.jkaninda.dev/v1alpha1
kind: Middleware
metadata:
name: forward-auth
spec:
type: forwardAuth
rule:
authUrl: http://auth.default.svc.cluster.local:8080/verify
authSignIn: https://app.example.com/login
trustForwardHeader: true
authResponseHeaders:
- X-User-Id
- X-User-Roles

Attaching to routes

Reference middlewares by name from a Route:

apiVersion: gateway.jkaninda.dev/v1alpha1
kind: Route
metadata:
name: api
spec:
gateways:
- gateway
path: /
hosts:
- api.example.com
target: http://api.default.svc.cluster.local:8080
middlewares:
- api-jwt
- api-rate-limit

The order matters — middlewares run in the order they appear in spec.middlewares.

Path scoping

spec.paths constrains the middleware to a subset of paths within the route it's attached to. Use it for fine-grained protection:

spec:
type: basic
paths:
- /admin # exact
- /admin/.* # subpaths
rule:
realm: admin
users:
- "admin:$2a$12$EXAMPLEHASHREPLACEME.................."

Spec reference

FieldTypeDescription
typeenumRequired. Middleware type (see Supported types).
paths[]stringPaths within the attached route to apply this middleware to.
ruleobjectType-specific configuration. Schema depends on type.

rule is preserved as-is by the API server (x-kubernetes-preserve-unknown-fields), so any field accepted by the corresponding gateway middleware can be set here.

Status

kubectl get middlewares
NAME TYPE READY AGE
admin-basic-auth basic true 3m
api-jwt jwtAuth true 3m
api-rate-limit rateLimit true 3m

status.referencedBy lists Routes that consume the middleware. Ready: true means the rule is well-formed and synced.