From 50c211e6285c7225cf7812ba10bc6171bc393a54 Mon Sep 17 00:00:00 2001 From: Stephen Bee Date: Wed, 20 Dec 2023 18:45:59 +0000 Subject: [PATCH] Include JSON data about repo in blocker reporting. --- elevate-cpanel | 43 +++++++++++++++++++++------- lib/Elevate/Blockers/Base.pm | 24 ++++++++++------ lib/Elevate/Blockers/Repositories.pm | 21 +++++++++++--- t/blocker-Repositories.t | 17 +++++++---- 4 files changed, 77 insertions(+), 28 deletions(-) diff --git a/elevate-cpanel b/elevate-cpanel index 5a5a0bd3..db31eeee 100755 --- a/elevate-cpanel +++ b/elevate-cpanel @@ -146,16 +146,24 @@ BEGIN { # Suppress load of all of these at earliest point. sub has_blocker ( $self, $msg, %others ) { - my ( undef, undef, undef, $id ) = caller(1); - $id ||= ref $self; + my $caller_id; + if ( $others{'blocker_id'} ) { + $caller_id = $others{'blocker_id'}; + } + else { + ( undef, undef, undef, $caller_id ) = caller(1); + $caller_id ||= ref $self; + } - my $blocker = cpev::Blocker->new( id => $id, msg => $msg, %others ); + my $blocker = cpev::Blocker->new( id => $caller_id, msg => $msg, %others ); die $blocker if $self->cpev->_abort_on_first_blocker; - WARN( <<~"EOS"); - *** Elevation Blocker detected: *** - $msg - EOS + if ( !$others{'quiet'} ) { + WARN( <<~"EOS"); + *** Elevation Blocker detected: *** + $msg + EOS + } $self->blockers->add_blocker($blocker); @@ -1453,7 +1461,8 @@ EOS use cPstrict; - use Cpanel::OS (); + use Cpanel::OS (); + use Cpanel::JSON (); use Elevate::Constants (); @@ -1586,7 +1595,12 @@ EOS $status_hr = $self->_check_yum_repos(); } - $self->has_blocker( $msg, repos => $self->{_yum_repos_unsupported_with_packages} ) if _yum_status_hr_contains_blocker($status_hr); + return 0 unless _yum_status_hr_contains_blocker($status_hr); + + for my $unsupported_repo ( @{ $self->{_yum_repos_unsupported_with_packages} } ) { + my $blocker_id = ref($self) . '::' . $unsupported_repo->{'name'}; + $self->has_blocker( $unsupported_repo->{'json_report'}, 'blocker_id' => $blocker_id, 'quiet' => 1 ); + } } return 0; @@ -1674,7 +1688,8 @@ EOS if ( !$is_vetted ) { $status{'UNVETTED'} = 1; - if ( my $total_pkg = scalar cpev::get_installed_rpms_in_repo($current_repo_name) ) { # FIXME + my @installed_packages = cpev::get_installed_rpms_in_repo($current_repo_name); + if ( my $total_pkg = scalar @installed_packages ) { # FIXME ERROR( sprintf( "%d package(s) installed from unsupported YUM repo '%s' from %s", @@ -1682,7 +1697,13 @@ EOS $current_repo_name, $path ) ); - push( $self->{_yum_repos_unsupported_with_packages}->@*, $current_repo_name ); + push( + $self->{_yum_repos_unsupported_with_packages}->@*, + { + 'name' => $current_repo_name, + 'json_report' => Cpanel::JSON::Dump( { 'name' => $current_repo_name, 'path' => $path, 'packages' => \@installed_packages } ) + } + ); $status{'USE_RPMS_FROM_UNVETTED_REPO'} = 1; } else { diff --git a/lib/Elevate/Blockers/Base.pm b/lib/Elevate/Blockers/Base.pm index 047962ef..2f7e53d7 100644 --- a/lib/Elevate/Blockers/Base.pm +++ b/lib/Elevate/Blockers/Base.pm @@ -80,17 +80,25 @@ sub is_check_mode ( $self, @args ) { # sub has_blocker ( $self, $msg, %others ) { - # get the function caller or the object type as id (used by tests) - my ( undef, undef, undef, $id ) = caller(1); - $id ||= ref $self; + my $caller_id; + if ( $others{'blocker_id'} ) { + $caller_id = $others{'blocker_id'}; + } + else { + # get the function caller or the object type as id (used by tests) + ( undef, undef, undef, $caller_id ) = caller(1); + $caller_id ||= ref $self; + } - my $blocker = cpev::Blocker->new( id => $id, msg => $msg, %others ); + my $blocker = cpev::Blocker->new( id => $caller_id, msg => $msg, %others ); die $blocker if $self->cpev->_abort_on_first_blocker; - WARN( <<~"EOS"); - *** Elevation Blocker detected: *** - $msg - EOS + if ( !$others{'quiet'} ) { + WARN( <<~"EOS"); + *** Elevation Blocker detected: *** + $msg + EOS + } $self->blockers->add_blocker($blocker); diff --git a/lib/Elevate/Blockers/Repositories.pm b/lib/Elevate/Blockers/Repositories.pm index 1a15b4a8..38416c5a 100644 --- a/lib/Elevate/Blockers/Repositories.pm +++ b/lib/Elevate/Blockers/Repositories.pm @@ -12,7 +12,8 @@ Blocker to check if the Yum repositories are compliant with the elevate process. use cPstrict; -use Cpanel::OS (); +use Cpanel::OS (); +use Cpanel::JSON (); use Elevate::Constants (); @@ -145,7 +146,12 @@ sub _blocker_invalid_yum_repos ($self) { $status_hr = $self->_check_yum_repos(); } - $self->has_blocker( $msg, repos => $self->{_yum_repos_unsupported_with_packages} ) if _yum_status_hr_contains_blocker($status_hr); + return 0 unless _yum_status_hr_contains_blocker($status_hr); + + for my $unsupported_repo ( @{ $self->{_yum_repos_unsupported_with_packages} } ) { + my $blocker_id = ref($self) . '::' . $unsupported_repo->{'name'}; + $self->has_blocker( $unsupported_repo->{'json_report'}, 'blocker_id' => $blocker_id, 'quiet' => 1 ); + } } return 0; @@ -242,7 +248,8 @@ sub _check_yum_repos ($self) { if ( !$is_vetted ) { $status{'UNVETTED'} = 1; - if ( my $total_pkg = scalar cpev::get_installed_rpms_in_repo($current_repo_name) ) { # FIXME + my @installed_packages = cpev::get_installed_rpms_in_repo($current_repo_name); + if ( my $total_pkg = scalar @installed_packages ) { # FIXME ERROR( sprintf( "%d package(s) installed from unsupported YUM repo '%s' from %s", @@ -250,7 +257,13 @@ sub _check_yum_repos ($self) { $current_repo_name, $path ) ); - push( $self->{_yum_repos_unsupported_with_packages}->@*, $current_repo_name ); + push( + $self->{_yum_repos_unsupported_with_packages}->@*, + { + 'name' => $current_repo_name, + 'json_report' => Cpanel::JSON::Dump( { 'name' => $current_repo_name, 'path' => $path, 'packages' => \@installed_packages } ) + } + ); $status{'USE_RPMS_FROM_UNVETTED_REPO'} = 1; } else { diff --git a/t/blocker-Repositories.t b/t/blocker-Repositories.t index 46199355..b0875b58 100644 --- a/t/blocker-Repositories.t +++ b/t/blocker-Repositories.t @@ -74,15 +74,22 @@ my $mock_unknown_repo = Test::MockFile->file( "$path_yum_repos_d/Unknown.repo" = enabled=1 EOS -$cpev_mock->redefine( get_installed_rpms_in_repo => 0 ); +$cpev_mock->redefine( get_installed_rpms_in_repo => sub { return () } ); +my $mock_json = Test::MockModule->new('Cpanel::JSON'); +$mock_json->redefine('Dump' => 'foo'); is $yum->_check_yum_repos() => { $unused_repo_enabled => 1, $unvetted => 1 }, "Using an unknown enabled repo detected"; - $cpev_mock->redefine( get_installed_rpms_in_repo => 1 ); is $yum->_check_yum_repos() => { $unvetted => 1, $rpms_from_unvetted => 1 }, "Using an unknown enabled repo with installed packages detected"; -is $yum->{_yum_repos_unsupported_with_packages}, ['MyRepo'], "Names of repos are recorded in object"; - -$cpev_mock->redefine( get_installed_rpms_in_repo => 0 ); +is $yum->{_yum_repos_unsupported_with_packages}[0], + { + 'json_report' => 'foo', + 'name' => 'MyRepo' + } + , + "Names and JSON data of repos are recorded in object"; + +$cpev_mock->redefine( get_installed_rpms_in_repo => sub { return () } ); $mock_unknown_repo->contents('# whatever'); is $yum->_check_yum_repos(), {}, "no repo set";