【问题标题】:Google API Gateway: Provide API key in headerGoogle API Gateway:在标头中提供 API 密钥
【发布时间】:2021-04-17 08:22:08
【问题描述】:

我正在尝试将 Google API Gateway 设置为使用调用方在标头中发送的 API 密钥。
我的 api config yaml 如下所示:

...
securityDefinitions:
  api_key_header:
    type: apiKey
    name: key
    in: header
  api_key_query:
    type: apiKey
    name: key
    in: query
paths:
  /foo-header:
    get:
      summary: Test foo endpoint
      operationId: testGet-header
      x-google-backend:
        address: "<backend address>"
        protocol: h2
        path_translation: APPEND_PATH_TO_ADDRESS
      security:
        - api_key_header: []
      responses:
        204:
          description: A successful response
  /foo-query:
    get:
      summary: Test foo endpoint
      operationId: testGet-header
      x-google-backend:
        address: "<backend address>"
        protocol: h2
        path_translation: APPEND_PATH_TO_ADDRESS
      security:
        - api_key_query: []
      responses:
        204:
          description: A successful response 

如果没有通过标头或查询参数提供有效的 API 密钥,我预计 /foo-header/foo-query 两个调用都会失败并返回 401 状态。

但实际上只有/foo-query 的行为符合预期。
即使请求标头中未提供 API 密钥,对 /foo-header 的请求也会传递到后端。

是我的配置有问题,还是在请求标头中提供 API 密钥时 Google API 网关无法正常工作?

【问题讨论】:

    标签: google-cloud-platform google-api-gateway


    【解决方案1】:
    【解决方案2】:

    似乎 Google API Gateway 应该在请求标头中提供 API 密钥时工作正常,因为 Google API Gateway 文档指出:

    开发人员在 Cloud Console 的项目中生成 API 密钥,并将该密钥作为 查询 参数或请求 标头 嵌入到对 API 的每次调用中。

    但是,我能够重现您报告的行为,因此我认为您的配置没有问题。

    为此,我一直在关注 Google API Gateway 的 GCP quickstart,对其稍作修改,以便我的 OpenAPI 规范也有 2 条路径:一个是在 query 中寻找密钥参数,而另一个在请求 header.

    paths:
      /foo-header:
        get:
          summary: Test security
          operationId: headerkey
          x-google-backend:
            address: [MY_CLOUD_FUNCTION_1]
          security:
          - api_key_header: []
          responses:
            '200':
              description: A successful response
              schema:
                type: string
      /foo-query:
        get:
          summary: Test security
          operationId: querykey
          x-google-backend:
            address: [MY_CLOUD_FUNCTION_2]
          security:
          - api_key_query: []
          responses:
            '200':
              description: A successful response
              schema:
                type: string
    securityDefinitions:
      # This section configures basic authentication with an API key.
      api_key_header:
        type: "apiKey"
        name: "key"
        in: "header"
      api_key_query:
        type: "apiKey"
        name: "key"
        in: "query"
    

    就像你一样,即使没有提供 API 密钥,我也可以看到对 /foo-header 的请求传递到后端。


    我建议您在Public Issue Trackerreport 这个问题,以便相应的 GCP 工程团队对其进行审查。

    【讨论】:

      猜你喜欢
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 2017-05-29
      • 2014-05-19
      • 2019-01-07
      • 1970-01-01
      相关资源
      最近更新 更多