Give a Gmail-API outside our google cloud Pub/Sub publisher privileges - google-cloud-platform

Let me try to explain to you what we are trying to do.
Saying it quickly: We want to give Pub/Sub Publisher (in our GoogleCloud) privileges to a GMail-API that is outside of our GoogleCloud.
What we have:
Following instructions here: https://developers.google.com/gmail/api/quickstart/ruby
We've created a project for GMail-API and the credentials in GoogleCloud, let's call it Cloud-A. (We'll not own this side in a production environment; this project and cloud will be managed by our customers' IT department.)
Next, we followed this other guide https://developers.google.com/gmail/api/guides/push
We created a Pub/Sub topic in our GoogleCloud (Cloud-B), we own this portion and it's the topic where we want to subscribe in order to listen for messages/notifications. (This topic is in a different account from the GMail-API that will be publishing messages that is Cloud-A).
So, following that last guide, it says that we need to give permissions to gmail-api-push#system.gserviceaccount.com and from my understanding what that means is that I'm giving privileges to GMail-API from Cloud-B to publish messages in Pub/Sub Cloud-B.
What I can't find out is a way to give permissions to Gmail-API from Cloud-A to publish messages in Pub/Sub Cloud-B.
To wrap up, I want to listen to Pub/Sub in Cloud-B that will receive notifications from Gmail-API in Cloud-A.
We used this https://github.com/googleapis/google-api-ruby-client/blob/master/generated/google/apis/gmail_v1/service.rb#L144 and I get an error saying that the topic doesn't exist (Probably because it is in Cloud-B and I'm configuring Cloud-A Gmail-Api)
I hope I was clear enough, we are not looking to given another project inside the same Google Cloud access to a Pub/Sub, it isn't even a service that we wrote since it is Gmail-Api and the only thing we are allowed to do is to send it the topic name we want it to publish in.

I'm not familiar with how the GMail-API publishes to Pub/Sub, but, if you have already figured out how to publish from GMail-API in project Cloud-A to a Pub/Sub topic in Cloud-A, you may try the following workarounds:
Alternative A:
Create the topic (topic-A) in project Cloud-A.
Create a pull subscription (subs-A) associated to topic-A also in project Cloud-A.
Create a service account (account-B) in project Cloud-B and grant it the Pub/Sub subscriber role for subscription subs-A.
Make your consumers (e.g. AppEngine, GKE, GCE) use service account account-B to pull messages from subs-A.
Alternative B:
Create the topic (topic-A) in project Cloud-A.
Create a push subscription (subs-A) associated to topic-A pointing to an endpoint of a service hosted in project Cloud-B (e.g. GCE, GKE, AppEngine, Cloud Function, etc.)
Alternative C:
Create the topic (topic-A) in project Cloud-A.
Create a pull or push subscription (subs-B) in project Cloud-B associated to topic-A in project Cloud-A. The user creating this subscription should have the Pub/Sub Editor role granted for topic-A.
Consume the messages from subs-B.

Related

Trigger Google Cloud function in one project on publish in Google PubSub from another project

There is an application already pushing to Google PubSub topic T1 in a different project and I would like to have the Google Cloud function triggered by the publishing on T1. I read the official document https://cloud.google.com/functions/docs/calling/pubsub that it is not possible to trigger cross-project and tried creating the cloud function too from the GCP console but do not see the topics from other projects even though I have access to those projects
I am looking for alternatives. Is it possible to achieve it in a different way?
The trick is to use HTTP communication and not native event communication. I mean you have to change your Background functions (that you plug on a PubSub topic) in a HTTP function.
By doing that, you also have to create a PubSub push subscription on your PubSub topic and to add the Cloud Functions HTTP URL as target.
Don't forget to configure the security part to allow only secure and authenticated communication between PubSub and your Function.
Alternatively, you can follow Piotr advice (in comment) and use other HTTP oriented product, like Cloud RUn. But the principle is still the same: PubSub push subscription is the key!

Sharing my GCP pub/sub topic with customer to publish

