Can webhook target GCP PubSub directly?

Viewed 1318

We're using webhooks to listen to Jira issue events. Anytime activity on a ticket occurs, the JIRA API notifies an HTTP endpoint running inside a Google Cloud Function. In this Cloud Function, we are simply forwarding the request unaltered into Pub/Sub via:

def forward_to_pubsub(request):

    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(project_id, topic_id)

    # Request body contains user, project, issue basically everything we care about
    jira_body = request.json
    publisher.publish(topic_path, data=json.dumps(jira_body).encode('utf-8'))

This seems like an unneeded hop. Is there anyway to configure Pub/Sub to be the target of a HTTP webhook? Can't figure out from docs either way.

2 Answers

It is actually possible to publish messages to a Pub/Sub topic directly over HTTP using the REST API. From the quickstart guide at https://cloud.google.com/pubsub/docs/publisher#rest -

To publish a message, send a POST request like the following:

POST  https://pubsub.googleapis.com/v1/projects/PROJECT_ID/topics/TOPIC_ID:publish
Authorization: Bearer $(gcloud auth application-default print-access-token)

Replace the following:

  • PROJECT_ID: the project ID of the project with the topic
  • TOPIC_ID: the ID of the topic

Specify the following fields in the request body:

{
"messages": [
  {
    "attributes": {
      "KEY": "VALUE",
      ...
    },
    "data": MESSAGE_DATA,
  }
]
}

Replace the following:

  • KEY: the key of a message attribute
  • VALUE: the value for the key of the message attribute
  • MESSAGE_DATA: a base64-encoded string with the message data

If the request is successful, the response is a JSON object with the message ID. The following example is a response with a message ID:

{
  "messageIds": [
    "19916711285"
  ]
}

After you publish a message, the Pub/Sub service returns the message ID to the publisher.

There is also a REST API reference and an overview of service endpoints and common instructions when working with Google REST APIs which should be helpful.

Please note that publish requests have to confirm to the projects.topic.publish method specification and so would still need to be transformed from the JIRA webhook notification.

No, it's not possible. Pub/Sub topics don't have corresponding HTTP endpoints that can be used as webhooks. If you want to trigger a Pub/Sub topic from an end-user application, you need to use a front-end server between them:

enter image description here

See https://cloud.google.com/pubsub/docs/overview#endpoints for more details.

Related