forked from openedx-unsupported/edx-analytics-data-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
179 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
import time | ||
|
||
from boto.connection import AWSAuthConnection | ||
from django.conf import settings | ||
from elasticsearch import Connection | ||
|
||
|
||
class BotoHttpConnection(Connection): | ||
""" | ||
Uses AWS configured connection to sign requests before they're sent to | ||
elasticsearch nodes. | ||
""" | ||
|
||
connection = None | ||
|
||
def __init__(self, host='localhost', port=443, **kwargs): | ||
super(BotoHttpConnection, self).__init__(host=host, port=port, **kwargs) | ||
connection_params = {'host': host, 'port': port} | ||
|
||
# Set the credentials through settings. If not provided, boto will attempt to use | ||
# default environment variables. | ||
if settings.ELASTICSEARCH_AWS_ACCESS_KEY_ID: | ||
connection_params['aws_access_key_id'] = settings.ELASTICSEARCH_AWS_ACCESS_KEY_ID | ||
if settings.ELASTICSEARCH_AWS_SECRET_ACCESS_KEY: | ||
connection_params['aws_secret_access_key'] = settings.ELASTICSEARCH_AWS_SECRET_ACCESS_KEY | ||
self.connection = ESConnection(**connection_params) | ||
|
||
# pylint: disable=unused-argument | ||
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()): | ||
""" | ||
Called when making requests elasticsearch. Requests are signed and | ||
http status, headers, and response is returned. | ||
Note: the "timeout" kwarg is ignored in this case. Boto manages the timeout | ||
and the default is 70 seconds. | ||
See: https://github.com/boto/boto/blob/develop/boto/connection.py#L533 | ||
""" | ||
if not isinstance(body, basestring): | ||
body = json.dumps(body) | ||
start = time.time() | ||
response = self.connection.make_request(method, url, params=params, data=body) | ||
duration = time.time() - start | ||
raw_data = response.read() | ||
|
||
# raise errors based on http status codes and let the client handle them | ||
if not (200 <= response.status < 300) and response.status not in ignore: | ||
self.log_request_fail(method, url, body, duration, response.status) | ||
self._raise_error(response.status, raw_data) | ||
|
||
self.log_request_success(method, url, url, body, response.status, raw_data, duration) | ||
|
||
return response.status, dict(response.getheaders()), raw_data | ||
|
||
|
||
class ESConnection(AWSAuthConnection): | ||
""" | ||
Use to sign requests for an AWS hosted elasticsearch cluster. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
region = kwargs.pop('region', settings.ELASTICSEARCH_CONNECTION_DEFAULT_REGION) | ||
kwargs.setdefault('is_secure', True) | ||
super(ESConnection, self).__init__(*args, **kwargs) | ||
self.auth_region_name = region | ||
self.auth_service_name = 'es' | ||
|
||
def _required_auth_capability(self): | ||
return ['hmac-v4'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from django.test import TestCase | ||
from django.test.utils import override_settings | ||
from elasticsearch.exceptions import ElasticsearchException | ||
from mock import patch | ||
|
||
from analytics_data_api.v0.connections import BotoHttpConnection, ESConnection | ||
|
||
|
||
class ESConnectionTests(TestCase): | ||
|
||
@override_settings(ELASTICSEARCH_CONNECTION_DEFAULT_REGION='region_123') | ||
def test_region(self): | ||
connection = ESConnection('mockservice.cc-zone-1.amazonaws.com', | ||
aws_access_key_id='access_key', | ||
aws_secret_access_key='secret') | ||
self.assertEqual(connection.auth_region_name, 'region_123') | ||
|
||
|
||
@override_settings(ELASTICSEARCH_AWS_ACCESS_KEY_ID='access_key') | ||
@override_settings(ELASTICSEARCH_AWS_SECRET_ACCESS_KEY='secret') | ||
class BotoHttpConnectionTests(TestCase): | ||
|
||
@patch('analytics_data_api.v0.connections.ESConnection.make_request') | ||
def test_perform_request_success(self, mock_response): | ||
mock_response.return_value.status = 200 | ||
connection = BotoHttpConnection() | ||
status, _header, _data = connection.perform_request('get', 'http://example.com') | ||
self.assertEqual(status, 200) | ||
|
||
@patch('analytics_data_api.v0.connections.ESConnection.make_request') | ||
def test_perform_request_error(self, mock_response): | ||
mock_response.return_value.status = 500 | ||
connection = BotoHttpConnection() | ||
with self.assertRaises(ElasticsearchException): | ||
connection.perform_request('get', 'http://example.com') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
boto==2.22.1 # MIT | ||
Django==1.7.5 # BSD License | ||
Markdown==2.6 # BSD | ||
django-model-utils==2.2 # BSD | ||
djangorestframework==2.4.4 # BSD | ||
ipython==2.4.1 # BSD | ||
django-rest-swagger==0.2.8 # BSD | ||
djangorestframework-csv==1.3.3 # BSD | ||
django-countries==3.2 # MIT | ||
elasticsearch-dsl==0.0.9 # Apache 2.0 | ||
elasticsearch-dsl==0.0.9 # Apache 2.0 | ||
ipython==2.4.1 # BSD | ||
Markdown==2.6 # BSD | ||
|
||
-e git+https://github.com/edx/opaque-keys.git@d45d0bd8d64c69531be69178b9505b5d38806ce0#egg=opaque-keys |