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
examples
openstack
@ -175,14 +175,14 @@ def option_parser():
|
|||||||
metavar='<region>',
|
metavar='<region>',
|
||||||
default=env('OS_REGION_NAME'),
|
default=env('OS_REGION_NAME'),
|
||||||
help='Service region (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',
|
'--os-cacert',
|
||||||
dest='cacert',
|
dest='verify',
|
||||||
metavar='<ca-bundle-file>',
|
metavar='<ca-bundle-file>',
|
||||||
default=env('OS_CACERT'),
|
default=env('OS_CACERT', default=True),
|
||||||
help='CA certificate bundle file (Env: OS_CACERT)',
|
help='CA certificate bundle file (Env: OS_CACERT)',
|
||||||
)
|
)
|
||||||
verify_group = parser.add_mutually_exclusive_group()
|
|
||||||
verify_group.add_argument(
|
verify_group.add_argument(
|
||||||
'--verify',
|
'--verify',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
@ -190,7 +190,8 @@ def option_parser():
|
|||||||
)
|
)
|
||||||
verify_group.add_argument(
|
verify_group.add_argument(
|
||||||
'--insecure',
|
'--insecure',
|
||||||
action='store_true',
|
dest='verify',
|
||||||
|
action='store_false',
|
||||||
help='Disable server certificate verification',
|
help='Disable server certificate verification',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
@ -38,8 +38,7 @@ def make_session(opts):
|
|||||||
domain_name=opts.domain_name,
|
domain_name=opts.domain_name,
|
||||||
project_domain_name=opts.project_domain_name,
|
project_domain_name=opts.project_domain_name,
|
||||||
user_domain_name=opts.user_domain_name,
|
user_domain_name=opts.user_domain_name,
|
||||||
cacert=opts.cacert,
|
verify=opts.verify,
|
||||||
insecure=opts.insecure,
|
|
||||||
user_agent='SDKExample',
|
user_agent='SDKExample',
|
||||||
region=opts.region_name,
|
region=opts.region_name,
|
||||||
)
|
)
|
||||||
|
@ -26,15 +26,9 @@ import sys
|
|||||||
from examples import common
|
from examples import common
|
||||||
from openstack import transport
|
from openstack import transport
|
||||||
|
|
||||||
USER_AGENT = 'SDKExample'
|
|
||||||
|
|
||||||
|
|
||||||
def make_transport(opts):
|
def make_transport(opts):
|
||||||
return transport.Transport.create(
|
return transport.Transport(verify=opts.verify)
|
||||||
cacert=opts.cacert,
|
|
||||||
insecure=opts.insecure,
|
|
||||||
user_agent=USER_AGENT
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def run_transport(opts):
|
def run_transport(opts):
|
||||||
|
@ -40,14 +40,10 @@ class Session(object):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, username=None, password=None, token=None, auth_url=None,
|
def create(cls, username=None, password=None, token=None, auth_url=None,
|
||||||
version=None, project_name=None, cacert=None, insecure=False,
|
version=None, project_name=None, verify=None, user_agent=None,
|
||||||
user_agent=None, region=None, domain_name=None,
|
region=None, domain_name=None, project_domain_name=None,
|
||||||
project_domain_name=None, user_domain_name=None):
|
user_domain_name=None):
|
||||||
xport = transport.Transport.create(
|
xport = transport.Transport(verify=verify, user_agent=user_agent)
|
||||||
cacert=cacert,
|
|
||||||
insecure=insecure,
|
|
||||||
user_agent=user_agent,
|
|
||||||
)
|
|
||||||
args = {
|
args = {
|
||||||
'username': username,
|
'username': username,
|
||||||
'password': password,
|
'password': password,
|
||||||
|
@ -92,8 +92,7 @@ class TestSessionCreate(base.TestCase):
|
|||||||
auth_url='4',
|
auth_url='4',
|
||||||
version='3',
|
version='3',
|
||||||
project_name='6',
|
project_name='6',
|
||||||
cacert='7',
|
verify='7',
|
||||||
insecure='8',
|
|
||||||
user_agent='9',
|
user_agent='9',
|
||||||
region='10',
|
region='10',
|
||||||
)
|
)
|
||||||
|
@ -387,10 +387,6 @@ class TestTransport(base.TestTransportBase):
|
|||||||
xport = transport.Transport()
|
xport = transport.Transport()
|
||||||
self.assertTrue(xport.verify)
|
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):
|
def test_verify_arg_false(self):
|
||||||
xport = transport.Transport(verify=False)
|
xport = transport.Transport(verify=False)
|
||||||
self.assertFalse(xport.verify)
|
self.assertFalse(xport.verify)
|
||||||
@ -638,32 +634,3 @@ class TestTransportRedirects(base.TestTransportBase):
|
|||||||
for r, s in zip(req_resp.history, resp.history):
|
for r, s in zip(req_resp.history, resp.history):
|
||||||
self.assertEqual(s.url, r.url)
|
self.assertEqual(s.url, r.url)
|
||||||
self.assertEqual(s.status_code, r.status_code)
|
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._redirect = redirect
|
||||||
self._accept = accept
|
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):
|
def request(self, method, url, redirect=None, **kwargs):
|
||||||
"""Send a request
|
"""Send a request
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user