Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Replace error messages and add to ERROR_MSGS enum #159

Merged
merged 3 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [5.0.2] - 2023-01-04
### Fixed
* `pyopenssl` dependency bumped
### Changed
* Error messages added to enum

## [5.0.1] - 2022-12-21
### Fixed
* Error when processing some exceptions
Expand Down
2 changes: 1 addition & 1 deletion devo/__version__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__description__ = 'Devo Python Library.'
__url__ = 'http://www.devo.com'
__version__ = "5.0.1"
__version__ = "5.0.2"
__author__ = 'Devo'
__author_email__ = 'support@devo.com'
__license__ = 'MIT'
Expand Down
86 changes: 47 additions & 39 deletions devo/sender/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@

class ERROR_MSGS(str, Enum):
WRONG_FILE_TYPE = "'%s' is not a valid type to be opened as a file"
ADDRESS_TUPLE = "Devo-SenderConfigSSL| address must be a tuple (\"hostname\", int(port))'",
WRONG_SSL_CONFIG = "Devo-SenderConfigSSL|Can't create SSL config: %s",
CONFIG_FILE_NOT_FOUND = "Error in the configuration, %s is not a file or the path does not exist",
CANT_READ_CONFIG_FILE = "Error in the configuration %s can't be read\noriginal error: %s",
ADDRESS_TUPLE = "Devo-SenderConfigSSL| address must be a tuple (\"hostname\", int(port))'"
WRONG_SSL_CONFIG = "Devo-SenderConfigSSL|Can't create SSL config: %s"
CONFIG_FILE_NOT_FOUND = "Error in the configuration, %s is not a file or the path does not exist"
CANT_READ_CONFIG_FILE = "Error in the configuration %s can't be read\noriginal error: %s"
CONFIG_FILE_PROBLEM = "Error in the configuration, %s problem related to: %s"
KEY_NOT_COMPATIBLE_WITH_CERT = "Error in the configuration, the key: %s is not compatible with the cert: %s\noriginal error: %s"
CHAIN_NOT_COMPATIBLE_WITH_CERT = "Error in config, the chain: %s is not compatible with the certificate: %s\noriginal error: %s"
TIMEOUT_RELATED_TO_AN_INCORRECT_ADDRESS_PORT = "Possible error in config, a timeout could be related to an incorrect address/port: %s\noriginal error: %s"
INCORRECT_ADDRESS_PORT = "Error in config, incorrect address/port: %s\noriginal error: %s"
CERTIFICATE_IN_ADDRESS_IS_NOT_COMPATIBLE = "Error in config, the certificate in the address: %s is not compatible with: %s"
ADDRESS_MUST_BE_A_TUPLE = "Devo-SenderConfigSSL| address must be a tuple '(\"hostname\", int(port))'"
CANT_CREATE_TCP_CONFIG = "DevoSenderConfigTCP|Can't create TCP config: %s"
PROBLEMS_WITH_SENDER_ARGS = "Problems with args passed to Sender"
TCP_CONN_ESTABLISHMENT_SOCKET = "TCP conn establishment socket error: %s"
SSL_CONN_ESTABLISHMENT_SOCKET = "SSL conn establishment socket error: %s"
PFX_CERTIFICATE_READ_FAILED = "PFX Certificate read failed: %s"
SEND_ERROR = "Send error"
SOCKET_ERROR = "Socket error: %s"
SOCKET_CANT_CONNECT_UNKNOWN_ERROR = "Socket cant connect: unknown error"
NO_ADDRESS = "No address"


class DevoSenderException(Exception):
Expand Down Expand Up @@ -142,9 +157,9 @@ def check_config_certificate_key(self):
context.check_privatekey()
except SSL.Error as message:
raise DevoSenderException(
"Error in the configuration, the key: " + self.key +
" is not compatible with the cert: " + self.cert +
"\noriginal error: " + str(message)) from message
ERROR_MSGS.KEY_NOT_COMPATIBLE_WITH_CERT % (
self.key, self.cert, str(message)
)) from message
return True

def check_config_certificate_chain(self):
Expand Down Expand Up @@ -172,9 +187,9 @@ def check_config_certificate_chain(self):
store_ctx.verify_certificate()
except crypto.X509StoreContextError as message:
raise DevoSenderException(
"Error in config, the chain: " + self.chain +
" is not compatible with the certificate: " + self.cert +
"\noriginal error: " + str(message)) from message
ERROR_MSGS.CHAIN_NOT_COMPATIBLE_WITH_CERT % (
self.chain, self.cert, str(message)
)) from message
return True

def check_config_certificate_address(self):
Expand All @@ -193,14 +208,14 @@ def check_config_certificate_address(self):
connection.connect(self.address)
except socket.timeout as message:
raise DevoSenderException(
"Possible error in config, a timeout could be related " +
"to an incorrect address/port: " + str(self.address) +
"\noriginal error: " + str(message)) from message
ERROR_MSGS.TIMEOUT_RELATED_TO_AN_INCORRECT_ADDRESS_PORT % (
str(self.address), str(message)
)) from message
except ConnectionRefusedError as message:
raise DevoSenderException(
"Error in config, incorrect address/port: "
+ str(self.address) +
"\noriginal error: " + str(message)) from message
ERROR_MSGS.INCORRECT_ADDRESS_PORT % (
str(self.address), str(message)
)) from message
sock.setblocking(True)
connection.do_handshake()
server_chain = connection.get_peer_cert_chain()
Expand All @@ -222,10 +237,9 @@ def check_config_certificate_address(self):
return True

