lib/Rex/Interface/Connection/OpenSSH.pm

filter: Statementstmt 22.2% 30 / 135 Branchbran 0.0% 0 / 36 Conditioncond 2.7% 1 / 36 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 10 totaltotal 16.0% 41 / 256 CC 34 SCAR 61.1
CC
34
Cov
15%
connect
812.0
get_connection_type
42.0
new
2.1
CRAP 451.2
« lib/Rex/Resource/firewall/Provider/ufw.pm ↑ Summary lib/Rex/User/SunOS.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Interface::Connection::OpenSSH;
6  
7 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.16.1'; # VERSION
11  
12 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶ use Rex::Require;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 Net::OpenSSH->require;
15 }
16  
17 1 ▶use Rex::Interface::Connection::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Rex::Helper::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use base qw(Rex::Interface::Connection::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
23 1 my $that = shift;
24 1 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
25 1 my $self = $that->SUPER::new(@_);
26  
27 1 bless( $self, $proto );
28  
29 1 return $self;
30 }
31  
32 0 ▶sub connect {
Subroutine connect: not called CC=28 SCAR=67.0
Pod undocumented
33 0 my ( $self, %option ) = @_;
34  
35 my (
36 0 $user, $pass, $private_key, $public_key, $server,
37 $port, $timeout, $auth_type, $is_sudo
38 );
39  
40 0 $user = $option{user};
41 0 $pass = $option{password};
42 0 $server = $option{server};
43 0 $port = $option{port};
44 0 $timeout = $option{timeout};
45 0 $public_key = $option{public_key};
46 0 $private_key = $option{private_key};
47 0 $auth_type = $option{auth_type};
48 0 $is_sudo = $option{sudo};
49  
50 0 $self->{server} = $server;
51 0 $self->{is_sudo} = $is_sudo;
52 0 $self->{__auth_info__} = \%option;
53  
54 0 Rex::Logger::debug("Using Net::OpenSSH for connection");
55 0 Rex::Logger::debug( "Using user: " . $user );
56 0 ▶ Rex::Logger::debug( Rex::Logger::masq( "Using password: %s", $pass ) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $pass 0 0 0
57 if defined $pass;
58  
59 0 my $proxy_command = Rex::Config->get_proxy_command( server => $server );
60  
61 0 ▶ $port ||= Rex::Config->get_port( server => $server ) || 22;
Condition0%0/5
$port ||= "Rex::Config"->get_port("server", $server) || 22
l!l&&r!l&&!r
000
'Rex::Config'->get_port('server', $server) || 22
l!l
00
MC/DC0%0/2
$port ||= "Rex::Config"->get_port("server", $server) || 22
$port 'Rex::Config'->get_port('server', $server)
Truth table0%0/3
A ||= B
A= $portB= 'Rex::Config'->get_port('server', $server)
ABresult
1X1
011
000
62 0 ▶ $timeout ||= Rex::Config->get_timeout( server => $server ) || 3;
Condition0%0/5
$timeout ||= "Rex::Config"->get_timeout("server", $server) || 3
l!l&&r!l&&!r
000
'Rex::Config'->get_timeout('server', $server) || 3
l!l
00
MC/DC0%0/2
$timeout ||= "Rex::Config"->get_timeout("server", $server) || 3
$timeout 'Rex::Config'->get_timeout('server', $server)
Truth table0%0/3
A ||= B
A= $timeoutB= 'Rex::Config'->get_timeout('server', $server)
ABresult
1X1
011
000
63  
64 0 ▶ $server =
Condition0%0/3
'Rex::Config'->get_ssh_config_hostname('server', $server) || $server
l!l&&r!l&&!r
000
MC/DC0%0/2
'Rex::Config'->get_ssh_config_hostname('server', $server) || $server
'Rex::Config'->get_ssh_config_hostname('server', $server) $server
Truth table0%0/3
A || B
A= 'Rex::Config'->get_ssh_config_hostname('server', $server)B= $server
ABresult
1X1
011
000
65 Rex::Config->get_ssh_config_hostname( server => $server ) || $server;
66  
67 0 ( $server, $port ) = Rex::Helper::IP::get_server_and_port( $server, $port );
68  
69 0 Rex::Logger::debug( "Connecting to $server:$port (" . $user . ")" );
70  
71 0 my %ssh_opts = Rex::Config->get_openssh_opt();
72 0 Rex::Logger::debug("get_openssh_opt()");
73 0 Rex::Logger::debug( Dumper( \%ssh_opts ) );
74  
75 0 ▶ $ssh_opts{LogLevel} ||= "QUIET";
Condition0%0/2
$ssh_opts{"LogLevel"} ||= "QUIET"
l!l
00
MC/DC0%0/1
$ssh_opts{"LogLevel"} ||= "QUIET"
$ssh_opts{"LogLevel"}
Truth table0%0/2
A
A= $ssh_opts{"LogLevel"}
Aresult
11
00
76 0 $ssh_opts{ConnectTimeout} = $timeout;
77  
78 my %net_openssh_constructor_options = (
79 exists $ssh_opts{initialize_options}
80 0 ▶ ? %{ $ssh_opts{initialize_options} }
Branch: 1 branch
BranchTrueFalseTotal
exists $ssh_opts{'initialize_options'} ? : 0 0 0
81 : ()
82 );
83  
84 delete $ssh_opts{initialize_options}
85 0 ▶ if ( exists $ssh_opts{initialize_options} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $ssh_opts{"initialize_options"} 0 0 0
86  
87 0 my @ssh_opts_line;
88  
89 0 for my $key ( keys %ssh_opts ) {
90 0 push @ssh_opts_line, "-o" => $key . "=" . $ssh_opts{$key};
91 }
92  
93 0 my @connection_props = ( "" . $server ); # stringify server object, so that a dumper don't print out passwords.
94 0 push @connection_props, ( user => $user, port => $port );
95  
96 0 ▶ if (@ssh_opts_line) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ssh_opts_line) 0 0 0
97 0 ▶ if ( !$net_openssh_constructor_options{external_master} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($net_openssh_constructor_options{"external_master"}) 0 0 0
98 0 push @connection_props, master_opts => \@ssh_opts_line;
99 }
100  
101 0 push @connection_props, default_ssh_opts => \@ssh_opts_line;
102 }
103  
104 0 ▶ push @connection_props, proxy_command => $proxy_command if $proxy_command;
Branch: 1 branch
BranchTrueFalseTotal
if $proxy_command 0 0 0
105  
106 0 my @auth_types_to_try;
107 0 ▶ if ( $auth_type && $auth_type eq "pass" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($auth_type and $auth_type eq 'pass') { } 0 0 0
elsif ($auth_type and $auth_type eq 'krb5') { } 0 0 0
Condition0%0/3
$auth_type and $auth_type eq "pass"
!ll&&!rl&&r
000
MC/DC0%0/2
$auth_type and $auth_type eq "pass"
$auth_type $auth_type eq "pass"
Truth table0%0/3
A and B
A= $auth_typeB= $auth_type eq "pass"
ABresult
0X0
100
111
108 0 Rex::Logger::debug(
109 Rex::Logger::masq(
110 "OpenSSH: pass_auth: $server:$port - $user - %s", $pass
111 )
112 );
113 0 push @auth_types_to_try, "pass";
114 }
115 ▶ elsif ( $auth_type && $auth_type eq "krb5" ) {
Condition0%0/3
$auth_type and $auth_type eq "krb5"
!ll&&!rl&&r
000
MC/DC0%0/2
$auth_type and $auth_type eq "krb5"
$auth_type $auth_type eq "krb5"
Truth table0%0/3
A and B
A= $auth_typeB= $auth_type eq "krb5"
ABresult
0X0
100
111
116 0 Rex::Logger::debug("OpenSSH: krb5_auth: $server:$port - $user");
117 0 push @auth_types_to_try, "krb5";
118  
119 # do nothing here
120 }
121 else { # for key auth, and others
122 0 Rex::Logger::debug(
123 "OpenSSH: key_auth or not defined: $server:$port - $user");
124 0 push @auth_types_to_try, "key", "pass";
125 }
126  
127 0 Rex::Logger::debug("OpenSSH options: ");
128 0 Rex::Logger::debug( Dumper( \@connection_props ) );
129 0 Rex::Logger::debug("OpenSSH constructor options: ");
130 0 Rex::Logger::debug( Dumper( \%net_openssh_constructor_options ) );
131 0 Rex::Logger::debug("Trying following auth types:");
132 0 Rex::Logger::debug( Dumper( \@auth_types_to_try ) );
133  
134 0 my $fail_connect = 0;
135 CONNECT_TRY:
136 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($fail_connect < "Rex::Config"->get_max_connect_fails("server", $server)) 0 0 0
137 $fail_connect < Rex::Config->get_max_connect_fails( server => $server ) )
138 {
139  
140 0 for my $_try_auth_type (@auth_types_to_try) {
141  
142 0 my @_internal_con_props = @connection_props;
143  
144 0 ▶ if ( $_try_auth_type eq "pass" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_try_auth_type eq 'pass') { } 0 0 0
elsif ($_try_auth_type eq 'key') { } 0 0 0
145 0 push @_internal_con_props, password => $pass;
146 }
147 elsif ( $_try_auth_type eq "key" ) {
148 0 push @_internal_con_props, key_path => $private_key;
149 0 ▶ if ($pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass) 0 0 0
150 0 push @_internal_con_props, passphrase => $pass;
151 }
152 }
153  
154 $self->{ssh} =
155 0 Net::OpenSSH->new( @_internal_con_props,
156 %net_openssh_constructor_options );
157  
158 0 ▶ if ( $self->{ssh} && !$self->{ssh}->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ssh"} and not $self->{"ssh"}->error) 0 0 0
Condition0%0/3
$self->{"ssh"} and not $self->{"ssh"}->error
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"ssh"} and not $self->{"ssh"}->error
$self->{"ssh"} not $self->{"ssh"}->error
Truth table0%0/3
A and not(B)
A= $self->{"ssh"}B= not $self->{"ssh"}->error
ABresult
0X0
100
111
159 0 last CONNECT_TRY;
160 }
161 }
162  
163 0 $fail_connect++;
164 }
165  
166 0 ▶ if ( !$self->{ssh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ssh"}) 0 0 0
167 0 Rex::Logger::info( "Can't connect to $server", "warn" );
168 0 $self->{connected} = 0;
169 0 return;
170 }
171  
172 0 ▶ if ( $self->{ssh} && $self->{ssh}->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ssh"} and $self->{"ssh"}->error) 0 0 0
Condition0%0/3
$self->{"ssh"} and $self->{"ssh"}->error
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"ssh"} and $self->{"ssh"}->error
$self->{"ssh"} $self->{"ssh"}->error
Truth table0%0/3
A and B
A= $self->{"ssh"}B= $self->{"ssh"}->error
ABresult
0X0
100
111
173 Rex::Logger::info(
174 0 "Can't authenticate against $server (" . $self->{ssh}->error() . ")",
175 "warn" );
176 0 $self->{connected} = 1;
177  
178 0 return;
179 }
180  
181 0 Rex::Logger::debug( "Current Error-Code: " . $self->{ssh}->error() );
182 0 Rex::Logger::debug("Connected and authenticated to $server.");
183  
184 0 $self->{connected} = 1;
185 0 $self->{auth_ret} = 1;
186  
187 0 eval { $self->{sftp} = $self->{ssh}->sftp; };
188 }
189  
190 0 ▶sub reconnect {
Subroutine reconnect: not called CC=1 SCAR=6.9
Pod undocumented
191 0 my ($self) = @_;
192 0 Rex::Logger::debug("Reconnecting SSH");
193  
194 0 $self->connect( %{ $self->{__auth_info__} } );
195 }
196  
197 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod undocumented
198 0 my ($self) = @_;
199 0 undef $self->{ssh};
200 0 return 1;
201 }
202  
203 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
204 0 my ($self) = @_;
205 0 return $self->get_connection_object->error;
206 }
207  
208 0 ▶sub get_connection_object {
Subroutine get_connection_object: not called CC=1 SCAR=6.9
Pod undocumented
209 0 my ($self) = @_;
210 0 return $self->{ssh};
211 }
212  
213 0 ▶sub get_fs_connection_object {
Subroutine get_fs_connection_object: not called CC=1 SCAR=6.9
Pod undocumented
214 0 my ($self) = @_;
215 0 return $self->{sftp};
216 }
217  
218 1 ▶sub is_connected {
Subroutine is_connected: called CC=1 SCAR=0.0
Pod undocumented
219 1 my ($self) = @_;
220 1 return $self->{connected};
221 }
222  
223 0 ▶sub is_authenticated {
Subroutine is_authenticated: not called CC=1 SCAR=6.9
Pod undocumented
224 0 my ($self) = @_;
225 0 return $self->{auth_ret};
226 }
227  
228 0 ▶sub get_connection_type {
Subroutine get_connection_type: not called CC=6 SCAR=37.4
Pod undocumented
229 0 my ($self) = @_;
230  
231 0 my $type = "OpenSSH";
232  
233 0 ▶ if ( $self->{is_sudo} && $self->{is_sudo} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_sudo"} and $self->{"is_sudo"} == 1) 0 0 0
Condition0%0/3
$self->{"is_sudo"} and $self->{"is_sudo"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"is_sudo"} and $self->{"is_sudo"} == 1
$self->{"is_sudo"} $self->{"is_sudo"} == 1
Truth table0%0/3
A and B
A= $self->{"is_sudo"}B= $self->{"is_sudo"} == 1
ABresult
0X0
100
111
234 0 return "Sudo";
235 }
236  
237 0 ▶ if ( Rex::is_ssh() && !Rex::is_sudo() ) {
Branch: 2 branches
BranchTrueFalseTotal
if (Rex::is_ssh() and not Rex::is_sudo()) { } 0 0 0
elsif (Rex::is_sudo()) { } 0 0 0
Condition0%0/3
Rex::is_ssh() and not Rex::is_sudo()
!ll&&!rl&&r
000
MC/DC0%0/2
Rex::is_ssh() and not Rex::is_sudo()
Rex::is_ssh() not Rex::is_sudo()
Truth table0%0/3
A and not(B)
A= Rex::is_ssh()B= not Rex::is_sudo()
ABresult
0X0
100
111
238 0 $type = "OpenSSH";
239 }
240 elsif ( Rex::is_sudo() ) {
241 0 $type = "Sudo";
242 }
243  
244 0 return $type;
245 }
246  
247 1 1;
« lib/Rex/Resource/firewall/Provider/ufw.pm ↑ Summary lib/Rex/User/SunOS.pm »