首页 > 解决方案 > API 网关不返回响应

问题描述

我正在尝试使用 firebase 身份验证的 GCP API Gateway。我可以看到我的请求已从日志中处理并以响应代码 200 完成。但是,我没有将响应返回给我的客户。当我直接调用该函数时,我得到了响应。我错过了什么吗?

API 配置

swagger: "2.0"
info:
  title: API Endpoints
  description: API Endpoints
  version: 1.0.1
schemes:
  - https
produces:
  - application/json
securityDefinitions:
  firebase:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    x-google-issuer: "https://securetoken.google.com/my-project"
    x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"
    x-google-audiences: "my-project"
paths:
  /hello:
    get:
      summary: Test link
      operationId: hello
      x-google-backend:
        address: https://us-central1-my-project.cloudfunctions.net/hello
      security:
        - firebase: []
      responses:
        "200":
          description: A successful response
          schema:
            type: string
        "403":
          description: Failed to authenticate

功能

 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

日志

日志

附加查询 最初,我的函数是私有的并且得到 403。一旦我添加到我试图调用的函数的权限,它给了我allUsers200 Cloud Functions Invoker。所以我在这里有点困惑。我使用 API 网关的部分原因firebase auth是为了保护它免受未经授权的调用。为了firebase auth工作,我必须添加allUsers,使其公开。我的理解是,API 网关本身就是公共的,而它调用的所有后端服务都是私有的。在这种情况下,任何人都可以直接调用该函数,从而导致 API Gateway 无用。如何将后端设置为私有并仅通过 API 网关响应经过身份验证的调用?

附加日志

{
 insertId: "8c13b49c-2752-4216-8188-d445f4724ef14850908905639612439@a1"  
 jsonPayload: {
  api_key_state: "NOT CHECKED"   
  api_method: "1.myapi.GenerateRTCToken"   
  api_name: "1.myapi"   
  api_version: "1.0.1"   
  client_ip: "999.999.999.999"   
  http_method: "GET"   
  http_response_code: 200   
  location: "us-central1"   
  log_message: "1.myapi.GenerateRTCToken is called"   
  producer_project_id: "myproject"   
  request_latency_in_ms: 161   
  request_size_in_bytes: 4020   
  response_size_in_bytes: 579   
  service_agent: "ESPv2/2.17.0"   
  service_config_id: "myapi-config"   
  timestamp: 1606313914.9804168   
  url: "/generateRTCToken"   
 }
 logName: "projects/myproject/logs/myapi%2Fendpoints_log"  
 receiveTimestamp: "2020-11-25T14:18:36.521292489Z"  
 resource: {
  labels: {
   location: "us-central1"    
   method: "1.myapi.GenerateRTCToken"    
   project_id: "myproject"    
   service: "myapi"    
   version: "1.0.1"    
  }
  type: "api"   
 }
 severity: "INFO"  
 timestamp: "2020-11-25T14:18:34.980416865Z"  
}

标签: google-cloud-platformgoogle-cloud-functionsgoogle-cloud-api-gateway

解决方案


Google Cloud FunctionApi-Gateway不公开的情况下调用,您可以授予API-Gateway角色使用的服务帐户CloudFunctions Invoker

创建 API 配置 (4)时:设置服务帐户,记下此服务帐户。

确定服务帐户后,您可以将Cloud Funtions Invoker角色授予服务帐户。对于这些,您可以按照以下步骤操作:

  • 前往IAM&Admin
  • 查找服务帐户,然后单击它旁边的铅笔(如果您没有看到服务帐户,请单击添加并输入服务帐户电子邮件)
  • 角色选择Cloud Functions Invoker
  • 点击保存

这将授予服务帐户在不公开函数的情况下调用函数的权限。


推荐阅读