From 1ba7b79662373c7efef4b47f907da2fb638b0eec Mon Sep 17 00:00:00 2001 From: Travis Holloway Date: Fri, 2 Aug 2024 00:08:58 -0500 Subject: [PATCH] Restore EA4 config files based on package prefix Case RE-499: The restore logic for EA4 config files was broken when the packages were provided by either CloudLinux's EA4 repo or Imunify's hardened PHP repo. This was because the restore logic was gathering the EA4 packages based on the repoid that they came from, but the regex only took cPanel's version of EA4 into account. This change makes it so that we gather these packages based on the package prefix of 'ea-' so that the config files for the installed packages are always gathered regardless of which repo provides them. Fixes #486 Changelog: Fix restore logic for config files of packages provided by EA4 --- elevate-cpanel | 21 ++++++++++++++------- lib/Elevate/Components/EA4.pm | 3 +-- lib/Elevate/RPM.pm | 18 +++++++++++++----- t/components-ea4.t | 10 ++++++++-- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/elevate-cpanel b/elevate-cpanel index 273c9e3d..c3ba6ef6 100755 --- a/elevate-cpanel +++ b/elevate-cpanel @@ -3441,8 +3441,7 @@ EOS Elevate::StageFile::remove_from_stage_file('ea4_config_files'); - my $ea4_regex = qr/^EA4(:?-c7)?/a; - my $ea4_config_files = $self->rpm->get_config_files_for_repo($ea4_regex); + my $ea4_config_files = $self->rpm->get_config_files_for_pkg_prefix('ea-'); Elevate::StageFile::update_stage_file( { ea4_config_files => $ea4_config_files } ); @@ -7226,10 +7225,11 @@ EOS die q[Missing cpev]; } - sub get_config_files_for_repo ( $self, $repo ) { + sub get_config_files_for_pkg_prefix ( $self, $prefix ) { - my @installed = cpev::get_installed_rpms_in_repo($repo); - my $config_files = $self->_get_config_files( \@installed ); + my @installed_rpms = $self->get_installed_rpms(q[%{NAME}\n]); + my @ea_rpms = grep { $_ =~ qr/^\Q$prefix\E/ } @installed_rpms; + my $config_files = $self->_get_config_files( \@ea_rpms ); return $config_files; } @@ -7288,8 +7288,15 @@ EOS return; } - sub get_installed_rpms ($self) { - my $out = $self->cpev->ssystem_capture_output( $rpm, '-qa' ); + sub get_installed_rpms ( $self, $format = undef ) { + my @args = qw{-qa}; + + if ($format) { + push @args, '--queryformat'; + push @args, $format; + } + + my $out = $self->cpev->ssystem_capture_output( $rpm, @args ); return @{ $out->{stdout} }; } diff --git a/lib/Elevate/Components/EA4.pm b/lib/Elevate/Components/EA4.pm index 9d557e88..1bef22df 100644 --- a/lib/Elevate/Components/EA4.pm +++ b/lib/Elevate/Components/EA4.pm @@ -118,8 +118,7 @@ sub _backup_config_files ($self) { Elevate::StageFile::remove_from_stage_file('ea4_config_files'); - my $ea4_regex = qr/^EA4(:?-c7)?/a; - my $ea4_config_files = $self->rpm->get_config_files_for_repo($ea4_regex); + my $ea4_config_files = $self->rpm->get_config_files_for_pkg_prefix('ea-'); Elevate::StageFile::update_stage_file( { ea4_config_files => $ea4_config_files } ); diff --git a/lib/Elevate/RPM.pm b/lib/Elevate/RPM.pm index e8eae60a..1fc4fd33 100644 --- a/lib/Elevate/RPM.pm +++ b/lib/Elevate/RPM.pm @@ -24,10 +24,11 @@ sub _build_cpev { die q[Missing cpev]; } -sub get_config_files_for_repo ( $self, $repo ) { +sub get_config_files_for_pkg_prefix ( $self, $prefix ) { - my @installed = cpev::get_installed_rpms_in_repo($repo); - my $config_files = $self->_get_config_files( \@installed ); + my @installed_rpms = $self->get_installed_rpms(q[%{NAME}\n]); + my @ea_rpms = grep { $_ =~ qr/^\Q$prefix\E/ } @installed_rpms; + my $config_files = $self->_get_config_files( \@ea_rpms ); return $config_files; } @@ -92,8 +93,15 @@ sub remove_no_dependencies_and_justdb ( $self, $pkg ) { return; } -sub get_installed_rpms ($self) { - my $out = $self->cpev->ssystem_capture_output( $rpm, '-qa' ); +sub get_installed_rpms ( $self, $format = undef ) { + my @args = qw{-qa}; + + if ($format) { + push @args, '--queryformat'; + push @args, $format; + } + + my $out = $self->cpev->ssystem_capture_output( $rpm, @args ); return @{ $out->{stdout} }; } diff --git a/t/components-ea4.t b/t/components-ea4.t index 75eb392e..f6b1d6c3 100644 --- a/t/components-ea4.t +++ b/t/components-ea4.t @@ -455,8 +455,7 @@ sub test_backup_and_restore_config_files : Test(10) ($self) { my $cpev_mock = Test::MockModule->new('cpev'); $cpev_mock->redefine( - get_installed_rpms_in_repo => sub { return ( 'ea-foo', 'ea-bar', 'ea-nginx' ) }, - ssystem_capture_output => sub ( $, @args ) { + ssystem_capture_output => sub ( $, @args ) { my $pkg = pop @args; my $config_file = $pkg =~ /foo$/ ? '/tmp/foo.conf' : '/tmp/bar.conf'; my $ret = { @@ -467,6 +466,13 @@ sub test_backup_and_restore_config_files : Test(10) ($self) { }, ); + my $mock_rpm = Test::MockModule->new('Elevate::RPM'); + $mock_rpm->redefine( + get_installed_rpms => sub { + return ( 'ea-foo', 'ea-bar', 'ea-nginx', 'not-an-ea-package' ); + }, + ); + my $ea4 = cpev->new->component('EA4'); is( $ea4->_backup_config_files(), undef, '_backup_config_files() successfully completes' );