I am working on an assignment where our customers will sync their crm data to our systems. The sync will be ongoing process. Any best practices or suggestions on google pub/sub for sharing one of our existing (or a new one) topic that our customer will publish too and we consume? Idea here is to keep sync asynchronous.
#Alex-hong is true if your customer is on GCP. If not, you have 2 solutions:
You can generate a service account and send it the JSON key file. Define the right role on the service account (as described by Alex) and let your customer use the Service Account and to publish to the PubSub topic. This solution implies an important development by your customer (Use external JSON key, use new libraries to push messages,...)
You can deploy a Cloud Function/Cloud Run endpoint and let your customer to simply perform an HTTP request. Of course, you can secure the call with Basic Authentication or APIkey (or something like that, that you check in your Cloud Function). The function only check the security and publish to PubSub. It's often simpler and more standard for your customer.
On the last point, it could be possible to set up an ESP in place of the function, but I never tested for publishing directly to PubSub.
You can give the other party the roles/pubsub.publisher role for your specific project if you own the topic. Alternatively, if they own the topic, they can grant you the role roles/pubsub.subscriber which will let you subscribe to that topic.
For more information, see Access Control

gmail push notifications from different accounts into single cloud pub/sub

I know that it is possible to send gmail messages to cloud pub/sub using watch options in gmail API. That is great! But, I have 4 different gmail accounts and I would like all them to push notifications into a single cloud pub/sub so that I can consume them with cloud functions and redistributed among slack channels (no, it cannot be done separately on each account since the billing is controlled only by one of them. It can also be external clients gmails which obviously won't pay for that.). Any option to push notifications into single cloud pub/sub?
You can configure the notifications from all four gmail accounts to be sent to the same Cloud Pub/Sub topic. From the documentation:
Using your Cloud Pub/Sub client, create the topic that the Gmail API
should send notifications to. The topic name can be any name you
choose under your project (i.e. matching projects/myproject/topics/*,
where myproject is the Project ID listed for your project in the
Google Developers Console).
You can then send watch() requests on each of the four accounts, specifying the same topicName.

Google cloud pubsub aggregated sink

I find the docs for gcloud stuff detailed but hardly the easiest to follow. Is anyone out there able to tell me what I'm doing that means my topic never received the logs I believe it should. Many thanks:
Create a project called logproj, enable PubSub API and create a topic called mytopic.
Create organization wide aggregated sink with the destination of the PubSub topic I just created, with the sink containing admin activity logs only:
gcloud logging sinks create mysink pubsub.googleapis.com/projects/logproj-12345/topics/mytopic --include-children --organization=123456789123 --log-filter='"logName:activity" AND logName:"/logs/cloudaudit.googleapis.com%2Factivity"'
The above command completes successfully and gives me a service account called o78732a92983-3234626#gcp-sa-logging.iam.gserviceaccount.com that I need to add to the topic and give PubSub Publisher role, which I do via the Web UI.
In order to test the topic is receiving organization wide activity logs, I create a test project. Then use the log viewer and select the pubsub topic, but the only logs I can see are from me creating the topic in the logproj project.
What am I missing?
Thanks in advance.
Messages published to Pub/Sub cannot be found in the Stackdriver logs. If you want to view the messages published to your topic:
Create a subscription for your topic
Use command-line to pull messages: https://cloud.google.com/sdk/gcloud/reference/pubsub/subscriptions/pull
Alternatively, you could implement a simple subscriber. Code examples can be found here: https://cloud.google.com/pubsub/docs/pull

How can I forward a GCP Pubsub subscription's messages to another topic?

I would like to be able to forward a Google Cloud Platform (GCP) Pubsub subscription's messages to another GCP topic, which is possibly in another GCP Project.
Is this possible via Push endpoint URL (What would the url of the destination topic be?)
Some other easy configuration via API?
(workaround) Alternatively, how would you provide this functionality?
There is a Dataflow template called Cloud_PubSub_to_Cloud_PubSub that read messages from a Pubsub subscription (inputSubscription) and publish the messages to a Pubsub topic (outputTopic).
Be sure that the service account used to launch the Dataflow job has the right permissions (roles/pubsub.subscriber in the project where inputSubscription is defined or directly on inputSubscription, and roles/pubsub.publisher in the project containing outputTopic or directly on outputTopic).
You can take a look at the code on GitHub if you are familiar with Java.
The only way to do this would be to publish the messages to the other topic in a subscriber you write when it receives the messages. There is no automated way to forward messages from one topic to another.