| 1 | package AWS::S3::Signer::V4; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use POSIX 'strftime'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Digest::SHA 'sha256_hex', 'hmac_sha256', 'hmac_sha256_hex'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use Date::Parse; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 12 | ▶ | 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 | 12 | ▶ | 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 | 12 | }; | |||||||||||||||||||||||||||||||||||||||||
| 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 | 104 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=35.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 104 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 104 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 104 | my ( $id, $secret, $token, $region, $service ); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 104 | ▶ | if ( ref $args{-security_token} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $args{"-security_token"} and $args{"-security_token"}->can("access_key_id")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 104 | ▶ | or croak | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$id ||= $args{"-access_key"} || $ENV{"EC2_ACCESS_KEY"}
$args{'-access_key'} || $ENV{'EC2_ACCESS_KEY'}
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | "Please provide -access_key parameter or define environment variable EC2_ACCESS_KEY"; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | $secret ||= $args{-secret_key} || $ENV{EC2_SECRET_KEY} | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 104 | ▶ | or croak | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$secret ||= $args{"-secret_key"} || $ENV{"EC2_SECRET_KEY"}
$args{'-secret_key'} || $ENV{'EC2_SECRET_KEY'}
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | "Please provide -secret_key or define environment variable EC2_SECRET_KEY"; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 104 | ▶ | $region = $args{-region} || $ENV{EC2_REGION}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{'-region'} || $ENV{'EC2_REGION'}
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 104 | ▶ | $service = $args{-service} || $ENV{EC2_SERVICE}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{'-service'} || $ENV{'EC2_SERVICE'}
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 104 | ▶ | : () | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 139 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | ref $self || $self; | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 122 | ▶ | sub access_key { shift->{access_key} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
access_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 114 | ▶ | 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 | 106 | ▶ | sub sign { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sign: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 106 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 106 | my ( $request, $region, $payload_sha256_hex ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 106 | $self->_add_date_header($request); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 106 | $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 | 8 | ▶ | sub signed_url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
signed_url: called CC=14 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 8 | my ( $arg1, $expires, $verb ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 8 | my ( $request, $uri ); | |||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 8 | ▶ | $verb ||= 'GET'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$verb ||= "GET"
MC/DC100%1/1
$verb ||= "GET"
$verb
Truth table100%2/2
A
A= $verb
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 8 | $verb = uc($verb); | |||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 8 | my $incorrect_verbs = { | |||||||||||||||||||||||||||||||||||||||||
| 209 | POST => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 210 | PUT => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 211 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 8 | ▶ | if ( exists( $incorrect_verbs->{$verb} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | die "Use AWS::S3::Signer::V4->sign sub for $verb method"; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 8 | ▶ | if ( ref $arg1 && UNIVERSAL::isa( $arg1, 'HTTP::Request' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $arg1 and UNIVERSAL::isa($arg1, "HTTP::Request")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | ▶ | if ( my $date = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$request->header('X-Amz-Date') || $request->header('Date')
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')
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | $request->header('X-Amz-Date') || $request->header('Date') ) | ||||||||||||||||||||||||||||||||||||||||||
| 224 | { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 2 | $uri->query_param( 'Date' => $date ); | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 8 | ▶ | $uri ||= URI->new($arg1); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$uri ||= "URI"->new($arg1)
MC/DC0%0/2
$uri ||= "URI"->new($arg1)
$uri
"URI"->new($arg1)
Truth table66%2/3
A ||= B
A= $uriB= "URI"->new($arg1)
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 8 | ▶ | my $date = $uri->query_param_delete('Date') | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$uri->query_param_delete('Date') || $uri->query_param_delete('X-Amz-Date')
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')
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | || $uri->query_param_delete('X-Amz-Date'); | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 8 | $request = HTTP::Request->new( $verb => $uri ); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 8 | $request->header( 'Date' => $date ); | |||||||||||||||||||||||||||||||||||||||||
| 234 | 8 | $uri = $request->uri; # because HTTP::Request->new() copies the uri! | |||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 8 | ▶ | return $uri if $uri->query_param('X-Amz-Signature'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 8 | my $scope = $self->_scope($request); | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 8 | $uri->query_param( 'X-Amz-Algorithm' => $self->_algorithm ); | |||||||||||||||||||||||||||||||||||||||||
| 241 | 8 | $uri->query_param( 'X-Amz-Credential' => $self->access_key . '/' . $scope ); | |||||||||||||||||||||||||||||||||||||||||
| 242 | 8 | $uri->query_param( 'X-Amz-Date' => $self->_datetime($request) ); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 8 | ▶ | $uri->query_param( 'X-Amz-Expires' => $expires ) if $expires; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 8 | $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 | 8 | ▶ | if ( defined( $self->{security_token} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 8 | ▶ | if ( $scope =~ /\/s3\/aws4_request$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 5 | $self->_sign( $request, undef, 'UNSIGNED-PAYLOAD' ); | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 3 | $self->_sign($request); | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 8 | my ( $algorithm, $credential, $signedheaders, $signature ) = | |||||||||||||||||||||||||||||||||||||||||
| 265 | $request->header('Authorization') =~ | ||||||||||||||||||||||||||||||||||||||||||
| 266 | /^(\S+) Credential=(\S+), SignedHeaders=(\S+), Signature=(\S+)/; | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 8 | $uri->query_param_append( 'X-Amz-Signature' => $signature ); | |||||||||||||||||||||||||||||||||||||||||
| 268 | 8 | return $uri; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 106 | ▶ | sub _add_date_header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_date_header: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 106 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 106 | my $request = shift; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 106 | my $datetime; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 106 | ▶ | unless ( $datetime = $request->header('x-amz-date') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 106 | $datetime = $self->_zulu_time($request); | |||||||||||||||||||||||||||||||||||||||||
| 277 | 106 | $request->header( 'x-amz-date' => $datetime ); | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 122 | ▶ | sub _scope { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_scope: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 122 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 122 | my ( $request, $region ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 122 | my $host = $request->uri->host; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 122 | my $datetime = $self->_datetime($request); | |||||||||||||||||||||||||||||||||||||||||
| 286 | 122 | my ($date) = $datetime =~ /^(\d+)T/; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 122 | my $service; | |||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 122 | ( $service, $region ) = $self->parse_host( $host, $region ); | |||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | 122 | ▶ | $service ||= $self->{service} || 's3'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
$service ||= $self->{"service"} || "s3"
$self->{'service'} || 's3'
MC/DC0%0/2
$service ||= $self->{"service"} || "s3"
$service
$self->{'service'}
Truth table33%1/3
A ||= B
A= $serviceB= $self->{'service'}
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 122 | ▶ | $region ||= $self->{region} || 'us-east-1'; # default | ||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
$region ||= $self->{"region"} || "us-east-1"
$self->{'region'} || 'us-east-1'
MC/DC0%0/2
$region ||= $self->{"region"} || "us-east-1"
$region
$self->{'region'}
Truth table33%1/3
A ||= B
A= $regionB= $self->{'region'}
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 122 | return "$date/$region/$service/aws4_request"; | |||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 1295 | ▶ | sub parse_host { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_host: called CC=15 SCAR=27.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 1295 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 1295 | my $host = shift; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 1295 | 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 | 1295 | my $bucket_re = '[a-z0-9][a-z0-9\-\.]{1,61}[a-z0-9]'; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 1295 | my $domain_re = 'amazonaws\.com'; | |||||||||||||||||||||||||||||||||||||||||
| 311 | 1295 | my $region_re = '(?:af|ap|ca|eu|il|me|mx|sa|us)-[a-z]+-\d'; | |||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 1295 | 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 | 1295 | ▶ | if ( $host =~ /^(\w+)([-.])($region_re)\.$domain_re/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 386 | $service = $1; | |||||||||||||||||||||||||||||||||||||||||
| 319 | 386 | ▶ | $region ||= $3; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$region ||= $3
MC/DC0%0/2
$region ||= $3
$region
$3
Truth table66%2/3
A ||= B
A= $regionB= $3
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 386 | ▶ | $url_style = $2 eq '-' ? 'regional dash-style' : 'regional dot-style'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | elsif ( $host =~ /^$bucket_re\.($region_re)\.s3\.$domain_re/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 192 | $service = 's3'; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 192 | ▶ | $region ||= $1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$region ||= $1
MC/DC0%0/2
$region ||= $1
$region
$1
Truth table66%2/3
A ||= B
A= $regionB= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 208 | $service = 's3'; | |||||||||||||||||||||||||||||||||||||||||
| 334 | 208 | ▶ | $region ||= 'us-east-1'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$region ||= "us-east-1"
MC/DC100%1/1
$region ||= "us-east-1"
$region
Truth table100%2/2
A
A= $region
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 208 | $url_style = 'legacy with virtual-hosted-style'; | |||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | elsif ( $host =~ /^$bucket_re\.s3[\.-]($region_re)\.$domain_re/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 6 | $service = 's3'; | |||||||||||||||||||||||||||||||||||||||||
| 339 | 6 | ▶ | $region ||= $1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$region ||= $1
MC/DC0%0/2
$region ||= $1
$region
$1
Truth table66%2/3
A ||= B
A= $regionB= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 6 | $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
MC/DC0%0/2
$region ||= $2
$region
$2
Truth table66%2/3
A ||= B
A= $regionB= $2
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 1295 | return ( $service, $region, $url_style ); | |||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 114 | ▶ | sub _parse_scope { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_scope: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 114 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 114 | my $scope = shift; | |||||||||||||||||||||||||||||||||||||||||
| 363 | 114 | return split '/', $scope; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | 244 | ▶ | sub _datetime { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_datetime: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 244 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 244 | my $request = shift; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 244 | ▶ | return $request->header('x-amz-date') || $self->_zulu_time($request); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$request->header('x-amz-date') || $self->_zulu_time($request)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | 122 | ▶ | sub _algorithm { return 'AWS4-HMAC-SHA256' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_algorithm: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | 114 | ▶ | sub _sign { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sign: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 114 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 376 | 114 | my ( $request, $region, $payload_sha256_hex ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 377 | 114 | ▶ | return if $request->header('Authorization'); # don't overwrite | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | 114 | my $datetime = $self->_datetime($request); | |||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 114 | ▶ | unless ( $request->header('host') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 110 | my $host = $request->uri->host; | |||||||||||||||||||||||||||||||||||||||||
| 383 | 110 | $request->header( host => $host ); | |||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | 114 | my $scope = $self->_scope( $request, $region ); | |||||||||||||||||||||||||||||||||||||||||
| 387 | 114 | my ( $date, $service ); | |||||||||||||||||||||||||||||||||||||||||
| 388 | 114 | ( $date, $region, $service ) = $self->_parse_scope($scope); | |||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | 114 | my $secret_key = $self->secret_key; | |||||||||||||||||||||||||||||||||||||||||
| 391 | 114 | my $access_key = $self->access_key; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 114 | my $algorithm = $self->_algorithm; | |||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | 114 | my ( $hashed_request, $signed_headers ) = | |||||||||||||||||||||||||||||||||||||||||
| 395 | $self->_hash_canonical_request( $request, $payload_sha256_hex ); | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 114 | my $string_to_sign = | |||||||||||||||||||||||||||||||||||||||||
| 397 | $self->_string_to_sign( $datetime, $scope, $hashed_request ); | ||||||||||||||||||||||||||||||||||||||||||
| 398 | 114 | my $signature = | |||||||||||||||||||||||||||||||||||||||||
| 399 | $self->_calculate_signature( $secret_key, $service, $region, $date, | ||||||||||||||||||||||||||||||||||||||||||
| 400 | $string_to_sign ); | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 114 | $request->header( Authorization => | |||||||||||||||||||||||||||||||||||||||||
| 402 | "$algorithm Credential=$access_key/$scope, SignedHeaders=$signed_headers, Signature=$signature" | ||||||||||||||||||||||||||||||||||||||||||
| 403 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | 138 | ▶ | sub _zulu_time { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_zulu_time: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 407 | 138 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 408 | 138 | my $request = shift; | |||||||||||||||||||||||||||||||||||||||||
| 409 | 138 | my $date = $request->header('Date'); | |||||||||||||||||||||||||||||||||||||||||
| 410 | 138 | ▶ | my @datetime = $date ? gmtime( str2time($date) ) : gmtime(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 138 | return strftime( '%Y%m%dT%H%M%SZ', @datetime ); | |||||||||||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | 114 | ▶ | sub _hash_canonical_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hash_canonical_request: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 114 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 416 | 114 | my ( $request, $hashed_payload ) = | |||||||||||||||||||||||||||||||||||||||||
| 417 | @_; # (HTTP::Request,sha256_hex($content)) | ||||||||||||||||||||||||||||||||||||||||||
| 418 | 114 | my $method = $request->method; | |||||||||||||||||||||||||||||||||||||||||
| 419 | 114 | my $uri = $request->uri; | |||||||||||||||||||||||||||||||||||||||||
| 420 | 114 | ▶ | my $path = $uri->path || '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$uri->path || '/'
MC/DC100%1/1
$uri->path || '/'
$uri->path
Truth table100%2/2
A
A= $uri->path
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 114 | my @params = $uri->query_form; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 114 | my $headers = $request->headers; | |||||||||||||||||||||||||||||||||||||||||
| 423 | 114 | ▶ | $hashed_payload ||= sha256_hex( $request->content ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$hashed_payload ||= &sha256_hex($request->content)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 114 | ▶ | if (scalar(@params) == 0 && defined($uri->query) && $uri->query ne '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
scalar @params == 0 and defined $uri->query and $uri->query ne ""
scalar @params == 0 and defined $uri->query
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | 19 | push @params, ($uri->query, ''); | |||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | 114 | my %canonical; | |||||||||||||||||||||||||||||||||||||||||
| 434 | 114 | ▶ | while ( my ( $key, $value ) = splice( @params, 0, 2 ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | 79 | $key = uri_escape($key); | |||||||||||||||||||||||||||||||||||||||||
| 436 | 79 | $value = uri_escape($value); | |||||||||||||||||||||||||||||||||||||||||
| 437 | 79 | push @{ $canonical{$key} }, $value; | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | my $canonical_query_string = join '&', map { | ||||||||||||||||||||||||||||||||||||||||||
| 440 | 114 | my $key = $_; | |||||||||||||||||||||||||||||||||||||||||
| 441 | 79 | map { "$key=$_" } sort @{ $canonical{$key} } | |||||||||||||||||||||||||||||||||||||||||
| 442 | } sort keys %canonical; | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | # canonicalize the request headers | ||||||||||||||||||||||||||||||||||||||||||
| 445 | 114 | my ( @canonical, %signed_fields ); | |||||||||||||||||||||||||||||||||||||||||
| 446 | 114 | for my $header ( sort map { lc } $headers->header_field_names ) { | |||||||||||||||||||||||||||||||||||||||||
| 447 | 578 | ▶ | next if $header =~ /^date$/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | 468 | my @values = $headers->header($header); | |||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | # remove redundant whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 451 | 468 | foreach (@values) { | |||||||||||||||||||||||||||||||||||||||||
| 452 | 468 | ▶ | next if /^".+"$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | 468 | s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 454 | 468 | s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||
| 455 | 468 | s/(\s)\s+/$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||
| 457 | 468 | push @canonical, "$header:" . join( ',', @values ); | |||||||||||||||||||||||||||||||||||||||||
| 458 | 468 | $signed_fields{$header}++; | |||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | 114 | my $canonical_headers = join "\n", @canonical; | |||||||||||||||||||||||||||||||||||||||||
| 461 | 114 | $canonical_headers .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 462 | 114 | my $signed_headers = join ';', sort map { lc } keys %signed_fields; | |||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 114 | my $canonical_request = join( "\n", | |||||||||||||||||||||||||||||||||||||||||
| 465 | $method, $path, $canonical_query_string, | ||||||||||||||||||||||||||||||||||||||||||
| 466 | $canonical_headers, $signed_headers, $hashed_payload ); | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 114 | my $request_digest = sha256_hex($canonical_request); | |||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | 114 | return ( $request_digest, $signed_headers ); | |||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 114 | ▶ | sub _string_to_sign { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_string_to_sign: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 114 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 474 | 114 | my ( $datetime, $credential_scope, $hashed_request ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 475 | 114 | 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 | 114 | ▶ | sub signing_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
signing_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | 114 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 487 | 114 | my ( $kSecret, $service, $region, $date ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 488 | 114 | my $kDate = hmac_sha256( $date, 'AWS4' . $kSecret ); | |||||||||||||||||||||||||||||||||||||||||
| 489 | 114 | my $kRegion = hmac_sha256( $region, $kDate ); | |||||||||||||||||||||||||||||||||||||||||
| 490 | 114 | my $kService = hmac_sha256( $service, $kRegion ); | |||||||||||||||||||||||||||||||||||||||||
| 491 | 114 | my $kSigning = hmac_sha256( 'aws4_request', $kService ); | |||||||||||||||||||||||||||||||||||||||||
| 492 | 114 | return $kSigning; | |||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | 114 | ▶ | sub _calculate_signature { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_calculate_signature: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | 114 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 497 | 114 | my ( $kSecret, $service, $region, $date, $string_to_sign ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 498 | 114 | my $kSigning = $self->signing_key( $kSecret, $service, $region, $date ); | |||||||||||||||||||||||||||||||||||||||||
| 499 | 114 | return hmac_sha256_hex( $string_to_sign, $kSigning ); | |||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | 12 | 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 | |||||||||||||||||||||||||||||||||||||||||||