S3/Signer/V4.pm

filter: Statementstmt 98.1% 210 / 214 Branchbran 77.5% 45 / 58 Conditioncond 57.8% 44 / 76 MC/DCmcdc 18.1% 8 / 44 Subroutinesub 100% 27 / 27 Podpod 57.1% 4 / 7 totaltotal 79.3% 338 / 426 CC 59 SCAR 25.5
CC
59
Cov
86%
new
35.6
parse_host
15.1
signed_url
14.9
CRAP 12.8
↑ Summary S3/Bucket.pm »
1 package AWS::S3::Signer::V4;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use POSIX 'strftime';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Digest::SHA 'sha256_hex', 'hmac_sha256', 'hmac_sha256_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
14 6 ▶use constant PAAPI_REGION => {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 qw/
16 webservices.amazon.com.au us-west-2
17 webservices.amazon.com.br us-east-1
18 webservices.amazon.ca us-east-1
19 webservices.amazon.fr eu-west-1
20 webservices.amazon.de eu-west-1
21 webservices.amazon.in eu-west-1
22 webservices.amazon.it eu-west-1
23 webservices.amazon.co.jp us-west-2
24 webservices.amazon.com.mx us-east-1
25 webservices.amazon.sg us-west-2
26 webservices.amazon.es eu-west-1
27 webservices.amazon.com.tr eu-west-1
28 webservices.amazon.ae eu-west-1
29 webservices.amazon.co.uk eu-west-1
30 webservices.amazon.com us-east-1
31 /
32 6 };
33  
34 =head1 NAME
35  
36 AWS::S3::Signer::V4 - Create a version4 signature for Amazon Web Services
37  
38 =head1 SYNOPSIS
39  
40 use AWS::S3::Signer::V4;
41 use HTTP::Request::Common;
42 use LWP;
43  
44 my $signer = AWS::S3::Signer::V4->new(-access_key => 'AKIDEXAMPLE',
45 -secret_key => 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY');
46 my $ua = LWP::UserAgent->new();
47  
48 # Example POST request
49 my $request = POST('https://iam.amazonaws.com',
50 [Action=>'ListUsers',
51 Version=>'2010-05-08']);
52 $signer->sign($request);
53 my $response = $ua->request($request);
54  
55 # Example GET request
56 my $uri = URI->new('https://iam.amazonaws.com');
57 $uri->query_form(Action=>'ListUsers',
58 Version=>'2010-05-08');
59  
60 my $url = $signer->signed_url($uri); # This gives a signed URL that can be fetched by a browser
61 my $response = $ua->get($url);
62  
63 =head1 DESCRIPTION
64  
65 This module implement's Amazon Web Service's Signature version 4
66 (http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html).
67  
68 =head1 METHODS
69  
70 =over 4
71  
72 =item $signer = AWS::S3::Signer::V4->new(-access_key => $account_id,-secret_key => $private_key);
73  
74 Create a signing object using your AWS account ID and secret key. You
75 may also use the temporary security tokens received from Amazon's STS
76 service, either by passing the access and secret keys derived from the
77 token, or by passing a VM::EC2::Security::Token produced by the
78 VM::EC2 module.
79  
80 Arguments:
81  
82 Argument name Argument Value
83 ------------- --------------
84 -access_key An AWS access key (account ID)
85  
86 -secret_key An AWS secret key
87  
88 -security_token A VM::EC2::Security::Token object
89  
90 -service An AWS service
91  
92 -region An AWS region
93  
94  
95 If a security token is provided, it overrides any values given for
96 -access_key or -secret_key.
97  
98 If the environment variables EC2_ACCESS_KEY and/or EC2_SECRET_KEY are
99 set, their contents are used as defaults for -access_key and
100 -secret_key.
101  
102 If -service and/or -region is not provided, they are automtically determined
103 according to endpoint.
104  
105 =cut
106  
107 31 ▶sub new {
Subroutine new: called CC=13 SCAR=35.7
Pod documented
108 31 my $self = shift;
109 31 my %args = @_;
110  
111 31 my ( $id, $secret, $token, $region, $service );
112 31 ▶ if ( ref $args{-security_token}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"-security_token"} and $args{"-security_token"}->can("access_key_id")) 0 31 31
Condition33%1/3
ref $args{"-security_token"} and $args{"-security_token"}->can("access_key_id")
!ll&&!rl&&r
3100
MC/DC0%0/2
ref $args{"-security_token"} and $args{"-security_token"}->can("access_key_id")
ref $args{"-security_token"} $args{"-security_token"}->can("access_key_id")
Truth table33%1/3
A and B
A= ref $args{"-security_token"}B= $args{"-security_token"}->can("access_key_id")
ABresult
0X0
100
111
113 && $args{-security_token}->can('access_key_id') )
114 {
115 0 $id = $args{-security_token}->accessKeyId;
116 0 $secret = $args{-security_token}->secretAccessKey;
117 }
118  
119 $id ||= $args{-access_key} || $ENV{EC2_ACCESS_KEY}
120 31 ▶ or croak
Branch: 1 branch
BranchTrueFalseTotal
$id ||= $args{"-access_key"} || $ENV{"EC2_ACCESS_KEY"} or croak("Please provide -access_key parameter or define environment variable EC2_ACCESS_KEY") 31 0 31
Condition33%2/6
$id ||= $args{"-access_key"} || $ENV{"EC2_ACCESS_KEY"}
l!l&&r!l&&!r
0310
$args{'-access_key'} || $ENV{'EC2_ACCESS_KEY'}
l!l&&r!l&&!r
3100
MC/DC0%0/3
$id ||= $args{"-access_key"} || $ENV{"EC2_ACCESS_KEY"}
$id $args{'-access_key'} $ENV{'EC2_ACCESS_KEY'}
Truth table0%0/4
A ||= B || C
A= $idB= $args{'-access_key'}C= $ENV{'EC2_ACCESS_KEY'}
ABCresult
1XX1
01X1
0011
0000
121 "Please provide -access_key parameter or define environment variable EC2_ACCESS_KEY";
122 $secret ||= $args{-secret_key} || $ENV{EC2_SECRET_KEY}
123 31 ▶ or croak
Branch: 1 branch
BranchTrueFalseTotal
$secret ||= $args{"-secret_key"} || $ENV{"EC2_SECRET_KEY"} or croak("Please provide -secret_key or define environment variable EC2_SECRET_KEY") 31 0 31
Condition33%2/6
$secret ||= $args{"-secret_key"} || $ENV{"EC2_SECRET_KEY"}
l!l&&r!l&&!r
0310
$args{'-secret_key'} || $ENV{'EC2_SECRET_KEY'}
l!l&&r!l&&!r
3100
MC/DC0%0/3
$secret ||= $args{"-secret_key"} || $ENV{"EC2_SECRET_KEY"}
$secret $args{'-secret_key'} $ENV{'EC2_SECRET_KEY'}
Truth table0%0/4
A ||= B || C
A= $secretB= $args{'-secret_key'}C= $ENV{'EC2_SECRET_KEY'}
ABCresult
1XX1
01X1
0011
0000
124 "Please provide -secret_key or define environment variable EC2_SECRET_KEY";
125 31 ▶ $region = $args{-region} || $ENV{EC2_REGION};
Condition33%1/3
$args{'-region'} || $ENV{'EC2_REGION'}
l!l&&r!l&&!r
0031
MC/DC0%0/2
$args{'-region'} || $ENV{'EC2_REGION'}
$args{'-region'} $ENV{'EC2_REGION'}
Truth table33%1/3
A || B
A= $args{'-region'}B= $ENV{'EC2_REGION'}
ABresult
1X1
011
000
126 31 ▶ $service = $args{-service} || $ENV{EC2_SERVICE};
Condition33%1/3
$args{'-service'} || $ENV{'EC2_SERVICE'}
l!l&&r!l&&!r
0031
MC/DC0%0/2
$args{'-service'} || $ENV{'EC2_SERVICE'}
$args{'-service'} $ENV{'EC2_SERVICE'}
Truth table33%1/3
A || B
A= $args{'-service'}B= $ENV{'EC2_SERVICE'}
ABresult
1X1
011
000
127  
128 return bless {
129 access_key => $id,
130 secret_key => $secret,
131 region => $region,
132 region => $args{-region},
133 service => $args{-service},
134 (
135 defined( $args{-security_token} )
136 ? ( security_token => $args{-security_token} )
137 31 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'-security_token'} ? : 0 31 31
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0310
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
138 ),
139 },
140 ref $self || $self;
141 }
142  
143 41 ▶sub access_key { shift->{access_key} }
Subroutine access_key: called CC=1 SCAR=0.0
Pod undocumented
144 35 ▶sub secret_key { shift->{secret_key} }
Subroutine secret_key: called CC=1 SCAR=0.0
Pod undocumented
145  
146 =item $signer->sign($request [,$region] [,$payload_sha256_hex])
147  
148 Given an HTTP::Request object, add the headers required by AWS and
149 then sign it with a version 4 signature by adding an "Authorization"
150 header.
151  
152 The request must include a URL from which the AWS endpoint and service
153 can be derived, such as "ec2.us-east-1.amazonaws.com." In some cases
154 (e.g. S3 bucket operations) the endpoint does not indicate the
155 region. In this case, the region can be forced by passing a defined
156 value for $region. The current date and time will be added to the
157 request using an "X-Amz-Date header." To force the date and time to a
158 fixed value, include the "Date" header in the request.
159  
160 The request content, or "payload" is retrieved from the HTTP::Request
161 object by calling its content() method.. Under some circumstances the
162 payload is not included directly in the request, but is in an external
163 file that will be uploaded as the request is executed. In this case,
164 you must pass a second argument containing the results of running
165 sha256_hex() (from the Digest::SHA module) on the content.
166  
167 The method returns a true value if successful. On errors, it will
168 throw an exception.
169  
170 =item $url = $signer->signed_url($request)
171  
172 This method will generate a signed GET URL for the request. The URL
173 will include everything needed to perform the request.
174  
175 =cut
176  
177 29 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
178 29 my $self = shift;
179 29 my ( $request, $region, $payload_sha256_hex ) = @_;
180 29 $self->_add_date_header($request);
181 29 $self->_sign( $request, $region, $payload_sha256_hex );
182 }
183  
184 =item my $url $signer->signed_url($request_or_uri [,$expires] [,$verb])
185  
186 Pass an HTTP::Request, a URI object, or just a plain URL string
187 containing the proper endpoint and parameters needed for an AWS REST
188 API Call. This method will return an appropriately signed request as a
189 URI object, which can be shared with non-AWS users for the purpose of,
190 e.g., accessing an object in a private S3 bucket.
191  
192 Pass an optional $expires argument to indicate that the URL will only
193 be valid for a finite period of time. The value of the argument is in
194 seconds.
195  
196 Pass an optional verb which is useful for HEAD requests, this defaults to GET.
197  
198 =cut
199  
200 6 ▶sub signed_url {
Subroutine signed_url: called CC=14 SCAR=27.0
Pod documented
201 6 my $self = shift;
202 6 my ( $arg1, $expires, $verb ) = @_;
203 6 my ( $request, $uri );
204  
205 6 ▶ $verb ||= 'GET';
Condition100%2/2
$verb ||= "GET"
l!l
24
MC/DC100%1/1
$verb ||= "GET"
$verb
Truth table100%2/2
A
A= $verb
Aresult
11
00
206 6 $verb = uc($verb);
207  
208 6 my $incorrect_verbs = {
209 POST => 1,
210 PUT => 1
211 };
212  
213 6 ▶ if ( exists( $incorrect_verbs->{$verb} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $incorrect_verbs->{$verb}) 0 6 6
214 0 die "Use AWS::S3::Signer::V4->sign sub for $verb method";
215 }
216  
217 6 ▶ if ( ref $arg1 && UNIVERSAL::isa( $arg1, 'HTTP::Request' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg1 and UNIVERSAL::isa($arg1, "HTTP::Request")) 2 4 6
Condition100%3/3
ref $arg1 and UNIVERSAL::isa($arg1, "HTTP::Request")
!ll&&!rl&&r
222
MC/DC100%2/2
ref $arg1 and UNIVERSAL::isa($arg1, "HTTP::Request")
ref $arg1 UNIVERSAL::isa($arg1, "HTTP::Request")
Truth table100%3/3
A and B
A= ref $arg1B= UNIVERSAL::isa($arg1, "HTTP::Request")
ABresult
0X0
100
111
218 2 $request = $arg1;
219 2 $uri = $request->uri;
220 2 my $content = $request->content;
221 2 ▶ $uri->query($content) if $content;
Branch: 1 branch
BranchTrueFalseTotal
if $content 0 2 2
222 2 ▶ if ( my $date =
Branch: 1 branch
BranchTrueFalseTotal
if (my $date = $request->header("X-Amz-Date") || $request->header("Date")) 2 0 2
Condition33%1/3
$request->header('X-Amz-Date') || $request->header('Date')
l!l&&r!l&&!r
020
MC/DC0%0/2
$request->header('X-Amz-Date') || $request->header('Date')
$request->header('X-Amz-Date') $request->header('Date')
Truth table33%1/3
A || B
A= $request->header('X-Amz-Date')B= $request->header('Date')
ABresult
1X1
011
000
223 $request->header('X-Amz-Date') || $request->header('Date') )
224 {
225 2 $uri->query_param( 'Date' => $date );
226 }
227 }
228  
229 6 ▶ $uri ||= URI->new($arg1);
Condition66%2/3
$uri ||= "URI"->new($arg1)
l!l&&r!l&&!r
240
MC/DC0%0/2
$uri ||= "URI"->new($arg1)
$uri "URI"->new($arg1)
Truth table66%2/3
A ||= B
A= $uriB= "URI"->new($arg1)
ABresult
1X1
011
000
230 6 ▶ my $date = $uri->query_param_delete('Date')
Condition66%2/3
$uri->query_param_delete('Date') || $uri->query_param_delete('X-Amz-Date')
l!l&&r!l&&!r
402
MC/DC50%1/2
$uri->query_param_delete('Date') || $uri->query_param_delete('X-Amz-Date')
$uri->query_param_delete('Date') $uri->query_param_delete('X-Amz-Date')
Truth table66%2/3
A || B
A= $uri->query_param_delete('Date')B= $uri->query_param_delete('X-Amz-Date')
ABresult
1X1
011
000
231 || $uri->query_param_delete('X-Amz-Date');
232 6 $request = HTTP::Request->new( $verb => $uri );
233 6 $request->header( 'Date' => $date );
234 6 $uri = $request->uri; # because HTTP::Request->new() copies the uri!
235  
236 6 ▶ return $uri if $uri->query_param('X-Amz-Signature');
Branch: 1 branch
BranchTrueFalseTotal
if $uri->query_param("X-Amz-Signature") 0 6 6
237  
238 6 my $scope = $self->_scope($request);
239  
240 6 $uri->query_param( 'X-Amz-Algorithm' => $self->_algorithm );
241 6 $uri->query_param( 'X-Amz-Credential' => $self->access_key . '/' . $scope );
242 6 $uri->query_param( 'X-Amz-Date' => $self->_datetime($request) );
243 6 ▶ $uri->query_param( 'X-Amz-Expires' => $expires ) if $expires;
Branch: 1 branch
BranchTrueFalseTotal
if $expires 2 4 6
244 6 $uri->query_param( 'X-Amz-SignedHeaders' => 'host' );
245  
246 # If there was a security token passed, we need to supply it as part of the authorization
247 # because AWS requires it to validate IAM Role temporary credentials.
248  
249 6 ▶ if ( defined( $self->{security_token} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"security_token"}) 0 6 6
250 0 $uri->query_param( 'X-Amz-Security-Token' => $self->{security_token} );
251 }
252  
253 # Since we're providing auth via query parameters, we need to include UNSIGNED-PAYLOAD
254 # http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
255 # it seems to only be needed for S3.
256  
257 6 ▶ if ( $scope =~ /\/s3\/aws4_request$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope =~ m[/s3/aws4_request$]) { } 3 3 6
258 3 $self->_sign( $request, undef, 'UNSIGNED-PAYLOAD' );
259 }
260 else {
261 3 $self->_sign($request);
262 }
263  
264 6 my ( $algorithm, $credential, $signedheaders, $signature ) =
265 $request->header('Authorization') =~
266 /^(\S+) Credential=(\S+), SignedHeaders=(\S+), Signature=(\S+)/;
267 6 $uri->query_param_append( 'X-Amz-Signature' => $signature );
268 6 return $uri;
269 }
270  
271 29 ▶sub _add_date_header {
Subroutine _add_date_header: called CC=2 SCAR=7.0
272 29 my $self = shift;
273 29 my $request = shift;
274 29 my $datetime;
275 29 ▶ unless ( $datetime = $request->header('x-amz-date') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($datetime = $request->header("x-amz-date")) 29 0 29
276 29 $datetime = $self->_zulu_time($request);
277 29 $request->header( 'x-amz-date' => $datetime );
278 }
279 }
280  
281 41 ▶sub _scope {
Subroutine _scope: called CC=5 SCAR=16.5
282 41 my $self = shift;
283 41 my ( $request, $region ) = @_;
284 41 my $host = $request->uri->host;
285 41 my $datetime = $self->_datetime($request);
286 41 my ($date) = $datetime =~ /^(\d+)T/;
287 41 my $service;
288  
289 41 ( $service, $region ) = $self->parse_host( $host, $region );
290  
291 41 ▶ $service ||= $self->{service} || 's3';
Condition60%3/5
$service ||= $self->{"service"} || "s3"
l!l&&r!l&&!r
10310
$self->{'service'} || 's3'
l!l
031
MC/DC0%0/2
$service ||= $self->{"service"} || "s3"
$service $self->{'service'}
Truth table33%1/3
A ||= B
A= $serviceB= $self->{'service'}
ABresult
1X1
011
000
292 41 ▶ $region ||= $self->{region} || 'us-east-1'; # default
Condition60%3/5
$region ||= $self->{"region"} || "us-east-1"
l!l&&r!l&&!r
10310
$self->{'region'} || 'us-east-1'
l!l
031
MC/DC0%0/2
$region ||= $self->{"region"} || "us-east-1"
$region $self->{'region'}
Truth table33%1/3
A ||= B
A= $regionB= $self->{'region'}
ABresult
1X1
011
000
293 41 return "$date/$region/$service/aws4_request";
294 }
295  
296 1208 ▶sub parse_host {
Subroutine parse_host: called CC=15 SCAR=27.2
Pod undocumented
297 1208 my $self = shift;
298 1208 my $host = shift;
299 1208 my $region = shift;
300  
301 # this entire thing should probably refactored into its own
302 # distribution, a la https://github.com/zirkelc/amazon-s3-url
303  
304 # https://docs.aws.amazon.com/prescriptive-guidance/latest/defining-bucket-names-data-lakes/faq.html
305 # Only lowercase letters, numbers, dashes, and dots are allowed in S3 bucket names.
306 # Bucket names must be three to 63 characters in length,
307 # must begin and end with a number or letter,
308 # and cannot be in an IP address format.
309 1208 my $bucket_re = '[a-z0-9][a-z0-9\-\.]{1,61}[a-z0-9]';
310 1208 my $domain_re = 'amazonaws\.com';
311 1208 my $region_re = '(?:af|ap|ca|eu|il|me|mx|sa|us)-[a-z]+-\d';
312  
313 1208 my ( $service, $url_style );
314  
315 # listed in order of appearance found in the docs:
316 # https://community.aws/content/2biM1C0TkMkvJ2BLICiff8MKXS9/format-and-parse-amazon-s3-url?lang=en
317 1208 ▶ if ( $host =~ /^(\w+)([-.])($region_re)\.$domain_re/ ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($host =~ /^(\w+)([-.])($region_re)\.$domain_re/) { } 384 824 1208
elsif ($host =~ /^$bucket_re\.($region_re)\.s3\.$domain_re/) { } 192 632 824
elsif ($host =~ /^s3\.$domain_re/) { } 192 440 632
elsif ($host =~ /^$bucket_re\.s3\.$domain_re/) { } 192 248 440
elsif ($host =~ /^$bucket_re\.s3[\.-]($region_re)\.$domain_re/) { } 2 246 248
elsif ($host =~ /^([\w-]+)\.([\w-]+)\.$domain_re/) { } 193 53 246
elsif ($host =~ /^([\w-]+)\.$domain_re/) { } 7 46 53
elsif (exists {webservices.amazon.ae => 'eu-west-1', webservices.amazon.ca => 'us-east-1', webservices.amazon.co.jp => 'us-west-2', webservices.amazon.co.uk => 'eu-west-1', webservices.amazon.com => 'us-east-1', webservices.amazon.com.au => 'us-west-2', webservices.amazon.com.br => 'us-east-1', webservices.amazon.com.mx => 'us-east-1', webservices.amazon.com.tr => 'eu-west-1', webservices.amazon.de => 'eu-west-1', webservices.amazon.es => 'eu-west-1', webservices.amazon.fr => 'eu-west-1', webservices.amazon.in => 'eu-west-1', webservices.amazon.it => 'eu-west-1', webservices.amazon.sg => 'us-west-2'}->{$host}) { } 15 31 46
318 384 $service = $1;
319 384 ▶ $region ||= $3;
Condition66%2/3
$region ||= $3
l!l&&r!l&&!r
1921920
MC/DC0%0/2
$region ||= $3
$region $3
Truth table66%2/3
A ||= B
A= $regionB= $3
ABresult
1X1
011
000
320 384 ▶ $url_style = $2 eq '-' ? 'regional dash-style' : 'regional dot-style';
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '-' ? : 192 192 384
321 }
322 elsif ( $host =~ /^$bucket_re\.($region_re)\.s3\.$domain_re/ ) {
323 192 $service = 's3';
324 192 ▶ $region ||= $1;
Condition66%2/3
$region ||= $1
l!l&&r!l&&!r
96960
MC/DC0%0/2
$region ||= $1
$region $1
Truth table66%2/3
A ||= B
A= $regionB= $1
ABresult
1X1
011
000
325 192 $url_style = 'regional virtual-hosted-style';
326 }
327 elsif ( $host =~ /^s3\.$domain_re/ ) {
328 192 $service = 's3';
329 192 $region = 'us-east-1';
330 192 $url_style = 'legacy with path-style';
331 }
332 elsif ( $host =~ /^$bucket_re\.s3\.$domain_re/ ) {
333 192 $service = 's3';
334 192 ▶ $region ||= 'us-east-1';
Condition100%2/2
$region ||= "us-east-1"
l!l
9696
MC/DC100%1/1
$region ||= "us-east-1"
$region
Truth table100%2/2
A
A= $region
Aresult
11
00
335 192 $url_style = 'legacy with virtual-hosted-style';
336 }
337 elsif ( $host =~ /^$bucket_re\.s3[\.-]($region_re)\.$domain_re/ ) {
338 2 $service = 's3';
339 2 ▶ $region ||= $1;
Condition33%1/3
$region ||= $1
l!l&&r!l&&!r
020
MC/DC0%0/2
$region ||= $1
$region $1
Truth table33%1/3
A ||= B
A= $regionB= $1
ABresult
1X1
011
000
340 2 $url_style = 'regional virtual-hosted-style';
341 }
342 elsif ($host =~ /^([\w-]+)\.([\w-]+)\.$domain_re/) {
343 193 $service = $1;
344 193 ▶ $region ||= $2;
Condition66%2/3
$region ||= $2
l!l&&r!l&&!r
96970
MC/DC0%0/2
$region ||= $2
$region $2
Truth table66%2/3
A ||= B
A= $regionB= $2
ABresult
1X1
011
000
345 193 $url_style = 'legacy path-style service';
346 }
347 elsif ( $host =~ /^([\w-]+)\.$domain_re/ ) {
348 7 $service = $1;
349 7 $region = 'us-east-1';
350 7 $url_style = 'legacy path-style';
351 }
352 elsif ( exists PAAPI_REGION->{$host} ) {
353 15 $service = 'ProductAdvertisingAPI';
354 15 $region = PAAPI_REGION->{$host};
355 }
356  
357 1208 return ( $service, $region, $url_style );
358 }
359  
360 35 ▶sub _parse_scope {
Subroutine _parse_scope: called CC=1 SCAR=0.0
361 35 my $self = shift;
362 35 my $scope = shift;
363 35 return split '/', $scope;
364 }
365  
366 82 ▶sub _datetime {
Subroutine _datetime: called CC=2 SCAR=7.0
367 82 my $self = shift;
368 82 my $request = shift;
369 82 ▶ return $request->header('x-amz-date') || $self->_zulu_time($request);
Condition66%2/3
$request->header('x-amz-date') || $self->_zulu_time($request)
l!l&&r!l&&!r
58240
MC/DC0%0/2
$request->header('x-amz-date') || $self->_zulu_time($request)
$request->header('x-amz-date') $self->_zulu_time($request)
Truth table66%2/3
A || B
A= $request->header('x-amz-date')B= $self->_zulu_time($request)
ABresult
1X1
011
000
370 }
371  
372 41 ▶sub _algorithm { return 'AWS4-HMAC-SHA256' }
Subroutine _algorithm: called CC=1 SCAR=0.0
373  
374 35 ▶sub _sign {
Subroutine _sign: called CC=3 SCAR=11.0
375 35 my $self = shift;
376 35 my ( $request, $region, $payload_sha256_hex ) = @_;
377 35 ▶ return if $request->header('Authorization'); # don't overwrite
Branch: 1 branch
BranchTrueFalseTotal
if $request->header("Authorization") 0 35 35
378  
379 35 my $datetime = $self->_datetime($request);
380  
381 35 ▶ unless ( $request->header('host') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->header("host")) 35 0 35
382 35 my $host = $request->uri->host;
383 35 $request->header( host => $host );
384 }
385  
386 35 my $scope = $self->_scope( $request, $region );
387 35 my ( $date, $service );
388 35 ( $date, $region, $service ) = $self->_parse_scope($scope);
389  
390 35 my $secret_key = $self->secret_key;
391 35 my $access_key = $self->access_key;
392 35 my $algorithm = $self->_algorithm;
393  
394 35 my ( $hashed_request, $signed_headers ) =
395 $self->_hash_canonical_request( $request, $payload_sha256_hex );
396 35 my $string_to_sign =
397 $self->_string_to_sign( $datetime, $scope, $hashed_request );
398 35 my $signature =
399 $self->_calculate_signature( $secret_key, $service, $region, $date,
400 $string_to_sign );
401 35 $request->header( Authorization =>
402 "$algorithm Credential=$access_key/$scope, SignedHeaders=$signed_headers, Signature=$signature"
403 );
404 }
405  
406 53 ▶sub _zulu_time {
Subroutine _zulu_time: called CC=2 SCAR=6.9
407 53 my $self = shift;
408 53 my $request = shift;
409 53 my $date = $request->header('Date');
410 53 ▶ my @datetime = $date ? gmtime( str2time($date) ) : gmtime();
Branch: 1 branch
BranchTrueFalseTotal
$date ? : 45 8 53
411 53 return strftime( '%Y%m%dT%H%M%SZ', @datetime );
412 }
413  
414 35 ▶sub _hash_canonical_request {
Subroutine _hash_canonical_request: called CC=11 SCAR=24.0
415 35 my $self = shift;
416 35 my ( $request, $hashed_payload ) =
417 @_; # (HTTP::Request,sha256_hex($content))
418 35 my $method = $request->method;
419 35 my $uri = $request->uri;
420 35 ▶ my $path = $uri->path || '/';
Condition100%2/2
$uri->path || '/'
l!l
296
MC/DC100%1/1
$uri->path || '/'
$uri->path
Truth table100%2/2
A
A= $uri->path
Aresult
11
00
421 35 my @params = $uri->query_form;
422 35 my $headers = $request->headers;
423 35 ▶ $hashed_payload ||= sha256_hex( $request->content );
Condition66%2/3
$hashed_payload ||= &sha256_hex($request->content)
l!l&&r!l&&!r
3050
MC/DC0%0/2
$hashed_payload ||= &sha256_hex($request->content)
$hashed_payload &sha256_hex($request->content)
Truth table66%2/3
A ||= B
A= $hashed_payloadB= &sha256_hex($request->content)
ABresult
1X1
011
000
424  
425 # canonicalize query string
426  
427 # in the case of the S3 api, but its still expected to be part of a
428 # canonical request.
429 35 ▶ if (scalar(@params) == 0 && defined($uri->query) && $uri->query ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0 and defined $uri->query and $uri->query ne "") 1 34 35
Condition83%5/6
scalar @params == 0 and defined $uri->query and $uri->query ne ""
!ll&&!rl&&r
3401
scalar @params == 0 and defined $uri->query
!ll&&!rl&&r
11231
MC/DC66%2/3
scalar @params == 0 and defined $uri->query and $uri->query ne ""
scalar @params == 0 defined $uri->query $uri->query ne ""
Truth table75%3/4
A and B and C
A= scalar @params == 0B= defined $uri->queryC= $uri->query ne ""
ABCresult
0XX0
10X0
1100
1111
430 1 push @params, ($uri->query, '');
431 }
432  
433 35 my %canonical;
434 35 ▶ while ( my ( $key, $value ) = splice( @params, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@params, 0, 2)) 44 35 79
435 44 $key = uri_escape($key);
436 44 $value = uri_escape($value);
437 44 push @{ $canonical{$key} }, $value;
438 }
439 my $canonical_query_string = join '&', map {
440 35 my $key = $_;
441 44 map { "$key=$_" } sort @{ $canonical{$key} }
442 } sort keys %canonical;
443  
444 # canonicalize the request headers
445 35 my ( @canonical, %signed_fields );
446 35 for my $header ( sort map { lc } $headers->header_field_names ) {
447 138 ▶ next if $header =~ /^date$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $header =~ /^date$/i 33 105 138
448 105 my @values = $headers->header($header);
449  
450 # remove redundant whitespace
451 105 foreach (@values) {
452 105 ▶ next if /^".+"$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^".+"$/ 0 105 105
453 105 s/^\s+//;
454 105 s/\s+$//;
455 105 s/(\s)\s+/$1/g;
456 }
457 105 push @canonical, "$header:" . join( ',', @values );
458 105 $signed_fields{$header}++;
459 }
460 35 my $canonical_headers = join "\n", @canonical;
461 35 $canonical_headers .= "\n";
462 35 my $signed_headers = join ';', sort map { lc } keys %signed_fields;
463  
464 35 my $canonical_request = join( "\n",
465 $method, $path, $canonical_query_string,
466 $canonical_headers, $signed_headers, $hashed_payload );
467 35 my $request_digest = sha256_hex($canonical_request);
468  
469 35 return ( $request_digest, $signed_headers );
470 }
471  
472 35 ▶sub _string_to_sign {
Subroutine _string_to_sign: called CC=1 SCAR=0.0
473 35 my $self = shift;
474 35 my ( $datetime, $credential_scope, $hashed_request ) = @_;
475 35 return join( "\n",
476 'AWS4-HMAC-SHA256', $datetime, $credential_scope, $hashed_request );
477 }
478  
479 =item $signing_key = AWS::S3::Signer::V4->signing_key($secret_access_key,$service_name,$region,$date)
480  
481 Return just the signing key in the event you wish to roll your own signature.
482  
483 =cut
484  
485 35 ▶sub signing_key {
Subroutine signing_key: called CC=1 SCAR=0.0
Pod documented
486 35 my $self = shift;
487 35 my ( $kSecret, $service, $region, $date ) = @_;
488 35 my $kDate = hmac_sha256( $date, 'AWS4' . $kSecret );
489 35 my $kRegion = hmac_sha256( $region, $kDate );
490 35 my $kService = hmac_sha256( $service, $kRegion );
491 35 my $kSigning = hmac_sha256( 'aws4_request', $kService );
492 35 return $kSigning;
493 }
494  
495 35 ▶sub _calculate_signature {
Subroutine _calculate_signature: called CC=1 SCAR=0.0
496 35 my $self = shift;
497 35 my ( $kSecret, $service, $region, $date, $string_to_sign ) = @_;
498 35 my $kSigning = $self->signing_key( $kSecret, $service, $region, $date );
499 35 return hmac_sha256_hex( $string_to_sign, $kSigning );
500 }
501  
502 6 1;
503  
504 =back
505  
506 =head1 SEE ALSO
507  
508 L<VM::EC2>
509  
510 =head1 AUTHOR
511  
512 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
513  
514 Forked by leejo for use in L<AWS::S3>.
515  
516 Copyright (c) 2014 Ontario Institute for Cancer Research
517  
518 This package and its accompanying libraries is free software; you can
519 redistribute it and/or modify it under the terms of the GPL (either
520 version 1, or at your option, any later version) or the Artistic
521 License 2.0. Refer to LICENSE for the full license text. In addition,
522 please see DISCLAIMER.txt for disclaimers of warranty.
523  
524 =cut
525  
↑ Summary S3/Bucket.pm »