Skip to main content

Protect APIs on Envoy with Choreo Identity & Access Management

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

This guide explains how to secure your APIs at the Envoy gateway layer using tokens issued by Choreo Identity & Access Management. By retrieving public signing keys from the Choreo Identity & Access Management JWKS endpoint, Envoy cryptographically verifies every incoming token at the edge before forwarding authorized requests to your backend services.

The guide covers two integration approaches available in Envoy:

  • Token Validation: Configure Envoy to validate incoming JWTs issued by Choreo Identity & Access Management using the envoy.filters.http.jwt_authn HTTP filter, ensuring only authenticated requests access your APIs.
  • Scope-Based Authorization: Use scopes issued by Choreo Identity & Access Management and Envoy's RBAC filter to enforce route-level access control on upstream APIs.

Prerequisites​

Before you begin, ensure you have the following:

  • Access to a Choreo Identity & Access Management tenant.
  • The latest stable Envoy version installed.
  • A backend REST API service running and accessible to Envoy. The examples use http://localhost:9000.
  • curl available in your terminal.

The examples use the following local ports:

PortComponentUsed for
8080Envoy HTTP listenerClient-facing API gateway listener
8081Envoy gRPC listenerClient-facing listener for the gRPC ext_authz example
8181HTTP authorization adapterEnvoy HTTP ext_authz checks
8182gRPC authorization adapterEnvoy gRPC ext_authz checks
9000Backend REST APIUpstream service that Envoy forwards allowed requests to
9901Envoy adminEnvoy administration interface

Verify your Envoy version:

envoy --version

Configure JWT Token Validation in Envoy​

JWT Token Validation is the foundation of API security with Envoy and Choreo Identity & Access Management. In this section, you register an application in Choreo Identity & Access Management to obtain credentials, configure Envoy to validate tokens using the Choreo Identity & Access Management JWKS endpoint, and verify that unauthenticated requests are rejected.

Configure Choreo Identity & Access Management​

Create an Application​

  1. Sign in to the Choreo Identity & Access Management Console at https://console.cloud.choreolabs.com/iam/.
  2. Navigate to Applications → New Application.
  3. Enter an Application name.
  4. Select Backend service as the application type.
  5. Click Create application.
  6. Note the Client ID and Client secret from the application details page.
Deploy your changes

Saving a change does not make it live. You author in the control plane, and your users sign in on a separate data plane, so a change takes effect in an environment only after you deploy it. Go to Deploy, deploy the latest version to your first environment, then promote it to the next environment when you are ready.

Auto-sync deploys saved changes to your first environment within seconds. Turning it on is your choice, per project. See Deploy a Version, or learn why you deploy and how auto-sync works.

Obtain an Access Token​

Use the client credentials grant to request an access token. The examples use HTTP Basic authentication for the client credentials:

curl -kL -X POST 'https://<DATA_PLANE_URL>/oauth2/token' \
--user '<CLIENT_ID>:<CLIENT_SECRET>' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials'

Save the returned access_token. You will use it in the Try It Out section.

Configure Envoy​

Create an API​

Create an Envoy configuration file. See the Envoy configuration overview for a full reference.

The listener defines the port Envoy exposes to clients. The route configuration defines the paths Envoy exposes. The cluster configuration defines the upstream service.

This example exposes Envoy on port 8080 and forwards requests for /resources to an upstream API on http://localhost:9000.

static_resources:
listeners:
- name: local_listener
address:
socket_address:
address: 127.0.0.1
port_value: 8080
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: resources_api_gateway
route_config:
name: local_route
virtual_hosts:
- name: resources_api
domains: ["*"]
routes:
- match:
prefix: "/resources"
route:
cluster: upstream_api
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

clusters:
- name: upstream_api
connect_timeout: 1s
type: STATIC
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: upstream_api
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 127.0.0.1
port_value: 9000

Start Envoy to verify the API is reachable before adding authentication:

envoy --mode validate -c envoy.yaml
envoy -c envoy.yaml

Envoy starts on port 8080. Verify the endpoint responds:

curl -i --location 'http://localhost:8080/resources' \
--header 'Content-Type: application/json'

Expected response:

HTTP/1.1 200 OK

Add JWT Validation​

Envoy validates JWTs using the jwt_authn HTTP filter. Envoy fetches public keys from the Choreo Identity & Access Management JWKS endpoint and uses the keys to verify the signature on incoming tokens.

Add the jwt_authn filter before the router filter:

