Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/olympia/abuse/management/commands/fake_cinder_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import hashlib
import hmac
import os.path
from urllib.parse import urljoin

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.urls import reverse
from django.utils.encoding import force_bytes

import requests


class Command(BaseCommand):
help = 'Send fake webhook request to local abuse response API like Cinder would.'

def add_arguments(self, parser):
parser.add_argument(
'--payload',
dest='payload_filename',
default=os.path.join(settings.ROOT, 'tmp/payload.json'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be nicer if the command didn't traceback when the default filepath didn't exist, and instead returned the help text.

help='Path to filename containing payload (default: %(default)s)',
)

def handle(self, *args, **options):
if settings.ENV != 'local':
raise CommandError('Only works in local environments')
body = open(options['payload_filename'], 'rb').read()
signature = hmac.new(
force_bytes(settings.CINDER_WEBHOOK_TOKEN),
msg=body,
digestmod=hashlib.sha256,
).hexdigest()
url = urljoin('http://nginx', reverse('v5:cinder-webhook'))
response = requests.post(
url,
data=body,
headers={
'X-Cinder-Signature': signature,
'Content-Type': 'application/json',
},
)
self.stdout.write(f'{response.status_code}\n')
self.stdout.write(f'{response.content}\n')
Loading