Skip to content

Commit

Permalink
Include JSON data about repo in blocker reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Bee committed Dec 20, 2023
1 parent 0e9de4a commit 50c211e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 28 deletions.
43 changes: 32 additions & 11 deletions elevate-cpanel
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -1453,7 +1461,8 @@ EOS

use cPstrict;

use Cpanel::OS ();
use Cpanel::OS ();
use Cpanel::JSON ();

use Elevate::Constants ();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1674,15 +1688,22 @@ 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",
$total_pkg,
$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 {
Expand Down
24 changes: 16 additions & 8 deletions lib/Elevate/Blockers/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
21 changes: 17 additions & 4 deletions lib/Elevate/Blockers/Repositories.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -242,15 +248,22 @@ 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",
$total_pkg,
$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 {
Expand Down
17 changes: 12 additions & 5 deletions t/blocker-Repositories.t
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 50c211e

Please sign in to comment.