Use JWTs to verify requests to API gateways on Kubernetes
This topic describes how to use JSON web tokens (JWT) to verify requests to API gateways deployed to Kubernetes-orchestrated containers. If your API gateway is deployed to virtual machines, refer to Use JWTs to verify requests to API gateways on VMs.
This feature is available in Consul Enterprise.
Overview
You can configure API gateways to use JWTs to verify incoming requests so that you can stop unverified traffic at the gateway. You can configure JWT verification at different levels:
- Listener defaults: Define basic defaults that apply to all routes attached to a listener.
- HTTP route-specific settings: You can define JWT authentication settings for specific HTTP routes. Route-specific JWT settings override default listener configurations.
- Listener overrides: Define override settings that take precedence over default and route-specific configurations. This enables you to set enforceable policies for listeners.
Complete the following steps to use JWTs to verify requests:
- Define a policy that specifies default and override settings for API gateway listeners and attach it to the gateway.
- Define an HTTP route auth filter that specifies route-specific JWT verification settings.
- Attach the auth filter to the HTTP route values file.
- Apply the configurations.
Requirements
- Consul v1.17+
- Consul on Kubernetes CLI or Helm chart v1.3.0+
- JWT details, such as claims and provider
Define override and default settings
Create a GatewayPolicy
values file and configure the following fields to define default and override settings for JWT verification. Refer to GatewayPolicy
configuration reference for details.
kind
: Must be set toGatewayPolicy
metadata.name
: Specifies a name for the policy.spec.targetRef.name
: Specifies the name of the API gateway to attach the policy to.spec.targetRef.kind
: Specifies the kind of resource to attach to the policy to. Must be set toGateway
.spec.targetRef.group
: Specifies the resource group. Unless you have created a custom group, this should be set togateway.networking.kuberenetes.io
.spec.targetRef.sectionName
: Specifies a part of the gateway that the policy applies to.spec.targetRef.override.jwt.providers
: Specifies a list of providers and claims used to verify requests to the gateway. The override settings take precedence over the default and route-specific JWT verification settings.spec.targetRef.default.jwt.providers
: Specifies a list of default providers and claims used to verify requests to the gateway.
Define an HTTP route auth filter
Create an RouteAuthFilter
values file and configure the following fields. Refer to RouteAuthFilter
configuration reference for details.
kind
: Must be set toRouteAuthFilter
metadata.name
: Specifies a name for the filter.metadata.namespace
: Specifies the Consul namespace the filter applies to.spec.jwt.providers
: Specifies a list of providers and claims used to verify requests to the gateway. The override settings take precedence over the default and route-specific JWT verification settings.
Attach the auth filter to your HTTP routes
In the filters
field of your HTTP route configuration, add the following fields. Refer to the extensionRef
configuration reference for details:
type: extensionRef
: Declare list of extension references.extensionRef.group
: Specifies the resource group. Unless you have created a custom group, this should be set togateway.networking.kuberenetes.io
.extensionRef.kind
: Specifies the type of extension reference to attach to the route. Must beRouteAuthFilter
extensionRef.name
: Specifies the name of the auth filter.
Apply the configurations
Run the kubectl apply
command and specify the values files to apply the configurations. The following example applies the values files stored in the jwt-routes
directory: