Skip to content
铭师堂的云原生升级实践Know more

External Authentication

Function Description

The ext-auth plugin implements sending authentication requests to an external authorization service to check whether the client request is authorized. This plugin is implemented with reference to Envoy’s native ext_authz filter, which covers some capabilities for connecting to HTTP services.

Execution Properties

Plugin Execution Phase: Authentication Phase
Plugin Execution Priority: 360

Configuration Fields

NameData TypeRequiredDefault ValueDescription
http_serviceobjectYes-Configuration for the external authorization service
failure_mode_allowboolNofalseWhen set to true, client requests will still be accepted even if communication with the authorization service fails or the authorization service returns an HTTP 5xx error
failure_mode_allow_header_addboolNofalseWhen both failure_mode_allow and failure_mode_allow_header_add are set to true, if communication with the authorization service fails or returns an HTTP 5xx error, the request header will include x-envoy-auth-failure-mode-allowed: true
status_on_errorintNo403Sets the HTTP status code returned to the client when the authorization service is unreachable or returns a 5xx status code. The default status code is 403

Configuration Fields for Each Item in http_service

NameData TypeRequiredDefault ValueDescription
endpoint_modestringNoenvoySelect either envoy or forward_auth as an optional choice
endpointobjectYes-Information about the HTTP service for sending authentication requests
timeoutintNo1000Connection timeout for ext-auth service, in milliseconds
authorization_requestobjectNo-Configuration for sending authentication requests
authorization_responseobjectNo-Configuration for processing authentication responses

Configuration Fields for Each Item in endpoint

NameData TypeRequiredDefault ValueDescription
service_namestringRequired-Input the name of the authorization service, in complete FQDN format, e.g., ext-auth.dns or ext-auth.my-ns.svc.cluster.local
service_portintNo80Input the port of the authorization service
service_hoststringNo-The Host header set when requesting the authorization service; remains the same as FQDN if not filled
path_prefixstringRequired when endpoint_mode is envoyRequest path prefix for the client when sending requests to the authorization service
request_methodstringNoGETHTTP Method for client requests to the authorization service when endpoint_mode is forward_auth
pathstringRequired when endpoint_mode is forward_auth-Request path for the client when sending requests to the authorization service

Configuration Fields for Each Item in authorization_request

NameData TypeRequiredDefault ValueDescription
allowed_headersarray of StringMatcherNo-When set, client request headers with matching criteria will be added to the headers of the request to the authorization service. The Authorization HTTP header will be automatically included in the authorization service request, and if endpoint_mode is forward_auth, the original request path will be set to X-Original-Uri and the original request HTTP method will be set to X-Original-Method.
headers_to_addmap[string]stringNo-Sets the list of request headers to include in the authorization service request. Note that headers with the same name from the client will be overwritten.
with_request_bodyboolNofalseBuffer the client request body and send it in the authentication request (does not take effect for HTTP Methods GET, OPTIONS, and HEAD)
max_request_body_bytesintNo10MBSets the maximum size of the client request body to keep in memory. When the client request body reaches the value set in this field, an HTTP 413 status code will be returned, and the authorization process will not start. Note that this setting takes precedence over the failure_mode_allow configuration.

Configuration Fields for Each Item in authorization_response

NameData TypeRequiredDefault ValueDescription
allowed_upstream_headersarray of StringMatcherNo-When set, the response headers of the authorization request with matching criteria will be added to the original client request headers. Note that headers with the same name will be overwritten.
allowed_client_headersarray of StringMatcherNo-If not set, all response headers from authorization requests will be added to the client’s response when a request is denied. When set, response headers from authorization requests with matching criteria will be added to the client’s response when a request is denied.

Field Descriptions for StringMatcher Type

When using array of StringMatcher, the fields are configured according to the order defined in the array.

NameData TypeRequiredDefault ValueDescription
exactstringNo, must select one from exact, prefix, suffix, contains, regex-Exact match
prefixstringNo, must select one from exact, prefix, suffix, contains, regex-Prefix match
suffixstringNo, must select one from exact, prefix, suffix, contains, regex-Suffix match
containsstringNo, must select one from exact, prefix, suffix, contains, regex-Contains match
regexstringNo, must select one from exact, prefix, suffix, contains, regex-Regex match

Configuration Example

Assuming the ext-auth service has a serviceName of ext-auth, port 8090, path /auth, and namespace backend in Kubernetes.

Two types of endpoint_mode are supported:

  • When endpoint_mode is envoy, the authentication request will use the original request HTTP Method, and the configured path_prefix will be concatenated with the original request path.
  • When endpoint_mode is forward_auth, the authentication request will use the configured request_method as the HTTP Method and the configured path as the request path.

Example 1: endpoint_mode is envoy

Configuration of ext-auth Plugin:

http_service:
endpoint_mode: envoy
endpoint:
service_name: ext-auth.backend.svc.cluster.local
service_port: 8090
path_prefix: /auth
timeout: 1000

Using the following request to the gateway, after enabling the ext-auth plugin:

Terminal window
curl -X POST http://localhost:8082/users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5 -X GET -H "foo: bar" -H "Authorization: xxx"

