| 1 | package App::FargateStack::CloudTrail; | ||||||||||||||||||||||||||||||||||||||||
| 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 App::FargateStack::Builder::Utils qw(normalize_time_range dmp jmespath_mapping normalize_timestamp choose); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use CLI::Simple::Constants qw(:booleans); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Date::Parse qw(str2time); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use English qw(no_match_vars); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use List::Util qw(any none); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Text::ASCIITable::EasyTable; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Text::Wrap; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | $Text::Wrap::columns = 72; | |||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||
| 21 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub cmd_cloudtrail_events { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
cmd_cloudtrail_events: not called CC=17 SCAR=57.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||
| 23 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | my ( $config, $cluster ) = $self->common_args(qw(config cluster)); | |||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my $cluster_name = $cluster->{name}; | |||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my ( undef, $task_name, $start_time, $end_time ) = $self->get_args; | |||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||
| 31 | die sprintf "usage: %s show cloudtrail-events task-name start-time [end-time]\n", $ENV{SCRIPT_NAME} | ||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if !$task_name || !$start_time; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$task_name and $start_time
MC/DC0%0/2
$task_name and $start_time
$task_name
$start_time
Truth table0%0/3
A and B
A= $task_nameB= $start_time
| |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | die "ERROR: show cloudtrail-events is for stacks with at least 1 scheduled event\n" | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 35 | if !$self->has_events; | ||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||
| 37 | # may support multiple groups and/or multiple roles in the future | ||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $group_names //= [ 'schedule:' . $task_name ]; | ||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
my $group_names //= ['schedule:' . $task_name]
MC/DC0%0/1
my $group_names //= ['schedule:' . $task_name]
my $group_names
Truth table0%0/2
A
A= my $group_names
| |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $group_names = ref($group_names) ? $group_names : [$group_names]; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my $role_names //= [ $config->{events_role}->{name} ]; | ||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
my $role_names //= [$config->{"events_role"}{"name"}]
MC/DC0%0/1
my $role_names //= [$config->{"events_role"}{"name"}]
my $role_names
Truth table0%0/2
A
A= my $role_names
| |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $role_names = ref($role_names) ? $role_names : [$role_names]; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my $ct = $self->fetch_cloudtrail; | |||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ($start_time) = normalize_time_range($start_time); | |||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | die sprintf "ERROR: invalid start-time: %s\n", | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 49 | if !$start_time; | ||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||
| 51 | # get CloudTrail events, filter on role | ||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $events = $ct->lookup_events( | |||||||||||||||||||||||||||||||||||||||
| 53 | start_time => int $start_time / 1000, | ||||||||||||||||||||||||||||||||||||||||
| 54 | attributes => { EventName => 'RunTask' } | ||||||||||||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $ct->check_result( message => 'ERROR: could not lookup events in CloudTrail' ); | |||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||
| 59 | die sprintf "ERROR: no events for: [%s] found, start-time: %s\n", $task_name, scalar localtime $start_time | ||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if !$events || !@{$events}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$events and @{$events;}
MC/DC0%0/2
$events and @{$events;}
$events
@{$events;}
Truth table0%0/3
A and B
A= $eventsB= @{$events;}
| |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my %status; | |||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | foreach my $e ( @{$events} ) { | |||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $e = $e->{CloudTrailEvent}; | |||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | next if !$e; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $userName = $e->{userIdentity}->{sessionContext}->{sessionIssuer}->{userName}; | |||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | next if !$userName || none { $_ eq $userName } @{$role_names}; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
Condition0%0/3
not $userName or &none(sub {
$_ eq $userName;
}
, @{$role_names;})
MC/DC0%0/2
not $userName or &none(sub {
$_ eq $userName;
}
, @{$role_names;})
not $userName
&none(sub {
$_ eq $userName;
}
, @{$role_names;})
Truth table0%0/3
not(A) or B
A= not $userNameB= &none(sub {
$_ eq $userName;
}
, @{$role_names;})
| |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||
| 71 | $status{ $e->{eventID} } = { | ||||||||||||||||||||||||||||||||||||||||
| 72 | eventTime => normalize_timestamp( $e->{eventTime} ), | ||||||||||||||||||||||||||||||||||||||||
| 73 | errorMessage => $e->{errorMessage}, | ||||||||||||||||||||||||||||||||||||||||
| 74 | errorCode => $e->{errorCode}, | ||||||||||||||||||||||||||||||||||||||||
| 75 | taskArn => $e->{responseElements}->{tasks}->[0]->{taskArn} | ||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | }; | |||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | die sprintf "ERROR: no events match this role: [%s]\n", join q{,}, @{$role_names} | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 80 | if !keys %status; | ||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my @arns = map { $status{$_}->{taskArn} // () } keys %status; | ||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$status{$_}{"taskArn"} // ()
MC/DC0%0/2
$status{$_}{"taskArn"} // ()
$status{$_}{"taskArn"}
()
Truth table0%0/3
A // B
A= $status{$_}{"taskArn"}B= ()
| |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $self->_decorate_status( \@arns, \%status, $cluster_name ); | |||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my $data = $self->_build_event_data( \%status ); | |||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my $output = choose { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return JSON->new->pretty->encode($data) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 90 | if $self->get_output eq 'json'; | ||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||
| 92 | return easy_table( | ||||||||||||||||||||||||||||||||||||||||
| 93 | columns => [qw(EventTime ErrorCode ErrorMessage StartTime StopTime LastStatus Task)], | ||||||||||||||||||||||||||||||||||||||||
| 94 | data => $data, | ||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | table_options => { headingText => sprintf "CloudTrail/ECS Events\nRoles: %s", join q{,}, @{$role_names} } | |||||||||||||||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | }; | |||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | print {*STDOUT} $output; | |||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | return; | |||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||
| 104 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub _build_event_data { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_event_data: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||
| 106 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my ( $self, $status ) = @_; | |||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my @eventIds = sort { $status->{$a}->{eventTime} <=> $status->{$b}->{eventTime} } keys %{$status}; | |||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my @data; | |||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | foreach my $e (@eventIds) { | |||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $row = $status->{$e}; | |||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | next if !$row->{errorMessage} && !$row->{taskDefinitionArn}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$row->{"errorMessage"} or $row->{"taskDefinitionArn"}
MC/DC0%0/2
$row->{"errorMessage"} or $row->{"taskDefinitionArn"}
$row->{"errorMessage"}
$row->{"taskDefinitionArn"}
Truth table0%0/3
A or B
A= $row->{"errorMessage"}B= $row->{"taskDefinitionArn"}
| |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||
| 117 | push @data, | ||||||||||||||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||||||||||||||
| 119 | EventTime => scalar( localtime( $status->{$e}->{eventTime} ) ), | ||||||||||||||||||||||||||||||||||||||||
| 120 | StartTime => $row->{startedAt} // q{}, | ||||||||||||||||||||||||||||||||||||||||
| 121 | StopTime => $row->{stoppedAt} // q{}, | ||||||||||||||||||||||||||||||||||||||||
| 122 | LastStatus => $row->{lastStatus} // q{}, | ||||||||||||||||||||||||||||||||||||||||
| 123 | Task => $row->{taskDefinitionArn} ? basename( $row->{taskDefinitionArn} ) : q{}, | ||||||||||||||||||||||||||||||||||||||||
| 124 | ErrorCode => $row->{errorCode}, | ||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | ErrorMessage => wrap( q{}, q{}, $row->{errorMessage} ), | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$row->{"startedAt"} // ""
$row->{"stoppedAt"} // ""
$row->{"lastStatus"} // ""
MC/DC0%0/1
$row->{"startedAt"} // ""
$row->{"startedAt"}
MC/DC0%0/1
$row->{"stoppedAt"} // ""
$row->{"stoppedAt"}
MC/DC0%0/1
$row->{"lastStatus"} // ""
$row->{"lastStatus"}
Truth table0%0/2
A
A= $row->{"startedAt"}
Truth table0%0/2
A
A= $row->{"stoppedAt"}
Truth table0%0/2
A
A= $row->{"lastStatus"}
| |||||||||||||||||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | return \@data; | |||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||
| 131 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub _decorate_status { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decorate_status: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||
| 133 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | my ( $self, $arns, $status, $cluster_name ) = @_; | |||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $query = jmespath_mapping( 'tasks[]' => [qw(taskArn startedAt stoppedAt lastStatus taskDefinitionArn)] ); | |||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my $ecs = $self->fetch_ecs; | |||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | my @arn_list = @{$arns}; | |||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my @task_list; | |||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||
| 144 | # AWS describe-tasks limits tasks to 100 | ||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | while ( my @arns = splice @arn_list, 0, 99 ) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | push @task_list, @{ $ecs->describe_tasks( $cluster_name, \@arns, $query ) }; | |||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my %tasks = map { ( $_->{taskArn} => $_ ) } @task_list; | |||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | foreach my $e ( keys %{$status} ) { | |||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my $arn = $status->{$e}->{taskArn}; | |||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | next if !$arn; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | $status->{$e}->{stoppedAt} = $tasks{$arn}->{stoppedAt}; | |||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $status->{$e}->{startedAt} = $tasks{$arn}->{startedAt}; | |||||||||||||||||||||||||||||||||||||||
| 157 | 0 | $status->{$e}->{lastStatus} = $tasks{$arn}->{lastStatus}; | |||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $status->{$e}->{taskDefinitionArn} = $tasks{$arn}->{taskDefinitionArn}; | |||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | return $status; | |||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||