lib/Kubernetes/REST/Kubeconfig.pm

filter: Statementstmt 100% 194 / 194 Branchbran 93.6% 88 / 94 Conditioncond 76.4% 52 / 68 MC/DCmcdc 53.6% 22 / 41 Subroutinesub 100% 34 / 34 Podpod 100% 6 / 6 totaltotal 90.6% 396 / 437 CC 86 SCAR 19.7
CC
86
Cov
94%
api
17.1
_merge_configs
13.2
_resolve_file_references
11.2
CRAP 7.2
« blib/lib/Kubernetes/REST/CLI/Cmd/Raw.pm ↑ Summary lib/Kubernetes/REST/HTTPTinyIO.pm »
1 package Kubernetes::REST::Kubeconfig;
2 # ABSTRACT: Parse kubeconfig files and create Kubernetes::REST instances
3 37 our $VERSION = '1.110';
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 Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 37 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 37 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 37 ▶use Kubernetes::REST;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 37 ▶use Kubernetes::REST::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 37 ▶use Kubernetes::REST::AuthToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 37 ▶use Kubernetes::REST::AuthTokenFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 37 ▶use Kubernetes::REST::AuthExec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 37 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 has kubeconfig_path => (
19 is => 'ro',
20 16 ▶ default => sub { $_[0]->_default_kubeconfig_path },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 37 );
22  
23  
24 16 ▶sub _default_kubeconfig_path {
Subroutine _default_kubeconfig_path: called CC=5 SCAR=16.1
25 16 my $self = shift;
26 16 ▶ return $ENV{KUBECONFIG} if defined $ENV{KUBECONFIG} and length $ENV{KUBECONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"KUBECONFIG"} and length $ENV{"KUBECONFIG"} 3 13 16
Condition100%3/3
defined $ENV{"KUBECONFIG"} and length $ENV{"KUBECONFIG"}
!ll&&!rl&&r
1213
MC/DC100%2/2
defined $ENV{"KUBECONFIG"} and length $ENV{"KUBECONFIG"}
defined $ENV{"KUBECONFIG"} length $ENV{"KUBECONFIG"}
Truth table100%3/3
A and B
A= defined $ENV{"KUBECONFIG"}B= length $ENV{"KUBECONFIG"}
ABresult
0X0
100
111
27 13 ▶ return undef unless defined $ENV{HOME} and length $ENV{HOME};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"HOME"} and length $ENV{"HOME"} 3 10 13
Condition66%2/3
defined $ENV{"HOME"} and length $ENV{"HOME"}
!ll&&!rl&&r
3010
MC/DC50%1/2
defined $ENV{"HOME"} and length $ENV{"HOME"}
defined $ENV{"HOME"} length $ENV{"HOME"}
Truth table66%2/3
A and B
A= defined $ENV{"HOME"}B= length $ENV{"HOME"}
ABresult
0X0
100
111
28 10 return "$ENV{HOME}/.kube/config";
29 }
30  
31 37 ▶my $PATH_SEP = $Config::Config{path_sep} || ':';
Condition50%1/2
$Config::Config{'path_sep'} || ':'
l!l
370
MC/DC0%0/1
$Config::Config{'path_sep'} || ':'
$Config::Config{'path_sep'}
Truth table50%1/2
A
A= $Config::Config{'path_sep'}
Aresult
11
00
32  
33 has kubeconfig_paths => (
34 is => 'lazy',
35 init_arg => undef,
36 68 ▶ builder => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
Subroutine _build_kubeconfig_paths: called CC=4 SCAR=14.0
37 68 my $self = shift;
38 68 my $path = $self->kubeconfig_path;
39 68 ▶ return [] unless defined $path;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $path 0 68 68
unless defined $path 3 94 97
40 68 ▶ my @entries = ref $path eq 'ARRAY' ? @$path : split /\Q$PATH_SEP\E/, $path, -1;
Branch: 2 branches
BranchTrueFalseTotal
ref $path eq 'ARRAY' ? : 2 66 68
ref $path eq 'ARRAY' ? : 3 91 94
41 68 ▶ return [ grep { defined and length } @entries ];
Condition50%3/6
defined $_ && length $_
!ll&&!rl&&r
0070
defined $_ && length $_
!ll&&!rl&&r
03112
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
42 },
43 37 );
44  
45  
46 37 has context_name => (
47 is => 'ro',
48 predicate => 1,
49 );
50  
51  
52 has refresh_token_files => (
53 is => 'ro',
54 173 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 37 );
56  
57  
58 has _config => (
59 is => 'lazy',
60 163 ▶ builder => sub {
Subroutine _build__config: called CC=5 SCAR=16.1
61 163 my $self = shift;
62 163 my @configs;
63 163 for my $path (@{ $self->kubeconfig_paths }) {
64 # A file the list names but that is not there is not an error, the
65 # same way it is not one for kubectl.
66 180 ▶ next unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 7 173 180
67 173 my $config = YAML::XS::LoadFile($path);
68 173 ▶ next unless ref $config eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 0 173 173
69 # While the file is being read is the only place where its own
70 # directory is still known: the merge below keeps whole entries but
71 # not where they came from, and with several files merged that
72 # directory differs per entry.
73 173 push @configs, $self->_resolve_file_references($config, $path);
74 }
75 163 ▶ return undef unless @configs;
Branch: 1 branch
BranchTrueFalseTotal
unless @configs 4 159 163
76 159 return $self->_merge_configs(@configs);
77 },
78 37 );
79  
80 # The file references a kubeconfig entry can hold, per section: the key of the
81 # entry body, and the fields in it that name a file. The *-data twins hold
82 # base64 content rather than a path and are none of this code's business, and
83 # neither is an exec plugin's command, which is looked up in PATH.
84 37 my %FILE_REFERENCES = (
85 clusters => { body => 'cluster', fields => ['certificate-authority'] },
86 users => { body => 'user', fields => [qw(client-certificate client-key tokenFile)] },
87 );
88  
89 173 ▶sub _resolve_file_references {
Subroutine _resolve_file_references: called CC=11 SCAR=24.2
90 173 my ($self, $config, $file) = @_;
91  
92 # Absolute, so the result survives a later chdir - the file itself may well
93 # have been named relatively.
94 173 my $dir = path($file)->absolute->parent;
95  
96 173 for my $section (sort keys %FILE_REFERENCES) {
97 346 my $spec = $FILE_REFERENCES{$section};
98 346 ▶ for my $entry (@{ $config->{$section} // [] }) {
Condition100%2/2
$config->{$section} // []
l!l
33016
MC/DC100%1/1
$config->{$section} // []
$config->{$section}
Truth table100%2/2
A
A= $config->{$section}
Aresult
11
00
99 895 ▶ next unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 895 895
100 895 my $body = $entry->{ $spec->{body} };
101 895 ▶ next unless ref $body eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 0 895 895
102 895 for my $field (@{ $spec->{fields} }) {
103 2167 my $value = $body->{$field};
104 2167 ▶ next unless defined $value and !ref $value and length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and length $value 1715 452 2167
Condition66%4/6
defined $value and not ref $value and length $value
!ll&&!rl&&r
17150452
defined $value and not ref $value
!ll&&!rl&&r
17150452
MC/DC33%1/3
defined $value and not ref $value and length $value
defined $value not ref $value length $value
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value
ABCresult
0XX0
10X0
1100
1111
105 452 ▶ next if path($value)->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
if path($value)->is_absolute 92 360 452
106 360 $body->{$field} = $dir->child($value)->stringify;
107 }
108 }
109 }
110  
111 173 return $config;
112 }
113  
114 # The sections keyed by name, which are unioned; everything else is a plain
115 # top-level key taken from the first file that has it.
116 37 my @NAMED_SECTIONS = qw(clusters contexts users);
117 37 my %IS_NAMED_SECTION = map { $_ => 1 } @NAMED_SECTIONS;
118  
119 159 ▶sub _merge_configs {
Subroutine _merge_configs: called CC=13 SCAR=25.8
120 159 my ($self, @configs) = @_;
121  
122 159 my (%merged, %seen);
123 159 for my $config (@configs) {
124 173 for my $section (@NAMED_SECTIONS) {
125 519 ▶ for my $entry (@{ $config->{$section} // [] }) {
Condition100%2/2
$config->{$section} // []
l!l
49623
MC/DC100%1/1
$config->{$section} // []
$config->{$section}
Truth table100%2/2
A
A= $config->{$section}
Aresult
11
00
126 1505 ▶ next unless ref $entry eq 'HASH' and defined $entry->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" and defined $entry->{"name"} 0 1505 1505
Condition33%1/3
ref $entry eq "HASH" and defined $entry->{"name"}
!ll&&!rl&&r
001505
MC/DC0%0/2
ref $entry eq "HASH" and defined $entry->{"name"}
ref $entry eq "HASH" defined $entry->{"name"}
Truth table33%1/3
A and B
A= ref $entry eq "HASH"B= defined $entry->{"name"}
ABresult
0X0
100
111
127 1505 ▶ next if $seen{$section}{ $entry->{name} }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$section}{$entry->{"name"}}++ 31 1474 1505
128 1474 push @{ $merged{$section} }, $entry;
129 }
130 }
131 173 for my $key (grep { !$IS_NAMED_SECTION{$_} } keys %$config) {
132 522 ▶ next if exists $merged{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $merged{$key} 40 482 522
133 482 my $value = $config->{$key};
134 482 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 482 482
135 482 ▶ next if !ref $value and !length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value or length $value 12 470 482
Condition100%3/3
ref $value or length $value
l!l&&r!l&&!r
746312
MC/DC100%2/2
ref $value or length $value
ref $value length $value
Truth table100%3/3
A or B
A= ref $valueB= length $value
ABresult
1X1
011
000
136 470 $merged{$key} = $value;
137 }
138 }
139  
140 159 return \%merged;
141 }
142  
143 22 ▶sub _kubeconfig_paths_string {
Subroutine _kubeconfig_paths_string: called CC=2 SCAR=6.9
144 22 my $self = shift;
145 22 my $paths = $self->kubeconfig_paths;
146 22 ▶ return join($PATH_SEP, @$paths) if @$paths;
Branch: 1 branch
BranchTrueFalseTotal
if @$paths 16 6 22
147 6 return 'neither KUBECONFIG nor HOME is set';
148 }
149  
150 9 ▶sub _not_found_error {
Subroutine _not_found_error: called CC=1 SCAR=0.0
151 9 my $self = shift;
152 9 return 'Kubeconfig not found: ' . $self->_kubeconfig_paths_string;
153 }
154  
155 # The two ways a lookup fails before a single name is compared: the kubeconfig
156 # defines no such section at all, or there is no name to look for. Both used to
157 # reach _find_by_name, which compared against undef and croaked "not found: "
158 # with nothing behind the colon - an unusable kubeconfig, described in a way
159 # nobody can act on.
160 400 ▶sub _assert_lookup {
Subroutine _assert_lookup: called CC=5 SCAR=16.2
161 400 my ($self, $config, $section, $name, $missing_name_error) = @_;
162 400 my $entries = $config->{$section};
163 400 ▶ croak "kubeconfig defines no $section: " . $self->_kubeconfig_paths_string
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entries eq "ARRAY" and @$entries 7 393 400
Condition66%2/3
ref $entries eq "ARRAY" and @$entries
!ll&&!rl&&r
70393
MC/DC50%1/2
ref $entries eq "ARRAY" and @$entries
ref $entries eq "ARRAY" @$entries
Truth table66%2/3
A and B
A= ref $entries eq "ARRAY"B= @$entries
ABresult
0X0
100
111
164 unless ref $entries eq 'ARRAY' and @$entries;
165 393 ▶ croak "$missing_name_error: " . $self->_kubeconfig_paths_string
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 6 387 393
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
60387
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
166 unless defined $name and length $name;
167 387 return;
168 }
169  
170 101 ▶sub current_context_name {
Subroutine current_context_name: called CC=3 SCAR=11.0
Pod documented
171 101 my $self = shift;
172  
173  
174 101 ▶ return $self->context_name if $self->has_context_name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_context_name 5 96 101
175 96 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 95 1 96
176 or croak $self->_not_found_error;
177 95 return $config->{'current-context'};
178 }
179  
180 10 ▶sub contexts {
Subroutine contexts: called CC=3 SCAR=11.0
Pod documented
181 10 my $self = shift;
182  
183  
184 10 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 6 4 10
185 or croak $self->_not_found_error;
186 6 ▶ return [ map { $_->{name} } @{$config->{contexts} // []} ];
Condition100%2/2
$config->{"contexts"} // []
l!l
51
MC/DC100%1/1
$config->{"contexts"} // []
$config->{"contexts"}
Truth table100%2/2
A
A= $config->{"contexts"}
Aresult
11
00
187 }
188  
189 387 ▶sub _find_by_name {
Subroutine _find_by_name: called CC=4 SCAR=13.9
190 387 my ($self, $list, $name) = @_;
191 387 ▶ for my $item (@{$list // []}) {
Condition50%1/2
$list // []
l!l
3870
MC/DC0%0/1
$list // []
$list
Truth table50%1/2
A
A= $list
Aresult
11
00
192 706 ▶ return $item if $item->{name} eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"name"} eq $name 381 325 706
193 }
194 6 return undef;
195 }
196  
197 131 ▶sub context {
Subroutine context: called CC=4 SCAR=13.9
Pod documented
198 131 my ($self, $name) = @_;
199  
200  
201 131 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 130 1 131
202 or croak $self->_not_found_error;
203 130 ▶ $name //= $self->current_context_name;
Condition100%3/3
$name //= $self->current_context_name
l!l&&r!l&&!r
12217
MC/DC100%2/2
$name //= $self->current_context_name
$name $self->current_context_name
Truth table100%3/3
A //= B
A= $nameB= $self->current_context_name
ABresult
1X1
011
000
204 130 $self->_assert_lookup($config, 'contexts', $name,
205 'kubeconfig has no current-context set');
206 122 ▶ my $ctx = $self->_find_by_name($config->{contexts}, $name)
Branch: 1 branch
BranchTrueFalseTotal
my $ctx = $self->_find_by_name($config->{"contexts"}, $name) or croak("Context not found: $name") 120 2 122
207 or croak "Context not found: $name";
208 120 return $ctx->{context};
209 }
210  
211 140 ▶sub cluster {
Subroutine cluster: called CC=3 SCAR=11.0
Pod documented
212 140 my ($self, $name) = @_;
213  
214  
215 140 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 139 1 140
216 or croak $self->_not_found_error;
217 139 $self->_assert_lookup($config, 'clusters', $name, 'no cluster name given');
218 136 ▶ my $cluster = $self->_find_by_name($config->{clusters}, $name)
Branch: 1 branch
BranchTrueFalseTotal
my $cluster = $self->_find_by_name($config->{"clusters"}, $name) or croak("Cluster not found: $name") 134 2 136
219 or croak "Cluster not found: $name";
220 134 return $cluster->{cluster};
221 }
222  
223 132 ▶sub user {
Subroutine user: called CC=3 SCAR=11.0
Pod documented
224 132 my ($self, $name) = @_;
225  
226  
227 132 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 131 1 132
228 or croak $self->_not_found_error;
229 131 $self->_assert_lookup($config, 'users', $name, 'no user name given');
230 129 ▶ my $user = $self->_find_by_name($config->{users}, $name)
Branch: 1 branch
BranchTrueFalseTotal
my $user = $self->_find_by_name($config->{"users"}, $name) or croak("User not found: $name") 127 2 129
231 or croak "User not found: $name";
232 127 return $user->{user};
233 }
234  
235 345 ▶sub _resolve_cert {
Subroutine _resolve_cert: called CC=3 SCAR=11.0
236 345 my ($self, $hash, $key) = @_;
237  
238 345 my $data_key = "${key}-data";
239 345 ▶ if (my $data = $hash->{$data_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $hash->{$data_key}) 7 338 345
240 7 return (pem => decode_base64($data));
241 }
242  
243 338 ▶ if (my $file = $hash->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $hash->{$key}) 20 318 338
244 20 return (file => $file);
245 }
246  
247 318 return ();
248 }
249  
250 119 ▶sub api {
Subroutine api: called CC=17 SCAR=28.4
Pod documented
251 119 my ($self, $context_name) = @_;
252  
253  
254 # If no kubeconfig, try in-cluster
255 119 ▶ unless ($self->_config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_config) 1 118 119
256 1 ▶ return $self->_in_cluster_api
Condition50%1/2
$self->_in_cluster_api // croak($self->_not_found_error . ' and not running in-cluster')
l!l
01
MC/DC0%0/1
$self->_in_cluster_api // croak($self->_not_found_error . ' and not running in-cluster')
$self->_in_cluster_api
Truth table50%1/2
A
A= $self->_in_cluster_api
Aresult
11
00
257 // croak $self->_not_found_error . " and not running in-cluster";
258 }
259  
260 118 ▶ $context_name //= $self->current_context_name;
Condition100%3/3
$context_name //= $self->current_context_name
l!l&&r!l&&!r
39772
MC/DC100%2/2
$context_name //= $self->current_context_name
$context_name $self->current_context_name
Truth table100%3/3
A //= B
A= $context_nameB= $self->current_context_name
ABresult
1X1
011
000
261 118 my $ctx = $self->context($context_name);
262 116 my $cluster = $self->cluster($ctx->{cluster});
263 115 my $user = $self->user($ctx->{user});
264  
265 # Build server config
266 my %server = (
267 endpoint => $cluster->{server},
268 115 );
269  
270 115 ▶ if (my %ca = $self->_resolve_cert($cluster, 'certificate-authority')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%ca) = $self->_resolve_cert($cluster, "certificate-authority")) 11 104 115
271 11 ▶ $server{ $ca{pem} ? 'ssl_ca_pem' : 'ssl_ca_file' } = $ca{pem} // $ca{file};
Branch: 1 branch
BranchTrueFalseTotal
$ca{'pem'} ? : 5 6 11
Condition66%2/3
$ca{"pem"} // $ca{"file"}
l!l&&r!l&&!r
560
MC/DC0%0/2
$ca{"pem"} // $ca{"file"}
$ca{"pem"} $ca{"file"}
Truth table66%2/3
A // B
A= $ca{"pem"}B= $ca{"file"}
ABresult
1X1
011
000
272 }
273  
274 115 ▶ if ($cluster->{'insecure-skip-tls-verify'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cluster->{'insecure-skip-tls-verify'}) { } 104 11 115
275 104 $server{ssl_verify_server} = 0;
276 } else {
277 11 $server{ssl_verify_server} = 1;
278 }
279  
280 115 ▶ if (my %cert = $self->_resolve_cert($user, 'client-certificate')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%cert) = $self->_resolve_cert($user, "client-certificate")) 8 107 115
281 8 ▶ $server{ $cert{pem} ? 'ssl_cert_pem' : 'ssl_cert_file' } = $cert{pem} // $cert{file};
Branch: 1 branch
BranchTrueFalseTotal
$cert{'pem'} ? : 1 7 8
Condition66%2/3
$cert{"pem"} // $cert{"file"}
l!l&&r!l&&!r
170
MC/DC0%0/2
$cert{"pem"} // $cert{"file"}
$cert{"pem"} $cert{"file"}
Truth table66%2/3
A // B
A= $cert{"pem"}B= $cert{"file"}
ABresult
1X1
011
000
282 }
283  
284 115 ▶ if (my %key = $self->_resolve_cert($user, 'client-key')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%key) = $self->_resolve_cert($user, "client-key")) 8 107 115
285 8 ▶ $server{ $key{pem} ? 'ssl_key_pem' : 'ssl_key_file' } = $key{pem} // $key{file};
Branch: 1 branch
BranchTrueFalseTotal
$key{'pem'} ? : 1 7 8
Condition66%2/3
$key{"pem"} // $key{"file"}
l!l&&r!l&&!r
170
MC/DC0%0/2
$key{"pem"} // $key{"file"}
$key{"pem"} $key{"file"}
Truth table66%2/3
A // B
A= $key{"pem"}B= $key{"file"}
ABresult
1X1
011
000
286 }
287  
288 # Build credentials
289 115 my $credentials;
290 115 ▶ if (my $token = $user->{token}) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $token = $user->{'token'}) { } 25 90 115
elsif (my $token_file = $user->{'tokenFile'}) { } 29 61 90
elsif (my $exec = $user->{'exec'}) { } 51 10 61
291 25 $credentials = Kubernetes::REST::AuthToken->new(token => $token);
292 } elsif (my $token_file = $user->{tokenFile}) {
293 29 $credentials = $self->_token_file_credential(
294 $token_file, "tokenFile of user '$ctx->{user}'"
295 );
296 } elsif (my $exec = $user->{exec}) {
297 51 $credentials = $self->_exec_credential($exec);
298 } else {
299 # No token auth, might be using client certs only
300 10 $credentials = Kubernetes::REST::AuthToken->new(token => '');
301 }
302  
303 90 return Kubernetes::REST->new(
304 server => Kubernetes::REST::Server->new(%server),
305 credentials => $credentials,
306 );
307 }
308  
309 # Credentials for a file holding a bearer token: a user's tokenFile, or the
310 # service account token mounted into a pod. Both are written by something else
311 # - the kubelet, a login helper - and both are rotated under a running process,
312 # so both get credentials that notice.
313 36 ▶sub _token_file_credential {
Subroutine _token_file_credential: called CC=2 SCAR=6.9
314 36 my ($self, $file, $what) = @_;
315  
316 36 ▶ return Kubernetes::REST::AuthTokenFile->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->refresh_token_files ? : 34 2 36
317 file => $file,
318 description => $what,
319 refresh => $self->refresh_token_files ? 1 : 0,
320 );
321 }
322  
323 51 ▶sub _exec_credential {
Subroutine _exec_credential: called CC=3 SCAR=11.0
324 51 my ($self, $exec) = @_;
325  
326 return Kubernetes::REST::AuthExec->new(
327 command => $exec->{command},
328 args => $exec->{args} // [],
329 51 ▶ env => $exec->{env} // [],
Condition100%4/4
$exec->{"args"} // []
l!l
1932
$exec->{"env"} // []
l!l
645
MC/DC100%1/1
$exec->{"args"} // []
$exec->{"args"}
MC/DC100%1/1
$exec->{"env"} // []
$exec->{"env"}
Truth table100%2/2
A
A= $exec->{"args"}
Aresult
11
00
Truth table100%2/2
A
A= $exec->{"env"}
Aresult
11
00
330 );
331 }
332  
333 37 our $SA_TOKEN = '/var/run/secrets/kubernetes.io/serviceaccount/token';
334 37 our $SA_CA = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt';
335  
336 7 ▶sub _in_cluster_api {
Subroutine _in_cluster_api: called CC=6 SCAR=17.9
337 7 my ($self) = @_;
338 7 ▶ return undef unless -f $SA_TOKEN;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $Kubernetes::REST::Kubeconfig::SA_TOKEN 2 5 7
339  
340 5 ▶ my $host = $ENV{KUBERNETES_SERVICE_HOST} // 'kubernetes.default.svc';
Condition100%2/2
$ENV{"KUBERNETES_SERVICE_HOST"} // "kubernetes.default.svc"
l!l
41
MC/DC100%1/1
$ENV{"KUBERNETES_SERVICE_HOST"} // "kubernetes.default.svc"
$ENV{"KUBERNETES_SERVICE_HOST"}
Truth table100%2/2
A
A= $ENV{"KUBERNETES_SERVICE_HOST"}
Aresult
11
00
341 5 ▶ my $port = $ENV{KUBERNETES_SERVICE_PORT} // '443';
Condition100%2/2
$ENV{"KUBERNETES_SERVICE_PORT"} // 443
l!l
23
MC/DC100%1/1
$ENV{"KUBERNETES_SERVICE_PORT"} // 443
$ENV{"KUBERNETES_SERVICE_PORT"}
Truth table100%2/2
A
A= $ENV{"KUBERNETES_SERVICE_PORT"}
Aresult
11
00
342 5 ▶ $host = "[$host]" if $host =~ /:/ && $host !~ /^\[/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /:/ and not $host =~ /^\[/ 1 4 5
Condition100%3/3
$host =~ /:/ and not $host =~ /^\[/
!ll&&!rl&&r
311
MC/DC100%2/2
$host =~ /:/ and not $host =~ /^\[/
$host =~ /:/ not $host =~ /^\[/
Truth table100%3/3
A and not(B)
A= $host =~ /:/B= not $host =~ /^\[/
ABresult
0X0
100
111
343  
344 5 return Kubernetes::REST->new(
345 server => Kubernetes::REST::Server->new(
346 endpoint => "https://$host:$port",
347 ssl_ca_file => $SA_CA,
348 ssl_verify_server => 1,
349 ),
350 credentials => $self->_token_file_credential(
351 $SA_TOKEN, 'service account token'
352 ),
353 );
354 }
355  
356 37 1;
357  
358 __END__
« blib/lib/Kubernetes/REST/CLI/Cmd/Raw.pm ↑ Summary lib/Kubernetes/REST/HTTPTinyIO.pm »