| 1 | package Kubernetes::REST::AuthTokenFile; | ||
| 2 | our $VERSION = '1.109'; | ||
| 3 | # ABSTRACT: Bearer token read from a file, re-read when the file changes | ||
| 4 | use Moo; | ||
| 5 | use Carp qw(croak); | ||
| 6 | use Path::Tiny qw(path); | ||
| 7 | use Time::HiRes (); | ||
| 8 | use namespace::clean; | ||
| 9 | |||
| 10 | |||
| 11 | has file => ( | ||
| 12 | is => 'ro', | ||
| 13 | required => 1, | ||
| 14 | ); | ||
| 15 | |||
| 16 | |||
| 17 | has description => ( | ||
| 18 | is => 'ro', | ||
| 19 | default => sub { 'token file' }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | |||
| 23 | has refresh => ( | ||
| 24 | is => 'ro', | ||
| 25 | default => sub { 1 }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | has _token => ( | ||
| 30 | is => 'rw', | ||
| 31 | init_arg => undef, | ||
| 32 | ); | ||
| 33 | |||
| 34 | has _fingerprint => ( | ||
| 35 | is => 'rw', | ||
| 36 | init_arg => undef, | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub BUILD { | ||
| 40 | 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 | $self->_read_file; | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub token { | ||
| 48 | my $self = shift; | ||
| 49 | |||
| 50 | |||
| 51 | return $self->_token unless $self->refresh; | ||
| 52 | |||
| 53 | my $fingerprint = $self->_fingerprint_now; | ||
| 54 | my $known = $self->_fingerprint; | ||
| 55 | |||
| 56 | # Unreadable right now, or unchanged: keep what we have. | ||
| 57 | return $self->_token | ||
| 58 | if !defined $fingerprint | ||
| 59 | or (defined $known and $fingerprint eq $known); | ||
| 60 | |||
| 61 | my $token = eval { $self->_read_file }; | ||
| 62 | return defined $token ? $token : $self->_token; | ||
| 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 | sub _fingerprint_now { | ||
| 83 | my $self = shift; | ||
| 84 | my @stat = Time::HiRes::stat($self->file) or return undef; | ||
| 85 | return join ':', @stat[0, 1, 7, 9]; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _read_file { | ||
| 89 | my $self = shift; | ||
| 90 | 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 | my $fingerprint = $self->_fingerprint_now; | ||
| 97 | |||
| 98 | my $token = eval { path($file)->slurp_raw }; | ||
| 99 | croak 'Cannot read the ' . $self->description . " ($file): $@" | ||
| 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 | $token =~ s/\A\s+//; | ||
| 105 | $token =~ s/\s+\z//; | ||
| 106 | croak 'The ' . $self->description . " ($file) holds no token" | ||
| 107 | unless length $token; | ||
| 108 | |||
| 109 | $self->_token($token); | ||
| 110 | $self->_fingerprint($fingerprint); | ||
| 111 | |||
| 112 | return $token; | ||
| 113 | } | ||
| 114 | |||
| 115 | 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 |