| 1 | package App::ElbV2; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use App::EC2; | ||
| 7 | use Carp; | ||
| 8 | use Data::Dumper; | ||
| 9 | use List::Util qw(first any); | ||
| 10 | use JSON; | ||
| 11 | |||
| 12 | use Role::Tiny::With; | ||
| 13 | with 'App::AWS'; | ||
| 14 | |||
| 15 | use parent 'App::Command'; | ||
| 16 | |||
| 17 | __PACKAGE__->follow_best_practice; | ||
| 18 | __PACKAGE__->mk_accessors( | ||
| 19 | qw( | ||
| 20 | alb | ||
| 21 | certificate_arn | ||
| 22 | ec2 | ||
| 23 | name | ||
| 24 | profile | ||
| 25 | region | ||
| 26 | security_groups | ||
| 27 | subnets | ||
| 28 | tags | ||
| 29 | vpc_id | ||
| 30 | _alb_cache | ||
| 31 | ) | ||
| 32 | ); | ||
| 33 | |||
| 34 | ######################################################################## | ||
| 35 | sub add_listener_certificate { | ||
| 36 | ######################################################################## | ||
| 37 | my ( $self, $listener_arn, $certificate_arn ) = @_; | ||
| 38 | |||
| 39 | return $self->command( | ||
| 40 | 'add-listener-certificates' => [ | ||
| 41 | '--listener-arn' => $listener_arn, | ||
| 42 | '--certificates' => "CertificateArn=$certificate_arn", | ||
| 43 | ] | ||
| 44 | ); | ||
| 45 | } | ||
| 46 | |||
| 47 | ######################################################################## | ||
| 48 | sub modify_listener_certificate { | ||
| 49 | ######################################################################## | ||
| 50 | my ( $self, $listener_arn, $certificate_arn, $default_action ) = @_; | ||
| 51 | |||
| 52 | if ( !$default_action ) { | ||
| 53 | my $response = $self->command( 'describe-listeners' => [ '--listener-arns' => $listener_arn ] ); | ||
| 54 | |||
| 55 | my ($listener) = ref $response eq 'HASH' ? @{ $response->{Listeners} || [] } : (); | ||
| 56 | |||
| 57 | if ( $listener && $listener->{DefaultActions} ) { | ||
| 58 | $default_action = $listener->{DefaultActions}; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | die "Unable to retrieve existing default actions for listener $listener_arn\n"; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | return $self->command( | ||
| 66 | 'modify-listener' => [ | ||
| 67 | '--listener-arn' => $listener_arn, | ||
| 68 | '--certificates' => "CertificateArn=$certificate_arn", | ||
| 69 | '--default-actions' => encode_json($default_action), | ||
| 70 | ] | ||
| 71 | ); | ||
| 72 | } | ||
| 73 | |||
| 74 | ######################################################################## | ||
| 75 | sub list_certificates { | ||
| 76 | ######################################################################## | ||
| 77 | my ( $self, $listener_arn ) = @_; | ||
| 78 | |||
| 79 | my $query = 'Certificates[].CertificateArn'; | ||
| 80 | |||
| 81 | return $self->command( | ||
| 82 | 'describe-listener-certificates' => [ | ||
| 83 | '--listener-arn' => $listener_arn, | ||
| 84 | '--query' => $query, | ||
| 85 | ] | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | |||
| 89 | ######################################################################## | ||
| 90 | sub create_load_balancer { | ||
| 91 | ######################################################################## | ||
| 92 | my ( $self, %args ) = @_; | ||
| 93 | |||
| 94 | my ( $lb_name, $type, $scheme, $subnets, $security_groups, $tags ) = @args{qw(name type scheme subnets security_groups tags)}; | ||
| 95 | |||
| 96 | $type //= 'application'; | ||
| 97 | $scheme //= 'internet-facing'; | ||
| 98 | $lb_name //= $self->get_name; | ||
| 99 | |||
| 100 | croak "ERROR: subnets must be an array ref\n" | ||
| 101 | if !$subnets || !ref $subnets; | ||
| 102 | |||
| 103 | croak "ERROR: security_groups must be an array ref\n" | ||
| 104 | if !$security_groups || !ref $security_groups; | ||
| 105 | |||
| 106 | return $self->command( | ||
| 107 | 'create-load-balancer' => [ | ||
| 108 | '--name' => $lb_name, | ||
| 109 | '--type' => $type, | ||
| 110 | '--scheme' => $scheme, | ||
| 111 | '--subnets' => @{$subnets}, | ||
| 112 | '--security-groups' => @{$security_groups}, | ||
| 113 | $tags ? ( '--tags' => $self->format_tags($tags) ) : () | ||
| 114 | ] | ||
| 115 | ); | ||
| 116 | } | ||
| 117 | |||
| 118 | ######################################################################## | ||
| 119 | sub describe_listeners { | ||
| 120 | ######################################################################## | ||
| 121 | my ( $self, $alb_arn, $query ) = @_; | ||
| 122 | |||
| 123 | croak "no ALB arn\n" | ||
| 124 | if !$alb_arn; | ||
| 125 | |||
| 126 | return $self->command( | ||
| 127 | 'describe-listeners' => [ | ||
| 128 | '--load-balancer-arn' => $alb_arn, | ||
| 129 | $query ? ( '--query' => $query ) : (), | ||
| 130 | ] | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | |||
| 134 | ######################################################################## | ||
| 135 | sub describe_rules { | ||
| 136 | ######################################################################## | ||
| 137 | my ( $self, $listener_arn, $query ) = @_; | ||
| 138 | |||
| 139 | croak "no listener arn\n" | ||
| 140 | if !$listener_arn; | ||
| 141 | |||
| 142 | return $self->command( | ||
| 143 | 'describe-rules' => [ | ||
| 144 | '--listener-arn' => $listener_arn, | ||
| 145 | $query ? ( '--query' => $query ) : (), | ||
| 146 | ] | ||
| 147 | ); | ||
| 148 | } | ||
| 149 | |||
| 150 | ######################################################################## | ||
| 151 | sub describe_load_balancers { | ||
| 152 | ######################################################################## | ||
| 153 | my ( $self, %args ) = @_; | ||
| 154 | |||
| 155 | my ( $query, $arn ) = @args{qw(query arn)}; | ||
| 156 | $query //= q{}; | ||
| 157 | $arn //= q{}; | ||
| 158 | |||
| 159 | my $alb_cache = $self->get__alb_cache // {}; | ||
| 160 | my $cache_key = sprintf '%s|%s', $arn, $query; | ||
| 161 | |||
| 162 | return $alb_cache->{$cache_key} | ||
| 163 | if $alb_cache->{$cache_key} && !$args{no_cache}; | ||
| 164 | |||
| 165 | my $result = $self->command( | ||
| 166 | 'describe-load-balancers' => [ $arn ? ( '--load-balancer-arns' => $arn ) : (), $query ? ( '--query' => $query ) : (), ] ); | ||
| 167 | |||
| 168 | $alb_cache->{$cache_key} = $result; | ||
| 169 | $self->set__alb_cache($alb_cache); | ||
| 170 | |||
| 171 | return $result; | ||
| 172 | } | ||
| 173 | |||
| 174 | ######################################################################## | ||
| 175 | sub delete_load_balancer { | ||
| 176 | ######################################################################## | ||
| 177 | my ( $self, $alb_arn, $query ) = @_; | ||
| 178 | |||
| 179 | my $result | ||
| 180 | = $self->command( 'delete-load-balancer' => [ '--load-balancer-arn' => $alb_arn, $query ? ( '--query' => $query ) : (), ] ); | ||
| 181 | |||
| 182 | return $result; | ||
| 183 | } | ||
| 184 | |||
| 185 | ######################################################################## | ||
| 186 | sub delete_target_group { | ||
| 187 | ######################################################################## | ||
| 188 | my ( $self, $target_group_arn, $query ) = @_; | ||
| 189 | |||
| 190 | return $self->command( | ||
| 191 | 'delete-target-group' => [ '--target-group-arn' => $target_group_arn, $query ? ( '--query' => $query ) : (), ] ); | ||
| 192 | } | ||
| 193 | |||
| 194 | ######################################################################## | ||
| 195 | sub delete_rule { | ||
| 196 | ######################################################################## | ||
| 197 | my ( $self, $rule_arn, $query ) = @_; | ||
| 198 | |||
| 199 | return $self->command( 'delete-rule' => [ '--rule-arn' => $rule_arn, $query ? ( '--query' => $query ) : (), ] ); | ||
| 200 | } | ||
| 201 | |||
| 202 | ######################################################################## | ||
| 203 | sub set_security_groups { | ||
| 204 | ######################################################################## | ||
| 205 | my ( $self, $arn, @security_groups ) = @_; | ||
| 206 | |||
| 207 | return $self->command( | ||
| 208 | 'set-security-groups' => [ | ||
| 209 | '--load-balancer-arn' => $arn, | ||
| 210 | '--security-groups' => join( q{ }, @security_groups ), | ||
| 211 | ] | ||
| 212 | ); | ||
| 213 | |||
| 214 | } | ||
| 215 | |||
| 216 | ######################################################################## | ||
| 217 | sub describe_load_balancer { | ||
| 218 | ######################################################################## | ||
| 219 | my ( $self, $arn, $query, @filters ) = @_; | ||
| 220 | |||
| 221 | return $self->command( | ||
| 222 | 'describe-load-balancers' => [ | ||
| 223 | '--load-balancer-arn' => $arn, | ||
| 224 | $query ? ( '--query' => $query ) : (), | ||
| 225 | map { ( '--filters' => $_ ) } @filters | ||
| 226 | ] | ||
| 227 | ); | ||
| 228 | } | ||
| 229 | |||
| 230 | ######################################################################## | ||
| 231 | sub describe_tags { | ||
| 232 | ######################################################################## | ||
| 233 | my ( $self, $arn, $query ) = @_; | ||
| 234 | |||
| 235 | return $self->command( | ||
| 236 | 'describe-tags' => [ | ||
| 237 | '--resource-arns' => $arn, | ||
| 238 | $query ? ( '--query' => $query ) : (), | ||
| 239 | ] | ||
| 240 | ); | ||
| 241 | } | ||
| 242 | |||
| 243 | ######################################################################## | ||
| 244 | sub find_public_alb { | ||
| 245 | ######################################################################## | ||
| 246 | my ($self) = @_; | ||
| 247 | |||
| 248 | return $self->_find_alb_of_type('internet-facing'); | ||
| 249 | } | ||
| 250 | |||
| 251 | ######################################################################## | ||
| 252 | sub find_alb { | ||
| 253 | ######################################################################## | ||
| 254 | my ( $self, $type ) = @_; | ||
| 255 | |||
| 256 | return $type eq 'private' ? $self->find_private_alb : $self->find_public_alb; | ||
| 257 | } | ||
| 258 | |||
| 259 | ######################################################################## | ||
| 260 | sub find_private_alb { | ||
| 261 | ######################################################################## | ||
| 262 | my ($self) = @_; | ||
| 263 | |||
| 264 | return $self->_find_alb_of_type('internal'); | ||
| 265 | } | ||
| 266 | |||
| 267 | ######################################################################## | ||
| 268 | sub _find_alb_of_type { | ||
| 269 | ######################################################################## | ||
| 270 | my ( $self, $type, $no_error ) = @_; | ||
| 271 | |||
| 272 | croak "usage: find_alb(internal|internet-facing)\n" | ||
| 273 | if !$type || $type !~ /internal|internet\-facing/xsm; | ||
| 274 | |||
| 275 | my $ec2 = $self->get_ec2; | ||
| 276 | |||
| 277 | my $vpc_id = $ec2->get_vpc_id; | ||
| 278 | |||
| 279 | croak "no vpc_id found\n" | ||
| 280 | if !$vpc_id; | ||
| 281 | |||
| 282 | my $query = sprintf 'LoadBalancers[?Scheme == `%s` && VpcId == `%s`]', $type, $vpc_id; | ||
| 283 | |||
| 284 | my $albs = $self->describe_load_balancers( query => $query ); | ||
| 285 | |||
| 286 | $self->get_logger->debug( Dumper( [ albs => $albs ] ) ); | ||
| 287 | |||
| 288 | croak "ERROR: No ALBs of type: $type\n" | ||
| 289 | if !$no_error && !$albs || !@{$albs}; | ||
| 290 | |||
| 291 | croak sprintf "ERROR: More than one ALBs of type: $type\n%s\n", join "\n", map { $_->{LoadBalancerName} } @{$albs} | ||
| 292 | if !$no_error && @{$albs} > 1; | ||
| 293 | |||
| 294 | my $alb = $albs->[0]; | ||
| 295 | |||
| 296 | $self->set_alb($alb); | ||
| 297 | |||
| 298 | $self->get_logger->debug( sub { return Dumper( [ alb => $alb ] ) } ); | ||
| 299 | |||
| 300 | return ( $alb->{LoadBalancerArn}, $alb->{SecurityGroups}->[0] ); | ||
| 301 | } | ||
| 302 | |||
| 303 | ######################################################################## | ||
| 304 | sub get_listener_by_port { | ||
| 305 | ######################################################################## | ||
| 306 | my ( $self, $alb_arn, $port ) = @_; | ||
| 307 | |||
| 308 | my $query = sprintf 'Listeners[?Port == `%s`]', $port; | ||
| 309 | |||
| 310 | my $result = $self->command( | ||
| 311 | 'describe-listeners' => [ | ||
| 312 | '--load-balancer' => $alb_arn, | ||
| 313 | '--query', => $query, | ||
| 314 | ] | ||
| 315 | ); | ||
| 316 | |||
| 317 | return @{$result}; | ||
| 318 | } | ||
| 319 | |||
| 320 | ######################################################################## | ||
| 321 | sub create_rule { | ||
| 322 | ######################################################################## | ||
| 323 | my ( $self, %args ) = @_; | ||
| 324 | |||
| 325 | my ( $listener_arn, $priority, $conditions, $default_action ) = @args{qw(listener_arn priority conditions default_action)}; | ||
| 326 | |||
| 327 | return $self->command( | ||
| 328 | 'create-rule' => [ | ||
| 329 | '--listener-arn' => $listener_arn, | ||
| 330 | '--priority' => $priority, | ||
| 331 | '--conditions' => $conditions, | ||
| 332 | '--actions' => $default_action, | ||
| 333 | ] | ||
| 334 | ); | ||
| 335 | } | ||
| 336 | |||
| 337 | ######################################################################## | ||
| 338 | sub validate_alb { | ||
| 339 | ######################################################################## | ||
| 340 | my ( $self, %args ) = @_; | ||
| 341 | |||
| 342 | my ( $alb_arn, $scheme ) = @args{qw(arn scheme)}; | ||
| 343 | |||
| 344 | croak "usage: validate_alb(alb-arn, [ vpc-id ])\n" | ||
| 345 | if !$alb_arn; | ||
| 346 | |||
| 347 | my $query = sprintf 'LoadBalancers[?LoadBalancerArn == `%s` && Scheme == `%s`]', $alb_arn, $scheme; | ||
| 348 | |||
| 349 | my $albs = $self->describe_load_balancers( query => $query ); | ||
| 350 | |||
| 351 | croak "ERROR: unable to find ALB: %s\n%s", $alb_arn, $self->get_error | ||
| 352 | if !$albs; | ||
| 353 | |||
| 354 | return $albs; | ||
| 355 | } | ||
| 356 | |||
| 357 | ######################################################################## | ||
| 358 | sub find_alb_subnets { | ||
| 359 | ######################################################################## | ||
| 360 | my ( $self, $alb_arn ) = @_; | ||
| 361 | |||
| 362 | my $query = sprintf "LoadBalancers[?LoadBalancerArn == `%s`] | [0].AvailabilityZones[].SubnetId", $alb_arn; | ||
| 363 | |||
| 364 | my @cmd = qw(aws elbv2 describe-load-balancers); | ||
| 365 | |||
| 366 | push @cmd, | ||
| 367 | '--profile' => $self->profile, | ||
| 368 | '--query' => $query; | ||
| 369 | |||
| 370 | my $result = $self->execute(@cmd); | ||
| 371 | |||
| 372 | return $result ? decode_json($result) : $result; | ||
| 373 | } | ||
| 374 | |||
| 375 | ######################################################################## | ||
| 376 | sub target_group_exists { | ||
| 377 | ######################################################################## | ||
| 378 | my ( $self, $target_group_name ) = @_; | ||
| 379 | |||
| 380 | my $query = sprintf "TargetGroups[?TargetGroupName == `%s`]|[0]", $target_group_name; | ||
| 381 | |||
| 382 | my @cmd = qw(aws elbv2 describe-target-groups); | ||
| 383 | |||
| 384 | push @cmd, | ||
| 385 | ( | ||
| 386 | '--query' => $query, | ||
| 387 | '--profile' => $self->profile, | ||
| 388 | ); | ||
| 389 | |||
| 390 | my $result = $self->execute(@cmd); | ||
| 391 | |||
| 392 | return $result ? decode_json($result) : $result; | ||
| 393 | } | ||
| 394 | |||
| 395 | ######################################################################## | ||
| 396 | sub create_target_group { | ||
| 397 | ######################################################################## | ||
| 398 | my ( $self, %args ) = @_; | ||
| 399 | |||
| 400 | my ( $port, $protocol, $name, $vpc_id, $target_type, $health_check ) | ||
| 401 | = @args{qw(port protocol name vpc_id target_type health_check)}; | ||
| 402 | |||
| 403 | $port //= '80'; | ||
| 404 | $protocol //= 'HTTP'; | ||
| 405 | $target_type //= 'ip'; | ||
| 406 | $vpc_id //= $self->get_vpc_id; | ||
| 407 | |||
| 408 | my $tags = $self->format_tags; | ||
| 409 | |||
| 410 | my @health_check_options = _create_health_check_options($health_check); | ||
| 411 | |||
| 412 | $self->get_logger->trace( | ||
| 413 | sub { | ||
| 414 | return Dumper( | ||
| 415 | [ health_check => $health_check, | ||
| 416 | health_check_options => \@health_check_options | ||
| 417 | ] | ||
| 418 | ); | ||
| 419 | } | ||
| 420 | ); | ||
| 421 | |||
| 422 | return $self->command( | ||
| 423 | 'create-target-group' => [ | ||
| 424 | '--name' => $name, | ||
| 425 | '--protocol' => $protocol, | ||
| 426 | '--port' => $port, | ||
| 427 | '--vpc-id' => $vpc_id, | ||
| 428 | '--target-type' => $target_type, | ||
| 429 | $tags ? ( '--tags' => $tags ) : (), | ||
| 430 | '--query' => 'TargetGroups[0].TargetGroupArn', | ||
| 431 | @health_check_options, | ||
| 432 | ] | ||
| 433 | ); | ||
| 434 | } | ||
| 435 | |||
| 436 | ######################################################################## | ||
| 437 | sub _create_health_check_options { | ||
| 438 | ######################################################################## | ||
| 439 | my ($health_check) = @_; | ||
| 440 | |||
| 441 | return | ||
| 442 | if !$health_check || !keys %{$health_check}; | ||
| 443 | |||
| 444 | my @health_check_options; | ||
| 445 | |||
| 446 | if ( $health_check && $health_check->{enabled} ) { | ||
| 447 | push @health_check_options, '--health-check-enabled'; | ||
| 448 | |||
| 449 | my @args = qw(port interval_seconds timeout_seconds matcher); | ||
| 450 | my @normalized_args; | ||
| 451 | |||
| 452 | foreach ( @args, qw(healthy_threshold_count unhealthy_threshold_count) ) { | ||
| 453 | my $k = $_; | ||
| 454 | $k =~ s/_/-/gxsm; | ||
| 455 | |||
| 456 | if ( !/threshold|matcher/xsm ) { | ||
| 457 | $k = "health-check-$k"; | ||
| 458 | } | ||
| 459 | |||
| 460 | my $value = /matcher/xsm ? "HttpCode=$health_check->{$_}" : $health_check->{$_}; | ||
| 461 | push @health_check_options, "--$k" => $value; | ||
| 462 | } | ||
| 463 | } | ||
| 464 | else { | ||
| 465 | push @health_check_options, '--no-health-check-enabled'; | ||
| 466 | } | ||
| 467 | |||
| 468 | return @health_check_options; | ||
| 469 | } | ||
| 470 | |||
| 471 | ######################################################################## | ||
| 472 | sub create_listener { | ||
| 473 | ######################################################################## | ||
| 474 | my ( $self, %args ) = @_; | ||
| 475 | |||
| 476 | my ( $alb_arn, $tg_arn, $port, $default_actions, $certificate_arn, $query ) | ||
| 477 | = @args{qw(alb_arn target_group_arn port default_actions certificate_arn query)}; | ||
| 478 | |||
| 479 | $certificate_arn //= $self->get_certificate_arn; | ||
| 480 | my $protocol = $certificate_arn ? 'HTTPS' : 'HTTP'; | ||
| 481 | |||
| 482 | croak "port is a required argument\n" | ||
| 483 | if !$port; | ||
| 484 | |||
| 485 | return $self->command( | ||
| 486 | 'create-listener' => [ | ||
| 487 | '--load-balancer-arn' => $alb_arn, | ||
| 488 | '--protocol' => $protocol, | ||
| 489 | '--port' => $port, | ||
| 490 | $default_actions ? ( '--default-actions' => $default_actions ) : (), | ||
| 491 | $query ? ( '--query' => $query ) : (), | ||
| 492 | $certificate_arn ? ( '--certificates' => sprintf 'CertificateArn=%s', $certificate_arn ) : (), | ||
| 493 | ] | ||
| 494 | ); | ||
| 495 | } | ||
| 496 | |||
| 497 | ######################################################################## | ||
| 498 | sub fetch_rules_by_domain { | ||
| 499 | ######################################################################## | ||
| 500 | my ( $self, $domain, $listener_arn ) = @_; | ||
| 501 | |||
| 502 | my $query = sprintf 'Rules[?Conditions[?Field == `host-header` && contains(Values,`%s`)]]', $domain; | ||
| 503 | |||
| 504 | return $self->command( | ||
| 505 | 'describe-rules' => [ | ||
| 506 | '--listener-arn' => $listener_arn, | ||
| 507 | '--query' => $query | ||
| 508 | ] | ||
| 509 | ); | ||
| 510 | |||
| 511 | } | ||
| 512 | |||
| 513 | ######################################################################## | ||
| 514 | sub fetch_rule_arns_by_domain { | ||
| 515 | ######################################################################## | ||
| 516 | my ( $self, @args ) = @_; | ||
| 517 | |||
| 518 | my $rules = $self->fetch_rules_by_domain(@args); | ||
| 519 | |||
| 520 | return | ||
| 521 | if !$rules || !@{$rules}; | ||
| 522 | |||
| 523 | return [ map { $_->{RuleArn} } @{$rules} ]; | ||
| 524 | } | ||
| 525 | |||
| 526 | ######################################################################## | ||
| 527 | sub format_tags { | ||
| 528 | ######################################################################## | ||
| 529 | my ( $self, $tags ) = @_; | ||
| 530 | |||
| 531 | return | ||
| 532 | if !$tags || !keys %{$tags}; | ||
| 533 | |||
| 534 | return join q{ }, map { sprintf 'Key=%s,Value=%s', $_, $tags->{$_} } keys %{$tags}; | ||
| 535 | } | ||
| 536 | |||
| 537 | 1; |