File Coverage

blib/lib/Jar/Signer.pm
Criterion Covered Total %
statement 6 79 7.5
branch 0 26 0.0
condition 0 5 0.0
subroutine 2 11 18.1
pod 0 7 0.0
total 8 128 6.2


line stmt bran cond sub pod time code
1             package Jar::Signer;
2            
3 1     1   1438 use File::Basename;
  1         2  
  1         122  
4 1     1   2711 use File::chdir;
  1         9676  
  1         2052  
5            
6             our $VERSION = 0.1;
7            
8             sub AUTOLOAD{
9 0     0     my $self = shift;
10 0   0       my $type = ref $self || die "$self is not an object";
11 0           my $name = $AUTOLOAD;
12 0           $name =~ s/.*://;
13 0 0         if(@_){
14 0           $self->{$name} = shift;
15             }
16 0           return $self->{$name};
17             }
18            
19             sub new{
20 0     0 0   my $self = shift;
21 0   0       my $class = ref $self || $self;
22 0           my $this = bless {}, $class;
23 0           return $this;
24             }
25            
26             sub process{
27 0     0 0   my $this = shift;
28 0           my $jar = $this->jar;
29 0 0         die "Cannot find Jar file $jar\n" unless -e $jar;
30            
31 0           (my $base = $jar) =~ s/\.jar$//;
32            
33 0           my $signed_jar = $this->signed_jar;
34 0 0         unless( $signed_jar ){
35 0           $this->signed_jar("$base.signed.jar");
36             }
37            
38 0           my $policy_file = "$base.policy";
39 0           $this->policy_file($policy_file);
40            
41 0 0         if( -e $policy_file ){
42 0           warn sprintf("Adding policy file %s to Jar %s\n",$policy_file,$jar);
43 0           $this->add_policy_file;
44             }
45             else {
46 0           my @files = `jar -tf $jar`;
47 0           my $base_policy = basename($policy_file);
48 0 0         unless( grep( /^${base_policy}$/, @files) ){
49 0           die sprintf("Cannot find policy file '%s' for this Jar file and the Jar does not appear to contain it.\nA policy file for this Jar might look something like:\n\n%s\n"
50             , $this->policy_file
51             , $this->_demo_policy_file
52             );
53             }
54             }
55            
56 0 0         $this->generate_keystore unless -e $this->keystore;
57 0           my $alias = $this->alias;
58 0 0         $this->generate_cert unless -e "$alias.cert";
59 0 0         $this->generate_fingerprint unless -e "$alias.fingerprint";
60            
61 0           $this->generate_signed_jar;
62 0           return;
63             }
64            
65             sub add_policy_file{
66 0     0 0   my $this = shift;
67 0           local $CWD = dirname($this->jar);
68 0           my $jar = basename($this->jar);
69 0           my $policy_file = basename($this->policy_file);
70 0 0         system('jar','-uf',$jar,$policy_file) == 0 or die $!;
71 0           return;
72             }
73            
74             sub generate_signed_jar{
75 0     0 0   my $this = shift;
76 0           my $jar = $this->jar;
77 0           my $signed_jar = $this->signed_jar;
78 0           my $keystore = $this->keystore;
79 0           my $alias = $this->alias;
80 0           my $base_alias = basename $alias;
81             # jar -> signedjar
82 0           @cmd = ( 'jarsigner', '-keystore', $keystore, '-signedjar', $signed_jar, $jar, $base_alias );
83 0 0         system(@cmd) == 0 or die $!;
84 0           return;
85             }
86            
87             sub generate_fingerprint{
88 0     0 0   my $this = shift;
89 0           my $keystore = $this->keystore;
90 0           my $alias = $this->alias;
91 0 0         system("keytool -printcert -file $alias.cert > $alias.fingerprint") == 0 or die $!;
92 0           return;
93             }
94            
95             sub generate_cert{
96 0     0 0   my $this = shift;
97 0           my $keystore = $this->keystore;
98 0           my $alias = $this->alias;
99 0           my $base_alias = basename $alias;
100 0           @cmd = ( 'keytool', '-export', '-rfc', '-keystore', $keystore, '-alias', $base_alias, '-file', "$alias.cert" );
101 0 0         system(@cmd) == 0 or die $!;
102 0           return;
103             }
104            
105             sub generate_keystore{
106 0     0 0   my $this = shift;
107 0           my $alias = $this->alias;
108 0           my $base_alias = basename $alias;
109 0           my $keystore = $this->keystore;
110 0           my $dname = $this->dname;
111 0           my @cmd = ( 'keytool', '-genkey', '-alias', $base_alias, '-keystore', $keystore, '-dname', $dname );
112 0 0         system(@cmd) == 0 or die $!;
113 0           return;
114             }
115            
116             sub _demo_policy_file{
117 0     0     my $this = shift;
118 0           my $signed_jar = basename( $this->signed_jar );
119 0           my $keystore = basename( $this->keystore );
120 0           my $policy = <<"EOF";
121             keystore "file:$keystore";
122            
123             grant signedBy "$keystore", codeBase "http://host/path/to/$signed_jar" {
124             permission java.security.AllPermission;
125             permission java.io.FilePermission "<>", "read, write, delete";
126             permission java.lang.RuntimePermission "createClassLoader";
127             };
128            
129             grant signedBy "$keystore", codeBase "file:$signed_jar" {
130             permission java.security.AllPermission;
131             permission java.io.FilePermission "<>", "read, write, delete";
132             permission java.lang.RuntimePermission "createClassLoader";
133             };
134             EOF
135 0           return $policy;
136             }
137            
138             1;
139            
140             __END__