【问题标题】:Conditional Istio EnvoyFilter based on Cookie presence基于 Cookie 存在的条件 Istio EnvoyFilter
【发布时间】:2022-01-10 06:12:29
【问题描述】:

目前我们正在使用 EnvoyFilter 来添加身份验证检查。这个过滤器使用了 envoy ext_authz 过滤器。

我们有一个用于匿名和授权调用的 GraphQL 端点。由于公共呼叫不需要身份验证检查/过滤器,我希望能够基于 cookie 跳过此过滤器。我尝试在网上搜索解决方案,但找不到。

请参阅下面的过滤器配置:

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: oathkeeper
  namespace: istio-system
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
    - applyTo: HTTP_FILTER
      match:
        context: GATEWAY
        listener:
          filterChain:
            filter:
              name: "envoy.filters.network.http_connection_manager"
              subFilter:
                name: "envoy.filters.http.router"
      patch:
        operation: INSERT_BEFORE
        value:
          name: envoy.filters.http.ext_authz
          typed_config:
            '@type': type.googleapis.com/envoy.extensions.filters.http.ext_authz.v3.ExtAuthz
            failure_mode_allow: false
            http_service:
              path_prefix: /decisions
              server_uri:
                uri: http://oathkeeper-api.default.svc.cluster.local:4456
                cluster: outbound|4456||oathkeeper-api.default.svc.cluster.local
                timeout: 10s
              authorization_request:
                allowed_headers:
                  patterns:
                  - exact: accept
                  - exact: authorization
                  - exact: cookie
                  - exact: content-type
                  - exact: x-forwarded-for
                  - exact: x-forwarded-proto
                  - exact: x-forwarded-host
              authorization_response:
                allowed_upstream_headers:
                  patterns:
                  - exact: authorization

【问题讨论】:

    标签: kubernetes istio envoyproxy istio-gateway


    【解决方案1】:

    您可以为匹配缺少 cookie 的端点配置第二个 VirtualService 路由

    [...]
      http:
      - name: secured-graph-ql-endpoint
        match:
        - headers:
            cookie:
              regex: <some-regex>
          uri:
            prefix: /graph-ql-endpoint
        route:
        - destination:
            host: graph-ql-endpoint
            port:
              number: 8000
      - name: public-graph-ql-endpoint
        match:
        - uri:
            prefix: /graph-ql-endpoint
        route:
        - destination:
            host: graph-ql-endpoint
            port:
              number: 8000
    

    根据您的 cookie 使用正则表达式 (&lt;some-regex&gt;)。你也可以有一个像authenticated: true 这样的标题并匹配它,而不是一个(丢失的)cookie。

    创建一个EnvoyFilter 以禁用该路由的ExtAuthz

    apiVersion​: ​networking.istio.io/v1alpha3 
    kind​: ​EnvoyFilter 
    metadata​: 
     ​ name​: bypass-ext-authz 
     ​ namespace​: ​istio-system 
    spec​: 
      workloadSelector​: 
     ​   labels​: 
     ​     istio​: ​ingressgateway 
     ​ configPatches​: 
     ​  - ​applyTo​: ​HTTP_ROUTE 
     ​    ​match​: 
     ​      ​routeConfiguration​: 
     ​        vhost​: 
     ​          route​: 
     ​            #​from virtual service http route name 
     ​            name​: public-graph-ql-endpoint
     ​    ​patch​: 
     ​      ​operation​: ​MERGE 
     ​      ​value​: 
             ​name​: ​custom.bypass_extauthz 
     ​        ​typed_per_filter_config​: 
     ​          ​envoy.ext_authz​: 
     ​            ​"​@type​"​: ​type.googleapis.com/envoy.extensions.filters.http.ext_authz.v3.ExtAuthzPerRoute 
     ​            ​disabled​: ​true
    

    您还可以将configPatch 添加到现有的EnvoyFilter

    话虽如此:

    从设计/安全的角度来看,我宁愿有一个不同的应用程序,它是公开的,并为匿名用户查询安全的 GraphQL 端点。这使您可以更好地控制流量,例如外部用户的速率限制、更简单的 dos 保护等。

    【讨论】:

    • 嘿克里斯感谢您的回答。我在答案中缺少的是查看如何根据缺少的 cookie 进行路由?第二件事对我来说真的没有意义,因为 GraphQL 通过单个端点工作。用于公共和私人呼叫。我只想在缺少 cookie 或标头时跳过身份验证检查。然后 GraphQL 服务判断查询是否需要授权。
    • 我更新了我的答案以显示如何根据 cookie 的存在/不存在进行路由。关于第二点:我的意思是,您不应该在同一服务上为经过身份验证和未经身份验证的用户公开端点。例如,您可以通过为未经身份验证的用户添加第二个端点作为查询经过身份验证的用户的代理来防止这种情况发生。但这只是一个旁注。
    猜你喜欢
    • 2021-08-21
    • 2022-06-29
    • 1970-01-01
    • 2020-11-22
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2021-07-21
    相关资源
    最近更新 更多