| 1 | package Mail::TLSRPT::Report; | ||||||||||
| 2 | # ABSTRACT: TLSRPT report object | ||||||||||
| 3 | 7 | our $VERSION = '2.20210112'; # VERSION | |||||||||
| 4 | 7 | ▶ | use 5.20.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 7 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 7 | ▶ | use Mail::TLSRPT::Pragmas; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | use Mail::TLSRPT::Policy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 7 | ▶ | use Mail::TLSRPT::Failure; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 7 | ▶ | use DateTime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 7 | ▶ | use Date::Parse qw{ str2time }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 7 | ▶ | use IO::Uncompress::Gunzip; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 7 | ▶ | use Text::CSV; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 7 | has organization_name => (is => 'rw', isa => Str, required => 1); | |||||||||
| 14 | 9 | ▶ | has start_datetime => (is => 'rw', isa => class_type('DateTime'), required => 1, coerce => sub{&_coerce_datetime}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 9 | ▶ | has end_datetime => (is => 'rw', isa => class_type('DateTime'), required => 1, coerce => sub{&_coerce_datetime}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 7 | has contact_info => (is => 'rw', isa => Str, required => 1); | |||||||||
| 17 | 7 | has report_id => (is => 'rw', isa => Str, required => 1); | |||||||||
| 18 | 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
| |||||||||||
| 19 | |||||||||||
| 20 | 18 | ▶ | sub _coerce_datetime { | ||||||||
|
Subroutine
_coerce_datetime: called CC=2 SCAR=6.9
| |||||||||||
| 21 | 18 | my $time = shift; | |||||||||
| 22 | 18 | ▶ | $time = DateTime->from_epoch( epoch=>$time ) unless ref $time eq 'DateTime'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 18 | return $time; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | 7 | ▶ | sub new_from_json($class,$json) { | ||||||||
|
Subroutine
new_from_json: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 28 | 7 | ▶ | if ( $json =~ /^\037\213/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | return $class->new_from_json_gz($json); | |||||||||
| 30 | } | ||||||||||
| 31 | 7 | my $j = JSON->new; | |||||||||
| 32 | 7 | my $data = $j->decode($json); | |||||||||
| 33 | 7 | return $class->new_from_data($data); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | 1 | ▶ | sub new_from_json_gz($class,$compressed_json) { | ||||||||
|
Subroutine
new_from_json_gz: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 38 | 1 | my $json; | |||||||||
| 39 | 1 | IO::Uncompress::Gunzip::gunzip(\$compressed_json,\$json); | |||||||||
| 40 | 1 | return $class->new_from_json($json); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | 8 | ▶ | sub new_from_data($class,$data) { | ||||||||
|
Subroutine
new_from_data: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 45 | 8 | my @policies; | |||||||||
| 46 | 8 | foreach my $policy ( $data->{policies}->@* ) { | |||||||||
| 47 | 6 | push @policies, Mail::TLSRPT::Policy->new_from_data($policy); | |||||||||
| 48 | } | ||||||||||
| 49 | my $self = $class->new( | ||||||||||
| 50 | organization_name => $data->{'organization-name'}, | ||||||||||
| 51 | start_datetime => DateTime->from_epoch(epoch => str2time($data->{'date-range'}->{'start-datetime'})), | ||||||||||
| 52 | end_datetime => DateTime->from_epoch(epoch => str2time($data->{'date-range'}->{'end-datetime'})), | ||||||||||
| 53 | contact_info => $data->{'contact-info'}, | ||||||||||
| 54 | 8 | report_id => $data->{'report-id'}, | |||||||||
| 55 | policies => \@policies, | ||||||||||
| 56 | ); | ||||||||||
| 57 | 8 | return $self; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | 3 | ▶ | sub as_json($self) { | ||||||||
|
Subroutine
as_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 62 | 3 | my $j = JSON->new; | |||||||||
| 63 | 3 | $j->canonical; | |||||||||
| 64 | 3 | return $j->encode( $self->as_struct ); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | |||||||||||
| 68 | 5 | ▶ | sub as_struct($self) { | ||||||||
|
Subroutine
as_struct: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 69 | 5 | my @policies = map {$_->as_struct} $self->policies->@*; | |||||||||
| 70 | return { | ||||||||||
| 71 | 5 | ▶ | 'organization-name' => $self->organization_name, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 'date-range' => { | ||||||||||
| 73 | 'start-datetime' => $self->start_datetime->datetime.'Z', | ||||||||||
| 74 | 'end-datetime' => $self->end_datetime->datetime.'Z', | ||||||||||
| 75 | }, | ||||||||||
| 76 | 'contact-info' => $self->contact_info, | ||||||||||
| 77 | 'report-id' => $self->report_id, | ||||||||||
| 78 | scalar $self->policies->@* ? ( policies => \@policies ) : (), | ||||||||||
| 79 | }; | ||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | 4 | ▶ | sub as_string($self) { | ||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 84 | return join( "\n", | ||||||||||
| 85 | 'Report-ID: <'.$self->report_id.'>', | ||||||||||
| 86 | 'From: "'.$self->organization_name.'" <'.$self->contact_info.'>', | ||||||||||
| 87 | 'Dates: '.$self->start_datetime->datetime.'Z to '.$self->end_datetime->datetime.'Z', | ||||||||||
| 88 | 4 | map { $_->as_string } $self->policies->@*, | |||||||||
| 89 | ); | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 1 | ▶ | sub _register_prometheus($self,$prometheus) { | ||||||||
|
Subroutine
_register_prometheus: called CC=1 SCAR=0.0
| |||||||||||
| 93 | 1 | $prometheus->declare('tlsrpt_reports_processed_total', help=>'TLSRPT reports processed', type=>'counter' ); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | 1 | ▶ | sub process_prometheus($self,$prometheus) { | ||||||||
|
Subroutine
process_prometheus: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||
| 98 | 1 | $self->_register_prometheus($prometheus); | |||||||||
| 99 | 1 | $prometheus->add('tlsrpt_reports_processed_total',1,{ | |||||||||
| 100 | organization_name=>$self->organization_name, | ||||||||||
| 101 | policies=>scalar $self->policies->@*, | ||||||||||
| 102 | }); | ||||||||||
| 103 | 1 | ▶ | Mail::TLSRPT::Policy->_register_prometheus($prometheus) if ! scalar $self->policies->@*; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 1 | ▶ | Mail::TLSRPT::Failure->_register_prometheus($prometheus) if ! scalar $self->policies->@*; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 1 | foreach my $policy ( $self->policies->@* ) { | |||||||||
| 106 | 1 | $policy->process_prometheus($self,$prometheus); | |||||||||
| 107 | } | ||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 1 | ▶ | sub _csv_headers($self) { | ||||||||
|
Subroutine
_csv_headers: called CC=1 SCAR=0.0
| |||||||||||
| 111 | return ( | ||||||||||
| 112 | 1 | 'report id', | |||||||||
| 113 | 'organization name', | ||||||||||
| 114 | 'start date time', | ||||||||||
| 115 | 'end date time', | ||||||||||
| 116 | 'contact info', | ||||||||||
| 117 | ); | ||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 3 | ▶ | sub _csv_fragment($self) { | ||||||||
|
Subroutine
_csv_fragment: called CC=1 SCAR=0.0
| |||||||||||
| 121 | return ( | ||||||||||
| 122 | 3 | $self->report_id, | |||||||||
| 123 | $self->organization_name, | ||||||||||
| 124 | $self->start_datetime->datetime.'Z', | ||||||||||
| 125 | $self->end_datetime->datetime.'Z', | ||||||||||
| 126 | $self->contact_info, | ||||||||||
| 127 | ); | ||||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | 1 | ▶ | sub as_csv($self,$args) { | ||||||||
|
Subroutine
as_csv: called CC=6 SCAR=19.7
Pod
documented
| |||||||||||
| 132 | 1 | my @output; | |||||||||
| 133 | 1 | my $csv = Text::CSV->new; | |||||||||
| 134 | 1 | ▶ | if ( $args->{add_header} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 1 | $csv->combine($self->_csv_headers,Mail::TLSRPT::Policy->_csv_headers,Mail::TLSRPT::Failure->_csv_headers); | |||||||||
| 136 | 1 | push @output, $csv->string; | |||||||||
| 137 | } | ||||||||||
| 138 | 1 | ▶ | if ( scalar $self->policies->@* ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 1 | foreach my $policy ( $self->policies->@* ) { | |||||||||
| 140 | 1 | ▶ | if ( scalar $policy->failures->@* ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 1 | foreach my $failure ( $policy->failures->@* ) { | |||||||||
| 142 | 3 | $csv->combine($self->_csv_fragment,$policy->_csv_fragment,$failure->_csv_fragment); | |||||||||
| 143 | 3 | push @output, $csv->string; | |||||||||
| 144 | } | ||||||||||
| 145 | } | ||||||||||
| 146 | else { | ||||||||||
| 147 | 0 | $csv->combine($self->_csv_fragment,$policy->_csv_fragment); | |||||||||
| 148 | 0 | push @output, $csv->string; | |||||||||
| 149 | } | ||||||||||
| 150 | } | ||||||||||
| 151 | } | ||||||||||
| 152 | else { | ||||||||||
| 153 | 0 | $csv->combine($self->_csv_fragment); | |||||||||
| 154 | 0 | push @output, $csv->string; | |||||||||
| 155 | } | ||||||||||
| 156 | 1 | return join( "\n", @output ); | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | 7 | 1; | |||||||||
| 160 | |||||||||||
| 161 | __END__ | ||||||||||