| 1 | package CSAF::Writer; | ||
| 2 | |||
| 3 | use 5.010001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use utf8; | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | use CSAF::Util qw(tracking_id_to_well_filename file_write gpg_sign); | ||
| 10 | use CSAF::Options::Writer; | ||
| 11 | use Digest::SHA qw(sha256_hex sha512_hex); | ||
| 12 | use File::Basename qw(basename dirname); | ||
| 13 | use File::Path qw(make_path); | ||
| 14 | use File::Spec::Functions qw(catfile); | ||
| 15 | use Tie::File; | ||
| 16 | |||
| 17 | use Moo; | ||
| 18 | extends 'CSAF::Base'; | ||
| 19 | with 'CSAF::Util::Log'; | ||
| 20 | |||
| 21 | use constant DEBUG => $ENV{CSAF_DEBUG}; | ||
| 22 | |||
| 23 | has options => ( | ||
| 24 | isa => sub { Carp::croak q{Not "CSAF::Options::Writer" object} unless ref($_[0]) eq 'CSAF::Options::Writer' }, | ||
| 25 | is => 'lazy', | ||
| 26 | build => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub _build_options { CSAF::Options::Writer->new } | ||
| 30 | |||
| 31 | has directory => (is => 'rw', required => 1, isa => sub { Carp::croak qq{Output directory not found} unless -d $_[0] }); | ||
| 32 | |||
| 33 | sub write { | ||
| 34 | |||
| 35 | my ($self) = @_; | ||
| 36 | |||
| 37 | my $csaf_id = $self->csaf->document->tracking->id; | ||
| 38 | my $csaf_json = $self->csaf->renderer->render; | ||
| 39 | my $csaf_directory = $self->directory; | ||
| 40 | my $csaf_filename = tracking_id_to_well_filename($csaf_id); | ||
| 41 | my $csaf_file_year = $self->csaf->document->tracking->initial_release_date->year; | ||
| 42 | my $csaf_current_release_date = $self->csaf->document->tracking->current_release_date->datetime; | ||
| 43 | |||
| 44 | my $json_file_path = catfile($csaf_directory, $csaf_file_year, $csaf_filename); | ||
| 45 | my $index_file_path = catfile($csaf_directory, 'index.txt'); | ||
| 46 | my $changes_file_path = catfile($csaf_directory, 'changes.csv'); | ||
| 47 | my $csaf_document_path = catfile($csaf_file_year, $csaf_filename); | ||
| 48 | |||
| 49 | if (DEBUG) { | ||
| 50 | $self->log->debug("Destination directory : $csaf_directory"); | ||
| 51 | $self->log->debug("CSAF document : $csaf_filename"); | ||
| 52 | $self->log->debug("CSAF document path : $json_file_path"); | ||
| 53 | $self->log->debug("index.txt path : $index_file_path"); | ||
| 54 | $self->log->debug("changes.csv path : $changes_file_path"); | ||
| 55 | $self->log->debug("SHA256 file path : $json_file_path.sha256") if $self->options->create_sha256_integrity; | ||
| 56 | $self->log->debug("SHA512 file path : $json_file_path.sha512") if $self->options->create_sha512_integrity; | ||
| 57 | $self->log->debug("Signature file path : $json_file_path.asc") if $self->options->create_gpg_signature; | ||
| 58 | } | ||
| 59 | |||
| 60 | make_path(dirname($json_file_path)); | ||
| 61 | |||
| 62 | $self->log->info("Save $csaf_id document ($csaf_filename)"); | ||
| 63 | |||
| 64 | file_write($json_file_path, $csaf_json); | ||
| 65 | |||
| 66 | if ($self->options->create_sha256_integrity) { | ||
| 67 | $self->log->info("Create SHA256 integrity file ($csaf_filename.sha256)"); | ||
| 68 | file_write("$json_file_path.sha256", sprintf("%s %s\n", sha256_hex($csaf_json), $csaf_filename)); | ||
| 69 | } | ||
| 70 | |||
| 71 | if ($self->options->create_sha512_integrity) { | ||
| 72 | $self->log->info("Create SHA512 integrity file ($csaf_filename.sha512)"); | ||
| 73 | file_write("$json_file_path.sha512", sprintf("%s %s\n", sha512_hex($csaf_json), $csaf_filename)); | ||
| 74 | } | ||
| 75 | |||
| 76 | if ($self->options->create_gpg_signature) { | ||
| 77 | |||
| 78 | $self->log->info("Sign CSAF document with GPG and create signature file ($csaf_filename.asc)"); | ||
| 79 | |||
| 80 | my $result = gpg_sign( | ||
| 81 | key => $self->options->gpg_key, | ||
| 82 | passphrase => $self->options->gpg_passphrase, | ||
| 83 | plaintext => $csaf_json | ||
| 84 | ); | ||
| 85 | |||
| 86 | DEBUG and $self->log->debug($_) for (split("\n", $result->{status})); | ||
| 87 | |||
| 88 | if ($result->{exit_code} == 0) { | ||
| 89 | file_write("$json_file_path.asc", $result->{stdout}); | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | $self->log->error($result->{logger}); | ||
| 93 | } | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | if ($self->options->update_index) { | ||
| 98 | |||
| 99 | $self->log->info("Update index.txt file ($index_file_path)"); | ||
| 100 | |||
| 101 | tie my @index_data, 'Tie::File', $index_file_path or Carp::croak "Unable to write $index_file_path"; | ||
| 102 | push @index_data, $csaf_document_path unless grep /^$csaf_document_path$/, @index_data; | ||
| 103 | |||
| 104 | @index_data = ((), sort { $b cmp $a } @index_data); | ||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | if ($self->options->update_changes) { | ||
| 109 | |||
| 110 | $self->log->info("Update changes.csv file ($changes_file_path)"); | ||
| 111 | |||
| 112 | my $changes_row = join ',', qq{"$csaf_document_path"}, qq{"$csaf_current_release_date"}; | ||
| 113 | |||
| 114 | tie my @changes_data, 'Tie::File', $changes_file_path or Carp::croak "Unable to write $changes_file_path"; | ||
| 115 | push @changes_data, $changes_row unless grep /^$changes_row$/, @changes_data; | ||
| 116 | |||
| 117 | @changes_data = ((), sort { (split(/\,/, $b))[1] cmp(split(/\,/, $a))[1] } @changes_data); | ||
| 118 | |||
| 119 | } | ||
| 120 | |||
| 121 | return 1; | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | 1; | ||
| 126 | |||
| 127 | __END__ |