Introduction
The Tyk OAS APIs that Tyk Operator creates from anHTTPRoute contain only routing configuration. They have no authentication, so any client that can reach the Tyk Gateway can call them.
To secure a route and add other Tyk features:
- Attach a Tyk OAS API definition to the route, or to its
Gateway. The definition adds settings such as authentication, rate limits and middleware. - Give clients access with a Policy that references the route.
Attach a Tyk OAS API Definition
How Attachment Works
You write a Tyk OAS API definition in a ConfigMap, and create aTykOasApiDefinition resource that refers to it. The TykOasApiDefinition doesn’t create an API. Instead, it targets an HTTPRoute or a Gateway with spec.targetRefs.
Tyk Operator then combines the two for each API that it creates from the route:
- The route sets the routing configuration: listen path, custom domain, upstream services and Gateway tags.
- The attached definition sets everything else, such as authentication, rate limits and middleware.
- an
HTTPRoute, to apply it to the APIs of that route - a
Gateway, to apply it to the APIs of every route on thatGatewaythat has no definition attached directly
Gateway. Tyk Operator doesn’t merge them. If several definitions target the same route or Gateway, the one with the oldest creationTimestamp applies. If they have the same timestamp, Tyk Operator uses the first in alphabetical order of namespace and name.
Example
This ConfigMap holds a Tyk OAS API definition that requires an auth token:TykOasApiDefinition attaches the definition to the orders route:
orders route requires an auth token.
Rules for Attached Definitions
Settings That Come from the Route
The route sets these settings, so the attached definition can’t set them. If it does, Tyk Operator rejects the definition and lists all of the settings in one message:x-tyk-api-gateway.server.listenPathx-tyk-api-gateway.server.customDomainx-tyk-api-gateway.server.gatewayTagsx-tyk-api-gateway.server.portx-tyk-api-gateway.server.protocolx-tyk-api-gateway.upstream.urlx-tyk-api-gateway.upstream.loadBalancingx-tyk-api-gateway.upstream.serviceDiscoveryx-tyk-api-gateway.upstream.proxyx-tyk-api-gateway.upstream.uptimeTestsx-tyk-api-gateway.info.versioningx-tyk-api-gateway.info.state.internalallowandinternalinx-tyk-api-gateway.middleware.operations
TykOasApiDefinition. It checks them again whenever it combines the definition with a route, and reports the result in the status of the route and the TykOasApiDefinition.
Conflicts with the Route
Some attached settings can’t work with some routes. Tyk Operator reports the route with the reasonAttachmentConflict if:
- the definition contains CORS settings, even if CORS is disabled, and the route matches the
OPTIONSmethod - the definition sets
transformRequestMethod, and the route matches by method - the definition sets
paths, and the route has more than one path, so it becomes more than one API
Attachment Status
Tyk Operator reports the result of an attachment in theAccepted condition of the TykOasApiDefinition, and reports problems with the attached definition in the status of the route. For the reasons and their meanings, see TykOasApiDefinition Conditions and HTTPRoute Conditions.
Give Clients Access with a Policy
To give clients access to a route, reference the route in theaccess_rights_array of a SecurityPolicy, with kind: HTTPRoute:
HTTPRoute entry covers every API that Tyk Operator creates from the route. Tyk Operator finds the API IDs for you, and copies the other settings of the entry, such as versions, limit and allowed_urls, to each API. It updates the Policy when the route gains or loses APIs, for example when you add a hostname or a path. Don’t set api_id on an HTTPRoute entry.
The Policy includes only the APIs that Tyk Operator creates in the same Tyk Dashboard and Organisation as the Policy. Tyk Operator can’t apply the Policy until the route has created its APIs, or if none of them are in that Tyk Dashboard. It reports the problem as an HTTPRouteAccessRejected event on the Policy.
You must enable Kubernetes Gateway API support in Tyk Operator. Otherwise, Tyk Operator rejects the Policy with the message access rights of kind HTTPRoute require the operator to run with --enable-gateway-api.
A Policy controls which clients can call an API, so it has an effect only if the API requires authentication. Attach a definition that enables authentication to the route, as described in Attach a Tyk OAS API Definition.