http_filters:
- name: envoy.filters.http.jwt_authn
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.jwt_authn.v3.JwtAuthentication
providers:
thunderid:
issuer: "https://<DATA_PLANE_URL>"
remote_jwks:
http_uri:
uri: "https://<DATA_PLANE_URL>/oauth2/jwks"
cluster: thunderid
timeout: 5s
cache_duration: 300s
from_headers:
- name: authorization
value_prefix: "Bearer "
rules:
- match:
prefix: "/resources"
requires:
provider_name: thunderid
- match:
prefix: "/"
requires:
allow_missing: {}
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

Add a cluster for the Choreo Identity & Access Management JWKS endpoint:

clusters:
- name: thunderid
connect_timeout: 1s
type: LOGICAL_DNS
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: thunderid
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: api.identity-platform.us.cloud.wso2.com
port_value: 443
transport_socket:
name: envoy.transport_sockets.tls
typed_config:
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
sni: api.identity-platform.us.cloud.wso2.com
common_tls_context:
tls_params:
tls_minimum_protocol_version: TLSv1_3
tls_maximum_protocol_version: TLSv1_3
validation_context:
trust_chain_verification: ACCEPT_UNTRUSTED
Local development only

trust_chain_verification: ACCEPT_UNTRUSTED skips certificate chain verification for local development with self-signed certificates. Remove this setting in production and configure Envoy with a certificate authority trusted by your deployment.

Configuration reference -- see the full list of options in the JWT authentication filter docs:

FieldValueDescription
issuerhttps://<DATA_PLANE_URL>Token issuer expected by Envoy
remote_jwks.http_uri.urihttps://<DATA_PLANE_URL>/oauth2/jwksChoreo Identity & Access Management JWKS endpoint for fetching public keys
cache_duration300sCaches JWKS keys to avoid fetching them on every request
from_headersAuthorization: BearerReads the JWT from the Authorization header
trust_chain_verificationACCEPT_UNTRUSTEDSkips TLS certificate chain verification. Use only in local development.

Restart Envoy to apply the changes:

pkill -f "envoy -c envoy.yaml"
envoy --mode validate -c envoy.yaml
envoy -c envoy.yaml

Try It Out​

Request without a token, expect 401 Unauthorized:

curl -i --location 'http://localhost:8080/resources' \
--header 'Content-Type: application/json'

Expected response:

HTTP/1.1 401 Unauthorized

Jwt is missing

Request with a valid Choreo Identity & Access Management token, expect 200 OK:

curl -i --location 'http://localhost:8080/resources' \
--header 'Authorization: Bearer <ACCESS_TOKEN>' \
--header 'Content-Type: application/json'

Expected response:

{
"resources": [
{
"id": "101",
"name": "Resource One",
"status": "active"
},
{
"id": "102",
"name": "Resource Two",
"status": "pending"
}
]
}

Configure Scope-Based Authorization in Envoy​

JWT authentication confirms that a token is valid and was issued by Choreo Identity & Access Management. It does not control what the token holder is allowed to do. With the configuration from the previous section, Envoy accepts any valid Choreo Identity & Access Management token for the protected route.

Scope-based authorization adds a second layer of control. Choreo Identity & Access Management embeds scopes in tokens based on the roles assigned to an application. Envoy then checks those scopes on every request and rejects tokens that do not carry the required permissions. This lets you enforce scope-based authorization, for example, restricting a read endpoint to tokens with resource:read, without changing the upstream service.

The steps below build on the JWT authentication configuration from the previous section. You will define a Resource Server, a Resource, and actions in Choreo Identity & Access Management to model your API permissions. Then bundle them into a role, assign the role to your application, and update the Envoy configuration to enforce scope validation.

Configure Choreo Identity & Access Management​

Create a Resource Server​

A Resource Server represents your API in Choreo Identity & Access Management. The Resource Server groups all resources and actions under a single identifier and acts as the audience for tokens issued to clients of that API. For a full reference, see Resource Servers.

Using the Console:

  1. Sign in to the Choreo Identity & Access Management Console at https://console.cloud.choreolabs.com/iam/.
  2. Navigate to Resource servers → New Resource Server.
  3. Fill in the following fields:
    • Name: Resources API
    • Identifier: https://api.example.com/resources
    • Description: Handles resource operations
    • Delimiter: : (colon)
  4. Click Create.
  5. Note the Resource Server ID from the details page.

Create a Resource​

A Resource represents a specific entity within the API, such as /resources. Resources let you model your API surface so that permissions can be granted at a granular level.

