| 1 | package Log::Log4perl::Layout::PatternLayout::Redact; | ||||||||||||
| 2 | |||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | # Due to circularities in module uses inside Log::Log4perl, loading | ||||||||||||
| 7 | # Log::Log4perl seems to pre-load the whole distribution in the correct order | ||||||||||||
| 8 | # and makes "use base 'Log::Log4perl::Layout::PatternLayout'" work instead of | ||||||||||||
| 9 | # dieing with a "Cannot find new() on Log::Log4perl::Layout::PatternLayout". | ||||||||||||
| 10 | # It's unfortunate, but this is why "use Log::Log4perl" is the first thing in | ||||||||||||
| 11 | # this file. | ||||||||||||
| 12 | 4 | ▶ | use Log::Log4perl; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 4 | ▶ | use base 'Log::Log4perl::Layout::PatternLayout'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 4 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 4 | ▶ | use Carp::Parse::Redact; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 4 | ▶ | use Data::Validate::Type; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 4 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | |||||||||||||
| 22 | =head1 NAME | ||||||||||||
| 23 | |||||||||||||
| 24 | Log::Log4perl::Layout::PatternLayout::Redact - Add stack traces without sensitive information in Log::Log4perl logs. | ||||||||||||
| 25 | |||||||||||||
| 26 | |||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||
| 28 | |||||||||||||
| 29 | C<Log::Log4perl> offers the ability to add stack traces to layouts using I<%T> | ||||||||||||
| 30 | in pattern layouts (see C<Log::Log4perl::Layout::PatternLayout>). | ||||||||||||
| 31 | |||||||||||||
| 32 | However, stack traces contain a list of arguments, and those arguments can | ||||||||||||
| 33 | be sensitive data like passwords or credit card data. This module redacts the | ||||||||||||
| 34 | sensitive information, replacing them with '[redacted]' so that the stack traces | ||||||||||||
| 35 | can be PCI-compliant. | ||||||||||||
| 36 | |||||||||||||
| 37 | |||||||||||||
| 38 | =head1 VERSION | ||||||||||||
| 39 | |||||||||||||
| 40 | Version 1.2.2 | ||||||||||||
| 41 | |||||||||||||
| 42 | =cut | ||||||||||||
| 43 | |||||||||||||
| 44 | 4 | our $VERSION = '1.2.2'; | |||||||||||
| 45 | |||||||||||||
| 46 | 4 | our $SENSITIVE_ARGUMENT_NAMES = undef; | |||||||||||
| 47 | 4 | our $SENSITIVE_REGEXP_PATTERNS = undef; | |||||||||||
| 48 | 4 | our $MESSAGE_REDACTION_CALLBACK = undef; | |||||||||||
| 49 | |||||||||||||
| 50 | =head1 SYNOPSIS | ||||||||||||
| 51 | |||||||||||||
| 52 | use Log::Log4perl::Layout::PatternLayout::Redact; | ||||||||||||
| 53 | |||||||||||||
| 54 | =head2 Redacting stack traces | ||||||||||||
| 55 | |||||||||||||
| 56 | Here's an example of log4perl configuration that outputs a redacted trace | ||||||||||||
| 57 | (use I<%E> instead of I<%T>) : | ||||||||||||
| 58 | |||||||||||||
| 59 | log4perl.logger = WARN, logfile | ||||||||||||
| 60 | log4perl.appender.logfile = Log::Log4perl::Appender::File | ||||||||||||
| 61 | log4perl.appender.logfile.filename = $file_name | ||||||||||||
| 62 | log4perl.appender.logfile.layout = Log::Log4perl::Layout::PatternLayout::Redact | ||||||||||||
| 63 | log4perl.appender.logfile.layout.ConversionPattern = %d %p: (%X{host}) %P %F:%L %M - %m{chomp}%E | ||||||||||||
| 64 | log4perl.appender.logfile.recreate = 1 | ||||||||||||
| 65 | log4perl.appender.logfile.mode = append | ||||||||||||
| 66 | |||||||||||||
| 67 | To set your own list of arguments to redact, rather than use the defaults in C<Carp::Parse::Redact>, | ||||||||||||
| 68 | you need to set a localized version of $SENSITIVE_ARGUMENT_NAMES: | ||||||||||||
| 69 | |||||||||||||
| 70 | $Log::Log4perl::Layout::PatternLayout::Redact::SENSITIVE_ARGUMENT_NAMES = | ||||||||||||
| 71 | [ | ||||||||||||
| 72 | 'password', | ||||||||||||
| 73 | 'luggage_combination', | ||||||||||||
| 74 | 'favorite_pony', | ||||||||||||
| 75 | ]; | ||||||||||||
| 76 | |||||||||||||
| 77 | And hash keys in the stack trace that match these names will have their values replaced with '[redacted]'. | ||||||||||||
| 78 | |||||||||||||
| 79 | To set your own list of regexes to use for redaction, rather than use the | ||||||||||||
| 80 | defaults in C<Carp::Parse::Redact>, you need to set a localized version of | ||||||||||||
| 81 | $SENSITIVE_REGEXP_PATTERNS: | ||||||||||||
| 82 | |||||||||||||
| 83 | $Log::Log4perl::Layout::PatternLayout::Redact::SENSITIVE_REGEXP_PATTERNS = | ||||||||||||
| 84 | [ | ||||||||||||
| 85 | qr/^\d{16}$/, | ||||||||||||
| 86 | ] | ||||||||||||
| 87 | |||||||||||||
| 88 | And any argument in the stack trace that matches one of the regexes provided | ||||||||||||
| 89 | will be replaced with '[redacted]'. | ||||||||||||
| 90 | |||||||||||||
| 91 | Be sure to do the localizations of the package variables after you have | ||||||||||||
| 92 | initialized your logger. | ||||||||||||
| 93 | |||||||||||||
| 94 | =head2 Redacting messages | ||||||||||||
| 95 | |||||||||||||
| 96 | Here's an example of log4perl configuration that outputs a redacted message | ||||||||||||
| 97 | (use I<%e> instead of I<%m>) : | ||||||||||||
| 98 | |||||||||||||
| 99 | log4perl.logger = WARN, logfile | ||||||||||||
| 100 | log4perl.appender.logfile = Log::Log4perl::Appender::File | ||||||||||||
| 101 | log4perl.appender.logfile.filename = $file_name | ||||||||||||
| 102 | log4perl.appender.logfile.layout = Log::Log4perl::Layout::PatternLayout::Redact | ||||||||||||
| 103 | log4perl.appender.logfile.layout.ConversionPattern = %d %p: (%X{host}) %P %F:%L %M - %e | ||||||||||||
| 104 | log4perl.appender.logfile.recreate = 1 | ||||||||||||
| 105 | log4perl.appender.logfile.mode = append | ||||||||||||
| 106 | |||||||||||||
| 107 | To redact the message, you will need to write your own redaction subroutine as | ||||||||||||
| 108 | follows: | ||||||||||||
| 109 | |||||||||||||
| 110 | $Log::Log4perl::Layout::PatternLayout::Redact::MESSAGE_REDACTION_CALLBACK = sub | ||||||||||||
| 111 | { | ||||||||||||
| 112 | my ( $message ) = @_; | ||||||||||||
| 113 | |||||||||||||
| 114 | # Do replacements on the messages to redact sensitive information. | ||||||||||||
| 115 | $message =~ s/(password=")[^"]+(")/$1\[redacted\]$2/g; | ||||||||||||
| 116 | |||||||||||||
| 117 | return $message; | ||||||||||||
| 118 | }; | ||||||||||||
| 119 | |||||||||||||
| 120 | Be sure to do the localizations of the package variable after you have | ||||||||||||
| 121 | initialized your logger. | ||||||||||||
| 122 | |||||||||||||
| 123 | =cut | ||||||||||||
| 124 | |||||||||||||
| 125 | # Add '%E' to the list of options available for the Log4perl layout. | ||||||||||||
| 126 | # This offers a redacted stack trace. | ||||||||||||
| 127 | Log::Log4perl::Layout::PatternLayout::add_global_cspec( | ||||||||||||
| 128 | 'E', | ||||||||||||
| 129 | sub | ||||||||||||
| 130 | 1 | ▶ | { | ||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||
| 131 | 1 | my $trace = Carp::longmess(); | |||||||||||
| 132 | 1 | chomp( $trace ); | |||||||||||
| 133 | |||||||||||||
| 134 | 1 | my $redacted_stack_trace = Carp::Parse::Redact::parse_stack_trace( | |||||||||||
| 135 | $trace, | ||||||||||||
| 136 | sensitive_argument_names => $SENSITIVE_ARGUMENT_NAMES, | ||||||||||||
| 137 | sensitive_regexp_patterns => $SENSITIVE_REGEXP_PATTERNS, | ||||||||||||
| 138 | ); | ||||||||||||
| 139 | |||||||||||||
| 140 | # For each line of the stack trace, replace the original arguments with the | ||||||||||||
| 141 | # newly redacted ones. | ||||||||||||
| 142 | 1 | my $lines = []; | |||||||||||
| 143 | 1 | my $bubbled_to_log4perl = 0; | |||||||||||
| 144 | 1 | ▶ | foreach my $caller_information ( @{ $redacted_stack_trace || [] } ) | ||||||||||
|
Condition50%1/2
$redacted_stack_trace || []
MC/DC0%0/1
$redacted_stack_trace || []
$redacted_stack_trace
Truth table50%1/2
A
A= $redacted_stack_trace
| |||||||||||||
| 145 | { | ||||||||||||
| 146 | # This caller is inside Log::Log4perl, skip it. | ||||||||||||
| 147 | 7 | ▶ | if ( $caller_information->get_line() =~ /^\s*Log::Log4perl/ ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | { | ||||||||||||
| 149 | # But not after indicating that we've bubbled up to Log::Log4perl. | ||||||||||||
| 150 | 3 | ▶ | $bubbled_to_log4perl = 1 if !$bubbled_to_log4perl; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 151 | 3 | next; | |||||||||||
| 152 | } | ||||||||||||
| 153 | # This caller is below Log::Log4perl, skip it. | ||||||||||||
| 154 | 4 | ▶ | next if !$bubbled_to_log4perl; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 155 | |||||||||||||
| 156 | 3 | push( @$lines, $caller_information->get_redacted_line() ); | |||||||||||
| 157 | } | ||||||||||||
| 158 | |||||||||||||
| 159 | 1 | my $redacted_trace = join( "\n", @$lines ); | |||||||||||
| 160 | |||||||||||||
| 161 | 1 | return "\n" . $redacted_trace; | |||||||||||
| 162 | } | ||||||||||||
| 163 | 4 | ); | |||||||||||
| 164 | |||||||||||||
| 165 | # Add '%e' to the list of options available for the Log4perl layout. | ||||||||||||
| 166 | # This offers a redacted message. | ||||||||||||
| 167 | Log::Log4perl::Layout::PatternLayout::add_global_cspec( | ||||||||||||
| 168 | 'e', | ||||||||||||
| 169 | sub | ||||||||||||
| 170 | 1 | ▶ | { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.8
| |||||||||||||
| 171 | 1 | my ( $self, $message ) = @_; | |||||||||||
| 172 | |||||||||||||
| 173 | 1 | ▶ | return $message | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 174 | if !defined( $MESSAGE_REDACTION_CALLBACK ); | ||||||||||||
| 175 | |||||||||||||
| 176 | 1 | my $redacted_message; | |||||||||||
| 177 | try | ||||||||||||
| 178 | 1 | ▶ | { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||
| 179 | 1 | ▶ | Carp::croak('the message redaction callback is not a valid code reference') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 180 | if !Data::Validate::Type::is_coderef( $MESSAGE_REDACTION_CALLBACK ); | ||||||||||||
| 181 | |||||||||||||
| 182 | 1 | $redacted_message = $MESSAGE_REDACTION_CALLBACK->( $message ); | |||||||||||
| 183 | } | ||||||||||||
| 184 | catch | ||||||||||||
| 185 | 0 | ▶ | { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 186 | 0 | my $error = $_; | |||||||||||
| 187 | 0 | Carp::carp("Failed to redact message: $error"); | |||||||||||
| 188 | 0 | return $message; | |||||||||||
| 189 | 1 | }; | |||||||||||
| 190 | |||||||||||||
| 191 | 1 | return $redacted_message; | |||||||||||
| 192 | } | ||||||||||||
| 193 | 4 | ); | |||||||||||
| 194 | |||||||||||||
| 195 | |||||||||||||
| 196 | =head1 AUTHOR | ||||||||||||
| 197 | |||||||||||||
| 198 | Kate Kirby, C<< <kate at cpan.org> >>. | ||||||||||||
| 199 | |||||||||||||
| 200 | Guillaume Aubert, C<< <aubertg at cpan.org> >>. | ||||||||||||
| 201 | |||||||||||||
| 202 | |||||||||||||
| 203 | =head1 BUGS | ||||||||||||
| 204 | |||||||||||||
| 205 | Please report any bugs or feature requests to C<bug-log-log4perl-layout-patternlayout-redact at rt.cpan.org>, or through | ||||||||||||
| 206 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log-Log4perl-Layout-PatternLayout-Redact>. | ||||||||||||
| 207 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||||
| 208 | your bug as I make changes. | ||||||||||||
| 209 | |||||||||||||
| 210 | |||||||||||||
| 211 | =head1 SUPPORT | ||||||||||||
| 212 | |||||||||||||
| 213 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 214 | |||||||||||||
| 215 | perldoc Log::Log4perl::Layout::PatternLayout::Redact | ||||||||||||
| 216 | |||||||||||||
| 217 | |||||||||||||
| 218 | You can also look for information at: | ||||||||||||
| 219 | |||||||||||||
| 220 | =over 4 | ||||||||||||
| 221 | |||||||||||||
| 222 | =item * RT: CPAN's request tracker | ||||||||||||
| 223 | |||||||||||||
| 224 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Log4perl-Layout-PatternLayout-Redact> | ||||||||||||
| 225 | |||||||||||||
| 226 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 227 | |||||||||||||
| 228 | L<http://annocpan.org/dist/Log-Log4perl-Layout-PatternLayout-Redact> | ||||||||||||
| 229 | |||||||||||||
| 230 | =item * CPAN Ratings | ||||||||||||
| 231 | |||||||||||||
| 232 | L<http://cpanratings.perl.org/d/Log-Log4perl-Layout-PatternLayout-Redact> | ||||||||||||
| 233 | |||||||||||||
| 234 | =item * Search CPAN | ||||||||||||
| 235 | |||||||||||||
| 236 | L<http://search.cpan.org/dist/Log-Log4perl-Layout-PatternLayout-Redact/> | ||||||||||||
| 237 | |||||||||||||
| 238 | =back | ||||||||||||
| 239 | |||||||||||||
| 240 | |||||||||||||
| 241 | =head1 ACKNOWLEDGEMENTS | ||||||||||||
| 242 | |||||||||||||
| 243 | Thanks to ThinkGeek (L<http://www.thinkgeek.com/>) and its corporate overlords | ||||||||||||
| 244 | at Geeknet (L<http://www.geek.net/>), for footing the bill while we eat pizza | ||||||||||||
| 245 | and write code for them! | ||||||||||||
| 246 | |||||||||||||
| 247 | |||||||||||||
| 248 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 249 | |||||||||||||
| 250 | Copyright 2012 Kate Kirby & Guillaume Aubert. | ||||||||||||
| 251 | |||||||||||||
| 252 | This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation. | ||||||||||||
| 253 | |||||||||||||
| 254 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||||||||||||
| 255 | |||||||||||||
| 256 | You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/ | ||||||||||||
| 257 | |||||||||||||
| 258 | =cut | ||||||||||||
| 259 | |||||||||||||
| 260 | 4 | 1; | |||||||||||