Skip to content

Commit

Permalink
Merge pull request #456 from cPholloway/RE-403
Browse files Browse the repository at this point in the history
Use 'CloudLinux_8' target to backup EA4 profile with I360 installed
  • Loading branch information
toddr authored Jun 26, 2024
2 parents aee719b + bdb1782 commit 9cf06b5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 155 deletions.
65 changes: 30 additions & 35 deletions elevate-cpanel
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,6 @@ EOS
return unless scalar keys $dropped_pkgs->%*;

my @incompatible;
my @imunify_pkgs;
foreach my $pkg ( sort keys %$dropped_pkgs ) {
my $type = $dropped_pkgs->{$pkg} // '';
next if $type eq 'exp'; # use of experimental packages is a non blocker
Expand All @@ -1077,36 +1076,13 @@ EOS
my $php_pkg = $1;
next unless $self->_php_version_is_in_use($php_pkg);

if ( $self->_pkg_is_provided_by_imunify_360($php_pkg) ) {
push @imunify_pkgs, $pkg;
next;
}
}

if ( $pkg eq 'ea-profiles-cloudlinux' && $self->_pkg_is_provided_by_imunify_360($pkg) ) {
push @imunify_pkgs, $pkg;
next;
}

push @incompatible, $pkg;
}

if (@imunify_pkgs) {
Elevate::StageFile::remove_from_stage_file('ea4_imunify_packages');
Elevate::StageFile::update_stage_file( { ea4_imunify_packages => \@imunify_pkgs } );
}

return @incompatible;
}

sub _pkg_is_provided_by_imunify_360 ( $self, $pkg ) {
return 0 unless -x Elevate::Constants::IMUNIFY_AGENT;

my $version = Cpanel::Pkgr::get_package_version($pkg);

return $version =~ m/cloudlinux/ ? 1 : 0;
}

sub _php_version_is_in_use ( $self, $php ) {
my $current_php_usage = $self->_get_php_versions_in_use();

Expand Down Expand Up @@ -3291,7 +3267,6 @@ EOS

$self->run_once('_restore_ea4_profile');
$self->run_once('_restore_ea_addons');
$self->run_once('_restore_imunify_phps');

$self->run_once('_restore_config_files');

Expand Down Expand Up @@ -3398,16 +3373,6 @@ EOS
return;
}

sub _restore_imunify_phps ($self) {

my $php_pkgs = Elevate::StageFile::read_stage_file('ea4_imunify_packages');
return unless ref $php_pkgs eq 'ARRAY';
return unless scalar $php_pkgs->@*;

$self->dnf->install(@$php_pkgs);
return;
}

