lib/Log/Munger/WhichRuleFile.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 86.3% 19 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 93.1% 82 / 88 CC 16 SCAR 14.5
CC
16
Cov
93%
rule_file_location
6.0
available_rule_files
6.0
search_dirs
4.0
CRAP 4.3
« lib/Log/Munger/RulesUsable.pm ↑ Summary lib/Log/Munger.pm »
1 package Log::Munger::WhichRuleFile;
2  
3 34 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 34 ▶use File::ShareDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Log::Munger::WhichRuleFile - Resolves a Log::Munger rule file name to a path.
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 34 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 use Log::Munger::WhichRuleFile;
23  
24 my $file_location = Log::Munger::WhichRuleFile->rule_file_location( 'file' => 'postfix' );
25 if ( !defined($file_location) ) {
26 print "Not found.\n";
27 } else {
28 print 'File Location: ' . $file_location . "\n";
29 }
30  
31 Rule files are looked for in a handful of places, and whichever is found first wins. That
32 ordering is what lets a local file shadow one shipped with the distribution: drop
33 your own C<sshd.yaml> in F</etc/log_munger/rules/> and every reference to C<sshd>
34 picks it up instead, with nothing else needing to change.
35  
36 C<log_munger which_rule_file -f E<lt>nameE<gt>> is this method on the command line,
37 and C<log_munger list -p> shows what every discoverable name currently resolves to.
38  
39 =head1 METHODS
40  
41 =head2 rule_file_location
42  
43 Returns the path a rule file name resolves to.
44  
45 A name beginning with C</>, C<./>, or C<../> is treated as a path and used as
46 given. Anything else is searched for in the directories L</search_dirs> returns,
47 in that order.
48  
49 Each location is tried twice, first for the name as given and then with C<.yaml>
50 appended, so C<sshd> and C<sshd.yaml> both find the same file.
51  
52 - file :: The name or path to locate. Required.
53 Default :: undef
54  
55 Returns the resolved path, or undef if the name turned up nothing anywhere. Dies
56 only if C<file> is undef.
57  
58 my $file_location = Log::Munger::WhichRuleFile->rule_file_location( 'file' => 'postfix' );
59  
60 =cut
61  
62 638 ▶sub rule_file_location {
Subroutine rule_file_location: called CC=6 SCAR=18.0
Pod documented
63 638 my ( $blank, %opts ) = @_;
64  
65 638 ▶ if ( !defined( $opts{'file'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"file"}) 0 638 638
66 0 die('$opts{file} is undef');
67 }
68  
69 # a name that is already a path is used as given rather than searched for
70 638 ▶ if ( $opts{'file'} =~ /^(\/|\.\/|\.\.\/)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"file"} =~ m[^(/|\./|\.\./)]) 13 625 638
71 13 return _existing_rule_file( $opts{'file'} );
72 }
73  
74 625 foreach my $rules_dir ( Log::Munger::WhichRuleFile->search_dirs ) {
75 738 ▶ next if ( !-d $rules_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $rules_dir 112 626 738
76 626 my $found = _existing_rule_file( $rules_dir . '/' . $opts{'file'} );
77 626 ▶ if ( defined($found) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found) 621 5 626
78 621 return $found;
79 }
80 }
81  
82 4 return undef;
83 } ## end sub rule_file_location
84  
85 # Checks whether a base path resolves to a rule file, first as given and then
86 # with .yaml appended, which is what lets "sshd" and "sshd.yaml" find the same
87 # file.
88 #
89 # Args:
90 #
91 # - $base :: The path to try, such as "/etc/log_munger/rules/sshd" or
92 # "./sshd".
93 #
94 # Returns the first of the two spellings that is a file, or undef when neither
95 # is.
96 #
97 # my $found = _existing_rule_file( $rules_dir . '/' . $name );
98 639 ▶sub _existing_rule_file {
Subroutine _existing_rule_file: called CC=3 SCAR=11.0
99 639 my ($base) = @_;
100  
101 639 ▶ if ( -f $base ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $base) { } 13 626 639
elsif (-f $base . '.yaml') { } 621 5 626
102 13 return $base;
103 } elsif ( -f $base . '.yaml' ) {
104 621 return $base . '.yaml';
105 }
106  
107 5 return undef;
108 } ## end sub _existing_rule_file
109  
110 =head2 search_dirs
111  
112 Returns the directories rule files are searched for in, highest precedence
113 first:
114  
115 =over 4
116  
117 =item 1. the directory named by the C<LOG_MUNGER_RULES_DIR> environment
118 variable, when set
119  
120 =item 2. F</etc/log_munger/rules>
121  
122 =item 3. F</usr/local/etc/log_munger/rules>
123  
124 =item 4. the distribution share directory, C<< File::ShareDir::dist_dir('Log-Munger') >>
125 (skipped when the distribution is not installed, e.g. running out of a checkout)
126  
127 =back
128  
129 Takes no arguments. The directories are not checked for existence, so a caller
130 walking them needs to skip any that are missing.
131  
132 my @search_dirs = Log::Munger::WhichRuleFile->search_dirs;
133  
134 =cut
135  
136 628 ▶sub search_dirs {
Subroutine search_dirs: called CC=4 SCAR=13.9
Pod documented
137 628 my @rules_dirs = ( '/etc/log_munger/rules', '/usr/local/etc/log_munger/rules' );
138  
139 # LOG_MUNGER_RULES_DIR jumps the queue, which is what lets a checkout (or
140 # a test run) resolve names against its own share dir instead of whatever
141 # happens to be installed
142 628 ▶ if ( defined( $ENV{'LOG_MUNGER_RULES_DIR'} ) && length( $ENV{'LOG_MUNGER_RULES_DIR'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"LOG_MUNGER_RULES_DIR"} and length $ENV{"LOG_MUNGER_RULES_DIR"}) 573 55 628
Condition66%2/3
defined $ENV{"LOG_MUNGER_RULES_DIR"} and length $ENV{"LOG_MUNGER_RULES_DIR"}
!ll&&!rl&&r
550573
MC/DC50%1/2
defined $ENV{"LOG_MUNGER_RULES_DIR"} and length $ENV{"LOG_MUNGER_RULES_DIR"}
defined $ENV{"LOG_MUNGER_RULES_DIR"} length $ENV{"LOG_MUNGER_RULES_DIR"}
Truth table66%2/3
A and B
A= defined $ENV{"LOG_MUNGER_RULES_DIR"}B= length $ENV{"LOG_MUNGER_RULES_DIR"}
ABresult
0X0
100
111
143 573 unshift( @rules_dirs, $ENV{'LOG_MUNGER_RULES_DIR'} );
144 }
145  
146 # dist_dir dies when the distribution is not installed, which is normal
147 # when running out of a checkout; treat that as one fewer place to look
148 628 my $share_dir;
149 628 eval { $share_dir = File::ShareDir::dist_dir('Log-Munger'); };
150 628 ▶ if ( defined($share_dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $share_dir) 628 0 628
151 628 push( @rules_dirs, $share_dir );
152 }
153  
154 628 return @rules_dirs;
155 } ## end sub search_dirs
156  
157 =head2 available_rule_files
158  
159 Returns every rule file discoverable across the search path, as a hash ref of
160 name to resolved path. The name is the file name with the C<.yaml> suffix
161 stripped, so what comes back is what L</rule_file_location> takes.
162  
163 A name found in more than one directory appears once, resolved to the copy in
164 the earliest directory, since that is the one L</rule_file_location> would
165 return for it.
166  
167 Takes no arguments.
168  
169 my $available = Log::Munger::WhichRuleFile->available_rule_files;
170 # $available = { 'base' => '/etc/log_munger/rules/base.yaml', ... }
171  
172 =cut
173  
174 3 ▶sub available_rule_files {
Subroutine available_rule_files: called CC=6 SCAR=17.9
Pod documented
175 3 my %found; # name => path (first, highest-precedence wins)
176 3 foreach my $rules_dir ( Log::Munger::WhichRuleFile->search_dirs ) {
177 12 ▶ next if ( !-d $rules_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $rules_dir 6 6 12
178 34 ▶ foreach my $file ( sort( glob( $rules_dir . '/*.yaml' ) ) ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
179 540 my ($name) = $file =~ m{([^/]+)\.yaml\z};
180 540 ▶ next if ( !defined($name) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 540 540
181 540 ▶ if ( !exists( $found{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $found{$name}) 270 270 540
182 270 $found{$name} = $file;
183 }
184 }
185 } ## end foreach my $rules_dir ( Log::Munger::WhichRuleFile...)
186  
187 3 return \%found;
188 } ## end sub available_rule_files
« lib/Log/Munger/RulesUsable.pm ↑ Summary lib/Log/Munger.pm »