From f4c47af7351aebc45f3781651fa24d0f1e939f98 Mon Sep 17 00:00:00 2001 From: Ben Bangert Date: Thu, 18 Aug 2016 12:36:04 -0700 Subject: [PATCH] fix: return 404 for invalid URL's for consistency URL's that are invalid should return a 404, just like any other invalid URL. Closes #578 --- autopush/metrics.py | 8 ++++---- autopush/settings.py | 24 +++++++++++++----------- autopush/tests/test_integration.py | 2 +- autopush/tests/test_metrics.py | 7 +++---- autopush/web/validation.py | 2 +- docs/http.rst | 5 ++++- 6 files changed, 26 insertions(+), 22 deletions(-) diff --git a/autopush/metrics.py b/autopush/metrics.py index a8047b56..ac79bdb1 100644 --- a/autopush/metrics.py +++ b/autopush/metrics.py @@ -5,7 +5,6 @@ import datadog from datadog import ThreadStats -from datadog.util.hostname import get_hostname class IMetrics(object): @@ -70,13 +69,14 @@ def timing(self, name, duration, **kwargs): class DatadogMetrics(object): """DataDog Metric backend""" - def __init__(self, api_key, app_key, flush_interval=10, + def __init__(self, api_key, app_key, hostname, flush_interval=10, namespace="autopush"): - datadog.initialize(api_key=api_key, app_key=app_key) + datadog.initialize(api_key=api_key, app_key=app_key, + host_name=hostname) self._client = ThreadStats() self._flush_interval = flush_interval - self._host = get_hostname() + self._host = hostname self._namespace = namespace def _prefix_name(self, name): diff --git a/autopush/settings.py b/autopush/settings.py index 1f49569c..941a7023 100644 --- a/autopush/settings.py +++ b/autopush/settings.py @@ -100,17 +100,6 @@ def __init__(self, pool = HTTPConnectionPool(reactor) self.agent = Agent(reactor, connectTimeout=5, pool=pool) - # Metrics setup - if datadog_api_key: - self.metrics = DatadogMetrics( - api_key=datadog_api_key, - app_key=datadog_app_key, - flush_interval=datadog_flush_interval - ) - elif statsd_host: - self.metrics = TwistedMetrics(statsd_host, statsd_port) - else: - self.metrics = SinkMetrics() if not crypto_key: crypto_key = [Fernet.generate_key()] if not isinstance(crypto_key, list): @@ -133,6 +122,19 @@ def __init__(self, if resolve_hostname: self.hostname = resolve_ip(self.hostname) + # Metrics setup + if datadog_api_key: + self.metrics = DatadogMetrics( + hostname=self.hostname, + api_key=datadog_api_key, + app_key=datadog_app_key, + flush_interval=datadog_flush_interval, + ) + elif statsd_host: + self.metrics = TwistedMetrics(statsd_host, statsd_port) + else: + self.metrics = SinkMetrics() + self.port = port self.endpoint_hostname = endpoint_hostname or self.hostname self.router_hostname = router_hostname or self.hostname diff --git a/autopush/tests/test_integration.py b/autopush/tests/test_integration.py index d56b1c1c..02514113 100644 --- a/autopush/tests/test_integration.py +++ b/autopush/tests/test_integration.py @@ -1318,7 +1318,7 @@ def test_with_key(self): yield client.send_notification( vapid=vapid, - status=400) + status=404) yield self.shut_down(client) diff --git a/autopush/tests/test_metrics.py b/autopush/tests/test_metrics.py index bbb5a9e7..40b4fbbb 100644 --- a/autopush/tests/test_metrics.py +++ b/autopush/tests/test_metrics.py @@ -5,8 +5,6 @@ from nose.tools import ok_, eq_ from mock import Mock, patch -from datadog.util.hostname import get_hostname - from autopush.metrics import ( IMetrics, DatadogMetrics, @@ -52,9 +50,10 @@ def test_basic(self, mock_reactor): class DatadogMetricsTestCase(unittest.TestCase): @patch("autopush.metrics.datadog") def test_basic(self, mock_dog): - hostname = get_hostname() + hostname = "localhost" - m = DatadogMetrics("someapikey", "someappkey", namespace="testpush") + m = DatadogMetrics("someapikey", "someappkey", namespace="testpush", + hostname="localhost") ok_(len(mock_dog.mock_calls) > 0) m._client = Mock() m.start() diff --git a/autopush/web/validation.py b/autopush/web/validation.py index 36c74e4b..ef4a29e9 100644 --- a/autopush/web/validation.py +++ b/autopush/web/validation.py @@ -199,7 +199,7 @@ def extract_subscription(self, d): ckey_header=d["ckey_header"], ) except (InvalidTokenException, InvalidToken): - raise InvalidRequest("invalid token", errno=102) + raise InvalidRequest("invalid token", status_code=404, errno=102) return result @validates_schema(skip_on_field_errors=True) diff --git a/docs/http.rst b/docs/http.rst index 424ffbad..611a4b07 100644 --- a/docs/http.rst +++ b/docs/http.rst @@ -89,7 +89,6 @@ Unless otherwise specified, all calls return the following error codes: - 400 - Bad Parameters - errno 101 - Missing neccessary crypto keys - - errno 102 - Invalid URL endpoint - errno 108 - Router type is invalid - errno 110 - Invalid crypto keys specified - errno 111 - Missing Required Header @@ -103,6 +102,10 @@ Unless otherwise specified, all calls return the following error codes: - errno 109 - Invalid authentication +- 404 - Not Found + + - errno 102 - Invalid URL endpoint + - 410 - `{UAID}` or `{CHID}` not found - errno 103 - Expired URL endpoint