lib/Kubernetes/REST/AuthTokenFile.pm

filter: Statementstmt 100% 50 / 50 Branchbran 100% 12 / 12 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 11 / 11 Podpod 50.0% 1 / 2 totaltotal 96.4% 81 / 84 CC 9 SCAR 11.0
CC
9
Cov
99%
token
6.0
_read_file
3.0
_fingerprint_now
2.0
CRAP 3.0
« lib/Kubernetes/REST/CLI/Role/Connection.pm ↑ Summary lib/Kubernetes/REST/APIError.pm »
1 package Kubernetes::REST::AuthTokenFile;
2 37 our $VERSION = '1.110';
3 # ABSTRACT: Bearer token read from a file, re-read when the file changes
4 37 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 37 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 37 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 37 has file => (
12 is => 'ro',
13 required => 1,
14 );
15  
16  
17 has description => (
18 is => 'ro',
19 6 ▶ default => sub { 'token file' },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 37 );
21  
22  
23 has refresh => (
24 is => 'ro',
25 6 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 37 );
27  
28  
29 37 has _token => (
30 is => 'rw',
31 init_arg => undef,
32 );
33  
34 37 has _fingerprint => (
35 is => 'rw',
36 init_arg => undef,
37 );
38  
39 43 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
40 43 my $self = shift;
41 # The first read is fatal: a caller naming a token file has said this is
42 # how it authenticates, and there is nothing to fall back on yet.
43 43 $self->_read_file;
44 34 return;
45 }
46  
47 54 ▶sub token {
Subroutine token: called CC=6 SCAR=17.9
Pod documented
48 54 my $self = shift;
49  
50  
51 54 ▶ return $self->_token unless $self->refresh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->refresh 5 49 54
52  
53 49 my $fingerprint = $self->_fingerprint_now;
54 49 my $known = $self->_fingerprint;
55  
56 # Unreadable right now, or unchanged: keep what we have.
57 49 ▶ return $self->_token
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fingerprint or defined $known and $fingerprint eq $known 42 7 49
Condition83%5/6
not defined $fingerprint or defined $known and $fingerprint eq $known
l!l&&r!l&&!r
2407
defined $known and $fingerprint eq $known
!ll&&!rl&&r
0740
MC/DC66%2/3
not defined $fingerprint or defined $known and $fingerprint eq $known
not defined $fingerprint defined $known $fingerprint eq $known
Truth table75%3/4
not(A) or B and C
A= not defined $fingerprintB= defined $knownC= $fingerprint eq $known
ABCresult
1XX1
0111
00X0
0100
58 if !defined $fingerprint
59 or (defined $known and $fingerprint eq $known);
60  
61 7 my $token = eval { $self->_read_file };
62 7 ▶ return defined $token ? $token : $self->_token;
Branch: 1 branch
BranchTrueFalseTotal
defined $token ? : 5 2 7
63 }
64  
65 # What "the file changed" is measured on: device and inode, size, and
66 # modification time.
67 #
68 # Inode is the one that matters for a rotation. The kubelet does not overwrite
69 # a mounted token, it writes a new timestamped directory and swaps the ..data
70 # symlink onto it, so the path resolves to a different file afterwards - and
71 # stat() follows symlinks, so it is the new file that gets measured. Size and
72 # mtime are what catch a plain overwrite in place, which is what a login helper
73 # writing the file itself tends to do.
74 #
75 # Time::HiRes::stat rather than stat, because core stat reports mtime in whole
76 # seconds and two writes within the same second are then indistinguishable.
77 #
78 # What this does not catch: an in-place rewrite that keeps the inode, the exact
79 # same size, and a modification time the filesystem cannot tell apart from the
80 # previous one. Nothing short of reading the file on every call would, and
81 # reading it on every call is the thing this stat exists to avoid.
82 99 ▶sub _fingerprint_now {
Subroutine _fingerprint_now: called CC=2 SCAR=6.9
83 99 my $self = shift;
84 99 ▶ my @stat = Time::HiRes::stat($self->file) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my(@stat) = &Time::HiRes::stat(scalar $self->file) or return undef 92 7 99
85 92 return join ':', @stat[0, 1, 7, 9];
86 }
87  
88 50 ▶sub _read_file {
Subroutine _read_file: called CC=3 SCAR=11.0
89 50 my $self = shift;
90 50 my $file = $self->file;
91  
92 # Fingerprint first, content second. Should the file change between the
93 # two, the pair on record is an old fingerprint with new content, so the
94 # next call reads once too often - the other order would record a new
95 # fingerprint with old content and never look again.
96 50 my $fingerprint = $self->_fingerprint_now;
97  
98 50 my $token = eval { path($file)->slurp_raw };
99 50 ▶ croak 'Cannot read the ' . $self->description . " ($file): $@"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 5 45 50
100 unless defined $token;
101  
102 # Such a file ends with a newline more often than not, and a newline inside
103 # an Authorization header is not something a server ever sees as intended.
104 45 $token =~ s/\A\s+//;
105 45 $token =~ s/\s+\z//;
106 45 ▶ croak 'The ' . $self->description . " ($file) holds no token"
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 6 39 45
107 unless length $token;
108  
109 39 $self->_token($token);
110 39 $self->_fingerprint($fingerprint);
111  
112 39 return $token;
113 }
114  
115 37 1;
116  
117 __END__
« lib/Kubernetes/REST/CLI/Role/Connection.pm ↑ Summary lib/Kubernetes/REST/APIError.pm »