File Coverage

blib/lib/Sys/Async/Virt/Connection/SSH.pm
Criterion Covered Total %
statement 26 67 38.8
branch 0 20 0.0
condition 0 12 0.0
subroutine 9 14 64.2
pod 2 4 50.0
total 37 117 31.6


line stmt bran cond sub pod time code
1             ####################################################################
2             #
3             # This file was generated using XDR::Parse version v1.0.1
4             # and LibVirt version v12.1.0
5             #
6             # Don't edit this file, use the source template instead
7             #
8             # ANY CHANGES HERE WILL BE LOST !
9             #
10             ####################################################################
11              
12              
13 1     1   1156 use v5.26;
  1         4  
14 1     1   4 use warnings;
  1         2  
  1         45  
15 1     1   4 use experimental 'signatures';
  1         1  
  1         6  
16 1     1   113 use Future::AsyncAwait;
  1         2  
  1         7  
17 1     1   62 use Object::Pad ':experimental(inherit_field)';
  1         1  
  1         6  
18              
19             class Sys::Async::Virt::Connection::SSH v0.6.1;
20              
21             inherit Sys::Async::Virt::Connection::Process;
22              
23 1     1   298 use Carp qw(croak);
  1         2  
  1         47  
24 1     1   3 use Log::Any qw($log);
  1         2  
  1         7  
25              
26 1     1   611 use Protocol::Sys::Virt::UNIXSocket v12.1.0; # imports socket_path
  1         643  
  1         64  
27 1     1   6 use Protocol::Sys::Virt::URI v12.1.0; # imports parse_url
  1         9  
  1         1357  
28              
29             field $_socket :reader :param = undef;
30 0     0 0   field $_readonly :reader :param;
  0     0 1    
  0            
  0            
31              
32 0     0 0   sub shell_escape($val) {
  0            
  0            
33 0 0         if ($val !~ m/[\s!"'`$<>#&*?;\\\[\]{}()~|^]/) { # no shell chars
34 0           return $val;
35             }
36              
37 0           return (q|'| . ($val =~ s/'/'\\''/gr) . q|'|);
38             }
39              
40             my $nc_proxy =
41             q{if %1$s -q 2>&1 | grep "requires an argument" >/dev/null 2>&1; } .
42             q{then A=-q0; } .
43             q{else A=; } .
44             q{fi; } .
45             q{%1$s $A -U %2$s};
46              
47             my $native_proxy =
48             q{virt-ssh-helper %s};
49              
50             my $auto_proxy =
51             q{if which virt-ssh-helper >/dev/null 2>&1; } .
52             q{then %s; } .
53             q{else %s; } .
54             q{fi};
55              
56 0     0     method _command( $url ) {
  0            
  0            
  0            
57 0           my %c = parse_url( $url );
58 0           my @args = ('-e', 'none');
59 0 0         push @args, ('-p', $c{port}) if $c{port};
60 0 0         push @args, ('-l', $c{username}) if $c{username};
61 0 0         push @args, ('-i', $c{query}->{keyfile}) if $c{query}->{keyfile};
62 0 0         push @args, ('-o', 'StrictHostKeyChecking=no') if $c{query}->{no_verify};
63 0 0         push @args, ('-T') if $c{query}->{no_tty};
64              
65 0           my $remote_cmd;
66 0   0       my $proxy_mode = $c{query}->{proxy} // 'auto';
67             my $socket_path = $_socket // $c{query}->{socket} //
68             socket_path(readonly => $_readonly,
69             hypervisor => $c{hypervisor},
70             mode => $c{query}->{mode},
71 0   0       type => $c{type});
      0        
72              
73             my $nc_command = sprintf($nc_proxy,
74 0   0       $c{query}->{netcat} // 'nc',
75             $socket_path);
76 0           my $native_command = 'virt-ssh-helper ';
77 0 0         $native_command .= '-r ' if $_readonly;
78              
79             # $c{proxy} is the URL to use on the proxy
80 0           $native_command .= shell_escape($c{proxy});
81 0 0         if ($proxy_mode eq 'netcat') {
    0          
    0          
82 0           $remote_cmd = sprintf(q|sh -c %s|, shell_escape($nc_command));
83             }
84             elsif ($proxy_mode eq 'native') {
85 0           $remote_cmd = $native_command;
86             }
87             elsif ($proxy_mode eq 'auto') {
88 0           $remote_cmd = sprintf(q|sh -c %s|,
89             shell_escape(sprintf($auto_proxy,
90             $native_command,
91             $nc_command)));
92             }
93             else {
94 0           croak $log->fatal( "Unknown proxy mode '$proxy_mode'" );
95             }
96              
97 0           $log->trace("SSH remote command: $remote_cmd");
98              
99 0   0       my $local_cmd = $c{query}->{command} // 'ssh';
100 0           my @cmd = ($local_cmd, @args, '--', $c{host}, $remote_cmd);
101              
102 0           return @cmd;
103             }
104              
105 0     0 1   method is_secure() {
  0            
  0            
106 0           return 1;
107             }
108              
109             1;
110              
111              
112             __END__