lib/Kubernetes/REST/AuthExec.pm

filter: Statementstmt 91.8% 90 / 98 Branchbran 68.7% 22 / 32 Conditioncond 63.6% 7 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 15 / 15 Podpod 50.0% 1 / 2 totaltotal 83.6% 138 / 165 CC 22 SCAR 16.9
CC
22
Cov
84%
_run_plugin
8.3
_refresh
8.2
_parse_timestamp
7.5
CRAP 5.4
« lib/Kubernetes/REST/HTTPTinyIO.pm ↑ Summary lib/Kubernetes/REST/CLI/Crd2Class.pm »
1 package Kubernetes::REST::AuthExec;
2 37 our $VERSION = '1.110';
3 # ABSTRACT: Bearer token from an exec credential plugin, run again before it expires
4 37 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 37 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶use Time::Local qw(timegm);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 37 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 37 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 37 has command => (
13 is => 'ro',
14 required => 1,
15 );
16  
17  
18 has args => (
19 is => 'ro',
20 76 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 37 );
22  
23  
24 has env => (
25 is => 'ro',
26 76 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 37 );
28  
29  
30 has expiry_margin => (
31 is => 'ro',
32 119 ▶ default => sub { 60 },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 37 );
34  
35  
36 has description => (
37 is => 'ro',
38 127 ▶ default => sub { 'exec credential' },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 37 );
40  
41  
42 37 has _token => (
43 is => 'rw',
44 init_arg => undef,
45 );
46  
47 37 has _expires => (
48 is => 'rw',
49 init_arg => undef,
50 );
51  
52 127 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
53 127 my ($self) = @_;
54 # The first run is fatal: nothing to fall back on yet.
55 127 $self->_refresh;
56 93 return;
57 }
58  
59 212 ▶sub token {
Subroutine token: called CC=4 SCAR=13.9
Pod documented
60 212 my ($self) = @_;
61  
62  
63 212 my $expires = $self->_expires;
64 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
65 38 ▶ eval { $self->_refresh; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_refresh; 1 } } or carp('Cannot renew the ' . $self->description . ': ' . $@) 29 5 34
66 or carp 'Cannot renew the ' . $self->description . ': ' . $@;
67 }
68 208 return $self->_token;
69 }
70  
71 165 ▶sub _refresh {
Subroutine _refresh: called CC=7 SCAR=21.1
72 165 my ($self) = @_;
73 165 my $cred = YAML::XS::Load($self->_run_plugin);
74 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
75  
76 127 my $token = $status->{token};
77 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
78 croak 'exec credential did not return a token: only token plugins are supported, not client certificate credentials'
79 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
80 0 croak 'exec credential did not return token';
81 }
82  
83 122 $self->_token($token);
84 122 $self->_expires($self->_parse_timestamp($status->{expirationTimestamp}));
85 122 return $token;
86 }
87  
88 # RFC3339: Z or +-hh:mm, optional fraction. Unparseable counts as no expiry.
89 122 ▶sub _parse_timestamp {
Subroutine _parse_timestamp: called CC=6 SCAR=20.2
90 122 my ($self, $ts) = @_;
91 122 ▶ return unless defined $ts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ts 45 77 122
92 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
93 (\d{4})-(\d\d)-(\d\d) [Tt ] (\d\d):(\d\d):(\d\d) (?:\.\d+)?
94 (Z|z|[+-]\d\d:?\d\d)
95 \z/x or return;
96 77 my $epoch = eval { timegm($s, $mi, $h, $d, $mo - 1, $y) };
97 77 ▶ return unless defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch 0 77 77
98 77 ▶ if ($zone =~ /\A([+-])(\d\d):?(\d\d)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zone =~ /\A([+-])(\d\d):?(\d\d)\z/) 0 77 77
99 0 ▶ my $offset = ($2 * 3600 + $3 * 60) * ($1 eq '-' ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 0 0 0
100 0 $epoch -= $offset;
101 }
102 77 return $epoch;
103 }
104  
105 165 ▶sub _run_plugin {
Subroutine _run_plugin: called CC=8 SCAR=21.1
106 165 my ($self) = @_;
107  
108 165 my $cmd = $self->command;
109 165 my @args = @{$self->args};
110  
111 # Set up environment
112 165 local %ENV = %ENV;
113 165 for my $env (@{$self->env}) {
114 6 $ENV{$env->{name}} = $env->{value};
115 }
116  
117 # No shell: fork and exec with the indirect-object form, so even a bare
118 # command (a one-element list, which perl would hand to /bin/sh when it
119 # holds whitespace or metacharacters) is one program path and each arg is
120 # exactly one argv element. stderr is inherited, so the plugin's own
121 # complaint stays visible. A failed exec is reported through a pipe, as
122 # perl does for the list form of open.
123 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
124 165 my $pid = open(my $fh, '-|');
125 165 ▶ croak "exec credential command $cmd could not be started: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 165 165
126 unless defined $pid;
127 165 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 23 142 165
128 23 close $err_r;
129 23 { exec {$cmd} $cmd, @args }
130 0 my $errno = $! + 0;
131 0 print {$err_w} $errno;
132 0 close $err_w;
133 0 POSIX::_exit(127);
134 }
135 142 close $err_w;
136 142 my $exec_errno = do { local $/; <$err_r> };
137 142 close $err_r;
138 142 my $output = do { local $/; <$fh> };
139 142 close $fh;
140 142 my $status = $?;
141 142 ▶ if (length $exec_errno) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $exec_errno) 3 139 142
142 3 local $! = $exec_errno;
143 3 croak "exec credential command $cmd could not be started: $!";
144 }
145 139 ▶ croak "exec credential command $cmd was killed by signal " . ($status & 127)
Branch: 1 branch
BranchTrueFalseTotal
if $status & 127 0 139 139
146 if $status & 127;
147 139 ▶ croak "exec credential command $cmd exited with status " . ($status >> 8)
Branch: 1 branch
BranchTrueFalseTotal
if $status 12 127 139
148 if $status;
149  
150 127 return $output;
151 }
152  
153 37 1;
154  
155 __END__
« lib/Kubernetes/REST/HTTPTinyIO.pm ↑ Summary lib/Kubernetes/REST/CLI/Crd2Class.pm »