PEP 0466 - SSL - Changes consideration
As of Python 2.7.9 the default SSL behavior has changed (see PEP 0466). SSL certificates are verified in strict mode. Since vCenter's default beginner use case is to deploy with a self-signed certificate we may need to add some additional flags, settings, and warnings inside pyVmomi to alert about and accommodate this change.
- We want new users to be able to just get going
- We don't want to encourage weak SSL which is a security concern
- We do want to inform a beginner user of what's going on and what to do about it
- We want this to be as easy as possible without sacrificing security
NOTE: this may have already been covered, this issue is here as a note to force me to examine the issue again just before the next roll out of pyVmomi
So far I have been unable to find a way to override the Python interpreter-wide ssl.SSLContext.verify_mode so I am leaning toward making a patch to the library to allow users to pass in a set of flags to Connect and SmartConnect ... more on this later.
Edit:
Note the problem could be that we need to expose all of these flags ... upward from Connect and SmartConnect along with flags from the SoapStubAdapter and HTTPSConnectionWrapper classes.
Hey Shawn,
PEP-476 talks about the technical details of the new SSL behaviour.
The Opting out section of PEP-476 also provides details on how to disable certificate verification.
For users who wish to opt out of certificate verification, they can achieve this by providing the context argument to urllib.urlopen :
import ssl # This restores the same behavior as before. context = ssl._create_unverified_context() urllib.urlopen("https://no-valid-cert", context=context)It is also possible though highly discouraged to globally disable verification by monkeypatching the ssl module:
import ssl ssl._create_default_https_context = ssl._create_unverified_context
Hope that helps.
Regards, Marin
@dnaeon thanks! That certainly helps clarify a few things and we were looking for a global monkey patch solution as a stop-gap. I don't want that as a permanent solution but I would want to have a work-around to show people while we work out what to do correctly.
Wouldn't the simplest fix be just adding a parameter for simply disabling ssl verification. Since requests is being used it would be extremely simple to disable SSL verification.
I'm willing to pick this one up if it's alright with @hartsock and @dnaeon.
I already do this myself allowing a parameter to my scripts to be passed, disabling SSL verification(or at least the warnings):
if nosslcheck:
requests.packages.urllib3.disable_warnings()
I think it's important to allow for SSL verification and enable it as a standard... Just for security awareness.
It would be nice to see this in the SmartConnect method as a parameter with a default to verify the certificate.
Something like a parameter called unsafe_ssl would be perfect. It could also write a warning to sys.stderr to make sure the developer is notified that their code is running in an unsafe state. Monkeypatching the SSL module would not really be needed unless the project is using another library for HTTP.
See also [https://github.com/vmware/pyvmomi/issues/235], [https://github.com/vmware/pyvmomi/issues/179], [https://github.com/gevent/gevent/issues/477]
NOTE: I attempted to avoid the global monkey-patch solution but pyVmomi has a connection somewhere that still opens verified even when I make the unverified context explicitly. I've not had time to run down all the connection open and closes. I'll attempt to do this at some point late this week or early next week.
This fixes it for me:
⟫ git diff
diff --git a/pyVmomi/SoapAdapter.py b/pyVmomi/SoapAdapter.py
index 71fd2e5..5aa0bd6 100644
--- a/pyVmomi/SoapAdapter.py
+++ b/pyVmomi/SoapAdapter.py
@@ -1208,6 +1208,8 @@ class SoapStubAdapter(SoapStubAdapterBase):
if cacertsFile:
self.schemeArgs['ca_certs'] = cacertsFile
self.schemeArgs['cert_reqs'] = ssl.CERT_REQUIRED
+ if self.schemeArgs == {}:
+ self.schemeArgs['context'] = ssl._create_unverified_context()
self.samlToken = samlToken
self.requestModifierList = []
self._acceptCompressedResponses = acceptCompressedResponses
I'm assuming if none of the schemeArgs are set that we can use the unverified cert.
Has this problem already been solved somewhere (another branch or something)? I'm using this because it's really annoying on Ubuntu Vivid (Python 2.7.9). Python 2.7.9 is when this context argument was added https://docs.python.org/2/library/httplib.html#httplib.HTTPSConnection - so may need a version check or something to be compatible.
@duffolonious thanks for the targeted patch! Do you think you could submit that as a pull request so we could discuss it?
@hartsock sure, I'll get something together later today I think.
Thanks to @duffolonious, I think @parthgala should look at the patch submitted here.
@parthgala how is your progress coming along on this issue?
Any news on this?
Hi,
Any news about that?
Thanks.
With pyVmomi 6.0.0, you can use a parameter in the SmartConnect class to specify a SSLContext which is then used. This context can be set to not verify the certificate.
A short code example as described in https://dellaert.org/2015/12/02/pyvmomi-6-0-0-vsphere-6-0-and-ssl/
import ssl
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, vmodl
# Disabling SSL certificate verification
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_NONE
vc = None
vcenter_host = "10.0.0.10"
vcenter_port = 443
vcenter_username = "root"
vcenter_password = "vmware"
# Connecting to vCenter
try:
vc = SmartConnect(host=vcenter_host, user=vcenter_username, pwd=vcenter_password, port=vcenter_port, sslContext=context)
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
# Do stuff
Thanks, I confirm that it's working. Good to know
Hi @pdellaert , How to get your above patch working for python 2.7.6 ( on Ubuntu 14.04). I'm getting
AttributeError: 'module' object has no attribute 'SSLContext' on calling
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1). Any way to backport this ssl feature into python 2.7.6.
I got this working with pyvmomi 5.5.0 - the 6.0.0 package appears to be broken with Python 2.7.6.
To use 5.5.0: sudo pip install pyvmomi==5.5.0
Then, put this in your script prior to the SmartConnect call to avoid issues if your Python ever becomes upgraded: https://gist.github.com/michaelrice/a6794a017e349fc65d01
@rajalokan : As in my blogpost, you can do this as follows:
import ssl
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, vmodl
context = None
# Disabling urllib3 ssl warnings
requests.packages.urllib3.disable_warnings()
# Disabling SSL certificate verification
if hasattr(ssl, 'SSLContext'):
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_NONE
vc = None
vcenter_host = "10.0.0.10"
vcenter_port = 443
vcenter_username = "root"
vcenter_password = "vmware"
# Connecting to vCenter
try:
if context:
vc = SmartConnect(host=vcenter_host, user=vcenter_username, pwd=vcenter_password, port=vcenter_port, sslContext=context)
else:
vc = SmartConnect(host=vcenter_host, user=vcenter_username, pwd=vcenter_password, port=vcenter_port)
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
# Do stuff
Thanks @pwrmiller , downgrading to pyvmomi==5.5.0 works on python 2.7.6. I will wait for https://github.com/vmware/pyvmomi/pull/252 before upgrading to 6.0. Thanks @pdellaert too for suggestion.