x-tyk-api-gateway
section should be present.
oas-api-definition.json
) locally.
tyk-oas-api-config
in the tyk
namespace (replace tyk
with your actual namespace if different).
kubectl apply
command, you may get an error that the annotation metadata cannot exceed 256KB. It is because by using kubectl apply
, kubectl
automatically saves the whole configuration in the annotation kubectl.kubernetes.io/last-applied-configuration for tracking changes. Your Tyk OAS API Definition may easily exceed the size limit of annotations (256KB). Therefore, kubectl create
is used here to get around the problem.TykOasApiDefinition
resource to tell the Tyk Operator to use the Tyk OAS API definition stored in the ConfigMap.
Create a manifest file named tyk-oas-api-definition.yaml
with the following content:
kubectl
to apply the TykOasApiDefinition
manifest to your cluster:
TykOasApiDefinition
resource in your cluster. The Tyk Operator will watch for this resource and configures Tyk Gateway or Tyk Dashboard with a new API using the provided Tyk OAS API definition.
/store/inventory"
, you can use curl
or any API client to test it:
kubectl replace
:
kubectl replace
without --save-config
option is used here instead of kubectl apply
because we do not want to save the Tyk OAS API definition in its annotation. If you want to enable --save-config
option or use kubectl apply
, the Tyk OAS API definition size would be further limited to at most 262144 bytes.ConfigMap
is created that contains the Tyk OAS API Definition with the data
field with key test_oas.json
. This is linked to from a TykOasApiDefinition
resource via spec.tykOAS.configmapRef
.
To apply it, simply save the manifest into a file (e.g., tyk-oas-api.yaml
) and use kubectl apply -f tyk-oas-api.yaml
to create the required resources in your Kubernetes cluster. This command will create the necessary ConfigMap and TykOasApiDefinition resources in the default
namespace.
oas-api-definition.json
and created a ConfigMap named tyk-oas-api-config
in the tyk
namespace.
Modify your Tyk OAS API Definition oas-api-definition.json
as follow.
components.securitySchemes
defines the authentication method (in this case, apiKey
in the header).security
: Applies the authentication globally to all endpoints.x-tyk-api-gateway.server.authentication
: Tyk-specific extension to enable the authentication scheme.oas-api-definition.json
.
tyk-oas-api-config
in the tyk
namespace (replace tyk
with your actual namespace if different) with the new Tyk OAS API Definition stored in oas-api-definition.json
.
Since a TykOasApiDefinition
resource has been created with reference to this ConfigMap in the previous tutorial:
TykOasApiDefinition
has been updated successfully:
latestTransaction
field in status
:
time
field.
/store/inventory"
, you can use curl
or any API client to test it:
401 Unauthorized
response now as an API key is required for access. Your API has been secured by Tyk Gateway.
httpbin.yaml
, then add the following:
httpbin
, as specified in spec.name
field, which listens to path /httpbin
and proxies requests to http://httpbin.org, as specified under spec.proxy
field. Now, any requests coming to the /httpbin
endpoint will be proxied to the target URL that we defined in spec.proxy.target_url
, which is http://httpbin.org in our example.
You can visit the ApiDefinition CRD page to see all the latest API Definitions fields and features we support.
proxy.target_url
as a Kubernetes Service following DNS for Services and Pods guideline, so that the requests will be proxied to your service.
In general, Kubernetes Services have a <service-name>.<namespace-name>
.svc.cluster.local DNS entry once they are created.
For example, if you have a service called httpbin
in default
namespace, you can contact httpbin
service with httpbin.default.svc
DNS record in the cluster, instead of IP addresses.
Please visit the official Kubernetes documentation for more details.
Suppose you want to create a Deployment of httpbin service using ci/upstreams/httpbin.yaml file. You are going to expose the application through port 8000
as described under the Service specification.
You can create Service and Deployment by either applying the manifest defined in our repository:
1/1
and STATUS Running
state.
Once the pod is ready, you can update your httpbin
API’s target_url
field to proxy your requests to the Service that you created above.
You can check all services in the <ns>
namespace as follows:
httpbin
as follows:
spec.proxy.target_url
field.
It is set to http://httpbin.default.svc:8000
by following the convention described above (<service_name>.<namespace>.svc:<service_port>
).
Now, if you send your request to the /httpbin
endpoint of the Tyk Gateway, the request will be proxied to the httpbin Service
:
httpbin.default.svc:8000
.
httpbin
API is keyless. If you check the APIDefinition’s specification, the use_keyless
field is set to true
.
Tyk keyless access represents completely open access for your API and causes Tyk to bypass any session-based middleware (middleware that requires access to token-related metadata). Keyless access will enable all requests through.
You can disable keyless access by setting use_keyless
to false.
httpbin.yaml
fileuse_keyless
to false, the default authentication mode is Authentication token.
Now, to access httpbin
API, you need to include a key to the header. Otherwise, you will get a HTTP 401 Unauthorized
response.
httpbin
API now. Follow this guide to see how to create an API key for your installation.
You can obtain the API name and API ID of our example httpbin
API by following command:
name
and status.api_id
field.
In our example, it is as follows:
httpbin
API.
HTTP 401 Unauthorized
response.
Authorization
header of the request. You can customize this behavior by configuring the following fields:
use_cookie
: Set to true to use a cookie value for the token.cookie_name
: Specify the name of the cookie if use_cookie is enabled.use_param
: Set to true to allow the token to be passed as a query parameter.param_name
: Specify the parameter name if use_param is enabled.use_certificate
: Enable client certificate. This allows you to create dynamic keys based on certificates.validate_signature
: Enable signature validation.jwt_signing_method
: Specify the method used to sign the JWT. Refer to the documentation on JWT Signatures for supported methods.jwt_source
: Specify the public key used for verifying the JWT.jwt_identity_base_field
: Define the identity source, typically set to sub
(subject), which uniquely identifies the user or entity.jwt_policy_field_name
: Specify the claim within the JWT payload that indicates the policy ID to apply.jwt_default_policies
(Optional): Define default policies to apply if no policy claim is found in the JWT payload.sub
claim as the identity source, uses the pol
claim for policy ID, and assigns a default policy (jwt-policy
SecurityPolicy in default
namespace) if no policy is specified in the token.
httpbin-go-auth
.
For an example of Golang authentication middleware, see Performing custom authentication with a Golang plugin.
httpbin-grpc-auth
.
For a detailed walkthrough on setting up Tyk with gRPC authentication plugins, refer to Extending Tyk with gRPC Authentication Plugins.
base_identity_provided_by
field to one of the supported chained enums. Consult the Multi (Chained) Authentication section for the supported auths.
In this example, we are creating an API definition with basic authentication and mTLS with basic authentication as base identity for httpbin-multiple-authentications
.
enable_ip_whitelisting
: Enables IPs allowlist. When set to true
, only requests coming from the explicit list of IP addresses defined in (allowed_ips
) are allowed through.allowed_ips
: A list of strings that defines the IP addresses (in CIDR notation) that are allowed access via Tyk.enable_ip_blacklisting
: Enables IPs blocklist. If set to true
, requests coming from the explicit list of IP addresses (blacklisted_ips) are not allowed through.blacklisted_ips
: A list of strings that defines the IP addresses (in CIDR notation) that are blocked access via Tyk. This list is explicit and wildcards are currently not supported.403
).
graphql
configuration object. A GraphQL schema is specified within the schema
field and the execution mode is set to proxyOnly
. The GraphQL public playground is enabled with the path set to /playground
.
/httpbin
to target URL http://httpbin.org
.
Traffic routing can be configured under spec.proxy
:
target_url
defines the upstream address (or target URL) to which requests should be proxied.listen_path
is the base path on Tyk to which requests for this API should be sent. Tyk listens out for any requests coming into the host at this path, on the port that Tyk is configured to run on and processes these accordingly. For example, /api/
or /
or /httpbin/
.strip_listen_path
removes the inbound listen path (as accessed by the client) when generating the outbound request for the upstream service. For example, consider the scenario where the Tyk base address is http://acme.com/
, the listen path is example/
and the upstream URL is http://httpbin.org/
: If the client application sends a request to http://acme.com/example/get
then the request will be proxied to http://httpbin.org/example/get
spec.domain
is the domain to bind this API to. This enforces domain matching for client requests.
In this example, requests to httpbin.tyk.io
will be proxied to upstream URL http://httpbin.org
6380
to tcp://localhost:6379
.
TykStreamsApiDefinition
resource.
x-tyk-api-gateway
extension for Tyk-specific settings.x-tyk-streaming
extension for Tyk Streams configuration.TykStreamsApiDefinition
custom resource.
Example manifest:
kubectl
command to apply the TykStreamsApiDefinition
manifest to your Kubernetes cluster:
TykStreamsApiDefinition
resource. The Tyk Operator watches this resource and configures the Tyk Gateway or Tyk Dashboard with the new API.
TykStreamsApiDefinition
resource to ensure that the API has been successfully created:
ConfigMap
. The Tyk Operator will automatically detect changes and update the API in the Tyk Gateway.
SecurityPolicy
CRD. The following example illustrates how to configure a default policy for trial users for a Tyk Classic API named httpbin
, a Tyk OAS API named petstore
, and a Tyk Streams API named http-to-kafka
.
trial-policy.yaml
In this example, we have defined a security policy as described below:
Define Security Policy status and metadata
name
: A descriptive name for the security policy.
active
: Marks the policy as active (true or false).
state
: The current state of the policy. It can have one of three values:
active
: Keys connected to this policy are enabled and new keys can be created.draft
: Keys connected to this policy are disabled; no new keys can be created.deny
: Policy is not published to Gateway; no keys can be created.tags
: A list of tags to categorize or label the security policy, e.g.
meta_data
: Key-value pairs for additional metadata related to the policy, e.g.
access_rights_array
: Defines the list of APIs that the security policy applies to and the versions of those APIs.
name
: The Kubernetes metadata name of the API resource to which the policy grants access.namespace
: The Kubernetes namespace where the API resource is deployed.kind
: Tyk OAS APIs (TykOasApiDefinition
), Tyk Streams (TykStreamsApiDefinition
) and Tyk Classic APIs (ApiDefinition
) can be referenced here. The API format can be specified by kind
field. If omitted, ApiDefinition
is assumed.versions
: Specifies the API versions the policy will cover. If the API is not versioned, include the default version here. The default version of a Classic API is “Default”. The default version of a Tyk OAS API is "".ApiDefinition
resource named httpbin
in the default
namespace, a TykOasApiDefinition
resource named petstore
in the default
namespace, and a TykStreamsApiDefinition
resource named http-to-kafka
in the default
namespace. Note that with Tyk Operator, you do not need to specify API ID as in the raw Policy definition. Tyk Operator will automatically retrieve the API ID of referenced API Definition resources for you.
Define Rate Limits, Usage Quota, and Throttling
rate
: The maximum number of requests allowed per time period (Set to -1
to disable).per
: The time period (in seconds) for the rate limit (Set to -1
to disable).throttle_interval
: The interval (in seconds) between each request retry (Set to -1
to disable).throttle_retry_limit
: The maximum number of retry attempts allowed (Set to -1
to disable).quota_max
: The maximum number of requests allowed over a quota period (Set to -1
to disable).quota_renewal_rate
: The time, in seconds, after which the quota is renewed.httpbin
API at a rate limit of maximum 120 times per 60 seconds ("rate": 120, "per": 60
), with a usage quota of 1000 every hour ("quota_max": 1000, "quota_renewal_rate": 3600
), without any request throttling (throttle_interval: -1, throttle_retry_limit: -1
).
kubectl apply
command:
status
field, you can see that this security policy has been linked to httpbin
, petstore
, and http-to-kafka
APIs.
limit
field for that API.
The following manifest defines a security policy with per-API rate limits and quotas for two APIs: httpbin
and petstore
.
httpbin
API:
petstore
API:
httpbin
and petstore
.
allowed_urls
field under access_rights_array
.
The following manifest defines a security policy that allows access only to specific URLs and HTTP methods for two APIs: httpbin
(a Tyk Classic API) and petstore
(a Tyk OAS API).
curl -H "Authorization: Bearer $KEY_AUTH" http://tyk-gw.org/petstore/pet/10
returns a 200 OK
response.curl -H "Authorization: Bearer $KEY_AUTH" http://tyk-gw.org/httpbin/get
returns a 200 OK
response.curl -H "Authorization: Bearer $KEY_AUTH" http://tyk-gw.org/petstore/pet
returns a 403 Forbidden
response with the message:curl -H "Authorization: Bearer $KEY_AUTH" http://tyk-gw.org/httpbin/anything
returns a 403 Forbidden
response with the message:partitions
field:
quota
: Set to true to enforce quota rules (limits the number of requests allowed over a period).rate_limit
: Set to true to enforce rate limiting rules (limits the number of requests per second or minute).acl
: Set to true to enforce access control rules (controls which APIs or paths can be accessed).complexity
: Set to true to enforce GraphQL complexity rules (limits the complexity of GraphQL queries to prevent resource exhaustion).api_id
from your API Definition to the YAML of your ApiDefinition
type. Then, the Operator will take care of the rest.
Example:
spec.api_id
field:5f8f3933f56e1a5ffe2cd58c
5f8f3933f56e1a5ffe2cd58c
into the YAML’s spec.id
field like below
spec.access_rights_array
field of the YAML must refer to the ApiDefinition object that the policy identified by the id will affect.
To find available ApiDefinition objects:
id
field as above, allowing keys to continue to work as before the delete event.
api_id
, this gives us the ability to preserve Keys that are tied to APIs or policies which are tied to APIs.
Imagine any use case where you have keys tied to policies, and policies tied to APIs.
Now imagine that these resources are unintentionally destroyed. Our database goes down, or our cluster, or something else.
Well, using the Tyk Operator, we can easily re-generate all our resources in a non-destructive fashion. That’s because the operator intelligently constructs the unique ID using the unique namespaced name of our CRD resources. For that reason.
Alternatively, if you don’t explicitly state it, it will be hard-coded for you by Base64 encoding the namespaced name of the CRD.
For example: