FargateStack/Builder/S3Bucket.pm

filter: Statementstmt 39.6% 25 / 63 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 80.0% 8 / 10 Podpod 0.0% 0 / 2 totaltotal 28.2% 33 / 117 CC 15 SCAR 40.4
CC
15
Cov
26%
build_bucket
110.0
add_bucket_policy
42.0
BEGIN
1.0
CRAP 56.7
« FargateStack/Autoscaling.pm ↑ Summary FargateStack/Builder/Cluster.pm »
1 package App::FargateStack::Builder::S3Bucket;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use App::S3Api;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use App::FargateStack::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 ########################################################################
16 # Create an S3 bucket and provides method adding a bucket policy for
17 # the Fargate role.
18 # YAML configuration syntax:
19 ########################################################################
20 # bucket:
21 # name: bucket-name
22 # policy:
23 ########################################################################
24 0 ▶sub build_bucket {
Subroutine build_bucket: not called CC=10 SCAR=47.0
Pod undocumented
25 ########################################################################
26 0 my ($self) = @_;
27  
28 0 my $config = $self->get_config;
29  
30 0 my $bucket = $config->{bucket};
31  
32 return
33 0 ▶ if !$bucket || !$bucket->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $bucket and $bucket->{"name"} 0 0 0
Condition0%0/3
$bucket and $bucket->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$bucket and $bucket->{"name"}
$bucket $bucket->{"name"}
Truth table0%0/3
A and B
A= $bucketB= $bucket->{"name"}
ABresult
0X0
100
111
34  
35 0 my $s3 = App::S3Api->new( %{$bucket}, %{ $self->get_global_options } );
36  
37 0 my $dryrun = $self->get_dryrun;
38  
39 0 ▶ if ( $s3->bucket_exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s3->bucket_exists) { } 0 0 0
40 0 $self->get_logger->info( sprintf 'bucket: [%s] exists skipping...', $bucket->{name} );
41  
42 0 $self->inc_existing_resources( bucket => $bucket->{name} );
43 }
44 else {
45 0 $self->inc_required_resources( bucket => $bucket->{name} );
46  
47 0 $self->get_logger->info( sprintf 'bucket: [%s] will be created...%s', $bucket->{name}, $dryrun );
48  
49 0 ▶ if ( !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryrun) 0 0 0
50 0 $s3->create_bucket;
51 }
52 }
53  
54 0 ▶ if ( $bucket->{policy} && !$s3->policy_exists ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bucket->{'policy'} and not $s3->policy_exists) { } 0 0 0
elsif ($s3->policy_exists) { } 0 0 0
Condition0%0/3
$bucket->{"policy"} and not $s3->policy_exists
!ll&&!rl&&r
000
MC/DC0%0/2
$bucket->{"policy"} and not $s3->policy_exists
$bucket->{"policy"} not $s3->policy_exists
Truth table0%0/3
A and not(B)
A= $bucket->{"policy"}B= not $s3->policy_exists
ABresult
0X0
100
111
55 0 $self->get_logger->info( sprintf 'bucket policy: [%s] will be attached...%s', $bucket->{policy}, $dryrun );
56 0 $self->inc_required_resources( bucket_policy => $bucket->{policy} );
57  
58 0 ▶ if ( !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryrun) 0 0 0
59 0 $s3->put_bucket_policy;
60 }
61 }
62 elsif ( $s3->policy_exists ) {
63 0 ▶ $self->inc_existing_resources( bucket_policy => $bucket->{policy} // 'configured policy' );
Condition0%0/2
$bucket->{"policy"} // "configured policy"
l!l
00
MC/DC0%0/1
$bucket->{"policy"} // "configured policy"
$bucket->{"policy"}
Truth table0%0/2
A
A= $bucket->{"policy"}
Aresult
11
00
64 }
65  
66 0 return $TRUE;
67 }
68  
69 ########################################################################
70 0 ▶sub add_bucket_policy {
Subroutine add_bucket_policy: not called CC=6 SCAR=37.4
Pod undocumented
71 ########################################################################
72 0 my ($self) = @_;
73  
74 0 my $bucket = $self->get_config->{bucket};
75  
76 0 my ( $readonly, $name, $paths ) = @{$bucket}{qw(readonly name paths)};
77  
78 0 ▶ $readonly //= $FALSE;
Condition0%0/3
$readonly //= $App::FargateStack::Builder::S3Bucket::FALSE
l!l&&r!l&&!r
000
MC/DC0%0/2
$readonly //= $App::FargateStack::Builder::S3Bucket::FALSE
$readonly $App::FargateStack::Builder::S3Bucket::FALSE
Truth table0%0/3
A //= B
A= $readonlyB= $App::FargateStack::Builder::S3Bucket::FALSE
ABresult
1X1
011
000
79 0 ▶ $paths //= [];
Condition0%0/2
$paths //= []
l!l
00
MC/DC0%0/1
$paths //= []
$paths
Truth table0%0/2
A
A= $paths
Aresult
11
00
80  
81 0 my $bucket_arn = sprintf $S3_BUCKET_ARN_TEMPLATE, $name;
82 0 ▶ my $action = $readonly ? ['s3:GetObject'] : ['s3:*'];
Branch: 1 branch
BranchTrueFalseTotal
$readonly ? : 0 0 0
83  
84 0 my @resources = ( $bucket_arn, map { sprintf '%s/%s', $bucket_arn, $_ } @{$paths} );
85  
86 # ListBucket needed if using paths with readonly access
87 0 ▶ if ( $readonly && @{$paths} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($readonly and @{$paths;}) 0 0 0
Condition0%0/3
$readonly and @{$paths;}
!ll&&!rl&&r
000
MC/DC0%0/2
$readonly and @{$paths;}
$readonly @{$paths;}
Truth table0%0/3
A and B
A= $readonlyB= @{$paths;}
ABresult
0X0
100
111
88 0 push @{$action}, 's3:ListBucket';
89 }
90  
91 return {
92 0 Effect => 'Allow',
93 Action => $action,
94 Resource => \@resources,
95 };
96 }
97  
98 1 1;
« FargateStack/Autoscaling.pm ↑ Summary FargateStack/Builder/Cluster.pm »