blib/lib/Google/Auth/ImpersonatedServiceAccountCredentials.pm

filter: Statementstmt 91.1% 72 / 79 Branchbran 65.0% 13 / 20 Conditioncond 38.8% 7 / 18 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 2 totaltotal 73.6% 106 / 144 CC 17 SCAR 19.1
CC
17
Cov
79%
__ANON__
13.4
fetch_access_token
7.3
BUILD
2.0
CRAP 6.7
« blib/lib/Google/Auth/ServiceAccountCredentials.pm ↑ Summary blib/lib/Google/Auth/ClientId.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::ImpersonatedServiceAccountCredentials;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 extends 'Google::Auth::Credentials';
22  
23 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Google::Auth::RetryHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 has base_credentials => (
30 is => 'ro',
31 required => 0,
32 );
33  
34 1 has source_credentials => (
35 is => 'ro',
36 required => 0,
37 );
38  
39 1 has impersonation_url => (
40 is => 'ro',
41 required => 1,
42 );
43  
44 1 has scope => (
45 is => 'ro',
46 required => 1,
47 );
48  
49 has ua => (
50 is => 'ro',
51 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 4 my $ua = LWP::UserAgent->new(timeout => 10);
53 4 $ua->env_proxy;
54 4 return $ua;
55 },
56 1 );
57  
58 8 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=10 SCAR=26.0
59 8 my ($orig, $class, @args) = @_;
60 8 my $args = $class->$orig(@args);
61  
62 8 ▶ if (my $json = $args->{json_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $json = $args->{"json_key"}) 1 7 8
63 1 ▶ $args->{impersonation_url} //= $json->{service_account_impersonation_url};
Condition33%1/3
$args->{"impersonation_url"} //= $json->{"service_account_impersonation_url"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{"impersonation_url"} //= $json->{"service_account_impersonation_url"}
$args->{"impersonation_url"} $json->{"service_account_impersonation_url"}
Truth table33%1/3
A //= B
A= $args->{"impersonation_url"}B= $json->{"service_account_impersonation_url"}
ABresult
1X1
011
000
64 1 ▶ $args->{scope} //= $json->{scopes};
Condition33%1/3
$args->{"scope"} //= $json->{"scopes"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{"scope"} //= $json->{"scopes"}
$args->{"scope"} $json->{"scopes"}
Truth table33%1/3
A //= B
A= $args->{"scope"}B= $json->{"scopes"}
ABresult
1X1
011
000
65 1 ▶ if (my $source_creds_info = $json->{source_credentials}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $source_creds_info = $json->{"source_credentials"}) 1 0 1
66 1 ▶ if ($source_creds_info->{type} eq 'impersonated_service_account') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_creds_info->{"type"} eq "impersonated_service_account") 0 1 1
67 0 Google::Auth::Error->throw(
68 'Source credentials can\'t be of type impersonated_service_account, use delegates to chain impersonation.'
69 );
70 }
71 1 require Google::Auth::DefaultCredentials;
72 $args->{source_credentials} //=
73 1 ▶ Google::Auth::DefaultCredentials->make_creds(
Condition33%1/3
$args->{"source_credentials"} //= "Google::Auth::DefaultCredentials"->make_creds("json_key", $source_creds_info)
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{"source_credentials"} //= "Google::Auth::DefaultCredentials"->make_creds("json_key", $source_creds_info)
$args->{"source_credentials"} "Google::Auth::DefaultCredentials"->make_creds("json_key", $source_creds_info)
Truth table33%1/3
A //= B
A= $args->{"source_credentials"}B= "Google::Auth::DefaultCredentials"->make_creds("json_key", $source_creds_info)
ABresult
1X1
011
000
74 json_key => $source_creds_info,);
75 }
76 }
77  
78 8 ▶ if ( !defined $args->{source_credentials}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"source_credentials"} or defined $args->{"base_credentials"}) 1 7 8
Condition66%2/3
defined $args->{"source_credentials"} or defined $args->{"base_credentials"}
l!l&&r!l&&!r
701
MC/DC50%1/2
defined $args->{"source_credentials"} or defined $args->{"base_credentials"}
defined $args->{"source_credentials"} defined $args->{"base_credentials"}
Truth table66%2/3
A or B
A= defined $args->{"source_credentials"}B= defined $args->{"base_credentials"}
ABresult
1X1
011
000
79 && !defined $args->{base_credentials})
80 {
81 1 Google::Auth::Error->throw(
82 'Missing required option: either source_credentials or base_credentials must be provided'
83 );
84 }
85  
86 7 ▶ $args->{source_credentials} //= $args->{base_credentials};
Condition33%1/3
$args->{"source_credentials"} //= $args->{"base_credentials"}
l!l&&r!l&&!r
700
MC/DC0%0/2
$args->{"source_credentials"} //= $args->{"base_credentials"}
$args->{"source_credentials"} $args->{"base_credentials"}
Truth table33%1/3
A //= B
A= $args->{"source_credentials"}B= $args->{"base_credentials"}
ABresult
1X1
011
000
87  
88 7 return $args;
89 1 };
90  
91 6 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
92 6 my ($self) = @_;
93  
94 6 my $source_creds = $self->source_credentials;
95 6 ▶ if ($source_creds->isa('Google::Auth::ImpersonatedServiceAccountCredentials'))
Branch: 1 branch
BranchTrueFalseTotal
if ($source_creds->isa("Google::Auth::ImpersonatedServiceAccountCredentials")) 1 5 6
96 {
97 1 Google::Auth::Error->throw(
98 'Source credentials can\'t be of type impersonated_service_account, use delegates to chain impersonation.'
99 );
100 }
101 }
102  
103 3 ▶sub fetch_access_token {
Subroutine fetch_access_token: called CC=6 SCAR=19.9
Pod undocumented
104 3 my ($self, %options) = @_;
105  
106 3 $self->_validate_url($self->impersonation_url, 'impersonation_url');
107  
108 2 my $source_creds = $self->source_credentials;
109 2 my $source_token;
110  
111 2 ▶ if ($source_creds->can('get_token')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($source_creds->can('get_token')) { } 0 2 2
elsif ($source_creds->can('access_token') and defined $source_creds->access_token) { } 2 0 2
elsif ($source_creds->can('fetch_access_token')) { } 0 0 0
112 0 $log->tracef('Fetching source credentials token using get_token...');
113 0 $source_token = $source_creds->get_token(%options);
114 ▶ } elsif ($source_creds->can('access_token')
Condition33%1/3
$source_creds->can("access_token") and defined $source_creds->access_token
!ll&&!rl&&r
002
MC/DC0%0/2
$source_creds->can("access_token") and defined $source_creds->access_token
$source_creds->can("access_token") defined $source_creds->access_token
Truth table33%1/3
A and B
A= $source_creds->can("access_token")B= defined $source_creds->access_token
ABresult
0X0
100
111
115 && defined $source_creds->access_token)
116 {
117 2 $log->tracef('Using cached access_token from source credentials...');
118 2 $source_token = $source_creds->access_token;
119 } elsif ($source_creds->can('fetch_access_token')) {
120 0 $log->tracef(
121 'Fetching source credentials token using fetch_access_token...');
122 0 $source_token = $source_creds->fetch_access_token(%options);
123 } else {
124 0 $log->errorf(
125 'Source credentials do not contain a valid token retrieval mechanism');
126 0 Google::Auth::Error->throw(
127 'Source credentials do not have a valid access token or fetch_access_token method'
128 );
129 }
130  
131 2 ▶ my $req_body = encode_json({
Branch: 1 branch
BranchTrueFalseTotal
ref $self->scope eq 'ARRAY' ? : 0 2 2
132 scope => ref $self->scope eq 'ARRAY' ? $self->scope : [$self->scope]});
133  
134 2 my $ua = $self->ua;
135 2 $log->infof('Requesting impersonated access token from %s...',
136 $self->impersonation_url);
137 my $response = Google::Auth::RetryHelper->execute_with_retry(
138 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
139 4 my $res = $ua->post(
140 $self->impersonation_url,
141 'Content-Type' => 'application/json',
142 'Authorization' => 'Bearer ' . $source_token,
143 'Content' => $req_body
144 );
145 4 ▶ if (!$res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 3 1 4
146 3 $log->warnf('Impersonated token exchange failed: status %s',
147 $res->code);
148 3 Google::Auth::Error->throw(
149 'Service account impersonation failed with status ' .
150 $res->code . ': ' .
151 $res->decoded_content);
152 }
153 1 return $res;
154 },
155 2 %options
156 );
157  
158 1 my $res_data = decode_json($response->decoded_content);
159 1 $self->access_token($res_data->{accessToken});
160 1 $self->expires_at($res_data->{expireTime});
161  
162 1 return $res_data->{accessToken};
163 }
164  
165 1 1;
« blib/lib/Google/Auth/ServiceAccountCredentials.pm ↑ Summary blib/lib/Google/Auth/ClientId.pm »