Permalink
Browse files

Merge pull request #2513 from N3TWORK/cloudsearch2-proxy

Copy CloudSearch proxy settings to endpoint services. Fixes #2513.
  • Loading branch information...
danielgtaylor committed Aug 13, 2014
2 parents 1f35224 + 1a81f79 commit 3cbbc21b61852738adbeda837588b60b5857ce68
@@ -93,6 +93,12 @@ def __init__(self, domain=None, endpoint=None):
self.documents_batch = []
self._sdf = None
# Copy proxy settings from connection
if self.domain and self.domain.layer1 and self.domain.layer1.use_proxy:
self.proxy = {'http': self.domain.layer1.get_proxy_url_with_auth()}
else:
self.proxy = {}
def add(self, _id, fields):
"""
Add a document to be processed by the DocumentService
@@ -184,6 +190,7 @@ def commit(self):
# Keep-alive is automatic in a post-1.0 requests world.
session = requests.Session()
session.proxies = self.proxy
adapter = requests.adapters.HTTPAdapter(
pool_connections=20,
pool_maxsize=50,
@@ -152,6 +152,10 @@ def __init__(self, domain=None, endpoint=None):
self.endpoint = endpoint
self.session = requests.Session()
# Copy proxy settings from connection
if self.domain and self.domain.layer1 and self.domain.layer1.use_proxy:
self.session.proxies['http'] = self.domain.layer1.get_proxy_url_with_auth()
if not endpoint:
self.endpoint = domain.search_service_endpoint
View
@@ -857,6 +857,21 @@ def get_proxy_auth_header(self):
auth = encodebytes(self.proxy_user + ':' + self.proxy_pass)
return {'Proxy-Authorization': 'Basic %s' % auth}
# For passing proxy information to other connection libraries, e.g. cloudsearch2
def get_proxy_url_with_auth(self):
if not self.use_proxy:
return None
if self.proxy_user or self.proxy_pass:
if self.proxy_pass:
login_info = '%s:%s@' % (self.proxy_user, self.proxy_pass)
else:
login_info = '%s@' % self.proxy_user
else:
login_info = ''
return 'http://%s%s:%s' % (login_info, self.proxy, str(self.proxy_port or self.port))
def set_host_header(self, request):
try:
request.headers['Host'] = \
View
@@ -23,6 +23,8 @@ def initialize_service_connection(self):
self.actual_request = None
self.original_mexe = self.service_connection._mexe
self.service_connection._mexe = self._mexe_spy
self.proxy = None
self.use_proxy = False
def create_service_connection(self, **kwargs):
if self.connection_class is None:
@@ -1 +1,18 @@
DEMO_DOMAIN_DATA = {
"SearchInstanceType": None,
"DomainId": "1234567890/demo",
"DomainName": "demo",
"Deleted": False,
"SearchInstanceCount": 0,
"Created": True,
"SearchService": {
"Endpoint": "search-demo.us-east-1.cloudsearch.amazonaws.com"
},
"RequiresIndexDocuments": False,
"Processing": False,
"DocService": {
"Endpoint": "doc-demo.us-east-1.cloudsearch.amazonaws.com"
},
"ARN": "arn:aws:cs:us-east-1:1234567890:domain/demo",
"SearchPartitionCount": 0
}
@@ -1,6 +1,8 @@
#!/usr/bin env python
from boto.cloudsearch2.domain import Domain
from boto.cloudsearch2.layer1 import CloudSearchConnection
from tests.unit import unittest
from tests.unit import unittest, AWSMockServiceTestCase
from httpretty import HTTPretty
from mock import MagicMock
@@ -11,6 +13,24 @@
ContentTooLongError, DocumentServiceConnection
import boto
from tests.unit.cloudsearch2 import DEMO_DOMAIN_DATA
class CloudSearchDocumentConnectionTest(AWSMockServiceTestCase):
connection_class = CloudSearchConnection
def test_proxy(self):
conn = self.service_connection
conn.proxy = "127.0.0.1"
conn.proxy_user = "john.doe"
conn.proxy_pass="p4ssw0rd"
conn.proxy_port="8180"
conn.use_proxy = True
domain = Domain(conn, DEMO_DOMAIN_DATA)
service = DocumentServiceConnection(domain=domain)
self.assertEqual(service.proxy, {'http': 'http://john.doe:p4ssw0rd@127.0.0.1:8180'})
class CloudSearchDocumentTest(unittest.TestCase):
def setUp(self):
@@ -25,6 +45,7 @@ def setUp(self):
def tearDown(self):
HTTPretty.disable()
class CloudSearchDocumentSingleTest(CloudSearchDocumentTest):
response = {
@@ -1,4 +1,6 @@
#!/usr/bin env python
from boto.cloudsearch2.domain import Domain
from boto.cloudsearch2.layer1 import CloudSearchConnection
from tests.compat import mock, unittest
from httpretty import HTTPretty
@@ -7,6 +9,9 @@
from boto.cloudsearch2.search import SearchConnection, SearchServiceException
from boto.compat import six, map
from tests.unit import AWSMockServiceTestCase
from tests.unit.cloudsearch2 import DEMO_DOMAIN_DATA
from tests.unit.cloudsearch2.test_connection import TestCloudSearchCreateDomain
HOSTNAME = "search-demo-userdomain.us-east-1.cloudsearch.amazonaws.com"
FULL_URL = 'http://%s/2013-01-01/search' % HOSTNAME
@@ -334,8 +339,9 @@ class FakeResponse(object):
content = b''
class CloudSearchConnectionTest(unittest.TestCase):
class CloudSearchConnectionTest(AWSMockServiceTestCase):
cloudsearch = True
connection_class = CloudSearchConnection
def setUp(self):
super(CloudSearchConnectionTest, self).setUp()
@@ -367,3 +373,15 @@ def test_expose_additional_error_info(self):
self.assertTrue('Unknown error' in str(cm.exception))
self.assertTrue('went wrong. Oops' in str(cm.exception))
def test_proxy(self):
conn = self.service_connection
conn.proxy = "127.0.0.1"
conn.proxy_user = "john.doe"
conn.proxy_pass="p4ssw0rd"
conn.proxy_port="8180"
conn.use_proxy = True
domain = Domain(conn, DEMO_DOMAIN_DATA)
search = SearchConnection(domain=domain)
self.assertEqual(search.session.proxies, {'http': 'http://john.doe:p4ssw0rd@127.0.0.1:8180'})
@@ -129,6 +129,19 @@ def test_connection_behind_proxy(self):
self.assertEqual(conn.proxy_port, '8180')
del os.environ['http_proxy']
def test_get_proxy_url_with_auth(self):
conn = AWSAuthConnection(
'mockservice.cc-zone-1.amazonaws.com',
aws_access_key_id='access_key',
aws_secret_access_key='secret',
suppress_consec_slashes=False,
proxy="127.0.0.1",
proxy_user="john.doe",
proxy_pass="p4ssw0rd",
proxy_port="8180"
)
self.assertEqual(conn.get_proxy_url_with_auth(), 'http://john.doe:p4ssw0rd@127.0.0.1:8180')
def test_connection_behind_proxy_without_explicit_port(self):
os.environ['http_proxy'] = "http://127.0.0.1"
conn = AWSAuthConnection(

0 comments on commit 3cbbc21

Please sign in to comment.