Make sure that the new webhook fields are populated when firing off a job

Also, added a temporary hacky workaround for the fact that something
in our request/response stack for APIView is consuming the request
contents in an unfriendly way, preventing the `.body` @property from
working.
This commit is contained in:
Jeff Bradberry
2019-09-06 11:36:18 -04:00
parent e0a363beb8
commit 7aa424b210
2 changed files with 17 additions and 11 deletions

View File

@@ -164,6 +164,9 @@ class APIView(views.APIView):
if custom_header.startswith('HTTP_'): if custom_header.startswith('HTTP_'):
request.environ.pop(custom_header, None) request.environ.pop(custom_header, None)
# WTF, FIXME
request.body
drf_request = super(APIView, self).initialize_request(request, *args, **kwargs) drf_request = super(APIView, self).initialize_request(request, *args, **kwargs)
request.drf_request = drf_request request.drf_request = drf_request
try: try:

View File

@@ -87,7 +87,9 @@ class WebhookReceiverBase(APIView):
if not obj.webhook_key: if not obj.webhook_key:
raise PermissionDenied raise PermissionDenied
mac = hmac.new(force_bytes(obj.webhook_key), msg=force_bytes(self.request.read()), digestmod=sha1) mac = hmac.new(force_bytes(obj.webhook_key), msg=force_bytes(self.request.body), digestmod=sha1)
logger.debug("header signature: %s", self.get_signature())
logger.debug("calculated signature: %s", force_bytes(mac.hexdigest()))
if not hmac.compare_digest(force_bytes(mac.hexdigest()), self.get_signature()): if not hmac.compare_digest(force_bytes(mac.hexdigest()), self.get_signature()):
raise PermissionDenied raise PermissionDenied
@@ -112,16 +114,17 @@ class WebhookReceiverBase(APIView):
logger.debug("Webhook previously received, returning without action.") logger.debug("Webhook previously received, returning without action.")
return Response(status=status.HTTP_202_ACCEPTED) return Response(status=status.HTTP_202_ACCEPTED)
data = {
'tower_webhook_event_type': event_type,
'tower_webhook_event_guid': event_guid,
'tower_webhook_payload': request.data,
}
new_job = obj.create_unified_job( new_job = obj.create_unified_job(
webhook_service=obj.webhook_service, _eager_fields={
webhook_credential=obj.webhook_credential, 'webhook_service': obj.webhook_service,
webhook_guid=event_guid, 'webhook_credential': obj.webhook_credential,
extra_vars=json.dumps(data) 'webhook_guid': event_guid,
},
extra_vars=json.dumps({
'tower_webhook_event_type': event_type,
'tower_webhook_event_guid': event_guid,
'tower_webhook_payload': request.data,
})
) )
new_job.signal_start() new_job.signal_start()
@@ -156,7 +159,7 @@ class GitlabWebhookReceiver(WebhookReceiverBase):
def get_event_guid(self): def get_event_guid(self):
# Gitlab does not provide a unique identifier on events, so construct one. # Gitlab does not provide a unique identifier on events, so construct one.
h = sha1() h = sha1()
h.update(force_bytes(self.request.read())) h.update(force_bytes(self.request.body))
return h.hexdigest() return h.hexdigest()
def get_signature(self): def get_signature(self):