Skip to content

Commit

Permalink
[PoC] Use public key authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
foursixnine committed Mar 18, 2019
1 parent 1d30c86 commit 0c5b2e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
40 changes: 30 additions & 10 deletions backend/baseclass.pm
Expand Up @@ -1171,24 +1171,44 @@ sub new_ssh_connection {
my ($self, %args) = @_;
$args{username} ||= 'root';

my $ssh = Net::SSH2->new(debug => get_var('DEBUG_SSH_CONNECTION', 0));

my $ssh = Net::SSH2->new(
debug => get_var('DEBUG_SSH_CONNECTION', 0),
);
bmwqemu::debug_call(pp(\%args));
my $privatekey_path = '/home/foursixnine/.openqa';
my $publickey_path = '/home/foursixnine/.openqa.pub';
# Retry 5 times, in case of the guest is not running yet
my $counter = 5;
while ($counter--) {
if ($ssh->connect($args{hostname})) {
# Attempt always key authentication
$ssh->auth_agent($args{username});
if (!$ssh->auth_ok && $args{password}) {
$ssh->auth(username => $args{username}, password => $args{password});
}
bmwqemu::diag "Connection to $args{username}\@$args{hostname} established" if $ssh->auth_ok;
sleep(5);
eval {
$ssh->auth(
username => $args{username},
password => $args{password},
privatekey => $privatekey_path,
publickey => $publickey_path,
passphrase => undef
);
};

carp "It seems that there's a problem with public key auth" . $@ if $@;

$ssh->auth(
username => $args{username},
password => $args{password},
) unless $ssh->auth_ok;

bmwqemu::diag "Could not connect to $args{username}\@$args{hostname}: error - " . $ssh->error unless $ssh->auth_ok;
bmwqemu::diag "Connection stablished $args{username}\@$args{hostname} " if $ssh->auth_ok;

last if $ssh->auth_ok;
sleep(10);
}

else {
bmwqemu::diag "Could not connect to $args{username}\@$args{hostname}, Retry";
sleep(10);
bmwqemu::diag "Could not connect to $args{username}\@$args{hostname}, Retry ($counter)";
sleep(20);
next;
}
}
Expand Down
1 change: 1 addition & 0 deletions consoles/sshVirtsh.pm
Expand Up @@ -30,6 +30,7 @@ use File::Temp 'tempfile';
use File::Basename;
use Class::Accessor 'antlers';
use Data::Dump qw(pp);
use Carp qw(croak);

has instance => (is => "rw", isa => "Num");
has name => (is => "rw", isa => "Str");
Expand Down

0 comments on commit 0c5b2e3

Please sign in to comment.