| 1 | package Paws::API::EndpointResolver; | ||||||||||||||||||||||||||||
| 2 | 32 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 32 | ▶ | use URI::Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 32 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 32 | ▶ | use Paws::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 32 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 32 | has region => (is => 'rw', isa => 'Str|Undef'); | |||||||||||||||||||||||||||
| 9 | 32 | requires 'service'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 32 | has _endpoint_info => ( | |||||||||||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||||||||||
| 13 | init_arg => undef, | ||||||||||||||||||||||||||||
| 14 | lazy => 1, | ||||||||||||||||||||||||||||
| 15 | builder => '_construct_endpoint', | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | has _region_for_signature => ( | ||||||||||||||||||||||||||||
| 19 | is => 'rw', | ||||||||||||||||||||||||||||
| 20 | isa => 'Str', | ||||||||||||||||||||||||||||
| 21 | lazy => 1, | ||||||||||||||||||||||||||||
| 22 | init_arg => undef, | ||||||||||||||||||||||||||||
| 23 | 586 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 24 | 586 | my $self = shift; | |||||||||||||||||||||||||||
| 25 | 586 | my $sig_region; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # For global services: don't specify region: we sign with the region in the credentialScope | ||||||||||||||||||||||||||||
| 28 | # specify the region: we override the credentialScope (use the region specified) | ||||||||||||||||||||||||||||
| 29 | # For regional services: use the region specified for signing | ||||||||||||||||||||||||||||
| 30 | # If endpoint is specified: use the region specified (no _endpoint_info) | ||||||||||||||||||||||||||||
| 31 | 586 | ▶ | if (defined $self->_endpoint_info->{ credentialScope } and not defined $self->region) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->_endpoint_info->{"credentialScope"} and not defined $self->region
MC/DC100%2/2
defined $self->_endpoint_info->{"credentialScope"} and not defined $self->region
defined $self->_endpoint_info->{"credentialScope"}
not defined $self->region
Truth table100%3/3
A and not(B)
A= defined $self->_endpoint_info->{"credentialScope"}B= not defined $self->region
| |||||||||||||||||||||||||||||
| 32 | $sig_region = $self->_endpoint_info->{ credentialScope }->{ region } | ||||||||||||||||||||||||||||
| 33 | 9 | } | |||||||||||||||||||||||||||
| 34 | 585 | ▶ | $sig_region = $self->region if (not defined $sig_region); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 585 | ▶ | Paws::Exception->throw( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | message => "Can't find a region for signing. region is required", | ||||||||||||||||||||||||||||
| 38 | code => 'NoRegionError', | ||||||||||||||||||||||||||||
| 39 | request_id => '' | ||||||||||||||||||||||||||||
| 40 | ) if (not defined $sig_region); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 585 | return $sig_region; | |||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||
| 44 | 32 | ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 32 | subtype 'Paws::EndpointURL', | |||||||||||||||||||||||||||
| 47 | as 'URI'; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | coerce 'Paws::EndpointURL', | ||||||||||||||||||||||||||||
| 50 | from 'Str', | ||||||||||||||||||||||||||||
| 51 | 4 | ▶ | via { URI->new($_); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | has endpoint => ( | ||||||||||||||||||||||||||||
| 54 | is => 'ro', | ||||||||||||||||||||||||||||
| 55 | isa => 'Paws::EndpointURL', | ||||||||||||||||||||||||||||
| 56 | lazy => 1, | ||||||||||||||||||||||||||||
| 57 | coerce => 1, | ||||||||||||||||||||||||||||
| 58 | 585 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 585 | shift->_endpoint_info->{ url }; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 32 | ); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | has endpoint_host => ( | ||||||||||||||||||||||||||||
| 64 | is => 'ro', | ||||||||||||||||||||||||||||
| 65 | isa => 'Str', | ||||||||||||||||||||||||||||
| 66 | lazy => 1, | ||||||||||||||||||||||||||||
| 67 | 161 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 161 | shift->endpoint->host; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 32 | ); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | has _api_endpoint => ( | ||||||||||||||||||||||||||||
| 73 | is => 'ro', | ||||||||||||||||||||||||||||
| 74 | isa => 'Str', | ||||||||||||||||||||||||||||
| 75 | lazy => 1, | ||||||||||||||||||||||||||||
| 76 | 115 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 115 | shift->endpoint->as_string; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 32 | ); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 32 | has region_rules => (is => 'ro', isa => 'ArrayRef'); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 589 | ▶ | has _default_rules => (is => 'ro', isa => 'ArrayRef', default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 589 | [ { constraints => [ [ 'region', 'startsWith', 'cn-' ] ], | |||||||||||||||||||||||||||
| 85 | properties => { signatureVersion => 'v4' }, | ||||||||||||||||||||||||||||
| 86 | uri => '{scheme}://{service}.{region}.amazonaws.com.cn' | ||||||||||||||||||||||||||||
| 87 | }, | ||||||||||||||||||||||||||||
| 88 | { constraints => [ [ 'region', 'notEquals', undef ] ], | ||||||||||||||||||||||||||||
| 89 | uri => '{scheme}://{service}.{region}.amazonaws.com' | ||||||||||||||||||||||||||||
| 90 | }, | ||||||||||||||||||||||||||||
| 91 | ] | ||||||||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||||||||
| 93 | 32 | ); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 32 | has default_scheme => ( is => 'ro', isa => 'Str', default => 'https' ); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 589 | ▶ | sub _construct_endpoint { | ||||||||||||||||||||||||||
|
Subroutine
_construct_endpoint: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 98 | 589 | my ($self) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 589 | my $args = {}; | |||||||||||||||||||||||||||
| 101 | 589 | $args->{ service } = $self->service; | |||||||||||||||||||||||||||
| 102 | 589 | $args->{ region } = $self->region; | |||||||||||||||||||||||||||
| 103 | 589 | ▶ | $args->{ scheme } = $self->default_scheme if (not defined $args->{scheme}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 589 | my $service_rules = $self->region_rules; | |||||||||||||||||||||||||||
| 106 | 589 | my $endpoint_info = $self->_match_rules($self->region_rules, $args->{ region }, $args); | |||||||||||||||||||||||||||
| 107 | 589 | ▶ | if (not defined $self->region_rules) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 357 | $endpoint_info = $self->_match_rules($self->region_rules, $args->{ region }, $args); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 589 | ▶ | if (not defined $endpoint_info) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 404 | $endpoint_info = $self->_match_rules($self->_default_rules, $args->{ region }, $args); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 589 | ▶ | if ( not defined $endpoint_info ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | ▶ | my $region_for_exception = (defined $args->{ region }) ? $args->{ region } : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 1 | Paws::Exception->throw( | |||||||||||||||||||||||||||
| 117 | message => "No endpoint for service $args->{ service } in region '$region_for_exception'", | ||||||||||||||||||||||||||||
| 118 | code => 'NoRegionError', | ||||||||||||||||||||||||||||
| 119 | request_id => '' | ||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||
| 122 | 588 | my $template = URI::Template->new($endpoint_info->{uri}); | |||||||||||||||||||||||||||
| 123 | 588 | my $url = $template->process($args); | |||||||||||||||||||||||||||
| 124 | 588 | $endpoint_info->{ url } = $url; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 588 | return $endpoint_info; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1350 | ▶ | sub _match_rules { | ||||||||||||||||||||||||||
|
Subroutine
_match_rules: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 131 | 1350 | my ( $self, $service_rules, $region, $args ) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1350 | ▶ | return undef if (not defined $service_rules); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 636 | ▶ | return undef if scalar(@$service_rules) == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 636 | for my $rule ( @$service_rules ) { | |||||||||||||||||||||||||||
| 137 | 1392 | ▶ | if ( $self->_matches_rule($rule, $region, $args) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 588 | ▶ | return { uri => $rule->{ uri }, (defined $rule->{ properties }) ? %{ $rule->{ properties } } : () }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 48 | return undef; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1392 | ▶ | sub _matches_rule { | ||||||||||||||||||||||||||
|
Subroutine
_matches_rule: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 145 | 1392 | my( $self, $rule, $region, $args ) = @_; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1392 | ▶ | return 1 if (not defined $rule->{ constraints }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1345 | my @constraints = @{ $rule->{ constraints } }; | |||||||||||||||||||||||||||
| 150 | 1345 | for my $constraint (@constraints) { | |||||||||||||||||||||||||||
| 151 | 1345 | ▶ | return 1 if ( $self->_matches_constraint($region, $constraint, $args) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 804 | return 0; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | has constraints => ( | ||||||||||||||||||||||||||||
| 157 | is => 'ro', | ||||||||||||||||||||||||||||
| 158 | init_arg => undef, | ||||||||||||||||||||||||||||
| 159 | 589 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||
| 160 | { | ||||||||||||||||||||||||||||
| 161 | 598 | ▶ | startsWith => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.2
| |||||||||||||||||||||||||||||
| 162 | 598 | my ( $a, $v ) = @_; | |||||||||||||||||||||||||||
| 163 | 598 | ▶ | return 0 if (not defined $a and defined $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $a and defined $v
MC/DC50%1/2
not defined $a and defined $v
not defined $a
defined $v
Truth table66%2/3
not(A) and B
A= not defined $aB= defined $v
| |||||||||||||||||||||||||||||
| 164 | 584 | ▶ | return 0 if (defined $a and not defined $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $a and not defined $v
MC/DC0%0/2
defined $a and not defined $v
defined $a
not defined $v
Truth table33%1/3
A and not(B)
A= defined $aB= not defined $v
| |||||||||||||||||||||||||||||
| 165 | 584 | return $a =~ /^$v.*/i; | |||||||||||||||||||||||||||
| 166 | }, | ||||||||||||||||||||||||||||
| 167 | 25 | ▶ | notStartsWith => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 168 | 25 | my ( $a, $v ) = @_; | |||||||||||||||||||||||||||
| 169 | 25 | ▶ | return 1 if (not defined $a); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 22 | return $a !~ /^$v.*/i; | |||||||||||||||||||||||||||
| 171 | }, | ||||||||||||||||||||||||||||
| 172 | 106 | ▶ | equals => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 173 | 106 | my ( $a, $v ) = @_; | |||||||||||||||||||||||||||
| 174 | 106 | ▶ | return 0 if (not defined $a and defined $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $a and defined $v
MC/DC100%2/2
not defined $a and defined $v
not defined $a
defined $v
Truth table100%3/3
not(A) and B
A= not defined $aB= defined $v
| |||||||||||||||||||||||||||||
| 175 | 105 | ▶ | return 0 if (defined $a and not defined $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $a and not defined $v
MC/DC100%2/2
defined $a and not defined $v
defined $a
not defined $v
Truth table100%3/3
A and not(B)
A= defined $aB= not defined $v
| |||||||||||||||||||||||||||||
| 176 | 94 | ▶ | return 1 if (not defined $a and not defined $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $a or defined $v
MC/DC50%1/2
defined $a or defined $v
defined $a
defined $v
Truth table66%2/3
A or B
A= defined $aB= defined $v
| |||||||||||||||||||||||||||||
| 177 | 93 | return $a eq $v; | |||||||||||||||||||||||||||
| 178 | }, | ||||||||||||||||||||||||||||
| 179 | 476 | ▶ | notEquals => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=23.2
| |||||||||||||||||||||||||||||
| 180 | # in the sample json, notEqual region's is always null | ||||||||||||||||||||||||||||
| 181 | # this needs review | ||||||||||||||||||||||||||||
| 182 | 476 | my ( $a, $v ) = @_; | |||||||||||||||||||||||||||
| 183 | 476 | ▶ | return 1 if (defined $a and not defined $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $a and not defined $v
MC/DC50%1/2
defined $a and not defined $v
defined $a
not defined $v
Truth table66%2/3
A and not(B)
A= defined $aB= not defined $v
| |||||||||||||||||||||||||||||
| 184 | 2 | ▶ | return 1 if (not defined $a and defined $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $a and defined $v
MC/DC0%0/2
not defined $a and defined $v
not defined $a
defined $v
Truth table33%1/3
not(A) and B
A= not defined $aB= defined $v
| |||||||||||||||||||||||||||||
| 185 | 2 | ▶ | return 0 if (not defined $a and not defined $v); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $a or defined $v
MC/DC0%0/2
defined $a or defined $v
defined $a
defined $v
Truth table33%1/3
A or B
A= defined $aB= defined $v
| |||||||||||||||||||||||||||||
| 186 | 0 | return $a ne $v; | |||||||||||||||||||||||||||
| 187 | }, | ||||||||||||||||||||||||||||
| 188 | 140 | ▶ | oneOf => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 189 | 140 | my ( $a, $v ) = @_; | |||||||||||||||||||||||||||
| 190 | 140 | for my $b (@$v) { | |||||||||||||||||||||||||||
| 191 | 741 | ▶ | return not defined($a) if (not defined($b)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 671 | ▶ | return not defined($b) if (not defined($a)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 671 | ▶ | return 1 if $a eq $b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 41 | return 0; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 589 | }; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 32 | ); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1345 | ▶ | sub _matches_constraint { | ||||||||||||||||||||||||||
|
Subroutine
_matches_constraint: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 202 | 1345 | my ($self, $region, $constraint, $args ) = @_; | |||||||||||||||||||||||||||
| 203 | 1345 | my $property = $constraint->[0]; | |||||||||||||||||||||||||||
| 204 | 1345 | ▶ | die "We only know how to apply constraints to region" if ($property ne 'region'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 1345 | my $func = $constraint->[1]; | |||||||||||||||||||||||||||
| 206 | 1345 | my $value = $constraint->[2]; | |||||||||||||||||||||||||||
| 207 | 1345 | return $self->constraints->{$func}->($region,$value); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 32 | 1; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||