【问题标题】:Letting upstream handle cors requests让上游处理 cors 请求
【发布时间】:2020-06-30 04:56:28
【问题描述】:

我正在尝试设置一个已经处理 CORS 请求的服务,并希望保持这种方式,而不是在边缘代理上处理 CORS 请求。

cors 字段留空根本没有帮助。

有没有办法通过大使实现这一目标?

【问题讨论】:

    标签: ambassador


    【解决方案1】:

    除非您在MappingModule 配置中设置cors 参数,否则大使将不会处理CORS。

    即使已设置,Envoy 处理 CORS 的方式似乎也是您正在寻找的行为。

    查看本期 https://github.com/envoyproxy/envoy/issues/300#issuecomment-296796675 中的链接评论,我们可以看到 Envoy 是如何选择实现它的 CORS 过滤器的。具体来说:

    1. 为响应中的 CORS 标头分配值:对于上面表 1 中指定的每个标头:

      一个。让 value 成为标题配置的选项

      b.如果未定义值,则继续下一个标题

      c。否则,为指定的配置选项写入响应头

    这意味着 Envoy 将首先获取上游服务设置的标头的值,如果未在响应中设置,则仅将其写入配置的值。

    您可以通过创建到 httpbin.org(处理 CORS)的路由并在 Mapping 中设置 cors 参数来测试这一点。

    ---
    apiVersion: getambassador.io/v2
    kind: Mapping
    metadata:
      name: cors-httpbin
    spec:
      prefix: /httpbin/
      service: httpbin.org
      cors:
        origins:
        - http://foo.example
        methods:
        - POST
        - OPTIONS
    

    上面的 Mapping 应该配置 Envoy 以将 access-control-allow-originsaccess-control-allow-methods 标头分别设置为 http://foo.example.comPOST。但是,在向该端点发送测试请求后,我们可以看到我们在响应中得到了非常不同的 CORS 标头:

    curl https://aes.example.com/httpbin/headers -v -H "Origin: http://bar.example.com" -H "Access-Control-Request-Method: GET" -X OPTIONS
    *   Trying 34.74.58.157:443...
    * Connected to aes.example.com (10.11.12.100) port 443 (#0)
    * TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
    * Server certificate: aes.example.com
    * Server certificate: Let's Encrypt Authority X3
    * Server certificate: DST Root CA X3
    > OPTIONS /httpbin/headers HTTP/1.1
    > Host: aes.example.com
    > User-Agent: curl/7.69.0
    > Accept: */*
    > Origin: http://bar.example.com
    > Access-Control-Request-Method: GET
    > 
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    < date: Thu, 19 Mar 2020 13:25:48 GMT
    < content-type: text/html; charset=utf-8
    < content-length: 0
    < server: envoy
    < allow: HEAD, OPTIONS, GET
    < access-control-allow-origin: http://bar.example.com
    < access-control-allow-credentials: true
    < access-control-allow-methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
    < access-control-max-age: 3600
    < x-envoy-upstream-service-time: 33
    < 
    * Connection #0 to host aes.example.com left intact
    

    这是因为 httpbin.org 上游在响应中设置了这些标头,因此 Envoy 默认使用它们,而不是强制使用我们提供的 CORS 配置。通过这种方式,Envoy 真正充当了 CORS 设置的默认设置,并允许上游设置或多或少的限制性配置,因为他们认为合适。

    这种行为可能会令人困惑,并让我很头疼,试图弄清楚它。我希望我能帮助你清理它。

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 2020-06-30
      • 1970-01-01
      • 2014-10-31
      • 2019-10-17
      • 2017-11-01
      • 2015-03-15
      • 2016-05-18
      • 1970-01-01
      相关资源
      最近更新 更多