FargateStack/Builder/HTTPService.pm

filter: Statementstmt 8.1% 28 / 345 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 77 MC/DCmcdc 0.0% 0 / 48 Subroutinesub 29.0% 9 / 31 Podpod 0.0% 0 / 11 totaltotal 5.8% 37 / 632 CC 93 SCAR 56.0
CC
93
Cov
5%
create_alb
812.0
create_listeners
306.0
create_target_group
156.0
CRAP 269.2
« FargateStack/CreateStack.pm ↑ Summary FargateStack/Builder.pm »
1 package App::FargateStack::Builder::HTTPService;
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 Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util qw(none any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use App::FargateStack::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use App::FargateStack::Builder::Utils qw(log_die);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 ########################################################################
17 0 ▶sub build_http_service {
Subroutine build_http_service: not called CC=8 SCAR=42.8
Pod undocumented
18 ########################################################################
19 0 my ($self) = @_;
20  
21 0 my ( $config, $dryrun, $security_groups ) = $self->common_args(qw(config dryrun security_groups));
22  
23 0 $self->create_alb();
24  
25 0 $self->benchmark('http-service:create-alb');
26  
27 0 my $fargate_sg = $security_groups->{fargate}->{group_id};
28 0 my $alb_sg = $security_groups->{alb}->{group_id};
29  
30 0 my $ec2 = $self->fetch_ec2;
31  
32 0 ▶ if ( $fargate_sg && $alb_sg && $ec2->is_sg_authorized( $fargate_sg, $alb_sg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fargate_sg and $alb_sg and $ec2->is_sg_authorized($fargate_sg, $alb_sg)) { } 0 0 0
Condition0%0/6
$fargate_sg and $alb_sg and $ec2->is_sg_authorized($fargate_sg, $alb_sg)
!ll&&!rl&&r
000
$fargate_sg and $alb_sg
!ll&&!rl&&r
000
MC/DC0%0/3
$fargate_sg and $alb_sg and $ec2->is_sg_authorized($fargate_sg, $alb_sg)
$fargate_sg $alb_sg $ec2->is_sg_authorized($fargate_sg, $alb_sg)
Truth table0%0/4
A and B and C
A= $fargate_sgB= $alb_sgC= $ec2->is_sg_authorized($fargate_sg, $alb_sg)
ABCresult
0XX0
10X0
1100
1111
33 0 $self->log_info( q{http-service: security-group-ingress: ALB's security group: [%s] already authorized...skipping},
34 $alb_sg );
35 }
36 else {
37  
38 0 ▶ $self->log_warn(
Condition0%0/4
$alb_sg // "???"
l!l
00
$fargate_sg // "???"
l!l
00
MC/DC0%0/1
$alb_sg // "???"
$alb_sg
MC/DC0%0/1
$fargate_sg // "???"
$fargate_sg
Truth table0%0/2
A
A= $alb_sg
Aresult
11
00
Truth table0%0/2
A
A= $fargate_sg
Aresult
11
00
39 q{http-service: security-group-ingress: ALB's security group: [%s] will be authorized to Fargate's: [%s]...%s},
40 $alb_sg // '???',
41 $fargate_sg // '???', $dryrun
42 );
43 }
44  
45 0 ▶ if ( !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryrun) 0 0 0
46 0 $ec2->authorize_security_group_ingress( group_id => $fargate_sg, source_group => $alb_sg );
47 }
48  
49 0 $self->create_target_group();
50 0 $self->benchmark('http-service:create-target-group');
51  
52 0 $self->create_listeners();
53 0 $self->benchmark('http-service:create-listeners');
54  
55 0 ▶ if ( $self->is_https ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_https) 0 0 0
56 0 $self->attach_certificate();
57 }
58  
59 0 return $TRUE;
60 }
61  
62 ########################################################################
63 0 ▶sub attach_certificate {
Subroutine attach_certificate: not called CC=11 SCAR=48.8
Pod undocumented
64 ########################################################################
65 0 my ($self) = @_;
66  
67 0 my ( $config, $dryrun, $alb ) = $self->common_args(qw(config dryrun alb));
68  
69 0 my $domain = $config->{domain};
70  
71 0 my $alb_arn = $alb->{arn};
72  
73 0 my $elb = $self->fetch_elbv2;
74  
75 0 ▶ if ( !$alb_arn || $alb_arn =~ /[?]{3}/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $alb_arn or $alb_arn =~ /[?]{3}/msx) 0 0 0
Condition0%0/3
not $alb_arn or $alb_arn =~ /[?]{3}/msx
l!l&&r!l&&!r
000
MC/DC0%0/2
not $alb_arn or $alb_arn =~ /[?]{3}/msx
not $alb_arn $alb_arn =~ /[?]{3}/msx
Truth table0%0/3
not(A) or B
A= not $alb_arnB= $alb_arn =~ /[?]{3}/msx
ABresult
1X1
011
000
76 0 $self->log_warn('http-service: no ALB created yet, cannot attach certificate');
77 0 return;
78 }
79  
80 0 my $listener = $elb->describe_listeners( $alb_arn, 'Listeners[?Port == `443`]' );
81  
82 log_die( $self, 'ERROR: could not find a 443 listener for ALB: [%s]', $alb_arn )
83 0 ▶ if !$listener || !@{$listener};
Branch: 1 branch
BranchTrueFalseTotal
unless $listener and @{$listener;} 0 0 0
Condition0%0/3
$listener and @{$listener;}
!ll&&!rl&&r
000
MC/DC0%0/2
$listener and @{$listener;}
$listener @{$listener;}
Truth table0%0/3
A and B
A= $listenerB= @{$listener;}
ABresult
0X0
100
111
84  
85 0 my $listener_arn = $listener->[0]->{ListenerArn};
86  
87 0 my $certificates = $elb->list_certificates($listener_arn);
88  
89 0 ▶ log_die( $self, 'ERROR: could not list certificates for ALB: [%s]', $alb_arn )
Branch: 1 branch
BranchTrueFalseTotal
unless $certificates 0 0 0
90 if !$certificates;
91  
92 0 ▶ $self->log_trace( sub { return Dumper( [ certificates => $certificates ] ); } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
93  
94 0 my $certificate_arn = $config->{certificate_arn};
95  
96 0 ▶ if ($certificate_arn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($certificate_arn) 0 0 0
97 0 ▶ if ( any { $_ eq $certificate_arn } @{$certificates} ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $certificate_arn; } , @{$certificates;})) 0 0 0
98 0 $self->log_info( 'http-service: certificate for [%s] already attached to listener...skipping', $domain );
99 0 return;
100 }
101 }
102  
103 0 $self->log_warn( 'http-service: certificate for [%s] will be added to listener...%s', $domain, $dryrun );
104  
105 return
106 0 ▶ if $dryrun;
Branch: 1 branch
BranchTrueFalseTotal
if $dryrun 0 0 0
107  
108 0 ▶ log_die( $self, 'http-service: no certificate arn' )
Branch: 1 branch
BranchTrueFalseTotal
unless $certificate_arn 0 0 0
109 if !$certificate_arn;
110  
111 0 my $result = $elb->add_listener_certificate( $listener_arn, $certificate_arn );
112  
113 0 ▶ log_die( $self, "ERROR: could not add certificate [%s] to ALB [%s]\n%s", $certificate_arn, $alb_arn, $elb->get_error )
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
114 if !$result;
115  
116 0 $self->log_warn( 'http-service: successfully added certificate for [%s] to ALB', $domain );
117  
118 0 return;
119 }
120  
121 ########################################################################
122 0 ▶sub fetch_listeners_by_port {
Subroutine fetch_listeners_by_port: not called CC=2 SCAR=17.9
Pod undocumented
123 ########################################################################
124 0 my ( $self, $alb_arn ) = @_;
125  
126 0 my $elb = $self->get_elbv2;
127  
128 0 my $listeners = $elb->describe_listeners( $alb_arn, q{Listeners} );
129  
130 0 ▶ return $listeners ? map { ( $_->{Port} => $_ ) } @{$listeners} : ();
Branch: 1 branch
BranchTrueFalseTotal
$listeners ? : 0 0 0
131 }
132  
133 ########################################################################
134 0 ▶sub create_listeners {
Subroutine create_listeners: not called CC=17 SCAR=57.2
Pod undocumented
135 ########################################################################
136 0 my ($self) = @_;
137  
138 0 my ( $config, $dryrun ) = $self->common_args(qw(config dryrun));
139  
140 0 my $elb = $self->get_elbv2;
141  
142 0 my ( $alb_arn, $alb_type, $alb_port, $alb_redirect ) = @{ $config->{alb} }{qw(arn type port redirect_80)};
143  
144 0 ▶ if ( !defined $alb_redirect && $self->is_https ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $alb_redirect and $self->is_https) 0 0 0
Condition0%0/3
not defined $alb_redirect and $self->is_https
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $alb_redirect and $self->is_https
not defined $alb_redirect $self->is_https
Truth table0%0/3
not(A) and B
A= not defined $alb_redirectB= $self->is_https
ABresult
0X0
100
111
145 0 $config->{arn}->{redirect_80} = $alb_redirect = $TRUE;
146 }
147  
148 0 ▶ if ( !$alb_arn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alb_arn) 0 0 0
149 0 $self->log_info( 'http-service: deferring listener creation, no ALB yet...%s', $dryrun );
150 0 return;
151 }
152  
153 0 my $service = $config->{tasks}->{ $self->get_http };
154  
155 0 my $target_group_arn = $service->{target_group_arn};
156  
157 0 my %listeners_by_port = $self->fetch_listeners_by_port($alb_arn);
158  
159 0 my %default_actions = ( $alb_port => [ { Type => 'forward', 'TargetGroupArn' => $target_group_arn } ] );
160  
161 0 ▶ if ( $self->is_https && $alb_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_https and $alb_redirect) 0 0 0
Condition0%0/3
$self->is_https and $alb_redirect
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_https and $alb_redirect
$self->is_https $alb_redirect
Truth table0%0/3
A and B
A= $self->is_httpsB= $alb_redirect
ABresult
0X0
100
111
162 0 $default_actions{80} = [
163 { Type => 'redirect',
164 RedirectConfig => {
165 Protocol => 'HTTPS',
166 Port => $alb_port,
167 StatusCode => 'HTTP_301'
168 }
169 }
170 ];
171 }
172  
173 0 my $domain = $config->{domain};
174 0 my @ports = keys %default_actions;
175  
176 0 my $needs_cert = $self->has_https_service;
177  
178 0 foreach my $port (@ports) {
179 0 $default_actions{$port} = encode_json( $default_actions{$port} );
180  
181 # create listener returns listener configuration (uses --query)
182 0 ▶ if ( !$listeners_by_port{$port} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $listeners_by_port{$port}) { } 0 0 0
183 0 $self->log_warn( 'http-service: listener for port [%s] will be created...%s', $port, $dryrun );
184  
185 $self->inc_required_resources(
186 listeners => [
187 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
188 0 my ($dryrun) = @_;
189  
190 0 ▶ return $dryrun ? 'arn:???' : $self->get_listeners_by_port( $alb_arn, $port );
Branch: 1 branch
BranchTrueFalseTotal
$dryrun ? : 0 0 0
191 }
192 0 ]
193 );
194  
195 0 ▶ if ( !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryrun) 0 0 0
196 $listeners_by_port{$port} = $elb->create_listener(
197 alb_arn => $alb_arn,
198 port => $port,
199 default_actions => $default_actions{$port},
200 query => 'Listeners[0]',
201 0 ▶ $needs_cert ? ( certificate_arn => $config->{certificate_arn} ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$needs_cert ? : 0 0 0
202 );
203  
204 0 $elb->check_result( message => 'ERROR: could not create listener for port: [%s]', $port );
205 }
206 }
207 else {
208 0 $self->log_info( 'http-service: listener for port [%s] exists...skipping', $port );
209 0 $self->inc_existing_resources( listeners => [ $listeners_by_port{$port}->{ListenerArn} ] );
210  
211 # add certificate...https
212 0 ▶ if ( $needs_cert && !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_cert and not $dryrun) 0 0 0
Condition0%0/3
$needs_cert and not $dryrun
!ll&&!rl&&r
000
MC/DC0%0/2
$needs_cert and not $dryrun
$needs_cert not $dryrun
Truth table0%0/3
A and not(B)
A= $needs_certB= not $dryrun
ABresult
0X0
100
111
213 0 $elb->add_listener_certificate( $listeners_by_port{$port}->{ListenerArn}, $config->{certificate_arn} );
214 }
215 }
216 }
217  
218 0 foreach my $port (@ports) {
219  
220 0 my $listener = $listeners_by_port{$port};
221  
222 0 $self->log_debug(
223 Dumper(
224 [ listener => $listener,
225 port => $port
226 ]
227 )
228 );
229  
230 0 my $rules = $elb->fetch_rules_by_domain( $domain, $listener->{ListenerArn} );
231  
232 0 ▶ $self->log_trace( sub { return Dumper( [ rules => $rules ] ); } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
233  
234 0 ▶ if ( $rules && @{$rules} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules and @{$rules;}) 0 0 0
Condition0%0/3
$rules and @{$rules;}
!ll&&!rl&&r
000
MC/DC0%0/2
$rules and @{$rules;}
$rules @{$rules;}
Truth table0%0/3
A and B
A= $rulesB= @{$rules;}
ABresult
0X0
100
111
235 0 $self->log_info( 'http-service: listener rule for port: [%s] exists...skipping', $port );
236 0 $self->inc_existing_resources( listener_rules => [ map { $_->{RuleArn} } @{$rules} ] );
237 0 next;
238 }
239  
240 0 $rules = $elb->describe_rules( $listener->{ListenerArn}, 'Rules' );
241  
242 # find next priority
243 my @priorities
244 0 = sort { $a <=> $b } map { $_->{Priority} } grep { $_->{Priority} =~ /^\d+$/xsm } @{$rules};
245  
246 0 ▶ my $priority = @priorities ? $priorities[-1] + 1 : 100;
Branch: 1 branch
BranchTrueFalseTotal
@priorities ? : 0 0 0
247  
248 0 $self->log_warn( 'http-service: listener rule for [%s] on port: [%s] will be created...%s', $domain, $port, $dryrun );
249  
250 $self->inc_required_resources(
251 listener_rules => [
252 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
253 0 my ($dryrun) = @_;
254  
255 0 ▶ return 'arn:???'
Branch: 1 branch
BranchTrueFalseTotal
if $dryrun 0 0 0
256 if $dryrun;
257  
258 0 my $rules = $elb->fetch_rule_arns_by_domain( $domain, $listener->{ListenerArn} );
259  
260 0 ▶ return q{}
Branch: 1 branch
BranchTrueFalseTotal
unless $rules 0 0 0
261 if !$rules;
262  
263 0 return join q{, }, @{$rules};
264 }
265 0 ]
266 );
267  
268 0 ▶ if ( !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryrun) 0 0 0
269 0 my $conditions = "Field=host-header,Values=$domain";
270  
271 $elb->create_rule(
272 listener_arn => $listener->{ListenerArn},
273 priority => $priority,
274 conditions => $conditions,
275 0 default_action => $default_actions{$port}
276 );
277 }
278 }
279  
280 0 return;
281 }
282  
283 ########################################################################
284 0 ▶sub create_target_group {
Subroutine create_target_group: not called CC=12 SCAR=50.5
Pod undocumented
285 ########################################################################
286 0 my ($self) = @_;
287  
288 0 my ( $config, $tasks, $dryrun ) = $self->common_args(qw(config tasks dryrun));
289  
290 0 my $elb = $self->fetch_elbv2;
291  
292 # we would not be here unless we have an HTTP service
293 0 my $task_name = $self->get_http;
294 0 my $task = $tasks->{$task_name};
295  
296 0 ▶ $task->{target_group_name} //= $self->create_default('target-group-name');
Condition0%0/3
$task->{"target_group_name"} //= $self->create_default("target-group-name")
l!l&&r!l&&!r
000
MC/DC0%0/2
$task->{"target_group_name"} //= $self->create_default("target-group-name")
$task->{"target_group_name"} $self->create_default("target-group-name")
Truth table0%0/3
A //= B
A= $task->{"target_group_name"}B= $self->create_default("target-group-name")
ABresult
1X1
011
000
297 0 my $target_group_name = $task->{target_group_name};
298  
299 0 ▶ if ( my $target_group = $elb->target_group_exists($target_group_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $target_group = $elb->target_group_exists($target_group_name)) { } 0 0 0
300 0 $self->log_info( 'http-service: target group [%s] exists...skipping', $target_group_name );
301  
302 0 $task->{target_group_arn} = $target_group->{TargetGroupArn};
303  
304 0 $self->inc_existing_resources( target_group => $task->{target_group_arn} );
305 }
306 else {
307 0 $self->log_warn( 'http-service: target group [%s] will be created...%s', $target_group_name, $dryrun );
308  
309 $self->inc_required_resources(
310 0 ▶ target_group => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
311 0 my ($dryrun) = @_;
312 0 ▶ return $dryrun ? "arn:???/$target_group_name" : $task->{target_group_arn};
Branch: 1 branch
BranchTrueFalseTotal
$dryrun ? : 0 0 0
313 }
314 0 );
315  
316 0 ▶ my $health_check = $task->{health_check} // {};
Condition0%0/2
$task->{"health_check"} // {}
l!l
00
MC/DC0%0/1
$task->{"health_check"} // {}
$task->{"health_check"}
Truth table0%0/2
A
A= $task->{"health_check"}
Aresult
11
00
317  
318 0 ▶ if ( !defined $task->{health_check} || $health_check->{enabled} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $task->{'health_check'} or $health_check->{'enabled'}) { } 0 0 0
Condition0%0/3
not defined $task->{"health_check"} or $health_check->{"enabled"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $task->{"health_check"} or $health_check->{"enabled"}
not defined $task->{"health_check"} $health_check->{"enabled"}
Truth table0%0/3
not(A) or B
A= not defined $task->{"health_check"}B= $health_check->{"enabled"}
ABresult
1X1
011
000
319 0 ▶ $health_check->{enabled} //= 'true';
Condition0%0/2
$health_check->{"enabled"} //= "true"
l!l
00
MC/DC0%0/1
$health_check->{"enabled"} //= "true"
$health_check->{"enabled"}
Truth table0%0/2
A
A= $health_check->{"enabled"}
Aresult
11
00
320 0 ▶ $health_check->{port} //= $task->{container_port};
Condition0%0/3
$health_check->{"port"} //= $task->{"container_port"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$health_check->{"port"} //= $task->{"container_port"}
$health_check->{"port"} $task->{"container_port"}
Truth table0%0/3
A //= B
A= $health_check->{"port"}B= $task->{"container_port"}
ABresult
1X1
011
000
321 0 ▶ $health_check->{path} //= q{/};
Condition0%0/2
$health_check->{"path"} //= "/"
l!l
00
MC/DC0%0/1
$health_check->{"path"} //= "/"
$health_check->{"path"}
Truth table0%0/2
A
A= $health_check->{"path"}
Aresult
11
00
322  
323 0 ▶ $health_check->{interval_seconds} //= $DEFAULT_HEALTH_CHECK_INTERVAL;
Condition0%0/3
$health_check->{"interval_seconds"} //= $App::FargateStack::Builder::HTTPService::DEFAULT_HEALTH_CHECK_INTERVAL
l!l&&r!l&&!r
000
MC/DC0%0/2
$health_check->{"interval_seconds"} //= $App::FargateStack::Builder::HTTPService::DEFAULT_HEALTH_CHECK_INTERVAL
$health_check->{"interval_seconds"} $App::FargateStack::Builder::HTTPService::DEFAULT_HEALTH_CHECK_INTERVAL
Truth table0%0/3
A //= B
A= $health_check->{"interval_seconds"}B= $App::FargateStack::Builder::HTTPService::DEFAULT_HEALTH_CHECK_INTERVAL
ABresult
1X1
011
000
324 0 ▶ $health_check->{timeout_seconds} //= $DEFAULT_HEALTH_CHECK_TIMEOUT;
Condition0%0/3
$health_check->{"timeout_seconds"} //= $App::FargateStack::Builder::HTTPService::DEFAULT_HEALTH_CHECK_TIMEOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$health_check->{"timeout_seconds"} //= $App::FargateStack::Builder::HTTPService::DEFAULT_HEALTH_CHECK_TIMEOUT
$health_check->{"timeout_seconds"} $App::FargateStack::Builder::HTTPService::DEFAULT_HEALTH_CHECK_TIMEOUT
Truth table0%0/3
A //= B
A= $health_check->{"timeout_seconds"}B= $App::FargateStack::Builder::HTTPService::DEFAULT_HEALTH_CHECK_TIMEOUT
ABresult
1X1
011
000
325 0 $health_check->{healthy_threshold_count} = $DEFAULT_HEALTH_HEALTHY_CHECK_THRESHOLD;
326 0 $health_check->{unhealthy_threshold_count} = $DEFAULT_HEALTH_UNHEALTHY_CHECK_THRESHOLD;
327  
328 0 $health_check->{matcher} = '200';
329 }
330 else {
331 0 log_die( $self, 'ERROR: health checks must be enabled' );
332 }
333  
334 0 $task->{health_check} = $health_check;
335  
336 0 ▶ $self->log_debug( sub { return Dumper( [ health_check => $health_check ] ); } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
337  
338 0 ▶ if ( !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryrun) 0 0 0
339  
340 0 $task->{target_group_arn} = $elb->create_target_group(
341 name => $target_group_name,
342 health_check => $health_check,
343 );
344  
345 0 $elb->check_result( message => 'ERROR: could not create target group:[%s]', params => [$target_group_name] );
346  
347 0 $self->log_info( "\tarn: %s", $task->{target_group_arn} );
348 }
349 }
350  
351 0 return;
352 }
353  
354 ########################################################################
355 0 ▶sub is_https { goto &has_https_service; }
Subroutine is_https: not called CC=1 SCAR=6.9
Pod undocumented
356 ########################################################################
357 0 ▶sub has_https_service {
Subroutine has_https_service: not called CC=2 SCAR=17.9
Pod undocumented
358 ########################################################################
359 0 my ($self) = @_;
360  
361 0 my $services = $self->get_config->{tasks};
362  
363 0 my $http_service = $self->get_http;
364  
365 return
366 0 ▶ if !$http_service;
Branch: 1 branch
BranchTrueFalseTotal
unless $http_service 0 0 0
367  
368 0 return $services->{$http_service}->{type} eq 'https';
369 }
370  
371 ########################################################################
372 0 ▶sub create_alb {
Subroutine create_alb: not called CC=28 SCAR=67.0
Pod undocumented
373 ########################################################################
374 0 my ($self) = @_;
375  
376 0 my ( $config, $dryrun, $alb, $security_groups ) = $self->common_args(qw(config dryrun alb security_groups));
377  
378 0 ▶ if ( !$alb ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alb) 0 0 0
379 0 $alb = $config->{alb} = {};
380 0 $config->{alb} = $alb;
381 }
382  
383 0 ▶ if ( !$security_groups ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($security_groups) 0 0 0
384 0 $security_groups = $config->{security_groups} = {};
385 0 $config->{security_groups} = $security_groups;
386 }
387  
388 0 my $elb = $self->fetch_elbv2();
389 0 my $ec2 = $self->fetch_ec2;
390  
391 # if we have defined one in the config, verify
392 0 ▶ if ( my $alb_arn = $alb->{arn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alb_arn = $alb->{"arn"}) 0 0 0
393  
394 0 my $alb_info = $elb->describe_load_balancer( $alb_arn, 'LoadBalancers[0]' );
395 0 $elb->check_result( message => 'ERROR: could not describe load balancer: [%s]', $alb_arn );
396  
397 0 $self->set_alb($alb_info);
398  
399 0 my ( $alb_name, $alb_security_groups ) = @{$alb_info}{qw(LoadBalancerName SecurityGroups)};
400  
401 0 ▶ $self->log_trace( sub { return Dumper( [ alb => $alb_info ] ); } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
402  
403 0 $self->log_info( 'http-service: load balancer [%s] defined in config and verified...skipping', $alb_name );
404  
405 0 $self->inc_existing_resources( alb => $alb_arn );
406  
407 0 my $alb_sg = $security_groups->{alb}->{group_id};
408  
409 0 ▶ if ( !$alb_sg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alb_sg) 0 0 0
410 0 $self->log_info('http-service: no ALB security group in configuration...looking...');
411  
412 # find the security group that allows 443 ingress
413  
414 0 my $query = 'SecurityGroupRules[?IsEgress == `false` && ToPort == `443`]';
415  
416 0 foreach my $group_id ( @{$alb_security_groups} ) {
417 0 my $sg = $ec2->describe_security_group_rules( group_id => $group_id, $query );
418  
419 0 ▶ next if !@{$sg};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$sg;} 0 0 0
420  
421 0 $alb_sg = $sg->[0]->{GroupId}; # or $group_id
422 0 last;
423 }
424  
425 0 ▶ if ($alb_sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alb_sg) 0 0 0
426 $self->log_warn( 'http-service: found a security group for ALB [%s] with 443 ingress, using [%s]',
427 0 $alb_info->{LoadBalancerName}, $alb_sg );
428 }
429 }
430  
431 0 ▶ if ( !$alb_sg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alb_sg) 0 0 0
432 0 $alb_sg = $alb_security_groups->[0];
433  
434 0 $self->log_warn( 'http-service: could not find a security group for ALB [%s] with 443 ingress, using [%s]',
435 $alb_name, $alb_sg );
436 }
437  
438 0 $security_groups->{alb}->{group_id} = $alb_sg;
439 0 $security_groups->{alb}->{group_name} = $ec2->find_security_group_name($alb_sg);
440  
441 0 $self->inc_existing_resources( security_groups => [$alb_sg] );
442  
443 0 return;
444 }
445  
446 # --create-alb forces creation of a new ALB
447 0 ▶ if ( !$self->get_create_alb && !$alb->{create} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_create_alb or $alb->{"create"}) 0 0 0
Condition0%0/3
$self->get_create_alb or $alb->{"create"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get_create_alb or $alb->{"create"}
$self->get_create_alb $alb->{"create"}
Truth table0%0/3
A or B
A= $self->get_create_albB= $alb->{"create"}
ABresult
1X1
011
000
448 0 my $alb_type = $alb->{type};
449  
450 0 $self->log_error( 'http-service: WARNING - no ALB ARN defined in configuration...looking for %s ALB', $alb_type );
451  
452 0 my ( $alb_arn, $security_group_id ) = eval { return $elb->find_alb($alb_type); };
453 0 my $err = $EVAL_ERROR;
454  
455 0 ▶ if ( !$alb_arn || $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $alb_arn or $err) 0 0 0
Condition0%0/3
not $alb_arn or $err
l!l&&r!l&&!r
000
MC/DC0%0/2
not $alb_arn or $err
not $alb_arn $err
Truth table0%0/3
not(A) or B
A= not $alb_arnB= $err
ABresult
1X1
011
000
456 0 ▶ if ( $err =~ /no\salbs/xsm ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err =~ /no\salbs/msx) { } 0 0 0
elsif ($err =~ /more\sthan\sone/msx) { } 0 0 0
457 0 $self->log_error( 'http-service: no %s ALBs were found in this VPC...a new ALB will be created...%s',
458 $alb_type, $dryrun );
459 }
460 elsif ( $err =~ /more\sthan\sone/xsm ) {
461 0 $self->log_die(
462 'http-service: more than 1 %s ALBs were found...add the ARN to your configuration file or use the --create-alb option.',
463 $alb_type
464 );
465 }
466 else {
467 0 die "$err";
468 }
469 }
470  
471 $self->log_trace(
472 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
473 0 return Dumper(
474 [ alb_arn => $alb_arn,
475 security_group => $security_group_id
476 ]
477 );
478 }
479 0 );
480  
481 0 my $security_group_name;
482 my $is_valid_alb;
483  
484 0 ▶ if ($alb_arn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alb_arn) 0 0 0
485 0 $security_group_name = $ec2->find_security_group_name($security_group_id);
486  
487 0 ▶ $is_valid_alb = $elb->validate_alb(
Branch: 1 branch
BranchTrueFalseTotal
$self->is_https ? : 0 0 0
488 arn => $alb_arn,
489 scheme => $self->is_https ? 'internet-facing' : 'internal'
490 );
491 }
492  
493 0 ▶ if ( $alb_arn && $is_valid_alb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alb_arn and $is_valid_alb) 0 0 0
Condition0%0/3
$alb_arn and $is_valid_alb
!ll&&!rl&&r
000
MC/DC0%0/2
$alb_arn and $is_valid_alb
$alb_arn $is_valid_alb
Truth table0%0/3
A and B
A= $alb_arnB= $is_valid_alb
ABresult
0X0
100
111
494  
495 # set this for later
496 0 $self->set_alb( $elb->get_alb );
497  
498 0 $self->log_error( 'http-service: Found an existing ALB (%s)...will be added to configuration.', $alb_arn );
499  
500 0 $security_groups->{alb}->{group_id} = $security_group_id;
501 0 $security_groups->{alb}->{name} = $security_group_name;
502  
503 0 $alb->{arn} = $alb_arn;
504 0 $alb->{name} = $self->get_alb->{LoadBalancerName};
505  
506 0 $self->inc_existing_resources( alb => $config->{alb}->{name} );
507  
508 # me may still need to attach Fargate's security group to this ALB
509 # check to see if fargate's security group is already attached
510  
511 0 my $query = sprintf 'LoadBalancers[?LoadBalancerArn == `%s`]|[0].SecurityGroups', $alb_arn;
512  
513 0 my $alb_security_groups = $elb->describe_load_balancers( query => $query );
514  
515 0 ▶ my $fargate_sg = $security_groups->{fargate}->{group_id} // 'not-provisioned-yet';
Condition0%0/2
$security_groups->{"fargate"}{"group_id"} // "not-provisioned-yet"
l!l
00
MC/DC0%0/1
$security_groups->{"fargate"}{"group_id"} // "not-provisioned-yet"
$security_groups->{"fargate"}{"group_id"}
Truth table0%0/2
A
A= $security_groups->{"fargate"}{"group_id"}
Aresult
11
00
516  
517 0 ▶ if ( none { $_ eq $fargate_sg } @{$alb_security_groups} ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $_ eq $fargate_sg; } , @{$alb_security_groups;})) { } 0 0 0
518 0 $self->log_warn( 'http-service: will add security group %s to load balancer...%s', $fargate_sg, $dryrun );
519  
520 0 ▶ if ( !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryrun) 0 0 0
521 0 $elb->set_security_groups( $alb_arn, @{$alb_security_groups}, $fargate_sg );
522 }
523 }
524 else {
525 0 $self->log_info( 'http-service: security group %s already attached...skipping', $fargate_sg );
526 }
527  
528 0 return;
529 }
530 }
531  
532 # create an alb
533 0 ▶ if ( $self->get_create_alb || $alb->{create} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_create_alb or $alb->{'create'}) { } 0 0 0
Condition0%0/3
$self->get_create_alb or $alb->{"create"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get_create_alb or $alb->{"create"}
$self->get_create_alb $alb->{"create"}
Truth table0%0/3
A or B
A= $self->get_create_albB= $alb->{"create"}
ABresult
1X1
011
000
534 0 $self->log_warn( 'http-service: ALB creation forced by configuration or option...will be created...%s', $dryrun );
535 }
536 else {
537 0 $self->log_error(
538 'http-service: no ALB defined in your configuration and no usable ALB found...an ALB will be created...%s', $dryrun );
539 }
540  
541 0 my $alb_sg = $self->create_alb_security_group;
542 0 ▶ my $alb_type = $self->is_https ? 'public' : 'private';
Branch: 1 branch
BranchTrueFalseTotal
$self->is_https ? : 0 0 0
543  
544 0 my $subnets = [ @{ $self->get_subnets->{$alb_type} }[ ( 0, 1 ) ] ];
545  
546 0 ▶ my $alb_name = $alb->{name} // $self->create_default('alb-name');
Condition0%0/3
$alb->{"name"} // $self->create_default("alb-name")
l!l&&r!l&&!r
000
MC/DC0%0/2
$alb->{"name"} // $self->create_default("alb-name")
$alb->{"name"} $self->create_default("alb-name")
Truth table0%0/3
A // B
A= $alb->{"name"}B= $self->create_default("alb-name")
ABresult
1X1
011
000
547 0 $alb->{name} = $alb_name;
548  
549 $self->inc_required_resources(
550 0 ▶ alb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
551 0 my ($dryrun) = @_;
552 0 ▶ return $dryrun ? "arn:???/$alb_name" : $alb->{arn};
Branch: 1 branch
BranchTrueFalseTotal
$dryrun ? : 0 0 0
553 }
554 0 );
555  
556 0 ▶ if ( !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryrun) 0 0 0
557  
558 0 ▶ my $alb_info = $elb->create_load_balancer(
Branch: 1 branch
BranchTrueFalseTotal
$alb_type eq 'public' ? : 0 0 0
559 name => $alb_name,
560 subnets => $subnets,
561 scheme => $alb_type eq 'public' ? 'internet-facing' : 'internal',
562 security_groups => [$alb_sg],
563 tags => { CreatedBy => 'FargateStack' },
564 );
565  
566 0 $elb->check_result( message => 'ERROR: could not create load balancer: [%s]', $alb_name );
567  
568 0 $alb->{arn} = $alb_info->{LoadBalancers}->[0]->{LoadBalancerArn};
569  
570 0 $self->set_alb($alb_info);
571 }
572  
573 0 return;
574 }
575  
576 ########################################################################
577 0 ▶sub create_alb_security_group {
Subroutine create_alb_security_group: not called CC=8 SCAR=42.8
Pod undocumented
578 ########################################################################
579 0 my ($self) = @_;
580  
581 0 my ( $config, $dryrun, $app, $security_groups ) = $self->common_args(qw(config dryrun app security_groups));
582  
583 0 ▶ $security_groups //= {};
Condition0%0/2
$security_groups //= {}
l!l
00
MC/DC0%0/1
$security_groups //= {}
$security_groups
Truth table0%0/2
A
A= $security_groups
Aresult
11
00
584 0 $config->{security_groups} = $security_groups;
585  
586 # create security groups
587 0 my $ec2 = $self->fetch_ec2;
588  
589 0 my $sg_name = $self->create_default('alb-security-group-name');
590  
591 0 my $query = sprintf 'SecurityGroups[?GroupName == `%s`].{group_id: GroupId}', $sg_name;
592 0 my $result = $ec2->describe_security_group( $sg_name, $query );
593 0 $ec2->check_result( message => 'ERROR: could not describe security group: [%s]', $sg_name );
594  
595 0 $self->log_debug( [ result => $result, error => $ec2->get_error ] );
596  
597 0 ▶ if ( !$result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $result) { } 0 0 0
598 0 $self->inc_required_resources( security_groups => [$sg_name] );
599 0 $self->log_info( 'http-service: ALB security group [%s] will be created...%s', $sg_name, $dryrun );
600 }
601 else {
602 0 $self->log_info( 'http-service: ALB security group [%s] exists...skipping', $sg_name );
603 }
604  
605 0 my $sg = $result->{group_id};
606  
607 0 ▶ my @allow_ports = ( $config->{alb}->{port}, $config->{alb}->{redirect_80} ? 80 : () );
Branch: 1 branch
BranchTrueFalseTotal
$config->{'alb'}{'redirect_80'} ? : 0 0 0
608  
609 0 ▶ if ( !$sg && !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sg || $dryrun) { } 0 0 0
Condition0%0/3
$sg || $dryrun
l!l&&r!l&&!r
000
MC/DC0%0/2
$sg || $dryrun
$sg $dryrun
Truth table0%0/3
A || B
A= $sgB= $dryrun
ABresult
1X1
011
000
610 0 my $sg_description = sprintf 'allow in-bound port(s): [%s] to %s-alb', join( q{,}, @allow_ports ), $app->{name};
611  
612 0 $sg = $ec2->create_security_group( $sg_name, $sg_description );
613 0 $ec2->check_result( message => 'ERROR: could not create security group: [%s]', $sg_name );
614  
615 0 $security_groups->{alb}->{group_id} = $sg;
616 0 $security_groups->{alb}->{name} = $sg_name;
617 }
618 else {
619 0 $sg = 'sg-????';
620 }
621  
622 0 $self->log_info( 'http-service: authorizing ingress for [%s] on port(s): [%s]...%s',
623 $sg, join( q{, }, @allow_ports ), $dryrun );
624  
625 0 ▶ if ( !$dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dryrun) 0 0 0
626 0 foreach my $port (@allow_ports) {
627 0 $ec2->authorize_security_group_ingress(
628 group_id => $sg,
629 port => $port,
630 cidr => '0.0.0.0/0',
631 );
632 }
633 }
634  
635 0 return $sg;
636 }
637  
638 ########################################################################
639 0 ▶sub create_alias {
Subroutine create_alias: not called CC=8 SCAR=42.8
Pod undocumented
640 ########################################################################
641 0 my ($self) = @_;
642  
643 0 my ( $config, $dryrun ) = $self->common_args(qw(config dryrun));
644  
645 0 my $domain = $config->{domain};
646  
647 0 my $zone_id = $config->{route53}->{zone_id};
648  
649 return
650 0 ▶ if !$domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain 0 0 0
651  
652 0 my $route53 = $self->fetch_route53;
653  
654 0 my $alb_arn = $config->{alb}->{arn};
655 0 my $elb = $self->fetch_elbv2;
656 0 my ( $alb_dns_name, $alb_zone_id );
657  
658 0 ▶ if ($alb_arn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alb_arn) 0 0 0
659  
660 0 my $alb = $elb->describe_load_balancer( $alb_arn, 'LoadBalancers[0]' );
661  
662 0 ( $alb_dns_name, $alb_zone_id ) = @{$alb}{qw(DNSName CanonicalHostedZoneId)};
663  
664 0 my $result = $route53->find_alias_record(
665 zone_id => $zone_id,
666 dns_name => $alb_dns_name,
667 domain_name => $domain
668 );
669  
670 0 $route53->check_result( message => 'ERROR: could not determine if an alias record exists for: [%s]', $domain );
671  
672 0 ▶ if ( $result && @{$result} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result and @{$result;}) 0 0 0
Condition0%0/3
$result and @{$result;}
!ll&&!rl&&r
000
MC/DC0%0/2
$result and @{$result;}
$result @{$result;}
Truth table0%0/3
A and B
A= $resultB= @{$result;}
ABresult
0X0
100
111
673 0 $self->log_info( 'http-service: alias record for [%s] exists...skipping', $domain );
674 0 return;
675 }
676 }
677  
678 0 $self->log_warn( 'route53: alias for [%s] will be created...%s', $domain, $dryrun );
679 0 $self->inc_required_resources( route53 => $domain );
680  
681 return
682 0 ▶ if $dryrun;
Branch: 1 branch
BranchTrueFalseTotal
if $dryrun 0 0 0
683  
684 0 ▶ log_die( $self, 'ERROR: ALB has not been created yet?' )
Branch: 1 branch
BranchTrueFalseTotal
unless $alb_dns_name and $alb_zone_id 0 0 0
Condition0%0/3
$alb_dns_name and $alb_zone_id
!ll&&!rl&&r
000
MC/DC0%0/2
$alb_dns_name and $alb_zone_id
$alb_dns_name $alb_zone_id
Truth table0%0/3
A and B
A= $alb_dns_nameB= $alb_zone_id
ABresult
0X0
100
111
685 if !$alb_dns_name || !$alb_zone_id;
686  
687 0 my $result = $route53->create_alias(
688 elb => $elb,
689 domain => $domain,
690 zone_id => $zone_id,
691 alb_dns_name => $alb_dns_name,
692 alb_zone_id => $alb_zone_id,
693 );
694  
695 0 $route53->check_result( message => 'ERROR: could not create alias record for [%s]', $domain );
696  
697 0 $self->log_warn( 'http-service: successfully create alias record for [%s]', $domain );
698  
699 0 return;
700 }
701  
702 ########################################################################
703 0 ▶sub get_listeners_by_port {
Subroutine get_listeners_by_port: not called CC=1 SCAR=6.9
Pod undocumented
704 ########################################################################
705 0 my ( $self, $alb_arn ) = @_;
706  
707 0 my $elb = $self->fetch_elbv2;
708  
709 0 my $listeners = $elb->describe_listeners( $alb_arn, 'Listeners' );
710 0 $elb->check_result( message => 'ERROR: could not describe listeners for: [%s]', $alb_arn );
711  
712 0 return map { $_->{Port} => $_->{ListenerArn} } @{$listeners};
713 }
714  
715 1 1;
« FargateStack/CreateStack.pm ↑ Summary FargateStack/Builder.pm »