blib/lib/HealthCheck/Diagnostic/FilePermissions.pm

filter: Statementstmt 99.0% 101 / 102 Branchbran 97.9% 47 / 48 Conditioncond 82.7% 24 / 29 MC/DCmcdc 80.0% 12 / 15 Subroutinesub 100% 14 / 14 Podpod 44.4% 4 / 9 totaltotal 93.0% 202 / 217 CC 41 SCAR 21.2
CC
41
Cov
96%
check
15.0
run
9.0
check_access
9.0
CRAP 8.3
↑ Summary
1 package HealthCheck::Diagnostic::FilePermissions;
2 2 ▶use parent 'HealthCheck::Diagnostic';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # ABSTRACT: Check the paths for expected permissions in a HealthCheck
5 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 our $VERSION = 'v1.4.8'; # VERSION
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 ▶sub new {
Subroutine new: called CC=4 SCAR=18.9
Pod documented
14 22 my ($class, @params) = @_;
15  
16 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
17 0 ▶ ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && (ref $params[0] || '') eq 'HASH' ? : 0 22 22
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
!ll&&!rl&&r
2200
ref $params[0] || ''
l!l
00
MC/DC0%0/2
@params == 1 and (ref $params[0] || "") eq "HASH"
@params == 1 ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
ABresult
0X0
100
111
18  
19 22 return $class->SUPER::new(
20 id => 'file_permissions',
21 label => 'File Permissions',
22 %params,
23 );
24 }
25  
26 52 ▶sub collapse_single_result {
Subroutine collapse_single_result: called CC=3 SCAR=11.0
Pod documented
27 52 my ($self, @args) = @_;
28 return $self->SUPER::collapse_single_result(@args)
29 52 ▶ if ref $self and exists $self->{collapse_single_result};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and exists $self->{"collapse_single_result"} 2 50 52
Condition100%3/3
ref $self and exists $self->{"collapse_single_result"}
!ll&&!rl&&r
8422
MC/DC100%2/2
ref $self and exists $self->{"collapse_single_result"}
ref $self exists $self->{"collapse_single_result"}
Truth table100%3/3
A and B
A= ref $selfB= exists $self->{"collapse_single_result"}
ABresult
0X0
100
111
30  
31 # If we are only checking a single parameter on a single file,
32 # the additional level of results is useless here.
33 50 return 1;
34 }
35  
36 70 ▶sub check {
Subroutine check: called CC=15 SCAR=27.1
Pod documented
37 70 my ($self, %params) = @_;
38  
39 # Make it so that the diagnostic can be used as an instance or a
40 # class, and the `check` params get preference.
41 70 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 52 18 70
42 $params{$_} = $self->{$_}
43 52 foreach grep { ! defined $params{$_} } keys %$self;
44 }
45  
46 # Allow the files to be either an anonymous sub, an array, or a
47 # string of the filename(s).
48 70 ▶ if ( ref $params{files} eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $params{'files'} eq 'CODE') { } 2 68 70
elsif (defined $params{'files'} and ref $params{'files'} ne 'ARRAY') { } 15 54 69
49 2 $params{files} = [ $params{files}->() ];
50 }
51 ▶ elsif ( defined $params{files} and ref $params{files} ne 'ARRAY' ) {
Condition100%3/3
defined $params{"files"} and ref $params{"files"} ne "ARRAY"
!ll&&!rl&&r
15215
MC/DC100%2/2
defined $params{"files"} and ref $params{"files"} ne "ARRAY"
defined $params{"files"} ref $params{"files"} ne "ARRAY"
Truth table100%3/3
A and B
A= defined $params{"files"}B= ref $params{"files"} ne "ARRAY"
ABresult
0X0
100
111
52 # Passed-in strings can only be one file name.
53 15 $params{files} = [ $params{files} ];
54 }
55 70 ▶ croak( 'No files extracted' ) unless @{ $params{files} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$params{"files"} or [];} 3 67 70
Condition100%2/2
$params{'files'} || []
l!l
691
MC/DC100%1/1
$params{'files'} || []
$params{'files'}
Truth table100%2/2
A
A= $params{'files'}
Aresult
11
00
56  
57 # Convert the access parameter into a r/w/x hash if it is not already
58 # a hash. Then convert that into a read/write/execute hash.
59 67 my %access;
60 67 ▶ if ( ref $params{access} ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"access"} ne "HASH") 40 27 67
61 # Everything to the left of the exclamation point is regarded as
62 # expecting an enabled permission. Everything to the right is
63 # regarded as expecting a disabled permission.
64 40 ▶ my ($enabled, $disabled) = split( '!', $params{access} || '' );
Condition100%2/2
$params{'access'} || ''
l!l
2317
MC/DC100%1/1
$params{'access'} || ''
$params{'access'}
Truth table100%2/2
A
A= $params{'access'}
Aresult
11
00
65 $params{access} = {
66 38 ▶ ( map { $_ => 1 } split //, $enabled || '' ),
Condition100%2/2
$enabled || ''
l!l
1624
MC/DC100%1/1
$enabled || ''
$enabled
Truth table100%2/2
A
A= $enabled
Aresult
11
00
67 40 ▶ ( map { $_ => 0 } split //, $disabled || '' ),
Condition100%2/2
$disabled || ''
l!l
1129
MC/DC100%1/1
$disabled || ''
$disabled
Truth table100%2/2
A
A= $disabled
Aresult
11
00
68 };
69 }
70 67 my %map = (
71 r => 'read',
72 read => 'read',
73 w => 'write',
74 write => 'write',
75 x => 'execute',
76 execute => 'execute',
77 );
78 67 ▶ foreach ( keys %{ $params{access} || {} } ) {
Condition50%1/2
$params{'access'} || {}
l!l
670
MC/DC0%0/1
$params{'access'} || {}
$params{'access'}
Truth table50%1/2
A
A= $params{'access'}
Aresult
11
00
79 croak( "Invalid access parameter: $_" )
80 120 ▶ unless defined $map{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $map{$_} 2 118 120
81 118 $access{ $map{$_} } = $params{access}->{$_};
82 }
83 65 $params{access} = \%access;
84  
85 65 return $self->SUPER::check(%params);
86 }
87  
88 65 ▶sub run {
Subroutine run: called CC=9 SCAR=22.0
Pod documented
89 65 my ( $self, %params ) = @_;
90  
91 65 my @results;
92 65 foreach my $file ( @{ $params{files} } ) {
93 76 my @file_results;
94 76 ▶ if ( ! -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 5 71 76
95 # Don't attempt to look for permissions if the file does
96 # not exist.
97 5 push @results, {
98 info => qq{'$file' does not exist},
99 status => 'CRITICAL',
100 };
101 5 next;
102 }
103 push @file_results,
104 $self->check_access( $file, $params{access} )
105 71 ▶ if %{ $params{access} };
Branch: 1 branch
BranchTrueFalseTotal
if %{$params{"access"};} 51 20 71
106 push @file_results,
107 $self->check_permissions( $file, $params{permissions} )
108 71 ▶ if defined $params{permissions};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"permissions"} 9 62 71
109 push @file_results,
110 $self->check_owner( $file, $params{owner} )
111 71 ▶ if defined $params{owner};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"owner"} 4 67 71
112 push @file_results,
113 $self->check_group( $file, $params{group} )
114 71 ▶ if defined $params{group};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"group"} 4 67 71
115  
116 # Add a default result if there were no permissions to check
117 # for the file.
118 71 ▶ push @file_results, {
Branch: 1 branch
BranchTrueFalseTotal
unless @file_results 7 64 71
119 info => qq{'$file' exists},
120 status => 'OK',
121 } unless @file_results;
122  
123 71 push @results, @file_results;
124 }
125  
126 # Construct the main info statement for the check, which consists
127 # of the failed info messages.
128 65 my @info = map { $_->{info} } grep { $_->{status} ne 'OK' } @results;
129  
130 # Use a default success message if no fail info messages were found.
131 push @info, 'Permissions are correct for '.
132 65 ▶ $self->pretty_join( map { qq{'$_'} } @{ $params{files} } )
Branch: 1 branch
BranchTrueFalseTotal
unless @info 26 39 65
133 unless @info;
134  
135 65 return { info => join( '; ', @info ), results => \@results };
136  
137 }
138  
139 57 ▶sub pretty_join {
Subroutine pretty_join: called CC=3 SCAR=11.0
Pod undocumented
140 57 my ($self, @list) = @_;
141  
142 # Join the list items with a command and final 'and'.
143 57 ▶ return $list[0] if @list == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @list == 1 39 18 57
144 18 $list[ $#list ] = 'and '.$list[ $#list ];
145 18 ▶ return join( ' ', @list ) if @list == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @list == 2 12 6 18
146 6 return join( ', ', @list );
147 }
148  
149 51 ▶sub check_access {
Subroutine check_access: called CC=9 SCAR=22.0
Pod undocumented
150 51 my ($self, $file, $access) = @_;
151  
152 # Run the tests and construct the error messages, identifying which
153 # access operation failed.
154 51 my $info = "Permissions for '$file':";
155 51 my @access_errors;
156 51 my %actual = (
157 read => -r $file,
158 write => -w $file,
159 execute => -x $file,
160 );
161 51 foreach ( sort keys %$access ) {
162 51 push @{ $access_errors[ $access->{$_} ] }, $_
163 126 ▶ if $access->{$_} xor $actual{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $access->{$_} xor $actual{$_} 51 75 126
Condition100%4/4
$access->{$_} xor $actual{$_}
l&&rl&&!r!l&&r!l&&!r
44282331
MC/DC100%2/2
$access->{$_} xor $actual{$_}
$access->{$_} $actual{$_}
Truth table100%4/4
A xor B
A= $access->{$_}B= $actual{$_}
ABresult
110
101
011
000
164 }
165  
166 # Return a default success access permission info message if it found
167 # no errors.
168 return {
169 51 ▶ status => 'OK',
Branch: 1 branch
BranchTrueFalseTotal
unless @access_errors 20 31 51
170 info => qq{Have correct access for '$file'},
171 } unless @access_errors;
172  
173 # Summarize the failed info messages in the results.
174 31 my @info;
175 push @info, 'Must have permission to '.
176 18 $self->pretty_join( @{ $access_errors[1] } ).qq{ '$file'}
177 31 ▶ if @{ $access_errors[1] || [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$access_errors[1] or [];} 18 13 31
Condition100%2/2
$access_errors[1] || []
l!l
1813
MC/DC100%1/1
$access_errors[1] || []
$access_errors[1]
Truth table100%2/2
A
A= $access_errors[1]
Aresult
11
00
178 push @info, 'Must not have permission to '.
179 13 $self->pretty_join( @{ $access_errors[0] } ).qq{ '$file'}
180 31 ▶ if @{ $access_errors[0] || [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$access_errors[0] or [];} 13 18 31
Condition100%2/2
$access_errors[0] || []
l!l
1318
MC/DC100%1/1
$access_errors[0] || []
$access_errors[0]
Truth table100%2/2
A
A= $access_errors[0]
Aresult
11
00
181 return {
182 31 status => 'CRITICAL',
183 info => join( '; ', @info ),
184 };
185 }
186  
187 9 ▶sub check_permissions {
Subroutine check_permissions: called CC=2 SCAR=6.9
Pod undocumented
188 9 my ($self, $file, $permissions) = @_;
189  
190 # Stringify the expected and actual permissions so that they can be
191 # easily understood.
192 9 my $actual = sprintf( '%04o', ( stat($file) )[2] & 07777 );
193 9 my $expected = sprintf( '%04o', $permissions & 07777 );
194  
195 return {
196 9 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $expected != $actual 3 6 9
197 info => "Permissions should be $expected but ".
198 "are $actual for '$file'",
199 } if $expected != $actual;
200  
201 return {
202 6 status => 'OK',
203 info => qq{Permissions are $actual for '$file'},
204 };
205 }
206  
207 4 ▶sub check_owner {
Subroutine check_owner: called CC=2 SCAR=6.9
Pod undocumented
208 4 my ($self, $file, $owner) = @_;
209  
210 4 my $actual = getpwuid( ( stat $file )[4] );
211 return {
212 4 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
unless $actual eq $owner 2 2 4
213 info => qq{Owner should be $owner but is $actual for '$file'},
214 } unless $actual eq $owner;
215  
216 return {
217 2 status => 'OK',
218 info => qq{Owner is $owner for '$file'},
219 };
220 }
221  
222 4 ▶sub check_group {
Subroutine check_group: called CC=2 SCAR=6.9
Pod undocumented
223 4 my ($self, $file, $group) = @_;
224  
225 4 my $actual = getgrgid( ( stat $file )[5] );
226 return {
227 4 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
unless $actual eq $group 2 2 4
228 info => qq{Group should be $group but is $actual for '$file'},
229 } unless $actual eq $group;
230  
231 return {
232 2 status => 'OK',
233 info => qq{Group is $group for '$file'},
234 };
235 }
236  
237 2 1;
238  
239 __END__
↑ Summary