【问题标题】:Anthos Service Mesh/Istio CORS not enabledAnthos Service Mesh/Istio CORS 未启用
【发布时间】:2020-12-17 17:08:59
【问题描述】:

我正在尝试使用 Istio 入口网关在具有 Anthos Service Mesh 1.8 的 GKE 集群上启用 CORS,但未正确返回 CORS 标头。

这里是服务配置

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: ClusterIP
  selector:
    app: my-service
  ports:
  - name: http
    port: 8080
    targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-service
spec:
  selector:
    matchLabels:
      app: my-service
  template:
    metadata:
      labels:
        app: my-service
    spec:
      serviceAccountName: ksa
      containers:
        - name: my-service
          image: <my image>
          ports:
            - name: http-server
              containerPort: 8080

和入口配置

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: istio-gateway
spec:
  selector:
    istio: ingressgateway 
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - "*"
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: istio-ingress
spec:
  hosts:
    - "*"
  gateways:
    - istio-gateway
  http:
    - name: default-public-route
      route:
        - destination:
            host: my-service
      corsPolicy:
        allowOrigins:
          - exact: "*"
        allowMethods:
          - GET
          - POST
          - PATCH
          - PUT
          - DELETE
          - OPTIONS
        allowCredentials: false
        allowHeaders:
          - authorization
        maxAge: "24h"

我也在使用this在线休息客户端来测试端点,如果我使用http前缀,我会得到不同的响应

&lt;ingress ip&gt;/mypath 我得到 403 Forbidden 错误,而http://&lt;ingress ip&gt;/mypath 我得到一个通用 CORS 未启用。如果我从 Postman 执行 api,一切正常,但不会返回 CORS 标头。我也尝试直接从 Flask 应用程序设置 CORS,但没有任何改变。

你知道如何解决吗?

谢谢

【问题讨论】:

  • 你能用allowOrigins: - regex: '*'试试吗?
  • 如果是正则表达式,它可能应该是 .* 。但我认为,与* 完全相同应该也可以

标签: google-cloud-platform istio google-anthos


【解决方案1】:

这应该可以,我刚刚测试过。

allowOrigins:
 - exact: "*"

根据我的经验,Istio CORS 选项是对通用 CORS 功能的一个相当薄的包装,所以我猜真正的问题不在于 Istio,而在于 CORS 配置。

allowCredentialsallowHeaders 可能有问题。此外,如果您有 Istio AuthorizationPolicy,则可能应该允许 HTTP OPTIONS 调用用于飞行前请求。

【讨论】:

  • 我认为问题在于 Content-Type 标头通过 GET 请求传递
猜你喜欢
  • 2022-10-26
  • 1970-01-01
  • 2023-02-25
  • 2020-04-23
  • 2021-07-12
  • 1970-01-01
  • 2019-11-30
  • 2019-09-01
  • 2020-12-12
相关资源
最近更新 更多