google-cloud-platform - API 网关不返回响应

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

我正在尝试使用 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);
};
日志
Logs
附加查询
最初,我的函数是私有(private)的,得到 403。一旦我添加 allUsers,它给了我 200。与 Cloud Functions Invoker我试图调用的函数的权限。所以我在这里有点困惑。我将 API 网关与 firebase 一起使用的部分原因auth是为了保护它免受未经授权的调用。对于 firebase auth要工作,我必须添加 allUsers ,公开。我的理解是,API 网关本身就是公共(public)的,而它调用的所有后端服务都是私有(private)的。在这种情况下,任何人都可以直接调用该函数,从而使 API Gateway 无用。如何将后端设置为私有(private)并仅通过 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 Function来自 Api-Gateway在不公开的情况下,您可以授予 API-Gateway 使用的服务帐户角色CloudFunctions InvokerCreating an API config (4) :你设置了一个服务账号,记下这个服务账号。
确定服务帐户后,您可以授予它 Cloud Funtions Invoker服务帐户的角色。对于这些,您可以按照以下步骤操作:

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

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

    关于google-cloud-platform - API 网关不返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64667633/

    相关文章:

    google-app-engine - Google Cloud Platform 上的双向 TLS 身份验证

    reactjs - 尝试从新的 Google Cloud API Gateway 获取时出现 CORS 错误

    google-cloud-api-gateway - Google Cloud API Gateway 是否带有可配置的超时?

    linux - 如何为 gcloud 的 linux 服务器分配外部 ip?

    kubernetes - 从 GKE 集群中删除节点后,GEK 不会自动启动新节点

    javascript - 如何调用 Firebase Cloud 函数中的两个函数

    google-cloud-functions - GCP API 网关到 Cloud Functions 默认服务帐户身份验证 : "Your client does not have permission to the requested URL""

    ssl - 带有谷歌管理证书的 argocd ssl 证书

    google-app-engine - GCP 中的 AWS Elastic Beanstalk 相当于什么?

    node.js - 在 Firebase 函数和 React 之间共享代码