Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Push events to Google Cloud Pub/Sub

Patryk Zawadzki12235a227e34
diff --git a/.isort.cfg b/.isort.cfg
index e63075b9ed..72d6f93212 100644
--- a/.isort.cfg
+++ b/.isort.cfg
@@ -1,2 +1,2 @@
[settings]
-known_third_party = PIL,babel,braintree,celery,dj_database_url,dj_email_url,django,django_cache_url,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_relay,html_to_draftjs,i18naddress,jaeger_client,jwt,markdown,measurement,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,pkg_resources,prices,promise,pytest,pythonjsonlogger,pytimeparse,pytz,razorpay,requests,sentry_sdk,storages,stripe,templated_email,tqdm,versatileimagefield,weasyprint
+known_third_party = PIL,babel,braintree,celery,dj_database_url,dj_email_url,django,django_cache_url,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_relay,html_to_draftjs,i18naddress,jaeger_client,jwt,markdown,measurement,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,pkg_resources,prices,promise,pytest,pythonjsonlogger,pytimeparse,pytz,razorpay,requests,sentry_sdk,storages,stripe,templated_email,tqdm,versatileimagefield,weasyprint
diff --git a/poetry.lock b/poetry.lock
index aa14b552b7..50d8ba6162 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -839,6 +839,10 @@ requests = ">=2.18.0,<3.0.0dev"
setuptools = ">=34.0.0"
six = ">=1.10.0"
+[package.dependencies.grpcio]
+optional = true
+version = ">=1.29.0,<2.0dev"
+
[package.extras]
grpc = ["grpcio (>=1.29.0,<2.0dev)"]
grpcgcp = ["grpcio-gcp (>=0.2.2)"]
@@ -876,6 +880,21 @@ google-api-core = ">=1.16.0,<2.0.0dev"
[package.extras]
grpc = ["grpcio (>=1.8.2,<2.0dev)"]
+[[package]]
+category = "main"
+description = "Google Cloud Pub/Sub API client library"
+name = "google-cloud-pubsub"
+optional = false
+python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*"
+version = "1.7.0"
+
+[package.dependencies]
+grpc-google-iam-v1 = ">=0.12.3,<0.13dev"
+
+[package.dependencies.google-api-core]
+extras = ["grpc"]
+version = ">=1.14.0,<1.17.0 || >=1.20.0"
+
[[package]]
category = "main"
description = "Google Cloud Storage API client library"
@@ -1042,6 +1061,32 @@ graphql-core = ">=2.2,<3"
promise = ">=2.2,<3"
six = ">=1.12"
+[[package]]
+category = "main"
+description = "GRPC library for the google-iam-v1 service"
+name = "grpc-google-iam-v1"
+optional = false
+python-versions = "*"
+version = "0.12.3"
+
+[package.dependencies]
+grpcio = ">=1.0.0,<2.0.0dev"
+
+[package.dependencies.googleapis-common-protos]
+extras = ["grpc"]
+version = ">=1.5.2,<2.0.0dev"
+
+[[package]]
+category = "main"
+description = "HTTP/2-based RPC framework"
+name = "grpcio"
+optional = false
+python-versions = "*"
+version = "1.30.0"
+
+[package.dependencies]
+six = ">=1.5.2"
+
[[package]]
category = "main"
description = "Convert basic HTML into DraftJS JSON format."
@@ -2316,7 +2361,8 @@ idna = ">=2.0"
multidict = ">=4.0"
[metadata]
-content-hash = "cac9dd601359e308f2d4a960a4b007518f3caac49315e7f22e4b2200d6f4605a"
+content-hash = "6811dfcd4176ab56a7c54bc896a21faf192b89e20321cb0f44c76657e9d19eac"
+lock-version = "1.0"
python-versions = "~3.8"
[metadata.files]
@@ -2657,6 +2703,10 @@ google-cloud-core = [
{file = "google-cloud-core-1.3.0.tar.gz", hash = "sha256:878f9ad080a40cdcec85b92242c4b5819eeb8f120ebc5c9f640935e24fc129d8"},
{file = "google_cloud_core-1.3.0-py2.py3-none-any.whl", hash = "sha256:6ae5c62931e8345692241ac1939b85a10d6c38dc9e2854bdbacb7e5ac3033229"},
]
+google-cloud-pubsub = [
+ {file = "google-cloud-pubsub-1.7.0.tar.gz", hash = "sha256:c8d098ebd208d00c8f3bb55eefecd8553e7391d59700426a97d35125f0dcb248"},
+ {file = "google_cloud_pubsub-1.7.0-py2.py3-none-any.whl", hash = "sha256:b7f577621f991b513034c50f3314ef66838701b3b0dd1fca0d5e9a0e82f9f801"},
+]
google-cloud-storage = [
{file = "google-cloud-storage-1.30.0.tar.gz", hash = "sha256:0634addb7576d48861d9963312fc82a0436042b8f282414ed58ca76d73edee54"},
{file = "google_cloud_storage-1.30.0-py2.py3-none-any.whl", hash = "sha256:02ac63059c798d4b8ba9057921be745707dc2d3316f5f366de91c24cc23cd77e"},
@@ -2715,6 +2765,42 @@ graphql-relay = [
{file = "graphql-relay-2.0.1.tar.gz", hash = "sha256:870b6b5304123a38a0b215a79eace021acce5a466bf40cd39fa18cb8528afabb"},
{file = "graphql_relay-2.0.1-py3-none-any.whl", hash = "sha256:ac514cb86db9a43014d7e73511d521137ac12cf0101b2eaa5f0a3da2e10d913d"},
]
+grpc-google-iam-v1 = [
+ {file = "grpc-google-iam-v1-0.12.3.tar.gz", hash = "sha256:0bfb5b56f648f457021a91c0df0db4934b6e0c300bd0f2de2333383fe958aa72"},
+]
+grpcio = [
+ {file = "grpcio-1.30.0-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:d5eee9d205518ee4feb9c424475ddad18a44fea97ff405780e7cd1d6df8ee96a"},
+ {file = "grpcio-1.30.0-cp27-cp27m-manylinux2010_i686.whl", hash = "sha256:3cb78f8078ae583810c2eb47e536b0803a039656685144db43897e8beca4e203"},
+ {file = "grpcio-1.30.0-cp27-cp27m-manylinux2010_x86_64.whl", hash = "sha256:d18e7fb5c5c336cc349d06cde24582e0bfa5e067fdd6268bf1519c4eb4af0199"},
+ {file = "grpcio-1.30.0-cp27-cp27m-win32.whl", hash = "sha256:0c334d6cbe27ebaa9e7211236dc99f3a9ca2ea4b3bf89b0d2544df2924343cc5"},
+ {file = "grpcio-1.30.0-cp27-cp27m-win_amd64.whl", hash = "sha256:7b47ec90cab0827679b511f7f9ef4fb0077cb5d7bb3d7b917154e718bb4d983b"},
+ {file = "grpcio-1.30.0-cp27-cp27mu-linux_armv7l.whl", hash = "sha256:872d45a2e01f47db095bec032470a8c5c0a5ebd00fc930b5ae35c756b20d2cff"},
+ {file = "grpcio-1.30.0-cp27-cp27mu-manylinux2010_i686.whl", hash = "sha256:9ae898c15d122a046f04ea99327e3e0bd10593eb413c4810b931103da6311a21"},
+ {file = "grpcio-1.30.0-cp27-cp27mu-manylinux2010_x86_64.whl", hash = "sha256:474bb992355b4a3cb8d7cb783b2d81f628c16ea921cec54ff492420e11c896f5"},
+ {file = "grpcio-1.30.0-cp35-cp35m-linux_armv7l.whl", hash = "sha256:37da010e209289085d3362f371d9feefc152790859470f5e413d84a95a8d3998"},
+ {file = "grpcio-1.30.0-cp35-cp35m-macosx_10_7_intel.whl", hash = "sha256:74e8b6bd0f7ae64a7eecfe9bf10bc7a905d3b3eb2775cd3a9fdcdafd277469dd"},
+ {file = "grpcio-1.30.0-cp35-cp35m-manylinux2010_i686.whl", hash = "sha256:b8e5194fb20f4365eacfc3c33d61662651e12e166978186faf378ee972eb0bab"},
+ {file = "grpcio-1.30.0-cp35-cp35m-manylinux2010_x86_64.whl", hash = "sha256:09bea7902adc33620d68462671942e163ab12214073ffb613d2fef3df94254f6"},
+ {file = "grpcio-1.30.0-cp35-cp35m-win32.whl", hash = "sha256:2522f1808fe41bd8807feb5330025378553745b727eacb07562319205d1fd405"},
+ {file = "grpcio-1.30.0-cp35-cp35m-win_amd64.whl", hash = "sha256:afe1f9173b51945e66c72002995eb6d4217384aaaee53215ae85d8543251fec2"},
+ {file = "grpcio-1.30.0-cp36-cp36m-linux_armv7l.whl", hash = "sha256:b934542dd61746651f7907d2d7878f62ef42fdb46935088fc6a1d8266a406ba5"},
+ {file = "grpcio-1.30.0-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:ac97beab4a749c7faf6f267f7b149f6dff4f3ad64f6f6ac1d94d04019785d6a4"},
+ {file = "grpcio-1.30.0-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:795f351ef70a931f8f7be6a10a509714ec0a6e36c674a071abe5da8eb6b8bb35"},
+ {file = "grpcio-1.30.0-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:8d3249566b2d8b97925fbb2ae6c5b63c5ebdb919828230eae06a25e9614e051b"},
+ {file = "grpcio-1.30.0-cp36-cp36m-win32.whl", hash = "sha256:32fe6369143c262d096995ebdd55eeb77f0e1dbe8343a956462ef0607527c7bc"},
+ {file = "grpcio-1.30.0-cp36-cp36m-win_amd64.whl", hash = "sha256:08362b8b09562179b14db6ffce4b88e1a6a6edac8bccb85dd35f7b214fa5a0f5"},
+ {file = "grpcio-1.30.0-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:c8ad75925e87ed68d5f7d5e3ec4b9f2ed209fae67c0abbcbd17481cc474421ba"},
+ {file = "grpcio-1.30.0-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:14743e8fdfdabbab1a2075ffafd25e0a8b1a864505e3cccdf19793766cdc4624"},
+ {file = "grpcio-1.30.0-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:0c4e316e02fc227c6fba858707baee46f30d890754fc4acdf2cfec2ea0bf0aa1"},
+ {file = "grpcio-1.30.0-cp37-cp37m-win32.whl", hash = "sha256:b0f7bfba0ae7a97b802348aba4e08b1e84988103cc1eb887241e7b069010058a"},
+ {file = "grpcio-1.30.0-cp37-cp37m-win_amd64.whl", hash = "sha256:2121afee4e3ebea7df1137bfb4dc396b1856aff4c517780108d9ce82f57bf2f8"},
+ {file = "grpcio-1.30.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:b022cedea66b7d6774bbd7d32d5a8a374947fb572da1a6915210b09a6f51cbdf"},
+ {file = "grpcio-1.30.0-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:38ab75168a9024d393bf43343960da425736038d249920955f223bc762587697"},
+ {file = "grpcio-1.30.0-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:1f45ec5003101f16673436b150bac73c2355cd9ae78cb14f3707be01a39b5450"},
+ {file = "grpcio-1.30.0-cp38-cp38-win32.whl", hash = "sha256:7f264d740906655a147448d57e4422723639d2d3f891734b8d5eb1675cb47192"},
+ {file = "grpcio-1.30.0-cp38-cp38-win_amd64.whl", hash = "sha256:31e9891ac742e6866aec0cf67f1892618982cfbaf08bdcf3bb2e0f0828530c38"},
+ {file = "grpcio-1.30.0.tar.gz", hash = "sha256:e8f2f5d16e0164c415f1b31a8d9a81f2e4645a43d1b261375d6bab7b0adf511f"},
+]
html-to-draftjs = [
{file = "html-to-draftjs-1.0.1.tar.gz", hash = "sha256:2212acf3b063c17b6dc91875f474159e1f730c1a4e5fbf74f9a66250f7040e9a"},
]
@@ -2845,11 +2931,6 @@ markupsafe = [
{file = "MarkupSafe-1.1.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6"},
{file = "MarkupSafe-1.1.1-cp37-cp37m-win32.whl", hash = "sha256:b00c1de48212e4cc9603895652c5c410df699856a2853135b3967591e4beebc2"},
{file = "MarkupSafe-1.1.1-cp37-cp37m-win_amd64.whl", hash = "sha256:9bf40443012702a1d2070043cb6291650a0841ece432556f784f004937f0f32c"},
- {file = "MarkupSafe-1.1.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:6788b695d50a51edb699cb55e35487e430fa21f1ed838122d722e0ff0ac5ba15"},
- {file = "MarkupSafe-1.1.1-cp38-cp38-manylinux1_i686.whl", hash = "sha256:cdb132fc825c38e1aeec2c8aa9338310d29d337bebbd7baa06889d09a60a1fa2"},
- {file = "MarkupSafe-1.1.1-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:13d3144e1e340870b25e7b10b98d779608c02016d5184cfb9927a9f10c689f42"},
- {file = "MarkupSafe-1.1.1-cp38-cp38-win32.whl", hash = "sha256:596510de112c685489095da617b5bcbbac7dd6384aeebeda4df6025d0256a81b"},
- {file = "MarkupSafe-1.1.1-cp38-cp38-win_amd64.whl", hash = "sha256:e8313f01ba26fbbe36c7be1966a7b7424942f670f38e666995b88d012765b9be"},
{file = "MarkupSafe-1.1.1.tar.gz", hash = "sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b"},
]
maxminddb = [
diff --git a/pyproject.toml b/pyproject.toml
index 2751cb805b..b50f0f778f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -67,6 +67,7 @@ python-json-logger = "^0.1.11"
pytimeparse = "^1.1.8"
django-redis = "^4.12.1"
Adyen = "^3.0.0"
+google-cloud-pubsub = "^1.7.0"
[tool.poetry.dev-dependencies]
black = "19.10b0"
diff --git a/saleor/plugins/webhook/__init__.py b/saleor/plugins/webhook/__init__.py
index 2e9ecea710..8049e8150e 100644
--- a/saleor/plugins/webhook/__init__.py
+++ b/saleor/plugins/webhook/__init__.py
@@ -5,19 +5,14 @@ from typing import Optional
from ...site.models import Site
-def create_hmac_signature(body: str, secret_key: str, encoding: str):
- b_body = bytes(body, encoding)
- hash = hmac.new(bytes(secret_key, encoding), b_body, hashlib.sha256)
+def signature_for_payload(body: bytes, secret_key):
+ hash = hmac.new(bytes(secret_key, "utf-8"), body, hashlib.sha256)
return hash.hexdigest()
def create_webhook_headers(
- event_name: str,
- body: Optional[str] = None,
- secret_key: Optional[str] = None,
- encoding: str = "utf-8",
+ event_name: str, body: Optional[bytes] = None, secret_key: Optional[str] = None
):
- signature_prefix = "sha1="
domain = Site.objects.get_current().domain
headers = {
"Content-Type": "application/json",
@@ -25,8 +20,6 @@ def create_webhook_headers(
"X-Saleor-Domain": domain,
}
if secret_key and body:
- saleor_hmac_sha256 = signature_prefix + create_hmac_signature(
- body, secret_key, encoding
- )
+ saleor_hmac_sha256 = signature_for_payload(body, secret_key)
headers["X-Saleor-HMAC-SHA256"] = saleor_hmac_sha256
return headers
diff --git a/saleor/plugins/webhook/tasks.py b/saleor/plugins/webhook/tasks.py
index f6b797ef39..665a96671b 100644
--- a/saleor/plugins/webhook/tasks.py
+++ b/saleor/plugins/webhook/tasks.py
@@ -1,18 +1,29 @@
+import json
import logging
+from enum import Enum
+from urllib.parse import urlparse
import requests
+from google.cloud import pubsub_v1
from requests.exceptions import RequestException
from ...celeryconf import app
+from ...site.models import Site
from ...webhook.event_types import WebhookEventType
from ...webhook.models import Webhook
-from . import create_webhook_headers
+from . import create_webhook_headers, signature_for_payload
logger = logging.getLogger(__name__)
WEBHOOK_TIMEOUT = 10
+class WebhookSchemes(str, Enum):
+ HTTP = "http"
+ HTTPS = "https"
+ GOOGLE_CLOUD_PUBSUB = "gcpubsub"
+
+
@app.task
def trigger_webhooks_for_event(event_type, data):
permissions = {}
@@ -38,17 +49,46 @@ def trigger_webhooks_for_event(event_type, data):
)
+def send_webhook_using_http(target_url, secret, event_type, data):
+ headers = create_webhook_headers(event_type, data, secret)
+ response = requests.post(
+ target_url, data=data, headers=headers, timeout=WEBHOOK_TIMEOUT
+ )
+ response.raise_for_status()
+
+
+def send_webhook_using_google_cloud_pubsub(target_url, secret, event_type, data):
+ parts = urlparse(target_url)
+ client = pubsub_v1.PublisherClient()
+ topic_name = parts.path[1:] # drop the leading slash
+ message = json.dumps(data).encode("utf-8")
+ domain = Site.objects.get_current().domain
+ signature = signature_for_payload(message, secret)
+ client.publish(
+ topic_name,
+ message,
+ saleorDomain=domain,
+ eventType=event_type,
+ signature=signature,
+ )
+
+
@app.task(
autoretry_for=(RequestException,),
retry_backoff=60,
retry_kwargs={"max_retries": 15},
)
def send_webhook_request(webhook_id, target_url, secret, event_type, data):
- headers = create_webhook_headers(event_type, data, secret)
- response = requests.post(
- target_url, data=data, headers=headers, timeout=WEBHOOK_TIMEOUT
- )
- response.raise_for_status()
+ parts = urlparse(target_url)
+ if parts.scheme.lower() in [WebhookSchemes.HTTP, WebhookSchemes.HTTPS]:
+ send_webhook_using_http(target_url, secret, event_type, data)
+ elif parts.scheme.lower() == WebhookSchemes.GOOGLE_CLOUD_PUBSUB:
+ send_webhook_using_google_cloud_pubsub(target_url, secret, event_type, data)
+ else:
+ raise ValueError("Unknown webhook scheme: %r" % (parts.scheme,))
logger.debug(
- f"[Webhook ID:{webhook_id}] Payload sent to {target_url} for event {event_type}"
+ "[Webhook ID:%r] Payload sent to %r for event %r",
+ webhook_id,
+ target_url,
+ event_type,
)
diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.py
index 8b7b071e8a..2d3215db7f 100644
--- a/saleor/plugins/webhook/tests/test_webhook.py
+++ b/saleor/plugins/webhook/tests/test_webhook.py
@@ -14,7 +14,7 @@ from ....webhook.payloads import (
generate_product_payload,
)
from ...manager import get_plugins_manager
-from ...webhook import create_hmac_signature
+from ...webhook import signature_for_payload
from ...webhook.tasks import trigger_webhooks_for_event
@@ -120,14 +120,12 @@ def test_trigger_webhooks_for_event_with_secret_key(
expected_data = serialize("json", [order_with_lines])
trigger_webhooks_for_event(WebhookEventType.ORDER_CREATED, expected_data)
- expected_signature = create_hmac_signature(
- expected_data, webhook.secret_key, "utf-8"
- )
+ expected_signature = signature_for_payload(expected_data, webhook.secret_key)
expected_headers = {
"Content-Type": "application/json",
"X-Saleor-Event": "order_created",
"X-Saleor-Domain": "mirumee.com",
- "X-Saleor-HMAC-SHA256": f"sha1={expected_signature}",
+ "X-Saleor-HMAC-SHA256": expected_signature,
}
mock_request.assert_called_once_with(
diff --git a/saleor/webhook/migrations/0006_auto_20200731_1440.py b/saleor/webhook/migrations/0006_auto_20200731_1440.py
new file mode 100644
index 0000000000..195ef0542f
--- /dev/null
+++ b/saleor/webhook/migrations/0006_auto_20200731_1440.py
@@ -0,0 +1,20 @@
+# Generated by Django 3.0.8 on 2020-07-31 14:40
+
+from django.db import migrations
+
+import saleor.webhook.models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("webhook", "0005_drop_manage_webhooks_permission"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="webhook",
+ name="target_url",
+ field=saleor.webhook.models.WebhookURLField(max_length=255),
+ ),
+ ]
diff --git a/saleor/webhook/models.py b/saleor/webhook/models.py
index 4db34a466c..b6ac17bc4b 100644
--- a/saleor/webhook/models.py
+++ b/saleor/webhook/models.py
@@ -1,12 +1,19 @@
+from django.core import validators
from django.db import models
from ..app.models import App
+class WebhookURLField(models.URLField):
+ default_validators = [
+ validators.URLValidator(schemes=["http", "https", "gcpubsub"])
+ ]
+
+
class Webhook(models.Model):
name = models.CharField(max_length=255, null=True, blank=True)
app = models.ForeignKey(App, related_name="webhooks", on_delete=models.CASCADE)
- target_url = models.URLField(max_length=255)
+ target_url = WebhookURLField(max_length=255)
is_active = models.BooleanField(default=True)
secret_key = models.CharField(max_length=255, null=True, blank=True)