Skip to content

Commit

Permalink
Check port 22 on first ssh connection (#19755)
Browse files Browse the repository at this point in the history
Add some preliminary connectivity test before to attempt the first ssh
connection to the internal VM.
  • Loading branch information
mpagot authored Jul 17, 2024
1 parent 0296afd commit 5ab4b94
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
34 changes: 25 additions & 9 deletions lib/sles4sap/ipaddr2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -374,19 +374,37 @@ sub ipaddr2_internal_key_accept {

my $bastion_ssh_addr = ipaddr2_bastion_ssh_addr(bastion_ip => $args{bastion_ip});

my ($vm_name, $vm_addr);
my ($vm_name, $vm_addr, $ret, $start_time, $exit_code, $score);
foreach my $i (1 .. 2) {
$vm_name = ipaddr2_get_internal_vm_private_ip(id => $i);
$vm_addr = "$user\@$vm_name";

# The worker reach the remote internal VM through
# the bastion using ssh proxy mode.
# This workers - internal_VM connection is only used
# for test purpose, to observe from the external
# The worker reaches the two remote internal VMs
# through the bastion VM, using ssh proxy mode.
# The connection between the worker and the internalVM
# is used for test purpose, to observe from the external
# what is going on inside the SUT.
my $ret;

# Sometimes it fails, do not know why.
# Start by waiting that the ssh port is open
$start_time = time();
$exit_code = 1;
$score = 0;
while ((time() - $start_time) < 300) {
$exit_code = ipaddr2_ssh_bastion_script_run(
cmd => "nc -vz -w 1 $vm_name 22",
bastion_ip => $args{bastion_ip});
# sleep before to evaluate as, even if port is open,
# it could take more time to be able to extablish
# the first ssh connection.
sleep 10;

# this score mechanism panalize more those systems
# that are not ready when reaching this code.
$score += (defined($exit_code) && $exit_code eq 0) ? +1 : -1;
last if $score > 1;
}
die "ssh port 22 not available on VM $vm_name" if (!(defined($exit_code) && $exit_code eq 0));

# Try two different variants of the same command.
$ret = script_run(join(' ',
'ssh',
Expand All @@ -398,8 +416,6 @@ sub ipaddr2_internal_key_accept {
'whoami'));

if ($ret) {
record_info("1 StrictHostKeyChecking", "ret:$ret");

$ret = script_run(join(' ',
'ssh',
'-vvv',
Expand Down
26 changes: 24 additions & 2 deletions t/22_ipaddr2.t
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ subtest '[ipaddr2_internal_key_accept]' => sub {
my $ipaddr2 = Test::MockModule->new('sles4sap::ipaddr2', no_auto => 1);
my @calls;
$ipaddr2->redefine(assert_script_run => sub { push @calls, $_[0]; return; });
$ipaddr2->redefine(script_run => sub { push @calls, $_[0]; return; });
$ipaddr2->redefine(script_run => sub {
push @calls, $_[0];
if ($_[0] =~ /nc.*22/) { return 0; }
if ($_[0] =~ /ssh.*accept-new/) { return 0; }
return 1; });
$ipaddr2->redefine(ipaddr2_bastion_pubip => sub { return '1.2.3.4'; });
$ipaddr2->redefine(ipaddr2_bastion_ssh_addr => sub { return 'artom@1.2.3.4'; });
$ipaddr2->redefine(ipaddr2_bastion_ssh_addr => sub { return 'AlessandroArtom@1.2.3.4'; });

my $ret = ipaddr2_internal_key_accept();

Expand All @@ -111,6 +115,24 @@ subtest '[ipaddr2_internal_key_accept]' => sub {
ok((any { /0\.42/ } @calls), 'Internal VM2 IP in the ssh command');
};

subtest '[ipaddr2_internal_key_accept] nc timeout' => sub {
my $ipaddr2 = Test::MockModule->new('sles4sap::ipaddr2', no_auto => 1);
my @calls;
$ipaddr2->redefine(assert_script_run => sub { push @calls, $_[0]; return; });
$ipaddr2->redefine(script_run => sub {
push @calls, $_[0];
if ($_[0] =~ /nc.*22/) { return 1; }
if ($_[0] =~ /ssh.*accept-new/) { return 0; }
return 1; });
$ipaddr2->redefine(ipaddr2_bastion_pubip => sub { return '1.2.3.4'; });
$ipaddr2->redefine(ipaddr2_bastion_ssh_addr => sub { return '[email protected]'; });

dies_ok { ipaddr2_internal_key_accept() } "die if ssh port 22 is not open";

note("\n --> " . join("\n --> ", @calls));
ok((none { /StrictHostKeyChecking=accept-new/ } @calls), 'Correct call ssh command');
};

subtest '[ipaddr2_create_cluster]' => sub {
my $ipaddr2 = Test::MockModule->new('sles4sap::ipaddr2', no_auto => 1);
my @calls;
Expand Down

0 comments on commit 5ab4b94

Please sign in to comment.