| 1 | package Net::Amazon::S3::Policy; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use version; our $VERSION = qv('0.1.6'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use MIME::Base64 qw< decode_base64 >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 15 | 2 | our @EXPORT_OK = qw( exact starts_with range ); | |||||||||||||||||||||||||||
| 16 | 2 | our %EXPORT_TAGS = (all => \@EXPORT_OK,); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Module implementation here | ||||||||||||||||||||||||||||
| 19 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 21 | 3 | ▶ | my %args = ref($_[0]) ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 3 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | if ($args{json}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 2 | $self->parse($args{json}); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | else { | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | $self->expiration($args{expiration}) if defined $args{expiration}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 1 | $self->conditions([]); | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | $self->add($_) for @{$args{conditions} || []}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'conditions'} || []
MC/DC0%0/1
$args{'conditions'} || []
$args{'conditions'}
Truth table50%1/2
A
A= $args{'conditions'}
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | return $self; | |||||||||||||||||||||||||||
| 34 | } ## end sub new | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Accessors | ||||||||||||||||||||||||||||
| 37 | 4 | ▶ | sub expiration { | ||||||||||||||||||||||||||
|
Subroutine
expiration: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 4 | my $previous = $self->{expiration}; | |||||||||||||||||||||||||||
| 40 | 4 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | my $time = shift; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | if ($time && $time =~ /\A \d+ \z/mxs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$time and $time =~ /\A \d+ \z/msx
MC/DC0%0/2
$time and $time =~ /\A \d+ \z/msx
$time
$time =~ /\A \d+ \z/msx
Truth table33%1/3
A and B
A= $timeB= $time =~ /\A \d+ \z/msx
| |||||||||||||||||||||||||||||
| 43 | 1 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = | |||||||||||||||||||||||||||
| 44 | gmtime($time); | ||||||||||||||||||||||||||||
| 45 | 1 | $time = sprintf "%04d-%02d-%02dT%02d:%02d:%02d.000Z", | |||||||||||||||||||||||||||
| 46 | $year + 1900, | ||||||||||||||||||||||||||||
| 47 | $mon + 1, $mday, $hour, $min, $sec; | ||||||||||||||||||||||||||||
| 48 | } ## end if ($time && $time =~ ... | ||||||||||||||||||||||||||||
| 49 | 1 | ▶ | $time ? ($self->{expiration} = $time) : delete $self->{expiration}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | } ## end if (@_) | ||||||||||||||||||||||||||||
| 51 | 4 | return $previous; | |||||||||||||||||||||||||||
| 52 | } ## end sub expiration | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 10 | ▶ | sub conditions { | ||||||||||||||||||||||||||
|
Subroutine
conditions: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | 10 | my $previous = $self->{conditions}; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 10 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | $self->{conditions} = (scalar(@_) == 1) ? shift : [@_]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 10 | return $previous; | |||||||||||||||||||||||||||
| 63 | } ## end sub conditions | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | { # try to understand rules | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 5 | ▶ | sub _prepend_dollar { | ||||||||||||||||||||||||||
|
Subroutine
_prepend_dollar: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 68 | 5 | ▶ | return substr($_[0], 0, 1) eq '$' ? $_[0] : '$' . $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | my @DWIMs = ( | ||||||||||||||||||||||||||||
| 71 | 1 | ▶ | qr{\A\s* (\S+?) \s* \* \s*\z}mxs => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 1 | my $target = _prepend_dollar(shift); | |||||||||||||||||||||||||||
| 73 | 1 | return starts_with($target, ''); | |||||||||||||||||||||||||||
| 74 | }, | ||||||||||||||||||||||||||||
| 75 | 1 | ▶ | qr{\A\s* (\S+) \s+ eq \s+ (.*?) \s*\z}mxs => sub{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 76 | 1 | my $target = _prepend_dollar(shift); | |||||||||||||||||||||||||||
| 77 | 1 | my $value = shift; | |||||||||||||||||||||||||||
| 78 | 1 | ▶ | return $value eq '*' ? starts_with($target, '') : exact($target, $value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||
| 80 | 3 | ▶ | qr{\A\s* (\S+) \s+ (?: ^ | starts[_-]?with) \s+ (.*?) \s*\z}mxs => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 3 | my $target = _prepend_dollar(shift); | |||||||||||||||||||||||||||
| 82 | 3 | my $prefix = shift; | |||||||||||||||||||||||||||
| 83 | 3 | return starts_with($target, $prefix); | |||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||
| 85 | 1 | ▶ | qr{\A\s* (\d+) \s*<=\s* (\S+) \s*<=\s* (\d+) \s*\z}mxs => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 86 | 1 | my ($min, $value, $max) = @_; | |||||||||||||||||||||||||||
| 87 | 1 | s{_}{}g for $min, $max; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # no "_prepend_dollar" for range conditions | ||||||||||||||||||||||||||||
| 90 | 1 | return range($value, $min, $max); | |||||||||||||||||||||||||||
| 91 | }, | ||||||||||||||||||||||||||||
| 92 | 2 | ); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 6 | ▶ | sub _resolve_rule { | ||||||||||||||||||||||||||
|
Subroutine
_resolve_rule: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 95 | 6 | my ($string) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 6 | for my $i (0 .. (@DWIMs - 1) / 2) { | |||||||||||||||||||||||||||
| 98 | 16 | my ($regex, $callback) = @DWIMs[$i * 2, $i * 2 + 1]; | |||||||||||||||||||||||||||
| 99 | 16 | ▶ | if (my @captures = $string =~ /$regex/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 6 | my $result = $callback->(@captures); | |||||||||||||||||||||||||||
| 101 | 6 | ▶ | return $result if defined $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | croak "could not understand '$_', bailing out"; | |||||||||||||||||||||||||||
| 106 | } ## end sub _resolve_rule | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 6 | my ($self, $condition) = @_; | |||||||||||||||||||||||||||
| 111 | 6 | ▶ | push @{$self->conditions()}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | ref($condition) ? $condition : _resolve_rule($condition); | ||||||||||||||||||||||||||||
| 113 | 6 | return; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 0 | my ($self, $condition) = @_; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | $condition = _resolve_rule($condition) unless ref $condition; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | my $conditions = $self->conditions(); | |||||||||||||||||||||||||||
| 120 | my @filtered = grep { | ||||||||||||||||||||||||||||
| 121 | 0 | my $keep; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | if (@$condition != @$_) { # different lengths => different | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $keep = 1; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | else { | ||||||||||||||||||||||||||||
| 126 | 0 | for my $i (0 .. $#$condition) { | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | last if $keep = $condition->[$i] ne $_->[$i]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 0 | $keep; | |||||||||||||||||||||||||||
| 131 | } @$conditions; | ||||||||||||||||||||||||||||
| 132 | 0 | $self->conditions(\@filtered); | |||||||||||||||||||||||||||
| 133 | 0 | return; | |||||||||||||||||||||||||||
| 134 | } ## end sub remove | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | sub exact { | ||||||||||||||||||||||||||
|
Subroutine
exact: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | shift if ref $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 1 | my ($target, $value) = @_; | |||||||||||||||||||||||||||
| 139 | 1 | return ['eq', $target, $value]; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 4 | ▶ | sub starts_with { | ||||||||||||||||||||||||||
|
Subroutine
starts_with: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 4 | ▶ | shift if ref $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 4 | my ($target, $value) = @_; | |||||||||||||||||||||||||||
| 145 | 4 | return ['starts-with', $target, $value]; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 1 | ▶ | sub range { | ||||||||||||||||||||||||||
|
Subroutine
range: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 1 | ▶ | shift if ref $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 1 | my ($target, $min, $max) = @_; | |||||||||||||||||||||||||||
| 151 | 1 | return [$target, $min, $max]; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2 | ▶ | sub json { | ||||||||||||||||||||||||||
|
Subroutine
json: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 2 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 156 | 2 | my %params = %$self; | |||||||||||||||||||||||||||
| 157 | 2 | ▶ | delete $params{expiration} unless defined $params{expiration}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 2 | return to_json(\%params, $args); | |||||||||||||||||||||||||||
| 159 | } ## end sub json | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1 | ▶ | sub base64 { | ||||||||||||||||||||||||||
|
Subroutine
base64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | 1 | return encode_base64(Encode::encode('utf-8', $self->json(@_))); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 2 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | 2 | *stringify = \&json; | |||||||||||||||||||||||||||
| 169 | 2 | *json_base64 = \&base64; | |||||||||||||||||||||||||||
| 170 | 2 | *stringify_base64 = \&base64; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 2 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | 2 | my ($self, $json) = @_; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 2 | ▶ | $json = decode_base64($json) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | unless substr($json, 0, 1) eq '{'; | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 2 | my %decoded = %{from_json($json)}; | |||||||||||||||||||||||||||
| 180 | $self->{conditions} = [ | ||||||||||||||||||||||||||||
| 181 | map { | ||||||||||||||||||||||||||||
| 182 | 14 | ▶ | if (ref($_) eq 'ARRAY') { $_; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | else { | ||||||||||||||||||||||||||||
| 184 | 2 | my ($name, $value) = %$_; | |||||||||||||||||||||||||||
| 185 | 2 | ['eq', '$' . $name, $value]; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 2 | } @{$decoded{conditions}} | |||||||||||||||||||||||||||
| 188 | ]; | ||||||||||||||||||||||||||||
| 189 | 2 | $self->{expiration} = $decoded{expiration}; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 2 | return $self; | |||||||||||||||||||||||||||
| 192 | } ## end sub parse | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | sub signature { | ||||||||||||||||||||||||||
|
Subroutine
signature: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 195 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 196 | 0 | require Digest::HMAC_SHA1; | |||||||||||||||||||||||||||
| 197 | 0 | return Digest::HMAC_SHA1::hmac_sha1($self->base64(), $key); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | ▶ | sub signature_base64 { | ||||||||||||||||||||||||||
|
Subroutine
signature_base64: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 201 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 202 | 0 | return encode_base64($self->signature($key)); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # Wrapper around base64 encoder, ensuring that there's no newline | ||||||||||||||||||||||||||||
| 206 | # to make AWS S3 happy | ||||||||||||||||||||||||||||
| 207 | 1 | ▶ | sub encode_base64 { | ||||||||||||||||||||||||||
|
Subroutine
encode_base64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | 1 | return MIME::Base64::encode_base64($_[0], ''); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 2 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||