实现禁用路由的Envoy OAuth2过滤



我部署了一个特使作为管理oauth2的侧车。所有资源都可以正常工作,客户端被重定向到OIDC以进行身份验证。这是我的配置文件的一部分(在Helm图表中管理):

- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
access_log:
- name: envoy.access_loggers.file
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
path: /dev/stdout
codec_type: auto
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: my-service
domains:
- "*"
routes:
- match:
prefix: "/"
route:
cluster: my-service
http_filters:
- name: envoy.filters.http.oauth2
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.oauth2.v3.OAuth2
config:
token_endpoint:
cluster: {{ .Values.back.envoy.oidc.name }}
uri: https://{{ .Values.back.envoy.oidc.address }}/oidc/token
timeout: 5s
authorization_endpoint: https://{{ .Values.back.envoy.oidc.address }}/oidc/authorize
redirect_uri: "%REQ(x-forwarded-proto)%://%REQ(:authority)%/oidc/callback"
redirect_path_matcher:
path:
exact: /oidc/callback
signout_path:
path:
exact: /oidc/signout
credentials:
client_id: {{ required "back.envoy.oidc.client_id is required" .Values.back.envoy.oidc.client_id }}
token_secret:
name: token
sds_config:
resource_api_version: V3
path: "/etc/envoy/token-secret.yaml"
hmac_secret:
name: hmac
sds_config:
resource_api_version: V3
path: "/etc/envoy/hmac-secret.yaml"
forward_bearer_token: true
# (Optional): defaults to 'user' scope if not provided
auth_scopes:
- user
- openid
- email
- homelan_devices_read
- homelan_topology_read
- homelan_devices_write
# (Optional): set resource parameter for Authorization request
#resources:
#- oauth2-resource
#- http://example.com
- name: envoy.filters.http.router
typed_config: {}

现在我希望一些公开的资源不需要进行身份验证。我在文档中看到了Oauth过滤器doc "将此保留为空以禁用特定路由的OAuth2,使用per filter config."(见https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/filters/http/oauth2/v3/oauth.proto envoy-v3-api-msg-extensions-filters-http-oauth2-v3-oauth2config)这句话使我认为这是可能的。我试图通过virtual_hosts改变我的配置来管理它:

virtual_hosts:
- name: no-oauth
domains: ["*"]
typed_per_filter_config:
envoy.filters.http.oauth2:
"@type": type.googleapis.com/envoy.extensions.filters.http.oauth2.v3.OAuth2
routes:
- match:
prefix: "/api/v1/myResource1"
route:
cluster: my-service
- name: my-service
domains: ["*"]
routes:
- match:
prefix: "/api/v1/myResource2"
route:
cluster: my-service

我有错误:[关键][主要][源/服务器/服务器。[Cc:117]初始化配置错误'/etc/envoy/envoy。:过滤器envoy.filters.http。Oauth2不支持虚拟主机特定配置

你知道吗?是否有人实现特使OAuth2过滤器禁用路由?

查看我的特使日志后,我意识到路径被称为header ":path"。
pass_through_matcher的数学头。

然后只添加:

pass_through_matcher:
- name: ":path"
prefix_match: "/healthz"
- name: ":path"
prefix_match: "/api/v1/myResource1"

在我的配置文件中没有lua过滤器(参见我之前的回答),它可以工作。

对于信息,我找到了一个解决方法:

我在OAuth2过滤器之前添加了一个LUA过滤器:

- name: envoy.filters.http.lua
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
inline_code: |
function envoy_on_request(request_handle)
request_handle:headers():add("X-Path", request_handle:headers():get(":path"))
end

为了在头文件中添加路径。然后我可以使用conf Oauth2的元素:

  • pass_through_matcher(重复config.route.v3.HeaderMatcher)任何匹配任何提供的匹配器的请求将不经过OAuth验证而通过。

所以我把这个添加到OAuth2过滤器中:

pass_through_matcher:
- name: "X-path"
prefix_match: "/healthz"
- name: "X-path"
prefix_match: "/api/v1/myResource1"

那么我的/api/v1/myResource1请求(和healthz也)不需要认证(从OAuth2禁用),而我的/api/v1/myResource2请求需要它。

我仍然有一个未回答的问题:

保留此为空以禁用特定路由的OAuth2,使用per filter config."

最新更新