CSAF/Util.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« CSAF/Validator/MandatoryTests.pm ↑ Summary CSAF/Type/Document.pm »
1 package CSAF::Util;
2  
3 35 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 35 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use GnuPG::Handles;
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use GnuPG::Interface;
14 use IO::Handle;
15 use List::Util qw(first);
16 use Time::Piece;
17  
18 use Exporter 'import';
19  
20 our @EXPORT_OK = (qw[
21 schema_cache_path resources_path tt_templates_path
22 parse_datetime tracking_id_to_well_filename
23 collect_product_ids file_read file_write product_in_group_exists
24 list_cves gpg_sign gpg_verify log_formatter uniq
25 ]);
26  
27 my %LOG_LEVELS = (
28 0 => 'EMERGENCY',
29 1 => 'ALERT',
30 2 => 'CRITICAL',
31 3 => 'ERROR',
32 4 => 'WARNING',
33 5 => 'NOTICE',
34 6 => 'INFO',
35 7 => 'DEBUG',
36 8 => 'TRACE',
37 );
38  
39 {
40 no warnings qw{ redefine };
41 sub Time::Piece::TO_JSON { shift->datetime }
42 }
43  
44 sub schema_cache_path { catfile(resources_path(), 'cache') }
45 sub tt_templates_path { catfile(resources_path(), 'template') }
46 sub resources_path { catfile(dirname(__FILE__), 'resources') }
47  
48 sub list_cves {
49  
50 my $csaf = shift;
51 my @cves = ();
52  
53 $csaf->vulnerabilities->each(sub {
54 push @cves, $_->cve;
55 });
56  
57 return wantarray ? @cves : "@cves";
58  
59 }
60  
61 # List::Util::uniq is included in the core module since Perl v5.26.0
62 sub uniq {
63 my %seen;
64 grep !$seen{$_}++, @_;
65 }
66  
67 sub parse_datetime {
68  
69 my $datetime = shift;
70 return unless $datetime;
71  
72 return $datetime if ($datetime->isa('Time::Piece'));
73  
74 return Time::Piece->new($datetime) if ($datetime =~ /^([0-9]+)$/);
75 return Time::Piece->new if ($datetime eq 'now');
76  
77 return Time::Piece->strptime($1, '%Y-%m-%dT%H:%M:%S') if ($datetime =~ /(\d{4}-\d{2}-\d{2}[T]\d{2}:\d{2}:\d{2})/);
78 return Time::Piece->strptime($1, '%Y-%m-%d %H:%M:%S') if ($datetime =~ /(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/);
79 return Time::Piece->strptime($1, '%Y-%m-%d') if ($datetime =~ /(\d{4}-\d{2}-\d{2})/);
80  
81 }
82  
83 sub tracking_id_to_well_filename {
84  
85 my $id = shift;
86  
87 $id = lc $id;
88 $id =~ s/[^+\-a-z0-9]+/_/g; # Rif. 5.1 (Additional Conventions - Filename)
89  
90 return "$id.json";
91  
92 }
93  
94 sub collect_product_ids {
95  
96 my $item = shift;
97 my @product_ids = ();
98  
99 my $ref_item = ref($item);
100  
101 if ($ref_item =~ /Branch$/) {
102  
103 if ($item->has_product) {
104 push @product_ids, $item->product->product_id;
105 }
106  
107 foreach (@{$item->branches->items}) {
108 push @product_ids, collect_product_ids($_);
109 }
110  
111 }
112  
113 if ($ref_item =~ /FullProductName$/) {
114 push @product_ids, $item->product_id;
115 }
116  
117 return @product_ids;
118  
119 }
120  
121 sub product_in_group_exists {
122  
123 my ($csaf, $product_id, $group_id) = @_;
124  
125 my $exists = 0;
126  
127 $csaf->product_tree->product_groups->each(sub {
128  
129 my ($group) = @_;
130  
131 if ($group->group_id eq $group_id) {
132 if (first { $product_id eq $_ } @{$group->product_ids}) {
133 $exists = 1;
134 return;
135 }
136 }
137  
138 });
139  
140 return $exists;
141  
142 }
143  
144 sub file_read {
145  
146 my $file = shift;
147  
148 if (ref($file) eq 'GLOB') {
149 return do { local $/; <$file> };
150 }
151  
152 return do {
153 open(my $fh, '<', $file) or Carp::croak qq{Failed to read file: $!};
154 local $/ = undef;
155 <$fh>;
156 };
157  
158 }
159  
160 sub file_write {
161  
162 my ($file, $content) = @_;
163  
164 my $fh = undef;
165  
166 if (ref($file) eq 'GLOB') {
167 $fh = $file;
168 }
169 else {
170 open($fh, '>', $file) or Carp::croak "Can't open file: $!";
171 }
172  
173 $fh->autoflush(1);
174  
175 print $fh $content;
176 close($fh);
177  
178 }
179  
180 sub gpg_get_result_from_handles {
181  
182 my %handle = @_;
183  
184 my %result = ();
185 my $error = undef;
186  
187 foreach (qw[stdout stderr logger status]) {
188  
189 $result{$_} = do { local $/ = undef; readline $handle{$_} };
190 delete $result{$_} unless $result{$_} && $result{$_} =~ /\S/s;
191  
192 if (not close $handle{$_}) {
193 $error ||= "Can't close gnupg $_ handle: $!";
194 }
195  
196 }
197  
198 Carp::carp $error if $error;
199  
200 $result{exit_code} = ($? >> 8);
201  
202 return \%result;
203  
204 }
205  
206 sub gpg_verify {
207  
208 my %args = (signed => undef, file => undef, @_);
209  
210 my %handle = (
211 stdin => IO::Handle->new,
212 stdout => IO::Handle->new,
213 stderr => IO::Handle->new,
214 logger => IO::Handle->new,
215 status => IO::Handle->new
216 );
217  
218 local $ENV{LANG} = 'C';
219  
220 my $gnupg = GnuPG::Interface->new();
221  
222 $gnupg->options->hash_init(meta_interactive => 0);
223  
224 my $pid = $gnupg->wrap_call(
225 commands => ['--verify'],
226 command_args => [$args{signed}, $args{file}],
227 handles => GnuPG::Handles->new(%handle)
228 );
229 waitpid $pid, 0;
230  
231 return gpg_get_result_from_handles(%handle);
232  
233 }
234  
235 sub gpg_sign {
236  
237 my %args = (passphrase => undef, plaintext => undef, key => undef, recipients => [], @_);
238  
239 my %handle = (
240 stdin => IO::Handle->new,
241 stdout => IO::Handle->new,
242 stderr => IO::Handle->new,
243 logger => IO::Handle->new,
244 status => IO::Handle->new
245 );
246  
247 local $ENV{LANG} = 'C';
248  
249 my $gnupg = GnuPG::Interface->new();
250  
251 $gnupg->options->hash_init(armor => 1, meta_interactive => 0);
252 $gnupg->options->default_key($args{key}) if defined $args{key};
253 $gnupg->options->push_recipients($_) for (@{$args{recipients}});
254  
255 $gnupg->passphrase($args{passphrase});
256  
257 my $pid = $gnupg->detach_sign(handles => GnuPG::Handles->new(%handle));
258  
259 print {$handle{stdin}} ($args{plaintext});
260 close $handle{stdin};
261  
262 waitpid $pid, 0;
263  
264 return gpg_get_result_from_handles(%handle);
265  
266 }
267  
268 sub log_formatter {
269  
270 my ($category, $level, $format, @params) = @_;
271  
272 @params = map { ref $_ ? Dumper($_) : $_ } @params;
273  
274 my $message = sprintf($format, @params);
275 my $now = Time::Piece->new->datetime;
276  
277 return sprintf('[%s] [%s] [%s] [%s] %s', $now, $$, lc($LOG_LEVELS{$level}), $category, $message);
278  
279 }
280  
281 1;
282  
283 __END__
« CSAF/Validator/MandatoryTests.pm ↑ Summary CSAF/Type/Document.pm »