| 1 | package App::FargateStack::Checker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use App::FargateStack::Builder::Utils qw(choose); | ||
| 7 | use Carp; | ||
| 8 | use Carp::Always; | ||
| 9 | use CLI::Simple::Constants qw(:booleans %LOG_LEVELS); | ||
| 10 | use Data::Dumper; | ||
| 11 | use English qw(-no_match_vars); | ||
| 12 | use Getopt::Long qw(GetOptions); | ||
| 13 | use List::Util qw(any none uniq); | ||
| 14 | use Text::ASCIITable::EasyTable; | ||
| 15 | use Sub::Util qw(subname); | ||
| 16 | |||
| 17 | use parent qw(CLI::Simple); | ||
| 18 | |||
| 19 | __PACKAGE__->use_log4perl( log_level => 'info' ); | ||
| 20 | |||
| 21 | caller or __PACKAGE__->main(); | ||
| 22 | |||
| 23 | ######################################################################## | ||
| 24 | sub check_fargate_env { | ||
| 25 | ######################################################################## | ||
| 26 | my ($self) = @_; | ||
| 27 | |||
| 28 | my @rows; | ||
| 29 | |||
| 30 | push @rows, check( $self, \&check_credentials ); | ||
| 31 | push @rows, check( $self, \&check_service_linked_roles ); | ||
| 32 | push @rows, check( $self, \&check_vpc_and_subnets ); | ||
| 33 | push @rows, check( $self, \&check_egress ); | ||
| 34 | push @rows, check( $self, \&check_ecs_permissions ); | ||
| 35 | push @rows, check( $self, \&check_elbv2_permissions ); | ||
| 36 | push @rows, check( $self, \&check_logs_permissions ); | ||
| 37 | push @rows, check( $self, \&check_ecr_access ); | ||
| 38 | push @rows, check( $self, \&check_events_permissions ); | ||
| 39 | push @rows, check( $self, \&check_passrole ); | ||
| 40 | |||
| 41 | if ( $self->get_dns ) { push @rows, check_route53($self); } | ||
| 42 | if ( $self->get_https ) { push @rows, check_acm($self); } | ||
| 43 | if ( $self->get_secrets ) { push @rows, check_secrets_hint($self); } | ||
| 44 | |||
| 45 | my $exit = render_table( $self, \@rows ); | ||
| 46 | |||
| 47 | my ( $caps, $m ) = summarize_capabilities( \@rows ); | ||
| 48 | # Temporary debug to stderr | ||
| 49 | my %sets = ( | ||
| 50 | 'HTTP services' => | ||
| 51 | [ 'Credentials', 'VPC/Subnets', 'Egress', 'ECS perms', 'ELBv2 perms', 'CloudWatch Logs perms', 'iam:PassRole' ], | ||
| 52 | 'HTTPS service' => | ||
| 53 | [ 'Credentials', 'VPC/Subnets', 'Egress', 'ECS perms', 'ELBv2 perms', 'CloudWatch Logs perms', 'iam:PassRole', 'ACM' ], | ||
| 54 | 'Scheduled tasks' => | ||
| 55 | [ 'Credentials', 'VPC/Subnets', 'Egress', 'ECS perms', 'Events perms', 'CloudWatch Logs perms', 'iam:PassRole' ], | ||
| 56 | 'One-shot tasks' => [ 'Credentials', 'VPC/Subnets', 'Egress', 'ECS perms', 'CloudWatch Logs perms', 'iam:PassRole' ], | ||
| 57 | 'Daemon services' => [ 'Credentials', 'VPC/Subnets', 'Egress', 'ECS perms', 'CloudWatch Logs perms', 'iam:PassRole' ], | ||
| 58 | ); | ||
| 59 | |||
| 60 | foreach my $cap ( sort keys %sets ) { | ||
| 61 | my @missing = grep { !exists $m->{$_} } @{ $sets{$cap} }; | ||
| 62 | if (@missing) { | ||
| 63 | print {*STDERR} sprintf "capability[%s] missing checks: %s\n", $cap, join q{, }, @missing; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | render_capabilities( $caps, $self->get_account, $self->get_region ); | ||
| 68 | |||
| 69 | return $exit; | ||
| 70 | } | ||
| 71 | |||
| 72 | ######################################################################## | ||
| 73 | sub check { | ||
| 74 | ######################################################################## | ||
| 75 | my ( $self, $sub ) = @_; | ||
| 76 | |||
| 77 | my ( undef, $name ) = split /_/xsm, subname($sub); | ||
| 78 | |||
| 79 | $self->get_logger->info( sprintf 'checking %s...', $name ); | ||
| 80 | |||
| 81 | return $sub->($self); | ||
| 82 | } | ||
| 83 | |||
| 84 | my %clients; | ||
| 85 | |||
| 86 | ######################################################################## | ||
| 87 | sub new_client { | ||
| 88 | ######################################################################## | ||
| 89 | my ( $class, @args ) = @_; | ||
| 90 | |||
| 91 | return $clients{$class} | ||
| 92 | if $clients{$class}; | ||
| 93 | |||
| 94 | my $class_path = $class; | ||
| 95 | |||
| 96 | my $options = choose { | ||
| 97 | return {@args} | ||
| 98 | if !ref $args[0]; | ||
| 99 | |||
| 100 | return { | ||
| 101 | profile => $args[0]->get_profile, | ||
| 102 | region => $args[0]->get_region, | ||
| 103 | log_level => $args[0]->get_log_level, | ||
| 104 | logger => $args[0]->get_logger, | ||
| 105 | }; | ||
| 106 | }; | ||
| 107 | |||
| 108 | $class_path =~ s/::/\//gxsm; | ||
| 109 | |||
| 110 | require "$class_path.pm"; | ||
| 111 | |||
| 112 | return $clients{$class} = $class->new($options); | ||
| 113 | } | ||
| 114 | |||
| 115 | ######################################################################## | ||
| 116 | sub try_aws { | ||
| 117 | ######################################################################## | ||
| 118 | my ($code_ref) = @_; | ||
| 119 | |||
| 120 | my $err; | ||
| 121 | |||
| 122 | my $out = eval { return ( $code_ref->() ); } or do { | ||
| 123 | $err = $EVAL_ERROR || 'unknown error'; | ||
| 124 | }; | ||
| 125 | |||
| 126 | return ( !!$out, $out, $err ); | ||
| 127 | } | ||
| 128 | |||
| 129 | ######################################################################## | ||
| 130 | sub render_table { | ||
| 131 | ######################################################################## | ||
| 132 | my ( $self, $rows ) = @_; | ||
| 133 | |||
| 134 | my $title = sprintf 'Fargate environment preflight (profile: [%s] region: [%s], Route53 profile: [%s])', | ||
| 135 | $self->get_profile, $self->get_region, $self->get_route53_profile; | ||
| 136 | |||
| 137 | my $exit_code = 0; | ||
| 138 | |||
| 139 | foreach my $r ( @{$rows} ) { | ||
| 140 | if ( $r->{Status} eq 'FAIL' ) { | ||
| 141 | $exit_code = 2; | ||
| 142 | } | ||
| 143 | if ( $r->{Status} eq 'WARN' && $exit_code == 0 ) { | ||
| 144 | $exit_code = 1; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | print {*STDOUT} easy_table( | ||
| 149 | table_options => { headingText => $title }, | ||
| 150 | columns => [qw(Check Status Detail)], | ||
| 151 | data => $rows, | ||
| 152 | ); | ||
| 153 | |||
| 154 | return $exit_code; | ||
| 155 | } | ||
| 156 | |||
| 157 | ######################################################################## | ||
| 158 | sub row_ok { return { Check => $_[0], Status => 'PASS', Detail => $_[1] // q{} }; } | ||
| 159 | sub row_warn { return { Check => $_[0], Status => 'WARN', Detail => $_[1] // q{} }; } | ||
| 160 | sub row_fail { return { Check => $_[0], Status => 'FAIL', Detail => $_[1] // q{} }; } | ||
| 161 | ######################################################################## | ||
| 162 | |||
| 163 | ######################################################################## | ||
| 164 | sub check_events_permissions { | ||
| 165 | ######################################################################## | ||
| 166 | my ($opt) = @_; | ||
| 167 | |||
| 168 | my $events = new_client( 'App::Events', $opt ); | ||
| 169 | |||
| 170 | # Read-only probe; no mutation. Either of these is fine: | ||
| 171 | # list-event-buses => broad, minimal perms | ||
| 172 | # list-rules => stricter, proves rule visibility | ||
| 173 | my ( $ok, $buses, $err ) = try_aws( sub { $events->command( 'list-event-buses' => [ '--query' => 'EventBuses[].Name' ] ) } ); | ||
| 174 | |||
| 175 | if ( !$ok ) { | ||
| 176 | return row_fail( 'Events perms', 'Cannot list event buses' ); | ||
| 177 | } | ||
| 178 | |||
| 179 | return row_ok( 'Events perms', 'Describe OK' ); | ||
| 180 | } | ||
| 181 | |||
| 182 | ######################################################################## | ||
| 183 | sub check_credentials { | ||
| 184 | ######################################################################## | ||
| 185 | my ($opt) = @_; | ||
| 186 | |||
| 187 | my $sts = new_client( 'App::STS', $opt ); | ||
| 188 | |||
| 189 | my ( $ok, $out, $err ) = try_aws( sub { $sts->get_caller_identity() } ); | ||
| 190 | |||
| 191 | if ( !$ok || !$out ) { | ||
| 192 | return row_fail( 'Credentials', 'Cannot call STS GetCallerIdentity' ); | ||
| 193 | } | ||
| 194 | |||
| 195 | my $acct = $out->{Account} || q{}; | ||
| 196 | my $arn = $out->{Arn} || q{}; | ||
| 197 | |||
| 198 | $opt->set_account($acct); | ||
| 199 | |||
| 200 | if ( !$acct ) { | ||
| 201 | return row_fail( 'Credentials', 'Missing account in STS response' ); | ||
| 202 | } | ||
| 203 | |||
| 204 | return row_ok( 'Credentials', "Account $acct, Principal $arn" ); | ||
| 205 | } | ||
| 206 | |||
| 207 | ######################################################################## | ||
| 208 | sub check_service_linked_roles { | ||
| 209 | ######################################################################## | ||
| 210 | my ($opt) = @_; | ||
| 211 | |||
| 212 | my $iam = new_client( 'App::IAM', $opt ); | ||
| 213 | |||
| 214 | my @required = ('AWSServiceRoleForECS'); # core ECS control plane | ||
| 215 | my @advisory = ('AWSServiceRoleForElasticLoadBalancing'); # only if you use ALB/NLB | ||
| 216 | |||
| 217 | # If you intend to use service autoscaling, include this: | ||
| 218 | if ( $opt->{check_scaling} ) { | ||
| 219 | push @advisory, 'AWSServiceRoleForApplicationAutoScaling_ECSService'; | ||
| 220 | } | ||
| 221 | |||
| 222 | my @missing_req; | ||
| 223 | my @missing_adv; | ||
| 224 | |||
| 225 | foreach my $r (@required) { | ||
| 226 | my ($ok) = try_aws( sub { $iam->command( 'get-role' => [ '--role-name' => $r ] ) } ); | ||
| 227 | if ( !$ok ) { | ||
| 228 | push @missing_req, $r; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | foreach my $r (@advisory) { | ||
| 233 | my ($ok) = try_aws( sub { $iam->command( 'get-role' => [ '--role-name' => $r ] ) } ); | ||
| 234 | if ( !$ok ) { | ||
| 235 | push @missing_adv, $r; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | if ( @missing_req && @missing_adv ) { | ||
| 240 | return row_warn( 'Service-linked roles', | ||
| 241 | 'Missing required: ' . join( ', ', @missing_req ) . ' ; advisory missing: ' . join( ', ', @missing_adv ) ); | ||
| 242 | } | ||
| 243 | |||
| 244 | if (@missing_req) { | ||
| 245 | return row_warn( 'Service-linked roles', 'Missing required: ' . join ', ', @missing_req ); | ||
| 246 | } | ||
| 247 | |||
| 248 | if (@missing_adv) { | ||
| 249 | return row_warn( 'Service-linked roles', 'Advisory missing: ' . join ', ', @missing_adv ); | ||
| 250 | } | ||
| 251 | |||
| 252 | return row_ok( 'Service-linked roles', 'Present or auto-creatable' ); | ||
| 253 | } | ||
| 254 | |||
| 255 | ######################################################################## | ||
| 256 | sub check_vpc_and_subnets { | ||
| 257 | ######################################################################## | ||
| 258 | my ($opt) = @_; | ||
| 259 | |||
| 260 | my $ec2 = new_client( 'App::EC2', $opt ); | ||
| 261 | |||
| 262 | my ( $ok_vpc, $vpcs, $err_vpc ) = try_aws( | ||
| 263 | sub { | ||
| 264 | $ec2->command( 'describe-vpcs' => [ '--query' => 'Vpcs[].VpcId' ] ); | ||
| 265 | } | ||
| 266 | ); | ||
| 267 | |||
| 268 | if ( !$ok_vpc || !$vpcs || !@{$vpcs} ) { | ||
| 269 | return row_fail( 'VPC/Subnets', 'No VPCs accessible in this region' ); | ||
| 270 | } | ||
| 271 | |||
| 272 | my ( $ok_sub, $subs, $err_sub ) = try_aws( | ||
| 273 | sub { | ||
| 274 | $ec2->command( 'describe-subnets' => [ '--query' => 'Subnets[].AvailabilityZone' ] ); | ||
| 275 | } | ||
| 276 | ); | ||
| 277 | |||
| 278 | if ( !$ok_sub || !$subs || !@{$subs} ) { | ||
| 279 | return row_fail( 'VPC/Subnets', 'No subnets found' ); | ||
| 280 | } | ||
| 281 | |||
| 282 | my %az = map { $_ => 1 } @{$subs}; | ||
| 283 | my $az_count = scalar keys %az; | ||
| 284 | |||
| 285 | if ( $az_count < 2 ) { | ||
| 286 | return row_fail( 'VPC/Subnets', 'Need at least two subnets across different AZs' ); | ||
| 287 | } | ||
| 288 | |||
| 289 | return row_ok( 'VPC/Subnets', sprintf 'OK (%d AZs)', $az_count ); | ||
| 290 | } | ||
| 291 | |||
| 292 | ######################################################################## | ||
| 293 | sub check_egress { | ||
| 294 | ######################################################################## | ||
| 295 | my ( $opt, $need_secrets ) = @_; | ||
| 296 | |||
| 297 | my $ec2 = new_client( 'App::EC2', $opt ); | ||
| 298 | |||
| 299 | my ( $ok_nat, $nat, $err_nat ) = try_aws( | ||
| 300 | sub { | ||
| 301 | $ec2->command( | ||
| 302 | 'describe-nat-gateways' => [ '--filter', 'Name=state,Values=available', '--query', 'NatGateways[].NatGatewayId' ] ); | ||
| 303 | } | ||
| 304 | ); | ||
| 305 | |||
| 306 | my ( $ok_ep, $eps, $err_ep ) = try_aws( | ||
| 307 | sub { | ||
| 308 | $ec2->command( 'describe-vpc-endpoints' => [ '--query', 'VpcEndpoints[].ServiceName' ] ); | ||
| 309 | } | ||
| 310 | ); | ||
| 311 | |||
| 312 | my $has_nat = $ok_nat && $nat && @{$nat}; | ||
| 313 | my $has_eps = $ok_ep && $eps; | ||
| 314 | |||
| 315 | if ( !$has_nat && !$has_eps ) { | ||
| 316 | return row_fail( 'Egress', 'Private subnets need NAT or VPC endpoints (ECR, Logs, Secrets)' ); | ||
| 317 | } | ||
| 318 | |||
| 319 | my %need = ( | ||
| 320 | 'com.amazonaws.%s.ecr.api' => 1, | ||
| 321 | 'com.amazonaws.%s.ecr.dkr' => 1, | ||
| 322 | 'com.amazonaws.%s.logs' => 1, | ||
| 323 | ); | ||
| 324 | |||
| 325 | if ($need_secrets) { | ||
| 326 | $need{'com.amazonaws.%s.secretsmanager'} = 1; | ||
| 327 | } | ||
| 328 | |||
| 329 | if ($has_eps) { | ||
| 330 | my %have = map { $_ => 1 } @{$eps}; | ||
| 331 | my @missing; | ||
| 332 | foreach my $tmpl ( keys %need ) { | ||
| 333 | my $svc = sprintf $tmpl, ( $opt->{region} || 'us-east-1' ); | ||
| 334 | if ( !$have{$svc} ) { | ||
| 335 | push @missing, $svc; | ||
| 336 | } | ||
| 337 | } | ||
| 338 | if ( @missing && !$has_nat ) { | ||
| 339 | return row_fail( 'Egress', 'Missing VPC endpoints: ' . join q{, }, @missing ); | ||
| 340 | } | ||
| 341 | if ( @missing && $has_nat ) { | ||
| 342 | return row_ok( 'Egress', 'Using NAT; missing optional endpoints: ' . join q{, }, @missing ); | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | return row_ok( 'Egress', $has_nat ? 'NAT available' : 'Required endpoints present' ); | ||
| 347 | } | ||
| 348 | |||
| 349 | ######################################################################## | ||
| 350 | sub check_ecs_permissions { | ||
| 351 | ######################################################################## | ||
| 352 | my ($opt) = @_; | ||
| 353 | |||
| 354 | my $ecs = new_client( 'App::ECS', $opt ); | ||
| 355 | |||
| 356 | my ( $ok1, $clusters, $err1 ) = try_aws( sub { $ecs->command( 'list-clusters' => [ '--query' => 'clusterArns[]' ] ) } ); | ||
| 357 | my ( $ok2, $tds, $err2 ) = try_aws( sub { $ecs->command( 'list-task-definitions' => [ '--max-results' => '1' ] ) } ); | ||
| 358 | |||
| 359 | if ( !$ok1 || !$ok2 ) { | ||
| 360 | return row_fail( 'ECS perms', 'Missing list/describe permissions' ); | ||
| 361 | } | ||
| 362 | |||
| 363 | return row_ok( 'ECS perms', 'List/Describe OK (create/update assumed during deploy)' ); | ||
| 364 | } | ||
| 365 | |||
| 366 | ######################################################################## | ||
| 367 | sub check_elbv2_permissions { | ||
| 368 | ######################################################################## | ||
| 369 | my ($opt) = @_; | ||
| 370 | |||
| 371 | my $elbv2 = new_client( 'App::ElbV2', $opt ); | ||
| 372 | |||
| 373 | my ( $ok, $lbs, $err ) | ||
| 374 | = try_aws( sub { $elbv2->command( 'describe-load-balancers' => [ '--query' => 'LoadBalancers[].LoadBalancerArn' ] ) } ); | ||
| 375 | |||
| 376 | if ( !$ok ) { | ||
| 377 | return row_fail( 'ELBv2 perms', 'Cannot describe load balancers' ); | ||
| 378 | } | ||
| 379 | |||
| 380 | return row_ok( 'ELBv2 perms', 'Describe OK' ); | ||
| 381 | } | ||
| 382 | |||
| 383 | ######################################################################## | ||
| 384 | sub check_logs_permissions { | ||
| 385 | ######################################################################## | ||
| 386 | my ($opt) = @_; | ||
| 387 | |||
| 388 | my $logs = new_client( 'App::Logs', $opt ); | ||
| 389 | |||
| 390 | my ( $ok, $lgs, $err ) = try_aws( sub { $logs->command( 'describe-log-groups' => [ '--limit' => '1' ] ) } ); | ||
| 391 | |||
| 392 | if ( !$ok ) { | ||
| 393 | return row_fail( 'CloudWatch Logs perms', 'Cannot describe log groups' ); | ||
| 394 | } | ||
| 395 | |||
| 396 | return row_ok( 'CloudWatch Logs perms', 'Describe OK' ); | ||
| 397 | } | ||
| 398 | |||
| 399 | ######################################################################## | ||
| 400 | sub check_route53 { | ||
| 401 | ######################################################################## | ||
| 402 | my ($self) = @_; | ||
| 403 | |||
| 404 | my %options = ( | ||
| 405 | profile => $self->get_route53_profile, | ||
| 406 | region => $self->get_region, | ||
| 407 | log_level => $self->get_log_level, | ||
| 408 | ); | ||
| 409 | |||
| 410 | my $r53 = new_client( 'App::Route53' => %options ); | ||
| 411 | |||
| 412 | my ( $ok, $hzs, $err ) = try_aws( sub { $r53->command( 'list-hosted-zones' => [ '--query' => 'HostedZones[].Id' ] ) } ); | ||
| 413 | |||
| 414 | if ( !$ok ) { | ||
| 415 | return row_fail( 'Route 53', 'Cannot list hosted zones' ); | ||
| 416 | } | ||
| 417 | |||
| 418 | return row_ok( 'Route 53' => 'List OK' ); | ||
| 419 | } | ||
| 420 | |||
| 421 | ######################################################################## | ||
| 422 | sub check_acm { | ||
| 423 | ######################################################################## | ||
| 424 | my ($opt) = @_; | ||
| 425 | |||
| 426 | my $acm = new_client( 'App::ACM', $opt ); | ||
| 427 | |||
| 428 | my ( $ok, $certs, $err ) | ||
| 429 | = try_aws( sub { $acm->command( 'list-certificates' => [ '--query' => 'CertificateSummaryList[].CertificateArn' ] ) } ); | ||
| 430 | |||
| 431 | if ( !$ok ) { | ||
| 432 | return row_fail( 'ACM', 'Cannot list certificates in region' ); | ||
| 433 | } | ||
| 434 | |||
| 435 | return row_ok( ACM => 'List OK' ); | ||
| 436 | } | ||
| 437 | |||
| 438 | ######################################################################## | ||
| 439 | sub check_ecr_access { | ||
| 440 | ######################################################################## | ||
| 441 | my ($opt) = @_; | ||
| 442 | |||
| 443 | my $ecr = new_client( 'App::ECR', $opt ); | ||
| 444 | |||
| 445 | my ( $ok, $repos, $err ) = try_aws( sub { $ecr->command( 'describe-repositories' => [ '--max-results' => '1' ] ) } ); | ||
| 446 | |||
| 447 | if ( !$ok ) { | ||
| 448 | return row_warn( 'ECR', 'Cannot describe repositories; image validation may fail' ); | ||
| 449 | } | ||
| 450 | |||
| 451 | return row_ok( 'ECR', 'Describe OK' ); | ||
| 452 | } | ||
| 453 | |||
| 454 | ######################################################################## | ||
| 455 | sub check_secrets_hint { | ||
| 456 | ######################################################################## | ||
| 457 | my ($opt) = @_; | ||
| 458 | |||
| 459 | # The deployer does not need GetSecretValue; tasks use task role. | ||
| 460 | # We only check control-plane reachability here. | ||
| 461 | my $ec2 = new_client( 'App::EC2', $opt ); | ||
| 462 | |||
| 463 | my ( $ok_ep, $eps, $err_ep ) = try_aws( | ||
| 464 | sub { | ||
| 465 | $ec2->command( 'describe-vpc-endpoints' => [ '--query', 'VpcEndpoints[].ServiceName' ] ); | ||
| 466 | } | ||
| 467 | ); | ||
| 468 | |||
| 469 | if ( !$ok_ep ) { | ||
| 470 | return row_warn( 'Secrets Manager', 'Could not confirm VPC endpoints for Secrets Manager' ); | ||
| 471 | } | ||
| 472 | |||
| 473 | my %have = map { $_ => 1 } @{ $eps || [] }; | ||
| 474 | my $svc = sprintf 'com.amazonaws.%s.secretsmanager', ( $opt->{region} || 'us-east-1' ); | ||
| 475 | |||
| 476 | if ( !$have{$svc} ) { | ||
| 477 | return row_warn( 'Secrets Manager', 'Missing VPC endpoint for Secrets Manager or rely on NAT' ); | ||
| 478 | } | ||
| 479 | |||
| 480 | return row_ok( 'Secrets Manager', 'Endpoint present' ); | ||
| 481 | } | ||
| 482 | ######################################################################## | ||
| 483 | sub check_passrole { | ||
| 484 | ######################################################################## | ||
| 485 | my ($opt) = @_; | ||
| 486 | my $role_names = $opt->get_role_names; | ||
| 487 | |||
| 488 | # role_names: arrayref of the exact role names your framework will create, | ||
| 489 | # e.g., [ 'FargateStack/my-svc/TaskExecutionRole', 'FargateStack/my-svc/TaskRole', | ||
| 490 | # 'FargateStack/my-svc/EventsInvokeRole' ] | ||
| 491 | |||
| 492 | my $sts = new_client( 'App::STS', $opt ); | ||
| 493 | my $iam = new_client( 'App::IAM', $opt ); | ||
| 494 | |||
| 495 | my $id = $sts->get_caller_identity(); | ||
| 496 | my $acct = $id->{Account} || q{}; | ||
| 497 | my $caller_arn = $id->{Arn} || q{}; | ||
| 498 | |||
| 499 | if ( !$acct || !$caller_arn ) { | ||
| 500 | return row_fail( 'iam:PassRole', 'Cannot resolve caller identity' ); | ||
| 501 | } | ||
| 502 | |||
| 503 | my $policy_source_arn = _principal_to_policy_source_arn( $caller_arn, $acct ); | ||
| 504 | if ( !$policy_source_arn ) { | ||
| 505 | return row_fail( 'iam:PassRole', 'Cannot derive policy-source ARN for simulation' ); | ||
| 506 | } | ||
| 507 | |||
| 508 | my $role_arns = _role_arns_from_names( $acct, $role_names || [] ); | ||
| 509 | if ( !$role_arns || !@{$role_arns} ) { | ||
| 510 | return row_warn( 'iam:PassRole', 'No target roles specified; skipping simulation' ); | ||
| 511 | } | ||
| 512 | |||
| 513 | my @services = qw(ecs-tasks.amazonaws.com events.amazonaws.com); | ||
| 514 | |||
| 515 | my %decisions_by_service; | ||
| 516 | |||
| 517 | foreach my $svc (@services) { | ||
| 518 | my $result = eval { _simulate_passrole_once( $iam, $policy_source_arn, $role_arns, $svc ) }; | ||
| 519 | if ( $EVAL_ERROR || !$result ) { | ||
| 520 | # If org blocks simulation, warn rather than fail | ||
| 521 | return row_warn( 'iam:PassRole', 'Simulation not permitted; verify PassRole manually' ); | ||
| 522 | } | ||
| 523 | |||
| 524 | # result is an arrayref of EvalDecision strings: allowed | explicitDeny | implicitDeny | ||
| 525 | my @dec = @{ $result || [] }; | ||
| 526 | $decisions_by_service{$svc} = \@dec; | ||
| 527 | } | ||
| 528 | |||
| 529 | # Aggregate decisions: any explicitDeny -> FAIL | ||
| 530 | # otherwise any implicitDeny -> WARN | ||
| 531 | # otherwise all allowed -> PASS | ||
| 532 | my $detail = q{}; | ||
| 533 | my $status = 'PASS'; | ||
| 534 | |||
| 535 | foreach my $svc (@services) { | ||
| 536 | my $dec = $decisions_by_service{$svc} || []; | ||
| 537 | my $svc_summary = sprintf '%s: %s', $svc, ( join q{,}, @{$dec} ); | ||
| 538 | |||
| 539 | if ( grep { $_ eq 'explicitDeny' } @{$dec} ) { | ||
| 540 | $status = 'FAIL'; | ||
| 541 | } | ||
| 542 | elsif ( $status ne 'FAIL' && grep { $_ eq 'implicitDeny' } @{$dec} ) { | ||
| 543 | $status = 'WARN'; | ||
| 544 | } | ||
| 545 | |||
| 546 | $detail .= $svc_summary . q{ }; | ||
| 547 | } | ||
| 548 | |||
| 549 | $detail =~ s/\s+\z//; | ||
| 550 | |||
| 551 | if ( $status eq 'FAIL' ) { return row_fail( 'iam:PassRole', $detail ); } | ||
| 552 | if ( $status eq 'WARN' ) { return row_warn( 'iam:PassRole', $detail ); } | ||
| 553 | return row_ok( 'iam:PassRole', $detail || 'allowed' ); | ||
| 554 | } | ||
| 555 | |||
| 556 | ######################################################################## | ||
| 557 | sub _principal_to_policy_source_arn { | ||
| 558 | ######################################################################## | ||
| 559 | my ( $sts_arn, $account_id ) = @_; | ||
| 560 | |||
| 561 | # Examples: | ||
| 562 | # arn:aws:sts::123456789012:assumed-role/DeployerRole/SESSION -> arn:aws:iam::123456789012:role/DeployerRole | ||
| 563 | # arn:aws:iam::123456789012:user/someuser -> arn:aws:iam::123456789012:user/someuser | ||
| 564 | |||
| 565 | return q{} if !$sts_arn || !$account_id; | ||
| 566 | |||
| 567 | if ( $sts_arn =~ m{\A arn:aws:sts::\Q$account_id\E:assumed-role/([^/]+)/}xsm ) { | ||
| 568 | my $role = $1; | ||
| 569 | return sprintf 'arn:aws:iam::%s:role/%s', $account_id, $role; | ||
| 570 | } | ||
| 571 | |||
| 572 | if ( $sts_arn =~ m{\A arn:aws:iam::\Q$account_id\E:(user|role)/}xsm ) { | ||
| 573 | return $sts_arn; | ||
| 574 | } | ||
| 575 | |||
| 576 | # Fallback: try to coerce to iam::role if it looks like an sts assumed role | ||
| 577 | return $sts_arn; | ||
| 578 | } | ||
| 579 | |||
| 580 | ######################################################################## | ||
| 581 | sub _role_arns_from_names { | ||
| 582 | ######################################################################## | ||
| 583 | my ( $account_id, $role_names ) = @_; | ||
| 584 | |||
| 585 | $role_names ||= []; | ||
| 586 | my @arns; | ||
| 587 | |||
| 588 | foreach my $name ( @{$role_names} ) { | ||
| 589 | next if !$name; | ||
| 590 | push @arns, sprintf 'arn:aws:iam::%s:role/%s', $account_id, $name; | ||
| 591 | } | ||
| 592 | |||
| 593 | return \@arns; | ||
| 594 | } | ||
| 595 | |||
| 596 | ######################################################################## | ||
| 597 | sub _simulate_passrole_once { | ||
| 598 | ######################################################################## | ||
| 599 | my ( $iam, $policy_source_arn, $resource_arns, $passed_to_service ) = @_; | ||
| 600 | |||
| 601 | # Build CLI JSON for --context-entries | ||
| 602 | my $context_json = sprintf | ||
| 603 | q|[{"ContextKeyName":"iam:PassedToService","ContextKeyValues":["%s"],"ContextKeyType":"string"}]|, | ||
| 604 | $passed_to_service; | ||
| 605 | |||
| 606 | # Ask for just the decision list | ||
| 607 | return $iam->command( | ||
| 608 | 'simulate-principal-policy' => [ | ||
| 609 | '--policy-source-arn' => $policy_source_arn, | ||
| 610 | '--action-names' => 'iam:PassRole', | ||
| 611 | '--resource-arns' => @{$resource_arns}, | ||
| 612 | '--context-entries' => $context_json, | ||
| 613 | '--query' => 'EvaluationResults[].EvalDecision', | ||
| 614 | '--output' => 'json', | ||
| 615 | ] | ||
| 616 | ); | ||
| 617 | } | ||
| 618 | |||
| 619 | ######################################################################## | ||
| 620 | sub _gate { | ||
| 621 | ######################################################################## | ||
| 622 | my ( $need, $caps ) = @_; | ||
| 623 | |||
| 624 | my $result = 'YES'; | ||
| 625 | |||
| 626 | foreach ( @{$need} ) { | ||
| 627 | next if $caps->{$_} =~ /PASS/xsm; | ||
| 628 | return 'NO' if $caps->{$_} eq 'FAIL'; | ||
| 629 | |||
| 630 | $result = 'MAYBE'; | ||
| 631 | } | ||
| 632 | |||
| 633 | return $result; | ||
| 634 | } | ||
| 635 | |||
| 636 | ######################################################################## | ||
| 637 | sub summarize_capabilities { | ||
| 638 | ######################################################################## | ||
| 639 | my ($rows) = @_; | ||
| 640 | |||
| 641 | my $m = { map { ( $_->{Check} => $_->{Status} ) } @{$rows} }; | ||
| 642 | |||
| 643 | my @default_needs = ( 'Credentials', 'VPC/Subnets', 'Egress', 'ECS perms', 'CloudWatch Logs perms', 'iam:PassRole' ); | ||
| 644 | my @need_http = ( @default_needs, 'ELBv2 perms' ); | ||
| 645 | my @need_https = ( @need_http, 'ACM' ); | ||
| 646 | my @need_sched = ( @default_needs, 'Events perms' ); | ||
| 647 | my @need_oneshot = @default_needs; | ||
| 648 | my @need_daemon = @default_needs; | ||
| 649 | |||
| 650 | my %caps = ( | ||
| 651 | 'HTTP services' => _gate( \@need_http, $m ), | ||
| 652 | 'HTTPS service' => _gate( \@need_https, $m ), | ||
| 653 | 'Scheduled tasks' => _gate( \@need_sched, $m ), | ||
| 654 | 'One-shot tasks' => _gate( \@need_oneshot, $m ), | ||
| 655 | 'Daemon services' => _gate( \@need_daemon, $m ), | ||
| 656 | ); | ||
| 657 | |||
| 658 | return ( \%caps, $m ); | ||
| 659 | } | ||
| 660 | |||
| 661 | ######################################################################## | ||
| 662 | sub render_capabilities { | ||
| 663 | ######################################################################## | ||
| 664 | my ( $caps, $account, $region ) = @_; | ||
| 665 | |||
| 666 | my $title = sprintf "Capabilities Summary\nAccount [%s] Region: [%s]", $account, $region; | ||
| 667 | |||
| 668 | my @data; | ||
| 669 | |||
| 670 | foreach my $k ( sort keys %{$caps} ) { | ||
| 671 | push @data, { 'Service Type' => $k, 'Available' => $caps->{$k} }; | ||
| 672 | } | ||
| 673 | |||
| 674 | print easy_table( | ||
| 675 | table_options => { headingText => $title }, | ||
| 676 | columns => [ 'Service Type', 'Available' ], | ||
| 677 | data => \@data | ||
| 678 | |||
| 679 | ); | ||
| 680 | |||
| 681 | return; | ||
| 682 | } | ||
| 683 | |||
| 684 | ######################################################################## | ||
| 685 | sub init { | ||
| 686 | ######################################################################## | ||
| 687 | my ($self) = @_; | ||
| 688 | |||
| 689 | $self->set_route53_profile($self->get_dns_profile // $self->get_profile); | ||
| 690 | |||
| 691 | return; | ||
| 692 | } | ||
| 693 | |||
| 694 | ######################################################################## | ||
| 695 | sub main { | ||
| 696 | ######################################################################## | ||
| 697 | |||
| 698 | my %default_options = ( | ||
| 699 | profile => $ENV{AWS_PROFILE} || q{default}, | ||
| 700 | region => $ENV{AWS_REGION} || q{us-east-1}, | ||
| 701 | log_level => 'info', | ||
| 702 | dns => $TRUE, | ||
| 703 | secrets => $TRUE, | ||
| 704 | https => $TRUE, | ||
| 705 | ); | ||
| 706 | |||
| 707 | my @option_specs = qw( | ||
| 708 | profile=s | ||
| 709 | region=s | ||
| 710 | https! | ||
| 711 | help|h | ||
| 712 | dns! | ||
| 713 | secrets! | ||
| 714 | dns-profile=s | ||
| 715 | log_level=s | ||
| 716 | ); | ||
| 717 | |||
| 718 | my %commands = ( default => \&check_fargate_env ); | ||
| 719 | |||
| 720 | my $cli = App::FargateStack::Checker->new( | ||
| 721 | commands => \%commands, | ||
| 722 | default_options => \%default_options, | ||
| 723 | option_specs => \@option_specs, | ||
| 724 | extra_options => [qw(account global_options role_names route53_profile)], | ||
| 725 | ); | ||
| 726 | |||
| 727 | my @expected_roles = ( | ||
| 728 | # Execution role and task role your builder will create | ||
| 729 | 'FargateStack/my-svc/TaskExecutionRole', | ||
| 730 | 'FargateStack/my-svc/TaskRole', | ||
| 731 | 'FargateStack/my-svc/EventsInvokeRole', | ||
| 732 | ); | ||
| 733 | |||
| 734 | $cli->set_role_names( \@expected_roles ); | ||
| 735 | |||
| 736 | $cli->set_global_options( | ||
| 737 | { profile => $cli->get_profile, | ||
| 738 | region => $cli->get_region | ||
| 739 | } | ||
| 740 | ); | ||
| 741 | |||
| 742 | return $cli->run(); | ||
| 743 | } | ||
| 744 | |||
| 745 | 1; | ||
| 746 | |||
| 747 | __END__ |