| 1 | # Copyright 2026 Google LLC and contributors | ||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||
| 4 | # you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||
| 5 | # You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||
| 12 | # See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||
| 13 | # limitations under the License. | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | package Google::Auth::ExternalAccountCredentials; | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 4 | extends 'Google::Auth::Credentials'; | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use URI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | use Google::Auth::Exceptions; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use Google::Auth::RetryHelper; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 4 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 4 | has audience => ( | |||||||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 33 | required => 1, | ||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 4 | has subject_token_type => ( | |||||||||||||||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 38 | required => 1, | ||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 4 | has token_url => ( | |||||||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 43 | required => 1, | ||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 4 | has credential_source => ( | |||||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 48 | required => 1, | ||||||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 4 | has service_account_impersonation_url => ( | |||||||||||||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 53 | required => 0, | ||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 4 | has scope => ( | |||||||||||||||||||||||||||||||
| 57 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 58 | required => 0, | ||||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | has ua => ( | ||||||||||||||||||||||||||||||||
| 62 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 63 | 25 | ▶ | default => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 64 | 25 | my $ua = LWP::UserAgent->new(timeout => 10); | |||||||||||||||||||||||||||||||
| 65 | 25 | $ua->env_proxy; | |||||||||||||||||||||||||||||||
| 66 | 25 | return $ua; | |||||||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||||||
| 68 | 4 | ); | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | has '+_is_universe_pinned' => (default => sub { 0 },); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 58 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.2
| |||||||||||||||||||||||||||||||||
| 73 | 58 | my ($orig, $class, @args) = @_; | |||||||||||||||||||||||||||||||
| 74 | 58 | my $args = $class->$orig(@args); | |||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 58 | my $pinned_universe; | |||||||||||||||||||||||||||||||
| 77 | 58 | ▶ | if (exists $args->{_is_universe_pinned}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 23 | $pinned_universe = $args->{_is_universe_pinned}; | |||||||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||||||
| 80 | 35 | $pinned_universe = exists $args->{universe_domain}; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 58 | ▶ | if (my $json = $args->{json_key}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 6 | ▶ | $args->{audience} //= $json->{audience}; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$args->{"audience"} //= $json->{"audience"}
MC/DC0%0/2
$args->{"audience"} //= $json->{"audience"}
$args->{"audience"}
$json->{"audience"}
Truth table66%2/3
A //= B
A= $args->{"audience"}B= $json->{"audience"}
| |||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | $args->{subject_token_type} //= $json->{subject_token_type}; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$args->{"subject_token_type"} //= $json->{"subject_token_type"}
MC/DC0%0/2
$args->{"subject_token_type"} //= $json->{"subject_token_type"}
$args->{"subject_token_type"}
$json->{"subject_token_type"}
Truth table66%2/3
A //= B
A= $args->{"subject_token_type"}B= $json->{"subject_token_type"}
| |||||||||||||||||||||||||||||||||
| 86 | 6 | ▶ | $args->{token_url} //= $json->{token_url}; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$args->{"token_url"} //= $json->{"token_url"}
MC/DC0%0/2
$args->{"token_url"} //= $json->{"token_url"}
$args->{"token_url"}
$json->{"token_url"}
Truth table66%2/3
A //= B
A= $args->{"token_url"}B= $json->{"token_url"}
| |||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | $args->{credential_source} //= $json->{credential_source}; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$args->{"credential_source"} //= $json->{"credential_source"}
MC/DC0%0/2
$args->{"credential_source"} //= $json->{"credential_source"}
$args->{"credential_source"}
$json->{"credential_source"}
Truth table66%2/3
A //= B
A= $args->{"credential_source"}B= $json->{"credential_source"}
| |||||||||||||||||||||||||||||||||
| 88 | $args->{service_account_impersonation_url} //= | ||||||||||||||||||||||||||||||||
| 89 | 6 | ▶ | $json->{service_account_impersonation_url}; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$args->{"service_account_impersonation_url"} //= $json->{"service_account_impersonation_url"}
MC/DC100%2/2
$args->{"service_account_impersonation_url"} //= $json->{"service_account_impersonation_url"}
$args->{"service_account_impersonation_url"}
$json->{"service_account_impersonation_url"}
Truth table100%3/3
A //= B
A= $args->{"service_account_impersonation_url"}B= $json->{"service_account_impersonation_url"}
| |||||||||||||||||||||||||||||||||
| 90 | 6 | ▶ | $args->{universe_domain} //= $json->{universe_domain}; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$args->{"universe_domain"} //= $json->{"universe_domain"}
MC/DC100%2/2
$args->{"universe_domain"} //= $json->{"universe_domain"}
$args->{"universe_domain"}
$json->{"universe_domain"}
Truth table100%3/3
A //= B
A= $args->{"universe_domain"}B= $json->{"universe_domain"}
| |||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 58 | ▶ | $args->{_is_universe_pinned} = $pinned_universe ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 58 | return $args; | |||||||||||||||||||||||||||||||
| 96 | 4 | }; | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 34 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=17 SCAR=28.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 99 | 34 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 34 | my $source = $self->credential_source; | |||||||||||||||||||||||||||||||
| 102 | 34 | ▶ | if (!defined $source) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 104 | 'Missing credential_source. A \'file\' or \'url\' must be provided.'); | ||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | # Only skip environment_id validation if we are in the AWS subclass | ||||||||||||||||||||||||||||||||
| 108 | 34 | ▶ | if (!$self->isa('Google::Auth::ExternalAccountCredentials::Aws')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | 21 | ▶ | if (exists $source->{environment_id} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $source->{"environment_id"} and defined $source->{"environment_id"}
MC/DC50%1/2
exists $source->{"environment_id"} and defined $source->{"environment_id"}
exists $source->{"environment_id"}
defined $source->{"environment_id"}
Truth table66%2/3
A and B
A= exists $source->{"environment_id"}B= defined $source->{"environment_id"}
| |||||||||||||||||||||||||||||||||
| 110 | && defined $source->{environment_id}) | ||||||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||||||
| 112 | 1 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 113 | 'Invalid Identity Pool credential_source field \'environment_id\''); | ||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | # Pluggable subclass does not require file or url since it executes command | ||||||||||||||||||||||||||||||||
| 118 | 33 | ▶ | if ( !$self->isa('Google::Auth::ExternalAccountCredentials::Pluggable') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->isa('Google::Auth::ExternalAccountCredentials::Pluggable') || $self->isa('Google::Auth::ExternalAccountCredentials::Aws')
MC/DC100%2/2
$self->isa('Google::Auth::ExternalAccountCredentials::Pluggable') || $self->isa('Google::Auth::ExternalAccountCredentials::Aws')
$self->isa('Google::Auth::ExternalAccountCredentials::Pluggable')
$self->isa('Google::Auth::ExternalAccountCredentials::Aws')
Truth table100%3/3
A || B
A= $self->isa('Google::Auth::ExternalAccountCredentials::Pluggable')B= $self->isa('Google::Auth::ExternalAccountCredentials::Aws')
| |||||||||||||||||||||||||||||||||
| 119 | && !$self->isa('Google::Auth::ExternalAccountCredentials::Aws')) | ||||||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||||||
| 121 | 12 | my $file = $source->{file}; | |||||||||||||||||||||||||||||||
| 122 | 12 | my $url = $source->{url}; | |||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 12 | ▶ | if (defined $file && defined $url) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $file and defined $url
MC/DC100%2/2
defined $file and defined $url
defined $file
defined $url
Truth table100%3/3
A and B
A= defined $fileB= defined $url
| |||||||||||||||||||||||||||||||||
| 125 | 1 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 126 | 'Ambiguous credential_source. \'file\' is mutually exclusive with \'url\'.' | ||||||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 11 | ▶ | if (!defined $file && !defined $url) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $file or defined $url
MC/DC100%2/2
defined $file or defined $url
defined $file
defined $url
Truth table100%3/3
A or B
A= defined $fileB= defined $url
| |||||||||||||||||||||||||||||||||
| 131 | 1 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 132 | 'Missing credential_source. A \'file\' or \'url\' must be provided.'); | ||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 10 | ▶ | my $format = $source->{format} // {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$source->{"format"} // {}
MC/DC100%1/1
$source->{"format"} // {}
$source->{"format"}
Truth table100%2/2
A
A= $source->{"format"}
| |||||||||||||||||||||||||||||||||
| 136 | 10 | ▶ | my $format_type = $format->{type} // 'text'; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$format->{"type"} // "text"
MC/DC100%1/1
$format->{"type"} // "text"
$format->{"type"}
Truth table100%2/2
A
A= $format->{"type"}
| |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 10 | ▶ | if ($format_type ne 'text' && $format_type ne 'json') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$format_type ne "text" and $format_type ne "json"
MC/DC100%2/2
$format_type ne "text" and $format_type ne "json"
$format_type ne "text"
$format_type ne "json"
Truth table100%3/3
A and B
A= $format_type ne "text"B= $format_type ne "json"
| |||||||||||||||||||||||||||||||||
| 139 | 1 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 140 | 'Invalid credential_source format ' . $format_type); | ||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 9 | ▶ | if ($format_type eq 'json' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$format_type eq "json" and not defined $format->{"subject_token_field_name"}
MC/DC100%2/2
$format_type eq "json" and not defined $format->{"subject_token_field_name"}
$format_type eq "json"
not defined $format->{"subject_token_field_name"}
Truth table100%3/3
A and not(B)
A= $format_type eq "json"B= not defined $format->{"subject_token_field_name"}
| |||||||||||||||||||||||||||||||||
| 144 | && !defined $format->{subject_token_field_name}) | ||||||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||||||
| 146 | 1 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 147 | 'Missing subject_token_field_name for JSON credential_source format'); | ||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 23 | ▶ | sub make_creds { | ||||||||||||||||||||||||||||||
|
Subroutine
make_creds: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 153 | 23 | my ($class, %options) = @_; | |||||||||||||||||||||||||||||||
| 154 | 23 | my $args = $class->BUILDARGS(%options); | |||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | 23 | my $source = $args->{credential_source}; | |||||||||||||||||||||||||||||||
| 157 | 23 | my $subject_token_type = $args->{subject_token_type}; | |||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 23 | ▶ | if (defined $subject_token_type | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $subject_token_type and $subject_token_type eq "urn:ietf:params:aws:token-type:aws4_request"
MC/DC50%1/2
defined $subject_token_type and $subject_token_type eq "urn:ietf:params:aws:token-type:aws4_request"
defined $subject_token_type
$subject_token_type eq "urn:ietf:params:aws:token-type:aws4_request"
Truth table66%2/3
A and B
A= defined $subject_token_typeB= $subject_token_type eq "urn:ietf:params:aws:token-type:aws4_request"
| |||||||||||||||||||||||||||||||||
| 160 | && $subject_token_type eq 'urn:ietf:params:aws:token-type:aws4_request') | ||||||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||||||
| 162 | 13 | require Google::Auth::ExternalAccountCredentials::Aws; | |||||||||||||||||||||||||||||||
| 163 | 13 | return Google::Auth::ExternalAccountCredentials::Aws->new($args); | |||||||||||||||||||||||||||||||
| 164 | ▶ | } elsif (defined $source && exists $source->{executable}) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $source and exists $source->{"executable"}
MC/DC50%1/2
defined $source and exists $source->{"executable"}
defined $source
exists $source->{"executable"}
Truth table66%2/3
A and B
A= defined $sourceB= exists $source->{"executable"}
| |||||||||||||||||||||||||||||||||
| 165 | 8 | require Google::Auth::ExternalAccountCredentials::Pluggable; | |||||||||||||||||||||||||||||||
| 166 | 8 | return Google::Auth::ExternalAccountCredentials::Pluggable->new($args); | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 2 | return Google::Auth::ExternalAccountCredentials->new($args); | |||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | 7 | ▶ | sub retrieve_subject_token { | ||||||||||||||||||||||||||||||
|
Subroutine
retrieve_subject_token: called CC=13 SCAR=26.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 173 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | 7 | my $source = $self->credential_source; | |||||||||||||||||||||||||||||||
| 176 | 7 | my $content; | |||||||||||||||||||||||||||||||
| 177 | my $source_name; | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 7 | ▶ | if (defined $source->{file}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 180 | 2 | $source_name = $source->{file}; | |||||||||||||||||||||||||||||||
| 181 | 2 | ▶ | if (!-f $source_name) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 182 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 183 | 'Credential source file ' . $source_name . ' not found'); | ||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||
| 185 | 2 | ▶ | open(my $fh, '<:encoding(UTF-8)', $source_name) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 186 | or Google::Auth::Error->throw( | ||||||||||||||||||||||||||||||||
| 187 | 'Could not open credential source file ' . $source_name . ': ' . $!); | ||||||||||||||||||||||||||||||||
| 188 | 2 | local $/; | |||||||||||||||||||||||||||||||
| 189 | 2 | $content = <$fh>; | |||||||||||||||||||||||||||||||
| 190 | 2 | ▶ | close($fh) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | or Google::Auth::Error->throw( | ||||||||||||||||||||||||||||||||
| 192 | 'Could not close credential source file ' . $source_name . ': ' . $!); | ||||||||||||||||||||||||||||||||
| 193 | } else { | ||||||||||||||||||||||||||||||||
| 194 | 5 | $source_name = $source->{url}; | |||||||||||||||||||||||||||||||
| 195 | 5 | ▶ | my $headers = $source->{headers} // {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$source->{"headers"} // {}
MC/DC100%1/1
$source->{"headers"} // {}
$source->{"headers"}
Truth table100%2/2
A
A= $source->{"headers"}
| |||||||||||||||||||||||||||||||||
| 196 | 5 | my $ua = $self->ua; | |||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 5 | my @header_list; | |||||||||||||||||||||||||||||||
| 199 | 5 | ▶ | while (my ($k, $v) = each %$headers) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 200 | 1 | push @header_list, $k => $v; | |||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | 5 | $log->tracef('Retrieving subject token from URL: %s...', $source_name); | |||||||||||||||||||||||||||||||
| 204 | 5 | $self->_validate_url($source_name, 'credential_source.url'); | |||||||||||||||||||||||||||||||
| 205 | 2 | my $response = $ua->get($source_name, @header_list); | |||||||||||||||||||||||||||||||
| 206 | 2 | ▶ | if (!$response->is_success) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 207 | 1 | $log->errorf('Failed to fetch subject token from URL %s: %s', | |||||||||||||||||||||||||||||||
| 208 | $source_name, $response->status_line); | ||||||||||||||||||||||||||||||||
| 209 | 1 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 210 | 'Failed to retrieve subject token from URL ' . | ||||||||||||||||||||||||||||||||
| 211 | $source_name . ': ' . | ||||||||||||||||||||||||||||||||
| 212 | $response->status_line); | ||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | 1 | $content = $response->decoded_content; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | 3 | ▶ | my $format = $source->{format} // {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$source->{"format"} // {}
MC/DC100%1/1
$source->{"format"} // {}
$source->{"format"}
Truth table100%2/2
A
A= $source->{"format"}
| |||||||||||||||||||||||||||||||||
| 218 | 3 | ▶ | my $format_type = $format->{type} // 'text'; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$format->{"type"} // "text"
MC/DC100%1/1
$format->{"type"} // "text"
$format->{"type"}
Truth table100%2/2
A
A= $format->{"type"}
| |||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 3 | ▶ | if ($format_type eq 'text') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 221 | 2 | $log->tracef('Subject token retrieved successfully as text.'); | |||||||||||||||||||||||||||||||
| 222 | 2 | return $content; | |||||||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||||||
| 224 | 1 | my $field = $format->{subject_token_field_name}; | |||||||||||||||||||||||||||||||
| 225 | 1 | $log->tracef( | |||||||||||||||||||||||||||||||
| 226 | 'Parsing OIDC JSON response to extract subject token field: %s', $field); | ||||||||||||||||||||||||||||||||
| 227 | 1 | my $data = eval { decode_json($content) }; | |||||||||||||||||||||||||||||||
| 228 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 229 | 0 | $log->errorf('OIDC JSON parsing failed: %s', $@); | |||||||||||||||||||||||||||||||
| 230 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 231 | 'Failed to parse JSON from credential resource ' . | ||||||||||||||||||||||||||||||||
| 232 | $source_name . ': ' . | ||||||||||||||||||||||||||||||||
| 233 | $@); | ||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | 1 | my $token = $data->{$field}; | |||||||||||||||||||||||||||||||
| 236 | 1 | ▶ | if (!defined $token) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 237 | 0 | $log->errorf('OIDC JSON response missing expected field: %s', $field); | |||||||||||||||||||||||||||||||
| 238 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 239 | 'Missing field ' . $field . ' in OIDC JSON response'); | ||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||
| 241 | 1 | $log->tracef('Subject token parsed successfully from JSON field.'); | |||||||||||||||||||||||||||||||
| 242 | 1 | return $token; | |||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 3 | ▶ | sub fetch_access_token { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch_access_token: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 247 | 3 | my ($self, %options) = @_; | |||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | 3 | my $subject_token = $self->retrieve_subject_token(); | |||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | 3 | ▶ | my $scopes = $self->scope | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->scope // ["https://www.googleapis.com/auth/cloud-platform"]
MC/DC0%0/1
$self->scope // ["https://www.googleapis.com/auth/cloud-platform"]
$self->scope
Truth table50%1/2
A
A= $self->scope
| |||||||||||||||||||||||||||||||||
| 252 | // ['https://www.googleapis.com/auth/cloud-platform']; | ||||||||||||||||||||||||||||||||
| 253 | 3 | ▶ | my @scopes_list = ref $scopes eq 'ARRAY' ? @$scopes : ($scopes); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | 3 | my $sts_payload = { | |||||||||||||||||||||||||||||||
| 256 | grant_type => 'urn:ietf:params:oauth:grant-type:token-exchange', | ||||||||||||||||||||||||||||||||
| 257 | audience => $self->audience, | ||||||||||||||||||||||||||||||||
| 258 | subject_token => $subject_token, | ||||||||||||||||||||||||||||||||
| 259 | subject_token_type => $self->subject_token_type, | ||||||||||||||||||||||||||||||||
| 260 | requested_token_type => 'urn:ietf:params:oauth:token-type:access_token', | ||||||||||||||||||||||||||||||||
| 261 | scope => join(' ', @scopes_list), | ||||||||||||||||||||||||||||||||
| 262 | }; | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | 3 | my $ua = $self->ua; | |||||||||||||||||||||||||||||||
| 265 | 3 | $log->infof('Exchanging subject token for GCP STS access token at %s...', | |||||||||||||||||||||||||||||||
| 266 | $self->token_url); | ||||||||||||||||||||||||||||||||
| 267 | 3 | $self->_validate_url($self->token_url, 'token_url'); | |||||||||||||||||||||||||||||||
| 268 | my $response = Google::Auth::RetryHelper->execute_with_retry( | ||||||||||||||||||||||||||||||||
| 269 | 5 | ▶ | sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 270 | 5 | my $res = $ua->post( | |||||||||||||||||||||||||||||||
| 271 | $self->token_url, | ||||||||||||||||||||||||||||||||
| 272 | 'Content-Type' => 'application/x-www-form-urlencoded', | ||||||||||||||||||||||||||||||||
| 273 | 'Content' => $sts_payload | ||||||||||||||||||||||||||||||||
| 274 | ); | ||||||||||||||||||||||||||||||||
| 275 | 5 | ▶ | if (!$res->is_success) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 276 | 3 | $log->warnf('STS token exchange request failed: status %s', $res->code); | |||||||||||||||||||||||||||||||
| 277 | 3 | Google::Auth::Error->throw('Token exchange failed with status ' . | |||||||||||||||||||||||||||||||
| 278 | $res->code . ': ' . $res->decoded_content); | ||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||
| 280 | 2 | return $res; | |||||||||||||||||||||||||||||||
| 281 | }, | ||||||||||||||||||||||||||||||||
| 282 | 3 | %options | |||||||||||||||||||||||||||||||
| 283 | ); | ||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||
| 285 | 2 | my $sts_data = decode_json($response->decoded_content); | |||||||||||||||||||||||||||||||
| 286 | 2 | my $sts_token = $sts_data->{access_token}; | |||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | 2 | ▶ | if ($self->service_account_impersonation_url) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 289 | 1 | $log->infof( | |||||||||||||||||||||||||||||||
| 290 | 'Service account impersonation URL set. Requesting delegated credentials via %s...', | ||||||||||||||||||||||||||||||||
| 291 | $self->service_account_impersonation_url | ||||||||||||||||||||||||||||||||
| 292 | ); | ||||||||||||||||||||||||||||||||
| 293 | 1 | my $impersonation_body = encode_json({ | |||||||||||||||||||||||||||||||
| 294 | scope => \@scopes_list | ||||||||||||||||||||||||||||||||
| 295 | }); | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | 1 | $self->_validate_url( | |||||||||||||||||||||||||||||||
| 298 | $self->service_account_impersonation_url, | ||||||||||||||||||||||||||||||||
| 299 | 'service_account_impersonation_url' | ||||||||||||||||||||||||||||||||
| 300 | ); | ||||||||||||||||||||||||||||||||
| 301 | my $impers_res = Google::Auth::RetryHelper->execute_with_retry( | ||||||||||||||||||||||||||||||||
| 302 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||||||
| 303 | 1 | my $res = $ua->post( | |||||||||||||||||||||||||||||||
| 304 | $self->service_account_impersonation_url, | ||||||||||||||||||||||||||||||||
| 305 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||
| 306 | 'Authorization' => 'Bearer ' . $sts_token, | ||||||||||||||||||||||||||||||||
| 307 | 'Content' => $impersonation_body | ||||||||||||||||||||||||||||||||
| 308 | ); | ||||||||||||||||||||||||||||||||
| 309 | 1 | ▶ | if (!$res->is_success) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 310 | 0 | $log->warnf( | |||||||||||||||||||||||||||||||
| 311 | 'Delegated service account impersonation failed: status %s', | ||||||||||||||||||||||||||||||||
| 312 | $res->code); | ||||||||||||||||||||||||||||||||
| 313 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 314 | 'Service account impersonation failed with status ' . | ||||||||||||||||||||||||||||||||
| 315 | $res->code . ': ' . | ||||||||||||||||||||||||||||||||
| 316 | $res->decoded_content); | ||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | 1 | return $res; | |||||||||||||||||||||||||||||||
| 319 | }, | ||||||||||||||||||||||||||||||||
| 320 | 1 | %options | |||||||||||||||||||||||||||||||
| 321 | ); | ||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | 1 | my $impers_data = decode_json($impers_res->decoded_content); | |||||||||||||||||||||||||||||||
| 324 | 1 | $self->access_token($impers_data->{accessToken}); | |||||||||||||||||||||||||||||||
| 325 | 1 | $self->expires_at($impers_data->{expireTime}); | |||||||||||||||||||||||||||||||
| 326 | 1 | $log->infof('Delegated impersonation completed successfully.'); | |||||||||||||||||||||||||||||||
| 327 | 1 | return $impers_data->{accessToken}; | |||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||
| 330 | 1 | $self->access_token($sts_token); | |||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | 1 | ▶ | my $expires_in = $sts_data->{expires_in} // 3600; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$sts_data->{"expires_in"} // 3600
MC/DC0%0/1
$sts_data->{"expires_in"} // 3600
$sts_data->{"expires_in"}
Truth table50%1/2
A
A= $sts_data->{"expires_in"}
| |||||||||||||||||||||||||||||||||
| 333 | 1 | my @t = gmtime(time + $expires_in); | |||||||||||||||||||||||||||||||
| 334 | 1 | my $expire_iso = sprintf( | |||||||||||||||||||||||||||||||
| 335 | '%04d-%02d-%02dT%02d:%02d:%02dZ', | ||||||||||||||||||||||||||||||||
| 336 | $t[5] + 1900, | ||||||||||||||||||||||||||||||||
| 337 | $t[4] + 1, | ||||||||||||||||||||||||||||||||
| 338 | $t[3], $t[2], $t[1], $t[0]); | ||||||||||||||||||||||||||||||||
| 339 | 1 | $self->expires_at($expire_iso); | |||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | 1 | return $sts_token; | |||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | 4 | 1; | |||||||||||||||||||||||||||||||