| 1 | package Mail::TLSRPT::Report; | ||||||||||||
| 2 | # ABSTRACT: TLSRPT report object | ||||||||||||
| 3 | 13 | our $VERSION = '2.20261112'; # VERSION | |||||||||||
| 4 | 13 | ▶ | use 5.20.0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 13 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 13 | ▶ | use Mail::TLSRPT::Pragmas; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 13 | ▶ | use Mail::TLSRPT::Policy; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 13 | ▶ | use Mail::TLSRPT::Failure; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 13 | ▶ | use DateTime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 13 | ▶ | use Date::Parse qw{ str2time }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 13 | ▶ | use IO::Uncompress::Gunzip; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 13 | ▶ | use Text::CSV; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 13 | ▶ | use constant DEFAULT_MAX_SIZE => 10 * 1024 * 1024; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 13 | has organization_name => (is => 'rw', isa => Str, required => 1); | |||||||||||
| 16 | 29 | ▶ | has start_datetime => (is => 'rw', isa => class_type('DateTime'), required => 1, coerce => sub{&_coerce_datetime}); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 29 | ▶ | has end_datetime => (is => 'rw', isa => class_type('DateTime'), required => 1, coerce => sub{&_coerce_datetime}); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 13 | has contact_info => (is => 'rw', isa => Str, required => 0); | |||||||||||
| 19 | 13 | has report_id => (is => 'rw', isa => Str, required => 1); | |||||||||||
| 20 | 0 | ▶ | has policies => (is => 'rw', isa => ArrayRef, required => 0, lazy => 1, builder => sub{return []} ); | ||||||||||
|
Subroutine
_build_policies: not called CC=1 SCAR=0.4
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||
| 21 | |||||||||||||
| 22 | 58 | ▶ | sub _coerce_datetime { | ||||||||||
|
Subroutine
_coerce_datetime: called CC=2 SCAR=6.9
| |||||||||||||
| 23 | 58 | my $time = shift; | |||||||||||
| 24 | 58 | ▶ | $time = DateTime->from_epoch( epoch=>$time ) unless ref $time eq 'DateTime'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | 58 | return $time; | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | |||||||||||||
| 29 | 18 | ▶ | sub new_from_json($class,$json,$args={}) { | ||||||||||
|
Subroutine
new_from_json: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 30 | 18 | ▶ | if ( $json =~ /^\037\213/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 5 | return $class->new_from_json_gz($json,$args); | |||||||||||
| 32 | } | ||||||||||||
| 33 | 13 | return $class->_new_from_json_string($json,$args); | |||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | |||||||||||||
| 37 | 16 | ▶ | sub new_from_json_gz($class,$compressed_json,$args={}) { | ||||||||||
|
Subroutine
new_from_json_gz: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||
| 38 | # JSON that isn't gzipped is read as it is, as when gunzip passed it through | ||||||||||||
| 39 | 16 | ▶ | return $class->_new_from_json_string($compressed_json,$args) unless $compressed_json =~ /^\037\213/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 15 | ▶ | my $max_size = $args->{max_size} // DEFAULT_MAX_SIZE; | ||||||||||
|
Condition100%2/2
$args->{"max_size"} // 10485760
MC/DC100%1/1
$args->{"max_size"} // 10485760
$args->{"max_size"}
Truth table100%2/2
A
A= $args->{"max_size"}
| |||||||||||||
| 41 | # Not transparent, so whatever follows the last member is left unread | ||||||||||||
| 42 | # rather than added to the JSON | ||||||||||||
| 43 | 15 | ▶ | my $gunzip = IO::Uncompress::Gunzip->new(\$compressed_json, Strict => 1, Transparent => 0) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | or croak "Could not gunzip report: $IO::Uncompress::Gunzip::GunzipError"; | ||||||||||||
| 45 | 15 | my $json = ''; | |||||||||||
| 46 | 15 | while (1) { | |||||||||||
| 47 | 189 | my $status = $gunzip->read(my $buffer, 65536); | |||||||||||
| 48 | 189 | ▶ | croak "Could not gunzip report: $IO::Uncompress::Gunzip::GunzipError" if $status < 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 187 | ▶ | if ( $status == 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 12 | my $next = $gunzip->nextStream; | |||||||||||
| 51 | 12 | ▶ | croak "Could not gunzip report: $IO::Uncompress::Gunzip::GunzipError" if $next < 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 12 | ▶ | last if $next == 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 2 | next; | |||||||||||
| 54 | } | ||||||||||||
| 55 | 175 | $json .= $buffer; | |||||||||||
| 56 | 175 | ▶ | croak "Report is larger than $max_size bytes when gunzipped" if length $json > $max_size; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | } | ||||||||||||
| 58 | # A gzip inside a gzip is not a report: say so, rather than fail as bad JSON | ||||||||||||
| 59 | 10 | ▶ | croak 'Report is gzipped twice' if $json =~ /^\037\213/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 9 | return $class->_new_from_json_string($json,$args); | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 23 | ▶ | sub _new_from_json_string($class,$json,$args) { | ||||||||||
|
Subroutine
_new_from_json_string: called CC=3 SCAR=11.0
| |||||||||||||
| 64 | 23 | ▶ | my $max_size = $args->{max_size} // DEFAULT_MAX_SIZE; | ||||||||||
|
Condition100%2/2
$args->{"max_size"} // 10485760
MC/DC100%1/1
$args->{"max_size"} // 10485760
$args->{"max_size"}
Truth table100%2/2
A
A= $args->{"max_size"}
| |||||||||||||
| 65 | 23 | ▶ | croak "Report is larger than $max_size bytes" if length $json > $max_size; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 22 | my $j = JSON->new->utf8; | |||||||||||
| 67 | 22 | my $data = $j->decode($json); | |||||||||||
| 68 | 22 | return $class->new_from_data($data); | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | |||||||||||||
| 72 | 49 | ▶ | sub new_from_data($class,$data) { | ||||||||||
|
Subroutine
new_from_data: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||
| 73 | 49 | ▶ | croak 'Report is not an object' unless ref $data eq 'HASH'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 48 | ▶ | my $date_range = $data->{'date-range'} // {}; | ||||||||||
|
Condition50%1/2
$data->{"date-range"} // {}
MC/DC0%0/1
$data->{"date-range"} // {}
$data->{"date-range"}
Truth table50%1/2
A
A= $data->{"date-range"}
| |||||||||||||
| 75 | 48 | ▶ | croak 'date-range: not an object' unless ref $date_range eq 'HASH'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 47 | ▶ | my $policies = $data->{policies} // []; | ||||||||||
|
Condition50%1/2
$data->{"policies"} // []
MC/DC0%0/1
$data->{"policies"} // []
$data->{"policies"}
Truth table50%1/2
A
A= $data->{"policies"}
| |||||||||||||
| 77 | 47 | ▶ | croak 'policies: not a list' unless ref $policies eq 'ARRAY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 46 | my @policies; | |||||||||||
| 79 | 46 | foreach my $i ( keys $policies->@* ) { | |||||||||||
| 80 | 40 | my $policy = eval { Mail::TLSRPT::Policy->new_from_data($policies->[$i]) }; | |||||||||||
| 81 | 40 | ▶ | die "policies[$i]: $@" unless $policy; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 33 | push @policies, $policy; | |||||||||||
| 83 | } | ||||||||||||
| 84 | my $self = $class->new( | ||||||||||||
| 85 | organization_name => $data->{'organization-name'}, | ||||||||||||
| 86 | start_datetime => DateTime->from_epoch(epoch => _parse_date($date_range->{'start-datetime'},'date-range.start-datetime')), | ||||||||||||
| 87 | end_datetime => DateTime->from_epoch(epoch => _parse_date($date_range->{'end-datetime'},'date-range.end-datetime')), | ||||||||||||
| 88 | $data->{'contact-info'} ? ( contact_info => $data->{'contact-info'} ) : (), | ||||||||||||
| 89 | 39 | ▶ | report_id => $data->{'report-id'}, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | policies => \@policies, | ||||||||||||
| 91 | ); | ||||||||||||
| 92 | 27 | return $self; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | # RFC 3339 section 5.6 date-time, with the lowercase t and z and the space | ||||||||||||
| 96 | # for T that its notes allow. str2time alone would take "12:00" as today. | ||||||||||||
| 97 | 13 | my $rfc3339_date_time = qr{\A\d{4}-\d\d-\d\d[Tt ]\d\d:\d\d:\d\d(?:\.\d+)?(?:[Zz]|[+-](?:[01]\d|2[0-3]):[0-5]\d)\z}a; | |||||||||||
| 98 | |||||||||||||
| 99 | 67 | ▶ | sub _parse_date($date,$name) { | ||||||||||
|
Subroutine
_parse_date: called CC=5 SCAR=16.1
| |||||||||||||
| 100 | 67 | ▶ | croak "$name: missing" unless defined $date; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | 66 | ▶ | croak "$name: not a string" if ref $date; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 65 | ▶ | my $epoch = $date =~ $rfc3339_date_time ? str2time($date) : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 65 | ▶ | croak "$name: could not parse date \"$date\"" unless defined $epoch; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 55 | return $epoch; | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | |||||||||||||
| 108 | 5 | ▶ | sub as_json($self) { | ||||||||||
|
Subroutine
as_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 109 | 5 | my $j = JSON->new->utf8; | |||||||||||
| 110 | 5 | $j->canonical; | |||||||||||
| 111 | 5 | return $j->encode( $self->as_struct ); | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | |||||||||||||
| 115 | 8 | ▶ | sub as_struct($self) { | ||||||||||
|
Subroutine
as_struct: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 116 | 8 | my @policies = map {$_->as_struct} $self->policies->@*; | |||||||||||
| 117 | return { | ||||||||||||
| 118 | 8 | ▶ | 'organization-name' => $self->organization_name, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | 'date-range' => { | ||||||||||||
| 120 | 'start-datetime' => $self->start_datetime->datetime.'Z', | ||||||||||||
| 121 | 'end-datetime' => $self->end_datetime->datetime.'Z', | ||||||||||||
| 122 | }, | ||||||||||||
| 123 | $self->contact_info ? ( 'contact-info' => $self->contact_info ) : (), | ||||||||||||
| 124 | 'report-id' => $self->report_id, | ||||||||||||
| 125 | policies => \@policies, | ||||||||||||
| 126 | }; | ||||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | |||||||||||||
| 130 | 5 | ▶ | sub as_string($self) { | ||||||||||
|
Subroutine
as_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 131 | return join( "\n", | ||||||||||||
| 132 | 'Report-ID: <'.$self->report_id.'>', | ||||||||||||
| 133 | 'From: "'.$self->organization_name.'"'.( $self->contact_info ? ' <'.$self->contact_info.'>' : '' ), | ||||||||||||
| 134 | 'Dates: '.$self->start_datetime->datetime.'Z to '.$self->end_datetime->datetime.'Z', | ||||||||||||
| 135 | 5 | ▶ | map { $_->as_string } $self->policies->@*, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | ); | ||||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 2 | ▶ | sub _register_prometheus($self,$prometheus) { | ||||||||||
|
Subroutine
_register_prometheus: called CC=1 SCAR=0.0
| |||||||||||||
| 140 | 2 | $prometheus->declare('tlsrpt_reports_processed_total', help=>'TLSRPT reports processed', type=>'counter' ); | |||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | |||||||||||||
| 144 | 2 | ▶ | sub process_prometheus($self,$prometheus) { | ||||||||||
|
Subroutine
process_prometheus: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||
| 145 | 2 | $self->_register_prometheus($prometheus); | |||||||||||
| 146 | 2 | $prometheus->add('tlsrpt_reports_processed_total',1,{ | |||||||||||
| 147 | organization_name=>$self->organization_name, | ||||||||||||
| 148 | policies=>scalar $self->policies->@*, | ||||||||||||
| 149 | }); | ||||||||||||
| 150 | 2 | ▶ | Mail::TLSRPT::Policy->_register_prometheus($prometheus) if ! scalar $self->policies->@*; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 151 | 2 | ▶ | Mail::TLSRPT::Failure->_register_prometheus($prometheus) if ! scalar $self->policies->@*; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 152 | 2 | foreach my $policy ( $self->policies->@* ) { | |||||||||||
| 153 | 2 | $policy->process_prometheus($self,$prometheus); | |||||||||||
| 154 | } | ||||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | 2 | ▶ | sub _csv_headers($self) { | ||||||||||
|
Subroutine
_csv_headers: called CC=1 SCAR=0.0
| |||||||||||||
| 158 | return ( | ||||||||||||
| 159 | 2 | 'report id', | |||||||||||
| 160 | 'organization name', | ||||||||||||
| 161 | 'start date time', | ||||||||||||
| 162 | 'end date time', | ||||||||||||
| 163 | 'contact info', | ||||||||||||
| 164 | ); | ||||||||||||
| 165 | } | ||||||||||||
| 166 | |||||||||||||
| 167 | 5 | ▶ | sub _csv_fragment($self) { | ||||||||||
|
Subroutine
_csv_fragment: called CC=2 SCAR=6.9
| |||||||||||||
| 168 | return ( | ||||||||||||
| 169 | 5 | ▶ | $self->report_id, | ||||||||||
|
Condition100%2/2
$self->contact_info // ""
MC/DC100%1/1
$self->contact_info // ""
$self->contact_info
Truth table100%2/2
A
A= $self->contact_info
| |||||||||||||
| 170 | $self->organization_name, | ||||||||||||
| 171 | $self->start_datetime->datetime.'Z', | ||||||||||||
| 172 | $self->end_datetime->datetime.'Z', | ||||||||||||
| 173 | $self->contact_info // '', | ||||||||||||
| 174 | ); | ||||||||||||
| 175 | } | ||||||||||||
| 176 | |||||||||||||
| 177 | |||||||||||||
| 178 | 3 | ▶ | sub as_csv($self,$args) { | ||||||||||
|
Subroutine
as_csv: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||
| 179 | 3 | my @output; | |||||||||||
| 180 | 3 | my $csv = Text::CSV->new; | |||||||||||
| 181 | 3 | ▶ | if ( $args->{add_header} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 182 | 2 | $csv->combine($self->_csv_headers,Mail::TLSRPT::Policy->_csv_headers,Mail::TLSRPT::Failure->_csv_headers); | |||||||||||
| 183 | 2 | push @output, $csv->string; | |||||||||||
| 184 | } | ||||||||||||
| 185 | 3 | ▶ | if ( scalar $self->policies->@* ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 186 | 3 | foreach my $policy ( $self->policies->@* ) { | |||||||||||
| 187 | 3 | ▶ | if ( scalar $policy->failures->@* ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | 2 | foreach my $failure ( $policy->failures->@* ) { | |||||||||||
| 189 | 4 | $csv->combine($self->_csv_fragment,$policy->_csv_fragment,$failure->_csv_fragment); | |||||||||||
| 190 | 4 | push @output, $csv->string; | |||||||||||
| 191 | } | ||||||||||||
| 192 | } | ||||||||||||
| 193 | else { | ||||||||||||
| 194 | 1 | $csv->combine($self->_csv_fragment,$policy->_csv_fragment); | |||||||||||
| 195 | 1 | push @output, $csv->string; | |||||||||||
| 196 | } | ||||||||||||
| 197 | } | ||||||||||||
| 198 | } | ||||||||||||
| 199 | else { | ||||||||||||
| 200 | 0 | $csv->combine($self->_csv_fragment); | |||||||||||
| 201 | 0 | push @output, $csv->string; | |||||||||||
| 202 | } | ||||||||||||
| 203 | 3 | return join( "\n", @output ); | |||||||||||
| 204 | } | ||||||||||||
| 205 | |||||||||||||
| 206 | 13 | 1; | |||||||||||
| 207 | |||||||||||||
| 208 | __END__ | ||||||||||||