Resolves Bandit 101 errors from misusing asserts

From Bandit's docs:
It was discovered that some projects used assert to enforce interface
constraints. However, assert is removed with compiling to optimised byte
coden (python -o producing *.pyo files). This caused various protections
to be removed. The use of assert is also considered as general bad
practice in OpenStack codebases.

Change-Id: Ie2b5e9cc3e1afcf9e9bd0f8675947754fa1e4b7d
This commit is contained in:
Ian Pittwood 2019-04-08 14:47:50 -05:00
parent 89dfec7b4c
commit 8a39a6fd17
2 changed files with 23 additions and 29 deletions

View File

@ -36,13 +36,11 @@ LOG = logging.getLogger(__name__)
class FormationPlugin(BaseDataSourcePlugin): class FormationPlugin(BaseDataSourcePlugin):
def __init__(self, region): def __init__(self, region):
# Save site name is valid # Save site name is valid
try: if not region:
assert region is not None
super().__init__(region)
except AssertionError:
LOG.error("Site: None! Spyglass exited!") LOG.error("Site: None! Spyglass exited!")
LOG.info("Check spyglass --help for details") LOG.info("Check spyglass --help for details")
exit() exit()
super().__init__(region)
self.source_type = "rest" self.source_type = "rest"
self.source_name = "formation" self.source_name = "formation"
@ -80,23 +78,21 @@ class FormationPlugin(BaseDataSourcePlugin):
def get_plugin_conf(self, kwargs): def get_plugin_conf(self, kwargs):
""" Validates the plugin param and return if success""" """ Validates the plugin param and return if success"""
try:
assert ( if not kwargs["formation_url"]:
kwargs["formation_url"] LOG.error("formation_url not specified! Spyglass exited!")
) is not None, "formation_url is Not Specified"
url = kwargs["formation_url"]
assert (
kwargs["formation_user"]
) is not None, "formation_user is Not Specified"
user = kwargs["formation_user"]
assert (
kwargs["formation_password"]
) is not None, "formation_password is Not Specified"
password = kwargs["formation_password"]
except AssertionError:
LOG.error("Insufficient plugin parameter! Spyglass exited!")
raise
exit() exit()
url = kwargs["formation_url"]
if not kwargs["formation_user"]:
LOG.error("formation_user not specified! Spyglass exited!")
exit()
user = kwargs["formation_user"]
if not kwargs["formation_password"]:
LOG.error("formation_password not specified! Spyglass exited!")
exit()
password = kwargs['formation_password']
plugin_conf = {"url": url, "user": user, "password": password} plugin_conf = {"url": url, "user": user, "password": password}
return plugin_conf return plugin_conf

View File

@ -68,16 +68,14 @@ class TugboatPlugin(BaseDataSourcePlugin):
and excel specs are not specified. The below code has been and excel specs are not specified. The below code has been
written as an additional safeguard. written as an additional safeguard.
""" """
try: if not kwargs["excel"]:
assert len(kwargs["excel"]), "Engineering Spec file not specified" LOG.error("Engineering excel file not specified: Spyglass exited!")
excel_file_info = kwargs["excel"]
assert (
kwargs["excel_spec"]
) is not None, "Excel Spec file not specified"
excel_spec_info = kwargs["excel_spec"]
except AssertionError as e:
LOG.error("{}:Spyglass exited!".format(e))
exit() exit()
excel_file_info = kwargs["excel"]
if not kwargs["excel_spec"]:
LOG.error("Engineering spec file not specified: Spyglass exited!")
exit()
excel_spec_info = kwargs["excel_spec"]
plugin_conf = { plugin_conf = {
"excel_path": excel_file_info, "excel_path": excel_file_info,
"excel_spec": excel_spec_info, "excel_spec": excel_spec_info,