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

filter: Statementstmt 100% 41 / 41 Branchbran 100% 14 / 14 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 92.5% 75 / 81 CC 11 SCAR 17.7
CC
11
Cov
95%
check
10.0
validate
2.0
BEGIN
1.0
CRAP 5.9
« blib/lib/Monitoring/Sneck/Checks/swap.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/memory.pm »
1 package Monitoring::Sneck::Checks::selinux;
2  
3 22 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::selinux - checks SELinux is in the mode it should be
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 22 our $VERSION = '0.0.1';
20  
21 # Where selinuxfs is mounted and the config is. Set by tests.
22 22 our $SELINUXFS = '/sys/fs/selinux';
23 22 our $CONFIG = '/etc/selinux/config';
24  
25 =head1 SYNOPSIS
26  
27 selinux|perl:selinux
28 selinux_permissive|perl:selinux -m permissive
29  
30 =head1 DESCRIPTION
31  
32 Checks the mode SELinux is running in, from enforce under
33 /sys/fs/selinux, it being disabled if that is not there, and the mode
34 /etc/selinux/config sets for the next boot.
35  
36 - CRITICAL :: Running in a mode other than --mode.
37  
38 - WARNING :: /etc/selinux/config setting a mode other than the running
39 one, which takes effect at the next boot.
40  
41 - UNKNOWN :: Not being Linux.
42  
43 The policy type, such as targeted, is from SELINUXTYPE in the config.
44  
45 =head1 OPTIONS
46  
47 - -m, --mode :: The mode it should be running in, enforcing,
48 permissive, or disabled.
49 Default :: enforcing
50  
51 =cut
52  
53 7 ▶sub option_spec { return ('mode|m=s') }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
54  
55 7 ▶sub defaults { return { mode => 'enforcing' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
56  
57 7 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
58 7 my ( $self, $options ) = @_;
59  
60 return ('--mode must be enforcing, permissive, or disabled')
61 7 ▶ if $options->{mode} !~ /\A(?:enforcing|permissive|disabled)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"mode"} =~ /\A(?:enforcing|permissive|disabled)\z/u 1 6 7
62  
63 6 return ();
64 }
65  
66 =head1 METHODS
67  
68 =head2 check
69  
70 Does the check.
71  
72 =cut
73  
74 6 ▶sub check {
Subroutine check: called CC=10 SCAR=23.1
Pod documented
75 6 my ($self) = @_;
76  
77 6 ▶ die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'linux';
Branch: 1 branch
BranchTrueFalseTotal
if $self->os ne "linux" 1 5 6
78  
79 5 my $enforce = read_file( $SELINUXFS . '/enforce', err_mode => 'quiet' );
80 5 my $running = 'disabled';
81 5 ▶ $running = ( $enforce =~ /\A\s*1/ ? 'enforcing' : 'permissive' ) if defined($enforce);
Branch: 2 branches
BranchTrueFalseTotal
if defined $enforce 3 2 5
$enforce =~ /\A\s*1/u ? : 1 2 3
82  
83 5 ▶ my $config = read_file( $CONFIG, err_mode => 'quiet' ) // '';
Condition50%1/2
read_file($Monitoring::Sneck::Checks::selinux::CONFIG, "err_mode", "quiet") // ""
l!l
50
MC/DC0%0/1
read_file($Monitoring::Sneck::Checks::selinux::CONFIG, "err_mode", "quiet") // ""
read_file($Monitoring::Sneck::Checks::selinux::CONFIG, "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($Monitoring::Sneck::Checks::selinux::CONFIG, "err_mode", "quiet")
Aresult
11
00
84 5 my ($config_mode) = $config =~ /^\s*SELINUX\s*=\s*"?(\w+)/m;
85 5 my ($type) = $config =~ /^\s*SELINUXTYPE\s*=\s*"?(\w+)/m;
86  
87 5 my @codes = ( $self->OK );
88 5 my @parts = ($running);
89 5 ▶ push( @parts, $type . ' policy' ) if defined($type) && $running ne 'disabled';
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $running ne "disabled" 3 2 5
Condition66%2/3
defined $type and $running ne "disabled"
!ll&&!rl&&r
023
MC/DC50%1/2
defined $type and $running ne "disabled"
defined $type $running ne "disabled"
Truth table66%2/3
A and B
A= defined $typeB= $running ne "disabled"
ABresult
0X0
100
111
90 5 ▶ if ( $running ne $self->opt('mode') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($running ne $self->opt("mode")) 2 3 5
91 2 push( @codes, $self->CRITICAL );
92 2 push( @parts, 'wanted ' . $self->opt('mode') );
93 }
94 5 ▶ if ( defined($config_mode) && lc($config_mode) ne $running ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_mode and lc $config_mode ne $running) 2 3 5
Condition66%2/3
defined $config_mode and lc $config_mode ne $running
!ll&&!rl&&r
032
MC/DC50%1/2
defined $config_mode and lc $config_mode ne $running
defined $config_mode lc $config_mode ne $running
Truth table66%2/3
A and B
A= defined $config_modeB= lc $config_mode ne $running
ABresult
0X0
100
111
95 2 push( @codes, $self->WARNING );
96 2 push( @parts, $CONFIG . ' says ' . lc($config_mode) . ' from the next boot' );
97 }
98  
99 5 return $self->finish( $self->worst(@codes), join( ', ', @parts ) );
100 } ## end sub check
101  
102 22 1;
« blib/lib/Monitoring/Sneck/Checks/swap.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/memory.pm »