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
This commit is contained in:
parent
ec5fc4543f
commit
2fee0f038f
@ -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(
|
||||
|
@ -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,
|
||||
)
|
||||
|
@ -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):
|
||||
|
@ -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,
|
||||
|
@ -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',
|
||||
)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user