| 1 | package Kubernetes::REST::Kubeconfig; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Parse kubeconfig files and create Kubernetes::REST instances | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 40 | our $VERSION = '1.113'; | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Config (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 40 | ▶ | use YAML::XS (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use MIME::Base64 qw(decode_base64); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use Kubernetes::REST; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use Kubernetes::REST::Server; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use Kubernetes::REST::AuthToken; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 40 | ▶ | use Kubernetes::REST::AuthTokenFile; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 40 | ▶ | use Kubernetes::REST::AuthExec; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 40 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | has kubeconfig_path => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 28 | ▶ | default => sub { $_[0]->_default_kubeconfig_path }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 40 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 28 | ▶ | sub _default_kubeconfig_path { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_default_kubeconfig_path: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 28 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | 28 | ▶ | if (defined $ENV{KUBECONFIG} and length $ENV{KUBECONFIG}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ENV{"KUBECONFIG"} and length $ENV{"KUBECONFIG"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 15 | $self->_set_kubeconfig_path_from_env(1); | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 15 | return $ENV{KUBECONFIG}; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 13 | ▶ | return undef unless defined $ENV{HOME} and length $ENV{HOME}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ENV{"HOME"} and length $ENV{"HOME"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 10 | return "$ENV{HOME}/.kube/config"; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # Whether kubeconfig_path is the KUBECONFIG environment variable, for the one | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # message that has to say where a value came from. Only the default knows: once | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # constructed, an explicit kubeconfig_path => $ENV{KUBECONFIG} looks the same. | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 40 | has _kubeconfig_path_from_env => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | writer => '_set_kubeconfig_path_from_env', | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 40 | ▶ | my $PATH_SEP = $Config::Config{path_sep} || ':'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$Config::Config{'path_sep'} || ':'
MC/DC0%0/1
$Config::Config{'path_sep'} || ':'
$Config::Config{'path_sep'}
Truth table50%1/2
A
A= $Config::Config{'path_sep'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # The kubeconfig content where its file name belongs - kubeconfig_path => $yaml, | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # KUBECONFIG="$(cat config)". No file name a caller means holds a line break or | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # a NUL, and none opens like a JSON or YAML document. Stat-ing such a value | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # warns, and naming it in "Kubeconfig not found" put every token and key of the | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # kubeconfig into the logs. | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 973 | ▶ | sub _is_document { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_document: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 973 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | 973 | ▶ | return $value =~ /[\n\r\0]/ || $value =~ /\A\s*(?:[{\[]|---)/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$value =~ /[\n\r\0]/ || $value =~ /\A\s*(?:[{\[]|---)/
MC/DC100%2/2
$value =~ /[\n\r\0]/ || $value =~ /\A\s*(?:[{\[]|---)/
$value =~ /[\n\r\0]/
$value =~ /\A\s*(?:[{\[]|---)/
Truth table100%3/3
A || B
A= $value =~ /[\n\r\0]/B= $value =~ /\A\s*(?:[{\[]|---)/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | has kubeconfig_paths => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 125 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_kubeconfig_paths: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 125 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 125 | my $path = $self->kubeconfig_path; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | 125 | ▶ | return [] unless defined $path; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 122 | ▶ | my @entries = ref $path eq 'ARRAY' ? @$path : split /\Q$PATH_SEP\E/, $path, -1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # Per entry, so a document appended to a list ("path:{...}") is caught | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # too. The split leaves line breaks and NULs where they were, so for | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # those this is the same as looking at the whole value. | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 122 | ▶ | if (grep { defined and $self->_is_document($_) } @entries) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $_ && $self->_is_document($_)
defined $_ && $self->_is_document($_)
MC/DC50%1/2
defined $_ && $self->_is_document($_)
defined $_
$self->_is_document($_)
MC/DC0%0/2
defined $_ && $self->_is_document($_)
defined $_
$self->_is_document($_)
Truth table66%2/3
A && B
A= defined $_B= $self->_is_document($_)
Truth table33%1/3
A && B
A= defined $_B= $self->_is_document($_)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # Reached through lazy builders (api -> _config -> here): trusting | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # Moo's generated accessors for this croak names the caller's line, | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # not one in this file. Names the option or the variable, never the | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # value: the value is the secret. | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 41 | local our @CARP_NOT = ('Method::Generate::Accessor::_Generated'); | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 41 | ▶ | croak 'kubeconfig expects a path, got a document in ' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | . ($self->_kubeconfig_path_from_env | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | ? 'the environment variable KUBECONFIG' : 'kubeconfig_path') | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | . ' - pass the name of a kubeconfig file, not its content'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 81 | ▶ | return [ grep { defined and length } @entries ]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
defined $_ && length $_
defined $_ && length $_
MC/DC50%1/2
defined $_ && length $_
defined $_
length $_
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 40 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 40 | has context_name => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | predicate => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | has refresh_token_files => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 228 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 40 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | has _config => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 73 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Subroutine
_build__config: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 73 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 73 | my @configs; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 73 | for my $path (@{ $self->kubeconfig_paths }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | # A file the list names but that is not there is not an error, the | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # same way it is not one for kubectl. | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 87 | ▶ | next unless -f $path; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 81 | my $config = YAML::XS::LoadFile($path); | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 81 | ▶ | next unless ref $config eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # While the file is being read is the only place where its own | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # directory is still known: the merge below keeps whole entries but | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # not where they came from, and with several files merged that | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # directory differs per entry. | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 81 | push @configs, $self->_resolve_file_references($config, $path); | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 73 | ▶ | return undef unless @configs; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 69 | return $self->_merge_configs(@configs); | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 40 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # The file references a kubeconfig entry can hold, per section: the key of the | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # entry body, and the fields in it that name a file. The *-data twins hold | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # base64 content rather than a path and are none of this code's business, and | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # neither is an exec plugin's command, which is looked up in PATH. | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 40 | my %FILE_REFERENCES = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | clusters => { body => 'cluster', fields => ['certificate-authority'] }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | users => { body => 'user', fields => [qw(client-certificate client-key tokenFile)] }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 181 | ▶ | sub _resolve_file_references { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_resolve_file_references: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 181 | my ($self, $config, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # Absolute, so the result survives a later chdir - the file itself may well | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # have been named relatively. | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 181 | my $dir = path($file)->absolute->parent; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 181 | for my $section (sort keys %FILE_REFERENCES) { | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | 362 | my $spec = $FILE_REFERENCES{$section}; | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | 362 | ▶ | for my $entry (@{ $config->{$section} // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{$section} // []
MC/DC100%1/1
$config->{$section} // []
$config->{$section}
Truth table100%2/2
A
A= $config->{$section}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 923 | ▶ | next unless ref $entry eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 923 | my $body = $entry->{ $spec->{body} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | 923 | ▶ | next unless ref $body eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 923 | for my $field (@{ $spec->{fields} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | 2223 | my $value = $body->{$field}; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | 2223 | ▶ | next unless defined $value and !ref $value and length $value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $value and not ref $value and length $value
defined $value and not ref $value
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 452 | ▶ | next if path($value)->is_absolute; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 360 | $body->{$field} = $dir->child($value)->stringify; | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 181 | return $config; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # The sections keyed by name, which are unioned; everything else is a plain | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | # top-level key taken from the first file that has it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 40 | my @NAMED_SECTIONS = qw(clusters contexts users); | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | 40 | my %IS_NAMED_SECTION = map { $_ => 1 } @NAMED_SECTIONS; | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 167 | ▶ | sub _merge_configs { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merge_configs: called CC=13 SCAR=25.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 167 | my ($self, @configs) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 167 | my (%merged, %seen); | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | 167 | for my $config (@configs) { | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | 181 | for my $section (@NAMED_SECTIONS) { | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | 543 | ▶ | for my $entry (@{ $config->{$section} // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{$section} // []
MC/DC100%1/1
$config->{$section} // []
$config->{$section}
Truth table100%2/2
A
A= $config->{$section}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1547 | ▶ | next unless ref $entry eq 'HASH' and defined $entry->{name}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $entry eq "HASH" and defined $entry->{"name"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 1547 | ▶ | next if $seen{$section}{ $entry->{name} }++; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1516 | push @{ $merged{$section} }, $entry; | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 181 | for my $key (grep { !$IS_NAMED_SECTION{$_} } keys %$config) { | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 546 | ▶ | next if exists $merged{$key}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 506 | my $value = $config->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 506 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 506 | ▶ | next if !ref $value and !length $value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $value or length $value
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 494 | $merged{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 167 | return \%merged; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 23 | ▶ | sub _kubeconfig_paths_string { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_kubeconfig_paths_string: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 181 | 23 | my $paths = $self->kubeconfig_paths; | |||||||||||||||||||||||||||||||||||||||||||||
| 182 | 23 | ▶ | return join($PATH_SEP, @$paths) if @$paths; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 6 | return 'neither KUBECONFIG nor HOME is set'; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 10 | ▶ | sub _not_found_error { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_not_found_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 188 | 10 | return 'Kubeconfig not found: ' . $self->_kubeconfig_paths_string; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # The two ways a lookup fails before a single name is compared: the kubeconfig | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # defines no such section at all, or there is no name to look for. Both used to | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # reach _find_by_name, which compared against undef and croaked "not found: " | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # with nothing behind the colon - an unusable kubeconfig, described in a way | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # nobody can act on. | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 418 | ▶ | sub _assert_lookup { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_lookup: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 418 | my ($self, $config, $section, $name, $missing_name_error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | 418 | my $entries = $config->{$section}; | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 418 | ▶ | croak "kubeconfig defines no $section: " . $self->_kubeconfig_paths_string | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $entries eq "ARRAY" and @$entries
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | unless ref $entries eq 'ARRAY' and @$entries; | ||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 411 | ▶ | croak "$missing_name_error: " . $self->_kubeconfig_paths_string | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and length $name
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | unless defined $name and length $name; | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 405 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 111 | ▶ | sub current_context_name { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
current_context_name: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 111 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 111 | ▶ | return $self->context_name if $self->has_context_name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 106 | ▶ | my $config = $self->_config | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | or croak $self->_not_found_error; | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 100 | return $config->{'current-context'}; | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 23 | ▶ | sub contexts { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
contexts: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 23 | ▶ | my $config = $self->_config | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | or croak $self->_not_found_error; | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 8 | ▶ | return [ map { $_->{name} } @{$config->{contexts} // []} ]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"contexts"} // []
MC/DC100%1/1
$config->{"contexts"} // []
$config->{"contexts"}
Truth table100%2/2
A
A= $config->{"contexts"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 405 | ▶ | sub _find_by_name { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_by_name: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 405 | my ($self, $list, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | 405 | ▶ | for my $item (@{$list // []}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$list // []
MC/DC0%0/1
$list // []
$list
Truth table50%1/2
A
A= $list
| |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 730 | ▶ | return $item if $item->{name} eq $name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 6 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 137 | ▶ | sub context { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
context: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 137 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 137 | ▶ | my $config = $self->_config | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | or croak $self->_not_found_error; | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 136 | ▶ | $name //= $self->current_context_name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$name //= $self->current_context_name
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 136 | $self->_assert_lookup($config, 'contexts', $name, | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | 'kubeconfig has no current-context set'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 128 | ▶ | my $ctx = $self->_find_by_name($config->{contexts}, $name) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | or croak "Context not found: $name"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 126 | return $ctx->{context}; | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 146 | ▶ | sub cluster { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cluster: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 146 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 146 | ▶ | my $config = $self->_config | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | or croak $self->_not_found_error; | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 145 | $self->_assert_lookup($config, 'clusters', $name, 'no cluster name given'); | |||||||||||||||||||||||||||||||||||||||||||||
| 254 | 142 | ▶ | my $cluster = $self->_find_by_name($config->{clusters}, $name) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | or croak "Cluster not found: $name"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 140 | return $cluster->{cluster}; | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 138 | ▶ | sub user { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
user: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 138 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 138 | ▶ | my $config = $self->_config | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | or croak $self->_not_found_error; | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 137 | $self->_assert_lookup($config, 'users', $name, 'no user name given'); | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | 135 | ▶ | my $user = $self->_find_by_name($config->{users}, $name) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 267 | or croak "User not found: $name"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 133 | return $user->{user}; | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 363 | ▶ | sub _resolve_cert { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_resolve_cert: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 363 | my ($self, $hash, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 363 | my $data_key = "${key}-data"; | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | 363 | ▶ | if (my $data = $hash->{$data_key}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 7 | return (pem => decode_base64($data)); | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 356 | ▶ | if (my $file = $hash->{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 20 | return (file => $file); | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 336 | return (); | |||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 144 | ▶ | sub api { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 144 | my ($self, $context_name, @options) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # Checked before the kubeconfig is read: a mistake in the call, not in the | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | # kubeconfig. The message names the key, never the value. | ||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 144 | ▶ | croak 'api() takes a context name and then key/value options,' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | . ' got an odd number of option elements' | ||||||||||||||||||||||||||||||||||||||||||||||
| 294 | if @options % 2; | ||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 143 | my %options = @options; | |||||||||||||||||||||||||||||||||||||||||||||
| 296 | 143 | for my $key (qw(server credentials)) { | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | croak "api() builds $key from the kubeconfig, $key is not an option" | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 285 | ▶ | if exists $options{$key}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # If no kubeconfig, try in-cluster | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 141 | ▶ | unless ($self->_config) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 3 | ▶ | return $self->_in_cluster_api(%options) | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_in_cluster_api(%options) // croak($self->_not_found_error . ' and not running in-cluster')
MC/DC0%0/1
$self->_in_cluster_api(%options) // croak($self->_not_found_error . ' and not running in-cluster')
$self->_in_cluster_api(%options)
Truth table50%1/2
A
A= $self->_in_cluster_api(%options)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 304 | // croak $self->_not_found_error . " and not running in-cluster"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 124 | ▶ | $context_name //= $self->current_context_name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$context_name //= $self->current_context_name
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 124 | my $ctx = $self->context($context_name); | |||||||||||||||||||||||||||||||||||||||||||||
| 309 | 122 | my $cluster = $self->cluster($ctx->{cluster}); | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | 121 | my $user = $self->user($ctx->{user}); | |||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # Build server config | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | my %server = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 314 | endpoint => $cluster->{server}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 121 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 121 | ▶ | if (my %ca = $self->_resolve_cert($cluster, 'certificate-authority')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 11 | ▶ | $server{ $ca{pem} ? 'ssl_ca_pem' : 'ssl_ca_file' } = $ca{pem} // $ca{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ca{"pem"} // $ca{"file"}
MC/DC0%0/2
$ca{"pem"} // $ca{"file"}
$ca{"pem"}
$ca{"file"}
Truth table66%2/3
A // B
A= $ca{"pem"}B= $ca{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 121 | ▶ | if ($cluster->{'insecure-skip-tls-verify'}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 104 | $server{ssl_verify_server} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 323 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 17 | $server{ssl_verify_server} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 121 | ▶ | if (my %cert = $self->_resolve_cert($user, 'client-certificate')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 8 | ▶ | $server{ $cert{pem} ? 'ssl_cert_pem' : 'ssl_cert_file' } = $cert{pem} // $cert{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cert{"pem"} // $cert{"file"}
MC/DC0%0/2
$cert{"pem"} // $cert{"file"}
$cert{"pem"}
$cert{"file"}
Truth table66%2/3
A // B
A= $cert{"pem"}B= $cert{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 121 | ▶ | if (my %key = $self->_resolve_cert($user, 'client-key')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 8 | ▶ | $server{ $key{pem} ? 'ssl_key_pem' : 'ssl_key_file' } = $key{pem} // $key{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key{"pem"} // $key{"file"}
MC/DC0%0/2
$key{"pem"} // $key{"file"}
$key{"pem"}
$key{"file"}
Truth table66%2/3
A // B
A= $key{"pem"}B= $key{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | # Build credentials | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 121 | my $credentials; | |||||||||||||||||||||||||||||||||||||||||||||
| 337 | 121 | ▶ | if (my $token = $user->{token}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 31 | $credentials = Kubernetes::REST::AuthToken->new(token => $token); | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | } elsif (my $token_file = $user->{tokenFile}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 29 | $credentials = $self->_token_file_credential( | |||||||||||||||||||||||||||||||||||||||||||||
| 341 | $token_file, "tokenFile of user '$ctx->{user}'" | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } elsif (my $exec = $user->{exec}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 51 | $credentials = $self->_exec_credential($exec); | |||||||||||||||||||||||||||||||||||||||||||||
| 345 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 346 | # No token auth, might be using client certs only | ||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 10 | $credentials = Kubernetes::REST::AuthToken->new(token => ''); | |||||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 96 | return Kubernetes::REST->new( | |||||||||||||||||||||||||||||||||||||||||||||
| 351 | %options, | ||||||||||||||||||||||||||||||||||||||||||||||
| 352 | server => Kubernetes::REST::Server->new(%server), | ||||||||||||||||||||||||||||||||||||||||||||||
| 353 | credentials => $credentials, | ||||||||||||||||||||||||||||||||||||||||||||||
| 354 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # Credentials for a file holding a bearer token: a user's tokenFile, or the | ||||||||||||||||||||||||||||||||||||||||||||||
| 358 | # service account token mounted into a pod. Both are written by something else | ||||||||||||||||||||||||||||||||||||||||||||||
| 359 | # - the kubelet, a login helper - and both are rotated under a running process, | ||||||||||||||||||||||||||||||||||||||||||||||
| 360 | # so both get credentials that notice. | ||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 38 | ▶ | sub _token_file_credential { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_token_file_credential: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 38 | my ($self, $file, $what) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 38 | ▶ | return Kubernetes::REST::AuthTokenFile->new( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 365 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||||||
| 366 | description => $what, | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | refresh => $self->refresh_token_files ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 368 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 51 | ▶ | sub _exec_credential { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_exec_credential: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 51 | my ($self, $exec) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||
| 374 | return Kubernetes::REST::AuthExec->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 375 | command => $exec->{command}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 376 | args => $exec->{args} // [], | ||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 51 | ▶ | env => $exec->{env} // [], | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%4/4
$exec->{"args"} // []
$exec->{"env"} // []
MC/DC100%1/1
$exec->{"args"} // []
$exec->{"args"}
MC/DC100%1/1
$exec->{"env"} // []
$exec->{"env"}
Truth table100%2/2
A
A= $exec->{"args"}
Truth table100%2/2
A
A= $exec->{"env"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 378 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 40 | our $SA_TOKEN = '/var/run/secrets/kubernetes.io/serviceaccount/token'; | |||||||||||||||||||||||||||||||||||||||||||||
| 382 | 40 | our $SA_CA = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'; | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 9 | ▶ | sub _in_cluster_api { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_in_cluster_api: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 9 | my ($self, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 386 | 9 | ▶ | return undef unless -f $SA_TOKEN; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 7 | ▶ | my $host = $ENV{KUBERNETES_SERVICE_HOST} // 'kubernetes.default.svc'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{"KUBERNETES_SERVICE_HOST"} // "kubernetes.default.svc"
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 7 | ▶ | my $port = $ENV{KUBERNETES_SERVICE_PORT} // '443'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{"KUBERNETES_SERVICE_PORT"} // 443
MC/DC100%1/1
$ENV{"KUBERNETES_SERVICE_PORT"} // 443
$ENV{"KUBERNETES_SERVICE_PORT"}
Truth table100%2/2
A
A= $ENV{"KUBERNETES_SERVICE_PORT"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 7 | ▶ | $host = "[$host]" if $host =~ /:/ && $host !~ /^\[/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$host =~ /:/ and not $host =~ /^\[/
MC/DC100%2/2
$host =~ /:/ and not $host =~ /^\[/
$host =~ /:/
not $host =~ /^\[/
Truth table100%3/3
A and not(B)
A= $host =~ /:/B= not $host =~ /^\[/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 7 | return Kubernetes::REST->new( | |||||||||||||||||||||||||||||||||||||||||||||
| 393 | %options, | ||||||||||||||||||||||||||||||||||||||||||||||
| 394 | server => Kubernetes::REST::Server->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 395 | endpoint => "https://$host:$port", | ||||||||||||||||||||||||||||||||||||||||||||||
| 396 | ssl_ca_file => $SA_CA, | ||||||||||||||||||||||||||||||||||||||||||||||
| 397 | ssl_verify_server => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 398 | ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | credentials => $self->_token_file_credential( | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | $SA_TOKEN, 'service account token' | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 402 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 40 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||
| 407 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||