Use yaml.safe_load to load YAML files

Since PyYAML 5.1, yaml.load without specifying the Loader option is
deprecated and shows the following warning.

YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated,
as the default Loader is unsafe.
Please read https://msg.pyyaml.org/load for full details.

This change replaces yaml.load by yaml.safe_load (which is effectively
same as adding Loader=yaml.SafeLoader) to get rid of that warning
message. Also, existing all usage of yaml.load with the Loader option
are also replaced so that we to make all implementation to load yaml
files consistent.

Change-Id: Id44fa2354429b944fbc0809f63db558bb7de23f7
This commit is contained in:
Takashi Kajinami 2021-07-22 22:08:45 +09:00
parent 9baa416151
commit 53040573ab
2 changed files with 22 additions and 19 deletions

View File

@ -58,7 +58,7 @@ def main():
output = opts.output
# We open the resource registry once
resource_registry = "./overcloud-resource-registry-puppet.yaml"
resource_reg = yaml.load(open(os.path.join(resource_registry), 'r'))
resource_reg = yaml.safe_load(open(os.path.join(resource_registry), 'r'))
if (opts.all):
# This means we will parse all the services defined
@ -73,7 +73,8 @@ def main():
# The service definition will be the same resource registry
role_resources = resource_reg
else:
role_resources = yaml.load(open(os.path.join("./roles/", role + ".yaml"), 'r'))
role_resources = yaml.safe_load(
open(os.path.join("./roles/", role + ".yaml"), 'r'))
for section_task in opts.ansible_tasks:
if(opts.all):
@ -102,7 +103,8 @@ def main():
if('::' in config_file):
print("This is a nested Heat resource")
else:
data_source = yaml.load(open("./" + config_file, 'r'))
data_source = yaml.safe_load(
open("./" + config_file, 'r'))
expression = engine(
"$.outputs.role_data.value.get(" + section_task + ").flatten().distinct()"
)
@ -121,7 +123,8 @@ def main():
if exc.errno != errno.EEXIST:
raise
save = open(tasks_output_file, 'w+')
yaml.dump(yaml.load(json.dumps(role_ansible_tasks)), save, default_flow_style=False)
yaml.dump(yaml.safe_load(json.dumps(role_ansible_tasks)),
save, default_flow_style=False)
if __name__ == '__main__':

View File

