From 9bccb666e29dadc8b84a3de32a42a5bbc3a343b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20R=C5=AF=C5=BEi=C4=8Dka?= Date: Tue, 3 Dec 2024 13:47:20 +0100 Subject: [PATCH] Fixes #38006 - Allow extension of list of packages (#585) * Fixes #38006 - Allow extension of list of packages with scap contents * Refs #38006 - Make it rails7-compatible --- .../v2/compliance/scap_contents_controller.rb | 2 -- .../services}/foreman_openscap/bulk_upload.rb | 21 ++++++++++++------- lib/tasks/foreman_openscap_tasks.rake | 1 - test/lib/foreman_openscap/bulk_upload_test.rb | 7 +++---- 4 files changed, 17 insertions(+), 14 deletions(-) rename {lib => app/services}/foreman_openscap/bulk_upload.rb (80%) diff --git a/app/controllers/api/v2/compliance/scap_contents_controller.rb b/app/controllers/api/v2/compliance/scap_contents_controller.rb index 991f19d99..c16b5f292 100644 --- a/app/controllers/api/v2/compliance/scap_contents_controller.rb +++ b/app/controllers/api/v2/compliance/scap_contents_controller.rb @@ -1,5 +1,3 @@ -require 'foreman_openscap/bulk_upload' - module Api::V2 module Compliance class ScapContentsController < ::Api::V2::BaseController diff --git a/lib/foreman_openscap/bulk_upload.rb b/app/services/foreman_openscap/bulk_upload.rb similarity index 80% rename from lib/foreman_openscap/bulk_upload.rb rename to app/services/foreman_openscap/bulk_upload.rb index 81ada2e85..3e0c6ef89 100644 --- a/lib/foreman_openscap/bulk_upload.rb +++ b/app/services/foreman_openscap/bulk_upload.rb @@ -7,21 +7,28 @@ def initialize @result = OpenStruct.new(:errors => [], :results => []) end - def files_from_guide - `rpm -ql scap-security-guide | grep ds.xml`.split + def security_guide_packages + %w(scap-security-guide) end - def scap_guide_installed? - `rpm -qa | grep scap-security-guide`.present? + def files_from_guide(package) + `rpm -ql #{package} | grep ds.xml`.split + end + + def package_installed?(package) + `rpm -qa | grep #{package}`.present? end def upload_from_scap_guide - unless scap_guide_installed? - @result.errors.push(_("Can't find scap-security-guide RPM, are you sure it is installed on your server?")) + package = security_guide_packages.find { |p| package_installed? p } + unless package + joined_packages = security_guide_packages.join(', ') + msg = _("Can't find %{packages} RPM(s), are you sure it is installed on your server?") + @result.errors.push(msg % {packages: joined_packages}) return @result end - upload_from_files(files_from_guide, true) + upload_from_files(files_from_guide(package), true) end def upload_from_files(files_array, from_scap_guide = false) diff --git a/lib/tasks/foreman_openscap_tasks.rake b/lib/tasks/foreman_openscap_tasks.rake index 47cd1ffc4..11c750397 100644 --- a/lib/tasks/foreman_openscap_tasks.rake +++ b/lib/tasks/foreman_openscap_tasks.rake @@ -1,6 +1,5 @@ # Tasks namespace :foreman_openscap do - require 'foreman_openscap/bulk_upload' require 'foreman_openscap/message_cleaner' namespace :bulk_upload do diff --git a/test/lib/foreman_openscap/bulk_upload_test.rb b/test/lib/foreman_openscap/bulk_upload_test.rb index 1aa714340..8f41a75d8 100644 --- a/test/lib/foreman_openscap/bulk_upload_test.rb +++ b/test/lib/foreman_openscap/bulk_upload_test.rb @@ -2,7 +2,6 @@ class BulkUploadTest < ActiveSupport::TestCase setup do - require 'foreman_openscap/bulk_upload' ForemanOpenscap::ScapContent.all.map(&:destroy) end @@ -48,14 +47,14 @@ class BulkUploadTest < ActiveSupport::TestCase test 'should handle case when scap security guide is not installed' do upload = ForemanOpenscap::BulkUpload.new - upload.stubs(:scap_guide_installed?).returns(false) + upload.stubs(:package_installed?).returns(false) res = upload.upload_from_scap_guide - assert_equal "Can't find scap-security-guide RPM, are you sure it is installed on your server?", res.errors.first + assert_equal "Can't find scap-security-guide RPM(s), are you sure it is installed on your server?", res.errors.first end test 'should upload files from guide' do upload = ForemanOpenscap::BulkUpload.new - upload.stubs(:scap_guide_installed?).returns(true) + upload.stubs(:package_installed?).returns(true) upload.stubs(:files_from_guide).returns(["#{ForemanOpenscap::Engine.root}/test/files/scap_contents/ssg-fedora-ds.xml"]) assert_difference('ForemanOpenscap::ScapContent.count', 1) do upload.upload_from_scap_guide