script/sslmaker

filter: Statementstmt 81.2% 108 / 133 Branchbran 36.5% 19 / 52 Conditioncond 27.5% 8 / 29 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 80.6% 25 / 31 Podpod n/a totaltotal 61.0% 160 / 262 CC 42 SCAR 19.5
CC
42
Cov
63%
home
29.4
__ANON__
12.4
subcommand_dhparam
12.0
CRAP 7.0
↑ Summary blib/lib/App/sslmaker.pm »
1 #!/usr/bin/env perl
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.0
3 1 ▶ $ENV{SSLMAKER_DEBUG} //= (grep {/^--silent/} @ARGV) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep({/^--silent/;} @ARGV) ? : 1 0 1
Condition50%1/2
$ENV{"SSLMAKER_DEBUG"} //= grep({/^--silent/;} @ARGV) ? 0 : 1
l!l
01
MC/DC0%0/1
$ENV{"SSLMAKER_DEBUG"} //= grep({/^--silent/;} @ARGV) ? 0 : 1
$ENV{"SSLMAKER_DEBUG"}
Truth table50%1/2
A
A= $ENV{"SSLMAKER_DEBUG"}
Aresult
11
00
4 }
5  
6 package App::sslmaker::script;
7 1 ▶use Getopt::App -complete;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use if -e 'lib/App/sslmaker.pm', qw(lib lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use App::sslmaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Spec::Functions qw(catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 my @options = (
15 'ext=s@ # Add extensions such as "subjectAltName=DNS:example.com" (default)',
16 'subject=s # /C=US/ST=Texas/L=Dallas/O=Company/OU=Department/CN=example.com/emailAddress=user@domain',
17 'bits=s # SSL key bit size',
18 'days=s # Number of days the cert should be valid',
19 'home=s # sslmaker working directory',
20 'root=s # Path to root key.pem. Required for making intermediate key+cert',
21 'silent # Only output data on failure',
22 'h|help # Show this help text',
23 );
24  
25 9 ▶my $wrapper = sub {
Subroutine __ANON__: called CC=5 SCAR=25.2
26 9 my ($sslmaker, $method, @args) = @_;
27  
28 9 ▶ eval { return $ENV{OPENSSL_CONF} ? $sslmaker->$method(@args) : $sslmaker->with_config($method, @args); } or do {
Branch: 2 branches
BranchTrueFalseTotal
eval { do { return $ENV{"OPENSSL_CONF"} ? $sslmaker->$method(@args) : $sslmaker->with_config($method, @args) } } or do { $@ =~ s/\s+at\s+\S+\s+line.*/\n/su unless $ENV{"TRACE"}; print STDERR $@; exit($? || $!) } 9 0 9
$ENV{'OPENSSL_CONF'} ? : 0 9 9
29 0 ▶ $@ =~ s/\s+at\s+\S+\s+line.*/\n/s unless $ENV{TRACE};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"TRACE"} 0 0 0
30 0 print STDERR $@;
31 0 ▶ exit($? || $!);
Condition0%0/3
$? || $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$? || $!
$? $!
Truth table0%0/3
A || B
A= $?B= $!
ABresult
1X1
011
000
32 };
33 1 };
34  
35 0 ▶sub getopt_subcommands {
Subroutine getopt_subcommands: not called CC=2 SCAR=11.6
36 0 my ($self) = @_;
37  
38 1 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 0 ▶ return undef if $Getopt::App::SUBCOMMAND;
Branch: 1 branch
BranchTrueFalseTotal
if $Getopt::App::SUBCOMMAND 0 0 0
40  
41 return [
42 [generate => run(@options, $self->can('subcommand_generate')), 'Generate a client certificate'],
43 [sign => run(@options, $self->can('subcommand_sign')), 'Sign a certificate'],
44 [revoke => run(@options, $self->can('subcommand_revoke')), 'Revoke a certificate'],
45 [intermediate => run(@options, $self->can('subcommand_intermediate')), 'Generate intermediate CA'],
46 [root => run(@options, $self->can('subcommand_root')), 'Generate root CA'],
47 [dhparam => run(@options, $self->can('subcommand_dhparam')), 'Create a dhparam file'],
48 [nginx => run(@options, $self->can('subcommand_nginx')), 'Print example nginx config'],
49 0 ▶ [man => run(@options, sub { exec perldoc => 'App::sslmaker' }), 'Show the sslmaker manual'],
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 ];
51 }
52  
53 14 ▶sub home {
Subroutine home: called CC=6 SCAR=33.8
54 14 ▶ return $_[0]->{home} if ref $_[0]->{home}; # Built
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"home"} 14 0 14
55  
56 0 my ($self) = @_;
57 0 ▶ my $home = $self->{home} || $ENV{SSLMAKER_HOME};
Condition0%0/3
$self->{'home'} || $ENV{'SSLMAKER_HOME'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'home'} || $ENV{'SSLMAKER_HOME'}
$self->{'home'} $ENV{'SSLMAKER_HOME'}
Truth table0%0/3
A || B
A= $self->{'home'}B= $ENV{'SSLMAKER_HOME'}
ABresult
1X1
011
000
58 0 ▶ return $self->{home} = Path::Tiny->new($home)->absolute if $home;
Branch: 1 branch
BranchTrueFalseTotal
if $home 0 0 0
59 0 ▶ -w "/etc/$_" && return ($self->{home} = Path::Tiny->new("/etc/$_/sslmaker")->absolute) for qw(pki ssl);
Branch: 1 branch
BranchTrueFalseTotal
-w "/etc/$_" and return $self->{"home"} = "Path::Tiny"->new("/etc/$_/sslmaker")->absolute 0 0 0
60 0 die "Cannot detect default --home. Maybe you have to run as root?\n";
61 }
62  
63 12 ▶sub root {
Subroutine root: called CC=3 SCAR=11.4
64 12 ▶ return $_[0]->{root} if ref $_[0]->{root}; # Built
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"root"} 11 1 12
65  
66 1 my ($self) = @_;
67 1 ▶ return $self->{root} = Path::Tiny->new($self->{root} || $self->home->child(qw(root ca.key.pem)))->absolute;
Condition33%1/3
$self->{'root'} || $self->home->child('root', 'ca.key.pem')
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{'root'} || $self->home->child('root', 'ca.key.pem')
$self->{'root'} $self->home->child('root', 'ca.key.pem')
Truth table33%1/3
A || B
A= $self->{'root'}B= $self->home->child('root', 'ca.key.pem')
ABresult
1X1
011
000
68 }
69  
70 2 ▶sub subcommand_revoke {
Subroutine subcommand_revoke: called CC=4 SCAR=14.3
71 2 my ($self, $cert) = @_;
72 2 ▶ return print extract_usage if $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"h"} 0 2 2
73 2 ▶ die "Usage: sslmaker revoke /path/to/file.cert\n" unless $cert;
Branch: 1 branch
BranchTrueFalseTotal
unless $cert 0 2 2
74  
75 2 my $sslmaker = App::sslmaker->new;
76 2 my $passphrase = $self->home->child(qw(private passphrase));
77  
78 2 ▶ $sslmaker->$wrapper(
Branch: 1 branch
BranchTrueFalseTotal
-e $passphrase ? : 2 0 2
79 revoke_cert => {
80 cert => $self->home->child(qw(certs ca.cert.pem)),
81 key => $self->home->child(qw(private ca.key.pem)),
82 passphrase => -e $passphrase ? $passphrase : undef,
83 revoke => $cert,
84 }
85 );
86  
87 2 return 0;
88 }
89  
90 1 ▶sub subcommand_root {
Subroutine subcommand_root: called CC=6 SCAR=18.3
91 1 my ($self) = @_;
92 1 ▶ return print extract_usage if $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"h"} 0 1 1
93 1 ▶ die "--subject is required\n" unless $self->{subject};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"subject"} 0 1 1
94  
95 my $args = {
96 bits => $self->{bits} || 8192,
97 cert => $self->_root_file('cert'),
98 1 ▶ days => $self->{days} || 365 * 30,
Condition50%2/4
$self->{'bits'} || 8192
l!l
01
$self->{'days'} || 10950
l!l
01
MC/DC0%0/1
$self->{'bits'} || 8192
$self->{'bits'}
MC/DC0%0/1
$self->{'days'} || 10950
$self->{'days'}
Truth table50%1/2
A
A= $self->{'bits'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'days'}
Aresult
11
00
99 home => $self->root->parent,
100 key => $self->_root_file('key'),
101 passphrase => $self->root->parent->child(qw(passphrase)),
102 };
103  
104 1 my $sslmaker = App::sslmaker->new;
105 1 $sslmaker->_d('# Root CA settings');
106 1 $sslmaker->_d(sprintf '- %-12s %s', "$_:", "$args->{$_}") for sort keys %$args;
107 1 $sslmaker->subject($self->{subject});
108 1 $sslmaker->make_directories({home => $args->{home}, templates => 1});
109 1 ▶ $self->run_maybe($args->{key}, sub { $sslmaker->$wrapper(make_key => $args) });
Subroutine __ANON__: called CC=1 SCAR=0.0
110 1 ▶ $self->run_maybe($args->{cert}, sub { $sslmaker->$wrapper(make_cert => $args) });
Subroutine __ANON__: called CC=1 SCAR=0.0
111 }
112  
113 0 ▶sub subcommand_dhparam {
Subroutine subcommand_dhparam: not called CC=3 SCAR=24.8
114 0 my ($self, $path, $bits) = @_;
115 0 ▶ return print extract_usage if $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"h"} 0 0 0
116 0 ▶ $path ||= $self->home->child('dhparam.pem');
Condition0%0/3
$path ||= $self->home->child("dhparam.pem")
l!l&&r!l&&!r
000
MC/DC0%0/2
$path ||= $self->home->child("dhparam.pem")
$path $self->home->child("dhparam.pem")
Truth table0%0/3
A ||= B
A= $pathB= $self->home->child("dhparam.pem")
ABresult
1X1
011
000
117 0 ▶ $self->run_maybe($path, sub { App::sslmaker::openssl(qw(dhparam -out) => $path, $bits || 2048) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$bits || 2048
l!l
00
MC/DC0%0/1
$bits || 2048
$bits
Truth table0%0/2
A
A= $bits
Aresult
11
00
118 }
119  
120 2 ▶sub subcommand_generate {
Subroutine subcommand_generate: called CC=5 SCAR=16.5
121 2 my ($self, $cn) = @_;
122 2 ▶ return print extract_usage if $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"h"} 0 2 2
123 2 ▶ die "Usage: sslmaker generate <name>\n" unless $cn;
Branch: 1 branch
BranchTrueFalseTotal
unless $cn 0 2 2
124  
125 my $args = {
126 bits => $self->{bits},
127 csr => "$cn.csr.pem",
128 days => $self->{days},
129 2 ▶ ext => $self->{ext} || ["subjectAltName=DNS:$cn"],
Condition50%1/2
$self->{'ext'} || ["subjectAltName=DNS:$cn"]
l!l
02
MC/DC0%0/1
$self->{'ext'} || ["subjectAltName=DNS:$cn"]
$self->{'ext'}
Truth table50%1/2
A
A= $self->{'ext'}
Aresult
11
00
130 home => $self->home,
131 key => "$cn.key.pem",
132 subject => "/CN=$cn",
133 };
134  
135 2 my $sslmaker = App::sslmaker->new;
136 2 my $intermediate_cert = $self->home->child(qw(certs ca.cert.pem));
137 2 ▶ $sslmaker->subject(-r $intermediate_cert ? ($intermediate_cert, $self->{subject}) : ($self->{subject}));
Branch: 1 branch
BranchTrueFalseTotal
-r $intermediate_cert ? : 2 0 2
138 2 ▶ $self->run_maybe($args->{key}, sub { $sslmaker->make_key($args) });
Subroutine __ANON__: called CC=1 SCAR=0.0
139 2 ▶ $self->run_maybe($args->{csr}, sub { $sslmaker->make_csr($args) });
Subroutine __ANON__: called CC=1 SCAR=0.0
140 2 $sslmaker->_d("# It is safe to send $args->{csr} to SSL admin for signing.");
141 }
142  
143 1 ▶sub subcommand_intermediate {
Subroutine subcommand_intermediate: called CC=5 SCAR=16.2
144 1 my ($self) = @_;
145 1 ▶ return print extract_usage if $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"h"} 0 1 1
146 1 ▶ $self->{bits} ||= 8192;
Condition50%1/2
$self->{"bits"} ||= 8192
l!l
01
MC/DC0%0/1
$self->{"bits"} ||= 8192
$self->{"bits"}
Truth table50%1/2
A
A= $self->{"bits"}
Aresult
11
00
147  
148 1 my $home = $self->home;
149 my $args = {
150 bits => $self->{bits},
151 ca_cert => $self->_root_file('cert'),
152 ca_key => $self->_root_file('key'),
153 cert => $home->child(qw(certs ca.cert.pem)),
154 csr => $home->child(qw(certs ca.csr.pem)),
155 1 ▶ days => $self->{days} || 365 * 28,
Condition50%1/2
$self->{'days'} || 10220
l!l
01
MC/DC0%0/1
$self->{'days'} || 10220
$self->{'days'}
Truth table50%1/2
A
A= $self->{'days'}
Aresult
11
00
156 extensions => 'v3_ca',
157 home => $home,
158 key => $home->child(qw(private ca.key.pem)),
159 passphrase => $home->child(qw(private passphrase)),
160 };
161  
162 1 my $sslmaker = App::sslmaker->new;
163 1 $sslmaker->_d('# Intermediate CA settings');
164 1 $sslmaker->_d(sprintf '- %-12s %s', "$_:", "$args->{$_}") for sort keys %$args;
165 1 $sslmaker->_d('');
166 1 $sslmaker->subject($args->{ca_cert}, $self->{subject});
167 1 $sslmaker->make_directories({home => $home, templates => 1});
168 1 ▶ $self->run_maybe($args->{key}, sub { $sslmaker->$wrapper(make_key => $args) });
Subroutine __ANON__: called CC=1 SCAR=0.0
169 1 ▶ $self->run_maybe($args->{csr}, sub { $sslmaker->$wrapper(make_csr => $args) });
Subroutine __ANON__: called CC=1 SCAR=0.0
170  
171 1 $args->{home} = $self->root->parent;
172 1 $args->{passphrase} = $self->root->parent->child(qw(passphrase));
173 1 ▶ $self->run_maybe($args->{cert}, sub { $sslmaker->$wrapper(sign_csr => $args) });
Subroutine __ANON__: called CC=1 SCAR=0.0
174  
175 1 $args->{chain_cert} = $home->child(qw(certs ca-chain.cert.pem));
176 1 $sslmaker->_cat(@$args{qw( cert ca_cert chain_cert )});
177 1 $sslmaker->_d("# Generated $args->{chain_cert} from CA and intermediate certificate");
178  
179 $sslmaker->openssl(
180 verify => -CAfile => @$args{qw( ca_cert cert )},
181 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
182 1 my ($sslmaker, $output) = @_;
183 1 ▶ die $output if $output =~ /error/;
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ /error/u 0 1 1
184 }
185 1 );
186  
187 1 return 0;
188 }
189  
190 0 ▶sub subcommand_nginx {
Subroutine subcommand_nginx: not called CC=3 SCAR=24.8
191 0 my ($self, $domain) = @_;
192 0 ▶ return print extract_usage if $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"h"} 0 0 0
193 0 ▶ die "Usage: sslmaker nginx <domain>\n" unless $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain 0 0 0
194  
195 0 print +App::sslmaker->_render_template(
196 'nginx.config',
197 {
198 domain => $domain,
199 key => "/etc/nginx/ssl/$domain.key.pem",
200 cert => "/etc/nginx/ssl/$domain.cert.pem",
201 ca_cert => $self->home->child(qw(certs ca-chain.cert.pem)),
202 },
203 );
204  
205 0 return 0;
206 }
207  
208 2 ▶sub subcommand_sign {
Subroutine subcommand_sign: called CC=4 SCAR=14.2
209 2 my ($self, $csr, $cert) = @_;
210 2 my $home = $self->home;
211 2 ▶ return print extract_usage if $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"h"} 0 2 2
212 2 ▶ die "Usage: sslmaker sign <csr> [cert]\n" unless $csr;
Branch: 1 branch
BranchTrueFalseTotal
unless $csr 0 2 2
213  
214 2 ▶ $cert ||= do { local $_ = $csr; s!(\.csr)?\.pem$!\.cert.pem!; $_ };
Condition33%1/3
$cert ||= do { local $_ = $csr; s/(\.csr)?\.pem$/.cert.pem/u; $_ }
l!l&&r!l&&!r
020
MC/DC0%0/2
$cert ||= do { local $_ = $csr; s/(\.csr)?\.pem$/.cert.pem/u; $_ }
$cert do { local $_ = $csr; s/(\.csr)?\.pem$/.cert.pem/u; $_ }
Truth table33%1/3
A ||= B
A= $certB= do { local $_ = $csr; s/(\.csr)?\.pem$/.cert.pem/u; $_ }
ABresult
1X1
011
000
215 2 my $sslmaker = App::sslmaker->new;
216 $sslmaker->$wrapper(
217 sign_csr => {
218 home => $home,
219 ca_cert => $home->child(qw(certs ca.cert.pem)),
220 ca_key => $home->child(qw(private ca.key.pem)),
221 cert => $cert,
222 csr => $csr,
223 days => $self->{days},
224 2 extensions => 'usr_cert',
225 passphrase => $home->child(qw(private passphrase)),
226 }
227 );
228  
229 2 $sslmaker->_d("# Generated $cert");
230 2 $sslmaker->_d("# Run this command for more details: openssl x509 -in $cert -noout -text");
231 }
232  
233 9 ▶sub run_maybe {
Subroutine run_maybe: called CC=2 SCAR=7.0
234 9 my ($self, $file, $cb) = @_;
235 9 ▶ return App::sslmaker->_d("! File $file exists.") if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 0 9 9
236 9 $self->$cb;
237 9 App::sslmaker->_d("# Generated $_[1]");
238 }
239  
240 4 ▶sub _root_file {
Subroutine _root_file: called CC=1 SCAR=0.0
241 4 my ($self, $ext) = @_;
242 4 my $base = $self->root->basename;
243 4 $base =~ s!\b(cert|key)\b!$ext!;
244 4 return $self->root->parent->child($base);
245 }
246  
247 run(
248 @options,
249 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
250 0 my ($self) = @_;
251 0 ▶ return print extract_usage if $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"h"} 0 0 0
252 0 return print extract_usage;
253 },
254 1 );
↑ Summary blib/lib/App/sslmaker.pm »