Skip to content

Commit 2f50633

Browse files
authored
Merge pull request #1638 from grycap/virtualenv
Add bandit tests
2 parents 7d8a838 + f044f99 commit 2f50633

File tree

12 files changed

+29
-23
lines changed

12 files changed

+29
-23
lines changed

.github/workflows/main.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ jobs:
2424
- name: Check code style
2525
run: tox -e style
2626

27+
- name: Check security
28+
run: tox -e bandit
29+
2730
- name: Unit tests
2831
run: tox -e coverage
2932

IM/ConfManager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ def configure_ansible(self, ssh, tmp_dir, ansible_version=None):
13841384
if ssh.proxy_host.private_key:
13851385
priv_key_filename = "/var/tmp/%s_%s_%s.pem" % (ssh.proxy_host.username,
13861386
ssh.username,
1387-
ssh.host)
1387+
ssh.host) # nosec
13881388
# copy it to the proxy host to enable im_client to use it
13891389
# ssh.proxy_host.sftp_put_content(ssh.proxy_host.private_key, priv_key_filename)
13901390
# ssh.proxy_host.sftp_chmod(priv_key_filename, 0o600)

IM/CtxtAgentBase.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def add_proxy_host_line(self, vm_data):
289289
# we must create it in the localhost to use it later with ansible
290290
priv_key_filename = "/var/tmp/%s_%s_%s.pem" % (proxy['user'],
291291
vm_data['user'],
292-
vm_data['ip'])
292+
vm_data['ip']) # nosec
293293
with open(priv_key_filename, 'w') as f:
294294
f.write(proxy['private_key'])
295295
os.chmod(priv_key_filename, 0o600)
@@ -502,7 +502,7 @@ def install_ansible_roles(self, general_conf_data, playbook):
502502

503503
if galaxy_collections:
504504
now = str(int(time.time() * 100))
505-
filename = "/tmp/galaxy_collections_%s.yml" % now
505+
filename = "/tmp/galaxy_collections_%s.yml" % now # nosec
506506
yaml_deps = yaml.safe_dump({"collections": galaxy_collections}, default_flow_style=True)
507507
self.logger.debug("Galaxy collections file: %s" % yaml_deps)
508508
task = {"copy": 'dest=%s content="%s"' % (filename, yaml_deps)}
@@ -556,7 +556,7 @@ def install_ansible_roles(self, general_conf_data, playbook):
556556

