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

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