blib/lib/HealthCheck/Diagnostic/FilePermissions.pm

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