| 1 | |||||||||||||||||||||
| 2 | package AWS::S3::Signer; | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 6 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 6 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 6 | ▶ | use HTTP::Date 'time2str'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 6 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 6 | ▶ | use Digest::HMAC_SHA1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 6 | ▶ | use Digest::MD5 'md5'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 6 | ▶ | use Moose::Util::TypeConstraints qw(enum); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 6 | ▶ | use MooseX::Types::URI qw(Uri); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 6 | has 's3' => ( | |||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||
| 16 | isa => 'AWS::S3', | ||||||||||||||||||||
| 17 | required => 1, | ||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 6 | has 'method' => ( | |||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||
| 22 | isa => enum([qw/ HEAD GET PUT POST DELETE /]), | ||||||||||||||||||||
| 23 | required => 1, | ||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | has 'bucket_name' => ( | ||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||
| 28 | isa => 'Str', | ||||||||||||||||||||
| 29 | required => 1, | ||||||||||||||||||||
| 30 | lazy => 1, | ||||||||||||||||||||
| 31 | 30 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 32 | 30 | my $s = shift; | |||||||||||||||||||
| 33 | 30 | my $endpoint = $s->s3->endpoint; | |||||||||||||||||||
| 34 | 30 | ▶ | if ( my ( $name ) = $s->uri->host =~ m{^(.+?)\.\Q$endpoint\E} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 19 | return $name; | |||||||||||||||||||
| 36 | } else { | ||||||||||||||||||||
| 37 | 11 | return ''; | |||||||||||||||||||
| 38 | } # end if() | ||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | 6 | ); | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 6 | has 'uri' => ( | |||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||
| 44 | isa => Uri, | ||||||||||||||||||||
| 45 | required => 1, | ||||||||||||||||||||
| 46 | coerce => 1, | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | has 'headers' => ( | ||||||||||||||||||||
| 50 | is => 'ro', | ||||||||||||||||||||
| 51 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||
| 52 | lazy => 1, | ||||||||||||||||||||
| 53 | 26 | ▶ | default => sub { [] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | 6 | ); | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | has 'date' => ( | ||||||||||||||||||||
| 57 | is => 'ro', | ||||||||||||||||||||
| 58 | isa => 'Str', | ||||||||||||||||||||
| 59 | 30 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 60 | 30 | time2str( time ); | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | 6 | ); | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | has 'string_to_sign' => ( | ||||||||||||||||||||
| 65 | is => 'ro', | ||||||||||||||||||||
| 66 | isa => 'Str', | ||||||||||||||||||||
| 67 | lazy => 1, | ||||||||||||||||||||
| 68 | 30 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 69 | 30 | my $s = shift; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | join "\n", | ||||||||||||||||||||
| 72 | ( | ||||||||||||||||||||
| 73 | $s->method, $s->content_md5, | ||||||||||||||||||||
| 74 | $s->content ? $s->content_type : '', | ||||||||||||||||||||
| 75 | $s->date || '', | ||||||||||||||||||||
| 76 | 30 | ▶ | ( join "\n", grep { $_ } ( $s->canonicalized_amz_headers, $s->canonicalized_resource ) ) | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$s->date || ''
MC/DC0%0/1
$s->date || ''
$s->date
Truth table50%1/2
A
A= $s->date
| |||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | 6 | ); | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | has 'canonicalized_amz_headers' => ( | ||||||||||||||||||||
| 82 | is => 'ro', | ||||||||||||||||||||
| 83 | isa => 'Str', | ||||||||||||||||||||
| 84 | lazy => 1, | ||||||||||||||||||||
| 85 | 30 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=20.1
| |||||||||||||||||||||
| 86 | 30 | my $s = shift; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 30 | my @h = @{ $s->headers }; | |||||||||||||||||||
| 89 | 30 | my %out = (); | |||||||||||||||||||
| 90 | 30 | ▶ | while ( my ( $k, $v ) = splice( @h, 0, 2 ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 2 | $k = lc( $k ); | |||||||||||||||||||
| 92 | 2 | ▶ | if ( exists $out{$k} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | ▶ | $out{$k} = [ $out{$k} ] unless ref( $out{$k} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | push @{ $out{$k} }, $v; | |||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||
| 96 | 2 | $out{$k} = $v; | |||||||||||||||||||
| 97 | } # end if() | ||||||||||||||||||||
| 98 | } # end while() | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 30 | my @parts = (); | |||||||||||||||||||
| 101 | 30 | ▶ | while ( my ( $k, $v ) = each %out ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 2 | ▶ | if ( ref( $out{$k} ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | push @parts, _trim( $k ) . ':' . join( ',', map { _trim( $_ ) } @{ $out{$k} } ); | |||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||
| 105 | 2 | push @parts, _trim( $k ) . ':' . _trim( $out{$k} ); | |||||||||||||||||||
| 106 | } # end if() | ||||||||||||||||||||
| 107 | } # end while() | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 30 | return join "\n", @parts; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | 6 | ); | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | has 'canonicalized_resource' => ( | ||||||||||||||||||||
| 114 | is => 'ro', | ||||||||||||||||||||
| 115 | isa => 'Str', | ||||||||||||||||||||
| 116 | lazy => 1, | ||||||||||||||||||||
| 117 | 30 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 118 | 30 | my $s = shift; | |||||||||||||||||||
| 119 | 30 | ▶ | my $str = $s->bucket_name ? '/' . $s->bucket_name . $s->uri->path : $s->uri->path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 30 | ▶ | if ( my ( $resource ) = | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$s->uri->query || ''
MC/DC100%1/1
$s->uri->query || ''
$s->uri->query
Truth table100%2/2
A
A= $s->uri->query
| |||||||||||||||||||||
| 122 | ( $s->uri->query || '' ) =~ m{[&]*(acl|website|location|policy|delete|lifecycle)(?!\=)} ) | ||||||||||||||||||||
| 123 | { | ||||||||||||||||||||
| 124 | 1 | $str .= '?' . $resource; | |||||||||||||||||||
| 125 | } # end if() | ||||||||||||||||||||
| 126 | 30 | return $str; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | 6 | ); | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | has 'content_type' => ( | ||||||||||||||||||||
| 131 | is => 'ro', | ||||||||||||||||||||
| 132 | isa => 'Str', | ||||||||||||||||||||
| 133 | lazy => 1, | ||||||||||||||||||||
| 134 | 2 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||
| 135 | 2 | my $s = shift; | |||||||||||||||||||
| 136 | 2 | ▶ | return '' if $s->method eq 'GET'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | 2 | ▶ | return '' unless $s->content; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 2 | return 'text/plain'; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | 6 | ); | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | has 'content_md5' => ( | ||||||||||||||||||||
| 143 | is => 'ro', | ||||||||||||||||||||
| 144 | isa => 'Str', | ||||||||||||||||||||
| 145 | lazy => 1, | ||||||||||||||||||||
| 146 | 27 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 147 | 27 | my $s = shift; | |||||||||||||||||||
| 148 | 27 | ▶ | return '' unless $s->content; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | 5 | return encode_base64( md5( ${ $s->content } ), '' ); | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | 6 | ); | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 6 | has 'content' => ( | |||||||||||||||||||
| 154 | is => 'ro', | ||||||||||||||||||||
| 155 | isa => 'Maybe[ScalarRef]', | ||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | has 'content_length' => ( | ||||||||||||||||||||
| 159 | is => 'ro', | ||||||||||||||||||||
| 160 | isa => 'Int', | ||||||||||||||||||||
| 161 | lazy => 1, | ||||||||||||||||||||
| 162 | 2 | ▶ | default => sub { length( ${ shift->content } ) } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 163 | 6 | ); | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | has 'signature' => ( | ||||||||||||||||||||
| 166 | is => 'ro', | ||||||||||||||||||||
| 167 | isa => 'Str', | ||||||||||||||||||||
| 168 | lazy => 1, | ||||||||||||||||||||
| 169 | 30 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 170 | 30 | my $s = shift; | |||||||||||||||||||
| 171 | 30 | my $hmac = Digest::HMAC_SHA1->new( $s->s3->secret_access_key ); | |||||||||||||||||||
| 172 | 30 | $hmac->add( $s->string_to_sign() ); | |||||||||||||||||||
| 173 | 30 | return encode_base64( $hmac->digest, '' ); | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | 6 | ); | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 30 | ▶ | sub auth_header { | ||||||||||||||||||
|
Subroutine
auth_header: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 178 | 30 | my $s = shift; | |||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 30 | return 'AWS ' . $s->s3->access_key_id . ':' . $s->signature; | |||||||||||||||||||
| 181 | } # end auth_header() | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 4 | ▶ | sub _trim { | ||||||||||||||||||
|
Subroutine
_trim: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 184 | 4 | my ( $value ) = @_; | |||||||||||||||||||
| 185 | 4 | $value =~ s/^\s+//; | |||||||||||||||||||
| 186 | 4 | $value =~ s/\s+$//; | |||||||||||||||||||
| 187 | 4 | return $value; | |||||||||||||||||||
| 188 | } # end _trim() | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 6 | 1; | |||||||||||||||||||