Jmx4Perl/Agent/Jolokia/Verifier/OpenPGPVerifier.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 90 CC 10 SCAR 40.3
CC
10
Cov
0%
verify
110.0
new
2.0
extension
2.0
CRAP 56.0
1 #!/usr/bin/perl
2  
3 package JMX::Jmx4Perl::Agent::Jolokia::Verifier::OpenPGPVerifier;
4  
5 use JMX::Jmx4Perl::Agent::Jolokia::Verifier::PGPKey;
6 use Crypt::OpenPGP::KeyRing;
7 use Crypt::OpenPGP;
8 use Module::Find;
9 use Data::Dumper;
10 use Cwd 'abs_path';
11  
12 use strict;
13  
14 =head1 NAME
15  
16 JMX::Jmx4Perl::Agent::Jolokia::Verifier::OpenPGPVerifier - Verifies PGP
17 signature with L<Crypt::OpenPGP>
18  
19 =head1 DESCRIPTION
20  
21 This verifier uses L<Crypt::OpenPGP> for validating a PGP signature obtained
22 from the download site. Ie. each URL used for download should have (and does
23 have) and associated signature ending with F<.asc>. This verifier typically
24 quite robust, however installing L<Crypt::OpenPGP> is a bit clumsy, so you
25 might omit this one.
26  
27 =head1 IMPORTANT
28  
29 It is not used currently since the new agents has been signed with 'digest
30 algortihm 10' which is not supported by OpenPGP. Use a native GnuPG instead
31 (i.e. a 'gpg' which is in the path)
32  
33 =cut
34  
35 sub new {
36 my $class = shift;
37 my $self = {};
38 $self->{keyring} = $JMX::Jmx4Perl::Agent::Jolokia::Verifier::PGPKey::KEY;
39 bless $self,(ref($class) || $class);
40 }
41  
42 sub extension {
43 return ".asc";
44 }
45  
46 sub name {
47 return "OpenPGP";
48 }
49  
50 sub verify {
51 my $self = shift;
52 my %args = @_;
53  
54 my $kr = new Crypt::OpenPGP::KeyRing(Data => $self->{keyring});
55 my $pgp = new Crypt::OpenPGP(PubRing => $kr);
56 my $path = $args{path};
57 my $log = $args{logger};
58 my $validate;
59 if ($path) {
60 $validate = $pgp->verify(Files => [abs_path($args{path})],Signature => $args{signature});
61 } else {
62 $validate = $pgp->verify(Data => $args{data},Signature => $args{signature});
63 }
64 if ($validate) {
65 my $key;
66 if ($validate != 1) {
67 my $kb = $kr->find_keyblock_by_uid($validate);
68 if ($kb) {
69 eval {
70 # Non-document method
71 $key = $kb->key->key_id_hex;
72 $key = substr $key,8,8 if length($key) > 8;
73 };
74 }
75 }
76 $log->info("Good PGP signature",
77 ($validate != 1 ? (", signed by ",$validate) : ""),
78 ($key ? " ($key)" :""));
79 return 1;
80 } elsif ($validate == 0) {
81 $log->error("Invalid signature",$path ? " for $path" : "",": " . $pgp->errstr);
82 die "\n";
83 } else {
84 $log->error("Error occured while verifying signature: ",$pgp->errstr);
85 die "\n";
86 }
87 }
88  
89 1;
90  
91 =head1 LICENSE
92  
93 This file is part of jmx4perl.
94 Jmx4perl is free software: you can redistribute it and/or modify
95 it under the terms of the GNU General Public License as published by
96 The Free Software Foundation, either version 2 of the License, or
97 (at your option) any later version.
98
99 jmx4perl is distributed in the hope that it will be useful,
100 but WITHOUT ANY WARRANTY; without even the implied warranty of
101 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
102 GNU General Public License for more details.
103
104 You should have received a copy of the GNU General Public License
105 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
106  
107 A commercial license is available as well. Please contact roland@cpan.org for
108 further details.
109  
110 =head1 AUTHOR
111  
112 roland@cpan.org
113  
114 =cut