CSAF

 view release on metacpan or  search on metacpan

lib/CSAF/Downloader.pm  view on Meta::CPAN

package CSAF::Downloader;

use 5.010001;
use strict;
use warnings;
use utf8;

use CSAF::Options::Downloader;
use CSAF::Util qw(file_read gpg_verify);
use CSAF;

use Cpanel::JSON::XS;
use File::Basename;
use File::Path            qw(make_path);
use File::Spec::Functions qw(catfile);
use LWP::UserAgent;
use Parallel::ForkManager;
use URI::URL;

use Moo;
with 'CSAF::Util::Log';

has options => (
    isa   => sub { Carp::croak 'Invalid configurator' unless ref($_[0]) eq 'CSAF::Options::Downloader' },
    is    => 'lazy',
    build => 1,
);

sub _build_options { CSAF::Options::Downloader->new }


sub ua {

    my $self = shift;

    my $ua = LWP::UserAgent->new(
        ssl_opts          => {verify_hostname => ($self->options->insecure ? !!0 : !!1)},
        agent             => sprintf('CSAF/%s', $CSAF::VERSION),
        protocols_allowed => ['http', 'https']
    );

    $ua->add_handler(
        'request_send' => sub {
            $self->log->trace('[HTTP Request]', $_[0]->method, $_[0]->uri);
            return;
        }
    );

    $ua->add_handler(
        'response_done' => sub {
            $self->log->trace('[HTTP Response]', $_[0]->status_line);
            return;
        }
    );

    return $ua;

}

sub mirror {

    my ($self, $url) = @_;

    my $is_provider_metadata = 0;
    my $is_index_txt         = 0;
    my $is_rolie_feed        = 0;
    my $is_base_url          = 0;

    $is_index_txt         = 1 if ($url =~ /\/index\.txt$/);

lib/CSAF/Downloader.pm  view on Meta::CPAN


        }

    }

}

sub _check_document {

    my ($self, $csaf_file) = @_;

    my $log = $self->log;

    if ($self->options->validate) {

        $log->info("Validate CSAF document");

        my $parser = CSAF::Parser->new(file => $csaf_file);
        my $csaf   = $parser->parse;

        if (my @errors = $csaf->validate($self->options->validate)) {
            $log->error($_) for (@errors);
        }

    }

    if ($self->options->integrity_check) {

        my @algo = (256, 512);

        foreach my $algo (@algo) {
            if (-e "$csaf_file.sha$algo") {

                $log->info("Check integrity of CSAF document ($algo)");

                my $sha = Digest::SHA->new($algo);
                $sha->addfile($csaf_file);
                my $digest = $sha->hexdigest;

                my ($verify) = split /\s+/, file_read("$csaf_file.sha$algo");

                if ($verify eq $digest) {
                    $log->info("Integrity check OK: $algo");
                }
                else {
                    $log->error("Integrity check KO: $algo");
                    Carp::croak "Integrity check failed for '$csaf_file'";
                }

            }
        }

    }

    if ($self->options->signature_check) {

        if (-e "$csaf_file.asc") {

            $log->info("Check signature of CSAF document");

            my $result = gpg_verify(signed => "$csaf_file.asc", file => $csaf_file);

            if ($result->{exit_code} == 0) {
                $log->info("Signature check OK");
                $log->trace($result->{status});
            }
            else {
                $log->error("Signature check FAILED");
                $log->trace($result->{status});
                Carp::croak "Signature check FAILED for '$csaf_file'";
            }

        }

    }

}

1;

__END__

=encoding utf-8

=head1 NAME

CSAF::Downloader - Download CSAF documents

=head1 SYNOPSIS

    use CSAF::Downloader;

    my $downloader = CSAF->Downloader;
    $downloader->mirror($url);

=head1 DESCRIPTION

L<CSAF::Downloader> allows the download of CSAF documents through C<index.txt>,
C<provider-metadata.json> or a ROLIE feed.

=head2 METHODS

=over

=item CSAF::Downloader>new

=item $downloader->mirror ( $url )

Download all CSAF document using the provided C<$url>.

=item $downloader->options

Change the default options for L<CSAF::Options::Downloader> configurator.

=back

Execute the command

=head1 SUPPORT

=head2 Bugs / Feature Requests



( run in 1.229 second using v1.01-cache-2.11-cpan-df04353d9ac )