跳转到内容

外部认证

功能说明

ext-auth 插件实现了向外部授权服务发送鉴权请求,以检查客户端请求是否得到授权。该插件实现时参考了 Envoy 原生的 ext_authz filter,实现了原生 filter 中对接 HTTP 服务的部分能力。

配置字段

名称数据类型必填默认值描述
http_serviceobject-外部授权服务配置
failure_mode_allowboolfalse当设置为 true 时,即使与授权服务的通信失败,或者授权服务返回了 HTTP 5xx 错误,仍会接受客户端请求
failure_mode_allow_header_addboolfalsefailure_mode_allowfailure_mode_allow_header_add 都设置为 true 时,若与授权服务的通信失败,或授权服务返回了 HTTP 5xx 错误,那么请求头中将会添加 x-envoy-auth-failure-mode-allowed: true
status_on_errorint403当授权服务无法访问或状态码为 5xx 时,设置返回给客户端的 HTTP 状态码。默认状态码是 403

http_service 中每一项的配置字段说明如下:

名称数据类型必填默认值描述
endpoint_modestringenvoyenvoyforward_auth 中选填一项
endpointobject-发送鉴权请求的 HTTP 服务信息
timeoutint1000ext-auth 服务连接超时时间,单位毫秒
authorization_requestobject-发送鉴权请求配置
authorization_responseobject-处理鉴权响应配置

endpoint 中每一项的配置字段说明如下:

名称数据类型必填默认值描述
service_namestring-输入授权服务名称,带服务类型的完整 FQDN 名称,例如 ext-auth.dnsext-auth.my-ns.svc.cluster.local
service_portint80输入授权服务的服务端口
path_prefixstringendpoint_modeenvoy 时必填-endpoint_modeenvoy 时,客户端向授权服务发送请求的请求路径前缀
request_methodstringGETendpoint_modeforward_auth 时,客户端向授权服务发送请求的 HTTP Method
pathstringendpoint_modeforward_auth 时必填-endpoint_modeforward_auth 时,客户端向授权服务发送请求的请求路径

authorization_request 中每一项的配置字段说明如下:

名称数据类型必填默认值描述
allowed_headersarray of StringMatcher-当设置后,具有相应匹配项的客户端请求头将添加到授权服务请求中的请求头中。除了用户自定义的头部匹配规则外,授权服务请求中会自动包含 Authorization 这个 HTTP 头(endpoint_modeforward_auth 时,会把原始请求的信息设置到 x-forwarded-* 请求头中)
headers_to_addmap[string]string-设置将包含在授权服务请求中的请求头列表。请注意,同名的客户端请求头将被覆盖
with_request_bodyboolfalse缓冲客户端请求体,并将其发送至鉴权请求中(HTTP Method 为 GET、OPTIONS、HEAD 请求时不生效)
max_request_body_bytesint10MB设置在内存中保存客户端请求体的最大尺寸。当客户端请求体达到在此字段中设置的数值时,将会返回 HTTP 413 状态码,并且不会启动授权过程。注意,这个设置会优先于 failure_mode_allow 的配置

authorization_response 中每一项的配置字段说明如下:

名称数据类型必填默认值描述
allowed_upstream_headersarray of StringMatcher-当设置后,具有相应匹配项的鉴权请求的响应头将添加到原始的客户端请求头中。请注意,同名的请求头将被覆盖
allowed_client_headersarray of StringMatcher-如果不设置,在请求被拒绝时,所有的鉴权请求的响应头将添加到客户端的响应头中。当设置后,在请求被拒绝时,具有相应匹配项的鉴权请求的响应头将添加到客户端的响应头中

StringMatcher 类型每一项的配置字段说明如下:

在使用 array of StringMatcher 时会按照数组中定义的 StringMatcher 顺序依次进行配置。

名称数据类型必填默认值描述
exactstring否,exactprefixsuffixcontainsregex 中选填一项-精确匹配
prefixstring否,exactprefixsuffixcontainsregex 中选填一项-前缀匹配
suffixstring否,exactprefixsuffixcontainsregex 中选填一项-后缀匹配
containsstring否,exactprefixsuffixcontainsregex 中选填一项-是否包含
regexstring否,exactprefixsuffixcontainsregex 中选填一项-正则匹配

