lib/Kubernetes/REST/AuthExec.pm

filter: Statementstmt 88.1% 127 / 144 Branchbran 64.8% 35 / 54 Conditioncond 63.6% 7 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 95.0% 19 / 20 Podpod 50.0% 1 / 2 totaltotal 80.6% 192 / 238 CC 33 SCAR 19.6
CC
33
Cov
81%
_run_plugin_spawn
13.5
_run_plugin
9.4
_refresh
8.2
CRAP 7.1
« blib/lib/Kubernetes/REST/CLI/Cmd/Raw.pm ↑ Summary lib/Kubernetes/REST/HTTPTinyIO.pm »
1 package Kubernetes::REST::AuthExec;
2 41 our $VERSION = '1.113';
3 # ABSTRACT: Bearer token from an exec credential plugin, run again before it expires
4 41 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 41 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 41 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 41 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 41 ▶use Time::Local qw(timegm);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 41 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 41 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 41 has command => (
14 is => 'ro',
15 required => 1,
16 );
17  
18  
19 has args => (
20 is => 'ro',
21 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 41 );
23  
24  
25 has env => (
26 is => 'ro',
27 76 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 41 );
29  
30  
31 has expiry_margin => (
32 is => 'ro',
33 119 ▶ default => sub { 60 },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 41 );
35  
36  
37 has description => (
38 is => 'ro',
39 127 ▶ default => sub { 'exec credential' },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 41 );
41  
42  
43 41 has _token => (
44 is => 'rw',
45 init_arg => undef,
46 );
47  
48 41 has _expires => (
49 is => 'rw',
50 init_arg => undef,
51 );
52  
53 127 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
54 127 my ($self) = @_;
55 # The first run is fatal: nothing to fall back on yet.
56 127 $self->_refresh;
57 93 return;
58 }
59  
60 212 ▶sub token {
Subroutine token: called CC=4 SCAR=13.9
Pod documented
61 212 my ($self) = @_;
62  
63  
64 212 my $expires = $self->_expires;
65 212 ▶ if (defined $expires and $expires - time <= $self->expiry_margin) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expires and $expires - time <= $self->expiry_margin) 38 174 212
Condition100%3/3
defined $expires and $expires - time <= $self->expiry_margin
!ll&&!rl&&r
1017338
MC/DC100%2/2
defined $expires and $expires - time <= $self->expiry_margin
defined $expires $expires - time <= $self->expiry_margin
Truth table100%3/3
A and B
A= defined $expiresB= $expires - time <= $self->expiry_margin
ABresult
0X0
100
111
66 38 ▶ eval { $self->_refresh; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_refresh; 1 } } or carp('Cannot renew the ' . $self->description . ': ' . $@) 29 5 34
67 or carp 'Cannot renew the ' . $self->description . ': ' . $@;
68 }
69 208 return $self->_token;
70 }
71  
72 165 ▶sub _refresh {
Subroutine _refresh: called CC=7 SCAR=21.1
73 165 my ($self) = @_;
74 165 my $cred = YAML::XS::Load($self->_run_plugin);
75 127 ▶ my $status = ref $cred eq 'HASH' ? ($cred->{status} // {}) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $cred eq 'HASH' ? : 127 0 127
Condition50%1/2
$cred->{"status"} // {}
l!l
1270
MC/DC0%0/1
$cred->{"status"} // {}
$cred->{"status"}
Truth table50%1/2
A
A= $cred->{"status"}
Aresult
11
00
76  
77 127 my $token = $status->{token};
78 127 ▶ unless (defined $token and length $token) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $token and length $token) 5 122 127
Condition66%2/3
defined $token and length $token
!ll&&!rl&&r
50122
MC/DC50%1/2
defined $token and length $token
defined $token length $token
Truth table66%2/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
79 croak 'exec credential did not return a token: only token plugins are supported, not client certificate credentials'
80 5 ▶ if $status->{clientCertificateData} or $status->{clientKeyData};
Branch: 1 branch
BranchTrueFalseTotal
if $status->{"clientCertificateData"} or $status->{"clientKeyData"} 5 0 5
Condition33%1/3
$status->{"clientCertificateData"} or $status->{"clientKeyData"}
l!l&&r!l&&!r
500
MC/DC0%0/2
$status->{"clientCertificateData"} or $status->{"clientKeyData"}
$status->{"clientCertificateData"} $status->{"clientKeyData"}
Truth table33%1/3
A or B
A= $status->{"clientCertificateData"}B= $status->{"clientKeyData"}
ABresult
1X1
011
000
81 0 croak 'exec credential did not return token';
82 }
83  
84 122 $self->_token($token);
85 122 $self->_expires($self->_parse_timestamp($status->{expirationTimestamp}));
86 122 return $token;
87 }
88  
89 # RFC3339: Z or +-hh:mm, optional fraction. Unparseable counts as no expiry.
90 122 ▶sub _parse_timestamp {
Subroutine _parse_timestamp: called CC=6 SCAR=20.2
91 122 my ($self, $ts) = @_;
92 122 ▶ return unless defined $ts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ts 45 77 122
93 77 ▶ my ($y, $mo, $d, $h, $mi, $s, $zone) = "$ts" =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
() = "$ts" =~ /\A (\d{4})-(\d\d)-(\d\d) [Tt ] (\d\d):(\d\d):(\d\d) (?:\.\d+)? (Z|z|[+-]\d\d:?\d\d) \z/x or return 77 0 77
94 (\d{4})-(\d\d)-(\d\d) [Tt ] (\d\d):(\d\d):(\d\d) (?:\.\d+)?
95 (Z|z|[+-]\d\d:?\d\d)
96 \z/x or return;
97 77 my $epoch = eval { timegm($s, $mi, $h, $d, $mo - 1, $y) };
98 77 ▶ return unless defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch 0 77 77
99 77 ▶ if ($zone =~ /\A([+-])(\d\d):?(\d\d)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zone =~ /\A([+-])(\d\d):?(\d\d)\z/) 0 77 77
100 0 ▶ my $offset = ($2 * 3600 + $3 * 60) * ($1 eq '-' ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 0 0
101 0 $epoch -= $offset;
102 }
103 77 return $epoch;
104 }
105  
106 165 ▶sub _run_plugin {
Subroutine _run_plugin: called CC=9 SCAR=22.4
107 165 my ($self) = @_;
108  
109 165 my $cmd = $self->command;
110 165 my @args = @{$self->args};
111  
112 # Set up environment
113 165 local %ENV = %ENV;
114 165 for my $env (@{$self->env}) {
115 6 $ENV{$env->{name}} = $env->{value};
116 }
117  
118 # Win32 has no forking pipe open and runs a bare "-" through cmd.exe
119 # instead (perlport, perlfork), so it takes the spawn branch below.
120 165 ▶ return $self->_run_plugin_spawn($cmd, @args) if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 165 165
121  
122 # No shell: fork and exec with the indirect-object form, so even a bare
123 # command (a one-element list, which perl would hand to /bin/sh when it
124 # holds whitespace or metacharacters) is one program path and each arg is
125 # exactly one argv element. stderr is inherited, so the plugin's own
126 # complaint stays visible. A failed exec is reported through a pipe, as
127 # perl does for the list form of open.
128 165 ▶ pipe(my $err_r, my $err_w) or croak "exec credential pipe failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $err_r, my $err_w or croak("exec credential pipe failed: $!") 165 0 165
129 165 my $pid = open(my $fh, '-|');
130 165 ▶ croak "exec credential command $cmd could not be started: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 165 165
131 unless defined $pid;
132 165 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 23 142 165
133 23 close $err_r;
134 23 { CORE::exec {$cmd} $cmd, @args }
135 0 my $errno = $! + 0;
136 0 print {$err_w} $errno;
137 0 close $err_w;
138 0 POSIX::_exit(127);
139 }
140 142 close $err_w;
141 142 my $exec_errno = do { local $/; <$err_r> };
142 142 close $err_r;
143 142 my $output = do { local $/; <$fh> };
144 142 close $fh;
145 142 my $status = $?;
146 142 ▶ if (length $exec_errno) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $exec_errno) 3 139 142
147 3 local $! = $exec_errno;
148 3 croak "exec credential command $cmd could not be started: $!";
149 }
150 139 ▶ croak "exec credential command $cmd was killed by signal " . ($status & 127)
Branch: 1 branch
BranchTrueFalseTotal
if $status & 127 0 139 139
151 if $status & 127;
152 139 ▶ croak "exec credential command $cmd exited with status " . ($status >> 8)
Branch: 1 branch
BranchTrueFalseTotal
if $status 12 127 139
153 if $status;
154  
155 127 return $output;
156 }
157  
158 # One argument as the Microsoft C runtime (and perl.exe) reads it back: in
159 # double quotes, a double quote as \", backslashes in front of a double quote
160 # or of the closing quote doubled. An empty argument is "".
161 13 ▶sub _quote_win32_arg {
Subroutine _quote_win32_arg: called CC=1 SCAR=0.0
162 13 my ($self, $arg) = @_;
163 13 $arg =~ s/(\\*)"/$1$1\\"/g;
164 13 $arg =~ s/(\\+)\z/$1$1/;
165 13 return qq{"$arg"};
166 }
167  
168 # Where fork cannot be used (Windows): run the plugin with the indirect-object
169 # system, which never involves a shell either, with STDOUT pointed at a
170 # temporary file and read back afterwards. Same contract as the fork branch:
171 # one argv element per argument, stderr inherited, the failures worded alike.
172 6 ▶sub _run_plugin_spawn {
Subroutine _run_plugin_spawn: called CC=11 SCAR=26.1
173 6 my ($self, $cmd, @args) = @_;
174  
175 6 my $tmp = File::Temp->new(TEMPLATE => 'k8s-exec-XXXXXXXX', TMPDIR => 1);
176 6 binmode $tmp;
177  
178 6 STDOUT->flush;
179 6 my $saved;
180 6 my $had_stdout = open($saved, '>&', \*STDOUT);
181 6 ▶ open(STDOUT, '>&', $tmp) or croak "exec credential could not redirect STDOUT: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $tmp or croak("exec credential could not redirect STDOUT: $!") 6 0 6
182  
183 # Windows: perl joins the arguments into one command line itself and puts
184 # an argument holding a space in quotes without escaping a quote inside it,
185 # but leaves one that already starts and ends with a quote alone. So every
186 # argument is quoted here, by the rules of the C runtime that parses it.
187 0 ▶ my @argv = $^O eq 'MSWin32' ? map { $self->_quote_win32_arg($_) } @args : @args;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 6 6
188  
189 6 my ($rc, $errno, $status);
190 6 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 6 6
191 # A failed spawn there sets status 255 (which a plugin can also exit
192 # with) and raises the exec warning: made fatal, it is told apart.
193 0 my $started = eval {
194 41 ▶ use warnings FATAL => 'exec';
Subroutine BEGIN: called CC=1 SCAR=0.0
195 0 $rc = CORE::system {$cmd} $cmd, @argv;
196 0 ($errno, $status) = ($! + 0, $?);
197 0 1;
198 };
199 0 ▶ ($errno, $rc) = ($! + 0, -1) unless $started;
Branch: 1 branch
BranchTrueFalseTotal
unless $started 0 0 0
200 }
201 else {
202 # Not fatal: perl's fork child would catch it and carry on. The
203 # parent gets -1 and $! for a failed start.
204 41 ▶ no warnings 'exec';
Subroutine BEGIN: called CC=1 SCAR=0.0
205 6 $rc = CORE::system {$cmd} $cmd, @argv;
206 6 ($errno, $status) = ($! + 0, $?);
207 }
208  
209 6 ▶ if ($had_stdout) { open(STDOUT, '>&', $saved) or croak "exec credential could not restore STDOUT: $!" }
Branch: 2 branches
BranchTrueFalseTotal
if ($had_stdout) { } 6 0 6
open STDOUT, ">&", $saved or croak("exec credential could not restore STDOUT: $!") 6 0 6
210 0 else { close STDOUT }
211  
212 6 ▶ if ($rc == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == -1) 1 5 6
213 1 local $! = $errno;
214 1 croak "exec credential command $cmd could not be started: $!";
215 }
216 5 ▶ croak "exec credential command $cmd was killed by signal " . ($status & 127)
Branch: 1 branch
BranchTrueFalseTotal
if $status & 127 1 4 5
217 if $status & 127;
218 4 ▶ croak "exec credential command $cmd exited with status " . ($status >> 8)
Branch: 1 branch
BranchTrueFalseTotal
if $status 2 2 4
219 if $status;
220  
221 2 ▶ seek $tmp, 0, 0 or croak "exec credential output could not be read: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $tmp, 0, 0 or croak("exec credential output could not be read: $!") 2 0 2
222 2 return do { local $/; <$tmp> };
223 }
224  
225 41 1;
226  
227 __END__
« blib/lib/Kubernetes/REST/CLI/Cmd/Raw.pm ↑ Summary lib/Kubernetes/REST/HTTPTinyIO.pm »