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

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 145 CC 16 SCAR 31.3
CC
16
Cov
0%
verify
42.0
_verify_gpg_output
42.0
_import_key
20.0
CRAP 22.8
1 #!/usr/bin/perl
2  
3 package JMX::Jmx4Perl::Agent::Jolokia::Verifier::GnuPGVerifier;
4  
5 use JMX::Jmx4Perl::Agent::Jolokia::Verifier::PGPKey;
6 use Module::Find;
7 use Data::Dumper;
8 use File::Temp qw/tempfile/;
9  
10 use strict;
11  
12 =head1 NAME
13  
14 JMX::Jmx4Perl::Agent::Jolokia::Verifier::GnuPGVerifier - Verifies PGP
15 signature with a natively installed GnuPG (with gpg found in the path)
16  
17 =head1 DESCRIPTION
18  
19 This verifier uses a natively installed GPG for validating a PGP signature
20 obtained from the download site. It's similar to
21 L<JMX::Jmx4Perl::Agent::Jolokia::Verifier::OpenPGPVerifier> except that it will
22 use a locally installed GnuPG installation. Please note, that it will import
23 the public key used for signature verification into the local keystore.
24  
25 =cut
26  
27 sub new {
28 my $class = shift;
29 my $self = {};
30 ($self->{gpg},$self->{version}) = &_gpg_version();
31 bless $self,(ref($class) || $class);
32 }
33  
34 sub extension {
35 return ".asc";
36 }
37  
38 sub name {
39 return "GnuPG";
40 }
41  
42 sub verify {
43 my $self = shift;
44 my %args = @_;
45  
46 my $log = $args{logger};
47 my $gpg = $self->{gpg};
48  
49 die "Neither 'path' nor 'data' given for specifying the file/data to verify"
50 unless $args{path} || $args{data};
51  
52 my $signature_path = $self->_store_tempfile($args{signature});
53 my $path = $args{path} ? $args{path} : $self->_store_tempfile($args{data});
54 my @cmd = (
55 $gpg,
56 qw(--verify --batch --no-tty -q --logger-fd=1),
57 );
58 eval {
59 push @cmd, $signature_path,$path;
60 # Unset language for proper parsing of the output independent
61 # of the locale
62 local $ENV{LANG} = undef;
63 my $cmd = join ' ', @cmd;
64 my $output = `$cmd`;
65 if ($output =~ /public\s*key/i) {
66 # Import key and retry
67 $self->_import_key(\%args);
68 $output = `$cmd`;
69 }
70
71 $self->_verify_gpg_output($?,$output,\%args);
72 };
73
74 # Always cleanup
75 my $error = $@;
76 unlink $signature_path;
77 unlink $path unless $args{path};
78 die $error if $error;
79  
80 }
81  
82 sub _verify_gpg_output {
83 my $self = shift;
84 my $code = shift;
85 my $output = shift;
86 my $args = shift;
87 my $log = $args->{logger};
88 my $key = $1 if $output =~ /\s+([\dA-F]{8})/;
89 # print $output,"\n";
90 if ($code) {
91 $log->error("Invalid signature",$args->{path} ? " for " . $args->{path} : "",$key ? " (key: $key)" : "");
92 die "\n";
93 } else {
94 $log->info("Good PGP signature" . ($key ? " ($key)" : ""));
95 }
96 }
97  
98 sub _import_key {
99 my $self = shift;
100 my $args = shift;
101  
102 my $gpg = $self->{gpg};
103 my $log = $args->{logger};
104 my $key_path = $self->_store_tempfile($JMX::Jmx4Perl::Agent::Jolokia::Verifier::PGPKey::KEY);
105  
106 my @cmd = ($gpg,qw(--import --verbose --batch --no-tty --logger-fd=1),$key_path);
107 my $cmd = join ' ', @cmd;
108 my $output = `$cmd 2>&1`;
109 if ($?) {
110 $log->error("Cannot add public PGP used for verification to local keystore: $output");
111 die "\n";
112 } else {
113 #$log->info($output);
114 my $info = $1 if $output =~ /([\dA-F]{8}.*import.*)$/mi;
115 $log->info($info ? $info : "Added jmx4perl key");
116 }
117 unlink $key_path;
118 }
119  
120  
121 sub _gpg_version {
122 my $gpg = "gpg2";
123 my $out = `gpg2 --version`;
124 if ($?) {
125 $out = `gpg --version`;
126 $gpg = "gpg";
127 if ($?) {
128 die "Cannot find gpg or gpg2: $out\n";
129 }
130 }
131 $out =~ /GnuPG.*?(\S+)\s*$/m;
132 return ($gpg,$1);
133 }
134  
135 sub _store_tempfile {
136 my $self = shift;
137 my $sig = shift || die "No data given to store in temp file";
138 my ($fh,$path) = tempfile();
139 print $fh $sig;
140 close $fh;
141 return $path;
142 }
143  
144  
145 1;
146  
147 =head1 LICENSE
148  
149 This file is part of jmx4perl.
150 Jmx4perl is free software: you can redistribute it and/or modify
151 it under the terms of the GNU General Public License as published by
152 The Free Software Foundation, either version 2 of the License, or
153 (at your option) any later version.
154
155 jmx4perl is distributed in the hope that it will be useful,
156 but WITHOUT ANY WARRANTY; without even the implied warranty of
157 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
158 GNU General Public License for more details.
159
160 You should have received a copy of the GNU General Public License
161 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
162  
163 A commercial license is available as well. Please contact roland@cpan.org for
164 further details.
165  
166 =head1 AUTHOR
167  
168 roland@cpan.org
169  
170 =cut