Connector/Proxy/Proc/SafeExec.pm

filter: Statementstmt 93.5% 130 / 139 Branchbran 63.1% 24 / 38 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.8% 16 / 18 Podpod 100% 4 / 4 totaltotal 85.7% 175 / 204 CC 24 SCAR 21.1
CC
24
Cov
86%
_run_command
16.2
set
7.5
_build_config
6.0
CRAP 8.3
« Connector/Builtin/Authentication/PasswordScheme.pm ↑ Summary Connector/Role/LocalPath.pm »
1 # Connector::Proxy::Proc::SafeExec
2 #
3 # Connector class for running system commands
4 #
5 # Written by Martin Bartosch for the OpenXPKI project 2012
6 #
7 package Connector::Proxy::Proc::SafeExec;
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Proc::SafeExec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Temp qw(tempdir tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 extends 'Connector::Proxy';
21  
22 has args => (
23 is => 'rw',
24 isa => 'ArrayRef[Str]',
25 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 1 );
27  
28 1 has timeout => (
29 is => 'rw',
30 isa => 'Int',
31 default => 5,
32 );
33  
34 1 has chomp_output => (
35 is => 'rw',
36 isa => 'Bool',
37 default => 1,
38 );
39  
40 1 has stdin => (
41 is => 'rw',
42 isa => 'Str|ArrayRef[Str]|Undef',
43 );
44  
45 1 has env => (
46 is => 'rw',
47 isa => 'HashRef[Str]',
48 );
49  
50 1 has content => (
51 is => 'rw',
52 isa => 'Str',
53 );
54  
55 0 ▶sub _build_config {
Subroutine _build_config: not called CC=2 SCAR=17.9
56 0 my $self = shift;
57  
58 0 ▶ if (! -x $self->LOCATION()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-x $self->LOCATION) 0 0 0
59 0 die("Specified system command is not executable: " . $self->LOCATION());
60 }
61  
62 0 return 1;
63 }
64  
65 # this method always returns the file contents, regardless of the specified
66 # key
67 17 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
68  
69 17 my $self = shift;
70 17 my @args = $self->_build_path( shift );
71  
72 # compose a list of command arguments
73 17 return $self->_run_command({ ARGS => \@args });
74 }
75  
76 2 ▶sub set {
Subroutine set: called CC=6 SCAR=20.1
Pod documented
77  
78 2 my $self = shift;
79 2 my @args = $self->_build_path( shift );
80 2 my $data = shift;
81  
82 2 my $content;
83 2 ▶ if ($self->content()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content) { } 2 0 2
84 2 $self->log()->debug('Process template for content ' . $self->content());
85 2 my $template = Template->new({});
86 2 ▶ $data = { DATA => $data } if (ref $data eq '');
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "" 0 2 2
87 2 ▶ $template->process( \$self->content(), $data, \$content) || $self->_log_and_die("Error processing content template.");
Branch: 1 branch
BranchTrueFalseTotal
$template->process(\$self->content, $data, \$content) or $self->_log_and_die("Error processing content template.") 2 0 2
88 } else {
89 0 ▶ if (ref $data ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "") 0 0 0
90 0 $self->_log_and_die("You need to define a content template if data is not a scalar");
91 }
92 0 $content = $data;
93 }
94  
95 2 my $tmpdir = tempdir( CLEANUP => 1 );
96 2 my ($fh, $source) = tempfile( DIR => $tmpdir );
97  
98 2 ▶ open FILE, ">$source" || $self->_log_and_die("Unable to open file for writing");
Condition33%1/3
">$source" || $self->_log_and_die('Unable to open file for writing')
l!l&&r!l&&!r
200
MC/DC0%0/2
">$source" || $self->_log_and_die('Unable to open file for writing')
">$source" $self->_log_and_die('Unable to open file for writing')
Truth table33%1/3
A || B
A= ">$source"B= $self->_log_and_die('Unable to open file for writing')
ABresult
1X1
011
000
99 2 print FILE $content;
100 2 close FILE;
101  
102 2 return $self->_run_command({ ARGS => \@args, FILE => $source, DATA => $data });
103  
104 }
105  
106  
107 19 ▶sub _run_command {
Subroutine _run_command: called CC=16 SCAR=27.9
108  
109 19 my $self = shift;
110 19 my $template_vars = shift;
111  
112 19 my $template = Template->new({});
113  
114 # process configured system command arguments and replace templates
115 # in it with the passed arguments, accessible via [% ARGS.0 %]
116 19 my @cmd_args;
117 19 foreach my $item (@{$self->args()}) {
118 32 my $value;
119 32 ▶ $template->process(\$item, $template_vars, \$value) || die "Error processing argument template.";
Branch: 1 branch
BranchTrueFalseTotal
$template->process(\$item, $template_vars, \$value) or die "Error processing argument template." 32 0 32
120 32 push @cmd_args, $value;
121 }
122  
123 19 my %filehandles;
124  
125 my @feed_to_stdin;
126 19 ▶ if (defined $self->stdin()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->stdin) 8 11 19
127 8 my @raw_stdin_data;
128 8 ▶ if (ref $self->stdin() eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->stdin eq '') { } 3 5 8
elsif (ref $self->stdin eq 'ARRAY') { } 5 0 5
129 3 push @raw_stdin_data, $self->stdin();
130 } elsif (ref $self->stdin() eq 'ARRAY') {
131 5 push @raw_stdin_data, @{$self->stdin()};
132 }
133 8 foreach my $line (@raw_stdin_data) {
134 13 my $value;
135 13 ▶ $template->process(\$line, $template_vars, \$value) || die "Error processing stdin template.";
Branch: 1 branch
BranchTrueFalseTotal
$template->process(\$line, $template_vars, \$value) or die "Error processing stdin template." 13 0 13
136 13 push @feed_to_stdin, $value;
137 }
138  
139 # we have data to pipe to stdin, create a filehandle
140 8 $filehandles{stdin} = 'new';
141 }
142  
143 19 ▶ if (defined $self->env()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->env) 10 9 19
144 10 ▶ if (ref $self->env() eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->env eq "HASH") 10 0 10
145 10 foreach my $key (keys %{$self->env()}) {
146 10 my $value;
147 10 ▶ $template->process(\$self->env()->{$key}, $template_vars, \$value) || die "Error processing environment template.";
Branch: 1 branch
BranchTrueFalseTotal
$template->process(\$self->env->{$key}, $template_vars, \$value) or die "Error processing environment template." 10 0 10
148 10 $ENV{$key} = $value;
149 }
150 }
151 }
152  
153 19 my $stdout = File::Temp->new();
154 19 $filehandles{stdout} = \*$stdout;
155  
156 19 my $stderr = File::Temp->new();
157 19 $filehandles{stderr} = \*$stderr;
158  
159  
160 # compose the system command to execute
161 19 my @cmd;
162 19 push @cmd, $self->{LOCATION};
163 19 push @cmd, @cmd_args;
164  
165 19 my $command = Proc::SafeExec->new({
166 exec => \@cmd,
167 %filehandles,
168 });
169  
170 19 try {
171 19 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
172 19 ▶ if (scalar @feed_to_stdin) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @feed_to_stdin) 8 11 19
173 8 my $stdin = $command->stdin();
174 8 print $stdin join("\n", @feed_to_stdin);
175 }
176 19 alarm $self->timeout();
177 19 $command->wait();
178 } catch ($error) {
179 1 ▶ if ($error eq "alarm\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($error eq "alarm\n") 1 0 1
180 1 die "System command timed out after " . $self->timeout() . " seconds";
181 }
182 0 die $error;
183 } finally {
184 19 alarm 0;
185 };
186  
187 18 my $stderr_content = do {
188 18 open my $fh, '<', $stderr->filename;
189 18 local $INPUT_RECORD_SEPARATOR;
190 18 <$fh>;
191 };
192  
193 18 ▶ if ($command->exit_status() != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command->exit_status != 0) 1 17 18
194 1 die "System command exited with return code " . ($command->exit_status() >> 8) . ". STDERR: $stderr_content";
195 }
196  
197 17 my $stdout_content = do {
198 17 open my $fh, '<', $stdout->filename;
199 17 local $INPUT_RECORD_SEPARATOR;
200 17 <$fh>;
201 };
202  
203 17 ▶ if ($self->chomp_output()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->chomp_output) 17 0 17
204 17 chomp $stdout_content;
205 }
206  
207 17 return $stdout_content;
208 }
209  
210 2 ▶sub get_meta {
Subroutine get_meta: called CC=2 SCAR=6.9
Pod documented
211 2 my $self = shift;
212  
213 # If we have no path, we tell the caller that we are a connector
214 2 my @path = $self->_build_path( shift );
215 2 ▶ if (scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path == 0) 1 1 2
216 1 return { TYPE => "connector" };
217 }
218  
219 1 return {TYPE => "scalar" };
220 }
221  
222 3 ▶sub exists {
Subroutine exists: called CC=2 SCAR=6.9
Pod documented
223  
224 3 my $self = shift;
225  
226 # No path = connector root which always exists
227 3 my @path = $self->_build_path( shift );
228 3 ▶ if (scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path == 0) 1 2 3
229 1 return 1;
230 }
231 2 my $val;
232 2 eval {
233 2 $val = $self->get( \@path );
234 };
235 2 return defined $val;
236  
237 }
238  
239 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
240 1 __PACKAGE__->meta->make_immutable;
241  
242 1 1;
243 __END__
« Connector/Builtin/Authentication/PasswordScheme.pm ↑ Summary Connector/Role/LocalPath.pm »