예제 #1
0
파일: tasks.py 프로젝트: taigaio/taiga-back
def _send_request(webhook_id, url, key, data):
    serialized_data = UnicodeJSONRenderer().render(data)
    signature = _generate_signature(serialized_data, key)
    headers = {
        "X-TAIGA-WEBHOOK-SIGNATURE": signature,        # For backward compatibility
        "X-Hub-Signature": "sha1={}".format(signature),
        "Content-Type": "application/json"
    }

    if settings.WEBHOOKS_BLOCK_PRIVATE_ADDRESS:
        try:
            urls.validate_private_url(url)
        except (urls.IpAddresValueError, urls.HostnameException) as e:
            # Error validating url
            webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url,
                                                    status=0,
                                                    request_data=data,
                                                    request_headers=dict(),
                                                    response_data="error-in-request: {}".format(
                                                        str(e)),
                                                    response_headers={},
                                                    duration=0)
            _remove_leftover_webhooklogs(webhook_id)

            return webhook_log

    request = requests.Request('POST', url, data=serialized_data, headers=headers)
    prepared_request = request.prepare()

    with requests.Session() as session:
        try:
            response = session.send(prepared_request)
        except RequestException as e:
            # Error sending the webhook
            webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url, status=0,
                                                    request_data=data,
                                                    request_headers=dict(prepared_request.headers),
                                                    response_data="error-in-request: {}".format(str(e)),
                                                    response_headers={},
                                                    duration=0)
        else:
            # Webhook was sent successfully

            # response.content can be a not valid json so we encapsulate it
            response_data = json.dumps({"content": response.text})
            webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url,
                                                    status=response.status_code,
                                                    request_data=data,
                                                    request_headers=dict(prepared_request.headers),
                                                    response_data=response_data,
                                                    response_headers=dict(response.headers),
                                                    duration=response.elapsed.total_seconds())
        finally:
            _remove_leftover_webhooklogs(webhook_id)

    return webhook_log
예제 #2
0
파일: tasks.py 프로젝트: zen9073/taiga-back
def _send_request(webhook_id, url, key, data):
    serialized_data = UnicodeJSONRenderer().render(data)
    signature = _generate_signature(serialized_data, key)
    headers = {
        "X-TAIGA-WEBHOOK-SIGNATURE": signature,        # For backward compatibility
        "X-Hub-Signature": "sha1={}".format(signature),
        "Content-Type": "application/json"
    }

    if settings.WEBHOOKS_BLOCK_PRIVATE_ADDRESS:
        try:
            urls.validate_private_url(url)
        except (urls.IpAddresValueError, urls.HostnameException) as e:
            # Error validating url
            webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url,
                                                    status=0,
                                                    request_data=data,
                                                    request_headers=dict(),
                                                    response_data="error-in-request: {}".format(
                                                        str(e)),
                                                    response_headers={},
                                                    duration=0)
            _remove_leftover_webhooklogs(webhook_id)

            return webhook_log

    request = requests.Request('POST', url, data=serialized_data, headers=headers)
    prepared_request = request.prepare()

    with requests.Session() as session:
        try:
            response = session.send(prepared_request)
        except RequestException as e:
            # Error sending the webhook
            webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url, status=0,
                                                    request_data=data,
                                                    request_headers=dict(prepared_request.headers),
                                                    response_data="error-in-request: {}".format(str(e)),
                                                    response_headers={},
                                                    duration=0)
        else:
            # Webhook was sent successfully

            # response.content can be a not valid json so we encapsulate it
            response_data = json.dumps({"content": response.text})
            webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url,
                                                    status=response.status_code,
                                                    request_data=data,
                                                    request_headers=dict(prepared_request.headers),
                                                    response_data=response_data,
                                                    response_headers=dict(response.headers),
                                                    duration=response.elapsed.total_seconds())
        finally:
            _remove_leftover_webhooklogs(webhook_id)

    return webhook_log
예제 #3
0
def test_validate_good_destination_address(url):
    assert validate_private_url(url) is None
예제 #4
0
def test_validate_invalid_destination_address(url):
    with pytest.raises(HostnameException):
        validate_private_url(url)
예제 #5
0
def test_validate_bad_destination_address(url):
    with pytest.raises(IpAddresValueError):
        validate_private_url(url)