Admin/HTTPRequest.pm

filter: Statementstmt 74.2% 104 / 140 Branchbran 34.0% 15 / 44 Conditioncond 50.0% 8 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 85.7% 18 / 21 Podpod 100% 2 / 2 totaltotal 63.6% 147 / 231 CC 31 SCAR 30.6
CC
31
Cov
64%
_is_dns_bucket
72.0
_canonical_string
29.0
_merge_meta
6.1
CRAP 21.2
↑ Summary Admin.pm »
1 package Ceph::RadosGW::Admin::HTTPRequest;
2 2 $Ceph::RadosGW::Admin::HTTPRequest::VERSION = '0.4';
3 2 ▶use Moose 0.85;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use MooseX::StrictConstructor 0.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI::Escape qw( uri_escape_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Digest::HMAC_SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # this is almost a direct copy of
15 # https://metacpan.org/pod/Net::Amazon::S3::HTTPRequest
16  
17 # ABSTRACT: Create a signed HTTP::Request
18  
19 2 my $METADATA_PREFIX = 'x-amz-meta-';
20 2 my $AMAZON_HEADER_PREFIX = 'x-amz-';
21  
22 2 enum 'HTTPMethod' => [ qw(DELETE GET HEAD PUT POST) ];
23  
24 2 has 'url' => ( is => 'ro', isa => 'Str', required => 1 );
25 2 has 'method' => ( is => 'ro', isa => 'HTTPMethod', required => 1 );
26 2 has 'path' => ( is => 'ro', isa => 'Str', required => 1 );
27 2 has 'access_key' => ( is => 'ro', isa => 'Str', required => 1 );
28 2 has 'secret_key' => ( is => 'ro', isa => 'Str', required => 1 );
29  
30 has 'headers' =>
31 52 ▶ ( is => 'ro', isa => 'HashRef', required => 0, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
32 2 has 'content' =>
33 ( is => 'ro', isa => 'Str|CodeRef', required => 0, default => '' );
34 has 'metadata' =>
35 52 ▶ ( is => 'ro', isa => 'HashRef', required => 0, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
36  
37 2 __PACKAGE__->meta->make_immutable;
38  
39 # make the HTTP::Request object
40 52 ▶sub http_request {
Subroutine http_request: called CC=2 SCAR=6.9
Pod documented
41 52 my $self = shift;
42 52 my $method = $self->method;
43 52 my $path = $self->path;
44 52 my $headers = $self->headers;
45 52 my $content = $self->content;
46 52 my $metadata = $self->metadata;
47 52 my $uri = $self->url . $path;
48
49 52 my $http_headers = $self->_merge_meta( $headers, $metadata );
50  
51 $self->_add_auth_header( $http_headers, $method, $path )
52 52 ▶ unless exists $headers->{Authorization};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $headers->{"Authorization"} 52 0 52
53
54  
55
56 52 my $request
57 = HTTP::Request->new( $method, $uri, $http_headers, $content );
58  
59 #my $req_as = $request->as_string;
60 #$req_as =~ s/[^\n\r\x20-\x7f]/?/g;
61 #$req_as = substr( $req_as, 0, 1024 ) . "\n\n";
62 #warn $req_as;
63  
64 52 return $request;
65 }
66  
67 0 ▶sub query_string_authentication_uri {
Subroutine query_string_authentication_uri: not called CC=1 SCAR=6.9
Pod documented
68 0 my ( $self, $expires ) = @_;
69 0 my $method = $self->method;
70 0 my $path = $self->path;
71 0 my $headers = $self->headers;
72  
73 0 my $aws_access_key_id = $self->access_key;
74 0 my $aws_secret_access_key = $self->secret_key;
75 0 my $canonical_string
76 = $self->_canonical_string( $method, $path, $headers, $expires );
77 0 my $encoded_canonical
78 = $self->_encode( $aws_secret_access_key, $canonical_string );
79  
80 0 my $uri = $self->url . $path;
81 0 $uri = URI->new($uri);
82  
83 0 $uri->query_param( AWSAccessKeyId => $aws_access_key_id );
84 0 $uri->query_param( Expires => $expires );
85 0 $uri->query_param( Signature => $encoded_canonical );
86  
87 0 return $uri;
88 }
89  
90  
91 52 ▶sub _add_auth_header {
Subroutine _add_auth_header: called CC=4 SCAR=14.0
92 52 my ( $self, $headers, $method, $path ) = @_;
93 52 my $aws_access_key_id = $self->access_key;
94 52 my $aws_secret_access_key = $self->secret_key;
95  
96 52 ▶ if ( not $headers->header('Date') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->header("Date")) 52 0 52
97 52 $headers->header( Date => time2str(time) );
98 }
99
100 52 ▶ if ( not $headers->header('Content-Type') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->header("Content-Type")) 52 0 52
101 52 $headers->header( 'Content-Type' => 'text/plain' );
102 }
103
104 52 ▶ if ( not $headers->header('Content-MD5') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->header("Content-MD5")) 52 0 52
105 52 $headers->header( 'Content-MD5' => Digest::MD5::md5_base64($self->content));
106 }
107  
108 52 my $canonical_string
109 = $self->_canonical_string( $method, $path, $headers );
110 52 my $encoded_canonical
111 = $self->_encode( $aws_secret_access_key, $canonical_string );
112 52 $headers->header(
113 Authorization => "AWS $aws_access_key_id:$encoded_canonical" );
114 }
115  
116 # generate a canonical string for the given parameters. expires is optional and is
117 # only used by query string authentication.
118 52 ▶sub _canonical_string {
Subroutine _canonical_string: called CC=15 SCAR=33.7
119 52 my ( $self, $method, $path, $headers, $expires ) = @_;
120 52 my %interesting_headers = ();
121 52 ▶ while ( my ( $key, $value ) = each %$headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 156 52 208
122 156 my $lk = lc $key;
123 156 ▶ if ( $lk eq 'content-md5'
Branch: 1 branch
BranchTrueFalseTotal
if ($lk eq "content-md5" or $lk eq "content-type" or $lk eq "date" or $lk =~ /^$AMAZON_HEADER_PREFIX/) 156 0 156
Condition66%6/9
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date" or $lk =~ /^$AMAZON_HEADER_PREFIX/
l!l&&r!l&&!r
15600
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date"
l!l&&r!l&&!r
104520
$lk eq "content-md5" or $lk eq "content-type"
l!l&&r!l&&!r
525252
MC/DC0%0/4
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date" or $lk =~ /^$AMAZON_HEADER_PREFIX/
$lk eq "content-md5" $lk eq "content-type" $lk eq "date" $lk =~ /^$AMAZON_HEADER_PREFIX/
Truth table60%3/5
A or B or C or D
A= $lk eq "content-md5"B= $lk eq "content-type"C= $lk eq "date"D= $lk =~ /^$AMAZON_HEADER_PREFIX/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
124 or $lk eq 'content-type'
125 or $lk eq 'date'
126 or $lk =~ /^$AMAZON_HEADER_PREFIX/ )
127 {
128 156 $interesting_headers{$lk} = $self->_trim($value);
129 }
130 }
131  
132
133  
134 # just in case someone used this. it's not necessary in this lib.
135 $interesting_headers{'date'} = ''
136 52 ▶ if $interesting_headers{'x-amz-date'};
Branch: 1 branch
BranchTrueFalseTotal
if $interesting_headers{"x-amz-date"} 0 52 52
137  
138 # if you're using expires for query string auth, then it trumps date
139 # (and x-amz-date)
140 52 ▶ $interesting_headers{'date'} = $expires if $expires;
Branch: 1 branch
BranchTrueFalseTotal
if $expires 0 52 52
141  
142 52 my $buf = "$method\n";
143 52 foreach my $key ( sort keys %interesting_headers ) {
144 156 ▶ if ( $key =~ /^$AMAZON_HEADER_PREFIX/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^$AMAZON_HEADER_PREFIX/) { } 0 156 156
145 0 $buf .= "$key:$interesting_headers{$key}\n";
146 } else {
147 156 $buf .= "$interesting_headers{$key}\n";
148 }
149 }
150  
151 # don't include anything after the first ? in the resource...
152 52 $path =~ /^([^?]*)/;
153 52 $path = "/$1";
154 52 $path =~ s:/+:/:g;
155 52 $buf .= $path;
156
157
158 # ...unless there any parameters we're interested in...
159 52 ▶ if ( $path =~ /[&?](acl|torrent|location|uploads|delete)($|=|&)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ /[&?](acl|torrent|location|uploads|delete)($|=|&)/) { } 0 52 52
elsif (my(%query_params) = 'URI'->new($path)->query_form) { } 0 52 52
160 0 $buf .= "?$1";
161 } elsif ( my %query_params = URI->new($path)->query_form ){
162 #see if the remaining parsed query string provides us with any query string or upload id
163 0 ▶ if($query_params{partNumber} && $query_params{uploadId}){
Branch: 2 branches
BranchTrueFalseTotal
if ($query_params{'partNumber'} and $query_params{'uploadId'}) { } 0 0 0
elsif ($query_params{'uploadId'}) { } 0 0 0
Condition0%0/3
$query_params{"partNumber"} and $query_params{"uploadId"}
!ll&&!rl&&r
000
MC/DC0%0/2
$query_params{"partNumber"} and $query_params{"uploadId"}
$query_params{"partNumber"} $query_params{"uploadId"}
Truth table0%0/3
A and B
A= $query_params{"partNumber"}B= $query_params{"uploadId"}
ABresult
0X0
100
111
164 #re-evaluate query string, the order of the params is important for request signing, so we can't depend on URI to do the right thing
165 0 $buf .= sprintf("?partNumber=%s&uploadId=%s", $query_params{partNumber}, $query_params{uploadId});
166 }
167 elsif($query_params{uploadId}){
168 0 $buf .= sprintf("?uploadId=%s",$query_params{uploadId});
169 }
170 }
171  
172 #warn "Buf:\n$buf\n";
173
174 52 return $buf;
175 }
176  
177 # finds the hmac-sha1 hash of the canonical string and the aws secret access key and then
178 # base64 encodes the result (optionally urlencoding after that).
179 52 ▶sub _encode {
Subroutine _encode: called CC=2 SCAR=7.1
180 52 my ( $self, $aws_secret_access_key, $str, $urlencode ) = @_;
181 52 my $hmac = Digest::HMAC_SHA1->new($aws_secret_access_key);
182 52 $hmac->add($str);
183 52 my $b64 = encode_base64( $hmac->digest, '' );
184 52 ▶ if ($urlencode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($urlencode) { } 0 52 52
185 0 return $self->_urlencode($b64);
186 } else {
187 52 return $b64;
188 }
189 }
190  
191 # EU buckets must be accessed via their DNS name. This routine figures out if
192 # a given bucket name can be safely used as a DNS name.
193 0 ▶sub _is_dns_bucket {
Subroutine _is_dns_bucket: not called CC=8 SCAR=42.8
194 0 my $bucketname = $_[0];
195  
196 0 ▶ if ( length $bucketname > 63 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bucketname > 63) 0 0 0
197 0 return 0;
198 }
199 0 ▶ if ( length $bucketname < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bucketname < 3) 0 0 0
200 0 return;
201 }
202 0 ▶ return 0 unless $bucketname =~ m{^[a-z0-9][a-z0-9.-]+$};
Branch: 1 branch
BranchTrueFalseTotal
unless $bucketname =~ /^[a-z0-9][a-z0-9.-]+$/ 0 0 0
203 0 my @components = split /\./, $bucketname;
204 0 for my $c (@components) {
205 0 ▶ return 0 if $c =~ m{^-};
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /^-/ 0 0 0
206 0 ▶ return 0 if $c =~ m{-$};
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /-$/ 0 0 0
207 0 ▶ return 0 if $c eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "" 0 0 0
208 }
209 0 return 1;
210 }
211  
212 # generates an HTTP::Headers objects given one hash that represents http
213 # headers to set and another hash that represents an object's metadata.
214 52 ▶sub _merge_meta {
Subroutine _merge_meta: called CC=5 SCAR=18.1
215 52 my ( $self, $headers, $metadata ) = @_;
216 52 ▶ $headers ||= {};
Condition50%1/2
$headers ||= {}
l!l
520
MC/DC0%0/1
$headers ||= {}
$headers
Truth table50%1/2
A
A= $headers
Aresult
11
00
217 52 ▶ $metadata ||= {};
Condition50%1/2
$metadata ||= {}
l!l
520
MC/DC0%0/1
$metadata ||= {}
$metadata
Truth table50%1/2
A
A= $metadata
Aresult
11
00
218  
219 52 my $http_header = HTTP::Headers->new;
220 52 ▶ while ( my ( $k, $v ) = each %$headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 0 52 52
221 0 $http_header->header( $k => $v );
222 }
223 52 ▶ while ( my ( $k, $v ) = each %$metadata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$metadata) 0 52 52
224 0 $http_header->header( "$METADATA_PREFIX$k" => $v );
225 }
226  
227 52 return $http_header;
228 }
229  
230 156 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
231 156 my ( $self, $value ) = @_;
232 156 $value =~ s/^\s+//;
233 156 $value =~ s/\s+$//;
234 156 return $value;
235 }
236  
237 0 ▶sub _urlencode {
Subroutine _urlencode: not called CC=1 SCAR=6.9
238 0 my ( $self, $unencoded ) = @_;
239 0 return uri_escape_utf8( $unencoded, '^A-Za-z0-9_-' );
240 }
241  
242 2 1;
243  
244 __END__
↑ Summary Admin.pm »