From 16c56e6aff39211eb9c57f98447d89babbea6f87 Mon Sep 17 00:00:00 2001 From: Albert Santoni Date: Mon, 17 Mar 2014 14:43:50 -0400 Subject: [PATCH] CC-5709: Airtime Analyzer * Fixed error in media API authentication * Improved documentation --- .../rest/controllers/MediaController.php | 49 +++++++++++++++---- python_apps/airtime_analyzer/README.rst | 11 +++++ .../airtime_analyzer/message_listener.py | 5 +- .../airtime_analyzer/status_reporter.py | 2 +- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/airtime_mvc/application/modules/rest/controllers/MediaController.php b/airtime_mvc/application/modules/rest/controllers/MediaController.php index 9bf4866a8..ae10c6792 100644 --- a/airtime_mvc/application/modules/rest/controllers/MediaController.php +++ b/airtime_mvc/application/modules/rest/controllers/MediaController.php @@ -1,5 +1,6 @@ view->layout()->disableLayout(); } - + public function indexAction() { - if (!$this->verifyApiKey() && !$this->verifySession()) { + if (!$this->verifyAuth(true, true)) + { return; } @@ -52,9 +54,11 @@ class Rest_MediaController extends Zend_Rest_Controller public function getAction() { - if (!$this->verifyApiKey() && !$this->verifySession()) { + if (!$this->verifyAuth(true, true)) + { return; } + $id = $this->getId(); if (!$id) { return; @@ -73,9 +77,11 @@ class Rest_MediaController extends Zend_Rest_Controller public function postAction() { - if (!$this->verifyApiKey() && !$this->verifySession()) { + if (!$this->verifyAuth(true, true)) + { return; } + //If we do get an ID on a POST, then that doesn't make any sense //since POST is only for creating. if ($id = $this->_getParam('id', false)) { @@ -104,9 +110,11 @@ class Rest_MediaController extends Zend_Rest_Controller public function putAction() { - if (!$this->verifyApiKey() && !$this->verifySession()) { + if (!$this->verifyAuth(true, true)) + { return; } + $id = $this->getId(); if (!$id) { return; @@ -150,9 +158,11 @@ class Rest_MediaController extends Zend_Rest_Controller public function deleteAction() { - if (!$this->verifyApiKey() && !$this->verifySession()) { + if (!$this->verifyAuth(true, true)) + { return; } + $id = $this->getId(); if (!$id) { return; @@ -179,6 +189,27 @@ class Rest_MediaController extends Zend_Rest_Controller } return $id; } + + private function verifyAuth($checkApiKey, $checkSession) + { + //Session takes precedence over API key for now: + if ($checkSession && $this->verifySession()) + { + return true; + } + + if ($checkApiKey && $this->verifyAPIKey()) + { + return true; + } + + $resp = $this->getResponse(); + $resp->setHttpResponseCode(401); + $resp->appendBody("ERROR: Incorrect API key."); + + return false; + } + private function verifyAPIKey() { @@ -196,11 +227,10 @@ class Rest_MediaController extends Zend_Rest_Controller { return true; } else { - $resp = $this->getResponse(); - $resp->setHttpResponseCode(401); - $resp->appendBody("ERROR: Incorrect API key."); return false; } + + return false; } private function verifySession() @@ -210,6 +240,7 @@ class Rest_MediaController extends Zend_Rest_Controller { return true; } + return false; //Token checking stub code. We'd need to change LoginController.php to generate a token too, but //but luckily all the token code already exists and works. diff --git a/python_apps/airtime_analyzer/README.rst b/python_apps/airtime_analyzer/README.rst index 2f0c9e133..7b2ba335d 100644 --- a/python_apps/airtime_analyzer/README.rst +++ b/python_apps/airtime_analyzer/README.rst @@ -12,6 +12,15 @@ You will need to allow the "airtime" RabbitMQ user to access all exchanges and q Usage ========== +This program must run as a user with permissions to write to your Airtime music library +directory. For standard Airtime installations, run it as the www-data user: + + $ sudo -u www-data airtime_analyzer --debug + +Or during development, add the --debug flag for more verbose output: + + $ sudo -u www-data airtime_analyzer --debug + To print usage instructions, run: $ airtime_analyzer --help @@ -35,6 +44,8 @@ For example, run: $ php tools/message_sender.php '{ "tmp_file_path" : "foo.mp3", "final_directory" : ".", "callback_url" : "http://airtime.localhost/rest/media/1", "api_key" : "YOUR_API_KEY" }' + $ php tools/message_sender.php '{"tmp_file_path":"foo.mp3", "import_directory":"/srv/airtime/stor/imported/1","original_filename":"foo.mp3","callback_url": "http://airtime.localhost/rest/media/1", "api_key":"YOUR_API_KEY"}' + Logging ========= diff --git a/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py b/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py index d1311a468..edde9c083 100644 --- a/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py +++ b/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py @@ -112,12 +112,13 @@ class MessageListener: # TODO: Report this as a failed upload to the File Upload REST API. # - # TODO: If the JSON was invalid, then don't report to the REST API + # TODO: If the JSON was invalid or the web server is down, + # then don't report that failure to the REST API StatusReporter.report_failure_to_callback_url(callback_url, api_key, error_status=1, reason=u'An error occurred while importing this file') - logging.error(e) + logging.exception(e) else: # ACK at the very end, after the message has been successfully processed. diff --git a/python_apps/airtime_analyzer/airtime_analyzer/status_reporter.py b/python_apps/airtime_analyzer/airtime_analyzer/status_reporter.py index acc8ba81a..e91b246a8 100644 --- a/python_apps/airtime_analyzer/airtime_analyzer/status_reporter.py +++ b/python_apps/airtime_analyzer/airtime_analyzer/status_reporter.py @@ -19,9 +19,9 @@ class StatusReporter(): timeout=StatusReporter._HTTP_REQUEST_TIMEOUT) logging.debug("HTTP request returned status: " + str(r.status_code)) logging.debug(r.text) # Log the response body - r.raise_for_status() # Raise an exception if there was an HTTP error code returned #TODO: Queue up failed requests and try them again later. + r.raise_for_status() # Raise an exception if there was an HTTP error code returned @classmethod def report_failure_to_callback_url(self, callback_url, api_key, error_status, reason):