blib/lib/Rex/Interface/Connection/LibSSH.pm

filter: Statementstmt 25.4% 26 / 102 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 44.4% 8 / 18 Podpod 0.0% 0 / 10 totaltotal 14.2% 34 / 239 CC 36 SCAR 63.7
CC
36
Cov
14%
connect
992.0
disconnect
6.0
error
6.0
CRAP 581.7
↑ Summary lib/Rex/Interface/Exec/LibSSH.pm »
1 # ABSTRACT: Rex SSH connection via Net::LibSSH (no SFTP required)
2  
3 package Rex::Interface::Connection::LibSSH;
4 1 our $VERSION = '0.005';
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Net::LibSSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Rex::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Rex::Helper::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Rex::Interface::Connection::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use base qw(Rex::Interface::Connection::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
16 0 my ( $that, %args ) = @_;
17 0 my $self = $that->SUPER::new(%args);
18 0 return $self;
19 }
20  
21 0 ▶sub connect {
Subroutine connect: not called CC=31 SCAR=69.0
Pod undocumented
22 0 my ( $self, %opt ) = @_;
23  
24 0 my $server = $opt{server};
25 0 ▶ my $port = $opt{port} || Rex::Config->get_port( server => $server ) || 22;
Condition0%0/5
$opt{'port'} || 'Rex::Config'->get_port('server', $server) || 22
l!l
00
$opt{"port"} or "Rex::Config"->get_port("server", $server)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'port'} || 'Rex::Config'->get_port('server', $server) || 22
$opt{"port"} "Rex::Config"->get_port("server", $server)
Truth table0%0/3
A or B
A= $opt{"port"}B= "Rex::Config"->get_port("server", $server)
ABresult
1X1
011
000
26 0 ▶ my $timeout = $opt{timeout} || Rex::Config->get_timeout( server => $server ) || 10;
Condition0%0/5
$opt{'timeout'} || 'Rex::Config'->get_timeout('server', $server) || 10
l!l
00
$opt{"timeout"} or "Rex::Config"->get_timeout("server", $server)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'timeout'} || 'Rex::Config'->get_timeout('server', $server) || 10
$opt{"timeout"} "Rex::Config"->get_timeout("server", $server)
Truth table0%0/3
A or B
A= $opt{"timeout"}B= "Rex::Config"->get_timeout("server", $server)
ABresult
1X1
011
000
27 0 my $user = $opt{user};
28 0 my $password = $opt{password};
29 0 my $privkey = $opt{private_key};
30 0 ▶ my $auth = $opt{auth_type} // 'key';
Condition0%0/2
$opt{"auth_type"} // "key"
l!l
00
MC/DC0%0/1
$opt{"auth_type"} // "key"
$opt{"auth_type"}
Truth table0%0/2
A
A= $opt{"auth_type"}
Aresult
11
00
31  
32 0 $self->{server} = $server;
33 0 $self->{is_sudo} = $opt{sudo};
34 0 $self->{__auth_info__} = \%opt;
35  
36 0 ( $server, $port ) = Rex::Helper::IP::get_server_and_port( $server, $port );
37  
38 # Host key verification: a per-connect option wins, otherwise the same
39 # openssh_opt knobs the OpenSSH backend honours -- StrictHostKeyChecking
40 # (what -feature => ['disable_strict_host_key_checking'] sets to "no")
41 # and UserKnownHostsFile. Strict unless explicitly turned off.
42 0 my %openssh_opt = Rex::Config->get_openssh_opt();
43 0 my $strict = $opt{strict_hostkeycheck};
44 0 ▶ unless ( defined $strict ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $strict) 0 0 0
45 0 ▶ my $shkc = $openssh_opt{StrictHostKeyChecking} // '';
Condition0%0/2
$openssh_opt{"StrictHostKeyChecking"} // ""
l!l
00
MC/DC0%0/1
$openssh_opt{"StrictHostKeyChecking"} // ""
$openssh_opt{"StrictHostKeyChecking"}
Truth table0%0/2
A
A= $openssh_opt{"StrictHostKeyChecking"}
Aresult
11
00
46 0 ▶ $strict = $shkc =~ /^(?:no|off)$/i ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$shkc =~ /^(?:no|off)$/i ? : 0 0 0
47 }
48 0 ▶ my $knownhosts = $opt{knownhosts} // $openssh_opt{UserKnownHostsFile};
Condition0%0/3
$opt{"knownhosts"} // $openssh_opt{"UserKnownHostsFile"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"knownhosts"} // $openssh_opt{"UserKnownHostsFile"}
$opt{"knownhosts"} $openssh_opt{"UserKnownHostsFile"}
Truth table0%0/3
A // B
A= $opt{"knownhosts"}B= $openssh_opt{"UserKnownHostsFile"}
ABresult
1X1
011
000
49  
50 0 Rex::Logger::debug("LibSSH: connecting to $server:$port as $user");
51  
52 0 my $ssh = Net::LibSSH->new;
53 0 $ssh->option( host => "$server" );
54 0 $ssh->option( port => $port );
55 0 ▶ $ssh->option( user => $user ) if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 0 0 0
56 0 $ssh->option( timeout => $timeout );
57 0 ▶ $ssh->option( strict_hostkeycheck => $strict ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$strict ? : 0 0 0
58 0 ▶ $ssh->option( knownhosts => $knownhosts ) if defined $knownhosts;
Branch: 1 branch
BranchTrueFalseTotal
if defined $knownhosts 0 0 0
59  
60 0 ▶ unless ( $ssh->connect ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssh->connect) 0 0 0
61 # The hint only for a host key refusal -- Net::LibSSH names
62 # known_hosts in each of them. A refused or unreachable port (a host
63 # still rebooting) has nothing to do with host key checking.
64 0 ▶ my $error = $ssh->error // '';
Condition0%0/2
$ssh->error // ""
l!l
00
MC/DC0%0/1
$ssh->error // ""
$ssh->error
Truth table0%0/2
A
A= $ssh->error
Aresult
11
00
65 0 ▶ Rex::Logger::info( "LibSSH: can't connect to $server: " . $error
Branch: 1 branch
BranchTrueFalseTotal
$strict && $error =~ /known_hosts/ ? : 0 0 0
Condition0%0/3
$strict and $error =~ /known_hosts/
!ll&&!rl&&r
000
MC/DC0%0/2
$strict and $error =~ /known_hosts/
$strict $error =~ /known_hosts/
Truth table0%0/3
A and B
A= $strictB= $error =~ /known_hosts/
ABresult
0X0
100
111
66 . ( $strict && $error =~ /known_hosts/
67 ? " -- host key checking is on: add the host to known_hosts (ssh-keyscan)"
68 . " or opt out with strict_hostkeycheck => 0 / use Rex -feature => ['disable_strict_host_key_checking']"
69 : '' ), 'warn' );
70 0 $self->{error} = $ssh->error;
71 0 $self->{connected} = 0;
72 0 $self->{auth_ret} = 0;
73 0 return;
74 }
75  
76 # Reset timeout to 0 (infinite) after connect — the connect timeout must
77 # be short so unreachable hosts fail fast, but channel reads (e.g. apt-get
78 # with DKMS build) must not time out. libssh's timeout option affects both.
79 0 $ssh->option( timeout => 0 );
80  
81 0 $self->{connected} = 1;
82  
83 # Try authentication in order
84 0 my $authed = 0;
85  
86 0 ▶ if ( $auth eq 'pass' && defined $password ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($auth eq 'pass' and defined $password) { } 0 0 0
elsif ($auth eq 'key' and defined $privkey) { } 0 0 0
Condition0%0/3
$auth eq "pass" and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
$auth eq "pass" and defined $password
$auth eq "pass" defined $password
Truth table0%0/3
A and B
A= $auth eq "pass"B= defined $password
ABresult
0X0
100
111
87 0 $authed = $ssh->auth_password($password);
88 }
89 ▶ elsif ( $auth eq 'key' && defined $privkey ) {
Condition0%0/3
$auth eq "key" and defined $privkey
!ll&&!rl&&r
000
MC/DC0%0/2
$auth eq "key" and defined $privkey
$auth eq "key" defined $privkey
Truth table0%0/3
A and B
A= $auth eq "key"B= defined $privkey
ABresult
0X0
100
111
90 0 $authed = $ssh->auth_publickey($privkey);
91 0 ▶ $authed ||= $ssh->auth_agent if !$authed;
Branch: 1 branch
BranchTrueFalseTotal
unless $authed 0 0 0
Condition0%0/3
$authed ||= $ssh->auth_agent
l!l&&r!l&&!r
000
MC/DC0%0/2
$authed ||= $ssh->auth_agent
$authed $ssh->auth_agent
Truth table0%0/3
A ||= B
A= $authedB= $ssh->auth_agent
ABresult
1X1
011
000
92 }
93 else {
94 # auto: agent first, then key file, then password
95 0 $authed = $ssh->auth_agent;
96 0 ▶ $authed ||= $ssh->auth_publickey($privkey) if !$authed && $privkey;
Branch: 1 branch
BranchTrueFalseTotal
if not $authed and $privkey 0 0 0
Condition0%0/6
not $authed and $privkey
!ll&&!rl&&r
000
$authed ||= $ssh->auth_publickey($privkey)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $authed and $privkey
not $authed $privkey
MC/DC0%0/2
$authed ||= $ssh->auth_publickey($privkey)
$authed $ssh->auth_publickey($privkey)
Truth table0%0/3
not(A) and B
A= not $authedB= $privkey
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $authedB= $ssh->auth_publickey($privkey)
ABresult
1X1
011
000
97 0 ▶ $authed ||= $ssh->auth_password($password) if !$authed && $password;
Branch: 1 branch
BranchTrueFalseTotal
if not $authed and $password 0 0 0
Condition0%0/6
not $authed and $password
!ll&&!rl&&r
000
$authed ||= $ssh->auth_password($password)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $authed and $password
not $authed $password
MC/DC0%0/2
$authed ||= $ssh->auth_password($password)
$authed $ssh->auth_password($password)
Truth table0%0/3
not(A) and B
A= not $authedB= $password
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $authedB= $ssh->auth_password($password)
ABresult
1X1
011
000
98 }
99  
100 0 ▶ unless ($authed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($authed) 0 0 0
101 0 ▶ Rex::Logger::info( "LibSSH: authentication failed for $user\@$server: "
Condition0%0/2
$ssh->error // ""
l!l
00
MC/DC0%0/1
$ssh->error // ""
$ssh->error
Truth table0%0/2
A
A= $ssh->error
Aresult
11
00
102 . ( $ssh->error // '' ), 'warn' );
103 0 $self->{error} = $ssh->error;
104 0 $self->{auth_ret} = 0;
105 0 return;
106 }
107  
108 0 Rex::Logger::debug("LibSSH: authenticated $user\@$server");
109 0 delete $self->{error};
110 0 $self->{ssh} = $ssh;
111 0 $self->{auth_ret} = 1;
112 }
113  
114 0 ▶sub reconnect {
Subroutine reconnect: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my ($self) = @_;
116 0 Rex::Logger::debug("LibSSH: reconnecting");
117 0 $self->connect( %{ $self->{__auth_info__} } );
118 }
119  
120 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod undocumented
121 0 my ($self) = @_;
122 0 ▶ if ( $self->{ssh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ssh"}) 0 0 0
123 0 $self->{ssh}->disconnect;
124 0 undef $self->{ssh};
125 }
126 0 return 1;
127 }
128  
129 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
130 0 my ($self) = @_;
131 0 ▶ return $self->{ssh} ? $self->{ssh}->error : $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ssh'} ? : 0 0 0
132 }
133  
134 0 ▶sub get_connection_object {
Subroutine get_connection_object: not called CC=1 SCAR=6.9
Pod undocumented
135 0 my ($self) = @_;
136 0 return $self->{ssh};
137 }
138  
139 0 ▶sub get_fs_connection_object {
Subroutine get_fs_connection_object: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my ($self) = @_;
141 0 return $self; # exec-based Fs needs no separate SFTP object
142 }
143  
144 0 ▶sub is_connected {
Subroutine is_connected: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my ($self) = @_;
146 0 ▶ return $self->{connected} // 0;
Condition0%0/2
$self->{"connected"} // 0
l!l
00
MC/DC0%0/1
$self->{"connected"} // 0
$self->{"connected"}
Truth table0%0/2
A
A= $self->{"connected"}
Aresult
11
00
147 }
148  
149 0 ▶sub is_authenticated {
Subroutine is_authenticated: not called CC=2 SCAR=17.9
Pod undocumented
150 0 my ($self) = @_;
151 0 ▶ return $self->{auth_ret} // 0;
Condition0%0/2
$self->{"auth_ret"} // 0
l!l
00
MC/DC0%0/1
$self->{"auth_ret"} // 0
$self->{"auth_ret"}
Truth table0%0/2
A
A= $self->{"auth_ret"}
Aresult
11
00
152 }
153  
154 0 ▶sub get_connection_type {
Subroutine get_connection_type: not called CC=2 SCAR=17.9
Pod undocumented
155 0 my ($self) = @_;
156 0 ▶ return Rex::is_sudo() ? 'Sudo' : 'LibSSH';
Branch: 1 branch
BranchTrueFalseTotal
Rex::is_sudo() ? : 0 0 0
157 }
158  
159 1 1;
160  
161 __END__
↑ Summary lib/Rex/Interface/Exec/LibSSH.pm »