Remote/Connector/LocalSudo.pm

filter: Statementstmt 20.9% 18 / 86 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 31.5% 6 / 19 Podpod n/a totaltotal 15.7% 24 / 152 CC 23 SCAR 37.7
CC
23
Cov
14%
_start_perl
90.0
_start_perl
90.0
__ANON__
30.0
CRAP 43.3
« Remote/Connector/STDIO.pm ↑ Summary Remote/Prompt.pm »
1 package Object::Remote::Connector::LocalSudo;
2  
3 11 ▶use Object::Remote::Logging qw (:log :dlog);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 extends 'Object::Remote::Connector::Local';
10  
11 has target_user => (is => 'ro', required => 1);
12  
13 has password_callback => (is => 'lazy');
14  
15 0 ▶sub _build_password_callback {
Subroutine _build_password_callback: not called CC=1 SCAR=6.9
16 0 ▶ my ($self) = @_;
Subroutine _build_password_callback: not called CC=1 SCAR=6.9
17 0 my $pw_prompt = use_module('Object::Remote::Prompt')->can('prompt_pw');
18 0 my $user = $self->target_user;
19 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 0 ▶ $pw_prompt->("sudo password for ${user}", undef, { cache => 1 })
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 }
22 0 }
23  
24 has sudo_perl_command => (is => 'lazy');
25  
26 0 ▶sub _build_sudo_perl_command {
Subroutine _build_sudo_perl_command: not called CC=1 SCAR=6.9
27 0 ▶ my ($self) = @_;
Subroutine _build_sudo_perl_command: not called CC=1 SCAR=6.9
28 return [
29 'sudo', '-S', '-u', $self->target_user, '-p', "[sudo] password please\n",
30 'perl', '-MPOSIX=dup2',
31 '-e', 'print STDERR "GO\n"; exec(@ARGV);',
32 0 @{$self->perl_command},
33 ];
34 }
35  
36 0 ▶sub _start_perl {
Subroutine _start_perl: not called CC=9 SCAR=45.0
37 0 ▶ my $self = shift;
Subroutine _start_perl: not called CC=9 SCAR=45.0
38 0 my $sudo_stderr = gensym;
39 my $pid = open3(
40 my $foreign_stdin,
41 my $foreign_stdout,
42 $sudo_stderr,
43 0 ▶ @{$self->sudo_perl_command}
Branch: 2 branches
BranchTrueFalseTotal
my $pid = open3(my $foreign_stdin, my $foreign_stdout, $sudo_stderr, @{$self->sudo_perl_command;}) or die "open3 failed: $!" 0 0 0
my $pid = open3(my $foreign_stdin, my $foreign_stdout, $sudo_stderr, @{$self->sudo_perl_command;}) or die "open3 failed: $!" 0 0 0
44 ) or die "open3 failed: $!";
45 0 chomp(my $line = <$sudo_stderr>);
46 0 ▶ if ($line eq "GO") {
Branch: 4 branches
BranchTrueFalseTotal
if ($line eq 'GO') { } 0 0 0
elsif ($line =~ /\[sudo\]/) { } 0 0 0
if ($line eq 'GO') { } 0 0 0
elsif ($line =~ /\[sudo\]/) { } 0 0 0
47 # started already, we're good
48 } elsif ($line =~ /\[sudo\]/) {
49 0 my $cb = $self->password_callback;
50 0 ▶ die "sudo sent ${line} but we have no password callback"
Branch: 2 branches
BranchTrueFalseTotal
unless $cb 0 0 0
unless $cb 0 0 0
51 unless $cb;
52 0 print $foreign_stdin $cb->($line, @_), "\n";
53 0 chomp($line = <$sudo_stderr>);
54 0 ▶ if ($line and $line ne 'GO') {
Branch: 4 branches
BranchTrueFalseTotal
if ($line and $line ne 'GO') { } 0 0 0
elsif (not $line) { } 0 0 0
if ($line and $line ne 'GO') { } 0 0 0
elsif (not $line) { } 0 0 0
Condition0%0/6
$line and $line ne "GO"
!ll&&!rl&&r
000
$line and $line ne "GO"
!ll&&!rl&&r
000
MC/DC0%0/2
$line and $line ne "GO"
$line $line ne "GO"
MC/DC0%0/2
$line and $line ne "GO"
$line $line ne "GO"
Truth table0%0/3
A and B
A= $lineB= $line ne "GO"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $lineB= $line ne "GO"
ABresult
0X0
100
111
55 0 die "sent password and expected newline from sudo, got ${line}";
56 }
57 elsif (not $line) {
58 0 chomp($line = <$sudo_stderr>);
59 0 ▶ die "sent password but next line was ${line}"
Branch: 2 branches
BranchTrueFalseTotal
unless $line eq "GO" 0 0 0
unless $line eq "GO" 0 0 0
60 unless $line eq "GO";
61 }
62 } else {
63 0 die "Got inexplicable line ${line} trying to sudo";
64 };
65 Object::Remote->current_loop
66 ->watch_io(
67 handle => $sudo_stderr,
68 0 ▶ on_read_ready => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
69 0 ▶ Dlog_debug { "LocalSudo: Preparing to read data from $_" } $sudo_stderr;
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 ▶ if (sysread($sudo_stderr, my $buf, 32768) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (sysread($sudo_stderr, my $buf, 32768) > 0) { } 0 0 0
if (sysread($sudo_stderr, my $buf, 32768) > 0) { } 0 0 0
71 0 ▶ log_trace { "LocalSudo: successfully read data, printing it to STDERR" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 print STDERR $buf;
73 0 ▶ log_trace { "LocalSudo: print() to STDERR is done" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 } else {
75 0 ▶ log_debug { "LocalSudo: received EOF or error on file handle, unwatching it" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 Object::Remote->current_loop
77 ->unwatch_io(
78 handle => $sudo_stderr,
79 on_read_ready => 1
80 );
81 }
82 }
83 0 );
84 0 return ($foreign_stdin, $foreign_stdout, $pid);
85 };
86  
87 11 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 push @Object::Remote::Connection::Guess, sub {
90 for ($_[0]) {
91 # username followed by @
92 ▶ if (defined and !ref and /^ ([^\@]*?) \@ $/x) {
Condition0%0/3
defined $_ and not ref $_ and /^ ([^\@]*?) \@ $/x
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and not ref $_ and /^ ([^\@]*?) \@ $/x
defined $_ and not ref $_ /^ ([^\@]*?) \@ $/x
Truth table0%0/3
A and B
A= defined $_ and not ref $_B= /^ ([^\@]*?) \@ $/x
ABresult
0X0
100
111
93 shift(@_);
94 return __PACKAGE__->new(@_, target_user => $1);
95 }
96 }
97 return;
98 };
99  
100 1;
« Remote/Connector/STDIO.pm ↑ Summary Remote/Prompt.pm »