Successful request to the ext-auth service: The ext-auth service will receive the following authentication request:

POST /auth/users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5 HTTP/1.1
Host: ext-auth.backend.svc.cluster.local
Authorization: xxx
Content-Length: 0

Failed request to the ext-auth service: When the ext-auth service responds with a 5xx error, the client will receive an HTTP response code of 403 along with all response headers returned by the ext-auth service.

If the ext-auth service returns x-auth-version: 1.0 and x-auth-failed: true headers, these will be conveyed to the client:

HTTP/1.1 403 Forbidden
x-auth-version: 1.0
x-auth-failed: true
date: Tue, 16 Jul 2024 00:19:41 GMT
server: istio-envoy
content-length: 0

When the ext-auth service is inaccessible or returns a status code of 5xx, the client request will be denied with the status code configured in status_on_error. When the ext-auth service returns other HTTP status codes, the client request will be denied with the returned status code. If allowed_client_headers is configured, the matching response headers will be added to the client’s response.

Example 2: ext-auth Plugin Configuration:

http_service:
authorization_request:
allowed_headers:
- exact: x-auth-version
headers_to_add:
x-envoy-header: true
authorization_response:
allowed_upstream_headers:
- exact: x-user-id
- exact: x-auth-version
endpoint_mode: envoy
endpoint:
service_name: ext-auth.backend.svc.cluster.local
service_host: my-domain.local
service_port: 8090
path_prefix: /auth
timeout: 1000

Using the following request to the gateway after enabling the ext-auth plugin:

Terminal window
curl -X POST http://localhost:8082/users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5 -X GET -H "foo: bar" -H "Authorization: xxx"

The ext-auth service will receive the following authentication request:

POST /auth/users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5 HTTP/1.1
Host: my-domain.local
Authorization: xxx
X-Auth-Version: 1.0
x-envoy-header: true
Content-Length: 0

If the ext-auth service returns headers containing x-user-id and x-auth-version, these two request headers will be included in requests to the upstream when the gateway calls it.

Example 1: endpoint_mode is forward_auth

ext-auth Plugin Configuration:

http_service:
endpoint_mode: forward_auth
endpoint:
service_name: ext-auth.backend.svc.cluster.local
service_port: 8090
path: /auth
request_method: POST
timeout: 1000

Using the following request to the gateway after enabling the ext-auth plugin:

Terminal window
curl -i http://localhost:8082/users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5 -X GET -H "foo: bar" -H "Authorization: xxx"

Successful request to the ext-auth service: The ext-auth service will receive the following authentication request:

POST /auth HTTP/1.1
Host: ext-auth.backend.svc.cluster.local
Authorization: xxx
X-Original-Uri: /users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5
X-Original-Method: GET
Content-Length: 0

Failed request to the ext-auth service: When the ext-auth service responds with a 5xx error, the client will receive an HTTP response code of 403 along with all response headers returned by the ext-auth service.

If the ext-auth service returns x-auth-version: 1.0 and x-auth-failed: true headers, these will be conveyed to the client:

HTTP/1.1 403 Forbidden
x-auth-version: 1.0
x-auth-failed: true
date: Tue, 16 Jul 2024 00:19:41 GMT
server: istio-envoy
content-length: 0

When the ext-auth service is inaccessible or returns a status code of 5xx, the client request will be denied with the status code configured in status_on_error. When the ext-auth service returns other HTTP status codes, the client request will be denied with the returned status code. If allowed_client_headers is configured, the matching response headers will be added to the client’s response.

Example 2: ext-auth Plugin Configuration:

http_service:
authorization_request:
allowed_headers:
- exact: x-auth-version
headers_to_add:
x-envoy-header: true
authorization_response:
allowed_upstream_headers:
- exact: x-user-id
- exact: x-auth-version
endpoint_mode: forward_auth
endpoint:
service_name: ext-auth.backend.svc.cluster.local
service_host: my-domain.local
service_port: 8090
path: /auth
request_method: POST
timeout: 1000

Using the following request to the gateway after enabling the ext-auth plugin:

Terminal window
curl -i http://localhost:8082/users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5 -X GET -H "foo: bar" -H "Authorization: xxx" -H "X-Auth-Version: 1.0"

The ext-auth service will receive the following authentication request:

POST /auth HTTP/1.1
Host: my-domain.local
Authorization: xxx
X-Original-Uri: /users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5
X-Original-Method: GET
X-Auth-Version: 1.0
x-envoy-header: true
Content-Length: 0

If the ext-auth service returns headers containing x-user-id and x-auth-version, these two request headers will be included in requests to the upstream when the gateway calls it.

x-forwarded-* Header

When endpoint_mode is forward_auth, Higress will automatically generate and send the following headers to the authorization service.

HeaderDescription
x-forwarded-protoThe scheme of the original request, e.g., http/https
x-forwarded-methodThe method of the original request, e.g., get/post/delete/patch
x-forwarded-hostThe host of the original request
x-forwarded-uriThe path of the original request, including path parameters, e.g., /v1/app?test=true
x-forwarded-forThe client IP address of the original request