From 3f39f354d1aac20852ff8c998928f68a09cbf552 Mon Sep 17 00:00:00 2001 From: RedProkofiev Date: Fri, 29 Sep 2023 14:16:55 +0000 Subject: [PATCH 1/6] String/Bytes fix --- ssm/ssm2.py | 8 +++++++- test/test_ssm.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ssm/ssm2.py b/ssm/ssm2.py index 9cbc28e2..e0d9ef83 100644 --- a/ssm/ssm2.py +++ b/ssm/ssm2.py @@ -318,6 +318,13 @@ def _handle_msg(self, text): def _save_msg_to_queue(self, body, empaid): """Extract message contents and add to the accept or reject queue.""" + try: + # if not bytes will fail with "'str' obj has no attribute decode" + body = body.decode('utf-8') + except (AttributeError): + # Message type is something string related + pass + extracted_msg, signer, err_msg = self._handle_msg(body) try: # If the message is empty or the error message is not empty @@ -332,7 +339,6 @@ def _save_msg_to_queue(self, body, empaid): body = extracted_msg log.warning("Message rejected: %s", err_msg) - name = self._rejectq.add({'body': body, 'signer': signer, 'empaid': empaid, diff --git a/test/test_ssm.py b/test/test_ssm.py index 5f96fd78..7d9f51a6 100644 --- a/test/test_ssm.py +++ b/test/test_ssm.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from __future__ import print_function import os @@ -93,6 +94,15 @@ def test_on_message(self): # Check that msg with ID and no real content doesn't raise exception. test_ssm.on_message({'empa-id': '012345'}, 'body') + def test_str_bytes_outgoing(self): + """Test to ensure that outgoing messages are converted from Bytes to Str""" + test_ssm = Ssm2(self._brokers, self._msgdir, TEST_CERT_FILE, + self._key_path, dest=self._dest, listen=self._listen) + + message = "Appelle Hippocampéléphantocamélos" + byte_message = message.encode() + test_ssm.on_message({'empa-id': '012345'}, byte_message) + def test_init_expired_cert(self): """Test right exception is thrown creating an SSM with expired cert.""" expected_error = ('Certificate %s has expired or will expire ' From d193577d487cd7ed3f3f1478f57ec46b39352aa2 Mon Sep 17 00:00:00 2001 From: Adrian Coveney Date: Fri, 16 Feb 2024 11:02:55 +0000 Subject: [PATCH 2/6] Revert changes to test_ssm We don't (yet) want to test against unicode. --- test/test_ssm.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/test/test_ssm.py b/test/test_ssm.py index 7d9f51a6..5f96fd78 100644 --- a/test/test_ssm.py +++ b/test/test_ssm.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from __future__ import print_function import os @@ -94,15 +93,6 @@ def test_on_message(self): # Check that msg with ID and no real content doesn't raise exception. test_ssm.on_message({'empa-id': '012345'}, 'body') - def test_str_bytes_outgoing(self): - """Test to ensure that outgoing messages are converted from Bytes to Str""" - test_ssm = Ssm2(self._brokers, self._msgdir, TEST_CERT_FILE, - self._key_path, dest=self._dest, listen=self._listen) - - message = "Appelle Hippocampéléphantocamélos" - byte_message = message.encode() - test_ssm.on_message({'empa-id': '012345'}, byte_message) - def test_init_expired_cert(self): """Test right exception is thrown creating an SSM with expired cert.""" expected_error = ('Certificate %s has expired or will expire ' From 4dbf4fd46abe28e4970865a96654f74525b62673 Mon Sep 17 00:00:00 2001 From: Adrian Coveney Date: Fri, 16 Feb 2024 11:11:04 +0000 Subject: [PATCH 3/6] Set cryptography to a more conservative version Some compatability issues raised in testing with 3.3.0 --- requirements.txt | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 900e2c36..bb9bede0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ argo-ams-library certifi<2020.4.5.2 # Used by AMS (via requests), 2020.4.5.2 dropped support for Python 2 pyopenssl<=21.0.0 # 22.0.0 dropped support for Python 2 -cryptography==3.3.0 # Crypto dropped support for Python 2 after 3.3. They're now on 41.X... +cryptography==3.2.0 # Crypto dropped support for Python 2 after 3.3 stomp.py<5.0.0 python-daemon<=2.3.0 # 2.3.1 dropped support for Python 2 python-ldap<3.4.0 # python-ldap-3.4.0 dropped support for Python 2 diff --git a/setup.py b/setup.py index a495ca0c..c72fc0f7 100644 --- a/setup.py +++ b/setup.py @@ -51,7 +51,7 @@ def main(): download_url='https://github.com/apel/ssm/releases', license='Apache License, Version 2.0', install_requires=[ - 'cryptography==3.3.0', + 'cryptography==3.2.0', 'stomp.py<5.0.0', 'python-ldap<3.4.0', 'setuptools', From 627c9d92a334239032f06abbd9a6415ce1bc625c Mon Sep 17 00:00:00 2001 From: Adrian Coveney Date: Fri, 16 Feb 2024 11:57:48 +0000 Subject: [PATCH 4/6] Change and add extra decoding - Add decoding when sending messages to fix Python 3 issues. - Set both to ascii only as this maintains the behaviour with Python 2 for now while in transition period. --- ssm/ssm2.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ssm/ssm2.py b/ssm/ssm2.py index e0d9ef83..b7dc495d 100644 --- a/ssm/ssm2.py +++ b/ssm/ssm2.py @@ -318,12 +318,8 @@ def _handle_msg(self, text): def _save_msg_to_queue(self, body, empaid): """Extract message contents and add to the accept or reject queue.""" - try: - # if not bytes will fail with "'str' obj has no attribute decode" - body = body.decode('utf-8') - except (AttributeError): - # Message type is something string related - pass + if isinstance(body, bytes): + body = body.decode('ascii') extracted_msg, signer, err_msg = self._handle_msg(body) try: @@ -485,6 +481,8 @@ def send_all(self): continue text = self._outq.get(msgid) + if isinstance(text, bytes): + text = text.decode('ascii') if self._protocol == Ssm2.STOMP_MESSAGING: # Then we are sending to a STOMP message broker. From 884ab2ccfde13243b476770d278ff4f336110065 Mon Sep 17 00:00:00 2001 From: Adrian Coveney Date: Fri, 16 Feb 2024 13:46:17 +0000 Subject: [PATCH 5/6] Fix dependency issue - Set pyopenssl >=19.1 so that it is compatable with cryptography >=3.3.0 - Revert cryptography requriement to 3.3 --- requirements.txt | 4 ++-- setup.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index bb9bede0..ed1271f1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,8 +2,8 @@ argo-ams-library certifi<2020.4.5.2 # Used by AMS (via requests), 2020.4.5.2 dropped support for Python 2 -pyopenssl<=21.0.0 # 22.0.0 dropped support for Python 2 -cryptography==3.2.0 # Crypto dropped support for Python 2 after 3.3 +pyopenssl >=19.1.0, <=21.0.0 # 22.0.0 dropped support for Python 2 +cryptography==3.3.0 # Crypto dropped support for Python 2 after 3.3 stomp.py<5.0.0 python-daemon<=2.3.0 # 2.3.1 dropped support for Python 2 python-ldap<3.4.0 # python-ldap-3.4.0 dropped support for Python 2 diff --git a/setup.py b/setup.py index c72fc0f7..9fa8a3cf 100644 --- a/setup.py +++ b/setup.py @@ -51,11 +51,11 @@ def main(): download_url='https://github.com/apel/ssm/releases', license='Apache License, Version 2.0', install_requires=[ - 'cryptography==3.2.0', + 'cryptography==3.3.0', 'stomp.py<5.0.0', 'python-ldap<3.4.0', 'setuptools', - 'pyopenssl<=21.0.0', + 'pyopenssl >=19.1.0, <=21.0.0', ], extras_require={ 'AMS': ['argo-ams-library', 'certifi<2020.4.5.2', ], From 4d200c57f6b8237b575f14b1c04678886441c5fa Mon Sep 17 00:00:00 2001 From: Adrian Coveney Date: Fri, 16 Feb 2024 14:31:02 +0000 Subject: [PATCH 6/6] Change cryptography version Pin to 3.3.2, the latest version that's still compatible with Python 2. --- requirements.txt | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index ed1271f1..92e5691d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ argo-ams-library certifi<2020.4.5.2 # Used by AMS (via requests), 2020.4.5.2 dropped support for Python 2 pyopenssl >=19.1.0, <=21.0.0 # 22.0.0 dropped support for Python 2 -cryptography==3.3.0 # Crypto dropped support for Python 2 after 3.3 +cryptography==3.3.2 # Crypto dropped support for Python 2 after 3.3 stomp.py<5.0.0 python-daemon<=2.3.0 # 2.3.1 dropped support for Python 2 python-ldap<3.4.0 # python-ldap-3.4.0 dropped support for Python 2 diff --git a/setup.py b/setup.py index 9fa8a3cf..c33e93c9 100644 --- a/setup.py +++ b/setup.py @@ -51,7 +51,7 @@ def main(): download_url='https://github.com/apel/ssm/releases', license='Apache License, Version 2.0', install_requires=[ - 'cryptography==3.3.0', + 'cryptography==3.3.2', 'stomp.py<5.0.0', 'python-ldap<3.4.0', 'setuptools',