From 1c26f10ebd7d80bb0fcc46d7f6bc4798f1f563de Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com> Date: Sat, 20 Nov 2021 09:34:14 +0100 Subject: [PATCH] Avoid assert statement outside of tests This fixes DeepSource alerts: Assert statement used outside of tests Usage of assert statement in application logic is discouraged. assert is removed with compiling to optimized byte code. Consider raising an exception instead. Ideally, assert statement should be used only in tests. Signed-off-by: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com> --- trojans/tncc-emulate.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/trojans/tncc-emulate.py b/trojans/tncc-emulate.py index dd443e0d..88a8d6cd 100755 --- a/trojans/tncc-emulate.py +++ b/trojans/tncc-emulate.py @@ -570,16 +570,17 @@ class tncc: for cert in self.avail_certs: fail = False for dn_name, dn_vals in req_dns.items(): - for name, val in dn_vals.items(): - try: - if dn_name == 'IssuerDN': - assert val in cert.issuer[name] - else: - logging.warning('Unknown DN type %s', str(dn_name)) - raise Exception() - except Exception: - fail = True - break + if dn_name == 'IssuerDN': + for name, val in dn_vals.items(): + if ( + name not in cert.issuer + or val not in cert.issuer[name] + ): + fail = True + break + else: + logging.warning('Unknown DN type %s', str(dn_name)) + fail = True if fail: break if not fail: @@ -678,10 +679,11 @@ if __name__ == "__main__": for iface in netifaces.interfaces(): try: mac = netifaces.ifaddresses(iface)[netifaces.AF_LINK][0]['addr'] - assert mac != '00:00:00:00:00:00' - mac_addrs.append(mac) - except Exception: + except (IndexError, KeyError): pass + else: + if mac != '00:00:00:00:00:00': + mac_addrs.append(mac) hostname = os.environ.get('TNCC_HOSTNAME', socket.gethostname()) -- 2.49.0