| 1 | 5 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Ekorn::Pipeline::Stages::Taps; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Data::Dumper qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Ref::Util qw(is_coderef is_plain_scalarref); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Scalar::Util qw(openhandle); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Ekorn::Pipeline::Errors qw(croakf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Ekorn::Pipeline::Gather qw(gather); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Ekorn::Pipeline::Stage qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use namespace::clean -except => 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | our @EXPORT = qw(peek count_into); ## no critic (Modules::ProhibitAutomaticExportation) | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | our @EXPORT_OK = (@EXPORT, qw(dump_to)); | |||||||||||||||||||||||||||
| 19 | 5 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Croak at the line that called Ekorn::Pipeline. | ||||||||||||||||||||||||||||
| 22 | 5 | our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # True, dump_to writes secrets as they are, without redacting them. A | ||||||||||||||||||||||||||||
| 25 | # package variable, so that it can be localised, as Data::Dumper's are. | ||||||||||||||||||||||||||||
| 26 | 5 | our $DumpSecrets; ## no critic (Variables::ProhibitPackageVars) | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # What dump_to redacts with, looked for at its first dump: undef until | ||||||||||||||||||||||||||||
| 29 | # then; then Text::Redact::Secrets' redact, '' when the module isn't | ||||||||||||||||||||||||||||
| 30 | # installed, or why it's there but can't be used. | ||||||||||||||||||||||||||||
| 31 | 5 | my $redactor; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Return a stage calling the block given with each value, for its side | ||||||||||||||||||||||||||||
| 34 | # effects, and passing the value on. | ||||||||||||||||||||||||||||
| 35 | 4 | ▶ | sub peek : prototype(&) ($block) { | ||||||||||||||||||||||||||
|
Subroutine
peek: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | croakf 'Expected a code reference' unless is_coderef($block); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 3 | ▶ | return Ekorn::Pipeline::Stage->new(sub ($value) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 3 | $block->($value); | |||||||||||||||||||||||||||
| 39 | 3 | return $value; | |||||||||||||||||||||||||||
| 40 | 3 | }); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Return a stage counting the values into the scalar given, from 0 on | ||||||||||||||||||||||||||||
| 44 | # each run, and passing them on. | ||||||||||||||||||||||||||||
| 45 | 6 | ▶ | sub count_into ($count) { | ||||||||||||||||||||||||||
|
Subroutine
count_into: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 6 | ▶ | croakf 'Expected a reference to a scalar' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | unless is_plain_scalarref($count); | ||||||||||||||||||||||||||||
| 48 | 3 | ▶ | return gather { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 3 | $$count = 0; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||
| 52 | 3 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 3 | $$count++; | |||||||||||||||||||||||||||
| 54 | 3 | return $_[0]; | |||||||||||||||||||||||||||
| 55 | 3 | }; | |||||||||||||||||||||||||||
| 56 | 2 | }; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Return a stage writing each value, with Data::Dumper, to the filehandle | ||||||||||||||||||||||||||||
| 60 | # given, or STDERR, and passing it on, with its secrets redacted when | ||||||||||||||||||||||||||||
| 61 | # Text::Redact::Secrets is installed, unless $DumpSecrets is true. | ||||||||||||||||||||||||||||
| 62 | 37 | ▶ | sub dump_to ($fh = \*STDERR) { | ||||||||||||||||||||||||||
|
Subroutine
dump_to: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 37 | ▶ | croakf 'Expected an open filehandle' unless defined openhandle($fh); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||
| 65 | 34 | ▶ | return Ekorn::Pipeline::Stage->new(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 66 | 34 | my $dump = _dump($_[0]); | |||||||||||||||||||||||||||
| 67 | 34 | ▶ | unless ($DumpSecrets) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 32 | my $redact = _redactor(); | |||||||||||||||||||||||||||
| 69 | 10 | ▶ | $dump = $redact->($dump) if $redact; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 12 | print { $fh } $dump; | |||||||||||||||||||||||||||
| 72 | 12 | return $_[0]; | |||||||||||||||||||||||||||
| 73 | 34 | }); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 43 | ▶ | sub _dump ($value) { | ||||||||||||||||||||||||||
|
Subroutine
_dump: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 43 | local $Data::Dumper::Indent = 1; | |||||||||||||||||||||||||||
| 79 | 43 | local $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||
| 80 | 43 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 81 | 43 | return Data::Dumper::Dumper($value); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Return Text::Redact::Secrets' redact, or nothing when it isn't | ||||||||||||||||||||||||||||
| 85 | # installed; croak when it's there but can't be used. It's looked for | ||||||||||||||||||||||||||||
| 86 | # once, and the answer kept. | ||||||||||||||||||||||||||||
| 87 | 32 | ▶ | sub _redactor () { | ||||||||||||||||||||||||||
|
Subroutine
_redactor: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 88 | 32 | ▶ | $redactor //= _find_redactor(); | ||||||||||||||||||||||||||
|
Condition66%2/3
$redactor //= _find_redactor()
MC/DC0%0/2
$redactor //= _find_redactor()
$redactor
_find_redactor()
Truth table66%2/3
A //= B
A= $redactorB= _find_redactor()
| |||||||||||||||||||||||||||||
| 89 | 32 | ▶ | croakf 'Text::Redact::Secrets %s, so dump_to won\'t write a dump that' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $redactor or $redactor eq ""
MC/DC100%2/2
ref $redactor or $redactor eq ""
ref $redactor
$redactor eq ""
Truth table100%3/3
A or B
A= ref $redactorB= $redactor eq ""
| |||||||||||||||||||||||||||||
| 90 | . ' may hold secrets; local $Ekorn::Pipeline::Stages::Taps::DumpSecrets' | ||||||||||||||||||||||||||||
| 91 | . ' = 1 dumps them without redacting', $redactor | ||||||||||||||||||||||||||||
| 92 | unless ref $redactor || $redactor eq ''; | ||||||||||||||||||||||||||||
| 93 | 10 | ▶ | return ref $redactor ? $redactor : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Load Text::Redact::Secrets, check its version and that it redacts, and | ||||||||||||||||||||||||||||
| 97 | # return its redact, '' when it isn't installed, or why it can't be used. | ||||||||||||||||||||||||||||
| 98 | 15 | ▶ | sub _find_redactor () { | ||||||||||||||||||||||||||
|
Subroutine
_find_redactor: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 99 | 15 | my $loaded = eval { require Text::Redact::Secrets; 1 }; | |||||||||||||||||||||||||||
| 100 | 15 | ▶ | unless ($loaded) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 2 | my $error = $@; | |||||||||||||||||||||||||||
| 102 | 2 | ▶ | return '' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | if $error =~ m( \A Can't\ locate\ Text/Redact/Secrets\.pm\ in\ \@INC )x; | ||||||||||||||||||||||||||||
| 104 | 1 | return 'failed to load: ' . ($error =~ s( \s* \n .* )()xsr); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 13 | my $version = eval { version->parse(Text::Redact::Secrets->VERSION) }; | |||||||||||||||||||||||||||
| 107 | 13 | ▶ | return 'has no version we can read' unless defined $version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 12 | ▶ | return "is $version, not v1.x" if $version < v1.0.0 || $version >= v2.0.0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$version < v1.0.0 or $version >= v2.0.0
MC/DC100%2/2
$version < v1.0.0 or $version >= v2.0.0
$version < v1.0.0
$version >= v2.0.0
Truth table100%3/3
A or B
A= $version < v1.0.0B= $version >= v2.0.0
| |||||||||||||||||||||||||||||
| 109 | 10 | my $redact = Text::Redact::Secrets->can('redact'); | |||||||||||||||||||||||||||
| 110 | 10 | ▶ | return 'has no redact' unless $redact; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 9 | ▶ | return 'failed its self-test' unless _redacts($redact); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 3 | return $redact; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Whether redact takes each of a few made-up secrets out of a dump. Each | ||||||||||||||||||||||||||||
| 116 | # is built from pieces, so that no line holds one whole for a scanner. | ||||||||||||||||||||||||||||
| 117 | 9 | ▶ | sub _redacts ($redact) { | ||||||||||||||||||||||||||
|
Subroutine
_redacts: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 118 | 9 | my $password = join '', 'Zq8vR2mX', 'kP4wL9nT'; | |||||||||||||||||||||||||||
| 119 | 9 | my $key_id = join '', 'AKIA', 'QYLPMN5H', 'HHFPZAM2'; | |||||||||||||||||||||||||||
| 120 | 9 | my $dump = _dump({ | |||||||||||||||||||||||||||
| 121 | password => $password, | ||||||||||||||||||||||||||||
| 122 | dsn => "postgres://app:$password\@db/app", | ||||||||||||||||||||||||||||
| 123 | key_id => $key_id, | ||||||||||||||||||||||||||||
| 124 | }); | ||||||||||||||||||||||||||||
| 125 | 9 | my $redacted = eval { $redact->($dump) }; | |||||||||||||||||||||||||||
| 126 | 9 | ▶ | return 0 if !defined $redacted || ref $redacted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $redacted or ref $redacted
MC/DC100%2/2
not defined $redacted or ref $redacted
not defined $redacted
ref $redacted
Truth table100%3/3
not(A) or B
A= not defined $redactedB= ref $redacted
| |||||||||||||||||||||||||||||
| 127 | 7 | ▶ | return 0 if index($redacted, $password) >= 0 || index($redacted, $key_id) >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
index($redacted, $password) != -1 or index($redacted, $key_id) != -1
MC/DC100%2/2
index($redacted, $password) != -1 or index($redacted, $key_id) != -1
index($redacted, $password) != -1
index($redacted, $key_id) != -1
Truth table100%3/3
A or B
A= index($redacted, $password) != -1B= index($redacted, $key_id) != -1
| |||||||||||||||||||||||||||||
| 128 | 4 | ▶ | return 0 if index($redacted, '*REDACTED*') < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 3 | return 1; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Forget what was found, so the next dump looks again. For the tests. | ||||||||||||||||||||||||||||
| 133 | 30 | ▶ | sub _forget_redactor () { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||
|
Subroutine
_forget_redactor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | 30 | undef $redactor; | |||||||||||||||||||||||||||
| 135 | 30 | return; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 5 | 1; | |||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||