557557
if galaxy_dependencies:
558558
now = str(int(time.time() * 100))
559-
filename = "/tmp/galaxy_roles_%s.yml" % now
559+
filename = "/tmp/galaxy_roles_%s.yml" % now # nosec
560560
yaml_deps = yaml.safe_dump(galaxy_dependencies, default_flow_style=True)
561561
self.logger.debug("Galaxy depencies file: %s" % yaml_deps)
562562
task = {"copy": 'dest=%s content="%s"' % (filename, yaml_deps)}
@@ -598,7 +598,7 @@ def LaunchAnsiblePlaybook(self, output, remote_dir, playbook_file, vm, threads,
598598
gen_pk_file = pk_file
599599
else:
600600
if vm['private_key'] and not vm['passwd']:
601-
gen_pk_file = "/tmp/pk_" + vm['ip'] + ".pem"
601+
gen_pk_file = "/tmp/pk_" + vm['ip'] + ".pem" # nosec
602602
pk_out = open(gen_pk_file, 'w')
603603
pk_out.write(vm['private_key'])
604604
pk_out.close()

IM/InfrastructureList.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,13 @@ def _get_data_from_db(db_url, inf_id=None, auth=None):
182182
if db.db_type == DataBase.MONGO:
183183
res = db.find("inf_list", {"id": inf_id}, {data_field: True, "deleted": True})
184184
else:
185-
res = db.select("select " + data_field + ",deleted from inf_list where id = %s", (inf_id,))
185+
res = db.select("select " + data_field + ",deleted from inf_list where id = %s", # nosec
186+
(inf_id,))
186187
else:
187188
if db.db_type == DataBase.MONGO:
188189
res = db.find("inf_list", {"deleted": 0}, {data_field: True, "deleted": True}, [('_id', -1)])
189190
else:
190-
res = db.select("select " + data_field + ",deleted from inf_list where deleted = 0"
191+
res = db.select("select " + data_field + ",deleted from inf_list where deleted = 0" # nosec
191192
" order by rowid desc")
192193
if len(res) > 0:
193194
for elem in res:
@@ -296,7 +297,7 @@ def _get_inf_ids_from_db(auth=None):
296297
where = "where deleted = 0 and (%s)" % like
297298
else:
298299
where = "where deleted = 0"
299-
res = db.select("select id from inf_list %s order by rowid desc" % where)
300+
res = db.select("select id from inf_list %s order by rowid desc" % where) # nosec
300301
for elem in res:
301302
if db.db_type == DataBase.MONGO:
302303
inf_list.append(elem['id'])

IM/SSH.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def run(self):
7777
channel = self.client.get_transport().open_session()
7878
if self.ssh.tty:
7979
channel.get_pty()
80-
channel.exec_command(self.command + "\n")
80+
channel.exec_command(self.command + "\n") # nosec
8181
stdout = channel.makefile()
8282
stderr = channel.makefile_stderr()
8383
exit_status = channel.recv_exit_status()
@@ -182,13 +182,13 @@ def connect(self, time_out=None):
182182
return self.client, self.proxy
183183

184184
client = paramiko.SSHClient()
185-
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
185+
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # nosec
186186

187187
proxy = None
188188
proxy_channel = None
189189
if self.proxy_host:
190190
proxy = paramiko.SSHClient()
191-
proxy.set_missing_host_key_policy(paramiko.AutoAddPolicy())
191+
proxy.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # nosec
192192
proxy.connect(self.proxy_host.host, self.proxy_host.port, username=self.proxy_host.username,
193193
password=self.proxy_host.password, pkey=self.proxy_host.private_key_obj)
194194
proxy_transport = proxy.get_transport()
@@ -264,7 +264,7 @@ def execute(self, command, timeout=None):
264264
if self.tty:
265265
channel.get_pty()
266266

267-
channel.exec_command(command + "\n")
267+
channel.exec_command(command + "\n") # nosec
268268
stdout = channel.makefile()
269269
stderr = channel.makefile_stderr()
270270
exit_status = channel.recv_exit_status()

IM/Stats.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def get_stats(init_date="1970-01-01", end_date=None, auth=None):
125125
if like:
126126
where += " and"
127127
where += " date <= '%s'" % end_date
128-
res = db.select("select data, date, id from inf_list %s order by rowid desc" % where)
128+
res = db.select("select data, date, id from inf_list %s order by rowid desc" % where) # nosec
129129

130130
for elem in res:
131131
if db.db_type == DataBase.MONGO:

IM/VirtualMachine.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ def get_ssh_command(self):
11351135
reverse_opt = "-R %d:localhost:22" % (self.SSH_REVERSE_BASE_PORT + self.creation_im_id)
11361136

11371137
if ssh.private_key:
1138-
filename = "/tmp/%s_%s.pem" % (self.inf.id, self.im_id)
1138+
filename = "/tmp/%s_%s.pem" % (self.inf.id, self.im_id) # nosec
11391139
command = 'echo "%s" > %s && chmod 400 %s ' % (ssh.private_key, filename, filename)
11401140
command += ('&& ssh -N %s -p %s -i %s -o "UserKnownHostsFile=/dev/null"'
11411141
' -o "StrictHostKeyChecking=no" %s@%s &' % (reverse_opt,

IM/config.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ class Config:
5858
WAIT_SSH_ACCCESS_TIMEOUT = 300
5959
WAIT_PUBLIC_IP_TIMEOUT = 90
6060
XMLRCP_PORT = 8899
61-
XMLRCP_ADDRESS = "0.0.0.0"
61+
XMLRCP_ADDRESS = "0.0.0.0" # nosec
6262
ACTIVATE_REST = True
6363
REST_PORT = 8800
64-
REST_ADDRESS = "0.0.0.0"
64+
REST_ADDRESS = "0.0.0.0" # nosec
6565
USER_DB = ""
6666
IM_PATH = os.path.dirname(os.path.realpath(__file__))
6767
LOG_FILE = '/var/log/im/inf.log'
@@ -85,7 +85,7 @@ class Config:
8585
VM_INFO_UPDATE_FREQUENCY = 10
8686
# This value must be always higher than VM_INFO_UPDATE_FREQUENCY
8787
VM_INFO_UPDATE_ERROR_GRACE_PERIOD = 120
88-
REMOTE_CONF_DIR = "/var/tmp/.im"
88+
REMOTE_CONF_DIR = "/var/tmp/.im" # nosec
8989
MAX_SSH_ERRORS = 5
9090
PRIVATE_NET_MASKS = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16",
9191
"169.254.0.0/16", "100.64.0.0/10", "192.0.0.0/24", "198.18.0.0/15"]

IM/connectors/Docker.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,16 @@ def _generate_create_svc_request_data(self, image_name, outports, vm, ssh_port,
182182
command += " ; "
183183
command += "mkdir /var/run/sshd"
184184
command += " ; "
185-
command += "sed -i '/PermitRootLogin/c\PermitRootLogin yes' /etc/ssh/sshd_config"
185+
command += "sed -i '/PermitRootLogin/c\\PermitRootLogin yes' /etc/ssh/sshd_config"
186186
command += " ; "
187187
command += "rm -f /etc/ssh/ssh_host_rsa_key*"
188188
command += " ; "
189189
command += "ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''"
190190
command += " ; "
191191
command += "echo 'root:" + self._root_password + "' | chpasswd"
192192
command += " ; "
193-
command += "sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd"
193+
command += ("sed 's@session\\s*required\\s*pam_loginuid.so@session " +
194+
"optional pam_loginuid.so@g' -i /etc/pam.d/sshd")
194195
command += " ; "
195196
command += " /usr/sbin/sshd -D"
196197

@@ -264,7 +265,8 @@ def _generate_create_cont_request_data(self, image_name, outports, vm, ssh_port)
264265
command += " ; "
265266
command += "echo 'root:" + self._root_password + "' | chpasswd"
266267
command += " ; "
267-
command += "sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd"
268+
command += ("sed 's@session\\s*required\\s*pam_loginuid.so@session" +
269+
" optional pam_loginuid.so@g' -i /etc/pam.d/sshd")
268270
command += " ; "
269271
command += " /usr/sbin/sshd -D"
270272

IM/connectors/OCCI.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def get_net_info(occi_res):
229229
mask) for mask in Config.PRIVATE_NET_MASKS])
230230
elif kv[0].strip() == "occi.networkinterface.interface":
231231
net_interface = kv[1].strip('"')
232-
num_interface = re.findall('\d+', net_interface)[0]
232+
num_interface = re.findall(r'\d+', net_interface)[0]
233233
elif kv[0].strip() == "self":
234234
link = kv[1].strip('"')
235235
if num_interface and ip_address:

IM/tosca/Tosca.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ def _gen_configure_from_interfaces(self, node, compute, interfaces):
800800
variables = ""
801801
tasks = ""
802802
recipe_list = []
803-
remote_artifacts_path = "/tmp"
803+
remote_artifacts_path = "/tmp" # nosec
804804
# Take the interfaces in correct order
805805
for name in ['create', 'pre_configure_source', 'pre_configure_target', 'configure_rel',
806806
'configure', 'post_configure_source', 'post_configure_target', 'start',

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ commands = python -m coverage run --source=. -m unittest discover -v -s test/uni
1919

2020
[testenv:bandit]
2121
deps = bandit
22-
commands = bandit IM -r -f html -o bandit.html -s B108,B601,B608,B507,B104 -ll
22+
commands = bandit IM -r -ll
2323

2424
[flake8]
2525
ignore = E402,E265,W605,W504,F811

0 commit comments

Comments
 (0)