【问题标题】:Google Pub/Sub access rightsGoogle Pub/Sub 访问权限
【发布时间】:2015-11-09 10:06:50
【问题描述】:

我在我的项目 Project 1 中创建了一个主题,并且我在 Google 应用引擎上有一个应用程序,它每分钟都会向该主题发布一条消息。

我在第二个项目(项目 2)中有一台谷歌云计算机,它订阅了这个主题并接收消息。

我没有在我的 Project 2 上授予对机器的任何访问权限,但即使没有访问权限,它也设法接收消息。更准确地说,我没有写与我创建的主题相关的特定权限。

我的问题是:

1- 这正常吗? Project 2 上的机器不应该出现“禁止访问错误”吗?

2- 如何限制对某个主题的访问?

这是我订阅部分的代码:

import httplib2
import base64
import pandas
import json
from apiclient import discovery
from oauth2client import client as oauth2client
from oauth2client.client import SignedJwtAssertionCredentials
from oauth2client.client import GoogleCredentials

def create_pubsub_client(http=None):
    credentials = GoogleCredentials.get_application_default()
    if not http:
        http = httplib2.Http()
    credentials.authorize(http)
    return discovery.build('pubsub', 'v1', http=http)

client = create_pubsub_client()
# You can fetch multiple messages with a single API call.
batch_size = 1
subscription_str = 'projects/<myproject1>/subscriptions/testo'
# Create a POST body for the Pub/Sub request
body = {
    # Setting ReturnImmediately to false instructs the API to wait
    # to collect the message up to the size of MaxEvents, or until
    # the timeout.
    'returnImmediately': False,
    'maxMessages': batch_size,
}
while True:
    resp = client.projects().subscriptions().pull(
        subscription=subscription_str, body=body).execute()
    received_messages = resp.get('receivedMessages')
    if received_messages is not None:
        ack_ids = []
        for received_message in received_messages:
            pubsub_message = received_message.get('message')
            if pubsub_message:
                # Process messages
                msg =  base64.b64decode(str(pubsub_message.get('data')))
                treatment(msg)
                # Get the message's ack ID
                ack_ids.append(received_message.get('ackId'))
        # Create a POST body for the acknowledge request
        ack_body = {'ackIds': ack_ids}
        # Acknowledge the message.
        client.projects().subscriptions().acknowledge(
            subscription=subscription_str, body=ack_body).execute()

【问题讨论】:

    标签: google-app-engine google-compute-engine credentials google-cloud-pubsub


    【解决方案1】:

    项目 2 中的机器访问项目 1 中的主题/订阅的能力完全取决于机器的身份验证方式。如果它通过对两个项目都具有权限的东西进行身份验证,例如,您的开发者帐户,那么您将能够访问项目 1 中主题的订阅。这是正常的。

    如果您想限制访问,请在项目 1 中创建一个service account,并将您的主题和/或订阅的权限设置为仅允许该服务帐户。您可以在 Google Developers Console 的 Pub/Sub 部分执行此操作。然后,只有通过该服务帐户进行身份验证的机器才能访问它们。

    【讨论】:

    • 如何将订阅权限设置为仅允许该服务帐户?我在 Google Developers Console 中看不到任何指定订阅权限的方法?
    • @sweeeeeet,在 Google Developers Console 的 Pub/Sub 部分中公开了设置主题权限的功能,但不能设置订阅权限。为此,您可以使用API Explorer
    猜你喜欢
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2017-09-22
    • 1970-01-01
    • 2018-10-31
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多