【问题标题】:Enable CORS for Cloud Run with Cloud Endpoints v1使用 Cloud Endpoints v1 为 Cloud Run 启用 CORS
【发布时间】:2020-11-27 01:14:50
【问题描述】:

我一直在关注Medium 上的一篇文章,在托管 REST API 的 Cloud Run 服务前部署 Cloud Endpoints v1,一切正常。

我现在需要启用 CORS 支持,并且我已将以下配置添加到我的端点 YAML 文件中,但是当我的浏览器尝试发出飞行前请求时出现错误消息“此服务不允许 CORS 流量” (我也用 Postman 测试过同样的错误)。我知道有一个标志可以使用环境变量启用 CORS --cors_preset=basic,但我不确定要设置什么键。任何想法或帮助表示赞赏。

端点 YAML 狙击手:

swagger: '2.0'
info:
  title: Cloud Endpoints with Cloud Run
  description: Testing Cloud Endpoints with Cloud Run
  version: 1.0.0
host: endpoint-<hash>-uc.a.run.app
x-google-endpoints:
- name: endpoint-<hash>-uc.a.run.app
  allowCors: true
schemes:
  - https
produces:
  - application/json

错误:

{
    "code": 7,
    "message": "The service does not allow CORS traffic.",
    "details": [
        {
            "@type": "type.googleapis.com/google.rpc.DebugInfo",
            "stackEntries": [],
            "detail": "service_control"
        }
    ]
}

PS:感谢Guillaum Blaquiere 的精彩文章。

更新: 我最终使用不完整的 URL 进行了测试,因此收到了上述错误,因为我的后端服务未配置为响应所有飞行前请求 URL。修复此问题后,我现在仅在 CORS 飞行前配置的 URL 上收到以下错误。

{
  "code": 13,
  "message": "INTERNAL_SERVER_ERROR",
  "details": [
    {
      "@type": "type.googleapis.com/google.rpc.DebugInfo",
      "stackEntries": [
        
      ],
      "detail": "application"
    }
  ]
}

和日志:

invalid URL prefix in "", client: <CLIENT_IP>, server: , request: "OPTIONS /api/v1/<REMAINING_URL> HTTP/1.1", host: "endpoint-<HASH>-uc.a.run.app"

【问题讨论】:

    标签: google-cloud-endpoints google-cloud-run


    【解决方案1】:

    我会说有必要添加ESPv2 Config,我注意到自去年 4 月以来添加了有关 ESPv2 配置的注释,而 Medium 文档于 2019 年发布,所以我认为之前没有提到过这样的必要步骤.

    稍后在同一部分中提到了 cors 的标志由 deploy 命令的“--set-env-vars”标志传递。

    您可以在此处找到有关ESPv2 Beta startup options 的更多信息。

    【讨论】:

    • 感谢您的回答。我正在使用 gcr.io/endpoints-release/endpoints-runtime-serverless:1 作为 Cloud Endpoints 的映像,因此认为这不起作用,因为它们是 ESPv2 的环境变量
    • 我刚刚注意到您发布的错误,我想说您必须将 的值替换为字符串“endpoint--uc.a”。 run.app" 然后在 endpoint.yaml 适当的文件行中,我建议删除为 Cloud Functions 和 GAE 定义的行。 HASH 值是一个小写的 10 个字符的字符串。
    • 我的 YAML 文件中有实际的哈希值,但在此处将其删除以避免公开共享详细信息。
    • 我认为这是一个已知问题,请检查此SO question 是否与同一问题有关。
    • 谢谢塞尔吉奥。我设法通过在 openapi YAML 文件中为我的所有请求端点路径定义 OPTIONS 端点并处理位于我的云运行托管端点服务后面的后端服务器的 CORS 请求来解决我的问题。我将在本周末晚些时候更新完整的详细信息作为答案。我认为 openapi 的 CORS 定义没有按预期工作 -> x-google-endpoints: - name: endpoint--uc.a.run.app allowCors: true
    【解决方案2】:

    我设法通过在我的 YAML 文件中为我已经定义的每个路径定义 OPTIONS 操作来解决这个问题,没有安全性。请参阅下面的示例 YAML 文件,了解定义了 GET 和 OPTIONS 操作的端点路径“/api/v1/hello”。

    swagger: '2.0'
    info:
      title: Cloud Endpoints with Cloud Run
      description: Testing Cloud Endpoints with Cloud Run
      version: 1.0.0
    host: endpoint-randomhash-uc.a.run.app
    x-google-endpoints:
      - name: endpoint-randomhash-uc.a.run.app
        allowCors: true
    schemes:
      - https
    produces:
      - application/json
    x-google-backend:
      address: https://backend-randomhash-uc.a.run.app
      path_translation: APPEND_PATH_TO_ADDRESS
    security:
      - auth0_jwk: []
    paths:
      /api/v1/hello:
        get:
          summary: Say hello
          operationId: helloName
          parameters:
            - name: "name"
              in: "query"
              description: "Your name"
              type: "string"
          responses:
            '200':
              description: Successful operation
              schema:
                type: string
        options:
          summary: CORS pre-flight for say hello
          operationId: helloNameOptions
          parameters:
            - name: "name"
              in: "query"
              description: "Your name"
              type: "string"
          responses:
            '200':
              description: Successful operation
              schema:
                type: string
          security: []
    securityDefinitions:
      auth0_jwk:
        authorizationUrl: ""
        flow: "implicit"
        type: "oauth2"
        x-google-issuer: "https://project.auth0.com/"
        x-google-jwks_uri: "https://project.auth0.com/.well-known/jwks.json"
        x-google-audiences: "firebase-application-host"
    

    正如 Sergio 在他对 SO question 的评论中指出的那样,我的另一个选择是使用 Firebase Hosting proxy 来使用相同的域并避免使用 CORS。

    【讨论】:

      猜你喜欢
      • 2017-02-19
      • 2012-07-27
      • 2019-10-27
      • 1970-01-01
      • 2020-02-24
      • 2021-01-21
      • 2018-07-06
      • 2016-06-12
      • 2021-09-14
      相关资源
      最近更新 更多