From 2fee0f038f37f8e4757dd5a41a280dc5c7a3dd30 Mon Sep 17 00:00:00 2001 From: Terry Howe <terrylhowe@gmail.com> Date: Sat, 6 Sep 2014 16:40:12 -0600 Subject: [PATCH] Move cacert/insecure awkwardness examples/common The requests.Session object that transport derives from has this overloaded meaning for verify that is causing awkwardness around our code. I've moved the ugly to the examples/common.py argument parsing. Change-Id: I3e8f5311eeba6108d3ea086a01b71663401a52fa --- examples/common.py | 11 ++++++----- examples/session.py | 3 +-- examples/transport.py | 8 +------- openstack/session.py | 12 ++++------- openstack/tests/test_session.py | 3 +-- openstack/tests/test_transport.py | 33 ------------------------------- openstack/transport.py | 9 --------- 7 files changed, 13 insertions(+), 66 deletions(-) diff --git a/examples/common.py b/examples/common.py index ddfbfd930..09affe4d2 100755 --- a/examples/common.py +++ b/examples/common.py @@ -175,14 +175,14 @@ def option_parser(): metavar='<region>', default=env('OS_REGION_NAME'), help='Service region (Env: OS_REGION_NAME)') - parser.add_argument( + verify_group = parser.add_mutually_exclusive_group() + verify_group.add_argument( '--os-cacert', - dest='cacert', + dest='verify', metavar='<ca-bundle-file>', - default=env('OS_CACERT'), + default=env('OS_CACERT', default=True), help='CA certificate bundle file (Env: OS_CACERT)', ) - verify_group = parser.add_mutually_exclusive_group() verify_group.add_argument( '--verify', action='store_true', @@ -190,7 +190,8 @@ def option_parser(): ) verify_group.add_argument( '--insecure', - action='store_true', + dest='verify', + action='store_false', help='Disable server certificate verification', ) parser.add_argument( diff --git a/examples/session.py b/examples/session.py index 13639c1e6..b3873f664 100644 --- a/examples/session.py +++ b/examples/session.py @@ -38,8 +38,7 @@ def make_session(opts): domain_name=opts.domain_name, project_domain_name=opts.project_domain_name, user_domain_name=opts.user_domain_name, - cacert=opts.cacert, - insecure=opts.insecure, + verify=opts.verify, user_agent='SDKExample', region=opts.region_name, ) diff --git a/examples/transport.py b/examples/transport.py index 8317f82e9..5cd96ddcb 100755 --- a/examples/transport.py +++ b/examples/transport.py @@ -26,15 +26,9 @@ import sys from examples import common from openstack import transport -USER_AGENT = 'SDKExample' - def make_transport(opts): - return transport.Transport.create( - cacert=opts.cacert, - insecure=opts.insecure, - user_agent=USER_AGENT - ) + return transport.Transport(verify=opts.verify) def run_transport(opts): diff --git a/openstack/session.py b/openstack/session.py index 72ef48eb8..bffd01bac 100644 --- a/openstack/session.py +++ b/openstack/session.py @@ -40,14 +40,10 @@ class Session(object): @classmethod def create(cls, username=None, password=None, token=None, auth_url=None, - version=None, project_name=None, cacert=None, insecure=False, - user_agent=None, region=None, domain_name=None, - project_domain_name=None, user_domain_name=None): - xport = transport.Transport.create( - cacert=cacert, - insecure=insecure, - user_agent=user_agent, - ) + version=None, project_name=None, verify=None, user_agent=None, + region=None, domain_name=None, project_domain_name=None, + user_domain_name=None): + xport = transport.Transport(verify=verify, user_agent=user_agent) args = { 'username': username, 'password': password, diff --git a/openstack/tests/test_session.py b/openstack/tests/test_session.py index 5fdbdc6aa..b26026b48 100644 --- a/openstack/tests/test_session.py +++ b/openstack/tests/test_session.py @@ -92,8 +92,7 @@ class TestSessionCreate(base.TestCase): auth_url='4', version='3', project_name='6', - cacert='7', - insecure='8', + verify='7', user_agent='9', region='10', ) diff --git a/openstack/tests/test_transport.py b/openstack/tests/test_transport.py index b8ab7f171..1bb54d60c 100644 --- a/openstack/tests/test_transport.py +++ b/openstack/tests/test_transport.py @@ -387,10 +387,6 @@ class TestTransport(base.TestTransportBase): xport = transport.Transport() self.assertTrue(xport.verify) - def test_verify_arg_none(self): - xport = transport.Transport(verify=None) - self.assertIsNone(xport.verify) - def test_verify_arg_false(self): xport = transport.Transport(verify=False) self.assertFalse(xport.verify) @@ -638,32 +634,3 @@ class TestTransportRedirects(base.TestTransportBase): for r, s in zip(req_resp.history, resp.history): self.assertEqual(s.url, r.url) self.assertEqual(s.status_code, r.status_code) - - -class TestTransporCreate(base.TestCase): - def test_create(self): - xport = transport.Transport.create( - cacert='1', - insecure=False, - user_agent='2', - ) - self.assertEqual('1', xport.verify) - self.assertEqual('2', xport._user_agent) - - def test_create_no_cert(self): - xport = transport.Transport.create( - cacert=None, - insecure=False, - user_agent='3', - ) - self.assertEqual(True, xport.verify) - self.assertEqual('3', xport._user_agent) - - def test_create_verify(self): - xport = transport.Transport.create( - cacert=None, - insecure=True, - user_agent='4', - ) - self.assertEqual(False, xport.verify) - self.assertEqual('4', xport._user_agent) diff --git a/openstack/transport.py b/openstack/transport.py index 6e24963d3..2ac884b7c 100644 --- a/openstack/transport.py +++ b/openstack/transport.py @@ -82,15 +82,6 @@ class Transport(requests.Session): self._redirect = redirect self._accept = accept - @classmethod - def create(cls, cacert=None, insecure=False, user_agent=None): - # Certificate verification - defaults to True - if cacert: - verify = cacert - else: - verify = not insecure - return cls(verify=verify, user_agent=user_agent) - def request(self, method, url, redirect=None, **kwargs): """Send a request