Skip to content

Commit

Permalink
Add verify_ssl parameter to the remaining places that fire requests (#14
Browse files Browse the repository at this point in the history
)
  • Loading branch information
xmkevinchen authored and ecederstrand committed Aug 8, 2016
1 parent 7caa764 commit 85e72f9
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
6 changes: 5 additions & 1 deletion exchangelib/ewsdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class EWSTimeZone:
Represents a timezone as expected by the EWS TimezoneContext / TimezoneDefinition XML element, and returned by
services.GetServerTimeZones.
"""

@classmethod
def from_pytz(cls, tz):
# pytz timezones are dynamically generated. Subclass the tz.__class__ and add the extra Microsoft timezone
Expand Down Expand Up @@ -113,9 +114,11 @@ def localize(self, dt):

# Manually maintained translation between pytz location / timezone name and MS timezone IDs
PYTZ_TO_MS_MAP = {
'Europe/Copenhagen': 'Romance Standard Time',
'UTC': 'UTC',
'GMT': 'GMT Standard Time',
'US/Pacific': 'Pacific Standard Time',
'US/Eastern': 'Eastern Standard Time',
'Europe/Copenhagen': 'Romance Standard Time',
}

# This is a somewhat authoritative list of the timezones available on an Exchange server. Format is (id, name).
Expand Down Expand Up @@ -221,6 +224,7 @@ def localize(self, dt):
('Tonga Standard Time', "(UTC+13:00) Nuku'alofa"),
])


UTC = EWSTimeZone.timezone('UTC')

UTC_NOW = lambda: EWSDateTime.now(tz=UTC)
7 changes: 4 additions & 3 deletions exchangelib/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _test_docs_credentials(protocol):
# response.
auth = get_auth_instance(credentials=protocol.credentials, auth_type=protocol.docs_auth_type)
with requests.sessions.Session() as s:
r = s.get(url=protocol.types_url, auth=auth, allow_redirects=False)
r = s.get(url=protocol.types_url, auth=auth, allow_redirects=False, verify=protocol.verify_ssl)
return _test_response(auth=auth, response=r)


Expand All @@ -56,7 +56,8 @@ def _test_service_credentials(protocol):
data = dummy_xml(version=protocol.version.api_version)
auth = get_auth_instance(credentials=protocol.credentials, auth_type=protocol.auth_type)
with requests.sessions.Session() as s:
r = s.post(url=protocol.service_endpoint, headers=headers, data=data, auth=auth, allow_redirects=False)
r = s.post(url=protocol.service_endpoint, headers=headers, data=data, auth=auth, allow_redirects=False,
verify=protocol.verify_ssl)
return _test_response(auth=auth, response=r)


Expand Down Expand Up @@ -117,7 +118,7 @@ def wrap(content, version, account, ewstimezone=None, encoding='utf-8'):
body.append(content)
envelope.append(body)
return (b'<?xml version="1.0" encoding="' + encoding.encode(encoding)) + b'"?>' \
+ tostring(envelope, encoding=encoding)
+ tostring(envelope, encoding=encoding)


def get_auth_instance(credentials, auth_type):
Expand Down
7 changes: 4 additions & 3 deletions exchangelib/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def guess(cls, protocol):
# We can't use a session object from the protocol pool for docs because sessions are created with service auth.
try:
auth = get_auth_instance(credentials=protocol.credentials, auth_type=protocol.docs_auth_type)
shortname = cls._get_shortname_from_docs(auth=auth, types_url=protocol.types_url)
shortname = cls._get_shortname_from_docs(auth=auth, types_url=protocol.types_url,
verify_ssl=protocol.verify_ssl)
log.debug('Shortname according to %s: %s', protocol.types_url, shortname)
except (TransportError, UnauthorizedError) as e:
log.warning(str(e))
Expand All @@ -178,13 +179,13 @@ def guess(cls, protocol):
return cls._guess_version_from_service(protocol=protocol, hint=api_version)

@staticmethod
def _get_shortname_from_docs(auth, types_url):
def _get_shortname_from_docs(auth, types_url, verify_ssl):
# Get the server version from types.xsd. We can't necessarily use the service auth type since it may not be the
# same as the auth type for docs.
log.debug('Getting %s with auth type %s', types_url, auth.__class__.__name__)
# Some servers send an empty response if we send 'Connection': 'close' header
with requests.sessions.Session() as s:
r = s.get(url=types_url, auth=auth, allow_redirects=False, stream=False)
r = s.get(url=types_url, auth=auth, allow_redirects=False, stream=False, verify=verify_ssl)
log.debug('Request headers: %s', r.request.headers)
log.debug('Response code: %s', r.status_code)
log.debug('Response headers: %s', r.headers)
Expand Down
5 changes: 3 additions & 2 deletions optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
raise

categories = ['perftest']
tz = EWSTimeZone.timezone('Europe/Copenhagen')
tz = EWSTimeZone.timezone('US/Pacific')

config = Configuration(server=settings['server'], username=settings['username'], password=settings['password'])
config = Configuration(server=settings['server'], username=settings['username'], password=settings['password'],
verify_ssl=settings['verify_ssl'])
print(('Exchange server: %s' % config.protocol.server))

account = Account(config=config, primary_smtp_address=settings['account'], access_type=DELEGATE)
Expand Down
7 changes: 5 additions & 2 deletions perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
raise

categories = ['foobar', 'perftest']
tz = EWSTimeZone.timezone('Europe/Copenhagen')
tz = EWSTimeZone.timezone('US/Pacific')

t0 = datetime.now()

config = Configuration(server=settings['server'], username=settings['username'], password=settings['password'])
config = Configuration(server=settings['server'], username=settings['username'], password=settings['password'],
verify_ssl=False)
print(('Exchange server: %s' % config.protocol.server))

account = Account(config=config, primary_smtp_address=settings['account'], access_type=DELEGATE)
Expand Down Expand Up @@ -100,6 +101,7 @@ def perf_test(cbs, dbs, ps):
print(('Total time: %s' % total))
return avg_create, avg_fetch, avg_delete, total


from datetime import timedelta
from time import sleep

Expand Down Expand Up @@ -130,6 +132,7 @@ def run(cbs, dbs, ps):
# Let server cool off a bit. Otherwise perf numbers deteriorate over time even though the same settings are used.
sleep(60)


for poolsize in (2, 2, 2):
for batchsize in (25, 25, 25):
run(batchsize, batchsize, poolsize)
Expand Down
1 change: 1 addition & 0 deletions settings.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ server: 'example.com'
username: 'MYWINDOMAIN\myusername'
password: 'topsecret'
account: '[email protected]' # Don't use an account containing valuable data! We're polite, but things may go wrong.
verify_ssl: True
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def setUp(self):
self.tz = EWSTimeZone.timezone('Europe/Copenhagen')
self.categories = ['Test']
self.config = Configuration(server=settings['server'], username=settings['username'],
password=settings['password'])
password=settings['password'], verify_ssl=settings['verify_ssl'])
self.account = Account(primary_smtp_address=settings['account'], access_type=DELEGATE, config=self.config)
self.maxDiff = None

Expand Down

0 comments on commit 85e72f9

Please sign in to comment.