Using the Console:

  1. Open the Resources API Resource Server.
  2. Go to the Resources tab and click New Resource.
  3. Fill in the following fields:
    • Name: Resources
    • Handle: resource
  4. Click Create.

Create Resource Actions​

Actions define the operations allowed on a resource. Each action produces a scope in the format <resource-handle>:<action-handle>. Choreo Identity & Access Management includes these scopes in tokens issued to applications that hold the corresponding role.

Using the Console:

  1. Under the Resources API Resource Server, open the Resources resource.
  2. Go to the Actions tab and click New Action.
  3. Fill in the following fields:
    • Name: Read Resource
    • Handle: read
    • Description: Permission to read resources
  4. Click Create.
  5. Note the generated permission shown in the action details: resource:read.

Create a Role and assign permission​

Roles bundle one or more permissions together. Assign roles to applications to control which scopes appear in their tokens.

  1. Navigate to Roles → New Role.
  2. Fill in the following fields:
    • Name: Resources API Reader
  3. Click Continue.
  4. Under Assign permissions, choose resource:read.
  5. Click Continue.

Assign a Role to the Application​

Assign the role to the application you created in the previous section. Choreo Identity & Access Management includes the role's scopes in every token issued for that application.

Using the Console:

  1. Navigate to Applications and open your Backend Service application.
  2. Go to the Roles tab.
  3. Click Assign role, select Resources API Reader from the list, and confirm.

Configure Envoy​

Add claim_to_headers to the jwt_authn provider you configured in the previous section, then use the Envoy RBAC filter to enforce the required scope. See the JWT authentication filter docs and RBAC filter docs for the full list of options.

Envoy copies the JWT scope claim into a request header. The required scope can then be checked before Envoy forwards the request.

Then add scope validation before the router filter:

http_filters:
- name: envoy.filters.http.jwt_authn
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.jwt_authn.v3.JwtAuthentication
providers:
thunderid:
issuer: "https://<DATA_PLANE_URL>"
remote_jwks:
http_uri:
uri: "https://<DATA_PLANE_URL>/oauth2/jwks"
cluster: thunderid
timeout: 5s
cache_duration: 300s
from_headers:
- name: authorization
value_prefix: "Bearer "
claim_to_headers:
- header_name: x-jwt-scope
claim_name: scope
rules:
- match:
prefix: "/resources"
requires:
provider_name: thunderid
- match:
prefix: "/"
requires:
allow_missing: {}
- name: envoy.filters.http.rbac
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.rbac.v3.RBAC
rules:
action: ALLOW
policies:
resources-read:
permissions:
- and_rules:
rules:
- url_path:
path:
prefix: "/resources"
- header:
name: ":method"
string_match:
exact: "GET"
- header:
name: "x-jwt-scope"
string_match:
safe_regex:
regex: "(^|\\s)resource:read(\\s|$)"
principals:
- any: true
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

Add one scope policy for each protected route or operation. For example, an update endpoint can require a separate scope such as resource:update.

Scope configuration reference:

FieldValueDescription
claim_to_headers.claim_namescopeJWT claim that stores scopes
claim_to_headers.header_namex-jwt-scopeHeader Envoy uses to check the token scopes
string_match.safe_regex.regex(^|\\s)resource:read(\\s|$)Required scope to access the matching route. The regular expression matches scope token boundaries in a space-delimited scope claim.
actionALLOWAllows only requests that match one of the configured scope policies

Restart Envoy to apply the changes:

pkill -f "envoy -c envoy.yaml"
envoy --mode validate -c envoy.yaml
envoy -c envoy.yaml

Try It Out​

Obtain an Access Token with the Required Scope​

Request a token for the application that has the role assigned:

curl -kL -X POST 'https://<DATA_PLANE_URL>/oauth2/token' \
--user '<CLIENT_ID>:<CLIENT_SECRET>' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=resource:read'

The response confirms the scope is present:

{
"access_token": "<ACCESS_TOKEN>",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "resource:read"
}

Call the Protected API​

With a token that has the required scope, expect 200 OK:

curl -i --location 'http://localhost:8080/resources' \
--header 'Authorization: Bearer <ACCESS_TOKEN>' \
--header 'Content-Type: application/json'

With a token missing the required scope, expect 403 Forbidden:

curl -i --location 'http://localhost:8080/resources' \
--header 'Authorization: Bearer <TOKEN_WITHOUT_REQUIRED_SCOPE>' \
--header 'Content-Type: application/json'

Expected response:

HTTP/1.1 403 Forbidden

RBAC: access denied

Was this page helpful?