x-forwarded-* header

endpoint_modeforward_auth 时,Higress 会自动生成并发送以下 header 至鉴权服务:

Header说明
x-forwarded-proto原始请求的 scheme,比如 http/https
x-forwarded-method原始请求的方法,比如 get/post/delete/patch
x-forwarded-host原始请求的 host
x-forwarded-uri原始请求的 path,包含路径参数,比如 /v1/app?test=true

配置示例

下面假设 ext-auth 服务在 Kubernetes 中 serviceName 为 ext-auth,端口 8090,路径为 /auth,命名空间为 backend

支持两种 endpoint_mode

  • endpoint_modeenvoy 时,鉴权请求会使用原始请求的 HTTP Method 和配置的 path_prefix 作为请求路径前缀拼接上原始的请求路径。
  • endpoint_modeforward_auth 时,鉴权请求会使用配置的 request_method 作为 HTTP Method 和配置的 path 作为请求路径。

endpoint_modeenvoy

示例1

ext-auth 插件的配置:

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

使用如下请求网关,当开启 ext-auth 插件后:

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

请求 ext-auth 服务成功:

ext-auth 服务将接收到如下的鉴权请求:

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

请求 ext-auth 服务失败:

当调用 ext-auth 服务响应为 5xx 时,客户端将接收到 HTTP 响应码 403 和 ext-auth 服务返回的全量响应头。

假如 ext-auth 服务返回了 x-auth-version: 1.0x-auth-failed: true 的响应头,会传递给客户端:

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

ext-auth 无法访问或状态码为 5xx 时,将以 status_on_error 配置的状态码拒绝客户端请求。

ext-auth 服务返回其他 HTTP 状态码时,将以返回的状态码拒绝客户端请求。如果配置了 allowed_client_headers,具有相应匹配项的响应头将添加到客户端的响应中。

示例2

ext-auth 插件的配置:

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_port: 8090
path_prefix: /auth
timeout: 1000

使用如下请求网关,当开启 ext-auth 插件后:

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

ext-auth 服务将接收到如下的鉴权请求:

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

ext-auth 服务返回响应头中如果包含 x-user-idx-auth-version,网关调用 upstream 时的请求中会带上这两个请求头。

endpoint_modeforward_auth

示例1

ext-auth 插件的配置:

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

使用如下请求网关,当开启 ext-auth 插件后:

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

请求 ext-auth 服务成功:

ext-auth 服务将接收到如下的鉴权请求:

POST /auth HTTP/1.1
Host: ext-auth
Authorization: xxx
X-Forwarded-Uri: /users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5
X-Forwarded-Host: foo.bar.com
X-Forwarded-Method: GET
X-Forwarded-Proto: http
Content-Length: 0

请求 ext-auth 服务失败:

当调用 ext-auth 服务响应为 5xx 时,客户端将接收到 HTTP 响应码 403 和 ext-auth 服务返回的全量响应头。

假如 ext-auth 服务返回了 x-auth-version: 1.0x-auth-failed: true 的响应头,会传递给客户端:

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

ext-auth 无法访问或状态码为 5xx 时,将以 status_on_error 配置的状态码拒绝客户端请求。

ext-auth 服务返回其他 HTTP 状态码时,将以返回的状态码拒绝客户端请求。如果配置了 allowed_client_headers,具有相应匹配项的响应头将添加到客户端的响应中。

示例2

ext-auth 插件的配置:

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_port: 8090
path: /auth
request_method: POST
timeout: 1000

使用如下请求网关,当开启 ext-auth 插件后:

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

ext-auth 服务将接收到如下的鉴权请求:

POST /auth HTTP/1.1
Host: ext-auth
Authorization: xxx
X-Forwarded-Uri: /users?apikey=9a342114-ba8a-11ec-b1bf-00163e1250b5
X-Forwarded-Host: foo.bar.com
X-Forwarded-Method: GET
X-Forwarded-Proto: http
X-Auth-Version: 1.0
x-envoy-header: true
Content-Length: 0

ext-auth 服务返回响应头中如果包含 x-user-idx-auth-version,网关调用 upstream 时的请求中会带上这两个请求头。