CSAF/ROLIE/Feed.pm untested

filter: Statementstmt 0.0% 0 / 164 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 283 CC 23 SCAR 34.8
CC
23
Cov
0%
add_entry
110.0
from_csaf_directory
30.0
init
20.0
CRAP 32.6
1 package CSAF::ROLIE::Feed;
2  
3 use 5.010001;
4 use strict;
5 use warnings;
6 use utf8;
7  
8 use Moo;
9  
10  
11 use CSAF::Util qw(file_write file_read parse_datetime tracking_id_to_well_filename);
12 use CSAF::ROLIE::Entries;
13 use CSAF::Options::ROLIE;
14  
15 use Cpanel::JSON::XS;
16 use File::Basename;
17 use File::Find;
18 use File::Spec::Functions qw(catfile);
19 use Time::Piece;
20  
21 use constant TRUE => !!1;
22 use constant FALSE => !!0;
23  
24 my $CSAF_SCHEMAS = {'2.0' => 'https://docs.oasis-open.org/csaf/csaf/v2.0/csaf_json_schema.json'};
25  
26 has options => (
27 isa => sub { Carp::croak q{Not "CSAF::Options::ROLIE" object} unless ref($_[0]) eq 'CSAF::Options::ROLIE' },
28 is => 'lazy',
29 build => 1,
30 );
31  
32 sub _build_options { CSAF::Options::ROLIE->new }
33  
34 # ROLIE feed properties
35 has id => (is => 'rw');
36 has title => (is => 'rw');
37 has link => (is => 'rw');
38  
39 has category => (
40 is => 'lazy',
41 default => sub { [{scheme => 'urn:ietf:params:rolie:category:information-type', term => 'csaf'}] }
42 );
43  
44 has updated => (is => 'rw', required => 1, default => sub { Time::Piece->new }, coerce => \&parse_datetime);
45  
46  
47 sub init {
48  
49 my $self = shift;
50  
51 $self->id($self->options->feed_id) unless $self->id;
52 $self->title($self->options->feed_title) unless $self->title;
53 $self->link($self->options->feed_link) unless $self->link;
54  
55 }
56  
57 sub entry {
58 my $self = shift;
59 $self->{entry} ||= CSAF::ROLIE::Entries->new(@_);
60 }
61  
62 sub import_entry_from_file {
63  
64 my ($self, $feed_path) = @_;
65  
66 Carp::croak 'ROLIE feed not found' unless -e $feed_path;
67  
68 my $json = eval { Cpanel::JSON::XS->new->decode(file_read($feed_path)) };
69  
70 Carp::croak "Failed to parse the ROLIE feed: $@" if ($@);
71 Carp::croak 'Invalid ROLIE feed' unless defined $json->{feed};
72  
73 foreach my $entry (@{$json->{feed}->{entry}}) {
74 $self->entry->item(%{$entry});
75 }
76  
77 }
78  
79  
80 sub add_entry {
81  
82 my ($self, $csaf, $options) = @_;
83  
84 $options //= {};
85  
86 Carp::croak 'Not CSAF object' unless (ref($csaf) eq 'CSAF');
87  
88 my $id = $csaf->document->tracking->id;
89 my $title = $csaf->document->title;
90 my $published = $csaf->document->tracking->initial_release_date;
91 my $updated = $csaf->document->tracking->current_release_date;
92  
93 my $csaf_year = $csaf->document->tracking->initial_release_date->year;
94 my $csaf_url = join('/', $self->options->base_url, $csaf_year, tracking_id_to_well_filename($id));
95  
96 my $format = {schema => $CSAF_SCHEMAS->{$csaf->document->csaf_version}, version => $csaf->document->csaf_version};
97 my $content = {type => 'application/json', src => $csaf_url};
98 my $link = [{href => $csaf_url, rel => 'self'}];
99 my $summary = undef;
100  
101 $csaf->document->notes->each(sub {
102 my $note = shift;
103 $summary = $note->text if ($note->category eq 'summary');
104 });
105  
106 # TODO Add the check of the existence of integrity and signature files
107  
108 if (defined $options->{integrity}) {
109  
110 my $integrity = $options->{integrity};
111  
112 push @{$link}, {href => "$csaf_url.sha256", rel => 'hash'}
113 if (defined $integrity->{sha256} && $integrity->{sha256});
114  
115 push @{$link}, {href => "$csaf_url.sha512", rel => 'hash'}
116 if (defined $integrity->{sha512} && $integrity->{sha512});
117  
118 }
119  
120 push @{$link}, {href => "$csaf_url.asc", rel => 'signature'}
121 if (defined $options->{signature} && $options->{signature});
122  
123 my $is_updated = 0;
124  
125 $self->entry->each(sub {
126  
127 my ($item) = @_;
128  
129 if ($item->id eq $id) {
130  
131 if ($item->updated < $updated) {
132  
133 $item->updated($updated);
134 $item->title($title);
135 $item->summary($summary);
136  
137 }
138  
139 $is_updated = 1;
140 return;
141  
142 }
143  
144 });
145  
146 unless ($is_updated) {
147 my $entry = {
148 id => $id,
149 title => $title,
150 published => $published,
151 updated => $updated,
152 format => $format,
153 link => $link,
154 summary => $summary,
155 content => $content
156 };
157 $self->entry->item(%{$entry});
158 }
159  
160 $self->updated(Time::Piece->new);
161  
162 return $self;
163  
164 }
165  
166 sub from_file {
167  
168 my ($class, $feed_path) = @_;
169  
170 Carp::croak 'ROLIE feed not found' unless -e $feed_path;
171  
172 my $json = eval { Cpanel::JSON::XS->new->decode(file_read($feed_path)) };
173  
174 Carp::croak "Failed to parse the ROLIE feed: $@" if ($@);
175 Carp::croak 'Invalid ROLIE feed' unless defined $json->{feed};
176  
177 my $feed = $class->new(
178 id => $json->{feed}->{id},
179 title => $json->{feed}->{title},
180 link => $json->{feed}->{link},
181 updated => $json->{feed}->{updated}
182 );
183  
184 $feed->options->configure(feed_directory => dirname($feed_path), feed_filename => basename($feed_path));
185  
186 foreach my $entry (@{$json->{feed}->{entry}}) {
187 $feed->entry->item(%{$entry});
188 }
189  
190 return $feed;
191  
192 }
193  
194 sub render {
195  
196 my $self = shift;
197  
198 $self->init;
199  
200 my $json = Cpanel::JSON::XS->new->utf8->canonical->allow_nonref->allow_unknown->allow_blessed->convert_blessed
201 ->stringify_infnan->escape_slash(0)->allow_dupkeys->pretty->space_before(0);
202  
203 my @sorted_entries = sort { $b->updated->epoch <=> $a->updated->epoch } @{$self->entry->to_array};
204 $self->entry->items(\@sorted_entries);
205  
206 return $json->encode($self);
207  
208 }
209  
210 sub write {
211  
212 my ($self, $path) = @_;
213  
214 $path //= $self->options->feed_filename;
215  
216 file_write($path, $self->render);
217 return 1;
218  
219 }
220  
221 sub from_csaf_directory {
222  
223 my ($self, $path) = @_;
224  
225 $path //= $self->options->csaf_directory;
226  
227 Carp::croak 'Directory not found' unless -e -d $path;
228  
229 my @files = ();
230  
231 my $wanted = sub {
232 push @files, $File::Find::name if !-d $File::Find::name && $File::Find::name =~ /\.json$/;
233 };
234  
235 find {wanted => $wanted, no_chdir => 1}, $path;
236  
237 foreach my $file (@files) {
238  
239 my $parser = CSAF::Parser->new(file => $file);
240 my $csaf = eval { $parser->parse };
241  
242 next unless $csaf;
243  
244 my $id = $csaf->document->tracking->id;
245 my $tlp_label = $csaf->document->distribution->tlp->label;
246  
247 if ($tlp_label eq $self->options->tlp_label) {
248 $self->add_entry(
249 $csaf,
250 {
251 integrity => {sha256 => (-e "$file.sha256"), sha512 => (-e "$file.sha512")},
252 signature => (-e "$file.asc")
253 }
254 );
255 }
256  
257 }
258  
259 return $self;
260  
261 }
262  
263 sub TO_JSON {
264  
265 my $self = shift;
266  
267 my $json = {
268 feed => {
269 id => $self->id,
270 title => $self->title,
271 link => $self->link,
272 category => $self->category,
273 updated => $self->updated,
274 entry => $self->entry
275 }
276 };
277  
278 return $json;
279  
280 }
281  
282 1;
283  
284  
285 __END__