Jmx4Perl/Agent/Jolokia/Verifier.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 73 CC 8 SCAR 31.3
CC
8
Cov
0%
BEGIN
42.0
verify
12.0
new
2.0
CRAP 22.8
1 #!/usr/bin/perl
2  
3 package JMX::Jmx4Perl::Agent::Jolokia::Verifier;
4  
5 =head1 NAME
6  
7 JMX::Jmx4Perl::Agent::Verifier - Handler for various verifiers which picks
8 the most secure one first.
9  
10 =head1 DESCRIPTION
11  
12 Entry module for verification of downloaded artifacts. Depending on modules
13 installed, various validation mechanisms are tried in decreasing order fo
14 vialibility:
15  
16 =over
17  
18 =item L<Crypt::OpenPGP>
19  
20 The strongest validation is provided by PGP signatures with which Jolokia
21 artifact is signed. The verifier uses L<Crypt::OpenPGP> for verifying PGP
22 signatures.
23  
24 =item L<Digest::SHA1>
25  
26 If OpenPGP is not available or when no signature is provided from the Jolokia
27 site (unlikely), a simple SHA1 checksum is fetched and compared to the artifact
28 downloaded. This is not secure, but guarantees some degree of consistency.
29  
30 =item L<Digest::MD5>
31  
32 As last resort, when this module is availabl, a MD5 checksum is calculated and
33 compared to the checksum also downloaded from www.jolokia.org.
34  
35 =back
36  
37 =head1 METHODS
38  
39 =over 4
40  
41 =cut
42  
43 use Data::Dumper;
44 use vars qw(@VERIFIERS @WARNINGS);
45 use strict;
46  
47 # Pick the verifier, which is the most reliable
48  
49 BEGIN {
50 @VERIFIERS = ();
51 @WARNINGS = ();
52  
53 my $create = sub {
54 my $module = shift;
55 eval "require $module";
56 die $@ if $@;
57 my $verifier;
58 eval "\$verifier = new $module()";
59 die $@ if $@;
60 return $verifier;
61 };
62  
63 my $prefix = "JMX::Jmx4Perl::Agent::Jolokia::Verifier::";
64 if (`gpg --version` =~ /GnuPG/m) {
65 push @VERIFIERS,$create->($prefix . "GnuPGVerifier");
66 } else {
67 push @WARNINGS,"No signature verification available. Please install GnupPG.";
68 }
69  
70 # Disabled support for OpenPGP since it doesn't support the digest
71 # algorithm used for signging the jolokia artefacts
72 # } elsif (eval "requireCrypt::OpenPGP; 1") {
73 # push @VERIFIERS,$create->($prefix . "OpenPGPVerifier");
74  
75 push @VERIFIERS,$create->($prefix . "SHA1Verifier") if eval "require Digest::SHA1; 1";
76 push @VERIFIERS,$create->($prefix . "MD5Verifier") if eval "require Digest::MD5; 1";
77 }
78  
79 =item $verifier = JMX::Jmx4Perl::Agent::Jolokia::Verifier->new(%args)
80  
81 Creates a new verifier. It takes an expanded hash als argument, where the
82 following keys are respected:
83  
84 "ua_config" UserAgent configuration used for accessing
85 remote signatures/checksums
86 "logger" Logger
87  
88 =cut
89  
90 sub new {
91 my $class = shift;
92 my $self = {@_};
93 bless $self,(ref($class) || $class);
94 }
95  
96 =item $verifier->verify(url => $url,path => $file)
97  
98 =item $verifier->verify(url => $url,data => $data)
99  
100 Verifies the given file (C<path>) or scalar data (C<data>) by trying various
101 validators in turn. Technically, each validator is asked for an extension
102 (e.g. ".asc" for a PGP signature), which is appended to URL and this URL is
103 tried for downloading the signature/checksum. If found, the content of the
104 signature/checksum is passed to specific verifier along with the data/file to
105 validate. A verifier will die, if validation fails, so one should put this in
106 an eval if required. If validation passes, the method returns silently.
107  
108 =back
109  
110 =cut
111  
112 sub verify {
113 my $self = shift;
114 my %args = @_;
115 my $url = $args{url};
116
117 my $ua = new JMX::Jmx4Perl::Agent::Jolokia::DownloadAgent($self->{ua_config});
118 my $log = $self->{logger};
119 $log->warn($_) for @WARNINGS;
120 for my $verifier (@VERIFIERS) {
121 my $ext = $verifier->extension;
122 if ($ext) {
123 my $response = $ua->get($url . $ext);
124 if ($response->is_success) {
125 my $content = $response->decoded_content;
126 $verifier->verify(%args,signature => $content,logger => $log);
127 return;
128 } else {
129 $log->warn($verifier->name . ": Couldn't load $url$ext");
130 }
131 }
132 }
133 $log->warn("No suitable validation mechanism found with $url");
134 }
135  
136  
137 =head1 LICENSE
138  
139 This file is part of jmx4perl.
140 Jmx4perl is free software: you can redistribute it and/or modify
141 it under the terms of the GNU General Public License as published by
142 The Free Software Foundation, either version 2 of the License, or
143 (at your option) any later version.
144
145 jmx4perl is distributed in the hope that it will be useful,
146 but WITHOUT ANY WARRANTY; without even the implied warranty of
147 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148 GNU General Public License for more details.
149
150 You should have received a copy of the GNU General Public License
151 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
152  
153 A commercial license is available as well. Please contact roland@cpan.org for
154 further details.
155  
156 =head1 AUTHOR
157  
158 roland@cpan.org
159  
160 =cut
161  
162 1;