sweeeeeet
sweeeeeet

Reputation: 1819

Google Pub/Sub access rights

I created a topic in my project Project 1 and I have an app on Google app engine which posts every minute a message to this topic.

I have a google cloud compute machine in a second project (Project 2) which subscribed to this topic and receives the messages.

I did not give any access right to the machine on my Project 2, but even without access rights, It managed to receive the messages. More precisely, I did not write specific permissions associated to the topic I created.

My questions are:

1- is this normal? Shouldn't the machine on Project 2 get a "forbidden access error"?

2- how can I restrain access on a certain topic?

Here is the code of my subscription part:

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()

Upvotes: 0

Views: 1765

Answers (1)

Kamal Aboul-Hosn
Kamal Aboul-Hosn

Reputation: 17261

The ability of the machine in Project 2 to access the topic/subscription in Project 1 depends entirely on how machine is authenticated. If it is authenticated with something that has permissions on both projects, e.g., your developer account, then you would be able to access the subscription on the topic in Project 1. That is normal.

If you want to restrict the access, create a service account in Project 1 and set the permissions on your topic and/or subscription to allow only that service account. You would do so in the Pub/Sub section of the Google Developers Console. Then, only machines authenticated via that service account will be able to access them.

Upvotes: 1

Related Questions