From 05e02aa5806cdd6734422b5711ddee170372440a Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Thu, 9 Jan 2020 15:15:58 -0500 Subject: [PATCH] Open and close file objects using read to avoid leaking Followup to: https://github.com/ManageIQ/manageiq-ui-classic/pull/6596 --- .../application_controller/miq_request_methods.rb | 2 +- app/controllers/application_controller/performance.rb | 2 +- app/controllers/application_controller/timelines.rb | 2 +- app/presenters/menu/yaml_loader.rb | 2 +- app/services/charts_layout_service.rb | 2 +- app/views/layouts/_perf_options.html.haml | 2 +- spec/controllers/application_controller/report_data_spec.rb | 2 +- spec/other/travis_spec.rb | 2 +- spec/product/reports_spec.rb | 4 ++-- spec/services/charts_layout_service_spec.rb | 6 +++--- 10 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/controllers/application_controller/miq_request_methods.rb b/app/controllers/application_controller/miq_request_methods.rb index b1f428db787..1cda674a63b 100644 --- a/app/controllers/application_controller/miq_request_methods.rb +++ b/app/controllers/application_controller/miq_request_methods.rb @@ -168,7 +168,7 @@ def set_pre_prov_vars unless %w[image_miq_request_new miq_template_miq_request_new].include?(params[:pressed]) path_to_report = ManageIQ::UI::Classic::Engine.root.join("product", "views", provisioning_report).to_s - @view = MiqReport.new(YAML.load(File.open(path_to_report))) + @view = MiqReport.new(YAML.load(File.read(path_to_report))) @view.db = get_template_kls.to_s report_scopes = %i[eligible_for_provisioning non_deprecated] options = options_for_provisioning(@view.db, report_scopes) diff --git a/app/controllers/application_controller/performance.rb b/app/controllers/application_controller/performance.rb index c44fe40a024..52435be8557 100644 --- a/app/controllers/application_controller/performance.rb +++ b/app/controllers/application_controller/performance.rb @@ -562,7 +562,7 @@ def date_time_running_msg(typ, timestamp) # Load a chart miq_report object from YML def perf_get_chart_rpt(chart_rpt) - MiqReport.new(YAML.load(File.open("#{CHARTS_REPORTS_FOLDER}/#{chart_rpt}.yaml"))) + MiqReport.new(YAML.load(File.read("#{CHARTS_REPORTS_FOLDER}/#{chart_rpt}.yaml"))) end # Load a chart layout from YML diff --git a/app/controllers/application_controller/timelines.rb b/app/controllers/application_controller/timelines.rb index 36bc171cbf8..217e38df16d 100644 --- a/app/controllers/application_controller/timelines.rb +++ b/app/controllers/application_controller/timelines.rb @@ -42,7 +42,7 @@ def timeline_data private ############################ def tl_get_rpt(timeline) - MiqReport.new(YAML.load(File.open("#{ApplicationController::TIMELINES_FOLDER}/miq_reports/#{timeline}.yaml"))) + MiqReport.new(YAML.load(File.read("#{ApplicationController::TIMELINES_FOLDER}/miq_reports/#{timeline}.yaml"))) end def tl_build_init_options(refresh = nil) diff --git a/app/presenters/menu/yaml_loader.rb b/app/presenters/menu/yaml_loader.rb index 0745af66a2d..6a063cb6e7a 100644 --- a/app/presenters/menu/yaml_loader.rb +++ b/app/presenters/menu/yaml_loader.rb @@ -18,7 +18,7 @@ def load_custom_items private def load_custom_item(file_name) - properties = YAML.load(File.open(file_name)) + properties = YAML.load(File.read(file_name)) if properties['type'] == 'section' @sections << create_custom_menu_section(properties) else diff --git a/app/services/charts_layout_service.rb b/app/services/charts_layout_service.rb index 85519e57c54..28649cd2ca4 100644 --- a/app/services/charts_layout_service.rb +++ b/app/services/charts_layout_service.rb @@ -26,7 +26,7 @@ def layout private def build_charts - YAML.load(File.open(find_chart_path)) + YAML.load(File.read(find_chart_path)) end def find_chart_path diff --git a/app/views/layouts/_perf_options.html.haml b/app/views/layouts/_perf_options.html.haml index 82460e9f3f3..08b6c6bc837 100644 --- a/app/views/layouts/_perf_options.html.haml +++ b/app/views/layouts/_perf_options.html.haml @@ -11,7 +11,7 @@ :javascript miqInitSelectPicker(); - if @perf_options[:index] && %w(host vm).include?(request.parameters["controller"]) - - rt_chart = YAML.load(File.open("#{ApplicationController::Performance::CHARTS_LAYOUTS_FOLDER}/realtime_perf_charts/#{@perf_options[:model]}.yaml")) + - rt_chart = YAML.load(File.read("#{ApplicationController::Performance::CHARTS_LAYOUTS_FOLDER}/realtime_perf_charts/#{@perf_options[:model]}.yaml")) - @rt_hide = rt_chart[@perf_options[:index].to_i][:type] == "None" - if request.parameters["controller"] == "storage" && @perf_options[:cat] = select("perf", "typ", [[_("Hourly"), "Hourly"], [_("Daily"), "Daily"]], {:selected => @perf_options[:typ]}, diff --git a/spec/controllers/application_controller/report_data_spec.rb b/spec/controllers/application_controller/report_data_spec.rb index 76c5e497768..db877b15694 100644 --- a/spec/controllers/application_controller/report_data_spec.rb +++ b/spec/controllers/application_controller/report_data_spec.rb @@ -51,7 +51,7 @@ it "use report_name when is passed" do report_name = "ProvisionCloudTemplates.yaml" path_to_report = ManageIQ::UI::Classic::Engine.root.join("product", "views", report_name).to_s - view = MiqReport.new(YAML.load(File.open(path_to_report))) + view = MiqReport.new(YAML.load(File.read(path_to_report))) expect(controller).to_not receive(:get_db_view) controller.params = {:active_tree => "instances_tree"} controller.params = {:model_name => "ManageIQ::Providers::CloudManager::Template"} diff --git a/spec/other/travis_spec.rb b/spec/other/travis_spec.rb index 3b54426a8de..b3151abe2fa 100644 --- a/spec/other/travis_spec.rb +++ b/spec/other/travis_spec.rb @@ -1,5 +1,5 @@ describe 'travis.yml' do - let(:travis) { YAML.safe_load(File.open(ManageIQ::UI::Classic::Engine.root.join('.travis.yml'))) } + let(:travis) { YAML.safe_load(File.read(ManageIQ::UI::Classic::Engine.root.join('.travis.yml'))) } let(:versions) { travis['rvm'] } it "matches versions and excludes for multiple ruby versions" do diff --git a/spec/product/reports_spec.rb b/spec/product/reports_spec.rb index 05ba3ac0dc8..c0a71abcac7 100644 --- a/spec/product/reports_spec.rb +++ b/spec/product/reports_spec.rb @@ -18,7 +18,7 @@ shared_examples "all report type examples" do |report_yaml| let(:yaml) { report_yaml } - let(:report_data) { YAML.load(File.open(yaml)) } + let(:report_data) { YAML.load(File.read(yaml)) } it 'defines headers that match col_order' do col_order = report_data['col_order'].length @@ -30,7 +30,7 @@ context "regular reports" do shared_examples "regular report examples" do |report_yaml| let(:yaml) { report_yaml } - let(:report_data) { YAML.load(File.open(yaml)) } + let(:report_data) { YAML.load(File.read(yaml)) } it 'can be built even though without data' do # TODO: ApplicationController::Performance::CHARTS_REPORTS_FOLDER diff --git a/spec/services/charts_layout_service_spec.rb b/spec/services/charts_layout_service_spec.rb index 7149fc3c541..83fe26f38be 100644 --- a/spec/services/charts_layout_service_spec.rb +++ b/spec/services/charts_layout_service_spec.rb @@ -3,13 +3,13 @@ let(:host_redhat) { FactoryBot.create(:host_redhat) } let(:vm_openstack) { FactoryBot.create(:vm_openstack) } let(:host_openstack_infra_chart) do - YAML.load(File.open(File.join(ApplicationController::Performance::CHARTS_LAYOUTS_FOLDER, 'daily_perf_charts', 'ManageIQ_Providers_Openstack_InfraManager_Host') + '.yaml')) + YAML.load(File.read(File.join(ApplicationController::Performance::CHARTS_LAYOUTS_FOLDER, 'daily_perf_charts', 'ManageIQ_Providers_Openstack_InfraManager_Host') + '.yaml')) end let(:host_chart) do - YAML.load(File.open(File.join(ApplicationController::Performance::CHARTS_LAYOUTS_FOLDER, 'daily_perf_charts', 'Host') + '.yaml')) + YAML.load(File.read(File.join(ApplicationController::Performance::CHARTS_LAYOUTS_FOLDER, 'daily_perf_charts', 'Host') + '.yaml')) end let(:layout_chart) do - YAML.load(File.open(File.join(ApplicationController::Performance::CHARTS_LAYOUTS_FOLDER, 'daily_util_charts') + '.yaml')) + YAML.load(File.read(File.join(ApplicationController::Performance::CHARTS_LAYOUTS_FOLDER, 'daily_util_charts') + '.yaml')) end describe "#layout" do