« blib/lib/Google/Auth/ImpersonatedServiceAccountCredentials.pm
↑ Summary
blib/lib/Google/Auth/IAMCredentials.pm »
| 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 | 14 | ▶ | default => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 64 | 14 | my $ua = LWP::UserAgent->new(timeout => 10); | |||||||||||||||||||||||||||||||
| 65 | 14 | $ua->env_proxy; | |||||||||||||||||||||||||||||||
| 66 | 14 | 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 | 47 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=36.8
| |||||||||||||||||||||||||||||||||
| 73 | 47 | my ($orig, $class, @args) = @_; | |||||||||||||||||||||||||||||||
| 74 | 47 | my $args = $class->$orig(@args); | |||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 47 | my $pinned_universe; | |||||||||||||||||||||||||||||||
| 77 | 47 | ▶ | if (exists $args->{_is_universe_pinned}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 23 | $pinned_universe = $args->{_is_universe_pinned}; | |||||||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||||||
| 80 | 24 | $pinned_universe = exists $args->{universe_domain}; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 47 | ▶ | if (my $json = $args->{json_key}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $args->{audience} //= $json->{audience}; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"audience"} //= $json->{"audience"}
MC/DC0%0/2
$args->{"audience"} //= $json->{"audience"}
$args->{"audience"}
$json->{"audience"}
Truth table0%0/3
A //= B
A= $args->{"audience"}B= $json->{"audience"}
| |||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $args->{subject_token_type} //= $json->{subject_token_type}; | ||||||||||||||||||||||||||||||
|
Condition0%0/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 table0%0/3
A //= B
A= $args->{"subject_token_type"}B= $json->{"subject_token_type"}
| |||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $args->{token_url} //= $json->{token_url}; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"token_url"} //= $json->{"token_url"}
MC/DC0%0/2
$args->{"token_url"} //= $json->{"token_url"}
$args->{"token_url"}
$json->{"token_url"}
Truth table0%0/3
A //= B
A= $args->{"token_url"}B= $json->{"token_url"}
| |||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $args->{credential_source} //= $json->{credential_source}; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"credential_source"} //= $json->{"credential_source"}
MC/DC0%0/2
$args->{"credential_source"} //= $json->{"credential_source"}
$args->{"credential_source"}
$json->{"credential_source"}
Truth table0%0/3
A //= B
A= $args->{"credential_source"}B= $json->{"credential_source"}
| |||||||||||||||||||||||||||||||||
| 88 | $args->{service_account_impersonation_url} //= | ||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $json->{service_account_impersonation_url}; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"service_account_impersonation_url"} //= $json->{"service_account_impersonation_url"}
MC/DC0%0/2
$args->{"service_account_impersonation_url"} //= $json->{"service_account_impersonation_url"}
$args->{"service_account_impersonation_url"}
$json->{"service_account_impersonation_url"}
Truth table0%0/3
A //= B
A= $args->{"service_account_impersonation_url"}B= $json->{"service_account_impersonation_url"}
| |||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $args->{universe_domain} //= $json->{universe_domain}; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$args->{"universe_domain"} //= $json->{"universe_domain"}
MC/DC0%0/2
$args->{"universe_domain"} //= $json->{"universe_domain"}
$args->{"universe_domain"}
$json->{"universe_domain"}
Truth table0%0/3
A //= B
A= $args->{"universe_domain"}B= $json->{"universe_domain"}
| |||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 47 | ▶ | $args->{_is_universe_pinned} = $pinned_universe ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 47 | return $args; | |||||||||||||||||||||||||||||||
| 96 | 4 | }; | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 24 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=17 SCAR=36.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 99 | 24 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 24 | my $source = $self->credential_source; | |||||||||||||||||||||||||||||||
| 102 | 24 | ▶ | 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 | 24 | ▶ | if (!$self->isa('Google::Auth::ExternalAccountCredentials::Aws')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | 8 | ▶ | if (exists $source->{environment_id} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $source->{"environment_id"} and defined $source->{"environment_id"}
MC/DC0%0/2
exists $source->{"environment_id"} and defined $source->{"environment_id"}
exists $source->{"environment_id"}
defined $source->{"environment_id"}
Truth table33%1/3
A and B
A= exists $source->{"environment_id"}B= defined $source->{"environment_id"}
| |||||||||||||||||||||||||||||||||
| 110 | && defined $source->{environment_id}) | ||||||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||||||
| 112 | 0 | 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 | 24 | ▶ | 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 | 1 | my $file = $source->{file}; | |||||||||||||||||||||||||||||||
| 122 | 1 | my $url = $source->{url}; | |||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 1 | ▶ | if (defined $file && defined $url) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $file and defined $url
MC/DC0%0/2
defined $file and defined $url
defined $file
defined $url
Truth table33%1/3
A and B
A= defined $fileB= defined $url
| |||||||||||||||||||||||||||||||||
| 125 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 126 | 'Ambiguous credential_source. \'file\' is mutually exclusive with \'url\'.' | ||||||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | if (!defined $file && !defined $url) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $file or defined $url
MC/DC0%0/2
defined $file or defined $url
defined $file
defined $url
Truth table33%1/3
A or B
A= defined $fileB= defined $url
| |||||||||||||||||||||||||||||||||
| 131 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 132 | 'Missing credential_source. A \'file\' or \'url\' must be provided.'); | ||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | my $format = $source->{format} // {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$source->{"format"} // {}
MC/DC0%0/1
$source->{"format"} // {}
$source->{"format"}
Truth table50%1/2
A
A= $source->{"format"}
| |||||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | my $format_type = $format->{type} // 'text'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$format->{"type"} // "text"
MC/DC0%0/1
$format->{"type"} // "text"
$format->{"type"}
Truth table50%1/2
A
A= $format->{"type"}
| |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | if ($format_type ne 'text' && $format_type ne 'json') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$format_type ne "text" and $format_type ne "json"
MC/DC0%0/2
$format_type ne "text" and $format_type ne "json"
$format_type ne "text"
$format_type ne "json"
Truth table33%1/3
A and B
A= $format_type ne "text"B= $format_type ne "json"
| |||||||||||||||||||||||||||||||||
| 139 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 140 | 'Invalid credential_source format ' . $format_type); | ||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 1 | ▶ | if ($format_type eq 'json' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$format_type eq "json" and not defined $format->{"subject_token_field_name"}
MC/DC0%0/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 table33%1/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 | 0 | 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.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 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 | 16 | require Google::Auth::ExternalAccountCredentials::Aws; | |||||||||||||||||||||||||||||||
| 163 | 16 | return Google::Auth::ExternalAccountCredentials::Aws->new($args); | |||||||||||||||||||||||||||||||
| 164 | ▶ | } elsif (defined $source && exists $source->{executable}) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $source and exists $source->{"executable"}
MC/DC0%0/2
defined $source and exists $source->{"executable"}
defined $source
exists $source->{"executable"}
Truth table33%1/3
A and B
A= defined $sourceB= exists $source->{"executable"}
| |||||||||||||||||||||||||||||||||
| 165 | 7 | require Google::Auth::ExternalAccountCredentials::Pluggable; | |||||||||||||||||||||||||||||||
| 166 | 7 | return Google::Auth::ExternalAccountCredentials::Pluggable->new($args); | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 0 | return Google::Auth::ExternalAccountCredentials->new($args); | |||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | 1 | ▶ | sub retrieve_subject_token { | ||||||||||||||||||||||||||||||
|
Subroutine
retrieve_subject_token: called CC=12 SCAR=39.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 173 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | 1 | my $source = $self->credential_source; | |||||||||||||||||||||||||||||||
| 176 | 1 | my $content; | |||||||||||||||||||||||||||||||
| 177 | my $source_name; | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 1 | ▶ | if (defined $source->{file}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 180 | 1 | $source_name = $source->{file}; | |||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | if (!-f $source_name) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 182 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 183 | 'Credential source file ' . $source_name . ' not found'); | ||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||
| 185 | 1 | ▶ | 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 | 1 | local $/; | |||||||||||||||||||||||||||||||
| 189 | 1 | $content = <$fh>; | |||||||||||||||||||||||||||||||
| 190 | 1 | ▶ | close($fh) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | or Google::Auth::Error->throw( | ||||||||||||||||||||||||||||||||
| 192 | 'Could not close credential source file ' . $source_name . ': ' . $!); | ||||||||||||||||||||||||||||||||
| 193 | } else { | ||||||||||||||||||||||||||||||||
| 194 | 0 | $source_name = $source->{url}; | |||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | my $headers = $source->{headers} // {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$source->{"headers"} // {}
MC/DC0%0/1
$source->{"headers"} // {}
$source->{"headers"}
Truth table0%0/2
A
A= $source->{"headers"}
| |||||||||||||||||||||||||||||||||
| 196 | 0 | $log->tracef('Retrieving subject token from URL: %s...', $source_name); | |||||||||||||||||||||||||||||||
| 197 | 0 | $self->_validate_url($source_name, 'credential_source.url'); | |||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | 0 | my $future = $self->transport->request( | |||||||||||||||||||||||||||||||
| 200 | method => 'GET', | ||||||||||||||||||||||||||||||||
| 201 | url => $source_name, | ||||||||||||||||||||||||||||||||
| 202 | headers => $headers, | ||||||||||||||||||||||||||||||||
| 203 | ); | ||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | 0 | my ($body, $response_headers); | |||||||||||||||||||||||||||||||
| 206 | 0 | my $future_res = eval { ($body, $response_headers) = $future->get(); 1 }; | |||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | if (!$future_res) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | 0 | my ($err_msg, $cat, $details) = $future->failure; | |||||||||||||||||||||||||||||||
| 210 | 0 | $log->errorf('Failed to fetch subject token from URL %s: %s', | |||||||||||||||||||||||||||||||
| 211 | $source_name, $err_msg); | ||||||||||||||||||||||||||||||||
| 212 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 213 | 'Failed to retrieve subject token from URL ' . | ||||||||||||||||||||||||||||||||
| 214 | $source_name . ': ' . | ||||||||||||||||||||||||||||||||
| 215 | $err_msg); | ||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | 0 | $content = $body; | |||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 1 | ▶ | my $format = $source->{format} // {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$source->{"format"} // {}
MC/DC0%0/1
$source->{"format"} // {}
$source->{"format"}
Truth table50%1/2
A
A= $source->{"format"}
| |||||||||||||||||||||||||||||||||
| 221 | 1 | ▶ | my $format_type = $format->{type} // 'text'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$format->{"type"} // "text"
MC/DC0%0/1
$format->{"type"} // "text"
$format->{"type"}
Truth table50%1/2
A
A= $format->{"type"}
| |||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | 1 | ▶ | if ($format_type eq 'text') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 224 | 1 | $log->tracef('Subject token retrieved successfully as text.'); | |||||||||||||||||||||||||||||||
| 225 | 1 | return $content; | |||||||||||||||||||||||||||||||
| 226 | } else { | ||||||||||||||||||||||||||||||||
| 227 | 0 | my $field = $format->{subject_token_field_name}; | |||||||||||||||||||||||||||||||
| 228 | 0 | $log->tracef( | |||||||||||||||||||||||||||||||
| 229 | 'Parsing OIDC JSON response to extract subject token field: %s', $field); | ||||||||||||||||||||||||||||||||
| 230 | 0 | my $data = eval { decode_json($content) }; | |||||||||||||||||||||||||||||||
| 231 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 232 | 0 | $log->errorf('OIDC JSON parsing failed: %s', $@); | |||||||||||||||||||||||||||||||
| 233 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 234 | 'Failed to parse JSON from credential resource ' . | ||||||||||||||||||||||||||||||||
| 235 | $source_name . ': ' . | ||||||||||||||||||||||||||||||||
| 236 | $@); | ||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||
| 238 | 0 | my $token = $data->{$field}; | |||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | if (!defined $token) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 240 | 0 | $log->errorf('OIDC JSON response missing expected field: %s', $field); | |||||||||||||||||||||||||||||||
| 241 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 242 | 'Missing field ' . $field . ' in OIDC JSON response'); | ||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||
| 244 | 0 | $log->tracef('Subject token parsed successfully from JSON field.'); | |||||||||||||||||||||||||||||||
| 245 | 0 | return $token; | |||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | 1 | ▶ | sub fetch_access_token { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch_access_token: called CC=5 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 250 | 1 | my ($self, %options) = @_; | |||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | 1 | my $subject_token = $self->retrieve_subject_token(); | |||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | 1 | ▶ | 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
| |||||||||||||||||||||||||||||||||
| 255 | // ['https://www.googleapis.com/auth/cloud-platform']; | ||||||||||||||||||||||||||||||||
| 256 | 1 | ▶ | my @scopes_list = ref $scopes eq 'ARRAY' ? @$scopes : ($scopes); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 1 | my $sts_payload = { | |||||||||||||||||||||||||||||||
| 259 | grant_type => 'urn:ietf:params:oauth:grant-type:token-exchange', | ||||||||||||||||||||||||||||||||
| 260 | audience => $self->audience, | ||||||||||||||||||||||||||||||||
| 261 | subject_token => $subject_token, | ||||||||||||||||||||||||||||||||
| 262 | subject_token_type => $self->subject_token_type, | ||||||||||||||||||||||||||||||||
| 263 | requested_token_type => 'urn:ietf:params:oauth:token-type:access_token', | ||||||||||||||||||||||||||||||||
| 264 | scope => join(' ', @scopes_list), | ||||||||||||||||||||||||||||||||
| 265 | }; | ||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||
| 267 | 1 | require URI; | |||||||||||||||||||||||||||||||
| 268 | 1 | my $u = URI->new('http:'); | |||||||||||||||||||||||||||||||
| 269 | 1 | $u->query_form(%$sts_payload); | |||||||||||||||||||||||||||||||
| 270 | 1 | my $encoded_sts_payload = $u->query; | |||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 1 | $log->infof('Exchanging subject token for GCP STS access token at %s...', | |||||||||||||||||||||||||||||||
| 273 | $self->token_url); | ||||||||||||||||||||||||||||||||
| 274 | 1 | $self->_validate_url($self->token_url, 'token_url'); | |||||||||||||||||||||||||||||||
| 275 | my $response_body = Google::Auth::RetryHelper->execute_with_retry( | ||||||||||||||||||||||||||||||||
| 276 | 3 | ▶ | sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=44.2
| |||||||||||||||||||||||||||||||||
| 277 | 3 | my $future = $self->transport->request( | |||||||||||||||||||||||||||||||
| 278 | method => 'POST', | ||||||||||||||||||||||||||||||||
| 279 | url => $self->token_url, | ||||||||||||||||||||||||||||||||
| 280 | headers => {'Content-Type' => 'application/x-www-form-urlencoded'}, | ||||||||||||||||||||||||||||||||
| 281 | body => $encoded_sts_payload, | ||||||||||||||||||||||||||||||||
| 282 | ); | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | 0 | my ($body, $headers); | |||||||||||||||||||||||||||||||
| 285 | 0 | my $future_res = eval { ($body, $headers) = $future->get(); 1 }; | |||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | if (!$future_res) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 288 | 0 | my ($err_msg, $cat, $details) = $future->failure; | |||||||||||||||||||||||||||||||
| 289 | 0 | my $code = 0; | |||||||||||||||||||||||||||||||
| 290 | 0 | my $error_body = ''; | |||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | if (ref($details) eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | $code = $details->{code} // 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$details->{"code"} // 0
MC/DC0%0/1
$details->{"code"} // 0
$details->{"code"}
Truth table0%0/2
A
A= $details->{"code"}
| |||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | $error_body = $details->{body} // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$details->{"body"} // ""
MC/DC0%0/1
$details->{"body"} // ""
$details->{"body"}
Truth table0%0/2
A
A= $details->{"body"}
| |||||||||||||||||||||||||||||||||
| 294 | 0 | } elsif (eval { $details->can('code') }) { | |||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | $code = $details->code // 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$details->code // 0
MC/DC0%0/1
$details->code // 0
$details->code
Truth table0%0/2
A
A= $details->code
| |||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | $error_body = $details->decoded_content // $details->content // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/5
($details->decoded_content // $details->content) // ""
$details->decoded_content // $details->content
MC/DC0%0/2
($details->decoded_content // $details->content) // ""
$details->decoded_content
$details->content
Truth table0%0/3
A // B
A= $details->decoded_contentB= $details->content
| |||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | 0 | $log->warnf('STS token exchange request failed: status %s', $code); | |||||||||||||||||||||||||||||||
| 299 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 300 | 'Token exchange failed with status ' . $code . ': ' . $error_body); | ||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||
| 302 | 0 | return $body; | |||||||||||||||||||||||||||||||
| 303 | }, | ||||||||||||||||||||||||||||||||
| 304 | 1 | %options | |||||||||||||||||||||||||||||||
| 305 | ); | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | 0 | my $sts_data = decode_json($response_body); | |||||||||||||||||||||||||||||||
| 308 | 0 | my $sts_token = $sts_data->{access_token}; | |||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | if ($self->service_account_impersonation_url) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 311 | 0 | $log->infof( | |||||||||||||||||||||||||||||||
| 312 | 'Service account impersonation URL set. Requesting delegated credentials via %s...', | ||||||||||||||||||||||||||||||||
| 313 | $self->service_account_impersonation_url | ||||||||||||||||||||||||||||||||
| 314 | ); | ||||||||||||||||||||||||||||||||
| 315 | 0 | my $impersonation_body = encode_json({ | |||||||||||||||||||||||||||||||
| 316 | scope => \@scopes_list | ||||||||||||||||||||||||||||||||
| 317 | }); | ||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | 0 | $self->_validate_url( | |||||||||||||||||||||||||||||||
| 320 | $self->service_account_impersonation_url, | ||||||||||||||||||||||||||||||||
| 321 | 'service_account_impersonation_url' | ||||||||||||||||||||||||||||||||
| 322 | ); | ||||||||||||||||||||||||||||||||
| 323 | my $impers_res_body = Google::Auth::RetryHelper->execute_with_retry( | ||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||
| 325 | 0 | my $future = $self->transport->request( | |||||||||||||||||||||||||||||||
| 326 | method => 'POST', | ||||||||||||||||||||||||||||||||
| 327 | url => $self->service_account_impersonation_url, | ||||||||||||||||||||||||||||||||
| 328 | headers => { | ||||||||||||||||||||||||||||||||
| 329 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||
| 330 | 'Authorization' => 'Bearer ' . $sts_token, | ||||||||||||||||||||||||||||||||
| 331 | }, | ||||||||||||||||||||||||||||||||
| 332 | body => $impersonation_body, | ||||||||||||||||||||||||||||||||
| 333 | ); | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | 0 | my ($body, $headers); | |||||||||||||||||||||||||||||||
| 336 | 0 | my $future_res = eval { ($body, $headers) = $future->get(); 1 }; | |||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | if (!$future_res) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 339 | 0 | my ($err_msg, $cat, $details) = $future->failure; | |||||||||||||||||||||||||||||||
| 340 | 0 | my $code = 0; | |||||||||||||||||||||||||||||||
| 341 | 0 | my $error_body = ''; | |||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | if (ref($details) eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 343 | 0 | ▶ | $code = $details->{code} // 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$details->{"code"} // 0
MC/DC0%0/1
$details->{"code"} // 0
$details->{"code"}
Truth table0%0/2
A
A= $details->{"code"}
| |||||||||||||||||||||||||||||||||
| 344 | 0 | ▶ | $error_body = $details->{body} // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$details->{"body"} // ""
MC/DC0%0/1
$details->{"body"} // ""
$details->{"body"}
Truth table0%0/2
A
A= $details->{"body"}
| |||||||||||||||||||||||||||||||||
| 345 | 0 | } elsif (eval { $details->can('code') }) { | |||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | $code = $details->code // 0; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$details->code // 0
MC/DC0%0/1
$details->code // 0
$details->code
Truth table0%0/2
A
A= $details->code
| |||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | $error_body = $details->decoded_content // $details->content // ''; | ||||||||||||||||||||||||||||||
|
Condition0%0/5
($details->decoded_content // $details->content) // ""
$details->decoded_content // $details->content
MC/DC0%0/2
($details->decoded_content // $details->content) // ""
$details->decoded_content
$details->content
Truth table0%0/3
A // B
A= $details->decoded_contentB= $details->content
| |||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||
| 349 | $log->warnf( | ||||||||||||||||||||||||||||||||
| 350 | 0 | 'Delegated service account impersonation failed: status %s', $code); | |||||||||||||||||||||||||||||||
| 351 | 0 | Google::Auth::Error->throw( | |||||||||||||||||||||||||||||||
| 352 | 'Service account impersonation failed with status ' . | ||||||||||||||||||||||||||||||||
| 353 | $code . ': ' . | ||||||||||||||||||||||||||||||||
| 354 | $error_body); | ||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||
| 356 | 0 | return $body; | |||||||||||||||||||||||||||||||
| 357 | }, | ||||||||||||||||||||||||||||||||
| 358 | 0 | %options | |||||||||||||||||||||||||||||||
| 359 | ); | ||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||
| 361 | 0 | my $impers_data = decode_json($impers_res_body); | |||||||||||||||||||||||||||||||
| 362 | 0 | $self->access_token($impers_data->{accessToken}); | |||||||||||||||||||||||||||||||
| 363 | 0 | $self->expires_at($impers_data->{expireTime}); | |||||||||||||||||||||||||||||||
| 364 | 0 | $log->infof('Delegated impersonation completed successfully.'); | |||||||||||||||||||||||||||||||
| 365 | 0 | return $impers_data->{accessToken}; | |||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | 0 | $self->access_token($sts_token); | |||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | my $expires_in = $sts_data->{expires_in} // 3600; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$sts_data->{"expires_in"} // 3600
MC/DC0%0/1
$sts_data->{"expires_in"} // 3600
$sts_data->{"expires_in"}
Truth table0%0/2
A
A= $sts_data->{"expires_in"}
| |||||||||||||||||||||||||||||||||
| 371 | 0 | my @t = gmtime(time + $expires_in); | |||||||||||||||||||||||||||||||
| 372 | 0 | my $expire_iso = sprintf( | |||||||||||||||||||||||||||||||
| 373 | '%04d-%02d-%02dT%02d:%02d:%02dZ', | ||||||||||||||||||||||||||||||||
| 374 | $t[5] + 1900, | ||||||||||||||||||||||||||||||||
| 375 | $t[4] + 1, | ||||||||||||||||||||||||||||||||
| 376 | $t[3], $t[2], $t[1], $t[0]); | ||||||||||||||||||||||||||||||||
| 377 | 0 | $self->expires_at($expire_iso); | |||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||
| 379 | 0 | return $sts_token; | |||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | 4 | 1; | |||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | __END__ | ||||||||||||||||||||||||||||||||