blib/lib/Google/Auth/ExternalAccountCredentials/Aws.pm

filter: Statementstmt 98.8% 177 / 179 Branchbran 75.7% 50 / 66 Conditioncond 58.8% 50 / 85 MC/DCmcdc 31.2% 15 / 48 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 1 totaltotal 77.9% 307 / 394 CC 66 SCAR 34.4
CC
66
Cov
84%
_get_aws_security_credentials
54.9
_get_aws_region
15.9
retrieve_subject_token
8.0
CRAP 31.3
« blib/lib/Google/Auth/UserRefreshCredentials.pm ↑ Summary blib/lib/Google/Auth/Credentials.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::Aws;
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::ExternalAccountCredentials';
22  
23 1 ▶use Digest::SHA qw(hmac_sha256 hmac_sha256_hex sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 ▶sub _get_imdsv2_token {
Subroutine _get_imdsv2_token: called CC=7 SCAR=19.9
33 3 my ($self, $imdsv2_url) = @_;
34  
35 3 ▶ $imdsv2_url = 'http://169.254.169.254/latest/api/token'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $imdsv2_url and length $imdsv2_url 2 1 3
Condition66%2/3
defined $imdsv2_url and length $imdsv2_url
!ll&&!rl&&r
201
MC/DC50%1/2
defined $imdsv2_url and length $imdsv2_url
defined $imdsv2_url length $imdsv2_url
Truth table66%2/3
A and B
A= defined $imdsv2_urlB= length $imdsv2_url
ABresult
0X0
100
111
36 unless defined $imdsv2_url && length $imdsv2_url;
37 3 $log->debugf('Attempting to fetch AWS IMDSv2 token from: %s', $imdsv2_url);
38  
39 3 my $req = HTTP::Request->new(PUT => $imdsv2_url);
40 3 $req->header('X-aws-ec2-metadata-token-ttl-seconds' => '21600');
41  
42 3 my $res = $self->ua->request($req);
43 3 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 1 2 3
44 1 my $token = $res->decoded_content;
45 1 ▶ $token =~ s/^\s+|\s+$//g if defined $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 1 0 1
46 1 ▶ return $token if defined $token && length $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token and length $token 1 0 1
Condition33%1/3
defined $token and length $token
!ll&&!rl&&r
001
MC/DC0%0/2
defined $token and length $token
defined $token length $token
Truth table33%1/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
47 }
48  
49 2 $log->debugf('Could not obtain IMDSv2 token: %s', $res->status_line);
50 2 return;
51 }
52  
53 13 ▶sub _get_aws_security_credentials {
Subroutine _get_aws_security_credentials: called CC=39 SCAR=40.1
54 13 my ($self) = @_;
55  
56 # 1. Check static environment variables
57 13 my $access_key = $ENV{AWS_ACCESS_KEY_ID};
58 13 my $secret_key = $ENV{AWS_SECRET_ACCESS_KEY};
59 13 my $session_token = $ENV{AWS_SESSION_TOKEN};
60  
61 13 ▶ if ( defined $access_key
Branch: 1 branch
BranchTrueFalseTotal
if (defined $access_key and defined $secret_key and length $access_key and length $secret_key) 4 9 13
Condition66%6/9
defined $access_key and defined $secret_key and length $access_key and length $secret_key
!ll&&!rl&&r
904
defined $access_key and defined $secret_key and length $access_key
!ll&&!rl&&r
904
defined $access_key and defined $secret_key
!ll&&!rl&&r
904
MC/DC25%1/4
defined $access_key and defined $secret_key and length $access_key and length $secret_key
defined $access_key defined $secret_key length $access_key length $secret_key
Truth table40%2/5
A and B and C and D
A= defined $access_keyB= defined $secret_keyC= length $access_keyD= length $secret_key
ABCDresult
0XXX0
10XX0
110X0
11100
11111
62 && defined $secret_key
63 && length($access_key)
64 && length($secret_key))
65 {
66 4 $log->tracef('Using AWS static environment credentials with access key: %s',
67 $access_key);
68 4 return ($access_key, $secret_key, $session_token);
69 }
70  
71 # 2. Check AWS ECS / EKS Container task credentials
72 9 my $container_uri;
73 9 my $rel_uri = $ENV{AWS_CONTAINER_CREDENTIALS_RELATIVE_URI};
74 9 my $full_uri = $ENV{AWS_CONTAINER_CREDENTIALS_FULL_URI};
75  
76 9 ▶ if (defined $rel_uri && length $rel_uri) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rel_uri and length $rel_uri) { } 2 7 9
elsif (defined $full_uri and length $full_uri) { } 5 4 9
Condition66%2/3
defined $rel_uri and length $rel_uri
!ll&&!rl&&r
702
MC/DC50%1/2
defined $rel_uri and length $rel_uri
defined $rel_uri length $rel_uri
Truth table66%2/3
A and B
A= defined $rel_uriB= length $rel_uri
ABresult
0X0
100
111
77 # Defense-in-depth: Validate relative URI starts with single slash and contains no userinfo '@', double slashes, or path traversal
78 2 ▶ unless ($rel_uri =~ m{^/[^@]+$} && $rel_uri !~ m{//|\.\.}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rel_uri =~ m[^/[^\@]+$] and not $rel_uri =~ m[//|\.\.]) 1 1 2
Condition66%2/3
$rel_uri =~ m[^/[^\@]+$] and not $rel_uri =~ m[//|\.\.]
!ll&&!rl&&r
101
MC/DC50%1/2
$rel_uri =~ m[^/[^\@]+$] and not $rel_uri =~ m[//|\.\.]
$rel_uri =~ m[^/[^\@]+$] not $rel_uri =~ m[//|\.\.]
Truth table66%2/3
A and not(B)
A= $rel_uri =~ m[^/[^\@]+$]B= not $rel_uri =~ m[//|\.\.]
ABresult
0X0
100
111
79 1 $log->errorf(
80 'Security violation: Invalid AWS_CONTAINER_CREDENTIALS_RELATIVE_URI format: %s',
81 $rel_uri
82 );
83 1 Google::Auth::Error->throw(
84 "Invalid AWS_CONTAINER_CREDENTIALS_RELATIVE_URI format: $rel_uri");
85 }
86 1 $container_uri = 'http://169.254.170.2' . $rel_uri;
87 ▶ } elsif (defined $full_uri && length $full_uri) {
Condition66%2/3
defined $full_uri and length $full_uri
!ll&&!rl&&r
205
MC/DC50%1/2
defined $full_uri and length $full_uri
defined $full_uri length $full_uri
Truth table66%2/3
A and B
A= defined $full_uriB= length $full_uri
ABresult
0X0
100
111
88 5 my $uri_obj = URI->new($full_uri);
89 5 ▶ my $scheme = lc($uri_obj->scheme // '');
Condition50%1/2
$uri_obj->scheme // ""
l!l
50
MC/DC0%0/1
$uri_obj->scheme // ""
$uri_obj->scheme
Truth table50%1/2
A
A= $uri_obj->scheme
Aresult
11
00
90 5 ▶ my $host = lc($uri_obj->host // '');
Condition50%1/2
$uri_obj->host // ""
l!l
50
MC/DC0%0/1
$uri_obj->host // ""
$uri_obj->host
Truth table50%1/2
A
A= $uri_obj->host
Aresult
11
00
91  
92 5 $log->debugf("Validating FULL_URI: %s (Scheme: %s, Host: %s)",
93 $full_uri, $scheme, $host);
94  
95 # Enforce allowed schemes
96 5 ▶ unless ($scheme eq 'http' || $scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scheme eq "http" or $scheme eq "https") 1 4 5
Condition66%2/3
$scheme eq "http" or $scheme eq "https"
l!l&&r!l&&!r
401
MC/DC50%1/2
$scheme eq "http" or $scheme eq "https"
$scheme eq "http" $scheme eq "https"
Truth table66%2/3
A or B
A= $scheme eq "http"B= $scheme eq "https"
ABresult
1X1
011
000
97 1 $log->errorf(
98 'Security violation: AWS_CONTAINER_CREDENTIALS_FULL_URI scheme %s is not permitted',
99 $scheme
100 );
101 1 Google::Auth::Error->throw(
102 "AWS_CONTAINER_CREDENTIALS_FULL_URI scheme $scheme carries security violation"
103 );
104 }
105  
106 # Enforce allowed loopback / link-local container host allowlist per AWS SDK specifications
107 4 $host =~ s/^[\[\s]+|[\]\s]+$//g; # Trim brackets and spaces
108  
109 4 ▶ unless ($host eq '127.0.0.1'
Branch: 1 branch
BranchTrueFalseTotal
unless ($host eq "127.0.0.1" or $host eq "localhost" or $host eq "::1" or $host eq "169.254.170.2") 1 3 4
Condition77%7/9
$host eq "127.0.0.1" or $host eq "localhost" or $host eq "::1" or $host eq "169.254.170.2"
l!l&&r!l&&!r
301
$host eq "127.0.0.1" or $host eq "localhost" or $host eq "::1"
l!l&&r!l&&!r
211
$host eq "127.0.0.1" or $host eq "localhost"
l!l&&r!l&&!r
022
MC/DC50%2/4
$host eq "127.0.0.1" or $host eq "localhost" or $host eq "::1" or $host eq "169.254.170.2"
$host eq "127.0.0.1" $host eq "localhost" $host eq "::1" $host eq "169.254.170.2"
Truth table60%3/5
A or B or C or D
A= $host eq "127.0.0.1"B= $host eq "localhost"C= $host eq "::1"D= $host eq "169.254.170.2"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
110 || $host eq 'localhost'
111 || $host eq '::1'
112 || $host eq '169.254.170.2')
113 {
114 1 $log->errorf(
115 'Security violation: AWS_CONTAINER_CREDENTIALS_FULL_URI host %s is not permitted',
116 $host
117 );
118 1 Google::Auth::Error->throw(
119 "AWS_CONTAINER_CREDENTIALS_FULL_URI host $host carries security violation"
120 );
121 }
122 3 $container_uri = $full_uri;
123 }
124  
125 6 ▶ if ($container_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container_uri) 4 2 6
126 4 $log->debugf('Fetching AWS container task credentials from: %s',
127 $container_uri);
128 4 my $req = HTTP::Request->new(GET => $container_uri);
129 4 my $auth_token = $ENV{AWS_CONTAINER_AUTHORIZATION_TOKEN};
130 4 ▶ if (defined $auth_token && length $auth_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $auth_token and length $auth_token) 1 3 4
Condition66%2/3
defined $auth_token and length $auth_token
!ll&&!rl&&r
301
MC/DC50%1/2
defined $auth_token and length $auth_token
defined $auth_token length $auth_token
Truth table66%2/3
A and B
A= defined $auth_tokenB= length $auth_token
ABresult
0X0
100
111
131 1 $req->header(Authorization => $auth_token);
132 }
133  
134 4 my $res = $self->ua->request($req);
135 4 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 4 0 4
136 4 my $data = eval { decode_json($res->decoded_content) };
137 4 ▶ if ( ref $data eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH" and $data->{"AccessKeyId"} and $data->{"SecretAccessKey"}) 4 0 4
Condition33%2/6
ref $data eq "HASH" and $data->{"AccessKeyId"} and $data->{"SecretAccessKey"}
!ll&&!rl&&r
004
ref $data eq "HASH" and $data->{"AccessKeyId"}
!ll&&!rl&&r
004
MC/DC0%0/3
ref $data eq "HASH" and $data->{"AccessKeyId"} and $data->{"SecretAccessKey"}
ref $data eq "HASH" $data->{"AccessKeyId"} $data->{"SecretAccessKey"}
Truth table25%1/4
A and B and C
A= ref $data eq "HASH"B= $data->{"AccessKeyId"}C= $data->{"SecretAccessKey"}
ABCresult
0XX0
10X0
1100
1111
138 && $data->{AccessKeyId}
139 && $data->{SecretAccessKey})
140 {
141 $log->tracef('Successfully resolved AWS container credentials: %s',
142 4 $data->{AccessKeyId});
143 4 return ($data->{AccessKeyId}, $data->{SecretAccessKey}, $data->{Token});
144 }
145 }
146 0 $log->warnf('Failed to retrieve AWS container credentials from %s: %s',
147 $container_uri, $res->status_line);
148 }
149  
150 # 3. Check EC2 IMDSv2 Metadata Server
151 2 ▶ my $source = $self->credential_source // {};
Condition50%1/2
$self->credential_source // {}
l!l
20
MC/DC0%0/1
$self->credential_source // {}
$self->credential_source
Truth table50%1/2
A
A= $self->credential_source
Aresult
11
00
152 2 my $sec_url = $source->{url};
153  
154 2 ▶ if ($sec_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sec_url) 1 1 2
155 1 $log->debugf('Fetching AWS EC2 metadata credentials from: %s', $sec_url);
156 my $imdsv2_token =
157 1 $self->_get_imdsv2_token($source->{imdsv2_session_token_url});
158  
159 # Query attached role name
160 1 my $role_req = HTTP::Request->new(GET => $sec_url);
161 1 ▶ $role_req->header('X-aws-ec2-metadata-token' => $imdsv2_token)
Branch: 1 branch
BranchTrueFalseTotal
if defined $imdsv2_token 1 0 1
162 if defined $imdsv2_token;
163  
164 1 my $role_res = $self->ua->request($role_req);
165 1 ▶ if ($role_res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($role_res->is_success) 1 0 1
166 1 my $role_name = (split /\r?\n/, $role_res->decoded_content)[0];
167 1 ▶ $role_name =~ s/^\s+|\s+$//g if defined $role_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $role_name 1 0 1
168  
169 1 ▶ if (defined $role_name && length $role_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $role_name and length $role_name) 1 0 1
Condition33%1/3
defined $role_name and length $role_name
!ll&&!rl&&r
001
MC/DC0%0/2
defined $role_name and length $role_name
defined $role_name length $role_name
Truth table33%1/3
A and B
A= defined $role_nameB= length $role_name
ABresult
0X0
100
111
170 1 my $cred_url = $sec_url;
171 1 ▶ $cred_url .= '/' unless $cred_url =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $cred_url =~ m[/$] 1 0 1
172 1 $cred_url .= $role_name;
173  
174 1 my $cred_req = HTTP::Request->new(GET => $cred_url);
175 1 ▶ $cred_req->header('X-aws-ec2-metadata-token' => $imdsv2_token)
Branch: 1 branch
BranchTrueFalseTotal
if defined $imdsv2_token 1 0 1
176 if defined $imdsv2_token;
177  
178 1 my $cred_res = $self->ua->request($cred_req);
179 1 ▶ if ($cred_res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cred_res->is_success) 1 0 1
180 1 my $data = eval { decode_json($cred_res->decoded_content) };
181 1 ▶ if ( ref $data eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH" and $data->{"AccessKeyId"} and $data->{"SecretAccessKey"}) 1 0 1
Condition33%2/6
ref $data eq "HASH" and $data->{"AccessKeyId"} and $data->{"SecretAccessKey"}
!ll&&!rl&&r
001
ref $data eq "HASH" and $data->{"AccessKeyId"}
!ll&&!rl&&r
001
MC/DC0%0/3
ref $data eq "HASH" and $data->{"AccessKeyId"} and $data->{"SecretAccessKey"}
ref $data eq "HASH" $data->{"AccessKeyId"} $data->{"SecretAccessKey"}
Truth table25%1/4
A and B and C
A= ref $data eq "HASH"B= $data->{"AccessKeyId"}C= $data->{"SecretAccessKey"}
ABCresult
0XX0
10X0
1100
1111
182 && $data->{AccessKeyId}
183 && $data->{SecretAccessKey})
184 {
185 $log->tracef(
186 'Successfully resolved AWS EC2 metadata credentials for role %s: %s',
187 1 $role_name, $data->{AccessKeyId});
188 return ($data->{AccessKeyId}, $data->{SecretAccessKey},
189 1 $data->{Token});
190 }
191 }
192 }
193 }
194 0 $log->warnf('Failed to retrieve AWS EC2 metadata credentials: %s',
195 $role_res->status_line);
196 }
197  
198 $log->errorf(
199 1 'Unable to resolve AWS credentials from environment, container task, or EC2 metadata server'
200 );
201 1 Google::Auth::Error->throw(
202 'Unable to resolve AWS credentials from environment, container task, or EC2 metadata server'
203 );
204 }
205  
206 9 ▶sub _get_aws_region {
Subroutine _get_aws_region: called CC=15 SCAR=27.7
207 9 my ($self) = @_;
208  
209 9 my $region;
210 9 ▶ if (defined $ENV{AWS_REGION} && length $ENV{AWS_REGION}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ENV{'AWS_REGION'} and length $ENV{'AWS_REGION'}) { } 1 8 9
elsif (defined $ENV{'AWS_DEFAULT_REGION'} and length $ENV{'AWS_DEFAULT_REGION'}) { } 4 8 12
Condition100%3/3
defined $ENV{"AWS_REGION"} and length $ENV{"AWS_REGION"}
!ll&&!rl&&r
711
MC/DC100%2/2
defined $ENV{"AWS_REGION"} and length $ENV{"AWS_REGION"}
defined $ENV{"AWS_REGION"} length $ENV{"AWS_REGION"}
Truth table100%3/3
A and B
A= defined $ENV{"AWS_REGION"}B= length $ENV{"AWS_REGION"}
ABresult
0X0
100
111
211 1 $region = $ENV{AWS_REGION};
212 ▶ } elsif (defined $ENV{AWS_DEFAULT_REGION} && length $ENV{AWS_DEFAULT_REGION})
Condition66%2/3
defined $ENV{"AWS_DEFAULT_REGION"} and length $ENV{"AWS_DEFAULT_REGION"}
!ll&&!rl&&r
404
MC/DC50%1/2
defined $ENV{"AWS_DEFAULT_REGION"} and length $ENV{"AWS_DEFAULT_REGION"}
defined $ENV{"AWS_DEFAULT_REGION"} length $ENV{"AWS_DEFAULT_REGION"}
Truth table66%2/3
A and B
A= defined $ENV{"AWS_DEFAULT_REGION"}B= length $ENV{"AWS_DEFAULT_REGION"}
ABresult
0X0
100
111
213 {
214 4 $region = $ENV{AWS_DEFAULT_REGION};
215 }
216 9 ▶ if (defined $region && length $region) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $region and length $region) 5 4 9
Condition66%2/3
defined $region and length $region
!ll&&!rl&&r
405
MC/DC50%1/2
defined $region and length $region
defined $region length $region
Truth table66%2/3
A and B
A= defined $regionB= length $region
ABresult
0X0
100
111
217 5 return $region;
218 }
219  
220 4 ▶ my $source = $self->credential_source // {};
Condition50%1/2
$self->credential_source // {}
l!l
40
MC/DC0%0/1
$self->credential_source // {}
$self->credential_source
Truth table50%1/2
A
A= $self->credential_source
Aresult
11
00
221 4 my $region_url = $source->{region_url};
222  
223 4 ▶ if ($region_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($region_url) 2 2 4
224 2 $log->debugf('Fetching AWS region from metadata: %s', $region_url);
225 my $imdsv2_token =
226 2 $self->_get_imdsv2_token($source->{imdsv2_session_token_url});
227  
228 2 my $req = HTTP::Request->new(GET => $region_url);
229 2 ▶ $req->header('X-aws-ec2-metadata-token' => $imdsv2_token)
Branch: 1 branch
BranchTrueFalseTotal
if defined $imdsv2_token 0 2 2
230 if defined $imdsv2_token;
231  
232 2 my $res = $self->ua->request($req);
233 2 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 2 0 2
234 2 my $az = $res->decoded_content;
235 2 ▶ $az =~ s/^\s+|\s+$//g if defined $az;
Branch: 1 branch
BranchTrueFalseTotal
if defined $az 2 0 2
236 2 ▶ if (defined $az && length($az) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $az and length $az > 1) 2 0 2
Condition33%1/3
defined $az and length $az > 1
!ll&&!rl&&r
002
MC/DC0%0/2
defined $az and length $az > 1
defined $az length $az > 1
Truth table33%1/3
A and B
A= defined $azB= length $az > 1
ABresult
0X0
100
111
237 # Strip trailing availability zone character (e.g. us-west-2a -> us-west-2)
238 2 my $derived_region = substr($az, 0, -1);
239 2 ▶ if ($derived_region =~ m{^[a-z]{2,4}-[a-z]+-\d+$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($derived_region =~ /^[a-z]{2,4}-[a-z]+-\d+$/) { } 1 1 2
240 1 $log->tracef('Derived AWS region from AZ (%s): %s',
241 $az, $derived_region);
242 1 return $derived_region;
243 } else {
244 1 $log->warnf(
245 'Metadata AZ response (%s) produced invalid region format (%s), falling back to us-east-1',
246 $az, $derived_region
247 );
248 }
249 }
250 }
251 }
252  
253 3 return 'us-east-1';
254 }
255  
256 13 ▶sub retrieve_subject_token {
Subroutine retrieve_subject_token: called CC=8 SCAR=20.8
Pod undocumented
257 13 my ($self) = @_;
258  
259 13 my ($access_key, $secret_key, $session_token) =
260 $self->_get_aws_security_credentials();
261 9 my $region = $self->_get_aws_region();
262  
263 9 ▶ my $source = $self->credential_source // {};
Condition50%1/2
$self->credential_source // {}
l!l
90
MC/DC0%0/1
$self->credential_source // {}
$self->credential_source
Truth table50%1/2
A
A= $self->credential_source
Aresult
11
00
264 my $raw_url = $source->{regional_cred_verification_url}
265 9 ▶ // 'https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15';
Condition100%2/2
$source->{"regional_cred_verification_url"} // "https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15"
l!l
18
MC/DC100%1/1
$source->{"regional_cred_verification_url"} // "https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15"
$source->{"regional_cred_verification_url"}
Truth table100%2/2
A
A= $source->{"regional_cred_verification_url"}
Aresult
11
00
266  
267 9 $raw_url =~ s/\{region\}/$region/g;
268  
269 9 my $uri_obj = URI->new($raw_url);
270 9 my $host = $uri_obj->host;
271 9 my $method = 'POST';
272 9 ▶ my $uri = $uri_obj->path || '/';
Condition50%1/2
$uri_obj->path || '/'
l!l
09
MC/DC0%0/1
$uri_obj->path || '/'
$uri_obj->path
Truth table50%1/2
A
A= $uri_obj->path
Aresult
11
00
273 9 ▶ my $query = $uri_obj->query // 'Action=GetCallerIdentity&Version=2011-06-15';
Condition50%1/2
$uri_obj->query // "Action=GetCallerIdentity&Version=2011-06-15"
l!l
90
MC/DC0%0/1
$uri_obj->query // "Action=GetCallerIdentity&Version=2011-06-15"
$uri_obj->query
Truth table50%1/2
A
A= $uri_obj->query
Aresult
11
00
274  
275 9 my $t = gmtime();
276 9 my $amz_date = $t->strftime('%Y%m%dT%H%M%SZ');
277 9 my $datestamp = $t->strftime('%Y%m%d');
278  
279 9 my %headers = (
280 'host' => $host,
281 'x-amz-date' => $amz_date,
282 );
283 9 ▶ $headers{'x-amz-security-token'} = $session_token
Branch: 1 branch
BranchTrueFalseTotal
if defined $session_token and length $session_token 6 3 9
Condition66%2/3
defined $session_token and length $session_token
!ll&&!rl&&r
306
MC/DC50%1/2
defined $session_token and length $session_token
defined $session_token length $session_token
Truth table66%2/3
A and B
A= defined $session_tokenB= length $session_token
ABresult
0X0
100
111
284 if defined $session_token && length $session_token;
285  
286 9 my @sorted_header_names = sort keys %headers;
287 my $canonical_headers =
288 9 join('', map { $_ . ':' . $headers{$_} . "\n" } @sorted_header_names);
289 9 my $signed_headers = join(';', @sorted_header_names);
290  
291 9 my $payload_hash = sha256_hex('');
292  
293 9 my $canonical_request = join("\n",
294 $method, $uri, $query, $canonical_headers, $signed_headers, $payload_hash);
295  
296 9 my $algorithm = 'AWS4-HMAC-SHA256';
297 9 my $credential_scope = join('/', $datestamp, $region, 'sts', 'aws4_request');
298 9 my $string_to_sign = join("\n",
299 $algorithm, $amz_date, $credential_scope, sha256_hex($canonical_request));
300  
301 9 my $k_date = hmac_sha256($datestamp, 'AWS4' . $secret_key);
302 9 my $k_region = hmac_sha256($region, $k_date);
303 9 my $k_service = hmac_sha256('sts', $k_region);
304 9 my $k_signing = hmac_sha256('aws4_request', $k_service);
305  
306 9 my $signature = hmac_sha256_hex($string_to_sign, $k_signing);
307  
308 9 my $auth_header =
309 "$algorithm Credential=$access_key/$credential_scope, SignedHeaders=$signed_headers, Signature=$signature";
310 9 $headers{Authorization} = $auth_header;
311  
312 9 my @header_array;
313 9 foreach my $k (sort keys %headers) {
314 33 push @header_array, {key => $k, value => $headers{$k}};
315 }
316  
317 9 my $request_obj = {
318 url => $raw_url,
319 headers => \@header_array,
320 method => $method,
321 };
322  
323 9 my $subject_token = encode_base64(encode_json($request_obj), '');
324 9 $subject_token =~ s/\s+//g;
325 9 return $subject_token;
326 }
327  
328 1 1;
« blib/lib/Google/Auth/UserRefreshCredentials.pm ↑ Summary blib/lib/Google/Auth/Credentials.pm »