@ -292,18 +292,18 @@ def compare_parameters(old_impl_path, new_impl_path):
new_impl_params = []
for filename in glob.glob(old_impl_path + "/*.yaml"):
with open(filename, 'r') as f:
tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
tpl = yaml.safe_load(f.read())
old_impl_params.extend(tpl["parameters"].keys())
for filename in glob.glob(new_impl_path + "/*.yaml"):
with open(filename, 'r') as f:
tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
tpl = yaml.safe_load(f.read())
new_impl_params.extend(tpl["parameters"].keys())
return set(old_impl_params).difference(set(new_impl_params))
def validate_role_name(filename):
with open(filename, 'r') as f:
tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
tpl = yaml.safe_load(f.read())
role_data = tpl[0]
if role_data['name'] != os.path.basename(filename).split('.')[0]:
@ -319,7 +319,7 @@ def validate_hci_compute_services_default(env_filename, env_tpl):
roles_filename = os.path.join(os.path.dirname(env_filename),
'../roles/Compute.yaml')
with open(roles_filename, 'r') as f:
roles_tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
roles_tpl = yaml.safe_load(f.read())
for role in roles_tpl:
if role['name'] == 'Compute':
@ -335,7 +335,7 @@ def validate_hci_computehci_role(hci_role_filename, hci_role_tpl):
compute_role_filename = os.path.join(os.path.dirname(hci_role_filename),
'./Compute.yaml')
with open(compute_role_filename, 'r') as f:
compute_role_tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
compute_role_tpl = yaml.safe_load(f.read())
compute_role_services = compute_role_tpl[0]['ServicesDefault']
for role in hci_role_tpl:
@ -353,7 +353,7 @@ def validate_controller_dashboard(filename, tpl):
control_role_filename = os.path.join(os.path.dirname(filename),
'./Controller.yaml')
with open(control_role_filename, 'r') as f:
control_role_tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
control_role_tpl = yaml.safe_load(f.read())
control_role_services = control_role_tpl[0]['ServicesDefault']
for role in tpl:
@ -370,7 +370,7 @@ def validate_controller_storage_nfs(filename, tpl, exclude_service=()):
control_role_filename = os.path.join(os.path.dirname(filename),
'./Controller.yaml')
with open(control_role_filename, 'r') as f:
control_role_tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
control_role_tpl = yaml.safe_load(f.read())
control_role_services = control_role_tpl[0]['ServicesDefault']
for role in tpl:
@ -389,7 +389,7 @@ def validate_hci_role(hci_role_filename, hci_role_tpl):
compute_role_filename = \
os.path.join(os.path.dirname(hci_role_filename), './Compute.yaml')
with open(compute_role_filename, 'r') as f:
compute_role_tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
compute_role_tpl = yaml.safe_load(f.read())
compute_role_services = compute_role_tpl[0]['ServicesDefault']
for role in hci_role_tpl:
@ -428,7 +428,7 @@ def validate_ceph_role(ceph_role_filename, ceph_role_tpl):
ceph_storage_role_filename = \
os.path.join(os.path.dirname(ceph_role_filename), './CephStorage.yaml')
with open(ceph_storage_role_filename, 'r') as f:
ceph_storage_role_tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
ceph_storage_role_tpl = yaml.safe_load(f.read())
ceph_storage_role_services = ceph_storage_role_tpl[0]['ServicesDefault']
for role in ceph_role_tpl:
@ -459,7 +459,7 @@ def validate_controller_no_ceph_role(filename, tpl):
control_role_filename = os.path.join(os.path.dirname(filename),
'./Controller.yaml')
with open(control_role_filename, 'r') as f:
control_role_tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
control_role_tpl = yaml.safe_load(f.read())
control_role_services = control_role_tpl[0]['ServicesDefault']
for role in tpl:
@ -482,7 +482,7 @@ def validate_with_compute_role_services(role_filename, role_tpl, exclude_service
cmpt_filename = os.path.join(os.path.dirname(role_filename),
'./Compute.yaml')
with open(cmpt_filename, 'r') as f:
cmpt_tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
cmpt_tpl = yaml.safe_load(f.read())
cmpt_services = cmpt_tpl[0]['ServicesDefault']
cmpt_services = [x for x in cmpt_services if (x not in exclude_service)]
@ -621,7 +621,7 @@ def validate_docker_service_mysql_usage(filename, tpl):
os.path.exists(newfilename.replace('.yaml', '.j2.yaml')):
return # Skip for now if it's templated
with open(newfilename, 'r') as newfile:
newtmp = yaml.load(newfile.read(), Loader=yaml.SafeLoader)
newtmp = yaml.safe_load(newfile.read())
read_all(newfilename, newtmp)
read_all(filename, tpl)
@ -1080,7 +1080,7 @@ def validate(filename, param_map):
retval = 0
try:
with open(filename, 'r') as f:
tpl = yaml.load(f.read(), Loader=yaml.SafeLoader)
tpl = yaml.safe_load(f.read())
is_heat_template = 'heat_template_version' in tpl
@ -1274,11 +1274,11 @@ def validate_upgrade_tasks(upgrade_tasks):
def validate_network_data_file(data_file_path):
try:
with open(data_file_path, 'r') as data_file:
data_file = yaml.load(data_file.read(), Loader=yaml.SafeLoader)
data_file = yaml.safe_load(data_file.read())
base_file_path = os.path.dirname(data_file_path) + "/network_data.yaml"
with open(base_file_path, 'r') as base_file:
base_file = yaml.load(base_file.read(), Loader=yaml.SafeLoader)
base_file = yaml.safe_load(base_file.read())
retval = 0
for n in base_file: