blib/lib/Google/Auth/Credentials.pm

filter: Statementstmt 61.1% 82 / 134 Branchbran 27.5% 16 / 58 Conditioncond 25.5% 11 / 43 MC/DCmcdc 7.4% 2 / 27 Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 4 totaltotal 44.9% 128 / 285 CC 46 SCAR 34.7
CC
46
Cov
46%
_validate_url
57.5
is_expired
51.4
apply
42.0
CRAP 32.1
« blib/lib/Google/Auth/ComputeEngine.pm ↑ Summary blib/lib/Google/Auth/ExternalAccountCredentials/Aws.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::Credentials;
16  
17 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 17 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 17 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 17 has access_token => (is => 'rw',);
28  
29 17 has expires_at => (is => 'rw',);
30  
31 has universe_domain => (
32 is => 'ro',
33 34 ▶ default => sub { 'googleapis.com' },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 17 );
35  
36 17 has quota_project_id => (
37 is => 'ro',
38 required => 0,
39 );
40  
41 17 has project_id => (
42 is => 'ro',
43 required => 0,
44 );
45  
46 has _is_universe_pinned => (
47 is => 'ro',
48 10 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 17 );
50  
51 has clock_skew => (
52 is => 'ro',
53 34 ▶ default => sub { 300 },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 17 );
55  
56 has is_refreshing => (
57 is => 'rw',
58 34 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 17 );
60  
61 17 has transport => (
62 is => 'ro',
63 lazy => 1,
64 builder => '_build_transport',
65 );
66  
67 12 ▶sub _build_transport {
Subroutine _build_transport: called CC=2 SCAR=12.4
68 12 my ($self) = @_;
69 12 require Google::Cloud::Transport::Adapter::LWP;
70 0 ▶ if ($self->can('ua')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("ua")) 0 0 0
71 0 return Google::Cloud::Transport::Adapter::LWP->new(user_agent => $self->ua);
72 }
73 0 return Google::Cloud::Transport::Adapter::LWP->new();
74 }
75  
76 57 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=9 SCAR=24.2
77 57 my ($orig, $class, @args) = @_;
78  
79 # Standardize arguments to a hashref
80 57 my $args;
81 57 ▶ if (@args == 1 && ref $args[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq 'HASH') { } 23 34 57
Condition66%2/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
34023
MC/DC50%1/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
82 23 $args = {%{$args[0]}};
83 } else {
84 34 $args = {@args};
85 }
86  
87 # If universe_domain is explicitly passed in code/options, it is pinned (trusted).
88 57 ▶ if (exists $args->{universe_domain}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"universe_domain"}) 0 57 57
89 0 ▶ $args->{_is_universe_pinned} //= 1;
Condition0%0/2
$args->{"_is_universe_pinned"} //= 1
l!l
00
MC/DC0%0/1
$args->{"_is_universe_pinned"} //= 1
$args->{"_is_universe_pinned"}
Truth table0%0/2
A
A= $args->{"_is_universe_pinned"}
Aresult
11
00
90 }
91  
92 57 ▶ if (my $json = $args->{json_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $json = $args->{"json_key"}) 4 53 57
93 4 ▶ $args->{quota_project_id} //= $json->{quota_project_id};
Condition33%1/3
$args->{"quota_project_id"} //= $json->{"quota_project_id"}
l!l&&r!l&&!r
004
MC/DC0%0/2
$args->{"quota_project_id"} //= $json->{"quota_project_id"}
$args->{"quota_project_id"} $json->{"quota_project_id"}
Truth table33%1/3
A //= B
A= $args->{"quota_project_id"}B= $json->{"quota_project_id"}
ABresult
1X1
011
000
94 4 ▶ $args->{project_id} //= $json->{project_id};
Condition33%1/3
$args->{"project_id"} //= $json->{"project_id"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$args->{"project_id"} //= $json->{"project_id"}
$args->{"project_id"} $json->{"project_id"}
Truth table33%1/3
A //= B
A= $args->{"project_id"}B= $json->{"project_id"}
ABresult
1X1
011
000
95 }
96  
97 57 ▶ $args->{project_id} //= $ENV{GOOGLE_CLOUD_PROJECT};
Condition66%2/3
$args->{"project_id"} //= $ENV{"GOOGLE_CLOUD_PROJECT"}
l!l&&r!l&&!r
5052
MC/DC50%1/2
$args->{"project_id"} //= $ENV{"GOOGLE_CLOUD_PROJECT"}
$args->{"project_id"} $ENV{"GOOGLE_CLOUD_PROJECT"}
Truth table66%2/3
A //= B
A= $args->{"project_id"}B= $ENV{"GOOGLE_CLOUD_PROJECT"}
ABresult
1X1
011
000
98  
99 57 return $class->$orig($args);
100 17 };
101  
102 0 ▶sub fetch_access_token {
Subroutine fetch_access_token: not called CC=1 SCAR=6.9
Pod undocumented
103 0 my ($self, %options) = @_;
104 0 Google::Auth::Error->throw(
105 'fetch_access_token must be implemented by subclasses');
106 }
107  
108 1 ▶sub is_expired {
Subroutine is_expired: called CC=8 SCAR=39.4
Pod undocumented
109 1 my ($self, %options) = @_;
110 1 my $expires = $self->expires_at;
111 1 ▶ return 1 if !defined $expires;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expires 1 0 1
112  
113 0 ▶ my $skew = $options{clock_skew} // $self->clock_skew;
Condition0%0/3
$options{"clock_skew"} // $self->clock_skew
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"clock_skew"} // $self->clock_skew
$options{"clock_skew"} $self->clock_skew
Truth table0%0/3
A // B
A= $options{"clock_skew"}B= $self->clock_skew
ABresult
1X1
011
000
114  
115 0 my $expiry_epoch;
116 0 ▶ if ($expires =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires =~ /^\d+$/) { } 0 0 0
117 0 $expiry_epoch = $expires;
118 } else {
119 0 my $tp = eval { Time::Piece->strptime($expires, '%Y-%m-%dT%H:%M:%SZ') };
120 0 ▶ if ($@ || !$tp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $tp) 0 0 0
Condition0%0/3
$@ or not $tp
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $tp
$@ not $tp
Truth table0%0/3
A or not(B)
A= $@B= not $tp
ABresult
1X1
011
000
121 0 $tp = eval { Time::Piece->strptime($expires, '%Y-%m-%dT%H:%M:%S.%fZ'); };
122 }
123 0 ▶ $expiry_epoch = $tp ? $tp->epoch : time();
Branch: 1 branch
BranchTrueFalseTotal
$tp ? : 0 0 0
124 }
125  
126 0 my $now = time;
127 0 ▶ my $expired = ($now + $skew >= $expiry_epoch) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$now + $skew >= $expiry_epoch ? : 0 0 0
128 0 $log->tracef(
129 'Token expiration check for %s: expires_at=%s (epoch=%s), skew=%d, now=%s, expired=%d',
130 ref $self, $expires, $expiry_epoch, $skew, $now, $expired);
131 0 return $expired;
132 }
133  
134 1 ▶sub get_token {
Subroutine get_token: called CC=10 SCAR=35.5
Pod undocumented
135 1 my ($self, %options) = @_;
136  
137 1 ▶ if ($self->is_refreshing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_refreshing) 0 1 1
138 0 $log->debugf(
139 'Token refresh is already active in another thread/coroutine for %s. Yielding...',
140 ref $self
141 );
142 0 my $limit = 30;
143 0 ▶ while ($self->is_refreshing && $limit-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_refreshing and $limit-- > 0) 0 0 0
Condition0%0/3
$self->is_refreshing and $limit-- > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_refreshing and $limit-- > 0
$self->is_refreshing $limit-- > 0
Truth table0%0/3
A and B
A= $self->is_refreshingB= $limit-- > 0
ABresult
0X0
100
111
144 0 ▶ if (exists $INC{'Coro.pm'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $INC{'Coro.pm'}) { } 0 0 0
elsif (exists $INC{'threads.pm'}) { } 0 0 0
145 0 Coro::cede();
146 } elsif (exists $INC{'threads.pm'}) {
147 0 threads->yield();
148 } else {
149 0 Time::HiRes::sleep(0.1);
150 }
151 }
152 0 ▶ $log->debugf('Resume from lock for %s. Access token is: %s',
Branch: 1 branch
BranchTrueFalseTotal
defined $self->access_token ? : 0 0 0
153 ref $self, defined $self->access_token ? 'PRESENT' : 'MISSING');
154 0 return $self->access_token;
155 }
156  
157 1 ▶ if ($self->is_expired(%options) || !defined $self->access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_expired(%options) or not defined $self->access_token) 1 0 1
Condition33%1/3
$self->is_expired(%options) or not defined $self->access_token
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->is_expired(%options) or not defined $self->access_token
$self->is_expired(%options) not defined $self->access_token
Truth table33%1/3
A or not(B)
A= $self->is_expired(%options)B= not defined $self->access_token
ABresult
1X1
011
000
158 1 $log->infof(
159 'Access token is missing or expired for %s. Initiating refresh...',
160 ref $self);
161 1 $self->is_refreshing(1);
162 1 eval { $self->fetch_access_token(%options); };
163 1 my $err = $@;
164 1 $self->is_refreshing(0);
165 1 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 1 0 1
166 1 $log->errorf('Failed to fetch access token for %s: %s', ref $self, $err);
167 1 die $err;
168 }
169 0 $log->infof('Successfully refreshed access token for %s.', ref $self);
170 }
171 0 return $self->access_token;
172 }
173  
174 0 ▶sub apply {
Subroutine apply: not called CC=6 SCAR=37.4
Pod undocumented
175 0 my ($self, $req_or_headers, %options) = @_;
176 0 $log->debugf('Applying credentials decoration for %s...', ref $self);
177 0 my $token = $self->get_token(%options);
178  
179 0 ▶ if (ref $req_or_headers eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $req_or_headers eq 'HASH') { } 0 0 0
elsif (eval { do { $req_or_headers->isa('HTTP::Request') } }) { } 0 0 0
180 0 $req_or_headers->{Authorization} = 'Bearer ' . $token;
181 0 ▶ $req_or_headers->{'X-Goog-User-Project'} = $self->quota_project_id
Branch: 1 branch
BranchTrueFalseTotal
if $self->quota_project_id 0 0 0
182 if $self->quota_project_id;
183 0 } elsif (eval { $req_or_headers->isa('HTTP::Request') }) {
184 0 $req_or_headers->header(Authorization => 'Bearer ' . $token);
185 0 ▶ $req_or_headers->header('X-Goog-User-Project' => $self->quota_project_id)
Branch: 1 branch
BranchTrueFalseTotal
if $self->quota_project_id 0 0 0
186 if $self->quota_project_id;
187 } else {
188 0 ▶ $log->errorf(
Condition0%0/3
ref $req_or_headers || $req_or_headers
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $req_or_headers || $req_or_headers
ref $req_or_headers $req_or_headers
Truth table0%0/3
A || B
A= ref $req_or_headersB= $req_or_headers
ABresult
1X1
011
000
189 'Invalid apply target type: %s',
190 ref $req_or_headers || $req_or_headers
191 );
192 0 Google::Auth::Error->throw(
193 'apply expected a HASH reference or HTTP::Request object');
194 }
195 0 $log->debugf('Credentials applied successfully.');
196 }
197  
198 6 ▶sub _validate_url {
Subroutine _validate_url: called CC=16 SCAR=40.5
199 6 my ($self, $url_str, $name) = @_;
200 6 ▶ return unless defined $url_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url_str 0 6 6
201  
202 6 my $uri = URI->new($url_str);
203 6 ▶ my $scheme = lc($uri->scheme // '');
Condition50%1/2
$uri->scheme // ""
l!l
60
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
204  
205 6 ▶ unless ($scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scheme eq "https") 2 4 6
206 2 $log->errorf('URL %s (%s) uses non-HTTPS scheme (%s)',
207 $name, $url_str, $scheme);
208 2 Google::Auth::Error->throw('URL ' .
209 $name . ' (' .
210 $url_str .
211 ') carries security violation: scheme must be https');
212 }
213  
214 4 ▶ my $host = lc($uri->can('host') ? ($uri->host // '') : '');
Branch: 1 branch
BranchTrueFalseTotal
$uri->can('host') ? : 4 0 4
Condition50%1/2
$uri->host // ""
l!l
40
MC/DC0%0/1
$uri->host // ""
$uri->host
Truth table50%1/2
A
A= $uri->host
Aresult
11
00
215  
216 4 ▶ unless ($host) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host) 0 4 4
217 0 Google::Auth::Error->throw('Invalid URL for ' . $name . ': ' . $url_str);
218 }
219  
220 4 ▶ my $universe_domain = $self->universe_domain // 'googleapis.com';
Condition50%1/2
$self->universe_domain // "googleapis.com"
l!l
40
MC/DC0%0/1
$self->universe_domain // "googleapis.com"
$self->universe_domain
Truth table50%1/2
A
A= $self->universe_domain
Aresult
11
00
221  
222 4 my $is_valid = 0;
223 4 ▶ if ($host eq 'googleapis.com' || $host =~ /\.googleapis\.com$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($host eq 'googleapis.com' or $host =~ /\.googleapis\.com$/) { } 4 0 4
elsif ($host eq $universe_domain or $host =~ /\.\Q$universe_domain\E$/) { } 0 0 0
Condition33%1/3
$host eq "googleapis.com" or $host =~ /\.googleapis\.com$/
l!l&&r!l&&!r
040
MC/DC0%0/2
$host eq "googleapis.com" or $host =~ /\.googleapis\.com$/
$host eq "googleapis.com" $host =~ /\.googleapis\.com$/
Truth table33%1/3
A or B
A= $host eq "googleapis.com"B= $host =~ /\.googleapis\.com$/
ABresult
1X1
011
000
224 4 $is_valid = 1;
225 ▶ } elsif ($host eq $universe_domain || $host =~ /\.\Q$universe_domain\E$/) {
Condition0%0/3
$host eq $universe_domain or $host =~ /\.\Q$universe_domain\E$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$host eq $universe_domain or $host =~ /\.\Q$universe_domain\E$/
$host eq $universe_domain $host =~ /\.\Q$universe_domain\E$/
Truth table0%0/3
A or B
A= $host eq $universe_domainB= $host =~ /\.\Q$universe_domain\E$/
ABresult
1X1
011
000
226 0 my $is_pinned = $self->_is_universe_pinned;
227  
228 0 ▶ if ($is_pinned) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_pinned) { } 0 0 0
229 0 $is_valid = 1;
230 } else {
231 # Not pinned, came from JSON. Only allow if explicitly enabled via env var.
232 0 ▶ if (($ENV{GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES} // '0') eq '1')
Branch: 1 branch
BranchTrueFalseTotal
if (($ENV{'GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES'} // '0') eq '1') { } 0 0 0
Condition0%0/2
$ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES"} // 0
l!l
00
MC/DC0%0/1
$ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES"} // 0
$ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES"}
Truth table0%0/2
A
A= $ENV{"GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES"}
Aresult
11
00
233 {
234 0 $is_valid = 1;
235 } else {
236 0 $log->warnf(
237 "Custom universe_domain %s detected in JSON but not enabled via GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES=1",
238 $universe_domain
239 );
240 }
241 }
242 }
243  
244 4 ▶ unless ($is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_valid) 0 4 4
245 0 $log->errorf("URL %s (%s) does not match expected domain (%s)",
246 $name, $url_str, $universe_domain);
247 0 Google::Auth::Error->throw(
248 "URL $name ($url_str) carries security violation: domain does not match $universe_domain or googleapis.com"
249 );
250 }
251 }
252  
253 17 1;
« blib/lib/Google/Auth/ComputeEngine.pm ↑ Summary blib/lib/Google/Auth/ExternalAccountCredentials/Aws.pm »