CrlPublish/Method/scp.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 143 CC 14 SCAR 32.0
CC
14
Cov
0%
publish
56.0
validate
20.0
publish_pre
12.0
CRAP 24.5
1 package EJBCA::CrlPublish::Method::scp;
2 use warnings;
3 use strict;
4 #
5 # crlpublish
6 #
7 # Copyright (C) 2014, Kevin Cody-Little <kcodyjr@gmail.com>
8 #
9 # Portions derived from crlpublisher.sh, original copyright follows:
10 #
11 # Copyright (C) 2011, Branko Majic <branko@majic.rs>
12 #
13 # This program is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #
26 ###############################################################################
27  
28 =head1 NAME
29  
30 EJBCA::CrlPublish::Method::scp
31  
32 =head1 SYNOPSIS
33  
34 Implements publishing via scp.
35  
36 Updates are atomic; that is, CRLs are transferred to a temporary file and
37 then renamed into place, so there is no period of time that an intact CRL
38 cannot be retrieved from the server.
39  
40 =cut
41  
42  
43 ###############################################################################
44 # Library Dependencies
45  
46 use base 'EJBCA::CrlPublish::Method';
47  
48 use EJBCA::CrlPublish::Logging;
49  
50 our $VERSION = '0.60';
51  
52  
53 ###############################################################################
54 # Implementation
55  
56 sub validate {
57 my $self = shift;
58  
59 $self->argMustExist( qw( crlFile remoteHost remotePath remoteFile ) );
60  
61 my $host = $self->target->remoteHost;
62 my $path = $self->target->remotePath;
63 my $file = $self->target->remoteFile;
64  
65 my $user = $self->target->remoteUser;
66 my $pkey = $self->target->privateKeyFile;
67 my $args = $self->target->scpExtraArgs;
68  
69 my @args = qw( -o BatchMode=yes );
70  
71 if ( $pkey ) {
72 $self->checkFileType( 'SSH private key', $pkey )
73 or return undef;
74 push @args, '-i', $pkey;
75 }
76  
77 if ( $args ) {
78 push @args, split /\s+/, $args;
79 }
80  
81 my $t_host = $user ? $user . '@' : '';
82 $t_host .= $host;
83 my $t_file = $path . '/' . $file;
84 my $t_temp = $t_file . '.new';
85  
86 $self->target->targetArgs( \@args );
87 $self->target->targetHost( $t_host );
88 $self->target->targetFile( $t_file );
89 $self->target->targetTemp( $t_temp );
90  
91 return 1;
92 }
93  
94 sub publish {
95 my $self = shift;
96  
97 unless ( $self->publish_pre ) {
98 msgError "Failed pre-publish sanity check.";
99 return 0;
100 }
101  
102 if ( my $t = $self->target->targetCrlNumber ) {
103 my $u = $self->target->crlInfo->crlNumber;
104 unless ( $u > $t ) {
105 msgDebug "Target already has the newest CRL, skipping.";
106 return 1;
107 }
108 return 1 unless $u > $t;
109 }
110  
111 unless ( $self->publish_scp ) {
112 msgError "Failed to securely copy CRL file.";
113 return 0;
114 }
115  
116 unless ( $self->publish_smv ) {
117 msgError "Failed to activate new CRL file.";
118 return 0;
119 }
120  
121 return 1;
122 }
123  
124 sub publish_pre {
125 my $self = shift;
126  
127 my $args = $self->target->targetArgs;
128 my $t_host = $self->target->targetHost;
129 my $t_file = $self->target->targetFile;
130 my $t_temp = $self->target->targetTemp;
131  
132 my @trucall = ( 'ssh', @$args, $t_host, '/bin/true' );
133  
134 unless ( system( @trucall ) == 0 ) {
135 msgError "Failed to ssh $t_host /bin/true: $?";
136 return 0;
137 }
138  
139 my @sslcall = ( 'ssh', @$args, $t_host,
140 'openssl', 'crl',
141 '-in', $t_file,
142 '-inform', $self->target->crlInfo->crlFormat,
143 '-noout', '-text' );
144  
145 open my $fh, '-|', @sslcall
146 or return 1;
147  
148 my $t = EJBCA::CrlPublish::CrlInfo->retrieve( $fh )
149 or return 1;
150  
151 return 1 unless $t->crlNumber;
152  
153 $self->target->targetCrlNumber( $t->crlNumber );
154  
155 return 1;
156 }
157  
158 sub publish_scp {
159 my $self = shift;
160  
161 my $args = $self->target->targetArgs;
162 my $t_host = $self->target->targetHost;
163 my $t_temp = $self->target->targetTemp;
164  
165 my $source = $self->target->crlFile;
166 my $target = $t_host . ':' . $t_temp;
167  
168 $self->checkFileType( 'CRL file', $source )
169 or return 0;
170  
171 my @scpcall = ( 'scp', @$args, $source, $target );
172  
173 unless ( system( @scpcall ) == 0 ) {
174 msgError "Failed to scp $source $target: $?";
175 return 0;
176 }
177  
178 return 1;
179 }
180  
181 sub publish_smv {
182 my $self = shift;
183  
184 my $args = $self->target->targetArgs;
185 my $t_host = $self->target->targetHost;
186 my $t_temp = $self->target->targetTemp;
187 my $t_file = $self->target->targetFile;
188  
189 my @smvcall = ( 'ssh', @$args, $t_host, 'mv', $t_temp, $t_file );
190  
191 unless ( system( @smvcall ) == 0 ) {
192 msgError "Failed to ssh $t_host mv $t_temp $t_file: $?";
193 return 0;
194 }
195  
196 return 1;
197 }
198  
199  
200 ###############################################################################
201  
202 =head1 AUTHOR
203  
204 Kevin Cody-Little <kcody@cpan.org>
205  
206 =cut
207  
208  
209 ###############################################################################
210 ####################################### EOF ###################################
211 ###############################################################################
212 1;