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
« blib/lib/Kubernetes/REST/Error.pm ↑ Summary blib/lib/Kubernetes/REST/LWPIO.pm »
1 package Kubernetes::REST::AuthTokenFile;
2 11 our $VERSION = '1.109';
3 # ABSTRACT: Bearer token read from a file, re-read when the file changes
4 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 11 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 11 );
21  
22  
23 has refresh => (
24 is => 'ro',
25 6 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 11 );
27  
28  
29 11 has _token => (
30 is => 'rw',
31 init_arg => undef,
32 );
33  
34 11 has _fingerprint => (
35 is => 'rw',
36 init_arg => undef,
37 );
38  
39 32 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
40 32 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 32 $self->_read_file;
44 23 return;
45 }
46  
47 47 ▶sub token {
Subroutine token: called CC=6 SCAR=17.9
Pod documented
48 47 my $self = shift;
49  
50  
51 47 ▶ return $self->_token unless $self->refresh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->refresh 5 42 47
52  
53 42 my $fingerprint = $self->_fingerprint_now;
54 42 my $known = $self->_fingerprint;
55  
56 # Unreadable right now, or unchanged: keep what we have.
57 42 ▶ return $self->_token
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fingerprint or defined $known and $fingerprint eq $known 35 7 42
Condition83%5/6
not defined $fingerprint or defined $known and $fingerprint eq $known
l!l&&r!l&&!r
2337
defined $known and $fingerprint eq $known
!ll&&!rl&&r
0733
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 81 ▶sub _fingerprint_now {
Subroutine _fingerprint_now: called CC=2 SCAR=6.9
83 81 my $self = shift;
84 81 ▶ 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 74 7 81
85 74 return join ':', @stat[0, 1, 7, 9];
86 }
87  
88 39 ▶sub _read_file {
Subroutine _read_file: called CC=3 SCAR=11.0
89 39 my $self = shift;
90 39 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 39 my $fingerprint = $self->_fingerprint_now;
97  
98 39 my $token = eval { path($file)->slurp_raw };
99 39 ▶ croak 'Cannot read the ' . $self->description . " ($file): $@"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 5 34 39
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 34 $token =~ s/\A\s+//;
105 34 $token =~ s/\s+\z//;
106 34 ▶ croak 'The ' . $self->description . " ($file) holds no token"
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 6 28 34
107 unless length $token;
108  
109 28 $self->_token($token);
110 28 $self->_fingerprint($fingerprint);
111  
112 28 return $token;
113 }
114  
115 11 1;
116  
117 __END__
118  
119 =pod
120  
121 =encoding UTF-8
122  
123 =head1 NAME
124  
125 Kubernetes::REST::AuthTokenFile - Bearer token read from a file, re-read when the file changes
126  
127 =head1 VERSION
128  
129 version 1.109
130  
131 =head1 SYNOPSIS
132  
133 use Kubernetes::REST::AuthTokenFile;
134  
135 my $auth = Kubernetes::REST::AuthTokenFile->new(
136 file => '/var/run/secrets/kubernetes.io/serviceaccount/token',
137 );
138  
139 my $api = Kubernetes::REST->new(
140 server => $server,
141 credentials => $auth,
142 );
143  
144 # Read once, and again whenever the file has changed since
145 my $token = $auth->token;
146  
147 # Read once and never again
148 my $fixed = Kubernetes::REST::AuthTokenFile->new(
149 file => $path,
150 refresh => 0,
151 );
152  
153 =head1 DESCRIPTION
154  
155 Authentication credentials whose bearer token lives in a file: a kubeconfig
156 user's C<tokenFile>, or the service account token mounted into a pod at
157 F</var/run/secrets/kubernetes.io/serviceaccount/token>.
158  
159 Kubernetes rotates those files. The kubelet replaces a projected service
160 account token well before it expires, and a client that read the file once at
161 startup keeps sending a token that stops being valid while a good one sits in
162 the file next to it. This class re-reads instead: every call to L</token>
163 checks whether the file has changed, and reads it again when it has.
164  
165 L<Kubernetes::REST> asks its C<credentials> for a C<token()> on every request,
166 so passing one of these is all a long-running process needs.
167  
168 =head2 file
169  
170 Required. Path to the file holding the token. Used as given on every read, so
171 an absolute path is the safe choice for a process that may C<chdir> - that is
172 what L<Kubernetes::REST::Kubeconfig> passes.
173  
174 =head2 description
175  
176 How this file is named in error messages, e.g. C<tokenFile of user 'prod'> or
177 C<service account token>. Defaults to C<token file>.
178  
179 =head2 refresh
180  
181 Whether L</token> looks at the file again after the first read. True by
182 default. With C<< refresh => 0 >> the file is read exactly once, when the object is
183 built, and the token stays what it was then - the behaviour of a plain
184 L<Kubernetes::REST::AuthToken>, for a caller who wants no per-call C<stat> or
185 knows the file never changes.
186  
187 =head2 token
188  
189 my $token = $auth->token;
190  
191 The bearer token. Reads the file again if it has changed since the last read,
192 otherwise hands back what was read then.
193  
194 Cost: one C<stat> per call, so one extra syscall per request. That is the price
195 of noticing a rotation without a timer, and it is deliberate.
196  
197 If the file cannot be read any more, or holds no token, the last good token is
198 returned instead of dying: a rotation can take the file away for a moment, and
199 an error there would break a client that is one C<stat> away from recovering on
200 its own. Only the very first read, in the constructor, is fatal.
201  
202 =head1 SEE ALSO
203  
204 =over
205  
206 =item * L<Kubernetes::REST> - Main API client
207  
208 =item * L<Kubernetes::REST::AuthToken> - Credentials holding a fixed token
209  
210 =item * L<Kubernetes::REST::Kubeconfig> - Builds these from a C<tokenFile>
211  
212 =back
213  
214 =head1 SUPPORT
215  
216 =head2 Issues
217  
218 Please report bugs and feature requests on GitHub at
219 L<https://github.com/pplu/kubernetes-rest/issues>.
220  
221 =head2 IRC
222  
223 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
224  
225 =head1 CONTRIBUTING
226  
227 Contributions are welcome! Please fork the repository and submit a pull request.
228  
229 =head1 AUTHORS
230  
231 =over 4
232  
233 =item *
234  
235 Torsten Raudssus <getty@cpan.org>
236  
237 =item *
238  
239 Jose Luis Martinez Torres <jlmartin@cpan.org>
240  
241 =back
242  
243 =head1 COPYRIGHT AND LICENSE
244  
245 This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>.
246  
247 This is free software, licensed under:
248  
249 The Apache License, Version 2.0, January 2004
250  
251 =cut
« blib/lib/Kubernetes/REST/Error.pm ↑ Summary blib/lib/Kubernetes/REST/LWPIO.pm »