Make SmartConnect respect custom SSLContexts during version check

Also modify test fixtures to not expect an extra slash in uri from SmartConnect
This commit is contained in:
InvalidInterrupt 2016-01-14 18:04:27 -08:00
parent 76d72e52e9
commit 9458f9423f
7 changed files with 35 additions and 24 deletions

@ -29,6 +29,7 @@ import re
import ssl
from xml.etree import ElementTree
from xml.parsers.expat import ExpatError
from six.moves import http_client
import requests
from requests.auth import HTTPBasicAuth
@ -512,28 +513,38 @@ class SmartConnection(object):
Disconnect(self.si)
self.si = None
def __GetElementTreeFromUrl(url, sslContext):
def __GetElementTree(protocol, server, port, path, sslContext):
"""
Private method that returns a root from ElementTree for the XML document referenced by
the url.
Private method that returns a root from ElementTree for a remote XML document.
@param url: URL
@type url: string
@param protocol: What protocol to use for the connection (e.g. https or http).
@type protocol: string
@param server: Which server to connect to.
@type server: string
@param port: Port
@type port: int
@param path: Path
@type path: string
@param sslContext: SSL Context describing the various SSL options. It is only
supported in Python 2.7.9 or higher.
@type sslContext: SSL.Context
"""
try:
if sslContext is not None and sslContext.verify_mode == ssl.CERT_NONE:
sock = requests.get(url, verify=False)
else:
sock = requests.get(url)
if sock.status_code == 200:
tree = ElementTree.fromstring(sock.content)
if protocol == "https":
kwargs = {"context": sslContext} if sslContext else {}
conn = http_client.HTTPSConnection(server, port=port, **kwargs)
elif protocol == "http":
conn = http_client.HTTPConnection(server, port=port)
else:
raise Exception("Protocol " + protocol + " not supported.")
conn.request("GET", path)
response = conn.getresponse()
if response.status == 200:
try:
tree = ElementTree.fromstring(response.read())
return tree
except ExpatError:
pass
except ExpatError:
pass
return None
## Private method that returns an ElementTree describing the API versions
@ -559,13 +570,13 @@ def __GetServiceVersionDescription(protocol, server, port, path, sslContext):
@type sslContext: SSL.Context
"""
url = "%s://%s:%s/%s/vimServiceVersions.xml" % (protocol, server, port, path)
tree = __GetElementTreeFromUrl(url, sslContext)
tree = __GetElementTree(protocol, server, port,
path + "/vimServiceVersions.xml", sslContext)
if tree is not None:
return tree
url = "%s://%s:%s/%s/vimService.wsdl" % (protocol, server, port, path)
tree = __GetElementTreeFromUrl(url, sslContext)
tree = __GetElementTree(protocol, server, port,
path + "/vimService.wsdl", sslContext)
return tree

@ -7,7 +7,7 @@ interactions:
Connection: [keep-alive]
User-Agent: [python-requests/2.7.0 CPython/2.7.10 Darwin/15.0.0]
method: GET
uri: https://vcsa//sdk/vimServiceVersions.xml
uri: https://vcsa/sdk/vimServiceVersions.xml
response:
body: {string: !!python/unicode '<?xml version="1.0" encoding="UTF-8" ?><namespaces
version="1.0"><namespace><name>urn:vim25</name><version>6.0</version><priorVersions><version>5.5</version><version>5.1</version><version>5.0</version><version>4.1</version><version>4.0</version><version>2.5u2server</version><version>2.5u2</version><version>2.5</version></priorVersions></namespace><namespace><name>urn:vim2</name><version>2.0</version></namespace></namespaces>

@ -6,7 +6,7 @@ interactions:
Accept-Encoding: ['gzip, deflate']
User-Agent: [python-requests/2.3.0 CPython/3.4.1 Darwin/13.3.0]
method: GET
uri: https://vcsa:443//sdk/vimServiceVersions.xml
uri: https://vcsa:443/sdk/vimServiceVersions.xml
response:
body: {string: "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<!--\n Copyright
2008-2012 VMware, Inc. All rights reserved.\n-->\n<namespaces version=\"1.0\">\n

@ -7,7 +7,7 @@ interactions:
Connection: [keep-alive]
User-Agent: [python-requests/2.7.0 CPython/2.7.10 Darwin/15.0.0]
method: GET
uri: https://vcsa//sdk/vimServiceVersions.xml
uri: https://vcsa/sdk/vimServiceVersions.xml
response:
body: {string: !!python/unicode '<?xml version="1.0" encoding="UTF-8" ?><namespaces
version="1.0"><namespace><name>urn:vim25</name><version>6.0</version><priorVersions><version>5.5</version><version>5.1</version><version>5.0</version><version>4.1</version><version>4.0</version><version>2.5u2server</version><version>2.5u2</version><version>2.5</version></priorVersions></namespace><namespace><name>urn:vim2</name><version>2.0</version></namespace></namespaces>

@ -7,7 +7,7 @@ interactions:
Connection: [keep-alive]
User-Agent: [python-requests/2.7.0 CPython/2.7.10 Darwin/15.0.0]
method: GET
uri: https://vcsa//sdk/vimServiceVersions.xml
uri: https://vcsa/sdk/vimServiceVersions.xml
response:
body: {string: !!python/unicode '<?xml version="1.0" encoding="UTF-8" ?><namespaces
version="1.0"><namespace><name>urn:vim25</name><version>6.0</version><priorVersions><version>5.5</version><version>5.1</version><version>5.0</version><version>4.1</version><version>4.0</version><version>2.5u2server</version><version>2.5u2</version><version>2.5</version></priorVersions></namespace><namespace><name>urn:vim2</name><version>2.0</version></namespace></namespaces>

@ -6,7 +6,7 @@ interactions:
Accept-Encoding: ['gzip, deflate']
User-Agent: [python-requests/2.3.0 CPython/3.4.1 Darwin/13.3.0]
method: GET
uri: https://vcsa:443//sdk/vimServiceVersions.xml
uri: https://vcsa:443/sdk/vimServiceVersions.xml
response:
body: {string: "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<!--\n Copyright
2008-2012 VMware, Inc. All rights reserved.\n-->\n<namespaces version=\"1.0\">\n

@ -6,7 +6,7 @@ interactions:
Accept-Encoding: ['gzip, deflate']
User-Agent: [python-requests/2.3.0 CPython/3.4.1 Darwin/13.3.0]
method: GET
uri: https://vcsa:443//sdk/vimServiceVersions.xml
uri: https://vcsa:443/sdk/vimServiceVersions.xml
response:
body: {string: "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<!--\n Copyright
2008-2012 VMware, Inc. All rights reserved.\n-->\n<namespaces version=\"1.0\">\n