raise DevoSenderException(
"Error in config, the certificate in the address: "
+ self.address[0] +
" is not compatible with: " +
self.chain)
ERROR_MSGS.CERTIFICATE_IN_ADDRESS_IS_NOT_COMPATIBLE % (
self.address[0], self.chain
))

@staticmethod
def get_common_names(cert_chain, components_type):
Expand Down Expand Up @@ -261,17 +275,14 @@ class SenderConfigTCP:

def __init__(self, address=None):
if not isinstance(address, tuple):
raise DevoSenderException(
"Devo-SenderConfigSSL| address must be a tuple "
"'(\"hostname\", int(port))'")
raise DevoSenderException(ERROR_MSGS.ADDRESS_MUST_BE_A_TUPLE)
try:
self.address = address
self.hostname = socket.gethostname()
self.sec_level = None
except Exception as error:
raise DevoSenderException(
"DevoSenderConfigTCP|Can't create TCP config: "
"%s" % str(error)) from error
ERROR_MSGS.CANT_CREATE_TCP_CONFIG % str(error)) from error


class SenderBuffer:
Expand Down Expand Up @@ -299,7 +310,7 @@ class Sender(logging.Handler):
def __init__(self, config=None, con_type=None,
timeout=30, debug=False, logger=None):
if config is None:
raise DevoSenderException("Problems with args passed to Sender")
raise DevoSenderException(ERROR_MSGS.PROBLEMS_WITH_SENDER_ARGS)

self.socket = None
self.reconnection = 0
Expand Down Expand Up @@ -354,8 +365,7 @@ def __connect_tcp_socket(self):
except socket.error as error:
self.close()
raise DevoSenderException(
"TCP conn establishment socket error: %s" % str(
error)) from error
ERROR_MSGS.TCP_CONN_ESTABLISHMENT_SOCKET % str(error)) from error

self.timestart = int(round(time.time() * 1000))

Expand All @@ -380,8 +390,7 @@ def __connect_ssl(self):
except Exception as error:
self.close()
raise DevoSenderException(
"PFX Certificate read failed: %s" %
str(error)) from error
ERROR_MSGS.PFX_CERTIFICATE_READ_FAILED % str(error)) from error
try:
try:
if self._sender_config.key is not None \
Expand Down Expand Up @@ -425,8 +434,7 @@ def __connect_ssl(self):
except socket.error as error:
self.close()
raise DevoSenderException(
"SSL conn establishment socket error: %s" %
str(error)) from error
ERROR_MSGS.SSL_CONN_ESTABLISHMENT_SOCKET % str(error)) from error

def info(self, msg):
"""
Expand Down Expand Up @@ -562,10 +570,10 @@ def __send_oc(self, record):
part = record[int(iteration * 4096):
int((iteration + 1) * 4096)]
if self.socket.sendall(part) is not None:
raise DevoSenderException("Send error")
raise DevoSenderException(ERROR_MSGS.SEND_ERROR)
sent += len(part)
if sent == 0:
raise DevoSenderException("Send error")
raise DevoSenderException(ERROR_MSGS.SEND_ERROR)
return sent

def send_raw(self, record, multiline=False, zip=False):
Expand All @@ -586,7 +594,7 @@ def send_raw(self, record, multiline=False, zip=False):
msg = self.__encode_record(record)
sent = len(msg)
if self.socket.sendall(msg) is not None:
raise DevoSenderException("Send error")
raise DevoSenderException(ERROR_MSGS.SEND_ERROR)
return 1
if multiline:
record = self.__encode_multiline(record)
Expand All @@ -595,15 +603,15 @@ def send_raw(self, record, multiline=False, zip=False):
if sent:
return 1
return 0
except socket.error:
except socket.error as error:
self.close()
raise DevoSenderException(
"Socket error: %s" % str(socket.error)) from error
ERROR_MSGS.SOCKET_ERROR % str(error)) from error
finally:
if self.debug:
self.logger.debug('sent|%d|size|%d|msg|%s' %
(sent, len(record), record))
raise Exception("Socket cant connect: unknown error")
raise Exception(ERROR_MSGS.SOCKET_CANT_CONNECT_UNKNOWN_ERROR)
except Exception as error:
raise DevoSenderException(error) from error

Expand Down Expand Up @@ -773,7 +781,7 @@ def _from_dict(config=None, con_type=None):
address = config.get("address", None)

if not address:
raise DevoSenderException("No address")
raise DevoSenderException(ERROR_MSGS.NO_ADDRESS)

if not isinstance(address, tuple):
address = (address, int(config.get("port", 443)))
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ click==8.1.3
PyYAML==6.0
requests==2.27.1
pem==21.2.0
pyopenssl==22.0.0
pyopenssl==22.1.*
urllib3>=1.26.5
pytz>=2019.3

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"Topic :: Software Development :: Libraries :: Python Modules",
]
INSTALL_REQUIRES = ['requests==2.27.1', 'click==8.1.3', 'PyYAML==6.0',
'pem==21.2.0', 'pyopenssl==22.0.0', 'urllib3>=1.26.5','pytz>=2019.3']
'pem==21.2.0', 'pyopenssl==22.1.*', 'urllib3>=1.26.5','pytz>=2019.3']
CLI = ['devo-sender=devo.sender.scripts.sender_cli:cli',
'devo-api=devo.api.scripts.client_cli:cli']

Expand Down