CrlPublish/Config.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 158 CC 19 SCAR 39.8
CC
19
Cov
0%
parseFile
90.0
importAllFiles
72.0
importOldFile
12.0
CRAP 53.5
1 package EJBCA::CrlPublish::Config;
2 use warnings;
3 use strict;
4 #
5 # crlpublish
6 #
7 # Copyright (C) 2014, Kevin Cody-Little <kcody@cpan.org>
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::Config
31  
32 =head1 SYNOPSIS
33  
34 Loads and queries the contents of configuration files.
35  
36 All functions should be called as class methods.
37  
38 =cut
39  
40  
41 ###############################################################################
42 # Library Dependencies
43  
44 use EJBCA::CrlPublish::Logging;
45  
46 our $VERSION = '0.60';
47  
48  
49 ###############################################################################
50 # Config Value Storage and Retrieval
51  
52 =head1 CONFIG VALUE METHODS
53  
54 =cut
55  
56 { # private lexicals begin
57  
58 my %CONFIG = ();
59  
60 =head2 $class->configValue( $section, $name )
61  
62 =head2 $class->configValue( $section, $name, $value )
63  
64 Sets or retrieves a single value in a specific section.
65  
66 =cut
67  
68 sub configValue {
69 my ( $class, $section, $name, $value ) = @_;
70  
71 my $ref = $CONFIG{$section} ||= {};
72  
73 if ( defined $value ) {
74 $ref->{$name} = $value;
75 }
76  
77 return $ref->{$name};
78 }
79  
80 =head2 $class->applySection( $section, $target )
81  
82 Applies each attribute in the given section to the given target, by calling
83 $target->attributeName( attributeValue ).
84  
85 =cut
86  
87 sub applySection {
88 my ( $class, $section, $target ) = @_;
89  
90 my $ref = $CONFIG{$section}
91 or return;
92  
93 foreach my $name ( keys %$ref ) {
94 my $value = $ref->{$name};
95 $target->$name( $value );
96 }
97  
98 }
99  
100 } # private lexicals end
101  
102  
103 ###############################################################################
104  
105 =head1 DATA IMPORT METHOD
106  
107 =head2 $class->importAllFiles( $directory )
108  
109 Reads all of the config files in the given directory and stores their
110 contents using the configValue method described above.
111  
112 =cut
113  
114 sub importAllFiles {
115 my ( $class, $path ) = @_;
116 my $dh;
117  
118 return 1 unless -d $path;
119  
120 unless ( opendir $dh, $path ) {
121 msgError "opendir($path): $!";
122 return undef;
123 }
124  
125 while ( my $dirent = readdir $dh ) {
126 next if $dirent =~ /^\./;
127  
128 my $ffmt = 'unknown';
129 $ffmt = 'old' if $dirent =~ /\.conf$/;
130 $ffmt = 'new' if $dirent =~ /\.cfg$/;
131  
132 my $file = $path . '/' . $dirent;
133 my $fcfg = $class->parseFile( $file );
134  
135 if ( $ffmt eq 'old' ) {
136 $class->importOldFile( $fcfg->{_} )
137 or return undef;
138 }
139  
140 elsif ( $ffmt eq 'new' ) {
141 $class->importNewFile( $fcfg )
142 or return undef;
143 }
144  
145 else {
146 msgError "Unknown config file format '$dirent'.";
147 return undef;
148 }
149  
150 }
151  
152 closedir $dh;
153  
154 }
155  
156 sub importOldFile {
157 my ( $class, $fcfg ) = @_;
158  
159 my $sec;
160  
161 if ( $sec = $fcfg->{section} ) {
162 delete $fcfg->{section};
163 }
164  
165 elsif ( $sec = $fcfg->{issuerDn} ) {
166 delete $fcfg->{issuerDn};
167 }
168  
169 else {
170 msgError "No issuerDn found in an old style .conf file.";
171 return 0;
172 }
173  
174 foreach my $name ( keys %$fcfg ) {
175 $class->configValue( $sec, $name, $fcfg->{$name} );
176 }
177  
178 return 1;
179 }
180  
181 sub importNewFile {
182 my ( $class, $fcfg ) = @_;
183  
184 foreach my $sec ( keys %{$fcfg} ) {
185 foreach my $name ( keys %{$fcfg->{$sec}} ) {
186 $class->configValue( $sec, $name,
187 $fcfg->{$sec}->{$name} );
188 }
189 }
190  
191 return 1;
192 }
193  
194 sub parseFile {
195 my ( $class, $file ) = @_;
196 my $fh;
197  
198 unless ( open $fh, '< ' . $file ) {
199 msgError "open($file): $!";
200 return undef;
201 }
202  
203 my %rv;
204 my $sec = '_';
205  
206 while ( my $txt = <$fh> ) {
207 chomp( $txt );
208 $txt =~ s/#.*//;
209 $txt =~ s/^\s+//;
210 $txt =~ s/\s+$//;
211 next unless $txt;
212  
213 if ( $txt =~ /^\s*\[.*\]\s*$/ ) {
214 $txt =~ s/^\s*\[//;
215 $txt =~ s/\]\s*$//;
216 $sec = $txt;
217 next;
218 }
219  
220 my ( $var, $val ) = split /\s*=\s*/, $txt, 2;
221  
222 if ( $val =~ /^'/ ) {
223 $val =~ s/^'//;
224 $val =~ s/'$//;
225 }
226  
227 elsif ( $val =~ /^"/ ) {
228 $val =~ s/^"//;
229 $val =~ s/"$//;
230 }
231  
232 if ( $var eq 'remoteLocation' ) {
233 my @part = split /\//, $val;
234 my $file = pop @part;
235 my $path = join ( '/', @part );
236 $rv{$sec}->{remotePath} = $path;
237 $rv{$sec}->{remoteFile} = $file;
238 }
239  
240 elsif ( $var eq 'privateKey' ) {
241 $rv{$sec}->{privateKeyFile} = $val;
242 }
243  
244 elsif ( $var eq 'publisher' ) {
245 $rv{$sec}->{publishMethod} = $val;
246 }
247  
248 else {
249 $rv{$sec}->{$var} = $val;
250 }
251  
252 }
253  
254 close( $fh );
255  
256 return \%rv;
257 }
258  
259  
260 ###############################################################################
261  
262 =head1 AUTHOR
263  
264 Kevin Cody-Little <kcody@cpan.org>
265  
266 =cut
267  
268  
269 ###############################################################################
270 ####################################### EOF ###################################
271 ###############################################################################
272 1;