首页 > 解决方案 > Google Cloud API Gateway 对多个服务帐号进行身份验证

问题描述

对于托管在 GCP 中的 API,我计划借助具有私钥和 API 网关的服务帐户来验证消费服务(来自外部 gcp 环境)。

对于一个服务帐户 ( ),一切正常service-account-1,即:

paths:
    /hello:
      get:
        #...
        security:
          - service_account-1: []
securityDefinitions:
    service_account-1:
      authorizationUrl: ""
      flow: "implicit"
      type: "oauth2"
      x-google-issuer: "{service-account-1}@{project-id}.iam.gserviceaccount.com"
      x-google-jwks_uri: "https://www.googleapis.com/robot/v1/metadata/x509/{service-account-1}@{project-id}.iam.gserviceaccount.com"
      x-google-audiences: "{project-id}"

但是,我如何描述它以便项目中的任何服务帐户都可以访问?

编辑:我想避免每次创建新服务帐户并需要允许访问时更新 API 网关上的 openapi 配置。

更新:我最终使用 Identity-Aware Proxy 来保护 API,只允许授权的服务帐户。

标签: google-cloud-platformoauth-2.0google-oauthopenapigoogle-cloud-api-gateway

解决方案


根据官方文档:

服务之间的身份验证

您可以在 API 配置中定义多个安全定义,但每个定义必须具有不同的 x-google-issuer。如果您为每个调用服务创建了单独的服务帐户,则可以为每个服务帐户创建一个安全定义,例如:

securityDefinitions:
  service-1:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    x-google-issuer: "service-1@example-project-12345.iam.gserviceaccount.com"
    x-google-jwks_uri: "https://www.googleapis.com/robot/v1/metadata/x509/service-1@example-project-12345.iam.gserviceaccount.com"
  service-2:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    x-google-issuer: "service-2@example-project-12345.iam.gserviceaccount.com"
    x-google-jwks_uri: "https://www.googleapis.com/robot/v1/metadata/x509/service-2@example-project-12345.iam.gserviceaccount.com"

因此,您必须创建多个安全定义。


推荐阅读