【问题标题】:How to make specific endpoints private or atleast restricted in GCP App Engine Flex?如何在 GCP App Engine Flex 中将特定端点设为私有或至少受限制?
【发布时间】:2020-01-09 03:06:30
【问题描述】:

我有一个循环的 Cron 调度程序,它调用 Google Cloud Platform App Engine Flex Service(.net 核心 Web API 说 APP1)中的一个方法,该方法又调用我们的另一个 Google Cloud Platform App Engine Flex Services(另一个 .net核心 Web API 说 APP2)端点(例如:/v1/api/test)

我的问题是我将如何限制对这个特定 APP2 端点的访问仅限于 APP1?我必须使用 Cloud Endpoints 来实现这一点吗?请记住,APP2 还有其他对公众开放的端点。

【问题讨论】:

  • 云端点可能是您最好的选择,因为它们本机支持这一点,或者您可以实现您的身份验证机制,可能使用服务帐户凭据

标签: google-app-engine .net-core google-cloud-platform


【解决方案1】:

要限制从服务应用程序 2 访问服务应用程序 1,而不是一般公众,您必须使用 Cloud Endpoints。为了做到这一点,请按照以下步骤操作:

1) 您必须创建一个转到 Cloud Endpoints 的 openapi-appengine.yaml 文件。配置如下:

  swagger: '2.0'
  info:
    title: Cloud Endpoints
    description: Sample API on Cloud Endpoints with a Cloud Run backend
    version: 1.0.0
  host: endpoint-service.appspot.com   ---> you've to put your service URL
  x-google-allow: all
  schemes:
    - https
  produces:
    - application/json
  paths:
    /resdticted-endpoint-1:             --> Here you've to put all the endpoints you want to restrict
      get:
        summary: Greet a user
        operationId: hello
        responses:
          '200':
            description: A successful response
            schema:
              type: string
        security:
          - DEFINITION_NAME: []

    /resdticted-endpoint-2:
      get:
        summary: Greet a user
        operationId: hello
        responses:
          '200':
            description: A successful response
            schema:
              type: string
        security:
          - DEFINITION_NAME: []

securityDefinitions:                      
  DEFINITION_NAME:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    x-google-issuer: "SA_EMAIL_ADDRESS"   --> Here you've to add a Service Account, in case you don't have any, create a new one
    x-google-jwks_uri: "https://www.googleapis.com/robot/v1/metadata/x509/SA_EMAIL_ADDRESS"  --> put your service account name at the end

2) 然后,转到 App2 服务: - 将您的服务名称添加到 app.yaml 文件中:

   endpoints_api_service:
   # The following values are to be replaced by information from the output of
   # 'gcloud endpoints services deploy openapi-appengine.yaml' command.
   name: ENDPOINTS-SERVICE-NAME
   rollout_strategy: managed
  • 将 ENDPOINTS-SERVICE-NAME 替换为您的 Endpoints 服务的名称。这与您在 OpenAPI 文档的主机字段中配置的名称相同。例如:

    endpoints_api_service: 名称:example-project-12345.appspot.com rollout_strategy:托管

3) 最后继续这个Official Documentation 以便在服务之间进行身份验证。

【讨论】:

  • 这会让我只限制特定的端点,同时让其他人可以访问吗?
  • 是的。这正是我最终要做的。现在我被困在 JWT 部分。如果您想看一下,请将其作为另一个问题发布。 stackoverflow.com/questions/59687609/…。谢谢!
【解决方案2】:

调用私有端点时,可以使用X-Appengine-Inbound-AppId查看源APP。

请参考doc

【讨论】:

  • 看起来只能通过 URL Fetch Service 进来。不要认为 .net core 支持它。
  • 您可以创建自己的 url_fetch 服务并使用 AuthorizedSession (cloud.google.com/docs/authentication/production) 从 APP1 调用 APP2 并调用 APP2。在 APP2 中,您可以检查私有端点的请求令牌。
猜你喜欢
  • 2021-01-22
  • 1970-01-01
  • 2019-08-25
  • 2018-05-11
  • 2019-04-11
  • 2015-06-09
  • 1970-01-01
  • 2018-04-20
  • 2021-12-09
相关资源
最近更新 更多