sub _ensure_sites_use_correct_php_version ($self) {

my $vhost_versions = Elevate::StageFile::read_stage_file('php_get_vhost_versions');
Expand Down Expand Up @@ -5825,6 +5790,7 @@ EOS

use File::Temp ();

use Elevate::Constants ();
use Elevate::OS ();
use Elevate::StageFile ();

Expand All @@ -5836,6 +5802,8 @@ EOS
# use Log::Log4perl qw(:easy);
INIT { Log::Log4perl->import(qw{:easy}); }

use constant IMUNIFY_AGENT => Elevate::Constants::IMUNIFY_AGENT;

sub backup ( $check_mode = 0 ) {
Elevate::EA4::_backup_ea4_profile($check_mode);
Elevate::EA4::_backup_ea_addons();
Expand Down Expand Up @@ -5868,10 +5836,37 @@ EOS
return;
}

sub _imunify360_is_installed_and_provides_hardened_php () {
return 0 unless -x IMUNIFY_AGENT;

my $out = Cpanel::SafeRun::Simple::saferunnoerror( IMUNIFY_AGENT, qw{version --json} );
my $license_data = eval { Cpanel::JSON::Load($out) } // {};

return 0 unless ref $license_data->{license};

if ( $license_data->{'license'}->{'license_type'} eq 'imunify360' ) {

my $output = Cpanel::SafeRun::Simple::saferunnoerror( IMUNIFY_AGENT, qw{features list} );
my @features = map {
my $trim_spaces = $_;
$trim_spaces =~ s/\s+//g;
$trim_spaces;
} grep { m/\S/ } split( "\n", $output );

foreach my $feature (@features) {
return 1 if $feature eq 'hardened-php';
}
}

return 0;
}

sub _get_ea4_profile ($check_mode) {

my $ea_alias = Elevate::OS::ea_alias();

$ea_alias = 'CloudLinux_8' if Elevate::EA4::_imunify360_is_installed_and_provides_hardened_php();

my @cmd = ( '/usr/local/bin/ea_current_to_profile', "--target-os=$ea_alias" );

my $profile_file;
Expand Down
26 changes: 0 additions & 26 deletions lib/Elevate/Blockers/EA4.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ sub _get_incompatible_packages ($self) {
return unless scalar keys $dropped_pkgs->%*;

my @incompatible;
my @imunify_pkgs;
foreach my $pkg ( sort keys %$dropped_pkgs ) {
my $type = $dropped_pkgs->{$pkg} // '';
next if $type eq 'exp'; # use of experimental packages is a non blocker
Expand All @@ -77,38 +76,13 @@ sub _get_incompatible_packages ($self) {
my $php_pkg = $1;
next unless $self->_php_version_is_in_use($php_pkg);

if ( $self->_pkg_is_provided_by_imunify_360($php_pkg) ) {
push @imunify_pkgs, $pkg;
next;
}
}

if ( $pkg eq 'ea-profiles-cloudlinux' && $self->_pkg_is_provided_by_imunify_360($pkg) ) {
push @imunify_pkgs, $pkg;
next;
}

push @incompatible, $pkg;
}

if (@imunify_pkgs) {
Elevate::StageFile::remove_from_stage_file('ea4_imunify_packages');
Elevate::StageFile::update_stage_file( { ea4_imunify_packages => \@imunify_pkgs } );
}

return @incompatible;
}

sub _pkg_is_provided_by_imunify_360 ( $self, $pkg ) {
return 0 unless -x Elevate::Constants::IMUNIFY_AGENT;

my $version = Cpanel::Pkgr::get_package_version($pkg);

# If the package is coming from CL, then we can assume
# that it is provided by Imunify 360 at this point
return $version =~ m/cloudlinux/ ? 1 : 0;
}

sub _php_version_is_in_use ( $self, $php ) {
my $current_php_usage = $self->_get_php_versions_in_use();

Expand Down
11 changes: 0 additions & 11 deletions lib/Elevate/Components/EA4.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ sub post_leapp ($self) {

$self->run_once('_restore_ea4_profile');
$self->run_once('_restore_ea_addons');
$self->run_once('_restore_imunify_phps');

# This needs to happen after EA4 has been reinstalled
#
Expand Down Expand Up @@ -155,16 +154,6 @@ sub _restore_config_files ($self) {
return;
}

sub _restore_imunify_phps ($self) {

my $php_pkgs = Elevate::StageFile::read_stage_file('ea4_imunify_packages');
return unless ref $php_pkgs eq 'ARRAY';
return unless scalar $php_pkgs->@*;

$self->dnf->install(@$php_pkgs);
return;
}

sub _ensure_sites_use_correct_php_version ($self) {

my $vhost_versions = Elevate::StageFile::read_stage_file('php_get_vhost_versions');
Expand Down
30 changes: 30 additions & 0 deletions lib/Elevate/EA4.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use cPstrict;

use File::Temp ();

use Elevate::Constants ();
use Elevate::OS ();
use Elevate::StageFile ();

Expand All @@ -24,6 +25,8 @@ use Cpanel::SafeRun::Simple ();

use Log::Log4perl qw(:easy);

use constant IMUNIFY_AGENT => Elevate::Constants::IMUNIFY_AGENT;

sub backup ( $check_mode = 0 ) {
Elevate::EA4::_backup_ea4_profile($check_mode);
Elevate::EA4::_backup_ea_addons();
Expand Down Expand Up @@ -57,10 +60,37 @@ sub _backup_ea4_profile ($check_mode) {
return;
}

sub _imunify360_is_installed_and_provides_hardened_php () {
return 0 unless -x IMUNIFY_AGENT;

my $out = Cpanel::SafeRun::Simple::saferunnoerror( IMUNIFY_AGENT, qw{version --json} );
my $license_data = eval { Cpanel::JSON::Load($out) } // {};

return 0 unless ref $license_data->{license};

if ( $license_data->{'license'}->{'license_type'} eq 'imunify360' ) {

my $output = Cpanel::SafeRun::Simple::saferunnoerror( IMUNIFY_AGENT, qw{features list} );
my @features = map {
my $trim_spaces = $_;
$trim_spaces =~ s/\s+//g;
$trim_spaces;
} grep { m/\S/ } split( "\n", $output );

foreach my $feature (@features) {
return 1 if $feature eq 'hardened-php';
}
}

return 0;
}

sub _get_ea4_profile ($check_mode) {

my $ea_alias = Elevate::OS::ea_alias();

$ea_alias = 'CloudLinux_8' if Elevate::EA4::_imunify360_is_installed_and_provides_hardened_php();

my @cmd = ( '/usr/local/bin/ea_current_to_profile', "--target-os=$ea_alias" );

my $profile_file;
Expand Down
52 changes: 2 additions & 50 deletions t/blocker-ea4.t
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ Please remove these packages before continuing the update.
}, "blocker with expected error" or diag explain $blocker;

$mock_ea4->redefine(
_php_version_is_in_use => 1,
_pkg_is_provided_by_imunify_360 => 0,
_php_version_is_in_use => 1,
);

$stage_ea4->{'dropped_pkgs'} = {
Expand Down Expand Up @@ -185,8 +184,7 @@ Please remove these packages before continuing the update.
or diag explain $blocker;

$mock_ea4->redefine(
_php_version_is_in_use => 0,
_pkg_is_provided_by_imunify_360 => 0,
_php_version_is_in_use => 0,
);

$stage_ea4->{'dropped_pkgs'} = {
Expand All @@ -196,52 +194,6 @@ Please remove these packages before continuing the update.
ok !$ea4->_blocker_ea4_profile(), 'No blocker when dropped package is an ea-php version that is not in use';
ea_info_check($target_os);

$mock_ea4->redefine(
_php_version_is_in_use => 0,
_pkg_is_provided_by_imunify_360 => 1,
);

ok !$ea4->_blocker_ea4_profile(), 'No blocker when dropped package is an ea-php version that is in use but provided by Imunify 360';
ea_info_check($target_os);

is(
$update_stage_file_data,
{},
'No ea-php packages need to be installed for Imunify 360 when the PHP version is not in use'
) or diag explain $update_stage_file_data;

$stage_ea4->{'dropped_pkgs'} = {
'ea-php42' => 'reg',
'ea-profiles-cloudlinux' => 'reg',
};

ok !$ea4->_blocker_ea4_profile(), 'No blocker when dropped package is "ea-profiles-cloudlinux" and provided by Imunify 360';
ea_info_check($target_os);

is(
$update_stage_file_data,
{
ea4_imunify_packages => ['ea-profiles-cloudlinux'],
},
'No blocker for ea-profiles-cloudlinux provided by Imunify 360'
);

$mock_ea4->redefine(
_php_version_is_in_use => 1,
_pkg_is_provided_by_imunify_360 => 1,
);

ok !$ea4->_blocker_ea4_profile(), 'No blocker when dropped package is an ea-php version that is in use but provided by Imunify 360';
ea_info_check($target_os);

is(
$update_stage_file_data,
{
ea4_imunify_packages => [ 'ea-php42', 'ea-profiles-cloudlinux' ],
},
'No ea-php packages need to be installed for Imunify 360 when the PHP version is not in use'
) or diag explain $update_stage_file_data;

$stage_ea4 = {};
}

Expand Down
Loading

0 comments on commit 9cf06b5

Please sign in to comment.