Google API 网关:提供 API 输入 header

Google API Gateway: Provide API key in header

我正在尝试设置 Google API 网关以使用呼叫者在 header.
中发送的 API 密钥 我的 api 配置 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 

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

但实际上只有 /foo-query 表现符合预期。
即使在请求 header.

中未提供 API 密钥,对 /foo-header 的请求也会传递到后端

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

当在请求 header 中提供 API 密钥时,Google API 网关 应该 可以正常工作因为 Google API 网关文档指出:

A developer generates an API key in a project in the Cloud Console and embeds that key in every call to your API as a query parameter or in a request header.

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

为此,我一直在关注 Google API 网关的 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 的请求传递到后端。


我建议您 report this issue on the Public Issue Tracker,以便合适的 GCP 工程团队对其进行审核。

inheader时,name应为x-api-key

https://cloud.google.com/endpoints/docs/openapi/openapi-limitations#api_key_definition_limitations