blib/lib/Monitoring/Sneck/Checks/cert_file.pm

filter: Statementstmt 98.9% 98 / 99 Branchbran 100% 38 / 38 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 95.2% 161 / 169 CC 27 SCAR 25.0
CC
27
Cov
97%
check
19.0
key_status
9.0
__ANON__
1.1
CRAP 12.2
« blib/lib/Monitoring/Sneck/Checks/neighbors.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/file_mode.pm »
1 package Monitoring::Sneck::Checks::cert_file;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Fcntl qw(S_IROTH);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::cert_file - checks certificate files
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 3 our $VERSION = '0.0.1';
20  
21 =head1 SYNOPSIS
22  
23 www_cert_file|perl:cert_file -f /usr/local/etc/ssl/www.pem -k /usr/local/etc/ssl/www.key -n www.example.org
24 ca_files|perl:cert_file -f /usr/local/etc/ssl/ca.pem -f /usr/local/etc/ssl/intermediate.pem -w 90 -c 30
25  
26 =head1 DESCRIPTION
27  
28 Checks every certificate in each PEM file, so a chain file has its
29 intermediates checked too. A file that can not be read or has no
30 certificates is CRITICAL, as is a expired one or one not good yet. Then
31 it is WARNING or CRITICAL by the days left.
32  
33 --key and --name are checked against the first certificate that
34 parses, in the order the files are given. A key that does not match is CRITICAL, as is one that is
35 encrypted, as there is no passphrase to give it. A key readable by
36 everyone is WARNING.
37  
38 =head1 OPTIONS
39  
40 - -f, --file :: A PEM file. May be given more than once. Required.
41  
42 - -k, --key :: The PEM private key for the first certificate.
43  
44 - -n, --name :: A name the first certificate must cover. May be given
45 more than once.
46  
47 - -w, --warning :: Days left at or under which it is WARNING.
48 Default :: 30
49  
50 - -c, --critical :: Days left at or under which it is CRITICAL.
51 Default :: 14
52  
53 =cut
54  
55 16 ▶sub option_spec { return ( 'file|f=s@', 'key|k=s', 'name|n=s@', 'warning|w=i', 'critical|c=i' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
56  
57 16 ▶sub defaults { return { warning => 30, critical => 14 } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
58  
59 16 ▶sub required { return ('file') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
60  
61 =head1 METHODS
62  
63 =head2 check
64  
65 Does the check.
66  
67 =cut
68  
69 15 ▶sub check {
Subroutine check: called CC=19 SCAR=29.4
Pod documented
70 15 my ($self) = @_;
71  
72 15 require IO::Socket::SSL;
73 15 require IO::Socket::SSL::Utils;
74 15 my @codes;
75 my @problems;
76 15 my @lines;
77 15 my $first;
78 15 my $first_message;
79 15 my $fewest_days;
80  
81 15 foreach my $file ( @{ $self->opt('file') } ) {
82 15 my $certs = eval { $self->pem_certs($file) };
83 15 ▶ if ( !defined($certs) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $certs) 1 14 15
84 1 my $why = $@;
85 1 $why =~ s/\s+\z//;
86 1 push( @codes, $self->CRITICAL );
87 1 push( @problems, $why );
88 1 next;
89 }
90 14 ▶ if ( !scalar( @{$certs} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$certs;}) 1 13 14
91 1 push( @codes, $self->CRITICAL );
92 1 push( @problems, 'no certificates in ' . $file );
93 1 next;
94 }
95  
96 13 my $number = 0;
97 13 foreach my $cert ( @{$certs} ) {
98 15 $number++;
99 15 ▶ my $where = $file . ( scalar( @{$certs} ) > 1 ? ' #' . $number : '' );
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$certs;} > 1 ? : 4 11 15
100 15 ▶ if ( !$cert ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cert) 1 14 15
101 1 push( @codes, $self->CRITICAL );
102 1 push( @problems, $where . ' can not be parsed' );
103 1 next;
104 }
105 14 my $details = $self->cert_details($cert);
106 14 my ( $code, $message ) = $self->cert_status( $details, $self->opt('warning'), $self->opt('critical') );
107 14 push( @codes, $code );
108 14 push( @lines, $where . ' ' . $message );
109 14 ▶ $fewest_days = $details->{days_left} if !defined($fewest_days) || $details->{days_left} < $fewest_days;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fewest_days or $details->{"days_left"} < $fewest_days 12 2 14
Condition66%2/3
not defined $fewest_days or $details->{"days_left"} < $fewest_days
l!l&&r!l&&!r
1202
MC/DC50%1/2
not defined $fewest_days or $details->{"days_left"} < $fewest_days
not defined $fewest_days $details->{"days_left"} < $fewest_days
Truth table66%2/3
not(A) or B
A= not defined $fewest_daysB= $details->{"days_left"} < $fewest_days
ABresult
1X1
011
000
110 14 ▶ if ( !defined($first) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $first) 12 2 14
111 12 $first = $cert;
112 12 $first_message = $where . ' ' . $message;
113 12 next;
114 }
115 2 ▶ push( @problems, $where . ' ' . $message ) if $code != $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code != $self->OK 1 1 2
116 2 IO::Socket::SSL::Utils::CERT_free($cert);
117 } ## end foreach my $cert ( @{$certs} )
118 } ## end foreach my $file ( @{ $self->opt('file') } )
119  
120 15 ▶ if ( defined($first) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first) 12 3 15
121 12 ▶ foreach my $name ( @{ $self->opt('name') // [] } ) {
Condition100%2/2
$self->opt("name") // []
l!l
210
MC/DC100%1/1
$self->opt("name") // []
$self->opt("name")
Truth table100%2/2
A
A= $self->opt("name")
Aresult
11
00
122 3 ▶ next if IO::Socket::SSL::verify_hostname_of_cert( $name, $first, 'http' );
Branch: 1 branch
BranchTrueFalseTotal
if IO::Socket::SSL::verify_hostname_of_cert($name, $first, "http") 2 1 3
123 1 push( @codes, $self->CRITICAL );
124 1 push( @problems, 'does not cover ' . $name );
125 }
126 12 ▶ if ( defined( $self->opt('key') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("key")) 5 7 12
127 5 my ( $code, $problem ) = $self->key_status( $first, $self->opt('key') );
128 5 push( @codes, $code );
129 5 ▶ push( @problems, $problem ) if defined($problem);
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 4 1 5
130 }
131 12 IO::Socket::SSL::Utils::CERT_free($first);
132 } ## end if ( defined($first) )
133  
134 15 ▶ if ( defined($fewest_days) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fewest_days) 12 3 15
135 12 $self->perfdata(
136 label => 'days',
137 value => $fewest_days,
138 warning => $self->opt('warning'),
139 critical => $self->opt('critical')
140 );
141 }
142 15 my $message = join( ', ', grep { defined($_) } ( $first_message, @problems ) );
143 15 ▶ @lines = () if scalar(@lines) < 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @lines < 2 13 2 15
144  
145 15 return $self->finish( $self->worst(@codes), $message, @lines );
146 } ## end sub check
147  
148 =head2 key_status
149  
150 Checks a private key against a certificate.
151  
152 - cert :: The Net::SSLeay X509.
153  
154 - key :: The key file.
155  
156 Returns the code and what is wrong, undef if nothing.
157  
158 my ( $code, $problem ) = $check->key_status( $cert, '/usr/local/etc/ssl/www.key' );
159  
160 =cut
161  
162 5 ▶sub key_status {
Subroutine key_status: called CC=9 SCAR=22.0
Pod documented
163 5 my ( $self, $cert, $key ) = @_;
164  
165 5 require Net::SSLeay;
166 5 my @stat = stat($key);
167 5 ▶ if ( !defined( $stat[0] ) || !-r $key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stat[0] and -r $key) 1 4 5
Condition66%2/3
defined $stat[0] and -r $key
!ll&&!rl&&r
104
MC/DC50%1/2
defined $stat[0] and -r $key
defined $stat[0] -r $key
Truth table66%2/3
A and B
A= defined $stat[0]B= -r $key
ABresult
0X0
100
111
168 1 ▶ return ( $self->CRITICAL, 'can not read ' . $key . ', ' . ( $! || 'permission denied' ) );
Condition50%1/2
$! || 'permission denied'
l!l
10
MC/DC0%0/1
$! || 'permission denied'
$!
Truth table50%1/2
A
A= $!
Aresult
11
00
169 }
170  
171 # the key first, as loading a certificate that does not match a key already there drops the key
172 4 my $context = Net::SSLeay::CTX_new();
173 0 ▶ Net::SSLeay::CTX_set_default_passwd_cb( $context, sub { return '' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
174 4 my $loaded = Net::SSLeay::CTX_use_PrivateKey_file( $context, $key, Net::SSLeay::FILETYPE_PEM() );
175 4 my $matches;
176 4 ▶ if ($loaded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loaded) 3 1 4
177 3 Net::SSLeay::CTX_use_certificate( $context, $cert );
178 3 $matches = Net::SSLeay::CTX_check_private_key($context);
179 }
180 4 Net::SSLeay::CTX_free($context);
181 4 ▶ while ( Net::SSLeay::ERR_get_error() ) { }
Branch: 1 branch
BranchTrueFalseTotal
if (Net::SSLeay::ERR_get_error()) 3 4 7
182  
183 4 ▶ return ( $self->CRITICAL, $key . ' is not a key, or is encrypted' ) if !$loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 1 3 4
184 3 ▶ return ( $self->CRITICAL, $key . ' does not match' ) if !$matches;
Branch: 1 branch
BranchTrueFalseTotal
unless $matches 1 2 3
185 2 ▶ return ( $self->WARNING, $key . ' is readable by everyone' ) if $stat[2] & S_IROTH();
Branch: 1 branch
BranchTrueFalseTotal
if $stat[2] & 4 1 1 2
186  
187 1 return ( $self->OK, undef );
188 } ## end sub key_status
189  
190 3 1;
« blib/lib/Monitoring/Sneck/Checks/neighbors.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/file_mode.pm »