繁体   English   中英

使用禁用的路由实现 Envoy OAuth2 过滤器

[英]Implement Envoy OAuth2 filter with disabled routes

我部署了一个特使作为副车来管理 oauth2。 所有资源一切正常,客户端被重定向到 OIDC 以进行身份​​验证。 这是我的 conf 的一部分(在 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 过滤器文档“将此留空以禁用特定路由的 OAuth2,使用每个过滤器配置。” (见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 更改我的 conf 来管理它:

            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

我有错误:[critical][main] [source/server/server.cc:117] error initializing configuration '/etc/envoy/envoy.yaml': The filter envoy.filters.http.oauth2 doesn't support virtual特定于主机的配置

任何想法 ? 有人用禁用的路由实现了 Envoy OAuth2 过滤器吗?

有关信息,我找到了一种解决方法:

我在 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 (repeated 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 请求需要它。

我仍然有未回答的问题:OAuth 过滤器文档的含义是:“将此留空以禁用特定路由的 OAuth2,使用每个过滤器配置。”

查看我的特使日志后,我意识到路径被称为标题“:路径”。
pass_through_matcher对标头进行数学运算。

然后只添加:

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

在我没有 lua 过滤器的 conf 中(请参阅我之前的答案)它可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM