API/Signature4.pm

filter: Statementstmt 97.8% 45 / 46 Branchbran 64.2% 9 / 14 Conditioncond 53.8% 21 / 39 MC/DCmcdc 8.7% 2 / 23 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 65.9% 87 / 132 CC 24 SCAR 22.1
CC
24
Cov
76%
parse_service_url
12.5
new
11.8
sign
5.4
CRAP 9.1
« API/Utils.pm ↑ Summary API/EndpointContext/Compiler.pm »
1 package Amazon::API::Signature4;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use parent qw( Exporter Amazon::Signature4::Lite );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 our $VERSION = '2.8.2'; ## no critic (RequireInterpolationOfMetachars)
9  
10 13 our @EXPORT_OK = qw( parse_service_url );
11  
12 ########################################################################
13 5 ▶sub new {
Subroutine new: called CC=8 SCAR=24.7
Pod documented
14 ########################################################################
15 5 my ( $class, @options ) = @_;
16  
17 5 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
18  
19 0 ▶ my %args = ref $options[0] ? %{ $options[0] } : @options;
Branch: 1 branch
BranchTrueFalseTotal
ref $options[0] ? : 0 5 5
20  
21 # Amazon::API.pm constructs us with AWS::Signature4's dash-prefix
22 # convention (-access_key, -secret_key, -security_token). Translate
23 # to Amazon::Signature4::Lite's plain-key convention.
24 return $class->SUPER::new(
25 access_key => $args{-access_key} // $args{access_key},
26 secret_key => $args{-secret_key} // $args{secret_key},
27 session_token => $args{-security_token} // $args{session_token},
28 region => $args{region} // 'us-east-1',
29 service => $args{service} // 'execute-api',
30 disable_double_encoding => $args{disable_double_encoding},
31 5 ▶ );
Condition61%8/13
$args{"-access_key"} // $args{"access_key"}
l!l&&r!l&&!r
320
$args{"-secret_key"} // $args{"secret_key"}
l!l&&r!l&&!r
320
$args{"-security_token"} // $args{"session_token"}
l!l&&r!l&&!r
203
$args{"region"} // "us-east-1"
l!l
50
$args{"service"} // "execute-api"
l!l
50
MC/DC0%0/2
$args{"-access_key"} // $args{"access_key"}
$args{"-access_key"} $args{"access_key"}
MC/DC0%0/2
$args{"-secret_key"} // $args{"secret_key"}
$args{"-secret_key"} $args{"secret_key"}
MC/DC50%1/2
$args{"-security_token"} // $args{"session_token"}
$args{"-security_token"} $args{"session_token"}
MC/DC0%0/1
$args{"region"} // "us-east-1"
$args{"region"}
MC/DC0%0/1
$args{"service"} // "execute-api"
$args{"service"}
Truth table66%2/3
A // B
A= $args{"-access_key"}B= $args{"access_key"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"-secret_key"}B= $args{"secret_key"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"-security_token"}B= $args{"session_token"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{"region"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"service"}
Aresult
11
00
32 }
33  
34 ########################################################################
35 # parse_service_url must be an explicit sub (not just inherited) so it
36 # can be exported via @EXPORT_OK and called as a plain function:
37 # use Amazon::API::Signature4 qw(parse_service_url);
38 # my ($h,$s,$r) = parse_service_url(host => ..., default_region => ...);
39 #
40 # Carries a full S3 endpoint regex set covering FIPS, dual-stack, S3
41 # control plane, and account-id-prefixed endpoints that Amazon::API
42 # encounters - independent of Amazon::Signature4::Lite's patterns.
43 ########################################################################
44  
45 13 our @SERVICE_URL_REGEXP = (
46 # account-id.s3-control[-fips][.dualstack].region.amazonaws.com
47 qr/^[^.]+[.](s3)-control(?:-fips)?(?:[.]dualstack)?[.]([^.]+)[.]amazonaws[.]com\z/xsm,
48  
49 # s3[-fips][.dualstack].region.amazonaws.com
50 qr/(s3)(?:-fips)?(?:[.]dualstack)?[.]([^.]+)[.]amazonaws[.]com\z/xsm,
51  
52 # standard: service.region.amazonaws.com
53 qr/^([[:alnum:]-]+)[.]([^.]+)[.]amazonaws[.]com\z/xsm,
54  
55 # global: service.amazonaws.com (no region)
56 qr/^([[:alnum:]-]+)[.]amazonaws[.]com\z/xsm,
57 );
58  
59 ########################################################################
60 15 ▶sub parse_service_url {
Subroutine parse_service_url: called CC=11 SCAR=25.2
Pod documented
61 ########################################################################
62 15 my $first = shift;
63  
64 # handles three calling conventions:
65 # plain function: parse_service_url(host => ...)
66 # class method: Amazon::API::Signature4->parse_service_url(host => ...)
67 # instance method: $signer->parse_service_url(host => ...)
68 15 my %args;
69 15 ▶ if ( ref $first || ( defined $first && $first =~ /^Amazon::/xsm ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first or defined $first and $first =~ /^Amazon::/msx) { } 1 14 15
Condition66%4/6
ref $first or defined $first and $first =~ /^Amazon::/msx
l!l&&r!l&&!r
0114
defined $first and $first =~ /^Amazon::/msx
!ll&&!rl&&r
0141
MC/DC33%1/3
ref $first or defined $first and $first =~ /^Amazon::/msx
ref $first defined $first $first =~ /^Amazon::/msx
Truth table50%2/4
A or B and C
A= ref $firstB= defined $firstC= $first =~ /^Amazon::/msx
ABCresult
1XX1
0111
00X0
0100
70 1 %args = @_;
71 }
72 else {
73 14 %args = ( $first, @_ );
74 }
75  
76 15 my ( $host, $service ) = @args{qw(host service)};
77 15 my ( $region, $default_region ) = @args{qw(region default_region)};
78  
79 15 ▶ if ( !$service || !$region ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service and $region) 15 0 15
Condition33%1/3
$service and $region
!ll&&!rl&&r
1500
MC/DC0%0/2
$service and $region
$service $region
Truth table33%1/3
A and B
A= $serviceB= $region
ABresult
0X0
100
111
80 15 foreach my $regexp (@SERVICE_URL_REGEXP) {
81 33 ▶ if ( $host =~ $regexp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /$regexp/) 15 18 33
82 15 $service = $1;
83 15 ▶ $region = $2 || $region || $default_region;
Condition66%4/6
$2 || $region || $default_region
l!l&&r!l&&!r
1320
$2 or $region
l!l&&r!l&&!r
1302
MC/DC0%0/3
$2 || $region || $default_region
$2 $region $default_region
Truth table50%2/4
A or B || C
A= $2B= $regionC= $default_region
ABCresult
1XX1
01X1
0011
0000
84 15 last;
85 }
86 }
87 }
88  
89 15 ▶ $region ||= $default_region;
Condition33%1/3
$region ||= $default_region
l!l&&r!l&&!r
1500
MC/DC0%0/2
$region ||= $default_region
$region $default_region
Truth table33%1/3
A ||= B
A= $regionB= $default_region
ABresult
1X1
011
000
90  
91 15 return ( $host, $service, $region );
92 }
93  
94 ########################################################################
95 3 ▶sub sign {
Subroutine sign: called CC=5 SCAR=16.8
Pod documented
96 ########################################################################
97 3 my ( $self, $request, $region ) = @_;
98  
99 # Amazon::API.pm calls ->sign($http_request_object, $region), matching
100 # the AWS::Signature4 interface. Extract into plain scalars for
101 # Amazon::Signature4::Lite->sign, which works with HTTP::Tiny-style
102 # hashrefs rather than HTTP::Request objects.
103  
104 3 my $method = $request->method;
105 3 my $url = $request->uri->as_string;
106 3 ▶ my $payload = $request->content // q{};
Condition50%1/2
$request->content // ""
l!l
30
MC/DC0%0/1
$request->content // ""
$request->content
Truth table50%1/2
A
A= $request->content
Aresult
11
00
107  
108 # collect existing headers - Lite will add/overwrite signing headers
109 3 my %headers;
110  
111 3 ▶ $request->headers->scan( sub { $headers{ lc $_[0] } = $_[1] if defined $_[0] && defined $_[1] } );
Subroutine __ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and defined $_[1] 4 0 4
Condition33%1/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
004
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
112  
113 # region may be passed per-call (mirrors AWS::Signature4->sign API)
114 3 ▶ local $self->{region} = $region
Branch: 1 branch
BranchTrueFalseTotal
if $region 3 0 3
115 if $region;
116  
117 my $signed = $self->SUPER::sign(
118 method => $method,
119 url => $url,
120 headers => \%headers,
121 payload => \$payload,
122 3 ▶ add_sha256_header => $self->{service} eq 's3' ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$self->{'service'} eq 's3' ? : 0 3 3
123 );
124  
125 # apply signed headers back onto the HTTP::Request object
126 3 for my $h ( keys %{$signed} ) {
127 14 $request->header( $h => $signed->{$h} );
128 }
129  
130 3 return $request;
131 }
132  
133 13 1;
134  
135 __END__
« API/Utils.pm ↑ Summary API/EndpointContext/Compiler.pm »