PyKMIP icon indicating copy to clipboard operation
PyKMIP copied to clipboard

Add python 3.12 support

Open blink1073 opened this issue 2 years ago • 7 comments

Fixes #706

I'm a bit stuck on getting the integration tests to work, there is more different between ssl.wrap_socket and ssl.SSLContext.wrap_socket than I'd hoped.

blink1073 avatar Oct 04 '23 16:10 blink1073

The traceback on my fork is:

kmip/tests/integration/conftest.py:49: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
kmip/pie/client.py:173: in open
    self.proxy.open()
kmip/services/kmip_client.py:285: in open
    six.reraise(*last_error)
.tox/integration/lib/python3.9/site-packages/six.py:719: in reraise
    raise value
kmip/services/kmip_client.py:274: in open
    self.socket.connect((self.host, self.port))
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/ssl.py:1376: in connect
    self._real_connect(addr, False)
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/ssl.py:1367: in _real_connect
    self.do_handshake()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <ssl.SSLSocket [closed] fd=-1, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>
block = False

    @_sslcopydoc
    def do_handshake(self, block=False):
        self._check_connected()
        timeout = self.gettimeout()
        try:
            if timeout == 0.0 and block:
                self.settimeout(None)
>           self._sslobj.do_handshake()
E           ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate (_ssl.c:1129)

/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/ssl.py:1343: SSLCertVerificationError
_ ERROR at setup of TestProxyKmipClientIntegration.test_x509_certificate_register_get_destroy _

request = <SubRequest 'simple' for <TestCaseFunction test_asymmetric_key_pair_create_get_destroy>>

    @pytest.fixture(scope="class")
    def simple(request):
        config = request.config.getoption("--config")
    
        client = pclient.ProxyKmipClient(config=config)
>       client.open()

kmip/tests/integration/conftest.py:49: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
kmip/pie/client.py:173: in open
    self.proxy.open()
kmip/services/kmip_client.py:285: in open
    six.reraise(*last_error)
.tox/integration/lib/python3.9/site-packages/six.py:719: in reraise
    raise value
kmip/services/kmip_client.py:274: in open
    self.socket.connect((self.host, self.port))
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/ssl.py:1376: in connect
    self._real_connect(addr, False)
/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/ssl.py:1367: in _real_connect
    self.do_handshake()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <ssl.SSLSocket [closed] fd=-1, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>
block = False

    @_sslcopydoc
    def do_handshake(self, block=False):
        self._check_connected()
        timeout = self.gettimeout()
        try:
            if timeout == 0.0 and block:
                self.settimeout(None)
>           self._sslobj.do_handshake()
E           ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate (_ssl.c:1129)

/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/ssl.py:1343: SSLCertVerificationError

blink1073 avatar Oct 04 '23 16:10 blink1073

The problem with your mocking is that you can't mock ssl.SSLContext.wrap_socket because the code constructs an SSLContext object and then calls wrap_socket on that, so you're not mocking the right thing.

I've edited the test changes for your patch (and not your code changes, which looks good) thusly:

@@ -210,9 +210,9 @@ class TestKmipServer(testtools.TestCase)
         # Test that in ideal cases no errors are generated and the right
         # log messages are.
         with mock.patch('socket.socket') as socket_mock:
-            with mock.patch('ssl.wrap_socket') as ssl_mock:
+            with mock.patch('ssl.SSLContext') as ssl_mock:
                 socket_mock.return_value = a_mock
-                ssl_mock.return_value = b_mock
+                ssl_mock.return_value.wrap_socket.return_value = b_mock

                 manager_mock.assert_not_called()
                 monitor_mock.assert_not_called()

And I've confirmed the tests pass with Python 3.9-3.12.

s-t-e-v-e-n-k avatar Mar 28 '24 03:03 s-t-e-v-e-n-k

Thanks @s-t-e-v-e-n-k, new build in progress on my fork.

blink1073 avatar Mar 29 '24 16:03 blink1073

Still no joy: ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate (_ssl.c:1133)

blink1073 avatar Mar 29 '24 16:03 blink1073